diff options
author | Russell Belfer <rb@github.com> | 2012-05-09 02:03:59 +0400 |
---|---|---|
committer | Russell Belfer <rb@github.com> | 2012-05-09 02:03:59 +0400 |
commit | 7e000ab2ece977a028e3f1327de37342bef9f687 (patch) | |
tree | 5955c21f0174237dfa0f2b99b9de8016862aeb54 /src/iterator.c | |
parent | 364f51bdca8cd5bb11bb322f8cac1b0d7dfcf686 (diff) |
Add support for diffing index with no HEAD
When a repo is first created, there is no HEAD yet and attempting
to diff files in the index was showing nothing because a tree
iterator could not be constructed. This adds an "empty" iterator
and falls back on that when the head cannot be looked up.
Diffstat (limited to 'src/iterator.c')
-rw-r--r-- | src/iterator.c | 49 |
1 files changed, 48 insertions, 1 deletions
diff --git a/src/iterator.c b/src/iterator.c index 3a3be1755..646990d3f 100644 --- a/src/iterator.c +++ b/src/iterator.c @@ -11,6 +11,48 @@ #include "buffer.h" #include "git2/submodule.h" +static int empty_iterator__no_item( + git_iterator *iter, const git_index_entry **entry) +{ + GIT_UNUSED(iter); + *entry = NULL; + return 0; +} + +static int empty_iterator__at_end(git_iterator *iter) +{ + GIT_UNUSED(iter); + return 1; +} + +static int empty_iterator__noop(git_iterator *iter) +{ + GIT_UNUSED(iter); + return 0; +} + +static void empty_iterator__free(git_iterator *iter) +{ + GIT_UNUSED(iter); +} + +int git_iterator_for_nothing(git_iterator **iter) +{ + git_iterator *i = git__calloc(1, sizeof(git_iterator)); + GITERR_CHECK_ALLOC(i); + + i->type = GIT_ITERATOR_EMPTY; + i->current = empty_iterator__no_item; + i->at_end = empty_iterator__at_end; + i->advance = empty_iterator__no_item; + i->reset = empty_iterator__noop; + i->free = empty_iterator__free; + + *iter = i; + + return 0; +} + typedef struct tree_iterator_frame tree_iterator_frame; struct tree_iterator_frame { tree_iterator_frame *next; @@ -155,7 +197,12 @@ int git_iterator_for_tree( git_repository *repo, git_tree *tree, git_iterator **iter) { int error; - tree_iterator *ti = git__calloc(1, sizeof(tree_iterator)); + tree_iterator *ti; + + if (tree == NULL) + return git_iterator_for_nothing(iter); + + ti = git__calloc(1, sizeof(tree_iterator)); GITERR_CHECK_ALLOC(ti); ti->base.type = GIT_ITERATOR_TREE; |