]> git.proxmox.com Git - libgit2.git/blob - tests/repo/pathspec.c
New upstream version 1.4.3+dfsg.1
[libgit2.git] / tests / repo / pathspec.c
1 #include "clar_libgit2.h"
2 #include "git2/pathspec.h"
3
4 static git_repository *g_repo;
5
6 void test_repo_pathspec__initialize(void)
7 {
8 g_repo = cl_git_sandbox_init("status");
9 }
10
11 void test_repo_pathspec__cleanup(void)
12 {
13 cl_git_sandbox_cleanup();
14 g_repo = NULL;
15 }
16
17 static char *str0[] = { "*_file", "new_file", "garbage" };
18 static char *str1[] = { "*_FILE", "NEW_FILE", "GARBAGE" };
19 static char *str2[] = { "staged_*" };
20 static char *str3[] = { "!subdir", "*_file", "new_file" };
21 static char *str4[] = { "*" };
22 static char *str5[] = { "S*" };
23
24 void test_repo_pathspec__workdir0(void)
25 {
26 git_strarray s;
27 git_pathspec *ps;
28 git_pathspec_match_list *m;
29
30 /* { "*_file", "new_file", "garbage" } */
31 s.strings = str0; s.count = ARRAY_SIZE(str0);
32 cl_git_pass(git_pathspec_new(&ps, &s));
33
34 cl_git_pass(git_pathspec_match_workdir(&m, g_repo, 0, ps));
35 cl_assert_equal_sz(10, git_pathspec_match_list_entrycount(m));
36 cl_assert_equal_sz(0, git_pathspec_match_list_failed_entrycount(m));
37 git_pathspec_match_list_free(m);
38
39 cl_git_pass(git_pathspec_match_workdir(&m, g_repo,
40 GIT_PATHSPEC_FIND_FAILURES, ps));
41 cl_assert_equal_sz(10, git_pathspec_match_list_entrycount(m));
42 cl_assert_equal_sz(1, git_pathspec_match_list_failed_entrycount(m));
43 cl_assert_equal_s("garbage", git_pathspec_match_list_failed_entry(m, 0));
44 git_pathspec_match_list_free(m);
45
46 cl_git_pass(git_pathspec_match_workdir(&m, g_repo,
47 GIT_PATHSPEC_FIND_FAILURES | GIT_PATHSPEC_FAILURES_ONLY, ps));
48 cl_assert_equal_sz(0, git_pathspec_match_list_entrycount(m));
49 cl_assert_equal_sz(1, git_pathspec_match_list_failed_entrycount(m));
50 git_pathspec_match_list_free(m);
51
52 git_pathspec_free(ps);
53 }
54
55 void test_repo_pathspec__workdir1(void)
56 {
57 git_strarray s;
58 git_pathspec *ps;
59 git_pathspec_match_list *m;
60
61 /* { "*_FILE", "NEW_FILE", "GARBAGE" } */
62 s.strings = str1; s.count = ARRAY_SIZE(str1);
63 cl_git_pass(git_pathspec_new(&ps, &s));
64
65 cl_git_pass(git_pathspec_match_workdir(&m, g_repo,
66 GIT_PATHSPEC_IGNORE_CASE, ps));
67 cl_assert_equal_sz(10, git_pathspec_match_list_entrycount(m));
68 git_pathspec_match_list_free(m);
69
70 cl_git_pass(git_pathspec_match_workdir(&m, g_repo,
71 GIT_PATHSPEC_USE_CASE, ps));
72 cl_assert_equal_sz(0, git_pathspec_match_list_entrycount(m));
73 git_pathspec_match_list_free(m);
74
75 cl_git_fail(git_pathspec_match_workdir(&m, g_repo,
76 GIT_PATHSPEC_USE_CASE | GIT_PATHSPEC_NO_MATCH_ERROR, ps));
77
78 cl_git_pass(git_pathspec_match_workdir(&m, g_repo,
79 GIT_PATHSPEC_IGNORE_CASE | GIT_PATHSPEC_FIND_FAILURES, ps));
80 cl_assert_equal_sz(10, git_pathspec_match_list_entrycount(m));
81 cl_assert_equal_sz(1, git_pathspec_match_list_failed_entrycount(m));
82 git_pathspec_match_list_free(m);
83
84 cl_git_pass(git_pathspec_match_workdir(&m, g_repo,
85 GIT_PATHSPEC_USE_CASE | GIT_PATHSPEC_FIND_FAILURES, ps));
86 cl_assert_equal_sz(0, git_pathspec_match_list_entrycount(m));
87 cl_assert_equal_sz(3, git_pathspec_match_list_failed_entrycount(m));
88 git_pathspec_match_list_free(m);
89
90 git_pathspec_free(ps);
91 }
92
93 void test_repo_pathspec__workdir2(void)
94 {
95 git_strarray s;
96 git_pathspec *ps;
97 git_pathspec_match_list *m;
98
99 /* { "staged_*" } */
100 s.strings = str2; s.count = ARRAY_SIZE(str2);
101 cl_git_pass(git_pathspec_new(&ps, &s));
102
103 cl_git_pass(git_pathspec_match_workdir(&m, g_repo, 0, ps));
104 cl_assert_equal_sz(5, git_pathspec_match_list_entrycount(m));
105 git_pathspec_match_list_free(m);
106
107 cl_git_pass(git_pathspec_match_workdir(&m, g_repo,
108 GIT_PATHSPEC_FIND_FAILURES, ps));
109 cl_assert_equal_sz(5, git_pathspec_match_list_entrycount(m));
110 cl_assert_equal_sz(0, git_pathspec_match_list_failed_entrycount(m));
111 git_pathspec_match_list_free(m);
112
113 cl_git_fail(git_pathspec_match_workdir(&m, g_repo,
114 GIT_PATHSPEC_NO_GLOB | GIT_PATHSPEC_NO_MATCH_ERROR, ps));
115
116 cl_git_pass(git_pathspec_match_workdir(&m, g_repo,
117 GIT_PATHSPEC_NO_GLOB | GIT_PATHSPEC_FIND_FAILURES, ps));
118 cl_assert_equal_sz(0, git_pathspec_match_list_entrycount(m));
119 cl_assert_equal_sz(1, git_pathspec_match_list_failed_entrycount(m));
120 git_pathspec_match_list_free(m);
121
122 git_pathspec_free(ps);
123 }
124
125 void test_repo_pathspec__workdir3(void)
126 {
127 git_strarray s;
128 git_pathspec *ps;
129 git_pathspec_match_list *m;
130
131 /* { "!subdir", "*_file", "new_file" } */
132 s.strings = str3; s.count = ARRAY_SIZE(str3);
133 cl_git_pass(git_pathspec_new(&ps, &s));
134
135 cl_git_pass(git_pathspec_match_workdir(&m, g_repo, 0, ps));
136 cl_assert_equal_sz(7, git_pathspec_match_list_entrycount(m));
137 git_pathspec_match_list_free(m);
138
139 cl_git_pass(git_pathspec_match_workdir(&m, g_repo,
140 GIT_PATHSPEC_FIND_FAILURES, ps));
141 cl_assert_equal_sz(7, git_pathspec_match_list_entrycount(m));
142 cl_assert_equal_sz(0, git_pathspec_match_list_failed_entrycount(m));
143
144 cl_assert_equal_s("current_file", git_pathspec_match_list_entry(m, 0));
145 cl_assert_equal_s("modified_file", git_pathspec_match_list_entry(m, 1));
146 cl_assert_equal_s("new_file", git_pathspec_match_list_entry(m, 2));
147 cl_assert_equal_s("staged_changes_modified_file", git_pathspec_match_list_entry(m, 3));
148 cl_assert_equal_s("staged_delete_modified_file", git_pathspec_match_list_entry(m, 4));
149 cl_assert_equal_s("staged_new_file", git_pathspec_match_list_entry(m, 5));
150 cl_assert_equal_s("staged_new_file_modified_file", git_pathspec_match_list_entry(m, 6));
151 cl_assert_equal_s(NULL, git_pathspec_match_list_entry(m, 7));
152
153 git_pathspec_match_list_free(m);
154
155 git_pathspec_free(ps);
156 }
157
158 void test_repo_pathspec__workdir4(void)
159 {
160 git_strarray s;
161 git_pathspec *ps;
162 git_pathspec_match_list *m;
163
164 /* { "*" } */
165 s.strings = str4; s.count = ARRAY_SIZE(str4);
166 cl_git_pass(git_pathspec_new(&ps, &s));
167
168 cl_git_pass(git_pathspec_match_workdir(&m, g_repo, 0, ps));
169 cl_assert_equal_sz(13, git_pathspec_match_list_entrycount(m));
170 cl_assert_equal_s("\xE8\xBF\x99", git_pathspec_match_list_entry(m, 12));
171 git_pathspec_match_list_free(m);
172
173 git_pathspec_free(ps);
174 }
175
176
177 void test_repo_pathspec__index0(void)
178 {
179 git_index *idx;
180 git_strarray s;
181 git_pathspec *ps;
182 git_pathspec_match_list *m;
183
184 cl_git_pass(git_repository_index(&idx, g_repo));
185
186 /* { "*_file", "new_file", "garbage" } */
187 s.strings = str0; s.count = ARRAY_SIZE(str0);
188 cl_git_pass(git_pathspec_new(&ps, &s));
189
190 cl_git_pass(git_pathspec_match_index(&m, idx, 0, ps));
191 cl_assert_equal_sz(9, git_pathspec_match_list_entrycount(m));
192 cl_assert_equal_sz(0, git_pathspec_match_list_failed_entrycount(m));
193 cl_assert_equal_s("current_file", git_pathspec_match_list_entry(m, 0));
194 cl_assert_equal_s("modified_file", git_pathspec_match_list_entry(m, 1));
195 cl_assert_equal_s("staged_changes_modified_file", git_pathspec_match_list_entry(m, 2));
196 cl_assert_equal_s("staged_new_file", git_pathspec_match_list_entry(m, 3));
197 cl_assert_equal_s("staged_new_file_deleted_file", git_pathspec_match_list_entry(m, 4));
198 cl_assert_equal_s("staged_new_file_modified_file", git_pathspec_match_list_entry(m, 5));
199 cl_assert_equal_s("subdir/current_file", git_pathspec_match_list_entry(m, 6));
200 cl_assert_equal_s("subdir/deleted_file", git_pathspec_match_list_entry(m, 7));
201 cl_assert_equal_s("subdir/modified_file", git_pathspec_match_list_entry(m, 8));
202 cl_assert_equal_s(NULL, git_pathspec_match_list_entry(m, 9));
203 git_pathspec_match_list_free(m);
204
205 cl_git_pass(git_pathspec_match_index(&m, idx,
206 GIT_PATHSPEC_FIND_FAILURES, ps));
207 cl_assert_equal_sz(9, git_pathspec_match_list_entrycount(m));
208 cl_assert_equal_sz(2, git_pathspec_match_list_failed_entrycount(m));
209 cl_assert_equal_s("new_file", git_pathspec_match_list_failed_entry(m, 0));
210 cl_assert_equal_s("garbage", git_pathspec_match_list_failed_entry(m, 1));
211 cl_assert_equal_s(NULL, git_pathspec_match_list_failed_entry(m, 2));
212 git_pathspec_match_list_free(m);
213
214 git_pathspec_free(ps);
215 git_index_free(idx);
216 }
217
218 void test_repo_pathspec__index1(void)
219 {
220 /* Currently the USE_CASE and IGNORE_CASE flags don't work on the
221 * index because the index sort order for the index iterator is
222 * set by the index itself. I think the correct fix is for the
223 * index not to embed a global sort order but to support traversal
224 * in either case sensitive or insensitive order in a stateless
225 * manner.
226 *
227 * Anyhow, as it is, there is no point in doing this test.
228 */
229 #if 0
230 git_index *idx;
231 git_strarray s;
232 git_pathspec *ps;
233 git_pathspec_match_list *m;
234
235 cl_git_pass(git_repository_index(&idx, g_repo));
236
237 /* { "*_FILE", "NEW_FILE", "GARBAGE" } */
238 s.strings = str1; s.count = ARRAY_SIZE(str1);
239 cl_git_pass(git_pathspec_new(&ps, &s));
240
241 cl_git_pass(git_pathspec_match_index(&m, idx,
242 GIT_PATHSPEC_USE_CASE, ps));
243 cl_assert_equal_sz(0, git_pathspec_match_list_entrycount(m));
244 cl_assert_equal_sz(0, git_pathspec_match_list_failed_entrycount(m));
245 git_pathspec_match_list_free(m);
246
247 cl_git_pass(git_pathspec_match_index(&m, idx,
248 GIT_PATHSPEC_USE_CASE | GIT_PATHSPEC_FIND_FAILURES, ps));
249 cl_assert_equal_sz(0, git_pathspec_match_list_entrycount(m));
250 cl_assert_equal_sz(3, git_pathspec_match_list_failed_entrycount(m));
251 git_pathspec_match_list_free(m);
252
253 cl_git_pass(git_pathspec_match_index(&m, idx,
254 GIT_PATHSPEC_IGNORE_CASE | GIT_PATHSPEC_FIND_FAILURES, ps));
255 cl_assert_equal_sz(10, git_pathspec_match_list_entrycount(m));
256 cl_assert_equal_sz(2, git_pathspec_match_list_failed_entrycount(m));
257 git_pathspec_match_list_free(m);
258
259 git_pathspec_free(ps);
260 git_index_free(idx);
261 #endif
262 }
263
264 void test_repo_pathspec__tree0(void)
265 {
266 git_object *tree;
267 git_strarray s;
268 git_pathspec *ps;
269 git_pathspec_match_list *m;
270
271 /* { "*_file", "new_file", "garbage" } */
272 s.strings = str0; s.count = ARRAY_SIZE(str0);
273 cl_git_pass(git_pathspec_new(&ps, &s));
274
275 cl_git_pass(git_revparse_single(&tree, g_repo, "HEAD~2^{tree}"));
276
277 cl_git_pass(git_pathspec_match_tree(&m, (git_tree *)tree,
278 GIT_PATHSPEC_FIND_FAILURES, ps));
279 cl_assert_equal_sz(4, git_pathspec_match_list_entrycount(m));
280 cl_assert_equal_s("current_file", git_pathspec_match_list_entry(m, 0));
281 cl_assert_equal_s("modified_file", git_pathspec_match_list_entry(m, 1));
282 cl_assert_equal_s("staged_changes_modified_file", git_pathspec_match_list_entry(m, 2));
283 cl_assert_equal_s("staged_delete_modified_file", git_pathspec_match_list_entry(m, 3));
284 cl_assert_equal_s(NULL, git_pathspec_match_list_entry(m, 4));
285 cl_assert_equal_sz(2, git_pathspec_match_list_failed_entrycount(m));
286 cl_assert_equal_s("new_file", git_pathspec_match_list_failed_entry(m, 0));
287 cl_assert_equal_s("garbage", git_pathspec_match_list_failed_entry(m, 1));
288 cl_assert_equal_s(NULL, git_pathspec_match_list_failed_entry(m, 2));
289 git_pathspec_match_list_free(m);
290
291 git_object_free(tree);
292
293 cl_git_pass(git_revparse_single(&tree, g_repo, "HEAD^{tree}"));
294
295 cl_git_pass(git_pathspec_match_tree(&m, (git_tree *)tree,
296 GIT_PATHSPEC_FIND_FAILURES, ps));
297 cl_assert_equal_sz(7, git_pathspec_match_list_entrycount(m));
298 cl_assert_equal_s("current_file", git_pathspec_match_list_entry(m, 0));
299 cl_assert_equal_s("modified_file", git_pathspec_match_list_entry(m, 1));
300 cl_assert_equal_s("staged_changes_modified_file", git_pathspec_match_list_entry(m, 2));
301 cl_assert_equal_s("staged_delete_modified_file", git_pathspec_match_list_entry(m, 3));
302 cl_assert_equal_s("subdir/current_file", git_pathspec_match_list_entry(m, 4));
303 cl_assert_equal_s("subdir/deleted_file", git_pathspec_match_list_entry(m, 5));
304 cl_assert_equal_s("subdir/modified_file", git_pathspec_match_list_entry(m, 6));
305 cl_assert_equal_s(NULL, git_pathspec_match_list_entry(m, 7));
306 cl_assert_equal_sz(2, git_pathspec_match_list_failed_entrycount(m));
307 cl_assert_equal_s("new_file", git_pathspec_match_list_failed_entry(m, 0));
308 cl_assert_equal_s("garbage", git_pathspec_match_list_failed_entry(m, 1));
309 cl_assert_equal_s(NULL, git_pathspec_match_list_failed_entry(m, 2));
310 git_pathspec_match_list_free(m);
311
312 git_object_free(tree);
313
314 git_pathspec_free(ps);
315 }
316
317 void test_repo_pathspec__tree5(void)
318 {
319 git_object *tree;
320 git_strarray s;
321 git_pathspec *ps;
322 git_pathspec_match_list *m;
323
324 /* { "S*" } */
325 s.strings = str5; s.count = ARRAY_SIZE(str5);
326 cl_git_pass(git_pathspec_new(&ps, &s));
327
328 cl_git_pass(git_revparse_single(&tree, g_repo, "HEAD~2^{tree}"));
329
330 cl_git_pass(git_pathspec_match_tree(&m, (git_tree *)tree,
331 GIT_PATHSPEC_USE_CASE | GIT_PATHSPEC_FIND_FAILURES, ps));
332 cl_assert_equal_sz(0, git_pathspec_match_list_entrycount(m));
333 cl_assert_equal_sz(1, git_pathspec_match_list_failed_entrycount(m));
334 git_pathspec_match_list_free(m);
335
336 cl_git_pass(git_pathspec_match_tree(&m, (git_tree *)tree,
337 GIT_PATHSPEC_IGNORE_CASE | GIT_PATHSPEC_FIND_FAILURES, ps));
338 cl_assert_equal_sz(5, git_pathspec_match_list_entrycount(m));
339 cl_assert_equal_s("staged_changes", git_pathspec_match_list_entry(m, 0));
340 cl_assert_equal_s("staged_delete_modified_file", git_pathspec_match_list_entry(m, 4));
341 cl_assert_equal_sz(0, git_pathspec_match_list_failed_entrycount(m));
342 git_pathspec_match_list_free(m);
343
344 git_object_free(tree);
345
346 cl_git_pass(git_revparse_single(&tree, g_repo, "HEAD^{tree}"));
347
348 cl_git_pass(git_pathspec_match_tree(&m, (git_tree *)tree,
349 GIT_PATHSPEC_IGNORE_CASE | GIT_PATHSPEC_FIND_FAILURES, ps));
350 cl_assert_equal_sz(9, git_pathspec_match_list_entrycount(m));
351 cl_assert_equal_s("staged_changes", git_pathspec_match_list_entry(m, 0));
352 cl_assert_equal_s("subdir.txt", git_pathspec_match_list_entry(m, 5));
353 cl_assert_equal_s("subdir/current_file", git_pathspec_match_list_entry(m, 6));
354 cl_assert_equal_sz(0, git_pathspec_match_list_failed_entrycount(m));
355 git_pathspec_match_list_free(m);
356
357 git_object_free(tree);
358
359 git_pathspec_free(ps);
360 }
361
362 void test_repo_pathspec__in_memory(void)
363 {
364 static char *strings[] = { "one", "two*", "!three*", "*four" };
365 git_strarray s = { strings, ARRAY_SIZE(strings) };
366 git_pathspec *ps;
367
368 cl_git_pass(git_pathspec_new(&ps, &s));
369
370 cl_assert(git_pathspec_matches_path(ps, 0, "one"));
371 cl_assert(!git_pathspec_matches_path(ps, 0, "ONE"));
372 cl_assert(git_pathspec_matches_path(ps, GIT_PATHSPEC_IGNORE_CASE, "ONE"));
373 cl_assert(git_pathspec_matches_path(ps, 0, "two"));
374 cl_assert(git_pathspec_matches_path(ps, 0, "two.txt"));
375 cl_assert(!git_pathspec_matches_path(ps, 0, "three.txt"));
376 cl_assert(git_pathspec_matches_path(ps, 0, "anything.four"));
377 cl_assert(!git_pathspec_matches_path(ps, 0, "three.four"));
378 cl_assert(!git_pathspec_matches_path(ps, 0, "nomatch"));
379 cl_assert(!git_pathspec_matches_path(ps, GIT_PATHSPEC_NO_GLOB, "two"));
380 cl_assert(git_pathspec_matches_path(ps, GIT_PATHSPEC_NO_GLOB, "two*"));
381 cl_assert(!git_pathspec_matches_path(ps, GIT_PATHSPEC_NO_GLOB, "anyfour"));
382 cl_assert(git_pathspec_matches_path(ps, GIT_PATHSPEC_NO_GLOB, "*four"));
383
384 git_pathspec_free(ps);
385 }