diff options
author | Russell Belfer <rb@github.com> | 2012-09-24 20:52:34 -0700 |
---|---|---|
committer | Russell Belfer <rb@github.com> | 2012-09-25 16:35:05 -0700 |
commit | 5f69a31f7d706aa5788ad9937391577a66e3c77d (patch) | |
tree | 8201821372d02499f092b774a8fd521478564a7e /tests-clar/diff | |
parent | 9a12a6256efa7da4b4245d0f2b7df6f3b84edabd (diff) | |
download | libgit2-5f69a31f7d706aa5788ad9937391577a66e3c77d.tar.gz |
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.
Diffstat (limited to 'tests-clar/diff')
-rw-r--r-- | tests-clar/diff/diff_helpers.c | 75 | ||||
-rw-r--r-- | tests-clar/diff/diffiter.c | 275 | ||||
-rw-r--r-- | tests-clar/diff/tree.c | 75 | ||||
-rw-r--r-- | tests-clar/diff/workdir.c | 69 |
4 files changed, 299 insertions, 195 deletions
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; } diff --git a/tests-clar/diff/diffiter.c b/tests-clar/diff/diffiter.c index 23071e48b..9e33d91e1 100644 --- a/tests-clar/diff/diffiter.c +++ b/tests-clar/diff/diffiter.c @@ -14,11 +14,16 @@ void test_diff_diffiter__create(void) { git_repository *repo = cl_git_sandbox_init("attr"); git_diff_list *diff; - git_diff_iterator *iter; + size_t d, num_d; cl_git_pass(git_diff_workdir_to_index(repo, NULL, &diff)); - cl_git_pass(git_diff_iterator_new(&iter, diff)); - git_diff_iterator_free(iter); + + num_d = git_diff_num_deltas(diff); + for (d = 0; d < num_d; ++d) { + git_diff_delta *delta; + cl_git_pass(git_diff_get_patch(NULL, &delta, diff, d)); + } + git_diff_list_free(diff); } @@ -26,24 +31,22 @@ void test_diff_diffiter__iterate_files(void) { git_repository *repo = cl_git_sandbox_init("attr"); git_diff_list *diff; - git_diff_iterator *iter; - git_diff_delta *delta; - int error, count = 0; + size_t d, num_d; + int count = 0; cl_git_pass(git_diff_workdir_to_index(repo, NULL, &diff)); - cl_git_pass(git_diff_iterator_new(&iter, diff)); - while ((error = git_diff_iterator_next_file(&delta, iter)) != GIT_ITEROVER) { - cl_assert_equal_i(0, error); + num_d = git_diff_num_deltas(diff); + cl_assert_equal_i(6, num_d); + + for (d = 0; d < num_d; ++d) { + git_diff_delta *delta; + cl_git_pass(git_diff_get_patch(NULL, &delta, diff, d)); cl_assert(delta != NULL); count++; } - - cl_assert_equal_i(GIT_ITEROVER, error); - cl_assert(delta == NULL); cl_assert_equal_i(6, count); - git_diff_iterator_free(iter); git_diff_list_free(diff); } @@ -51,24 +54,22 @@ void test_diff_diffiter__iterate_files_2(void) { git_repository *repo = cl_git_sandbox_init("status"); git_diff_list *diff; - git_diff_iterator *iter; - git_diff_delta *delta; - int error, count = 0; + size_t d, num_d; + int count = 0; cl_git_pass(git_diff_workdir_to_index(repo, NULL, &diff)); - cl_git_pass(git_diff_iterator_new(&iter, diff)); - while ((error = git_diff_iterator_next_file(&delta, iter)) != GIT_ITEROVER) { - cl_assert_equal_i(0, error); + num_d = git_diff_num_deltas(diff); + cl_assert_equal_i(8, num_d); + + for (d = 0; d < num_d; ++d) { + git_diff_delta *delta; + cl_git_pass(git_diff_get_patch(NULL, &delta, diff, d)); cl_assert(delta != NULL); count++; } - - cl_assert_equal_i(GIT_ITEROVER, error); - cl_assert(delta == NULL); cl_assert_equal_i(8, count); - git_diff_iterator_free(iter); git_diff_list_free(diff); } @@ -77,12 +78,8 @@ void test_diff_diffiter__iterate_files_and_hunks(void) git_repository *repo = cl_git_sandbox_init("status"); git_diff_options opts = {0}; git_diff_list *diff = NULL; - git_diff_iterator *iter; - git_diff_delta *delta; - git_diff_range *range; - const char *header; - size_t header_len; - int error, file_count = 0, hunk_count = 0; + size_t d, num_d; + int file_count = 0, hunk_count = 0; opts.context_lines = 3; opts.interhunk_lines = 1; @@ -90,28 +87,42 @@ void test_diff_diffiter__iterate_files_and_hunks(void) cl_git_pass(git_diff_workdir_to_index(repo, &opts, &diff)); - cl_git_pass(git_diff_iterator_new(&iter, diff)); + num_d = git_diff_num_deltas(diff); + + for (d = 0; d < num_d; ++d) { + git_diff_patch *patch; + git_diff_delta *delta; + size_t h, num_h; + + cl_git_pass(git_diff_get_patch(&patch, &delta, diff, d)); - while ((error = git_diff_iterator_next_file(&delta, iter)) != GIT_ITEROVER) { - cl_assert_equal_i(0, error); cl_assert(delta); + cl_assert(patch); file_count++; - while ((error = git_diff_iterator_next_hunk( - &range, &header, &header_len, iter)) != GIT_ITEROVER) { - cl_assert_equal_i(0, error); + num_h = git_diff_patch_num_hunks(patch); + + for (h = 0; h < num_h; h++) { + git_diff_range *range; + const char *header; + size_t header_len, num_l; + + cl_git_pass(git_diff_patch_get_hunk( + &range, &header, &header_len, &num_l, patch, h)); + cl_assert(range); + cl_assert(header); + hunk_count++; } + + git_diff_patch_free(patch); } - cl_assert_equal_i(GIT_ITEROVER, error); - cl_assert(delta == NULL); cl_assert_equal_i(13, file_count); cl_assert_equal_i(8, hunk_count); - git_diff_iterator_free(iter); git_diff_list_free(diff); } @@ -120,45 +131,42 @@ void test_diff_diffiter__max_size_threshold(void) git_repository *repo = cl_git_sandbox_init("status"); git_diff_options opts = {0}; git_diff_list *diff = NULL; - git_diff_iterator *iter; - git_diff_delta *delta; - int error, file_count = 0, binary_count = 0, hunk_count = 0; + int file_count = 0, binary_count = 0, hunk_count = 0; + size_t d, num_d; opts.context_lines = 3; opts.interhunk_lines = 1; opts.flags |= GIT_DIFF_INCLUDE_IGNORED | GIT_DIFF_INCLUDE_UNTRACKED; cl_git_pass(git_diff_workdir_to_index(repo, &opts, &diff)); - cl_git_pass(git_diff_iterator_new(&iter, diff)); + num_d = git_diff_num_deltas(diff); - while ((error = git_diff_iterator_next_file(&delta, iter)) != GIT_ITEROVER) { - cl_assert_equal_i(0, error); + for (d = 0; d < num_d; ++d) { + git_diff_patch *patch; + git_diff_delta *delta; + + cl_git_pass(git_diff_get_patch(&patch, &delta, diff, d)); cl_assert(delta); + cl_assert(patch); file_count++; - - hunk_count += git_diff_iterator_num_hunks_in_file(iter); + hunk_count += git_diff_patch_num_hunks(patch); assert(delta->binary == 0 || delta->binary == 1); - binary_count += delta->binary; - } - cl_assert_equal_i(GIT_ITEROVER, error); - cl_assert(delta == NULL); + git_diff_patch_free(patch); + } cl_assert_equal_i(13, file_count); cl_assert_equal_i(0, binary_count); cl_assert_equal_i(8, hunk_count); - git_diff_iterator_free(iter); git_diff_list_free(diff); /* try again with low file size threshold */ - file_count = 0; - binary_count = 0; - hunk_count = 0; + file_count = binary_count = hunk_count = 0; opts.context_lines = 3; opts.interhunk_lines = 1; @@ -166,36 +174,169 @@ void test_diff_diffiter__max_size_threshold(void) opts.max_size = 50; /* treat anything over 50 bytes as binary! */ cl_git_pass(git_diff_workdir_to_index(repo, &opts, &diff)); - cl_git_pass(git_diff_iterator_new(&iter, diff)); + num_d = git_diff_num_deltas(diff); - while ((error = git_diff_iterator_next_file(&delta, iter)) != GIT_ITEROVER) { - cl_assert_equal_i(0, error); - cl_assert(delta); + for (d = 0; d < num_d; ++d) { + git_diff_patch *patch; + git_diff_delta *delta; - file_count++; + cl_git_pass(git_diff_get_patch(&patch, &delta, diff, d)); - hunk_count += git_diff_iterator_num_hunks_in_file(iter); + file_count++; + hunk_count += git_diff_patch_num_hunks(patch); assert(delta->binary == 0 || delta->binary == 1); - binary_count += delta->binary; - } - cl_assert_equal_i(GIT_ITEROVER, error); - cl_assert(delta == NULL); + git_diff_patch_free(patch); + } cl_assert_equal_i(13, file_count); - /* Three files are over the 50 byte threshold: * - staged_changes_file_deleted * - staged_changes_modified_file * - staged_new_file_modified_file */ cl_assert_equal_i(3, binary_count); - cl_assert_equal_i(5, hunk_count); - git_diff_iterator_free(iter); git_diff_list_free(diff); +} + + +void test_diff_diffiter__iterate_all(void) +{ + git_repository *repo = cl_git_sandbox_init("status"); + git_diff_options opts = {0}; + git_diff_list *diff = NULL; + diff_expects exp = {0}; + size_t d, num_d; + + opts.context_lines = 3; + opts.interhunk_lines = 1; + opts.flags |= GIT_DIFF_INCLUDE_IGNORED | GIT_DIFF_INCLUDE_UNTRACKED; + + cl_git_pass(git_diff_workdir_to_index(repo, &opts, &diff)); + + num_d = git_diff_num_deltas(diff); + for (d = 0; d < num_d; ++d) { + git_diff_patch *patch; + git_diff_delta *delta; + size_t h, num_h; + + cl_git_pass(git_diff_get_patch(&patch, &delta, diff, d)); + cl_assert(patch && delta); + exp.files++; + + num_h = git_diff_patch_num_hunks(patch); + for (h = 0; h < num_h; h++) { + git_diff_range *range; + const char *header; + size_t header_len, l, num_l; + + cl_git_pass(git_diff_patch_get_hunk( + &range, &header, &header_len, &num_l, patch, h)); + cl_assert(range && header); + exp.hunks++; + + for (l = 0; l < num_l; ++l) { + char origin; + const char *content; + size_t content_len; + + cl_git_pass(git_diff_patch_get_line_in_hunk( + &origin, &content, &content_len, NULL, NULL, patch, h, l)); + cl_assert(content); + exp.lines++; + } + } + + git_diff_patch_free(patch); + } + + cl_assert_equal_i(13, exp.files); + cl_assert_equal_i(8, exp.hunks); + cl_assert_equal_i(13, exp.lines); + + git_diff_list_free(diff); +} + +static void iterate_over_patch(git_diff_patch *patch, diff_expects *exp) +{ + size_t h, num_h = git_diff_patch_num_hunks(patch); + + exp->files++; + exp->hunks += num_h; + + /* let's iterate in reverse, just because we can! */ + for (h = 1; h <= num_h; ++h) + exp->lines += git_diff_patch_num_lines_in_hunk(patch, num_h - h); +} + +#define PATCH_CACHE 5 + +void test_diff_diffiter__iterate_randomly_while_saving_state(void) +{ + git_repository *repo = cl_git_sandbox_init("status"); + git_diff_options opts = {0}; + git_diff_list *diff = NULL; + diff_expects exp = {0}; + git_diff_patch *patches[PATCH_CACHE]; + size_t p, d, num_d; + + memset(patches, 0, sizeof(patches)); + + opts.context_lines = 3; + opts.interhunk_lines = 1; + opts.flags |= GIT_DIFF_INCLUDE_IGNORED | GIT_DIFF_INCLUDE_UNTRACKED; + + cl_git_pass(git_diff_workdir_to_index(repo, &opts, &diff)); + + num_d = git_diff_num_deltas(diff); + + /* To make sure that references counts work for diff and patch objects, + * this generates patches and randomly caches them. Only when the patch + * is removed from the cache are hunks and lines counted. At the end, + * there are still patches in the cache, so free the diff and try to + * process remaining patches after the diff is freed. + */ + + srand(121212); + p = rand() % PATCH_CACHE; + + for (d = 0; d < num_d; ++d) { + /* take old patch */ + git_diff_patch *patch = patches[p]; + patches[p] = NULL; + + /* cache new patch */ + cl_git_pass(git_diff_get_patch(&patches[p], NULL, diff, d)); + cl_assert(patches[p] != NULL); + + /* process old patch if non-NULL */ + if (patch != NULL) { + iterate_over_patch(patch, &exp); + git_diff_patch_free(patch); + } + + p = rand() % PATCH_CACHE; + } + + /* free diff list now - refcounts should keep things safe */ + git_diff_list_free(diff); + + /* process remaining unprocessed patches */ + for (p = 0; p < PATCH_CACHE; p++) { + git_diff_patch *patch = patches[p]; + + if (patch != NULL) { + iterate_over_patch(patch, &exp); + git_diff_patch_free(patch); + } + } + /* hopefully it all still added up right */ + cl_assert_equal_i(13, exp.files); + cl_assert_equal_i(8, exp.hunks); + cl_assert_equal_i(13, exp.lines); } diff --git a/tests-clar/diff/tree.c b/tests-clar/diff/tree.c index f5e72cadc..78ab093ed 100644 --- a/tests-clar/diff/tree.c +++ b/tests-clar/diff/tree.c @@ -264,10 +264,12 @@ void test_diff_tree__larger_hunks(void) git_tree *a, *b; git_diff_options opts = {0}; git_diff_list *diff = NULL; - git_diff_iterator *iter = NULL; + size_t d, num_d, h, num_h, l, num_l, header_len, line_len; git_diff_delta *delta; - diff_expects exp; - int error, num_files = 0; + git_diff_patch *patch; + git_diff_range *range; + const char *header, *line; + char origin; g_repo = cl_git_sandbox_init("diff"); @@ -277,61 +279,38 @@ void test_diff_tree__larger_hunks(void) opts.context_lines = 1; opts.interhunk_lines = 0; - memset(&exp, 0, sizeof(exp)); - cl_git_pass(git_diff_tree_to_tree(g_repo, &opts, a, b, &diff)); - cl_git_pass(git_diff_iterator_new(&iter, diff)); - /* this should be exact */ - cl_assert(git_diff_iterator_progress(iter) == 0.0f); + num_d = git_diff_num_deltas(diff); + for (d = 0; d < num_d; ++d) { + cl_git_pass(git_diff_get_patch(&patch, &delta, diff, d)); + cl_assert(patch && delta); - /* You wouldn't actually structure an iterator loop this way, but - * I have here for testing purposes of the return value - */ - while (!(error = git_diff_iterator_next_file(&delta, iter))) { - git_diff_range *range; - const char *header; - size_t header_len; - int actual_hunks = 0, num_hunks; - float expected_progress; - - num_files++; - - expected_progress = (float)num_files / 2.0f; - cl_assert(expected_progress == git_diff_iterator_progress(iter)); - - num_hunks = git_diff_iterator_num_hunks_in_file(iter); - - while (!(error = git_diff_iterator_next_hunk( - &range, &header, &header_len, iter))) - { - int actual_lines = 0; - int num_lines = git_diff_iterator_num_lines_in_hunk(iter); - char origin; - const char *line; - size_t line_len; - - while (!(error = git_diff_iterator_next_line( - &origin, &line, &line_len, iter))) - { - actual_lines++; - } + num_h = git_diff_patch_num_hunks(patch); + for (h = 0; h < num_h; h++) { + cl_git_pass(git_diff_patch_get_hunk( + &range, &header, &header_len, &num_l, patch, h)); - cl_assert_equal_i(GIT_ITEROVER, error); - cl_assert_equal_i(actual_lines, num_lines); + for (l = 0; l < num_l; ++l) { + cl_git_pass(git_diff_patch_get_line_in_hunk( + &origin, &line, &line_len, NULL, NULL, patch, h, l)); + cl_assert(line); + } - actual_hunks++; + cl_git_fail(git_diff_patch_get_line_in_hunk( + &origin, &line, &line_len, NULL, NULL, patch, h, num_l)); } - cl_assert_equal_i(GIT_ITEROVER, error); - cl_assert_equal_i(actual_hunks, num_hunks); + cl_git_fail(git_diff_patch_get_hunk( + &range, &header, &header_len, &num_l, patch, num_h)); + + git_diff_patch_free(patch); } - cl_assert_equal_i(GIT_ITEROVER, error); - cl_assert_equal_i(2, num_files); - cl_assert(git_diff_iterator_progress(iter) == 1.0f); + cl_git_fail(git_diff_get_patch(&patch, &delta, diff, num_d)); + + cl_assert_equal_i(2, num_d); - git_diff_iterator_free(iter); git_diff_list_free(diff); diff = NULL; diff --git a/tests-clar/diff/workdir.c b/tests-clar/diff/workdir.c index 40a888544..612e44303 100644 --- a/tests-clar/diff/workdir.c +++ b/tests-clar/diff/workdir.c @@ -678,7 +678,7 @@ void test_diff_workdir__larger_hunks(void) const char *b_commit = "7a9e0b02e63179929fed24f0a3e0f19168114d10"; git_tree *a, *b; git_diff_options opts = {0}; - int i, error; + size_t i, d, num_d, h, num_h, l, num_l, header_len, line_len; g_repo = cl_git_sandbox_init("diff"); @@ -690,9 +690,10 @@ void test_diff_workdir__larger_hunks(void) for (i = 0; i <= 2; ++i) { git_diff_list *diff = NULL; - git_diff_iterator *iter = NULL; - git_diff_delta *delta; - int num_files = 0; + git_diff_patch *patch; + git_diff_range *range; + const char *header, *line; + char origin; /* okay, this is a bit silly, but oh well */ switch (i) { @@ -707,54 +708,36 @@ void test_diff_workdir__larger_hunks(void) break; } - cl_git_pass(git_diff_iterator_new(&iter, diff)); + num_d = git_diff_num_deltas(diff); + cl_assert_equal_i(2, (int)num_d); - cl_assert(git_diff_iterator_progress(iter) == 0.0f); + for (d = 0; d < num_d; ++d) { + cl_git_pass(git_diff_get_patch(&patch, NULL, diff, d)); + cl_assert(patch); - while (!(error = git_diff_iterator_next_file(&delta, iter))) { - git_diff_range *range; - const char *header; - size_t header_len; - int actual_hunks = 0, num_hunks; - float expected_progress; + num_h = git_diff_patch_num_hunks(patch); + for (h = 0; h < num_h; h++) { + cl_git_pass(git_diff_patch_get_hunk( + &range, &header, &header_len, &num_l, patch, h)); - num_files++; - - expected_progress = (float)num_files / 2.0f; - cl_assert(expected_progress == git_diff_iterator_progress(iter)); - - num_hunks = git_diff_iterator_num_hunks_in_file(iter); - - while (!(error = git_diff_iterator_next_hunk( - &range, &header, &header_len, iter))) - { - int actual_lines = 0; - int num_lines = git_diff_iterator_num_lines_in_hunk(iter); - char origin; - const char *line; - size_t line_len; - - while (!(error = git_diff_iterator_next_line( - &origin, &line, &line_len, iter))) - { - actual_lines++; + for (l = 0; l < num_l; ++l) { + cl_git_pass(git_diff_patch_get_line_in_hunk( + &origin, &line, &line_len, NULL, NULL, patch, h, l)); + cl_assert(line); } - cl_assert_equal_i(GIT_ITEROVER, error); - cl_assert_equal_i(actual_lines, num_lines); - - actual_hunks++; + /* confirm fail after the last item */ + cl_git_fail(git_diff_patch_get_line_in_hunk( + &origin, &line, &line_len, NULL, NULL, patch, h, num_l)); } - cl_assert_equal_i(GIT_ITEROVER, error); - cl_assert_equal_i(actual_hunks, num_hunks); - } + /* confirm fail after the last item */ + cl_git_fail(git_diff_patch_get_hunk( + &range, &header, &header_len, &num_l, patch, num_h)); - cl_assert_equal_i(GIT_ITEROVER, error); - cl_assert_equal_i(2, num_files); - cl_assert(git_diff_iterator_progress(iter) == 1.0f); + git_diff_patch_free(patch); + } - git_diff_iterator_free(iter); git_diff_list_free(diff); } |