From 5f69a31f7d706aa5788ad9937391577a66e3c77d Mon Sep 17 00:00:00 2001 From: Russell Belfer Date: Mon, 24 Sep 2012 20:52:34 -0700 Subject: Initial implementation of new diff patch API Replacing the `git_iterator` object, this creates a simple API for accessing the "patch" for any file pair in a diff list and then gives indexed access to the hunks in the patch and the lines in the hunk. This is the initial implementation of this revised API - it is still broken, but at least builds cleanly. --- tests-clar/diff/diff_helpers.c | 75 +++++++++++++++++++++--------------------- 1 file changed, 38 insertions(+), 37 deletions(-) (limited to 'tests-clar/diff/diff_helpers.c') diff --git a/tests-clar/diff/diff_helpers.c b/tests-clar/diff/diff_helpers.c index 767b34392..1c0435975 100644 --- a/tests-clar/diff/diff_helpers.c +++ b/tests-clar/diff/diff_helpers.c @@ -112,64 +112,65 @@ int diff_foreach_via_iterator( git_diff_hunk_fn hunk_cb, git_diff_data_fn line_cb) { - int error; - git_diff_iterator *iter; - git_diff_delta *delta; + size_t d, num_d = git_diff_num_deltas(diff); - if ((error = git_diff_iterator_new(&iter, diff)) < 0) - return error; + for (d = 0; d < num_d; ++d) { + git_diff_patch *patch; + git_diff_delta *delta; + size_t h, num_h; - while (!(error = git_diff_iterator_next_file(&delta, iter))) { - git_diff_range *range; - const char *hdr; - size_t hdr_len; - float progress = git_diff_iterator_progress(iter); + cl_git_pass(git_diff_get_patch(&patch, &delta, diff, d)); + cl_assert(delta && patch); /* call file_cb for this file */ - if (file_cb != NULL && file_cb(data, delta, progress) != 0) + if (file_cb != NULL && file_cb(data, delta, (float)d / num_d) != 0) { + git_diff_patch_free(patch); goto abort; + } - if (!hunk_cb && !line_cb) + if (!hunk_cb && !line_cb) { + git_diff_patch_free(patch); continue; + } + + num_h = git_diff_patch_num_hunks(patch); - while (!(error = git_diff_iterator_next_hunk( - &range, &hdr, &hdr_len, iter))) { - char origin; - const char *line; - size_t line_len; + for (h = 0; h < num_h; h++) { + git_diff_range *range; + const char *hdr; + size_t hdr_len, l, num_l; - if (hunk_cb && hunk_cb(data, delta, range, hdr, hdr_len) != 0) + cl_git_pass(git_diff_patch_get_hunk( + &range, &hdr, &hdr_len, &num_l, patch, h)); + + if (hunk_cb && hunk_cb(data, delta, range, hdr, hdr_len) != 0) { + git_diff_patch_free(patch); goto abort; + } - if (!line_cb) - continue; + for (l = 0; l < num_l; ++l) { + char origin; + const char *line; + size_t line_len; + int old_lineno, new_lineno; - while (!(error = git_diff_iterator_next_line( - &origin, &line, &line_len, iter))) { + cl_git_pass(git_diff_patch_get_line_in_hunk( + &origin, &line, &line_len, &old_lineno, &new_lineno, + patch, h, l)); - if (line_cb(data, delta, range, origin, line, line_len) != 0) + if (line_cb(data, delta, range, origin, line, line_len) != 0) { + git_diff_patch_free(patch); goto abort; + } } - - if (error && error != GIT_ITEROVER) - goto done; } - if (error && error != GIT_ITEROVER) - goto done; + git_diff_patch_free(patch); } -done: - git_diff_iterator_free(iter); - - if (error == GIT_ITEROVER) - error = 0; - - return error; + return 0; abort: - git_diff_iterator_free(iter); giterr_clear(); - return GIT_EUSER; } -- cgit v1.2.1 From 642863086575a61b3ed0bbbe909f4f07d87ff9db Mon Sep 17 00:00:00 2001 From: Russell Belfer Date: Tue, 25 Sep 2012 10:48:50 -0700 Subject: Fix bugs in new diff patch code This fixes all the bugs in the new diff patch code. The only really interesting one is that when we merge two diffs, we now have to actually exclude diff delta records that are not supposed to be tracked, as opposed to before where they could be included because they would be skipped silently by `git_diff_foreach()`. Other than that, there are just minor errors. --- tests-clar/diff/diff_helpers.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'tests-clar/diff/diff_helpers.c') diff --git a/tests-clar/diff/diff_helpers.c b/tests-clar/diff/diff_helpers.c index 1c0435975..0c4721897 100644 --- a/tests-clar/diff/diff_helpers.c +++ b/tests-clar/diff/diff_helpers.c @@ -120,7 +120,7 @@ int diff_foreach_via_iterator( size_t h, num_h; cl_git_pass(git_diff_get_patch(&patch, &delta, diff, d)); - cl_assert(delta && patch); + cl_assert(delta); /* call file_cb for this file */ if (file_cb != NULL && file_cb(data, delta, (float)d / num_d) != 0) { @@ -128,6 +128,12 @@ int diff_foreach_via_iterator( goto abort; } + /* if there are no changes, then the patch will be NULL */ + if (!patch) { + cl_assert(delta->status == GIT_DELTA_UNMODIFIED || delta->binary == 1); + continue; + } + if (!hunk_cb && !line_cb) { git_diff_patch_free(patch); continue; -- cgit v1.2.1 From bae957b95d59a840df72a725b06f00635471cfd8 Mon Sep 17 00:00:00 2001 From: Russell Belfer Date: Tue, 25 Sep 2012 16:31:46 -0700 Subject: Add const to all shared pointers in diff API There are a lot of places where the diff API gives the user access to internal data structures and many of these were being exposed through non-const pointers. This replaces them all with const pointers for any object that the user can access but is still owned internally to the git_diff_list or git_diff_patch objects. This will probably break some bindings... Sorry! --- tests-clar/diff/diff_helpers.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'tests-clar/diff/diff_helpers.c') diff --git a/tests-clar/diff/diff_helpers.c b/tests-clar/diff/diff_helpers.c index 0c4721897..b4c68769e 100644 --- a/tests-clar/diff/diff_helpers.c +++ b/tests-clar/diff/diff_helpers.c @@ -23,7 +23,7 @@ git_tree *resolve_commit_oid_to_tree( int diff_file_fn( void *cb_data, - git_diff_delta *delta, + const git_diff_delta *delta, float progress) { diff_expects *e = cb_data; @@ -48,8 +48,8 @@ int diff_file_fn( int diff_hunk_fn( void *cb_data, - git_diff_delta *delta, - git_diff_range *range, + const git_diff_delta *delta, + const git_diff_range *range, const char *header, size_t header_len) { @@ -67,8 +67,8 @@ int diff_hunk_fn( int diff_line_fn( void *cb_data, - git_diff_delta *delta, - git_diff_range *range, + const git_diff_delta *delta, + const git_diff_range *range, char line_origin, const char *content, size_t content_len) @@ -116,7 +116,7 @@ int diff_foreach_via_iterator( for (d = 0; d < num_d; ++d) { git_diff_patch *patch; - git_diff_delta *delta; + const git_diff_delta *delta; size_t h, num_h; cl_git_pass(git_diff_get_patch(&patch, &delta, diff, d)); @@ -142,7 +142,7 @@ int diff_foreach_via_iterator( num_h = git_diff_patch_num_hunks(patch); for (h = 0; h < num_h; h++) { - git_diff_range *range; + const git_diff_range *range; const char *hdr; size_t hdr_len, l, num_l; -- cgit v1.2.1