diff options
author | Vicent Marti <tanoku@gmail.com> | 2013-04-16 19:46:41 +0400 |
---|---|---|
committer | Vicent Marti <tanoku@gmail.com> | 2013-04-16 19:46:41 +0400 |
commit | a50086d174658914d4d6462afbc83b02825b1f5b (patch) | |
tree | e8daa1c7bf678222cf351445179837bed7db3a72 /tests-clar/diff/diff_helpers.c | |
parent | 5b9fac39d8a76b9139667c26a63e6b3f204b3977 (diff) | |
parent | f124ebd457bfbf43de3516629aaba5a279636e04 (diff) |
Merge branch 'development'v0.18.0
Diffstat (limited to 'tests-clar/diff/diff_helpers.c')
-rw-r--r-- | tests-clar/diff/diff_helpers.c | 171 |
1 files changed, 142 insertions, 29 deletions
diff --git a/tests-clar/diff/diff_helpers.c b/tests-clar/diff/diff_helpers.c index 8587be9b1..19c005e2e 100644 --- a/tests-clar/diff/diff_helpers.c +++ b/tests-clar/diff/diff_helpers.c @@ -5,7 +5,7 @@ git_tree *resolve_commit_oid_to_tree( git_repository *repo, const char *partial_oid) { - unsigned int len = (unsigned int)strlen(partial_oid); + size_t len = strlen(partial_oid); git_oid oid; git_object *obj = NULL; git_tree *tree = NULL; @@ -21,38 +21,45 @@ git_tree *resolve_commit_oid_to_tree( return tree; } -int diff_file_fn( - void *cb_data, - git_diff_delta *delta, - float progress) +int diff_file_cb( + const git_diff_delta *delta, + float progress, + void *payload) { - diff_expects *e = cb_data; + diff_expects *e = payload; GIT_UNUSED(progress); - e-> at_least_one_of_them_is_binary = delta->binary; - e->files++; - switch (delta->status) { - case GIT_DELTA_ADDED: e->file_adds++; break; - case GIT_DELTA_DELETED: e->file_dels++; break; - case GIT_DELTA_MODIFIED: e->file_mods++; break; - case GIT_DELTA_IGNORED: e->file_ignored++; break; - case GIT_DELTA_UNTRACKED: e->file_untracked++; break; - case GIT_DELTA_UNMODIFIED: e->file_unmodified++; break; - default: break; - } + + if ((delta->flags & GIT_DIFF_FLAG_BINARY) != 0) + e->files_binary++; + + cl_assert(delta->status <= GIT_DELTA_TYPECHANGE); + + e->file_status[delta->status] += 1; + return 0; } -int diff_hunk_fn( - void *cb_data, - git_diff_delta *delta, - git_diff_range *range, +int diff_print_file_cb( + const git_diff_delta *delta, + float progress, + void *payload) +{ + fprintf(stderr, "%c %s\n", + git_diff_status_char(delta->status), delta->old_file.path); + return diff_file_cb(delta, progress, payload); +} + +int diff_hunk_cb( + const git_diff_delta *delta, + const git_diff_range *range, const char *header, - size_t header_len) + size_t header_len, + void *payload) { - diff_expects *e = cb_data; + diff_expects *e = payload; GIT_UNUSED(delta); GIT_UNUSED(header); @@ -64,15 +71,15 @@ int diff_hunk_fn( return 0; } -int diff_line_fn( - void *cb_data, - git_diff_delta *delta, - git_diff_range *range, +int diff_line_cb( + const git_diff_delta *delta, + const git_diff_range *range, char line_origin, const char *content, - size_t content_len) + size_t content_len, + void *payload) { - diff_expects *e = cb_data; + diff_expects *e = payload; GIT_UNUSED(delta); GIT_UNUSED(range); @@ -85,11 +92,17 @@ int diff_line_fn( e->line_ctxt++; break; case GIT_DIFF_LINE_ADDITION: + e->line_adds++; + break; case GIT_DIFF_LINE_ADD_EOFNL: + /* technically not a line add, but we'll count it as such */ e->line_adds++; break; case GIT_DIFF_LINE_DELETION: + e->line_dels++; + break; case GIT_DIFF_LINE_DEL_EOFNL: + /* technically not a line delete, but we'll count it as such */ e->line_dels++; break; default: @@ -97,3 +110,103 @@ int diff_line_fn( } return 0; } + +int diff_foreach_via_iterator( + git_diff_list *diff, + git_diff_file_cb file_cb, + git_diff_hunk_cb hunk_cb, + git_diff_data_cb line_cb, + void *data) +{ + size_t d, num_d = git_diff_num_deltas(diff); + + for (d = 0; d < num_d; ++d) { + git_diff_patch *patch; + const git_diff_delta *delta; + size_t h, num_h; + + cl_git_pass(git_diff_get_patch(&patch, &delta, diff, d)); + cl_assert(delta); + + /* call file_cb for this file */ + if (file_cb != NULL && file_cb(delta, (float)d / num_d, data) != 0) { + git_diff_patch_free(patch); + goto abort; + } + + /* if there are no changes, then the patch will be NULL */ + if (!patch) { + cl_assert(delta->status == GIT_DELTA_UNMODIFIED || + (delta->flags & GIT_DIFF_FLAG_BINARY) != 0); + continue; + } + + if (!hunk_cb && !line_cb) { + git_diff_patch_free(patch); + continue; + } + + num_h = git_diff_patch_num_hunks(patch); + + for (h = 0; h < num_h; h++) { + const git_diff_range *range; + const char *hdr; + size_t hdr_len, l, num_l; + + cl_git_pass(git_diff_patch_get_hunk( + &range, &hdr, &hdr_len, &num_l, patch, h)); + + if (hunk_cb && hunk_cb(delta, range, hdr, hdr_len, data) != 0) { + git_diff_patch_free(patch); + goto abort; + } + + for (l = 0; l < num_l; ++l) { + char origin; + const char *line; + size_t line_len; + int old_lineno, new_lineno; + + cl_git_pass(git_diff_patch_get_line_in_hunk( + &origin, &line, &line_len, &old_lineno, &new_lineno, + patch, h, l)); + + if (line_cb && + line_cb(delta, range, origin, line, line_len, data) != 0) { + git_diff_patch_free(patch); + goto abort; + } + } + } + + git_diff_patch_free(patch); + } + + return 0; + +abort: + giterr_clear(); + return GIT_EUSER; +} + +static int diff_print_cb( + const git_diff_delta *delta, + const git_diff_range *range, + char line_origin, /**< GIT_DIFF_LINE_... value from above */ + const char *content, + size_t content_len, + void *payload) +{ + GIT_UNUSED(payload); + GIT_UNUSED(delta); + GIT_UNUSED(range); + GIT_UNUSED(line_origin); + GIT_UNUSED(content_len); + fputs(content, (FILE *)payload); + return 0; +} + +void diff_print(FILE *fp, git_diff_list *diff) +{ + cl_git_pass(git_diff_print_patch(diff, diff_print_cb, fp ? fp : stderr)); +} |