diff options
author | Vicent Martà <tanoku@gmail.com> | 2012-05-08 16:06:27 -0700 |
---|---|---|
committer | Vicent Martà <tanoku@gmail.com> | 2012-05-08 16:06:27 -0700 |
commit | e65752bb3251f9a308fd4f9cb3b294c4f7d90783 (patch) | |
tree | fee4deed1d318f16a266fc5ce6d57e539284c070 /src | |
parent | fd5faae3466d3d74fd0601a1dac0ef0eaad48844 (diff) | |
parent | 7e000ab2ece977a028e3f1327de37342bef9f687 (diff) | |
download | libgit2-e65752bb3251f9a308fd4f9cb3b294c4f7d90783.tar.gz |
Merge pull request #677 from arrbee/status-without-head
Add support for diffing index with no HEAD
Diffstat (limited to 'src')
-rw-r--r-- | src/diff.c | 2 | ||||
-rw-r--r-- | src/iterator.c | 49 | ||||
-rw-r--r-- | src/iterator.h | 3 | ||||
-rw-r--r-- | src/status.c | 2 |
4 files changed, 53 insertions, 3 deletions
diff --git a/src/diff.c b/src/diff.c index 524cc9f59..fed22f403 100644 --- a/src/diff.c +++ b/src/diff.c @@ -631,7 +631,7 @@ int git_diff_index_to_tree( { git_iterator *a = NULL, *b = NULL; - assert(repo && old_tree && diff); + assert(repo && diff); if (git_iterator_for_tree(repo, old_tree, &a) < 0 || git_iterator_for_index(repo, &b) < 0) 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; diff --git a/src/iterator.h b/src/iterator.h index aa78c9f29..12eb96bb0 100644 --- a/src/iterator.h +++ b/src/iterator.h @@ -13,6 +13,7 @@ typedef struct git_iterator git_iterator; typedef enum { + GIT_ITERATOR_EMPTY = 0, GIT_ITERATOR_TREE = 1, GIT_ITERATOR_INDEX = 2, GIT_ITERATOR_WORKDIR = 3 @@ -27,6 +28,8 @@ struct git_iterator { void (*free)(git_iterator *); }; +int git_iterator_for_nothing(git_iterator **iter); + int git_iterator_for_tree( git_repository *repo, git_tree *tree, git_iterator **iter); diff --git a/src/status.c b/src/status.c index 546dc863d..d07b0c41c 100644 --- a/src/status.c +++ b/src/status.c @@ -101,7 +101,7 @@ int git_status_foreach_ext( diffopt.flags = diffopt.flags | GIT_DIFF_RECURSE_UNTRACKED_DIRS; /* TODO: support EXCLUDE_SUBMODULES flag */ - if (show != GIT_STATUS_SHOW_WORKDIR_ONLY && head != NULL && + if (show != GIT_STATUS_SHOW_WORKDIR_ONLY && (err = git_diff_index_to_tree(repo, &diffopt, head, &idx2head)) < 0) goto cleanup; |