]> git.proxmox.com Git - libgit2.git/blame - tests-clar/diff/patch.c
update internal index API to avoid cast
[libgit2.git] / tests-clar / diff / patch.c
CommitLineData
eb3d71a5 1#include "clar_libgit2.h"
2#include "diff_helpers.h"
3
4static git_repository *g_repo = NULL;
5
6void test_diff_patch__initialize(void)
7{
8 g_repo = cl_git_sandbox_init("status");
9}
10
11void test_diff_patch__cleanup(void)
12{
13 cl_git_sandbox_cleanup();
14}
15
1d2dd864 16#define EXPECTED_HEADER "diff --git a/subdir.txt b/subdir.txt\n" \
eb3d71a5 17 "deleted file mode 100644\n" \
18 "index e8ee89e..0000000\n" \
19 "--- a/subdir.txt\n" \
20 "+++ /dev/null\n"
21
1d2dd864 22#define EXPECTED_HUNK "@@ -1,2 +0,0 @@\n"
23
eb3d71a5 24static int check_removal_cb(
bae957b9
RB
25 const git_diff_delta *delta,
26 const git_diff_range *range,
eb3d71a5 27 char line_origin,
1d2dd864 28 const char *formatted_output,
793c4385
RB
29 size_t output_len,
30 void *payload)
eb3d71a5 31{
793c4385 32 GIT_UNUSED(payload);
52877c89 33 GIT_UNUSED(output_len);
eb3d71a5 34
1d2dd864 35 switch (line_origin) {
36 case GIT_DIFF_LINE_FILE_HDR:
37 cl_assert_equal_s(EXPECTED_HEADER, formatted_output);
38 cl_assert(range == NULL);
39 goto check_delta;
40
41 case GIT_DIFF_LINE_HUNK_HDR:
42 cl_assert_equal_s(EXPECTED_HUNK, formatted_output);
43 /* Fall through */
44
45 case GIT_DIFF_LINE_CONTEXT:
46 case GIT_DIFF_LINE_DELETION:
47 goto check_range;
48
49 default:
50 /* unexpected code path */
51 return -1;
52 }
53
54check_range:
55 cl_assert(range != NULL);
56 cl_assert_equal_i(1, range->old_start);
57 cl_assert_equal_i(2, range->old_lines);
58 cl_assert_equal_i(0, range->new_start);
59 cl_assert_equal_i(0, range->new_lines);
eb3d71a5 60
1d2dd864 61check_delta:
16b83019
RB
62 cl_assert_equal_s("subdir.txt", delta->old_file.path);
63 cl_assert_equal_s("subdir.txt", delta->new_file.path);
1d2dd864 64 cl_assert_equal_i(GIT_DELTA_DELETED, delta->status);
eb3d71a5 65
1d2dd864 66 return 0;
eb3d71a5 67}
68
69void test_diff_patch__can_properly_display_the_removal_of_a_file(void)
70{
71 /*
72 * $ git diff 26a125e..735b6a2
73 * diff --git a/subdir.txt b/subdir.txt
74 * deleted file mode 100644
75 * index e8ee89e..0000000
76 * --- a/subdir.txt
77 * +++ /dev/null
78 * @@ -1,2 +0,0 @@
79 * -Is it a bird?
80 * -Is it a plane?
81 */
82
83 const char *one_sha = "26a125e";
84 const char *another_sha = "735b6a2";
85 git_tree *one, *another;
86 git_diff_list *diff;
87
88 one = resolve_commit_oid_to_tree(g_repo, one_sha);
89 another = resolve_commit_oid_to_tree(g_repo, another_sha);
90
5735bf5e 91 cl_git_pass(git_diff_tree_to_tree(&diff, g_repo, one, another, NULL));
eb3d71a5 92
793c4385 93 cl_git_pass(git_diff_print_patch(diff, check_removal_cb, NULL));
eb3d71a5 94
95 git_diff_list_free(diff);
96
97 git_tree_free(another);
98 git_tree_free(one);
99}
93cf7bb8
RB
100
101void test_diff_patch__to_string(void)
102{
103 const char *one_sha = "26a125e";
104 const char *another_sha = "735b6a2";
105 git_tree *one, *another;
106 git_diff_list *diff;
107 git_diff_patch *patch;
108 char *text;
109 const char *expected = "diff --git a/subdir.txt b/subdir.txt\ndeleted file mode 100644\nindex e8ee89e..0000000\n--- a/subdir.txt\n+++ /dev/null\n@@ -1,2 +0,0 @@\n-Is it a bird?\n-Is it a plane?\n";
110
111 one = resolve_commit_oid_to_tree(g_repo, one_sha);
112 another = resolve_commit_oid_to_tree(g_repo, another_sha);
113
5735bf5e 114 cl_git_pass(git_diff_tree_to_tree(&diff, g_repo, one, another, NULL));
93cf7bb8
RB
115
116 cl_assert_equal_i(1, git_diff_num_deltas(diff));
117
118 cl_git_pass(git_diff_get_patch(&patch, NULL, diff, 0));
119
120 cl_git_pass(git_diff_patch_to_str(&text, patch));
121
122 cl_assert_equal_s(expected, text);
123
124 git__free(text);
125 git_diff_patch_free(patch);
126 git_diff_list_free(diff);
127 git_tree_free(another);
128 git_tree_free(one);
129}