diff options
| author | Vicent Martà <vicent@github.com> | 2013-06-12 14:52:47 -0700 |
|---|---|---|
| committer | Vicent Martà <vicent@github.com> | 2013-06-12 14:52:47 -0700 |
| commit | 93da7af707f7d6e9c466a319a7fa437883da7793 (patch) | |
| tree | 48f6d384f1ec242581a60528790e969dbf94eb06 /tests-clar/diff | |
| parent | 5438e9c2b8eeb79c5580c576da6e110aa0cd2e59 (diff) | |
| parent | 360f42f4b3f5de31270416220bd799b951202b2d (diff) | |
| download | libgit2-93da7af707f7d6e9c466a319a7fa437883da7793.tar.gz | |
Merge pull request #1642 from arrbee/diff-function-context
Diff code reorg plus function context in diff headers
Diffstat (limited to 'tests-clar/diff')
| -rw-r--r-- | tests-clar/diff/blob.c | 238 | ||||
| -rw-r--r-- | tests-clar/diff/drivers.c | 125 | ||||
| -rw-r--r-- | tests-clar/diff/patch.c | 2 | ||||
| -rw-r--r-- | tests-clar/diff/rename.c | 2 | ||||
| -rw-r--r-- | tests-clar/diff/submodules.c | 1 |
5 files changed, 366 insertions, 2 deletions
diff --git a/tests-clar/diff/blob.c b/tests-clar/diff/blob.c index 2ac8dbc51..b12186d98 100644 --- a/tests-clar/diff/blob.c +++ b/tests-clar/diff/blob.c @@ -120,6 +120,93 @@ void test_diff_blob__can_compare_text_blobs(void) git_blob_free(c); } +void test_diff_blob__can_compare_text_blobs_with_patch(void) +{ + git_blob *a, *b, *c; + git_oid a_oid, b_oid, c_oid; + git_diff_patch *p; + size_t tc, ta, td; + + /* tests/resources/attr/root_test1 */ + cl_git_pass(git_oid_fromstrn(&a_oid, "45141a79", 8)); + cl_git_pass(git_blob_lookup_prefix(&a, g_repo, &a_oid, 4)); + + /* tests/resources/attr/root_test2 */ + cl_git_pass(git_oid_fromstrn(&b_oid, "4d713dc4", 8)); + cl_git_pass(git_blob_lookup_prefix(&b, g_repo, &b_oid, 4)); + + /* tests/resources/attr/root_test3 */ + cl_git_pass(git_oid_fromstrn(&c_oid, "c96bbb2c2557a832", 16)); + cl_git_pass(git_blob_lookup_prefix(&c, g_repo, &c_oid, 8)); + + /* Doing the equivalent of a `git diff -U1` on these files */ + + /* diff on tests/resources/attr/root_test1 */ + cl_git_pass(git_diff_patch_from_blobs(&p, a, b, &opts)); + + cl_assert(p != NULL); + cl_assert_equal_i(GIT_DELTA_MODIFIED, git_diff_patch_delta(p)->status); + cl_assert_equal_i(1, (int)git_diff_patch_num_hunks(p)); + cl_assert_equal_i(6, git_diff_patch_num_lines_in_hunk(p, 0)); + + cl_git_pass(git_diff_patch_line_stats(&tc, &ta, &td, p)); + cl_assert_equal_i(1, (int)tc); + cl_assert_equal_i(5, (int)ta); + cl_assert_equal_i(0, (int)td); + + git_diff_patch_free(p); + + /* diff on tests/resources/attr/root_test2 */ + cl_git_pass(git_diff_patch_from_blobs(&p, b, c, &opts)); + + cl_assert(p != NULL); + cl_assert_equal_i(GIT_DELTA_MODIFIED, git_diff_patch_delta(p)->status); + cl_assert_equal_i(1, (int)git_diff_patch_num_hunks(p)); + cl_assert_equal_i(15, git_diff_patch_num_lines_in_hunk(p, 0)); + + cl_git_pass(git_diff_patch_line_stats(&tc, &ta, &td, p)); + cl_assert_equal_i(3, (int)tc); + cl_assert_equal_i(9, (int)ta); + cl_assert_equal_i(3, (int)td); + + git_diff_patch_free(p); + + /* diff on tests/resources/attr/root_test3 */ + cl_git_pass(git_diff_patch_from_blobs(&p, a, c, &opts)); + + cl_assert(p != NULL); + cl_assert_equal_i(GIT_DELTA_MODIFIED, git_diff_patch_delta(p)->status); + cl_assert_equal_i(1, (int)git_diff_patch_num_hunks(p)); + cl_assert_equal_i(13, git_diff_patch_num_lines_in_hunk(p, 0)); + + cl_git_pass(git_diff_patch_line_stats(&tc, &ta, &td, p)); + cl_assert_equal_i(0, (int)tc); + cl_assert_equal_i(12, (int)ta); + cl_assert_equal_i(1, (int)td); + + git_diff_patch_free(p); + + /* one more */ + cl_git_pass(git_diff_patch_from_blobs(&p, c, d, &opts)); + + cl_assert(p != NULL); + cl_assert_equal_i(GIT_DELTA_MODIFIED, git_diff_patch_delta(p)->status); + cl_assert_equal_i(2, (int)git_diff_patch_num_hunks(p)); + cl_assert_equal_i(5, git_diff_patch_num_lines_in_hunk(p, 0)); + cl_assert_equal_i(9, git_diff_patch_num_lines_in_hunk(p, 1)); + + cl_git_pass(git_diff_patch_line_stats(&tc, &ta, &td, p)); + cl_assert_equal_i(4, (int)tc); + cl_assert_equal_i(6, (int)ta); + cl_assert_equal_i(4, (int)td); + + git_diff_patch_free(p); + + git_blob_free(a); + git_blob_free(b); + git_blob_free(c); +} + void test_diff_blob__can_compare_against_null_blobs(void) { git_blob *e = NULL; @@ -175,6 +262,66 @@ void test_diff_blob__can_compare_against_null_blobs(void) cl_assert_equal_i(0, expected.lines); } +void test_diff_blob__can_compare_against_null_blobs_with_patch(void) +{ + git_blob *e = NULL; + git_diff_patch *p; + int line; + char origin; + + cl_git_pass(git_diff_patch_from_blobs(&p, d, e, &opts)); + + cl_assert(p != NULL); + cl_assert_equal_i(GIT_DELTA_DELETED, git_diff_patch_delta(p)->status); + cl_assert_equal_i(1, (int)git_diff_patch_num_hunks(p)); + cl_assert_equal_i(14, git_diff_patch_num_lines_in_hunk(p, 0)); + + for (line = 0; line < git_diff_patch_num_lines_in_hunk(p, 0); ++line) { + cl_git_pass(git_diff_patch_get_line_in_hunk( + &origin, NULL, NULL, NULL, NULL, p, 0, line)); + cl_assert_equal_i(GIT_DIFF_LINE_DELETION, (int)origin); + } + + git_diff_patch_free(p); + + opts.flags |= GIT_DIFF_REVERSE; + + cl_git_pass(git_diff_patch_from_blobs(&p, d, e, &opts)); + + cl_assert(p != NULL); + cl_assert_equal_i(GIT_DELTA_ADDED, git_diff_patch_delta(p)->status); + cl_assert_equal_i(1, (int)git_diff_patch_num_hunks(p)); + cl_assert_equal_i(14, git_diff_patch_num_lines_in_hunk(p, 0)); + + for (line = 0; line < git_diff_patch_num_lines_in_hunk(p, 0); ++line) { + cl_git_pass(git_diff_patch_get_line_in_hunk( + &origin, NULL, NULL, NULL, NULL, p, 0, line)); + cl_assert_equal_i(GIT_DIFF_LINE_ADDITION, (int)origin); + } + + git_diff_patch_free(p); + + opts.flags ^= GIT_DIFF_REVERSE; + + cl_git_pass(git_diff_patch_from_blobs(&p, alien, NULL, &opts)); + + cl_assert(p != NULL); + cl_assert_equal_i(GIT_DELTA_DELETED, git_diff_patch_delta(p)->status); + cl_assert((git_diff_patch_delta(p)->flags & GIT_DIFF_FLAG_BINARY) != 0); + cl_assert_equal_i(0, (int)git_diff_patch_num_hunks(p)); + + git_diff_patch_free(p); + + cl_git_pass(git_diff_patch_from_blobs(&p, NULL, alien, &opts)); + + cl_assert(p != NULL); + cl_assert_equal_i(GIT_DELTA_ADDED, git_diff_patch_delta(p)->status); + cl_assert((git_diff_patch_delta(p)->flags & GIT_DIFF_FLAG_BINARY) != 0); + cl_assert_equal_i(0, (int)git_diff_patch_num_hunks(p)); + + git_diff_patch_free(p); +} + static void assert_identical_blobs_comparison(diff_expects *expected) { cl_assert_equal_i(1, expected->files); @@ -206,6 +353,29 @@ void test_diff_blob__can_compare_identical_blobs(void) assert_identical_blobs_comparison(&expected); } +void test_diff_blob__can_compare_identical_blobs_with_patch(void) +{ + git_diff_patch *p; + + cl_git_pass(git_diff_patch_from_blobs(&p, d, d, &opts)); + cl_assert(p != NULL); + cl_assert_equal_i(GIT_DELTA_UNMODIFIED, git_diff_patch_delta(p)->status); + cl_assert_equal_i(0, (int)git_diff_patch_num_hunks(p)); + git_diff_patch_free(p); + + cl_git_pass(git_diff_patch_from_blobs(&p, NULL, NULL, &opts)); + cl_assert(p != NULL); + cl_assert_equal_i(GIT_DELTA_UNMODIFIED, git_diff_patch_delta(p)->status); + cl_assert_equal_i(0, (int)git_diff_patch_num_hunks(p)); + git_diff_patch_free(p); + + cl_git_pass(git_diff_patch_from_blobs(&p, alien, alien, &opts)); + cl_assert(p != NULL); + cl_assert_equal_i(GIT_DELTA_UNMODIFIED, git_diff_patch_delta(p)->status); + cl_assert_equal_i(0, (int)git_diff_patch_num_hunks(p)); + git_diff_patch_free(p); +} + static void assert_binary_blobs_comparison(diff_expects *expected) { cl_assert(expected->files_binary > 0); @@ -428,6 +598,74 @@ void test_diff_blob__can_compare_blob_to_buffer(void) git_blob_free(a); } +void test_diff_blob__can_compare_blob_to_buffer_with_patch(void) +{ + git_diff_patch *p; + git_blob *a; + git_oid a_oid; + const char *a_content = "Hello from the root\n"; + const char *b_content = "Hello from the root\n\nSome additional lines\n\nDown here below\n\n"; + size_t tc, ta, td; + + /* tests/resources/attr/root_test1 */ + cl_git_pass(git_oid_fromstrn(&a_oid, "45141a79", 8)); + cl_git_pass(git_blob_lookup_prefix(&a, g_repo, &a_oid, 4)); + + /* diff from blob a to content of b */ + cl_git_pass(git_diff_patch_from_blob_and_buffer( + &p, a, b_content, strlen(b_content), &opts)); + + cl_assert(p != NULL); + cl_assert_equal_i(GIT_DELTA_MODIFIED, git_diff_patch_delta(p)->status); + cl_assert_equal_i(1, (int)git_diff_patch_num_hunks(p)); + cl_assert_equal_i(6, git_diff_patch_num_lines_in_hunk(p, 0)); + + cl_git_pass(git_diff_patch_line_stats(&tc, &ta, &td, p)); + cl_assert_equal_i(1, (int)tc); + cl_assert_equal_i(5, (int)ta); + cl_assert_equal_i(0, (int)td); + + git_diff_patch_free(p); + + /* diff from blob a to content of a */ + cl_git_pass(git_diff_patch_from_blob_and_buffer( + &p, a, a_content, strlen(a_content), &opts)); + cl_assert(p != NULL); + cl_assert_equal_i(GIT_DELTA_UNMODIFIED, git_diff_patch_delta(p)->status); + cl_assert_equal_i(0, (int)git_diff_patch_num_hunks(p)); + git_diff_patch_free(p); + + /* diff from NULL blob to content of a */ + cl_git_pass(git_diff_patch_from_blob_and_buffer( + &p, NULL, a_content, strlen(a_content), &opts)); + cl_assert(p != NULL); + cl_assert_equal_i(GIT_DELTA_ADDED, git_diff_patch_delta(p)->status); + cl_assert_equal_i(1, (int)git_diff_patch_num_hunks(p)); + cl_assert_equal_i(1, git_diff_patch_num_lines_in_hunk(p, 0)); + git_diff_patch_free(p); + + /* diff from blob a to NULL buffer */ + cl_git_pass(git_diff_patch_from_blob_and_buffer( + &p, a, NULL, 0, &opts)); + cl_assert(p != NULL); + cl_assert_equal_i(GIT_DELTA_DELETED, git_diff_patch_delta(p)->status); + cl_assert_equal_i(1, (int)git_diff_patch_num_hunks(p)); + cl_assert_equal_i(1, git_diff_patch_num_lines_in_hunk(p, 0)); + git_diff_patch_free(p); + + /* diff with reverse */ + opts.flags ^= GIT_DIFF_REVERSE; + + cl_git_pass(git_diff_patch_from_blob_and_buffer( + &p, a, NULL, 0, &opts)); + cl_assert(p != NULL); + cl_assert_equal_i(GIT_DELTA_ADDED, git_diff_patch_delta(p)->status); + cl_assert_equal_i(1, (int)git_diff_patch_num_hunks(p)); + cl_assert_equal_i(1, git_diff_patch_num_lines_in_hunk(p, 0)); + git_diff_patch_free(p); + + git_blob_free(a); +} static void assert_one_modified_with_lines(diff_expects *expected, int lines) { diff --git a/tests-clar/diff/drivers.c b/tests-clar/diff/drivers.c new file mode 100644 index 000000000..06ab2ff14 --- /dev/null +++ b/tests-clar/diff/drivers.c @@ -0,0 +1,125 @@ +#include "clar_libgit2.h" +#include "diff_helpers.h" +#include "repository.h" +#include "diff_driver.h" + +static git_repository *g_repo = NULL; + +void test_diff_drivers__initialize(void) +{ +} + +void test_diff_drivers__cleanup(void) +{ + cl_git_sandbox_cleanup(); + g_repo = NULL; +} + +void test_diff_drivers__patterns(void) +{ + git_config *cfg; + const char *one_sha = "19dd32dfb1520a64e5bbaae8dce6ef423dfa2f13"; + git_tree *one; + git_diff_list *diff; + git_diff_patch *patch; + char *text; + const char *expected0 = "diff --git a/untimely.txt b/untimely.txt\nindex 9a69d96..57fd0cf 100644\n--- a/untimely.txt\n+++ b/untimely.txt\n@@ -22,3 +22,5 @@ Comes through the blood of the vanguards who\n dreamed--too soon--it had sounded.\r\n \r\n -- Rudyard Kipling\r\n+\r\n+Some new stuff\r\n"; + const char *expected1 = "diff --git a/untimely.txt b/untimely.txt\nindex 9a69d96..57fd0cf 100644\nBinary files a/untimely.txt and b/untimely.txt differ\n"; + const char *expected2 = "diff --git a/untimely.txt b/untimely.txt\nindex 9a69d96..57fd0cf 100644\n--- a/untimely.txt\n+++ b/untimely.txt\n@@ -22,3 +22,5 @@ Heaven delivers on earth the Hour that cannot be\n dreamed--too soon--it had sounded.\r\n \r\n -- Rudyard Kipling\r\n+\r\n+Some new stuff\r\n"; + + g_repo = cl_git_sandbox_init("renames"); + + one = resolve_commit_oid_to_tree(g_repo, one_sha); + + /* no diff */ + + cl_git_pass(git_diff_tree_to_workdir(&diff, g_repo, one, NULL)); + cl_assert_equal_i(0, (int)git_diff_num_deltas(diff)); + git_diff_list_free(diff); + + /* default diff */ + + cl_git_append2file("renames/untimely.txt", "\r\nSome new stuff\r\n"); + + cl_git_pass(git_diff_tree_to_workdir(&diff, g_repo, one, NULL)); + cl_assert_equal_i(1, (int)git_diff_num_deltas(diff)); + + cl_git_pass(git_diff_get_patch(&patch, NULL, diff, 0)); + cl_git_pass(git_diff_patch_to_str(&text, patch)); + cl_assert_equal_s(expected0, text); + + git__free(text); + git_diff_patch_free(patch); + git_diff_list_free(diff); + + /* attribute diff set to false */ + + cl_git_rewritefile("renames/.gitattributes", "untimely.txt -diff\n"); + + cl_git_pass(git_diff_tree_to_workdir(&diff, g_repo, one, NULL)); + cl_assert_equal_i(1, (int)git_diff_num_deltas(diff)); + + cl_git_pass(git_diff_get_patch(&patch, NULL, diff, 0)); + cl_git_pass(git_diff_patch_to_str(&text, patch)); + cl_assert_equal_s(expected1, text); + + git__free(text); + git_diff_patch_free(patch); + git_diff_list_free(diff); + + /* attribute diff set to unconfigured value (should use default) */ + + cl_git_rewritefile("renames/.gitattributes", "untimely.txt diff=kipling0\n"); + + cl_git_pass(git_diff_tree_to_workdir(&diff, g_repo, one, NULL)); + cl_assert_equal_i(1, (int)git_diff_num_deltas(diff)); + + cl_git_pass(git_diff_get_patch(&patch, NULL, diff, 0)); + cl_git_pass(git_diff_patch_to_str(&text, patch)); + cl_assert_equal_s(expected0, text); + + git__free(text); + git_diff_patch_free(patch); + git_diff_list_free(diff); + + /* let's define that driver */ + + cl_git_pass(git_repository_config(&cfg, g_repo)); + cl_git_pass(git_config_set_bool(cfg, "diff.kipling0.binary", 1)); + git_config_free(cfg); + + cl_git_pass(git_diff_tree_to_workdir(&diff, g_repo, one, NULL)); + cl_assert_equal_i(1, (int)git_diff_num_deltas(diff)); + + cl_git_pass(git_diff_get_patch(&patch, NULL, diff, 0)); + cl_git_pass(git_diff_patch_to_str(&text, patch)); + cl_assert_equal_s(expected1, text); + + git__free(text); + git_diff_patch_free(patch); + git_diff_list_free(diff); + + /* let's use a real driver with some regular expressions */ + + git_diff_driver_registry_free(g_repo->diff_drivers); + g_repo->diff_drivers = NULL; + + cl_git_pass(git_repository_config(&cfg, g_repo)); + cl_git_pass(git_config_set_bool(cfg, "diff.kipling0.binary", 0)); + cl_git_pass(git_config_set_string(cfg, "diff.kipling0.xfuncname", "^H")); + git_config_free(cfg); + + cl_git_pass(git_diff_tree_to_workdir(&diff, g_repo, one, NULL)); + cl_assert_equal_i(1, (int)git_diff_num_deltas(diff)); + + cl_git_pass(git_diff_get_patch(&patch, NULL, diff, 0)); + cl_git_pass(git_diff_patch_to_str(&text, patch)); + cl_assert_equal_s(expected2, text); + + git__free(text); + git_diff_patch_free(patch); + git_diff_list_free(diff); + + git_tree_free(one); +} + diff --git a/tests-clar/diff/patch.c b/tests-clar/diff/patch.c index f9e913a74..3f14a0de7 100644 --- a/tests-clar/diff/patch.c +++ b/tests-clar/diff/patch.c @@ -543,7 +543,7 @@ void test_diff_patch__line_counts_with_eofnl(void) "index 378a7d9..3d0154e 100644\n" "--- a/songof7cities.txt\n" "+++ b/songof7cities.txt\n" - "@@ -42,7 +42,7 @@\n" + "@@ -42,7 +42,7 @@ With peoples undefeated of the dark, enduring blood.\n" " \n" " To the sound of trumpets shall their seed restore my Cities\n" " Wealthy and well-weaponed, that once more may I behold\n" diff --git a/tests-clar/diff/rename.c b/tests-clar/diff/rename.c index 8bff96cf2..ca3f50676 100644 --- a/tests-clar/diff/rename.c +++ b/tests-clar/diff/rename.c @@ -558,7 +558,7 @@ void test_diff_rename__patch(void) git_diff_patch *patch; const git_diff_delta *delta; char *text; - const char *expected = "diff --git a/sixserving.txt b/ikeepsix.txt\nindex ad0a8e5..36020db 100644\n--- a/sixserving.txt\n+++ b/ikeepsix.txt\n@@ -1,3 +1,6 @@\n+I Keep Six Honest Serving-Men\n+=============================\n+\n I KEEP six honest serving-men\n (They taught me all I knew);\n Their names are What and Why and When\n@@ -21,4 +24,4 @@\n One million Hows, two million Wheres,\n And seven million Whys!\n \n- -- Rudyard Kipling\n+ -- Rudyard Kipling\n"; + const char *expected = "diff --git a/sixserving.txt b/ikeepsix.txt\nindex ad0a8e5..36020db 100644\n--- a/sixserving.txt\n+++ b/ikeepsix.txt\n@@ -1,3 +1,6 @@\n+I Keep Six Honest Serving-Men\n+=============================\n+\n I KEEP six honest serving-men\n (They taught me all I knew);\n Their names are What and Why and When\n@@ -21,4 +24,4 @@ She sends'em abroad on her own affairs,\n One million Hows, two million Wheres,\n And seven million Whys!\n \n- -- Rudyard Kipling\n+ -- Rudyard Kipling\n"; old_tree = resolve_commit_oid_to_tree(g_repo, sha0); new_tree = resolve_commit_oid_to_tree(g_repo, sha1); diff --git a/tests-clar/diff/submodules.c b/tests-clar/diff/submodules.c index f152af46f..6e52a6319 100644 --- a/tests-clar/diff/submodules.c +++ b/tests-clar/diff/submodules.c @@ -1,5 +1,6 @@ #include "clar_libgit2.h" #include "repository.h" +#include "posix.h" #include "../submodule/submodule_helpers.h" static git_repository *g_repo = NULL; |
