diff options
author | Vicent Marti <tanoku@gmail.com> | 2013-04-16 17:46:41 +0200 |
---|---|---|
committer | Vicent Marti <tanoku@gmail.com> | 2013-04-16 17:46:41 +0200 |
commit | a50086d174658914d4d6462afbc83b02825b1f5b (patch) | |
tree | e8daa1c7bf678222cf351445179837bed7db3a72 /tests-clar/diff/tree.c | |
parent | 5b9fac39d8a76b9139667c26a63e6b3f204b3977 (diff) | |
parent | f124ebd457bfbf43de3516629aaba5a279636e04 (diff) | |
download | libgit2-a50086d174658914d4d6462afbc83b02825b1f5b.tar.gz |
Merge branch 'development'v0.18.0
Diffstat (limited to 'tests-clar/diff/tree.c')
-rw-r--r-- | tests-clar/diff/tree.c | 408 |
1 files changed, 327 insertions, 81 deletions
diff --git a/tests-clar/diff/tree.c b/tests-clar/diff/tree.c index b932fa10e..850feefde 100644 --- a/tests-clar/diff/tree.c +++ b/tests-clar/diff/tree.c @@ -2,14 +2,32 @@ #include "diff_helpers.h" static git_repository *g_repo = NULL; +static git_diff_options opts; +static git_diff_list *diff; +static git_tree *a, *b; +static diff_expects expect; void test_diff_tree__initialize(void) { + GIT_INIT_STRUCTURE(&opts, GIT_DIFF_OPTIONS_VERSION); + /* The default context lines is set by _INIT which we can't use here */ + opts.context_lines = 3; + + memset(&expect, 0, sizeof(expect)); + + diff = NULL; + a = NULL; + b = NULL; } void test_diff_tree__cleanup(void) { + git_diff_list_free(diff); + git_tree_free(a); + git_tree_free(b); + cl_git_sandbox_cleanup(); + } void test_diff_tree__0(void) @@ -18,10 +36,7 @@ void test_diff_tree__0(void) const char *a_commit = "605812a"; const char *b_commit = "370fe9ec22"; const char *c_commit = "f5b0af1fb4f5c"; - git_tree *a, *b, *c; - git_diff_options opts = {0}; - git_diff_list *diff = NULL; - diff_expects exp; + git_tree *c; g_repo = cl_git_sandbox_init("attr"); @@ -32,51 +47,46 @@ void test_diff_tree__0(void) opts.context_lines = 1; opts.interhunk_lines = 1; - memset(&exp, 0, sizeof(exp)); - cl_git_pass(git_diff_tree_to_tree(g_repo, &opts, a, b, &diff)); + cl_git_pass(git_diff_tree_to_tree(&diff, g_repo, a, b, &opts)); cl_git_pass(git_diff_foreach( - diff, &exp, diff_file_fn, diff_hunk_fn, diff_line_fn)); + diff, diff_file_cb, diff_hunk_cb, diff_line_cb, &expect)); - cl_assert(exp.files == 5); - cl_assert(exp.file_adds == 2); - cl_assert(exp.file_dels == 1); - cl_assert(exp.file_mods == 2); + cl_assert_equal_i(5, expect.files); + cl_assert_equal_i(2, expect.file_status[GIT_DELTA_ADDED]); + cl_assert_equal_i(1, expect.file_status[GIT_DELTA_DELETED]); + cl_assert_equal_i(2, expect.file_status[GIT_DELTA_MODIFIED]); - cl_assert(exp.hunks == 5); + cl_assert_equal_i(5, expect.hunks); - cl_assert(exp.lines == 7 + 24 + 1 + 6 + 6); - cl_assert(exp.line_ctxt == 1); - cl_assert(exp.line_adds == 24 + 1 + 5 + 5); - cl_assert(exp.line_dels == 7 + 1); + cl_assert_equal_i(7 + 24 + 1 + 6 + 6, expect.lines); + cl_assert_equal_i(1, expect.line_ctxt); + cl_assert_equal_i(24 + 1 + 5 + 5, expect.line_adds); + cl_assert_equal_i(7 + 1, expect.line_dels); git_diff_list_free(diff); diff = NULL; - memset(&exp, 0, sizeof(exp)); + memset(&expect, 0, sizeof(expect)); - cl_git_pass(git_diff_tree_to_tree(g_repo, &opts, c, b, &diff)); + cl_git_pass(git_diff_tree_to_tree(&diff, g_repo, c, b, &opts)); cl_git_pass(git_diff_foreach( - diff, &exp, diff_file_fn, diff_hunk_fn, diff_line_fn)); - - cl_assert(exp.files == 2); - cl_assert(exp.file_adds == 0); - cl_assert(exp.file_dels == 0); - cl_assert(exp.file_mods == 2); + diff, diff_file_cb, diff_hunk_cb, diff_line_cb, &expect)); - cl_assert(exp.hunks == 2); + cl_assert_equal_i(2, expect.files); + cl_assert_equal_i(0, expect.file_status[GIT_DELTA_ADDED]); + cl_assert_equal_i(0, expect.file_status[GIT_DELTA_DELETED]); + cl_assert_equal_i(2, expect.file_status[GIT_DELTA_MODIFIED]); - cl_assert(exp.lines == 8 + 15); - cl_assert(exp.line_ctxt == 1); - cl_assert(exp.line_adds == 1); - cl_assert(exp.line_dels == 7 + 14); + cl_assert_equal_i(2, expect.hunks); - git_diff_list_free(diff); + cl_assert_equal_i(8 + 15, expect.lines); + cl_assert_equal_i(1, expect.line_ctxt); + cl_assert_equal_i(1, expect.line_adds); + cl_assert_equal_i(7 + 14, expect.line_dels); - git_tree_free(a); - git_tree_free(b); git_tree_free(c); } @@ -87,46 +97,47 @@ void test_diff_tree__options(void) const char *b_commit = "605812ab7fe421fdd"; const char *c_commit = "f5b0af1fb4f5"; const char *d_commit = "a97cc019851"; - git_tree *a, *b, *c, *d; - git_diff_options opts = {0}; - git_diff_list *diff = NULL; + git_tree *c, *d; diff_expects actual; int test_ab_or_cd[] = { 0, 0, 0, 0, 1, 1, 1, 1, 1 }; git_diff_options test_options[] = { /* a vs b tests */ - { GIT_DIFF_NORMAL, 1, 1, NULL, NULL, {0} }, - { GIT_DIFF_NORMAL, 3, 1, NULL, NULL, {0} }, - { GIT_DIFF_REVERSE, 2, 1, NULL, NULL, {0} }, - { GIT_DIFF_FORCE_TEXT, 2, 1, NULL, NULL, {0} }, + { 1, GIT_DIFF_NORMAL, 1, 1, NULL, NULL, {0} }, + { 1, GIT_DIFF_NORMAL, 3, 1, NULL, NULL, {0} }, + { 1, GIT_DIFF_REVERSE, 2, 1, NULL, NULL, {0} }, + { 1, GIT_DIFF_FORCE_TEXT, 2, 1, NULL, NULL, {0} }, /* c vs d tests */ - { GIT_DIFF_NORMAL, 3, 1, NULL, NULL, {0} }, - { GIT_DIFF_IGNORE_WHITESPACE, 3, 1, NULL, NULL, {0} }, - { GIT_DIFF_IGNORE_WHITESPACE_CHANGE, 3, 1, NULL, NULL, {0} }, - { GIT_DIFF_IGNORE_WHITESPACE_EOL, 3, 1, NULL, NULL, {0} }, - { GIT_DIFF_IGNORE_WHITESPACE | GIT_DIFF_REVERSE, 1, 1, NULL, NULL, {0} }, + { 1, GIT_DIFF_NORMAL, 3, 1, NULL, NULL, {0} }, + { 1, GIT_DIFF_IGNORE_WHITESPACE, 3, 1, NULL, NULL, {0} }, + { 1, GIT_DIFF_IGNORE_WHITESPACE_CHANGE, 3, 1, NULL, NULL, {0} }, + { 1, GIT_DIFF_IGNORE_WHITESPACE_EOL, 3, 1, NULL, NULL, {0} }, + { 1, GIT_DIFF_IGNORE_WHITESPACE | GIT_DIFF_REVERSE, 1, 1, NULL, NULL, {0} }, }; + /* to generate these values: * - cd to tests/resources/attr, * - mv .gitted .git * - git diff [options] 6bab5c79cd5140d0 605812ab7fe421fdd * - mv .git .gitted */ +#define EXPECT_STATUS_ADM(ADDS,DELS,MODS) { 0, ADDS, DELS, MODS, 0, 0, 0, 0, 0 } + diff_expects test_expects[] = { /* a vs b tests */ - { 5, 3, 0, 2, 0, 0, 0, 4, 0, 0, 51, 2, 46, 3 }, - { 5, 3, 0, 2, 0, 0, 0, 4, 0, 0, 53, 4, 46, 3 }, - { 5, 0, 3, 2, 0, 0, 0, 4, 0, 0, 52, 3, 3, 46 }, - { 5, 3, 0, 2, 0, 0, 0, 5, 0, 0, 54, 3, 48, 3 }, + { 5, 0, EXPECT_STATUS_ADM(3, 0, 2), 4, 0, 0, 51, 2, 46, 3 }, + { 5, 0, EXPECT_STATUS_ADM(3, 0, 2), 4, 0, 0, 53, 4, 46, 3 }, + { 5, 0, EXPECT_STATUS_ADM(0, 3, 2), 4, 0, 0, 52, 3, 3, 46 }, + { 5, 0, EXPECT_STATUS_ADM(3, 0, 2), 5, 0, 0, 54, 3, 47, 4 }, /* c vs d tests */ - { 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 22, 9, 10, 3 }, - { 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 19, 12, 7, 0 }, - { 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 20, 11, 8, 1 }, - { 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 20, 11, 8, 1 }, - { 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 18, 11, 0, 7 }, + { 1, 0, EXPECT_STATUS_ADM(0, 0, 1), 1, 0, 0, 22, 9, 10, 3 }, + { 1, 0, EXPECT_STATUS_ADM(0, 0, 1), 1, 0, 0, 19, 12, 7, 0 }, + { 1, 0, EXPECT_STATUS_ADM(0, 0, 1), 1, 0, 0, 20, 11, 8, 1 }, + { 1, 0, EXPECT_STATUS_ADM(0, 0, 1), 1, 0, 0, 20, 11, 8, 1 }, + { 1, 0, EXPECT_STATUS_ADM(0, 0, 1), 1, 0, 0, 18, 11, 0, 7 }, { 0 }, }; diff_expects *expected; - int i; + int i, j; g_repo = cl_git_sandbox_init("attr"); @@ -140,18 +151,17 @@ void test_diff_tree__options(void) opts = test_options[i]; if (test_ab_or_cd[i] == 0) - cl_git_pass(git_diff_tree_to_tree(g_repo, &opts, a, b, &diff)); + cl_git_pass(git_diff_tree_to_tree(&diff, g_repo, a, b, &opts)); else - cl_git_pass(git_diff_tree_to_tree(g_repo, &opts, c, d, &diff)); + cl_git_pass(git_diff_tree_to_tree(&diff, g_repo, c, d, &opts)); cl_git_pass(git_diff_foreach( - diff, &actual, diff_file_fn, diff_hunk_fn, diff_line_fn)); + diff, diff_file_cb, diff_hunk_cb, diff_line_cb, &actual)); expected = &test_expects[i]; cl_assert_equal_i(actual.files, expected->files); - cl_assert_equal_i(actual.file_adds, expected->file_adds); - cl_assert_equal_i(actual.file_dels, expected->file_dels); - cl_assert_equal_i(actual.file_mods, expected->file_mods); + for (j = GIT_DELTA_UNMODIFIED; j <= GIT_DELTA_TYPECHANGE; ++j) + cl_assert_equal_i(expected->file_status[j], actual.file_status[j]); cl_assert_equal_i(actual.hunks, expected->hunks); cl_assert_equal_i(actual.lines, expected->lines); cl_assert_equal_i(actual.line_ctxt, expected->line_ctxt); @@ -162,8 +172,6 @@ void test_diff_tree__options(void) diff = NULL; } - git_tree_free(a); - git_tree_free(b); git_tree_free(c); git_tree_free(d); } @@ -172,10 +180,6 @@ void test_diff_tree__bare(void) { const char *a_commit = "8496071c1b46c85"; const char *b_commit = "be3563ae3f79"; - git_tree *a, *b; - git_diff_options opts = {0}; - git_diff_list *diff = NULL; - diff_expects exp; g_repo = cl_git_sandbox_init("testrepo.git"); @@ -185,26 +189,268 @@ void test_diff_tree__bare(void) opts.context_lines = 1; opts.interhunk_lines = 1; - memset(&exp, 0, sizeof(exp)); + cl_git_pass(git_diff_tree_to_tree(&diff, g_repo, a, b, &opts)); - cl_git_pass(git_diff_tree_to_tree(g_repo, &opts, a, b, &diff)); + cl_git_pass(git_diff_foreach( + diff, diff_file_cb, diff_hunk_cb, diff_line_cb, &expect)); + + cl_assert_equal_i(3, expect.files); + cl_assert_equal_i(2, expect.file_status[GIT_DELTA_ADDED]); + cl_assert_equal_i(0, expect.file_status[GIT_DELTA_DELETED]); + cl_assert_equal_i(1, expect.file_status[GIT_DELTA_MODIFIED]); + + cl_assert_equal_i(3, expect.hunks); + + cl_assert_equal_i(4, expect.lines); + cl_assert_equal_i(0, expect.line_ctxt); + cl_assert_equal_i(3, expect.line_adds); + cl_assert_equal_i(1, expect.line_dels); +} + +void test_diff_tree__merge(void) +{ + /* grabbed a couple of commit oids from the history of the attr repo */ + const char *a_commit = "605812a"; + const char *b_commit = "370fe9ec22"; + const char *c_commit = "f5b0af1fb4f5c"; + git_tree *c; + git_diff_list *diff1 = NULL, *diff2 = NULL; + + g_repo = cl_git_sandbox_init("attr"); + + cl_assert((a = resolve_commit_oid_to_tree(g_repo, a_commit)) != NULL); + cl_assert((b = resolve_commit_oid_to_tree(g_repo, b_commit)) != NULL); + cl_assert((c = resolve_commit_oid_to_tree(g_repo, c_commit)) != NULL); + + cl_git_pass(git_diff_tree_to_tree(&diff1, g_repo, a, b, NULL)); + + cl_git_pass(git_diff_tree_to_tree(&diff2, g_repo, c, b, NULL)); + + git_tree_free(c); + + cl_git_pass(git_diff_merge(diff1, diff2)); + + git_diff_list_free(diff2); cl_git_pass(git_diff_foreach( - diff, &exp, diff_file_fn, diff_hunk_fn, diff_line_fn)); + diff1, diff_file_cb, diff_hunk_cb, diff_line_cb, &expect)); - cl_assert(exp.files == 3); - cl_assert(exp.file_adds == 2); - cl_assert(exp.file_dels == 0); - cl_assert(exp.file_mods == 1); + cl_assert_equal_i(6, expect.files); + cl_assert_equal_i(2, expect.file_status[GIT_DELTA_ADDED]); + cl_assert_equal_i(1, expect.file_status[GIT_DELTA_DELETED]); + cl_assert_equal_i(3, expect.file_status[GIT_DELTA_MODIFIED]); - cl_assert(exp.hunks == 3); + cl_assert_equal_i(6, expect.hunks); - cl_assert(exp.lines == 4); - cl_assert(exp.line_ctxt == 0); - cl_assert(exp.line_adds == 3); - cl_assert(exp.line_dels == 1); + cl_assert_equal_i(59, expect.lines); + cl_assert_equal_i(1, expect.line_ctxt); + cl_assert_equal_i(36, expect.line_adds); + cl_assert_equal_i(22, expect.line_dels); - git_diff_list_free(diff); - git_tree_free(a); - git_tree_free(b); + git_diff_list_free(diff1); +} + +void test_diff_tree__larger_hunks(void) +{ + const char *a_commit = "d70d245ed97ed2aa596dd1af6536e4bfdb047b69"; + const char *b_commit = "7a9e0b02e63179929fed24f0a3e0f19168114d10"; + size_t d, num_d, h, num_h, l, num_l, header_len, line_len; + const git_diff_delta *delta; + git_diff_patch *patch; + const git_diff_range *range; + const char *header, *line; + char origin; + + g_repo = cl_git_sandbox_init("diff"); + + cl_assert((a = resolve_commit_oid_to_tree(g_repo, a_commit)) != NULL); + cl_assert((b = resolve_commit_oid_to_tree(g_repo, b_commit)) != NULL); + + opts.context_lines = 1; + opts.interhunk_lines = 0; + + cl_git_pass(git_diff_tree_to_tree(&diff, g_repo, a, b, &opts)); + + 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); + + 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)); + + 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_git_fail(git_diff_patch_get_line_in_hunk( + &origin, &line, &line_len, NULL, NULL, patch, h, num_l)); + } + + cl_git_fail(git_diff_patch_get_hunk( + &range, &header, &header_len, &num_l, patch, num_h)); + + git_diff_patch_free(patch); + } + + cl_git_fail(git_diff_get_patch(&patch, &delta, diff, num_d)); + + cl_assert_equal_i(2, (int)num_d); +} + +void test_diff_tree__checks_options_version(void) +{ + const char *a_commit = "8496071c1b46c85"; + const char *b_commit = "be3563ae3f79"; + const git_error *err; + + g_repo = cl_git_sandbox_init("testrepo.git"); + + cl_assert((a = resolve_commit_oid_to_tree(g_repo, a_commit)) != NULL); + cl_assert((b = resolve_commit_oid_to_tree(g_repo, b_commit)) != NULL); + + opts.version = 0; + cl_git_fail(git_diff_tree_to_tree(&diff, g_repo, a, b, &opts)); + err = giterr_last(); + cl_assert_equal_i(GITERR_INVALID, err->klass); + + giterr_clear(); + opts.version = 1024; + cl_git_fail(git_diff_tree_to_tree(&diff, g_repo, a, b, &opts)); + err = giterr_last(); +} + +void process_tree_to_tree_diffing( + const char *old_commit, + const char *new_commit) +{ + g_repo = cl_git_sandbox_init("unsymlinked.git"); + + cl_assert((a = resolve_commit_oid_to_tree(g_repo, old_commit)) != NULL); + cl_assert((b = resolve_commit_oid_to_tree(g_repo, new_commit)) != NULL); + + cl_git_pass(git_diff_tree_to_tree(&diff, g_repo, a, b, &opts)); + + cl_git_pass(git_diff_foreach( + diff, diff_file_cb, NULL, NULL, &expect)); +} + +void test_diff_tree__symlink_blob_mode_changed_to_regular_file(void) +{ + /* + * $ git diff 7fccd7..806999 + * diff --git a/include/Nu/Nu.h b/include/Nu/Nu.h + * deleted file mode 120000 + * index 19bf568..0000000 + * --- a/include/Nu/Nu.h + * +++ /dev/null + * @@ -1 +0,0 @@ + * -../../objc/Nu.h + * \ No newline at end of file + * diff --git a/include/Nu/Nu.h b/include/Nu/Nu.h + * new file mode 100644 + * index 0000000..f9e6561 + * --- /dev/null + * +++ b/include/Nu/Nu.h + * @@ -0,0 +1 @@ + * +awesome content + * diff --git a/objc/Nu.h b/objc/Nu.h + * deleted file mode 100644 + * index f9e6561..0000000 + * --- a/objc/Nu.h + * +++ /dev/null + * @@ -1 +0,0 @@ + * -awesome content + */ + + process_tree_to_tree_diffing("7fccd7", "806999"); + + cl_assert_equal_i(3, expect.files); + cl_assert_equal_i(2, expect.file_status[GIT_DELTA_DELETED]); + cl_assert_equal_i(0, expect.file_status[GIT_DELTA_MODIFIED]); + cl_assert_equal_i(1, expect.file_status[GIT_DELTA_ADDED]); + cl_assert_equal_i(0, expect.file_status[GIT_DELTA_TYPECHANGE]); +} + +void test_diff_tree__symlink_blob_mode_changed_to_regular_file_as_typechange(void) +{ + /* + * $ git diff 7fccd7..a8595c + * diff --git a/include/Nu/Nu.h b/include/Nu/Nu.h + * deleted file mode 120000 + * index 19bf568..0000000 + * --- a/include/Nu/Nu.h + * +++ /dev/null + * @@ -1 +0,0 @@ + * -../../objc/Nu.h + * \ No newline at end of file + * diff --git a/include/Nu/Nu.h b/include/Nu/Nu.h + * new file mode 100755 + * index 0000000..f9e6561 + * --- /dev/null + * +++ b/include/Nu/Nu.h + * @@ -0,0 +1 @@ + * +awesome content + * diff --git a/objc/Nu.h b/objc/Nu.h + * deleted file mode 100644 + * index f9e6561..0000000 + * --- a/objc/Nu.h + * +++ /dev/null + * @@ -1 +0,0 @@ + * -awesome content + */ + + opts.flags = GIT_DIFF_INCLUDE_TYPECHANGE; + process_tree_to_tree_diffing("7fccd7", "a8595c"); + + cl_assert_equal_i(2, expect.files); + cl_assert_equal_i(1, expect.file_status[GIT_DELTA_DELETED]); + cl_assert_equal_i(0, expect.file_status[GIT_DELTA_MODIFIED]); + cl_assert_equal_i(0, expect.file_status[GIT_DELTA_ADDED]); + cl_assert_equal_i(1, expect.file_status[GIT_DELTA_TYPECHANGE]); +} + +void test_diff_tree__regular_blob_mode_changed_to_executable_file(void) +{ + /* + * $ git diff 806999..a8595c + * diff --git a/include/Nu/Nu.h b/include/Nu/Nu.h + * old mode 100644 + * new mode 100755 + */ + + process_tree_to_tree_diffing("806999", "a8595c"); + + cl_assert_equal_i(1, expect.files); + cl_assert_equal_i(0, expect.file_status[GIT_DELTA_DELETED]); + cl_assert_equal_i(1, expect.file_status[GIT_DELTA_MODIFIED]); + cl_assert_equal_i(0, expect.file_status[GIT_DELTA_ADDED]); + cl_assert_equal_i(0, expect.file_status[GIT_DELTA_TYPECHANGE]); +} + +void test_diff_tree__issue_1397(void) +{ + /* this test shows that it is not needed */ + + g_repo = cl_git_sandbox_init("issue_1397"); + + cl_repo_set_bool(g_repo, "core.autocrlf", true); + + cl_assert((a = resolve_commit_oid_to_tree(g_repo, "8a7ef04")) != NULL); + cl_assert((b = resolve_commit_oid_to_tree(g_repo, "7f483a7")) != NULL); + + cl_git_pass(git_diff_tree_to_tree(&diff, g_repo, a, b, &opts)); + + cl_git_pass(git_diff_foreach( + diff, diff_file_cb, diff_hunk_cb, diff_line_cb, &expect)); + + cl_assert_equal_i(1, expect.files); + cl_assert_equal_i(0, expect.file_status[GIT_DELTA_DELETED]); + cl_assert_equal_i(1, expect.file_status[GIT_DELTA_MODIFIED]); + cl_assert_equal_i(0, expect.file_status[GIT_DELTA_ADDED]); + cl_assert_equal_i(0, expect.file_status[GIT_DELTA_TYPECHANGE]); } |