]> git.proxmox.com Git - libgit2.git/blame_incremental - tests/index/addall.c
New upstream version 1.3.0+dfsg.1
[libgit2.git] / tests / index / addall.c
... / ...
CommitLineData
1#include "clar_libgit2.h"
2#include "../status/status_helpers.h"
3#include "posix.h"
4#include "futils.h"
5
6static git_repository *g_repo = NULL;
7#define TEST_DIR "addall"
8
9void test_index_addall__initialize(void)
10{
11}
12
13void test_index_addall__cleanup(void)
14{
15 cl_git_sandbox_cleanup();
16}
17
18#define STATUS_INDEX_FLAGS \
19 (GIT_STATUS_INDEX_NEW | GIT_STATUS_INDEX_MODIFIED | \
20 GIT_STATUS_INDEX_DELETED | GIT_STATUS_INDEX_RENAMED | \
21 GIT_STATUS_INDEX_TYPECHANGE)
22
23#define STATUS_WT_FLAGS \
24 (GIT_STATUS_WT_NEW | GIT_STATUS_WT_MODIFIED | \
25 GIT_STATUS_WT_DELETED | GIT_STATUS_WT_TYPECHANGE | \
26 GIT_STATUS_WT_RENAMED)
27
28typedef struct {
29 size_t index_adds;
30 size_t index_dels;
31 size_t index_mods;
32 size_t wt_adds;
33 size_t wt_dels;
34 size_t wt_mods;
35 size_t ignores;
36 size_t conflicts;
37} index_status_counts;
38
39static int index_status_cb(
40 const char *path, unsigned int status_flags, void *payload)
41{
42 index_status_counts *vals = payload;
43
44 /* cb_status__print(path, status_flags, NULL); */
45
46 GIT_UNUSED(path);
47
48 if (status_flags & GIT_STATUS_INDEX_NEW)
49 vals->index_adds++;
50 if (status_flags & GIT_STATUS_INDEX_MODIFIED)
51 vals->index_mods++;
52 if (status_flags & GIT_STATUS_INDEX_DELETED)
53 vals->index_dels++;
54 if (status_flags & GIT_STATUS_INDEX_TYPECHANGE)
55 vals->index_mods++;
56
57 if (status_flags & GIT_STATUS_WT_NEW)
58 vals->wt_adds++;
59 if (status_flags & GIT_STATUS_WT_MODIFIED)
60 vals->wt_mods++;
61 if (status_flags & GIT_STATUS_WT_DELETED)
62 vals->wt_dels++;
63 if (status_flags & GIT_STATUS_WT_TYPECHANGE)
64 vals->wt_mods++;
65
66 if (status_flags & GIT_STATUS_IGNORED)
67 vals->ignores++;
68 if (status_flags & GIT_STATUS_CONFLICTED)
69 vals->conflicts++;
70
71 return 0;
72}
73
74static void check_status_at_line(
75 git_repository *repo,
76 size_t index_adds, size_t index_dels, size_t index_mods,
77 size_t wt_adds, size_t wt_dels, size_t wt_mods, size_t ignores,
78 size_t conflicts, const char *file, const char *func, int line)
79{
80 index_status_counts vals;
81
82 memset(&vals, 0, sizeof(vals));
83
84 cl_git_pass(git_status_foreach(repo, index_status_cb, &vals));
85
86 clar__assert_equal(
87 file,func,line,"wrong index adds", 1, "%"PRIuZ, index_adds, vals.index_adds);
88 clar__assert_equal(
89 file,func,line,"wrong index dels", 1, "%"PRIuZ, index_dels, vals.index_dels);
90 clar__assert_equal(
91 file,func,line,"wrong index mods", 1, "%"PRIuZ, index_mods, vals.index_mods);
92 clar__assert_equal(
93 file,func,line,"wrong workdir adds", 1, "%"PRIuZ, wt_adds, vals.wt_adds);
94 clar__assert_equal(
95 file,func,line,"wrong workdir dels", 1, "%"PRIuZ, wt_dels, vals.wt_dels);
96 clar__assert_equal(
97 file,func,line,"wrong workdir mods", 1, "%"PRIuZ, wt_mods, vals.wt_mods);
98 clar__assert_equal(
99 file,func,line,"wrong ignores", 1, "%"PRIuZ, ignores, vals.ignores);
100 clar__assert_equal(
101 file,func,line,"wrong conflicts", 1, "%"PRIuZ, conflicts, vals.conflicts);
102}
103
104#define check_status(R,IA,ID,IM,WA,WD,WM,IG,C) \
105 check_status_at_line(R,IA,ID,IM,WA,WD,WM,IG,C,__FILE__,__func__,__LINE__)
106
107static void check_stat_data(git_index *index, const char *path, bool match)
108{
109 const git_index_entry *entry;
110 struct stat st;
111
112 cl_must_pass(p_lstat(path, &st));
113
114 /* skip repo base dir name */
115 while (*path != '/')
116 ++path;
117 ++path;
118
119 entry = git_index_get_bypath(index, path, 0);
120 cl_assert(entry);
121
122 if (match) {
123 cl_assert(st.st_ctime == entry->ctime.seconds);
124 cl_assert(st.st_mtime == entry->mtime.seconds);
125 cl_assert(st.st_size == entry->file_size);
126 cl_assert((uint32_t)st.st_uid == entry->uid);
127 cl_assert((uint32_t)st.st_gid == entry->gid);
128 cl_assert_equal_i_fmt(
129 GIT_MODE_TYPE(st.st_mode), GIT_MODE_TYPE(entry->mode), "%07o");
130 if (cl_is_chmod_supported())
131 cl_assert_equal_b(
132 GIT_PERMS_IS_EXEC(st.st_mode), GIT_PERMS_IS_EXEC(entry->mode));
133 } else {
134 /* most things will still match */
135 cl_assert(st.st_size != entry->file_size);
136 /* would check mtime, but with second resolution it won't work :( */
137 }
138}
139
140static void addall_create_test_repo(bool check_every_step)
141{
142 g_repo = cl_git_sandbox_init_new(TEST_DIR);
143
144 if (check_every_step)
145 check_status(g_repo, 0, 0, 0, 0, 0, 0, 0, 0);
146
147 cl_git_mkfile(TEST_DIR "/file.foo", "a file");
148 if (check_every_step)
149 check_status(g_repo, 0, 0, 0, 1, 0, 0, 0, 0);
150
151 cl_git_mkfile(TEST_DIR "/.gitignore", "*.foo\n");
152 if (check_every_step)
153 check_status(g_repo, 0, 0, 0, 1, 0, 0, 1, 0);
154
155 cl_git_mkfile(TEST_DIR "/file.bar", "another file");
156 if (check_every_step)
157 check_status(g_repo, 0, 0, 0, 2, 0, 0, 1, 0);
158}
159
160void test_index_addall__repo_lifecycle(void)
161{
162 int error;
163 git_index *index;
164 git_strarray paths = { NULL, 0 };
165 char *strs[1];
166
167 addall_create_test_repo(true);
168
169 cl_git_pass(git_repository_index(&index, g_repo));
170
171 strs[0] = "file.*";
172 paths.strings = strs;
173 paths.count = 1;
174
175 cl_git_pass(git_index_add_all(index, &paths, 0, NULL, NULL));
176 cl_git_pass(git_index_write(index));
177 check_stat_data(index, TEST_DIR "/file.bar", true);
178 check_status(g_repo, 1, 0, 0, 1, 0, 0, 1, 0);
179
180 cl_git_rewritefile(TEST_DIR "/file.bar", "new content for file");
181 check_stat_data(index, TEST_DIR "/file.bar", false);
182 check_status(g_repo, 1, 0, 0, 1, 0, 1, 1, 0);
183
184 cl_git_mkfile(TEST_DIR "/file.zzz", "yet another one");
185 cl_git_mkfile(TEST_DIR "/other.zzz", "yet another one");
186 cl_git_mkfile(TEST_DIR "/more.zzz", "yet another one");
187 check_status(g_repo, 1, 0, 0, 4, 0, 1, 1, 0);
188
189 cl_git_pass(git_index_update_all(index, NULL, NULL, NULL));
190 check_stat_data(index, TEST_DIR "/file.bar", true);
191 check_status(g_repo, 1, 0, 0, 4, 0, 0, 1, 0);
192
193 cl_git_pass(git_index_add_all(index, &paths, 0, NULL, NULL));
194 cl_git_pass(git_index_write(index));
195 check_stat_data(index, TEST_DIR "/file.zzz", true);
196 check_status(g_repo, 2, 0, 0, 3, 0, 0, 1, 0);
197
198 cl_repo_commit_from_index(NULL, g_repo, NULL, 0, "first commit");
199 check_status(g_repo, 0, 0, 0, 3, 0, 0, 1, 0);
200
201 if (cl_repo_get_bool(g_repo, "core.filemode")) {
202 cl_git_pass(git_index_update_all(index, NULL, NULL, NULL));
203 cl_must_pass(p_chmod(TEST_DIR "/file.zzz", 0777));
204 cl_git_pass(git_index_update_all(index, NULL, NULL, NULL));
205 check_status(g_repo, 0, 0, 1, 3, 0, 0, 1, 0);
206
207 /* go back to what we had before */
208 cl_must_pass(p_chmod(TEST_DIR "/file.zzz", 0666));
209 cl_git_pass(git_index_update_all(index, NULL, NULL, NULL));
210 check_status(g_repo, 0, 0, 0, 3, 0, 0, 1, 0);
211 }
212
213
214 /* attempt to add an ignored file - does nothing */
215 strs[0] = "file.foo";
216 cl_git_pass(git_index_add_all(index, &paths, 0, NULL, NULL));
217 cl_git_pass(git_index_write(index));
218 check_status(g_repo, 0, 0, 0, 3, 0, 0, 1, 0);
219
220 /* add with check - should generate error */
221 error = git_index_add_all(
222 index, &paths, GIT_INDEX_ADD_CHECK_PATHSPEC, NULL, NULL);
223 cl_assert_equal_i(GIT_EINVALIDSPEC, error);
224 cl_git_pass(git_index_write(index));
225 check_status(g_repo, 0, 0, 0, 3, 0, 0, 1, 0);
226
227 /* add with force - should allow */
228 cl_git_pass(git_index_add_all(
229 index, &paths, GIT_INDEX_ADD_FORCE, NULL, NULL));
230 cl_git_pass(git_index_write(index));
231 check_stat_data(index, TEST_DIR "/file.foo", true);
232 check_status(g_repo, 1, 0, 0, 3, 0, 0, 0, 0);
233
234 /* now it's in the index, so regular add should work */
235 cl_git_rewritefile(TEST_DIR "/file.foo", "new content for file");
236 check_stat_data(index, TEST_DIR "/file.foo", false);
237 check_status(g_repo, 1, 0, 0, 3, 0, 1, 0, 0);
238
239 cl_git_pass(git_index_add_all(index, &paths, 0, NULL, NULL));
240 cl_git_pass(git_index_write(index));
241 check_stat_data(index, TEST_DIR "/file.foo", true);
242 check_status(g_repo, 1, 0, 0, 3, 0, 0, 0, 0);
243
244 cl_git_pass(git_index_add_bypath(index, "more.zzz"));
245 check_stat_data(index, TEST_DIR "/more.zzz", true);
246 check_status(g_repo, 2, 0, 0, 2, 0, 0, 0, 0);
247
248 cl_git_rewritefile(TEST_DIR "/file.zzz", "new content for file");
249 check_status(g_repo, 2, 0, 0, 2, 0, 1, 0, 0);
250
251 cl_git_pass(git_index_add_bypath(index, "file.zzz"));
252 check_stat_data(index, TEST_DIR "/file.zzz", true);
253 check_status(g_repo, 2, 0, 1, 2, 0, 0, 0, 0);
254
255 strs[0] = "*.zzz";
256 cl_git_pass(git_index_remove_all(index, &paths, NULL, NULL));
257 check_status(g_repo, 1, 1, 0, 4, 0, 0, 0, 0);
258
259 cl_git_pass(git_index_add_bypath(index, "file.zzz"));
260 check_status(g_repo, 1, 0, 1, 3, 0, 0, 0, 0);
261
262 cl_repo_commit_from_index(NULL, g_repo, NULL, 0, "second commit");
263 check_status(g_repo, 0, 0, 0, 3, 0, 0, 0, 0);
264
265 cl_must_pass(p_unlink(TEST_DIR "/file.zzz"));
266 check_status(g_repo, 0, 0, 0, 3, 1, 0, 0, 0);
267
268 /* update_all should be able to remove entries */
269 cl_git_pass(git_index_update_all(index, NULL, NULL, NULL));
270 check_status(g_repo, 0, 1, 0, 3, 0, 0, 0, 0);
271
272 strs[0] = "*";
273 cl_git_pass(git_index_add_all(index, &paths, 0, NULL, NULL));
274 cl_git_pass(git_index_write(index));
275 check_status(g_repo, 3, 1, 0, 0, 0, 0, 0, 0);
276
277 /* must be able to remove at any position while still updating other files */
278 cl_must_pass(p_unlink(TEST_DIR "/.gitignore"));
279 cl_git_rewritefile(TEST_DIR "/file.zzz", "reconstructed file");
280 cl_git_rewritefile(TEST_DIR "/more.zzz", "altered file reality");
281 check_status(g_repo, 3, 1, 0, 1, 1, 1, 0, 0);
282
283 cl_git_pass(git_index_update_all(index, NULL, NULL, NULL));
284 check_status(g_repo, 2, 1, 0, 1, 0, 0, 0, 0);
285 /* this behavior actually matches 'git add -u' where "file.zzz" has
286 * been removed from the index, so when you go to update, even though
287 * it exists in the HEAD, it is not re-added to the index, leaving it
288 * as a DELETE when comparing HEAD to index and as an ADD comparing
289 * index to worktree
290 */
291
292 git_index_free(index);
293}
294
295void test_index_addall__files_in_folders(void)
296{
297 git_index *index;
298
299 addall_create_test_repo(true);
300
301 cl_git_pass(git_repository_index(&index, g_repo));
302
303 cl_git_pass(git_index_add_all(index, NULL, 0, NULL, NULL));
304 cl_git_pass(git_index_write(index));
305 check_stat_data(index, TEST_DIR "/file.bar", true);
306 check_status(g_repo, 2, 0, 0, 0, 0, 0, 1, 0);
307
308 cl_must_pass(p_mkdir(TEST_DIR "/subdir", 0777));
309 cl_git_mkfile(TEST_DIR "/subdir/file", "hello!\n");
310 check_status(g_repo, 2, 0, 0, 1, 0, 0, 1, 0);
311
312 cl_git_pass(git_index_add_all(index, NULL, 0, NULL, NULL));
313 cl_git_pass(git_index_write(index));
314 check_status(g_repo, 3, 0, 0, 0, 0, 0, 1, 0);
315
316 git_index_free(index);
317}
318
319void test_index_addall__hidden_files(void)
320{
321#ifdef GIT_WIN32
322 git_index *index;
323
324 addall_create_test_repo(true);
325
326 cl_git_pass(git_repository_index(&index, g_repo));
327
328 cl_git_pass(git_index_add_all(index, NULL, 0, NULL, NULL));
329 cl_git_pass(git_index_write(index));
330 check_stat_data(index, TEST_DIR "/file.bar", true);
331 check_status(g_repo, 2, 0, 0, 0, 0, 0, 1, 0);
332
333 cl_git_mkfile(TEST_DIR "/file.zzz", "yet another one");
334 cl_git_mkfile(TEST_DIR "/more.zzz", "yet another one");
335 cl_git_mkfile(TEST_DIR "/other.zzz", "yet another one");
336
337 check_status(g_repo, 2, 0, 0, 3, 0, 0, 1, 0);
338
339 cl_git_pass(git_win32__set_hidden(TEST_DIR "/file.zzz", true));
340 cl_git_pass(git_win32__set_hidden(TEST_DIR "/more.zzz", true));
341 cl_git_pass(git_win32__set_hidden(TEST_DIR "/other.zzz", true));
342
343 check_status(g_repo, 2, 0, 0, 3, 0, 0, 1, 0);
344
345 cl_git_pass(git_index_add_all(index, NULL, 0, NULL, NULL));
346 cl_git_pass(git_index_write(index));
347 check_stat_data(index, TEST_DIR "/file.bar", true);
348 check_status(g_repo, 5, 0, 0, 0, 0, 0, 1, 0);
349
350 git_index_free(index);
351#endif
352}
353
354static int addall_match_prefix(
355 const char *path, const char *matched_pathspec, void *payload)
356{
357 GIT_UNUSED(matched_pathspec);
358 return !git__prefixcmp(path, payload) ? 0 : 1;
359}
360
361static int addall_match_suffix(
362 const char *path, const char *matched_pathspec, void *payload)
363{
364 GIT_UNUSED(matched_pathspec);
365 return !git__suffixcmp(path, payload) ? 0 : 1;
366}
367
368static int addall_cancel_at(
369 const char *path, const char *matched_pathspec, void *payload)
370{
371 GIT_UNUSED(matched_pathspec);
372 return !strcmp(path, payload) ? -123 : 0;
373}
374
375void test_index_addall__callback_filtering(void)
376{
377 git_index *index;
378
379 addall_create_test_repo(false);
380
381 cl_git_pass(git_repository_index(&index, g_repo));
382
383 cl_git_pass(
384 git_index_add_all(index, NULL, 0, addall_match_prefix, "file."));
385 cl_git_pass(git_index_write(index));
386 check_stat_data(index, TEST_DIR "/file.bar", true);
387 check_status(g_repo, 1, 0, 0, 1, 0, 0, 1, 0);
388
389 cl_git_mkfile(TEST_DIR "/file.zzz", "yet another one");
390 cl_git_mkfile(TEST_DIR "/more.zzz", "yet another one");
391 cl_git_mkfile(TEST_DIR "/other.zzz", "yet another one");
392
393 cl_git_pass(git_index_update_all(index, NULL, NULL, NULL));
394 check_stat_data(index, TEST_DIR "/file.bar", true);
395 check_status(g_repo, 1, 0, 0, 4, 0, 0, 1, 0);
396
397 cl_git_pass(
398 git_index_add_all(index, NULL, 0, addall_match_prefix, "other"));
399 cl_git_pass(git_index_write(index));
400 check_stat_data(index, TEST_DIR "/other.zzz", true);
401 check_status(g_repo, 2, 0, 0, 3, 0, 0, 1, 0);
402
403 cl_git_pass(
404 git_index_add_all(index, NULL, 0, addall_match_suffix, ".zzz"));
405 cl_git_pass(git_index_write(index));
406 check_status(g_repo, 4, 0, 0, 1, 0, 0, 1, 0);
407
408 cl_git_pass(
409 git_index_remove_all(index, NULL, addall_match_suffix, ".zzz"));
410 check_status(g_repo, 1, 0, 0, 4, 0, 0, 1, 0);
411
412 cl_git_fail_with(
413 git_index_add_all(index, NULL, 0, addall_cancel_at, "more.zzz"), -123);
414 check_status(g_repo, 3, 0, 0, 2, 0, 0, 1, 0);
415
416 cl_git_fail_with(
417 git_index_add_all(index, NULL, 0, addall_cancel_at, "other.zzz"), -123);
418 check_status(g_repo, 4, 0, 0, 1, 0, 0, 1, 0);
419
420 cl_git_pass(
421 git_index_add_all(index, NULL, 0, addall_match_suffix, ".zzz"));
422 cl_git_pass(git_index_write(index));
423 check_status(g_repo, 5, 0, 0, 0, 0, 0, 1, 0);
424
425 cl_must_pass(p_unlink(TEST_DIR "/file.zzz"));
426 cl_must_pass(p_unlink(TEST_DIR "/more.zzz"));
427 cl_must_pass(p_unlink(TEST_DIR "/other.zzz"));
428
429 cl_git_fail_with(
430 git_index_update_all(index, NULL, addall_cancel_at, "more.zzz"), -123);
431 /* file.zzz removed from index (so Index Adds 5 -> 4) and
432 * more.zzz + other.zzz removed (so Worktree Dels 0 -> 2) */
433 check_status(g_repo, 4, 0, 0, 0, 2, 0, 1, 0);
434
435 cl_git_fail_with(
436 git_index_update_all(index, NULL, addall_cancel_at, "other.zzz"), -123);
437 /* more.zzz removed from index (so Index Adds 4 -> 3) and
438 * Just other.zzz removed (so Worktree Dels 2 -> 1) */
439 check_status(g_repo, 3, 0, 0, 0, 1, 0, 1, 0);
440
441 git_index_free(index);
442}
443
444void test_index_addall__adds_conflicts(void)
445{
446 git_index *index;
447 git_reference *ref;
448 git_annotated_commit *annotated;
449
450 g_repo = cl_git_sandbox_init("merge-resolve");
451 cl_git_pass(git_repository_index(&index, g_repo));
452
453 check_status(g_repo, 0, 0, 0, 0, 0, 0, 0, 0);
454
455 cl_git_pass(git_reference_lookup(&ref, g_repo, "refs/heads/branch"));
456 cl_git_pass(git_annotated_commit_from_ref(&annotated, g_repo, ref));
457
458 cl_git_pass(git_merge(g_repo, (const git_annotated_commit**)&annotated, 1, NULL, NULL));
459 check_status(g_repo, 0, 1, 2, 0, 0, 0, 0, 1);
460
461 cl_git_pass(git_index_add_all(index, NULL, 0, NULL, NULL));
462 cl_git_pass(git_index_write(index));
463 check_status(g_repo, 0, 1, 3, 0, 0, 0, 0, 0);
464
465 git_annotated_commit_free(annotated);
466 git_reference_free(ref);
467 git_index_free(index);
468}
469
470void test_index_addall__removes_deleted_conflicted_files(void)
471{
472 git_index *index;
473 git_reference *ref;
474 git_annotated_commit *annotated;
475
476 g_repo = cl_git_sandbox_init("merge-resolve");
477 cl_git_pass(git_repository_index(&index, g_repo));
478
479 check_status(g_repo, 0, 0, 0, 0, 0, 0, 0, 0);
480
481 cl_git_pass(git_reference_lookup(&ref, g_repo, "refs/heads/branch"));
482 cl_git_pass(git_annotated_commit_from_ref(&annotated, g_repo, ref));
483
484 cl_git_pass(git_merge(g_repo, (const git_annotated_commit**)&annotated, 1, NULL, NULL));
485 check_status(g_repo, 0, 1, 2, 0, 0, 0, 0, 1);
486
487 cl_git_rmfile("merge-resolve/conflicting.txt");
488
489 cl_git_pass(git_index_add_all(index, NULL, 0, NULL, NULL));
490 cl_git_pass(git_index_write(index));
491 check_status(g_repo, 0, 2, 2, 0, 0, 0, 0, 0);
492
493 git_annotated_commit_free(annotated);
494 git_reference_free(ref);
495 git_index_free(index);
496}