1 #include "clar_libgit2.h"
2 #include "git2/pathspec.h"
4 static git_repository
*g_repo
;
6 void test_repo_pathspec__initialize(void)
8 g_repo
= cl_git_sandbox_init("status");
11 void test_repo_pathspec__cleanup(void)
13 cl_git_sandbox_cleanup();
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*" };
24 void test_repo_pathspec__workdir0(void)
28 git_pathspec_match_list
*m
;
30 /* { "*_file", "new_file", "garbage" } */
31 s
.strings
= str0
; s
.count
= ARRAY_SIZE(str0
);
32 cl_git_pass(git_pathspec_new(&ps
, &s
));
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
);
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
);
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
);
52 git_pathspec_free(ps
);
55 void test_repo_pathspec__workdir1(void)
59 git_pathspec_match_list
*m
;
61 /* { "*_FILE", "NEW_FILE", "GARBAGE" } */
62 s
.strings
= str1
; s
.count
= ARRAY_SIZE(str1
);
63 cl_git_pass(git_pathspec_new(&ps
, &s
));
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
);
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
);
75 cl_git_fail(git_pathspec_match_workdir(&m
, g_repo
,
76 GIT_PATHSPEC_USE_CASE
| GIT_PATHSPEC_NO_MATCH_ERROR
, ps
));
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
);
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
);
90 git_pathspec_free(ps
);
93 void test_repo_pathspec__workdir2(void)
97 git_pathspec_match_list
*m
;
100 s
.strings
= str2
; s
.count
= ARRAY_SIZE(str2
);
101 cl_git_pass(git_pathspec_new(&ps
, &s
));
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
);
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
);
113 cl_git_fail(git_pathspec_match_workdir(&m
, g_repo
,
114 GIT_PATHSPEC_NO_GLOB
| GIT_PATHSPEC_NO_MATCH_ERROR
, ps
));
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
);
122 git_pathspec_free(ps
);
125 void test_repo_pathspec__workdir3(void)
129 git_pathspec_match_list
*m
;
131 /* { "!subdir", "*_file", "new_file" } */
132 s
.strings
= str3
; s
.count
= ARRAY_SIZE(str3
);
133 cl_git_pass(git_pathspec_new(&ps
, &s
));
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
);
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
));
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));
153 git_pathspec_match_list_free(m
);
155 git_pathspec_free(ps
);
158 void test_repo_pathspec__workdir4(void)
162 git_pathspec_match_list
*m
;
165 s
.strings
= str4
; s
.count
= ARRAY_SIZE(str4
);
166 cl_git_pass(git_pathspec_new(&ps
, &s
));
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
);
173 git_pathspec_free(ps
);
177 void test_repo_pathspec__index0(void)
182 git_pathspec_match_list
*m
;
184 cl_git_pass(git_repository_index(&idx
, g_repo
));
186 /* { "*_file", "new_file", "garbage" } */
187 s
.strings
= str0
; s
.count
= ARRAY_SIZE(str0
);
188 cl_git_pass(git_pathspec_new(&ps
, &s
));
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
);
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
);
214 git_pathspec_free(ps
);
218 void test_repo_pathspec__index1(void)
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
227 * Anyhow, as it is, there is no point in doing this test.
233 git_pathspec_match_list
*m
;
235 cl_git_pass(git_repository_index(&idx
, g_repo
));
237 /* { "*_FILE", "NEW_FILE", "GARBAGE" } */
238 s
.strings
= str1
; s
.count
= ARRAY_SIZE(str1
);
239 cl_git_pass(git_pathspec_new(&ps
, &s
));
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
);
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
);
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
);
259 git_pathspec_free(ps
);
264 void test_repo_pathspec__tree0(void)
269 git_pathspec_match_list
*m
;
271 /* { "*_file", "new_file", "garbage" } */
272 s
.strings
= str0
; s
.count
= ARRAY_SIZE(str0
);
273 cl_git_pass(git_pathspec_new(&ps
, &s
));
275 cl_git_pass(git_revparse_single(&tree
, g_repo
, "HEAD~2^{tree}"));
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
);
291 git_object_free(tree
);
293 cl_git_pass(git_revparse_single(&tree
, g_repo
, "HEAD^{tree}"));
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
);
312 git_object_free(tree
);
314 git_pathspec_free(ps
);
317 void test_repo_pathspec__tree5(void)
322 git_pathspec_match_list
*m
;
325 s
.strings
= str5
; s
.count
= ARRAY_SIZE(str5
);
326 cl_git_pass(git_pathspec_new(&ps
, &s
));
328 cl_git_pass(git_revparse_single(&tree
, g_repo
, "HEAD~2^{tree}"));
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
);
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
);
344 git_object_free(tree
);
346 cl_git_pass(git_revparse_single(&tree
, g_repo
, "HEAD^{tree}"));
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
);
357 git_object_free(tree
);
359 git_pathspec_free(ps
);
362 void test_repo_pathspec__in_memory(void)
364 static char *strings
[] = { "one", "two*", "!three*", "*four" };
365 git_strarray s
= { strings
, ARRAY_SIZE(strings
) };
368 cl_git_pass(git_pathspec_new(&ps
, &s
));
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"));
384 git_pathspec_free(ps
);