diff options
author | yorah <yoram.harmelin@gmail.com> | 2013-01-25 17:35:46 +0100 |
---|---|---|
committer | yorah <yoram.harmelin@gmail.com> | 2013-02-07 20:44:35 +0100 |
commit | 0d64ba48372f95ade17a27f1b11620638ca52d61 (patch) | |
tree | c713abaeb7e3629e221c09f4c9338e0878006e98 /src/diff.c | |
parent | 943700ecbbc2be4ef49c5c31d8e5c49353fd3d84 (diff) | |
download | libgit2-0d64ba48372f95ade17a27f1b11620638ca52d61.tar.gz |
diff: add a notify callback to `git_diff__from_iterators`
The callback will be called for each file, just before the `git_delta_t` gets inserted into the diff list.
When the callback:
- returns < 0, the diff process will be aborted
- returns > 0, the delta will not be inserted into the diff list, but the diff process continues
- returns 0, the delta is inserted into the diff list, and the diff process continues
Diffstat (limited to 'src/diff.c')
-rw-r--r-- | src/diff.c | 44 |
1 files changed, 34 insertions, 10 deletions
diff --git a/src/diff.c b/src/diff.c index 3e8028551..d9bc32a37 100644 --- a/src/diff.c +++ b/src/diff.c @@ -41,6 +41,18 @@ static git_diff_delta *diff_delta__alloc( return delta; } +static int diff_notify( + const git_diff_list *diff, + const git_diff_delta *delta, + const char *matched_pathspec) +{ + if (!diff->opts.notify_cb) + return 0; + + return diff->opts.notify_cb( + diff, delta, matched_pathspec, diff->opts.notify_payload); +} + static int diff_delta__from_one( git_diff_list *diff, git_delta_t status, @@ -48,6 +60,7 @@ static int diff_delta__from_one( { git_diff_delta *delta; const char *matched_pathspec; + int notify_res; if (status == GIT_DELTA_IGNORED && (diff->opts.flags & GIT_DIFF_INCLUDE_IGNORED) == 0) @@ -85,12 +98,16 @@ static int diff_delta__from_one( !git_oid_iszero(&delta->new_file.oid)) delta->new_file.flags |= GIT_DIFF_FILE_VALID_OID; - if (git_vector_insert(&diff->deltas, delta) < 0) { + notify_res = diff_notify(diff, delta, matched_pathspec); + + if (notify_res) + git__free(delta); + else if (git_vector_insert(&diff->deltas, delta) < 0) { git__free(delta); return -1; } - return 0; + return notify_res < 0 ? GIT_EUSER : 0; } static int diff_delta__from_two( @@ -100,9 +117,11 @@ static int diff_delta__from_two( uint32_t old_mode, const git_index_entry *new_entry, uint32_t new_mode, - git_oid *new_oid) + git_oid *new_oid, + const char *matched_pathspec) { git_diff_delta *delta; + int notify_res; if (status == GIT_DELTA_UNMODIFIED && (diff->opts.flags & GIT_DIFF_INCLUDE_UNMODIFIED) == 0) @@ -139,12 +158,16 @@ static int diff_delta__from_two( if (new_oid || !git_oid_iszero(&new_entry->oid)) delta->new_file.flags |= GIT_DIFF_FILE_VALID_OID; - if (git_vector_insert(&diff->deltas, delta) < 0) { + notify_res = diff_notify(diff, delta, matched_pathspec); + + if (notify_res) + git__free(delta); + else if (git_vector_insert(&diff->deltas, delta) < 0) { git__free(delta); return -1; } - return 0; + return notify_res < 0 ? GIT_EUSER : 0; } static git_diff_delta *diff_delta__last_for_item( @@ -528,7 +551,7 @@ static int maybe_modified( } return diff_delta__from_two( - diff, status, oitem, omode, nitem, nmode, use_noid); + diff, status, oitem, omode, nitem, nmode, use_noid, matched_pathspec); } static bool entry_is_prefixed( @@ -749,10 +772,11 @@ int git_diff__from_iterators( else { assert(oitem && nitem && cmp == 0); - if (maybe_modified(old_iter, oitem, new_iter, nitem, diff) < 0 || - git_iterator_advance(old_iter, &oitem) < 0 || - git_iterator_advance(new_iter, &nitem) < 0) - goto fail; + if (maybe_modified( + old_iter, oitem, new_iter, nitem, diff) < 0 || + git_iterator_advance(old_iter, &oitem) < 0 || + git_iterator_advance(new_iter, &nitem) < 0) + goto fail; } } |