]> git.proxmox.com Git - libgit2.git/blob - tests/index/addall.c
Merge pull request #2110 from libgit2/ed/crlf_input
[libgit2.git] / tests / index / addall.c
1 #include "clar_libgit2.h"
2 #include "../status/status_helpers.h"
3 #include "posix.h"
4 #include "fileops.h"
5
6 git_repository *g_repo = NULL;
7 #define TEST_DIR "addall"
8
9 void test_index_addall__initialize(void)
10 {
11 }
12
13 void test_index_addall__cleanup(void)
14 {
15 git_repository_free(g_repo);
16 g_repo = NULL;
17
18 cl_fixture_cleanup(TEST_DIR);
19 }
20
21 #define STATUS_INDEX_FLAGS \
22 (GIT_STATUS_INDEX_NEW | GIT_STATUS_INDEX_MODIFIED | \
23 GIT_STATUS_INDEX_DELETED | GIT_STATUS_INDEX_RENAMED | \
24 GIT_STATUS_INDEX_TYPECHANGE)
25
26 #define STATUS_WT_FLAGS \
27 (GIT_STATUS_WT_NEW | GIT_STATUS_WT_MODIFIED | \
28 GIT_STATUS_WT_DELETED | GIT_STATUS_WT_TYPECHANGE | \
29 GIT_STATUS_WT_RENAMED)
30
31 typedef struct {
32 size_t index_adds;
33 size_t index_dels;
34 size_t index_mods;
35 size_t wt_adds;
36 size_t wt_dels;
37 size_t wt_mods;
38 size_t ignores;
39 } index_status_counts;
40
41 static int index_status_cb(
42 const char *path, unsigned int status_flags, void *payload)
43 {
44 index_status_counts *vals = payload;
45
46 /* cb_status__print(path, status_flags, NULL); */
47
48 GIT_UNUSED(path);
49
50 if (status_flags & GIT_STATUS_INDEX_NEW)
51 vals->index_adds++;
52 if (status_flags & GIT_STATUS_INDEX_MODIFIED)
53 vals->index_mods++;
54 if (status_flags & GIT_STATUS_INDEX_DELETED)
55 vals->index_dels++;
56 if (status_flags & GIT_STATUS_INDEX_TYPECHANGE)
57 vals->index_mods++;
58
59 if (status_flags & GIT_STATUS_WT_NEW)
60 vals->wt_adds++;
61 if (status_flags & GIT_STATUS_WT_MODIFIED)
62 vals->wt_mods++;
63 if (status_flags & GIT_STATUS_WT_DELETED)
64 vals->wt_dels++;
65 if (status_flags & GIT_STATUS_WT_TYPECHANGE)
66 vals->wt_mods++;
67
68 if (status_flags & GIT_STATUS_IGNORED)
69 vals->ignores++;
70
71 return 0;
72 }
73
74 static 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 const char *file, 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,line,"wrong index adds", 1, "%"PRIuZ, index_adds, vals.index_adds);
88 clar__assert_equal(
89 file,line,"wrong index dels", 1, "%"PRIuZ, index_dels, vals.index_dels);
90 clar__assert_equal(
91 file,line,"wrong index mods", 1, "%"PRIuZ, index_mods, vals.index_mods);
92 clar__assert_equal(
93 file,line,"wrong workdir adds", 1, "%"PRIuZ, wt_adds, vals.wt_adds);
94 clar__assert_equal(
95 file,line,"wrong workdir dels", 1, "%"PRIuZ, wt_dels, vals.wt_dels);
96 clar__assert_equal(
97 file,line,"wrong workdir mods", 1, "%"PRIuZ, wt_mods, vals.wt_mods);
98 clar__assert_equal(
99 file,line,"wrong ignores", 1, "%"PRIuZ, ignores, vals.ignores);
100 }
101
102 #define check_status(R,IA,ID,IM,WA,WD,WM,IG) \
103 check_status_at_line(R,IA,ID,IM,WA,WD,WM,IG,__FILE__,__LINE__)
104
105 static void check_stat_data(git_index *index, const char *path, bool match)
106 {
107 const git_index_entry *entry;
108 struct stat st;
109
110 cl_must_pass(p_lstat(path, &st));
111
112 /* skip repo base dir name */
113 while (*path != '/')
114 ++path;
115 ++path;
116
117 entry = git_index_get_bypath(index, path, 0);
118 cl_assert(entry);
119
120 if (match) {
121 cl_assert(st.st_ctime == entry->ctime.seconds);
122 cl_assert(st.st_mtime == entry->mtime.seconds);
123 cl_assert(st.st_size == entry->file_size);
124 cl_assert(st.st_uid == entry->uid);
125 cl_assert(st.st_gid == entry->gid);
126 cl_assert_equal_i_fmt(
127 GIT_MODE_TYPE(st.st_mode), GIT_MODE_TYPE(entry->mode), "%07o");
128 if (cl_is_chmod_supported())
129 cl_assert_equal_b(
130 GIT_PERMS_IS_EXEC(st.st_mode), GIT_PERMS_IS_EXEC(entry->mode));
131 } else {
132 /* most things will still match */
133 cl_assert(st.st_size != entry->file_size);
134 /* would check mtime, but with second resolution it won't work :( */
135 }
136 }
137
138 static void addall_create_test_repo(bool check_every_step)
139 {
140 cl_git_pass(git_repository_init(&g_repo, TEST_DIR, false));
141 if (check_every_step)
142 check_status(g_repo, 0, 0, 0, 0, 0, 0, 0);
143
144 cl_git_mkfile(TEST_DIR "/file.foo", "a file");
145 if (check_every_step)
146 check_status(g_repo, 0, 0, 0, 1, 0, 0, 0);
147
148 cl_git_mkfile(TEST_DIR "/.gitignore", "*.foo\n");
149 if (check_every_step)
150 check_status(g_repo, 0, 0, 0, 1, 0, 0, 1);
151
152 cl_git_mkfile(TEST_DIR "/file.bar", "another file");
153 if (check_every_step)
154 check_status(g_repo, 0, 0, 0, 2, 0, 0, 1);
155 }
156
157 void test_index_addall__repo_lifecycle(void)
158 {
159 int error;
160 git_index *index;
161 git_strarray paths = { NULL, 0 };
162 char *strs[1];
163
164 addall_create_test_repo(true);
165
166 cl_git_pass(git_repository_index(&index, g_repo));
167
168 strs[0] = "file.*";
169 paths.strings = strs;
170 paths.count = 1;
171
172 cl_git_pass(git_index_add_all(index, &paths, 0, NULL, NULL));
173 check_stat_data(index, TEST_DIR "/file.bar", true);
174 check_status(g_repo, 1, 0, 0, 1, 0, 0, 1);
175
176 cl_git_rewritefile(TEST_DIR "/file.bar", "new content for file");
177 check_stat_data(index, TEST_DIR "/file.bar", false);
178 check_status(g_repo, 1, 0, 0, 1, 0, 1, 1);
179
180 cl_git_mkfile(TEST_DIR "/file.zzz", "yet another one");
181 cl_git_mkfile(TEST_DIR "/other.zzz", "yet another one");
182 cl_git_mkfile(TEST_DIR "/more.zzz", "yet another one");
183 check_status(g_repo, 1, 0, 0, 4, 0, 1, 1);
184
185 cl_git_pass(git_index_update_all(index, NULL, NULL, NULL));
186 check_stat_data(index, TEST_DIR "/file.bar", true);
187 check_status(g_repo, 1, 0, 0, 4, 0, 0, 1);
188
189 cl_git_pass(git_index_add_all(index, &paths, 0, NULL, NULL));
190 check_stat_data(index, TEST_DIR "/file.zzz", true);
191 check_status(g_repo, 2, 0, 0, 3, 0, 0, 1);
192
193 cl_repo_commit_from_index(NULL, g_repo, NULL, 0, "first commit");
194 check_status(g_repo, 0, 0, 0, 3, 0, 0, 1);
195
196 /* attempt to add an ignored file - does nothing */
197 strs[0] = "file.foo";
198 cl_git_pass(git_index_add_all(index, &paths, 0, NULL, NULL));
199 check_status(g_repo, 0, 0, 0, 3, 0, 0, 1);
200
201 /* add with check - should generate error */
202 error = git_index_add_all(
203 index, &paths, GIT_INDEX_ADD_CHECK_PATHSPEC, NULL, NULL);
204 cl_assert_equal_i(GIT_EINVALIDSPEC, error);
205 check_status(g_repo, 0, 0, 0, 3, 0, 0, 1);
206
207 /* add with force - should allow */
208 cl_git_pass(git_index_add_all(
209 index, &paths, GIT_INDEX_ADD_FORCE, NULL, NULL));
210 check_stat_data(index, TEST_DIR "/file.foo", true);
211 check_status(g_repo, 1, 0, 0, 3, 0, 0, 0);
212
213 /* now it's in the index, so regular add should work */
214 cl_git_rewritefile(TEST_DIR "/file.foo", "new content for file");
215 check_stat_data(index, TEST_DIR "/file.foo", false);
216 check_status(g_repo, 1, 0, 0, 3, 0, 1, 0);
217
218 cl_git_pass(git_index_add_all(index, &paths, 0, NULL, NULL));
219 check_stat_data(index, TEST_DIR "/file.foo", true);
220 check_status(g_repo, 1, 0, 0, 3, 0, 0, 0);
221
222 cl_git_pass(git_index_add_bypath(index, "more.zzz"));
223 check_stat_data(index, TEST_DIR "/more.zzz", true);
224 check_status(g_repo, 2, 0, 0, 2, 0, 0, 0);
225
226 cl_git_rewritefile(TEST_DIR "/file.zzz", "new content for file");
227 check_status(g_repo, 2, 0, 0, 2, 0, 1, 0);
228
229 cl_git_pass(git_index_add_bypath(index, "file.zzz"));
230 check_stat_data(index, TEST_DIR "/file.zzz", true);
231 check_status(g_repo, 2, 0, 1, 2, 0, 0, 0);
232
233 strs[0] = "*.zzz";
234 cl_git_pass(git_index_remove_all(index, &paths, NULL, NULL));
235 check_status(g_repo, 1, 1, 0, 4, 0, 0, 0);
236
237 cl_git_pass(git_index_add_bypath(index, "file.zzz"));
238 check_status(g_repo, 1, 0, 1, 3, 0, 0, 0);
239
240 cl_repo_commit_from_index(NULL, g_repo, NULL, 0, "second commit");
241 check_status(g_repo, 0, 0, 0, 3, 0, 0, 0);
242
243 cl_must_pass(p_unlink(TEST_DIR "/file.zzz"));
244 check_status(g_repo, 0, 0, 0, 3, 1, 0, 0);
245
246 /* update_all should be able to remove entries */
247 cl_git_pass(git_index_update_all(index, NULL, NULL, NULL));
248 check_status(g_repo, 0, 1, 0, 3, 0, 0, 0);
249
250 strs[0] = "*";
251 cl_git_pass(git_index_add_all(index, &paths, 0, NULL, NULL));
252 check_status(g_repo, 3, 1, 0, 0, 0, 0, 0);
253
254 /* must be able to remove at any position while still updating other files */
255 cl_must_pass(p_unlink(TEST_DIR "/.gitignore"));
256 cl_git_rewritefile(TEST_DIR "/file.zzz", "reconstructed file");
257 cl_git_rewritefile(TEST_DIR "/more.zzz", "altered file reality");
258 check_status(g_repo, 3, 1, 0, 1, 1, 1, 0);
259
260 cl_git_pass(git_index_update_all(index, NULL, NULL, NULL));
261 check_status(g_repo, 2, 1, 0, 1, 0, 0, 0);
262 /* this behavior actually matches 'git add -u' where "file.zzz" has
263 * been removed from the index, so when you go to update, even though
264 * it exists in the HEAD, it is not re-added to the index, leaving it
265 * as a DELETE when comparing HEAD to index and as an ADD comparing
266 * index to worktree
267 */
268
269 git_index_free(index);
270 }
271
272 static int addall_match_prefix(
273 const char *path, const char *matched_pathspec, void *payload)
274 {
275 GIT_UNUSED(matched_pathspec);
276 return !git__prefixcmp(path, payload) ? 0 : 1;
277 }
278
279 static int addall_match_suffix(
280 const char *path, const char *matched_pathspec, void *payload)
281 {
282 GIT_UNUSED(matched_pathspec);
283 return !git__suffixcmp(path, payload) ? 0 : 1;
284 }
285
286 static int addall_cancel_at(
287 const char *path, const char *matched_pathspec, void *payload)
288 {
289 GIT_UNUSED(matched_pathspec);
290 return !strcmp(path, payload) ? -123 : 0;
291 }
292
293 void test_index_addall__callback_filtering(void)
294 {
295 git_index *index;
296
297 addall_create_test_repo(false);
298
299 cl_git_pass(git_repository_index(&index, g_repo));
300
301 cl_git_pass(
302 git_index_add_all(index, NULL, 0, addall_match_prefix, "file."));
303 check_stat_data(index, TEST_DIR "/file.bar", true);
304 check_status(g_repo, 1, 0, 0, 1, 0, 0, 1);
305
306 cl_git_mkfile(TEST_DIR "/file.zzz", "yet another one");
307 cl_git_mkfile(TEST_DIR "/more.zzz", "yet another one");
308 cl_git_mkfile(TEST_DIR "/other.zzz", "yet another one");
309
310 cl_git_pass(git_index_update_all(index, NULL, NULL, NULL));
311 check_stat_data(index, TEST_DIR "/file.bar", true);
312 check_status(g_repo, 1, 0, 0, 4, 0, 0, 1);
313
314 cl_git_pass(
315 git_index_add_all(index, NULL, 0, addall_match_prefix, "other"));
316 check_stat_data(index, TEST_DIR "/other.zzz", true);
317 check_status(g_repo, 2, 0, 0, 3, 0, 0, 1);
318
319 cl_git_pass(
320 git_index_add_all(index, NULL, 0, addall_match_suffix, ".zzz"));
321 check_status(g_repo, 4, 0, 0, 1, 0, 0, 1);
322
323 cl_git_pass(
324 git_index_remove_all(index, NULL, addall_match_suffix, ".zzz"));
325 check_status(g_repo, 1, 0, 0, 4, 0, 0, 1);
326
327 cl_git_fail_with(
328 git_index_add_all(index, NULL, 0, addall_cancel_at, "more.zzz"), -123);
329 check_status(g_repo, 3, 0, 0, 2, 0, 0, 1);
330
331 cl_git_fail_with(
332 git_index_add_all(index, NULL, 0, addall_cancel_at, "other.zzz"), -123);
333 check_status(g_repo, 4, 0, 0, 1, 0, 0, 1);
334
335 cl_git_pass(
336 git_index_add_all(index, NULL, 0, addall_match_suffix, ".zzz"));
337 check_status(g_repo, 5, 0, 0, 0, 0, 0, 1);
338
339 cl_must_pass(p_unlink(TEST_DIR "/file.zzz"));
340 cl_must_pass(p_unlink(TEST_DIR "/more.zzz"));
341 cl_must_pass(p_unlink(TEST_DIR "/other.zzz"));
342
343 cl_git_fail_with(
344 git_index_update_all(index, NULL, addall_cancel_at, "more.zzz"), -123);
345 /* file.zzz removed from index (so Index Adds 5 -> 4) and
346 * more.zzz + other.zzz removed (so Worktree Dels 0 -> 2) */
347 check_status(g_repo, 4, 0, 0, 0, 2, 0, 1);
348
349 cl_git_fail_with(
350 git_index_update_all(index, NULL, addall_cancel_at, "other.zzz"), -123);
351 /* more.zzz removed from index (so Index Adds 4 -> 3) and
352 * Just other.zzz removed (so Worktree Dels 2 -> 1) */
353 check_status(g_repo, 3, 0, 0, 0, 1, 0, 1);
354
355 git_index_free(index);
356 }