]> git.proxmox.com Git - libgit2.git/blame - tests/libgit2/diff/notify.c
New upstream version 1.5.0+ds
[libgit2.git] / tests / libgit2 / diff / notify.c
CommitLineData
0d32f39e 1#include "clar_libgit2.h"
2#include "diff_helpers.h"
3
4static git_repository *g_repo = NULL;
5
6void test_diff_notify__initialize(void)
7{
8}
9
10void test_diff_notify__cleanup(void)
11{
12 cl_git_sandbox_cleanup();
13}
14
15static int assert_called_notifications(
3ff1d123 16 const git_diff *diff_so_far,
0d32f39e 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;
25e0b157 23 notify_expected *e;
0d32f39e 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
41static 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;
3ff1d123 48 git_diff *diff = NULL;
0d32f39e 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
3138ad93 58 opts.payload = expected_matched_pathspecs;
0d32f39e 59 memset(&exp, 0, sizeof(exp));
60
61 cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts));
8147b1af 62 cl_git_pass(git_diff_foreach(diff, diff_file_cb, NULL, NULL, NULL, &exp));
0d32f39e 63
64 cl_assert_equal_i(expected_diffed_files_count, exp.files);
65
3ff1d123 66 git_diff_free(diff);
0d32f39e 67}
68
69void 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
83void 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
106void 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
132void 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
157static int abort_diff(
3ff1d123 158 const git_diff *diff_so_far,
0d32f39e 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
171void test_diff_notify__notify_cb_can_abort_diff(void)
172{
173 git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
3ff1d123 174 git_diff *diff = NULL;
0d32f39e 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";
8b22d862
RB
185 cl_git_fail_with(
186 git_diff_index_to_workdir(&diff, g_repo, NULL, &opts), -42);
0d32f39e 187
188 pathspec = "staged_changes_modified_file";
8b22d862
RB
189 cl_git_fail_with(
190 git_diff_index_to_workdir(&diff, g_repo, NULL, &opts), -42);
0d32f39e 191}
192
193static int filter_all(
3ff1d123 194 const git_diff *diff_so_far,
0d32f39e 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
207void test_diff_notify__notify_cb_can_be_used_as_filtering_function(void)
208{
209 git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
3ff1d123 210 git_diff *diff = NULL;
0d32f39e 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));
8147b1af 225 cl_git_pass(git_diff_foreach(diff, diff_file_cb, NULL, NULL, NULL, &exp));
0d32f39e 226
227 cl_assert_equal_i(0, exp.files);
228
3ff1d123 229 git_diff_free(diff);
0d32f39e 230}
3138ad93
JH
231
232static 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{
87428c55 238 GIT_UNUSED(diff_so_far);
3138ad93
JH
239 GIT_UNUSED(old_path);
240 GIT_UNUSED(new_path);
241 GIT_UNUSED(payload);
242
243 return -42;
244}
245
246void 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}