]> git.proxmox.com Git - libgit2.git/blob - tests/libgit2/diff/notify.c
New upstream version 1.5.0+ds
[libgit2.git] / tests / libgit2 / diff / notify.c
1 #include "clar_libgit2.h"
2 #include "diff_helpers.h"
3
4 static git_repository *g_repo = NULL;
5
6 void test_diff_notify__initialize(void)
7 {
8 }
9
10 void test_diff_notify__cleanup(void)
11 {
12 cl_git_sandbox_cleanup();
13 }
14
15 static int assert_called_notifications(
16 const git_diff *diff_so_far,
17 const git_diff_delta *delta_to_add,
18 const char *matched_pathspec,
19 void *payload)
20 {
21 bool found = false;
22 notify_expected *exp = (notify_expected*)payload;
23 notify_expected *e;
24
25 GIT_UNUSED(diff_so_far);
26
27 for (e = exp; e->path != NULL; e++) {
28 if (strcmp(e->path, delta_to_add->new_file.path))
29 continue;
30
31 cl_assert_equal_s(e->matched_pathspec, matched_pathspec);
32
33 found = true;
34 break;
35 }
36
37 cl_assert(found);
38 return 0;
39 }
40
41 static void test_notify(
42 char **searched_pathspecs,
43 int pathspecs_count,
44 notify_expected *expected_matched_pathspecs,
45 int expected_diffed_files_count)
46 {
47 git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
48 git_diff *diff = NULL;
49 diff_expects exp;
50
51 g_repo = cl_git_sandbox_init("status");
52
53 opts.flags |= GIT_DIFF_INCLUDE_IGNORED | GIT_DIFF_INCLUDE_UNTRACKED;
54 opts.notify_cb = assert_called_notifications;
55 opts.pathspec.strings = searched_pathspecs;
56 opts.pathspec.count = pathspecs_count;
57
58 opts.payload = expected_matched_pathspecs;
59 memset(&exp, 0, sizeof(exp));
60
61 cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts));
62 cl_git_pass(git_diff_foreach(diff, diff_file_cb, NULL, NULL, NULL, &exp));
63
64 cl_assert_equal_i(expected_diffed_files_count, exp.files);
65
66 git_diff_free(diff);
67 }
68
69 void test_diff_notify__notify_single_pathspec(void)
70 {
71 char *searched_pathspecs[] = {
72 "*_deleted",
73 };
74 notify_expected expected_matched_pathspecs[] = {
75 { "file_deleted", "*_deleted" },
76 { "staged_changes_file_deleted", "*_deleted" },
77 { NULL, NULL }
78 };
79
80 test_notify(searched_pathspecs, 1, expected_matched_pathspecs, 2);
81 }
82
83 void test_diff_notify__notify_multiple_pathspec(void)
84 {
85 char *searched_pathspecs[] = {
86 "staged_changes_cant_find_me",
87 "subdir/modified_cant_find_me",
88 "subdir/*",
89 "staged*"
90 };
91 notify_expected expected_matched_pathspecs[] = {
92 { "staged_changes_file_deleted", "staged*" },
93 { "staged_changes_modified_file", "staged*" },
94 { "staged_delete_modified_file", "staged*" },
95 { "staged_new_file_deleted_file", "staged*" },
96 { "staged_new_file_modified_file", "staged*" },
97 { "subdir/deleted_file", "subdir/*" },
98 { "subdir/modified_file", "subdir/*" },
99 { "subdir/new_file", "subdir/*" },
100 { NULL, NULL }
101 };
102
103 test_notify(searched_pathspecs, 4, expected_matched_pathspecs, 8);
104 }
105
106 void test_diff_notify__notify_catchall_with_empty_pathspecs(void)
107 {
108 char *searched_pathspecs[] = {
109 "",
110 ""
111 };
112 notify_expected expected_matched_pathspecs[] = {
113 { "file_deleted", NULL },
114 { "ignored_file", NULL },
115 { "modified_file", NULL },
116 { "new_file", NULL },
117 { "\xe8\xbf\x99", NULL },
118 { "staged_changes_file_deleted", NULL },
119 { "staged_changes_modified_file", NULL },
120 { "staged_delete_modified_file", NULL },
121 { "staged_new_file_deleted_file", NULL },
122 { "staged_new_file_modified_file", NULL },
123 { "subdir/deleted_file", NULL },
124 { "subdir/modified_file", NULL },
125 { "subdir/new_file", NULL },
126 { NULL, NULL }
127 };
128
129 test_notify(searched_pathspecs, 1, expected_matched_pathspecs, 13);
130 }
131
132 void test_diff_notify__notify_catchall(void)
133 {
134 char *searched_pathspecs[] = {
135 "*",
136 };
137 notify_expected expected_matched_pathspecs[] = {
138 { "file_deleted", "*" },
139 { "ignored_file", "*" },
140 { "modified_file", "*" },
141 { "new_file", "*" },
142 { "\xe8\xbf\x99", "*" },
143 { "staged_changes_file_deleted", "*" },
144 { "staged_changes_modified_file", "*" },
145 { "staged_delete_modified_file", "*" },
146 { "staged_new_file_deleted_file", "*" },
147 { "staged_new_file_modified_file", "*" },
148 { "subdir/deleted_file", "*" },
149 { "subdir/modified_file", "*" },
150 { "subdir/new_file", "*" },
151 { NULL, NULL }
152 };
153
154 test_notify(searched_pathspecs, 1, expected_matched_pathspecs, 13);
155 }
156
157 static int abort_diff(
158 const git_diff *diff_so_far,
159 const git_diff_delta *delta_to_add,
160 const char *matched_pathspec,
161 void *payload)
162 {
163 GIT_UNUSED(diff_so_far);
164 GIT_UNUSED(delta_to_add);
165 GIT_UNUSED(matched_pathspec);
166 GIT_UNUSED(payload);
167
168 return -42;
169 }
170
171 void test_diff_notify__notify_cb_can_abort_diff(void)
172 {
173 git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
174 git_diff *diff = NULL;
175 char *pathspec = NULL;
176
177 g_repo = cl_git_sandbox_init("status");
178
179 opts.flags |= GIT_DIFF_INCLUDE_IGNORED | GIT_DIFF_INCLUDE_UNTRACKED;
180 opts.notify_cb = abort_diff;
181 opts.pathspec.strings = &pathspec;
182 opts.pathspec.count = 1;
183
184 pathspec = "file_deleted";
185 cl_git_fail_with(
186 git_diff_index_to_workdir(&diff, g_repo, NULL, &opts), -42);
187
188 pathspec = "staged_changes_modified_file";
189 cl_git_fail_with(
190 git_diff_index_to_workdir(&diff, g_repo, NULL, &opts), -42);
191 }
192
193 static int filter_all(
194 const git_diff *diff_so_far,
195 const git_diff_delta *delta_to_add,
196 const char *matched_pathspec,
197 void *payload)
198 {
199 GIT_UNUSED(diff_so_far);
200 GIT_UNUSED(delta_to_add);
201 GIT_UNUSED(matched_pathspec);
202 GIT_UNUSED(payload);
203
204 return 42;
205 }
206
207 void test_diff_notify__notify_cb_can_be_used_as_filtering_function(void)
208 {
209 git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
210 git_diff *diff = NULL;
211 char *pathspec = NULL;
212 diff_expects exp;
213
214 g_repo = cl_git_sandbox_init("status");
215
216 opts.flags |= GIT_DIFF_INCLUDE_IGNORED | GIT_DIFF_INCLUDE_UNTRACKED;
217 opts.notify_cb = filter_all;
218 opts.pathspec.strings = &pathspec;
219 opts.pathspec.count = 1;
220
221 pathspec = "*_deleted";
222 memset(&exp, 0, sizeof(exp));
223
224 cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts));
225 cl_git_pass(git_diff_foreach(diff, diff_file_cb, NULL, NULL, NULL, &exp));
226
227 cl_assert_equal_i(0, exp.files);
228
229 git_diff_free(diff);
230 }
231
232 static int progress_abort_diff(
233 const git_diff *diff_so_far,
234 const char *old_path,
235 const char *new_path,
236 void *payload)
237 {
238 GIT_UNUSED(diff_so_far);
239 GIT_UNUSED(old_path);
240 GIT_UNUSED(new_path);
241 GIT_UNUSED(payload);
242
243 return -42;
244 }
245
246 void test_diff_notify__progress_cb_can_abort_diff(void)
247 {
248 git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
249 git_diff *diff = NULL;
250
251 g_repo = cl_git_sandbox_init("status");
252
253 opts.flags |= GIT_DIFF_INCLUDE_IGNORED | GIT_DIFF_INCLUDE_UNTRACKED;
254 opts.progress_cb = progress_abort_diff;
255
256 cl_git_fail_with(
257 git_diff_index_to_workdir(&diff, g_repo, NULL, &opts), -42);
258 }