From eb3d71a5bcd78cb4840e62194e8998141508af88 Mon Sep 17 00:00:00 2001 From: nulltoken Date: Wed, 25 Apr 2012 22:23:35 +0200 Subject: diff: fix generation of the header of a removal patch --- tests-clar/diff/patch.c | 68 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 tests-clar/diff/patch.c (limited to 'tests-clar/diff/patch.c') diff --git a/tests-clar/diff/patch.c b/tests-clar/diff/patch.c new file mode 100644 index 000000000..e2576277f --- /dev/null +++ b/tests-clar/diff/patch.c @@ -0,0 +1,68 @@ +#include "clar_libgit2.h" +#include "diff_helpers.h" + +static git_repository *g_repo = NULL; + +void test_diff_patch__initialize(void) +{ + g_repo = cl_git_sandbox_init("status"); +} + +void test_diff_patch__cleanup(void) +{ + cl_git_sandbox_cleanup(); +} + +#define EXPECTED_OUTPUT "diff --git a/subdir.txt b/subdir.txt\n" \ + "deleted file mode 100644\n" \ + "index e8ee89e..0000000\n" \ + "--- a/subdir.txt\n" \ + "+++ /dev/null\n" + +static int check_removal_cb( + void *cb_data, + char line_origin, + const char *formatted_output) +{ + GIT_UNUSED(cb_data); + + if (line_origin != 'F') + return 0; + + if (strcmp(EXPECTED_OUTPUT, formatted_output) == 0) + return 0; + + return -1; +} + +void test_diff_patch__can_properly_display_the_removal_of_a_file(void) +{ + /* + * $ git diff 26a125e..735b6a2 + * diff --git a/subdir.txt b/subdir.txt + * deleted file mode 100644 + * index e8ee89e..0000000 + * --- a/subdir.txt + * +++ /dev/null + * @@ -1,2 +0,0 @@ + * -Is it a bird? + * -Is it a plane? + */ + + const char *one_sha = "26a125e"; + const char *another_sha = "735b6a2"; + git_tree *one, *another; + git_diff_list *diff; + + one = resolve_commit_oid_to_tree(g_repo, one_sha); + another = resolve_commit_oid_to_tree(g_repo, another_sha); + + cl_git_pass(git_diff_tree_to_tree(g_repo, NULL, one, another, &diff)); + + cl_git_pass(git_diff_print_patch(diff, NULL, check_removal_cb)); + + git_diff_list_free(diff); + + git_tree_free(another); + git_tree_free(one); +} -- cgit v1.2.1 From 1d2dd864add4835c49744a566c226a1c7da04e99 Mon Sep 17 00:00:00 2001 From: nulltoken Date: Sun, 29 Apr 2012 19:42:51 +0200 Subject: diff: provide more context to the consumer of the callbacks Update the callback to provide some information related to the file change being processed and the range of the hunk, when applicable. --- tests-clar/diff/patch.c | 44 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 37 insertions(+), 7 deletions(-) (limited to 'tests-clar/diff/patch.c') diff --git a/tests-clar/diff/patch.c b/tests-clar/diff/patch.c index e2576277f..3da9ce82b 100644 --- a/tests-clar/diff/patch.c +++ b/tests-clar/diff/patch.c @@ -13,26 +13,56 @@ void test_diff_patch__cleanup(void) cl_git_sandbox_cleanup(); } -#define EXPECTED_OUTPUT "diff --git a/subdir.txt b/subdir.txt\n" \ +#define EXPECTED_HEADER "diff --git a/subdir.txt b/subdir.txt\n" \ "deleted file mode 100644\n" \ "index e8ee89e..0000000\n" \ "--- a/subdir.txt\n" \ "+++ /dev/null\n" +#define EXPECTED_HUNK "@@ -1,2 +0,0 @@\n" + static int check_removal_cb( void *cb_data, + git_diff_delta *delta, + git_diff_range *range, char line_origin, - const char *formatted_output) + const char *formatted_output, + size_t output_len) { GIT_UNUSED(cb_data); - if (line_origin != 'F') - return 0; + switch (line_origin) { + case GIT_DIFF_LINE_FILE_HDR: + cl_assert_equal_s(EXPECTED_HEADER, formatted_output); + cl_assert(range == NULL); + goto check_delta; + + case GIT_DIFF_LINE_HUNK_HDR: + cl_assert_equal_s(EXPECTED_HUNK, formatted_output); + /* Fall through */ + + case GIT_DIFF_LINE_CONTEXT: + case GIT_DIFF_LINE_DELETION: + goto check_range; + + default: + /* unexpected code path */ + return -1; + } + +check_range: + cl_assert(range != NULL); + cl_assert_equal_i(1, range->old_start); + cl_assert_equal_i(2, range->old_lines); + cl_assert_equal_i(0, range->new_start); + cl_assert_equal_i(0, range->new_lines); - if (strcmp(EXPECTED_OUTPUT, formatted_output) == 0) - return 0; +check_delta: + cl_assert_equal_s("subdir.txt", delta->old.path); + cl_assert_equal_s("subdir.txt", delta->new.path); + cl_assert_equal_i(GIT_DELTA_DELETED, delta->status); - return -1; + return 0; } void test_diff_patch__can_properly_display_the_removal_of_a_file(void) -- cgit v1.2.1 From 52877c897504ed610bc957b88b6ba9e442077c07 Mon Sep 17 00:00:00 2001 From: Michael Schubert Date: Tue, 1 May 2012 14:28:18 +0200 Subject: tests-clar/diff: mark output_len unused --- tests-clar/diff/patch.c | 1 + 1 file changed, 1 insertion(+) (limited to 'tests-clar/diff/patch.c') diff --git a/tests-clar/diff/patch.c b/tests-clar/diff/patch.c index 3da9ce82b..fdb79a475 100644 --- a/tests-clar/diff/patch.c +++ b/tests-clar/diff/patch.c @@ -30,6 +30,7 @@ static int check_removal_cb( size_t output_len) { GIT_UNUSED(cb_data); + GIT_UNUSED(output_len); switch (line_origin) { case GIT_DIFF_LINE_FILE_HDR: -- cgit v1.2.1 From 16b83019af63d837b6934bcf1b71b8697d5d94c8 Mon Sep 17 00:00:00 2001 From: Russell Belfer Date: Sun, 4 Mar 2012 23:28:36 -0800 Subject: Fix usage of "new" for fieldname in public header This should restore the ability to include libgit2 headers in C++ projects. Cherry picked 2de60205dfea2c4a422b2108a5e8605f97c2e895 from development into new-error-handling. --- tests-clar/diff/patch.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'tests-clar/diff/patch.c') diff --git a/tests-clar/diff/patch.c b/tests-clar/diff/patch.c index fdb79a475..05e748667 100644 --- a/tests-clar/diff/patch.c +++ b/tests-clar/diff/patch.c @@ -59,8 +59,8 @@ check_range: cl_assert_equal_i(0, range->new_lines); check_delta: - cl_assert_equal_s("subdir.txt", delta->old.path); - cl_assert_equal_s("subdir.txt", delta->new.path); + cl_assert_equal_s("subdir.txt", delta->old_file.path); + cl_assert_equal_s("subdir.txt", delta->new_file.path); cl_assert_equal_i(GIT_DELTA_DELETED, delta->status); return 0; -- cgit v1.2.1