1 #include "clar_libgit2.h"
2 #include "../status/status_helpers.h"
6 git_repository
*g_repo
= NULL
;
7 #define TEST_DIR "addall"
9 void test_index_addall__initialize(void)
13 void test_index_addall__cleanup(void)
15 git_repository_free(g_repo
);
18 cl_fixture_cleanup(TEST_DIR
);
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)
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)
39 } index_status_counts
;
41 static int index_status_cb(
42 const char *path
, unsigned int status_flags
, void *payload
)
44 index_status_counts
*vals
= payload
;
46 /* cb_status__print(path, status_flags, NULL); */
50 if (status_flags
& GIT_STATUS_INDEX_NEW
)
52 if (status_flags
& GIT_STATUS_INDEX_MODIFIED
)
54 if (status_flags
& GIT_STATUS_INDEX_DELETED
)
56 if (status_flags
& GIT_STATUS_INDEX_TYPECHANGE
)
59 if (status_flags
& GIT_STATUS_WT_NEW
)
61 if (status_flags
& GIT_STATUS_WT_MODIFIED
)
63 if (status_flags
& GIT_STATUS_WT_DELETED
)
65 if (status_flags
& GIT_STATUS_WT_TYPECHANGE
)
68 if (status_flags
& GIT_STATUS_IGNORED
)
74 static void check_status_at_line(
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
)
80 index_status_counts vals
;
82 memset(&vals
, 0, sizeof(vals
));
84 cl_git_pass(git_status_foreach(repo
, index_status_cb
, &vals
));
87 file
,line
,"wrong index adds", 1, "%"PRIuZ
, index_adds
, vals
.index_adds
);
89 file
,line
,"wrong index dels", 1, "%"PRIuZ
, index_dels
, vals
.index_dels
);
91 file
,line
,"wrong index mods", 1, "%"PRIuZ
, index_mods
, vals
.index_mods
);
93 file
,line
,"wrong workdir adds", 1, "%"PRIuZ
, wt_adds
, vals
.wt_adds
);
95 file
,line
,"wrong workdir dels", 1, "%"PRIuZ
, wt_dels
, vals
.wt_dels
);
97 file
,line
,"wrong workdir mods", 1, "%"PRIuZ
, wt_mods
, vals
.wt_mods
);
99 file
,line
,"wrong ignores", 1, "%"PRIuZ
, ignores
, vals
.ignores
);
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__)
105 static void check_stat_data(git_index
*index
, const char *path
, bool match
)
107 const git_index_entry
*entry
;
110 cl_must_pass(p_lstat(path
, &st
));
112 /* skip repo base dir name */
117 entry
= git_index_get_bypath(index
, path
, 0);
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())
130 GIT_PERMS_IS_EXEC(st
.st_mode
), GIT_PERMS_IS_EXEC(entry
->mode
));
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 :( */
138 static void addall_create_test_repo(bool check_every_step
)
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);
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);
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);
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);
157 void test_index_addall__repo_lifecycle(void)
161 git_strarray paths
= { NULL
, 0 };
164 addall_create_test_repo(true);
166 cl_git_pass(git_repository_index(&index
, g_repo
));
169 paths
.strings
= strs
;
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
226 cl_git_rewritefile(TEST_DIR
"/file.zzz", "new content for file");
227 check_status(g_repo
, 2, 0, 0, 2, 0, 1, 0);
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);
234 cl_git_pass(git_index_remove_all(index
, &paths
, NULL
, NULL
));
235 check_status(g_repo
, 1, 1, 0, 4, 0, 0, 0);
237 cl_git_pass(git_index_add_bypath(index
, "file.zzz"));
238 check_status(g_repo
, 1, 0, 1, 3, 0, 0, 0);
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);
243 cl_must_pass(p_unlink(TEST_DIR
"/file.zzz"));
244 check_status(g_repo
, 0, 0, 0, 3, 1, 0, 0);
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);
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);
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);
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
269 git_index_free(index
);
272 static int addall_match_prefix(
273 const char *path
, const char *matched_pathspec
, void *payload
)
275 GIT_UNUSED(matched_pathspec
);
276 return !git__prefixcmp(path
, payload
) ? 0 : 1;
279 static int addall_match_suffix(
280 const char *path
, const char *matched_pathspec
, void *payload
)
282 GIT_UNUSED(matched_pathspec
);
283 return !git__suffixcmp(path
, payload
) ? 0 : 1;
286 static int addall_cancel_at(
287 const char *path
, const char *matched_pathspec
, void *payload
)
289 GIT_UNUSED(matched_pathspec
);
290 return !strcmp(path
, payload
) ? -123 : 0;
293 void test_index_addall__callback_filtering(void)
297 addall_create_test_repo(false);
299 cl_git_pass(git_repository_index(&index
, g_repo
));
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);
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");
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);
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);
320 git_index_add_all(index
, NULL
, 0, addall_match_suffix
, ".zzz"));
321 check_status(g_repo
, 4, 0, 0, 1, 0, 0, 1);
324 git_index_remove_all(index
, NULL
, addall_match_suffix
, ".zzz"));
325 check_status(g_repo
, 1, 0, 0, 4, 0, 0, 1);
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);
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);
336 git_index_add_all(index
, NULL
, 0, addall_match_suffix
, ".zzz"));
337 check_status(g_repo
, 5, 0, 0, 0, 0, 0, 1);
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"));
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);
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);
355 git_index_free(index
);