1 #include "clar_libgit2.h"
2 #include "repository.h"
3 #include "../submodule/submodule_helpers.h"
5 static git_repository
*g_repo
;
6 static git_index
*g_idx
;
8 void test_index_bypath__initialize(void)
10 g_repo
= setup_fixture_submod2();
11 cl_git_pass(git_repository_index__weakptr(&g_idx
, g_repo
));
14 void test_index_bypath__cleanup(void)
20 void test_index_bypath__add_directory(void)
22 cl_git_fail_with(GIT_EDIRECTORY
, git_index_add_bypath(g_idx
, "just_a_dir"));
25 void test_index_bypath__add_submodule(void)
28 const char *sm_name
= "sm_changed_head";
30 cl_git_pass(git_submodule_status(&status
, g_repo
, sm_name
, 0));
31 cl_assert_equal_i(GIT_SUBMODULE_STATUS_WD_MODIFIED
, status
& GIT_SUBMODULE_STATUS_WD_MODIFIED
);
32 cl_git_pass(git_index_add_bypath(g_idx
, sm_name
));
33 cl_git_pass(git_submodule_status(&status
, g_repo
, sm_name
, 0));
34 cl_assert_equal_i(0, status
& GIT_SUBMODULE_STATUS_WD_MODIFIED
);
37 void test_index_bypath__add_submodule_unregistered(void)
39 const char *sm_name
= "not-submodule";
40 const char *sm_head
= "68e92c611b80ee1ed8f38314ff9577f0d15b2444";
41 const git_index_entry
*entry
;
43 cl_git_pass(git_index_add_bypath(g_idx
, sm_name
));
45 cl_assert(entry
= git_index_get_bypath(g_idx
, sm_name
, 0));
46 cl_assert_equal_s(sm_head
, git_oid_tostr_s(&entry
->id
));
47 cl_assert_equal_s(sm_name
, entry
->path
);
50 void test_index_bypath__add_hidden(void)
53 const git_index_entry
*entry
;
56 cl_git_mkfile("submod2/hidden_file", "you can't see me");
58 cl_git_pass(git_win32__hidden(&hidden
, "submod2/hidden_file"));
61 cl_git_pass(git_win32__set_hidden("submod2/hidden_file", true));
63 cl_git_pass(git_win32__hidden(&hidden
, "submod2/hidden_file"));
66 cl_git_pass(git_index_add_bypath(g_idx
, "hidden_file"));
68 cl_assert(entry
= git_index_get_bypath(g_idx
, "hidden_file", 0));
69 cl_assert_equal_i(GIT_FILEMODE_BLOB
, entry
->mode
);
73 void test_index_bypath__add_keeps_existing_case(void)
75 const git_index_entry
*entry
;
77 if (!cl_repo_get_bool(g_repo
, "core.ignorecase"))
80 cl_git_mkfile("submod2/just_a_dir/file1.txt", "This is a file");
81 cl_git_pass(git_index_add_bypath(g_idx
, "just_a_dir/file1.txt"));
83 cl_assert(entry
= git_index_get_bypath(g_idx
, "just_a_dir/file1.txt", 0));
84 cl_assert_equal_s("just_a_dir/file1.txt", entry
->path
);
86 cl_git_rewritefile("submod2/just_a_dir/file1.txt", "Updated!");
87 cl_git_pass(git_index_add_bypath(g_idx
, "just_a_dir/FILE1.txt"));
89 cl_assert(entry
= git_index_get_bypath(g_idx
, "just_a_dir/FILE1.txt", 0));
90 cl_assert_equal_s("just_a_dir/file1.txt", entry
->path
);
93 void test_index_bypath__add_honors_existing_case(void)
95 const git_index_entry
*entry
;
97 if (!cl_repo_get_bool(g_repo
, "core.ignorecase"))
100 cl_git_mkfile("submod2/just_a_dir/file1.txt", "This is a file");
101 cl_git_mkfile("submod2/just_a_dir/file2.txt", "This is another file");
102 cl_git_mkfile("submod2/just_a_dir/file3.txt", "This is another file");
103 cl_git_mkfile("submod2/just_a_dir/file4.txt", "And another file");
105 cl_git_pass(git_index_add_bypath(g_idx
, "just_a_dir/File1.txt"));
106 cl_git_pass(git_index_add_bypath(g_idx
, "JUST_A_DIR/file2.txt"));
107 cl_git_pass(git_index_add_bypath(g_idx
, "Just_A_Dir/FILE3.txt"));
109 cl_assert(entry
= git_index_get_bypath(g_idx
, "just_a_dir/File1.txt", 0));
110 cl_assert_equal_s("just_a_dir/File1.txt", entry
->path
);
112 cl_assert(entry
= git_index_get_bypath(g_idx
, "JUST_A_DIR/file2.txt", 0));
113 cl_assert_equal_s("just_a_dir/file2.txt", entry
->path
);
115 cl_assert(entry
= git_index_get_bypath(g_idx
, "Just_A_Dir/FILE3.txt", 0));
116 cl_assert_equal_s("just_a_dir/FILE3.txt", entry
->path
);
118 cl_git_rewritefile("submod2/just_a_dir/file3.txt", "Rewritten");
119 cl_git_pass(git_index_add_bypath(g_idx
, "Just_A_Dir/file3.txt"));
121 cl_assert(entry
= git_index_get_bypath(g_idx
, "Just_A_Dir/file3.txt", 0));
122 cl_assert_equal_s("just_a_dir/FILE3.txt", entry
->path
);
125 void test_index_bypath__add_honors_existing_case_2(void)
127 git_index_entry dummy
= { { 0 } };
128 const git_index_entry
*entry
;
130 if (!cl_repo_get_bool(g_repo
, "core.ignorecase"))
133 dummy
.mode
= GIT_FILEMODE_BLOB
;
134 cl_git_pass(git_oid_fromstr(&dummy
.id
, "f990a25a74d1a8281ce2ab018ea8df66795cd60b"));
136 /* note that `git_index_add` does no checking to canonical directories */
137 dummy
.path
= "Just_a_dir/file0.txt";
138 cl_git_pass(git_index_add(g_idx
, &dummy
));
140 dummy
.path
= "just_a_dir/fileA.txt";
141 cl_git_pass(git_index_add(g_idx
, &dummy
));
143 dummy
.path
= "Just_A_Dir/fileB.txt";
144 cl_git_pass(git_index_add(g_idx
, &dummy
));
146 dummy
.path
= "JUST_A_DIR/fileC.txt";
147 cl_git_pass(git_index_add(g_idx
, &dummy
));
149 dummy
.path
= "just_A_dir/fileD.txt";
150 cl_git_pass(git_index_add(g_idx
, &dummy
));
152 dummy
.path
= "JUST_a_DIR/fileE.txt";
153 cl_git_pass(git_index_add(g_idx
, &dummy
));
155 cl_git_mkfile("submod2/just_a_dir/file1.txt", "This is a file");
156 cl_git_mkfile("submod2/just_a_dir/file2.txt", "This is another file");
157 cl_git_mkfile("submod2/just_a_dir/file3.txt", "This is another file");
158 cl_git_mkfile("submod2/just_a_dir/file4.txt", "And another file");
160 cl_git_pass(git_index_add_bypath(g_idx
, "just_a_dir/File1.txt"));
161 cl_git_pass(git_index_add_bypath(g_idx
, "JUST_A_DIR/file2.txt"));
162 cl_git_pass(git_index_add_bypath(g_idx
, "Just_A_Dir/FILE3.txt"));
163 cl_git_pass(git_index_add_bypath(g_idx
, "JusT_A_DIR/FILE4.txt"));
165 cl_assert(entry
= git_index_get_bypath(g_idx
, "just_a_dir/File1.txt", 0));
166 cl_assert_equal_s("just_a_dir/File1.txt", entry
->path
);
168 cl_assert(entry
= git_index_get_bypath(g_idx
, "JUST_A_DIR/file2.txt", 0));
169 cl_assert_equal_s("JUST_A_DIR/file2.txt", entry
->path
);
171 cl_assert(entry
= git_index_get_bypath(g_idx
, "Just_A_Dir/FILE3.txt", 0));
172 cl_assert_equal_s("Just_A_Dir/FILE3.txt", entry
->path
);
174 cl_git_rewritefile("submod2/just_a_dir/file3.txt", "Rewritten");
175 cl_git_pass(git_index_add_bypath(g_idx
, "Just_A_Dir/file3.txt"));
177 cl_assert(entry
= git_index_get_bypath(g_idx
, "Just_A_Dir/file3.txt", 0));
178 cl_assert_equal_s("Just_A_Dir/FILE3.txt", entry
->path
);
181 void test_index_bypath__add_honors_existing_case_3(void)
183 git_index_entry dummy
= { { 0 } };
184 const git_index_entry
*entry
;
186 if (!cl_repo_get_bool(g_repo
, "core.ignorecase"))
189 dummy
.mode
= GIT_FILEMODE_BLOB
;
190 cl_git_pass(git_oid_fromstr(&dummy
.id
, "f990a25a74d1a8281ce2ab018ea8df66795cd60b"));
192 dummy
.path
= "just_a_dir/filea.txt";
193 cl_git_pass(git_index_add(g_idx
, &dummy
));
195 dummy
.path
= "Just_A_Dir/fileB.txt";
196 cl_git_pass(git_index_add(g_idx
, &dummy
));
198 dummy
.path
= "just_A_DIR/FILEC.txt";
199 cl_git_pass(git_index_add(g_idx
, &dummy
));
201 dummy
.path
= "Just_a_DIR/FileD.txt";
202 cl_git_pass(git_index_add(g_idx
, &dummy
));
204 cl_git_mkfile("submod2/JuSt_A_DiR/fILEE.txt", "This is a file");
206 cl_git_pass(git_index_add_bypath(g_idx
, "just_a_dir/fILEE.txt"));
208 cl_assert(entry
= git_index_get_bypath(g_idx
, "JUST_A_DIR/fILEE.txt", 0));
209 cl_assert_equal_s("just_a_dir/fILEE.txt", entry
->path
);
212 void test_index_bypath__add_honors_existing_case_4(void)
214 git_index_entry dummy
= { { 0 } };
215 const git_index_entry
*entry
;
217 if (!cl_repo_get_bool(g_repo
, "core.ignorecase"))
220 dummy
.mode
= GIT_FILEMODE_BLOB
;
221 cl_git_pass(git_oid_fromstr(&dummy
.id
, "f990a25a74d1a8281ce2ab018ea8df66795cd60b"));
223 dummy
.path
= "just_a_dir/a/b/c/d/e/file1.txt";
224 cl_git_pass(git_index_add(g_idx
, &dummy
));
226 dummy
.path
= "just_a_dir/a/B/C/D/E/file2.txt";
227 cl_git_pass(git_index_add(g_idx
, &dummy
));
229 cl_must_pass(p_mkdir("submod2/just_a_dir/a", 0777));
230 cl_must_pass(p_mkdir("submod2/just_a_dir/a/b", 0777));
231 cl_must_pass(p_mkdir("submod2/just_a_dir/a/b/z", 0777));
232 cl_must_pass(p_mkdir("submod2/just_a_dir/a/b/z/y", 0777));
233 cl_must_pass(p_mkdir("submod2/just_a_dir/a/b/z/y/x", 0777));
235 cl_git_mkfile("submod2/just_a_dir/a/b/z/y/x/FOO.txt", "This is a file");
237 cl_git_pass(git_index_add_bypath(g_idx
, "just_a_dir/A/b/Z/y/X/foo.txt"));
239 cl_assert(entry
= git_index_get_bypath(g_idx
, "just_a_dir/A/b/Z/y/X/foo.txt", 0));
240 cl_assert_equal_s("just_a_dir/a/b/Z/y/X/foo.txt", entry
->path
);
243 void test_index_bypath__add_honors_mode(void)
245 const git_index_entry
*entry
;
246 git_index_entry new_entry
;
248 cl_assert((entry
= git_index_get_bypath(g_idx
, "README.txt", 0)) != NULL
);
250 memcpy(&new_entry
, entry
, sizeof(git_index_entry
));
251 new_entry
.path
= "README.txt";
252 new_entry
.mode
= GIT_FILEMODE_BLOB_EXECUTABLE
;
254 cl_must_pass(p_chmod("submod2/README.txt", GIT_FILEMODE_BLOB_EXECUTABLE
));
256 cl_git_pass(git_index_add(g_idx
, &new_entry
));
257 cl_git_pass(git_index_write(g_idx
));
259 cl_git_rewritefile("submod2/README.txt", "Modified but still executable");
261 cl_git_pass(git_index_add_bypath(g_idx
, "README.txt"));
262 cl_git_pass(git_index_write(g_idx
));
264 cl_assert((entry
= git_index_get_bypath(g_idx
, "README.txt", 0)) != NULL
);
265 cl_assert_equal_i(GIT_FILEMODE_BLOB_EXECUTABLE
, entry
->mode
);
268 void test_index_bypath__add_honors_conflict_mode(void)
270 const git_index_entry
*entry
;
271 git_index_entry new_entry
;
274 cl_assert((entry
= git_index_get_bypath(g_idx
, "README.txt", 0)) != NULL
);
276 memcpy(&new_entry
, entry
, sizeof(git_index_entry
));
277 new_entry
.path
= "README.txt";
278 new_entry
.mode
= GIT_FILEMODE_BLOB_EXECUTABLE
;
280 cl_must_pass(p_chmod("submod2/README.txt", GIT_FILEMODE_BLOB_EXECUTABLE
));
282 cl_git_pass(git_index_remove_bypath(g_idx
, "README.txt"));
284 for (stage
= 1; stage
<= 3; stage
++) {
285 new_entry
.flags
= stage
<< GIT_INDEX_ENTRY_STAGESHIFT
;
286 cl_git_pass(git_index_add(g_idx
, &new_entry
));
289 cl_git_pass(git_index_write(g_idx
));
291 cl_git_rewritefile("submod2/README.txt", "Modified but still executable");
293 cl_git_pass(git_index_add_bypath(g_idx
, "README.txt"));
294 cl_git_pass(git_index_write(g_idx
));
296 cl_assert((entry
= git_index_get_bypath(g_idx
, "README.txt", 0)) != NULL
);
297 cl_assert_equal_i(GIT_FILEMODE_BLOB_EXECUTABLE
, entry
->mode
);
300 void test_index_bypath__add_honors_conflict_case(void)
302 const git_index_entry
*entry
;
303 git_index_entry new_entry
;
306 cl_assert((entry
= git_index_get_bypath(g_idx
, "README.txt", 0)) != NULL
);
308 memcpy(&new_entry
, entry
, sizeof(git_index_entry
));
309 new_entry
.path
= "README.txt";
310 new_entry
.mode
= GIT_FILEMODE_BLOB_EXECUTABLE
;
312 cl_must_pass(p_chmod("submod2/README.txt", GIT_FILEMODE_BLOB_EXECUTABLE
));
314 cl_git_pass(git_index_remove_bypath(g_idx
, "README.txt"));
316 for (stage
= 1; stage
<= 3; stage
++) {
317 new_entry
.flags
= stage
<< GIT_INDEX_ENTRY_STAGESHIFT
;
318 cl_git_pass(git_index_add(g_idx
, &new_entry
));
321 cl_git_pass(git_index_write(g_idx
));
323 cl_git_rewritefile("submod2/README.txt", "Modified but still executable");
325 cl_git_pass(git_index_add_bypath(g_idx
, "README.txt"));
326 cl_git_pass(git_index_write(g_idx
));
328 cl_assert((entry
= git_index_get_bypath(g_idx
, "README.txt", 0)) != NULL
);
329 cl_assert_equal_i(GIT_FILEMODE_BLOB_EXECUTABLE
, entry
->mode
);
332 void test_index_bypath__add_honors_symlink(void)
334 const git_index_entry
*entry
;
335 git_index_entry new_entry
;
338 cl_git_pass(git_repository__configmap_lookup(&symlinks
, g_repo
, GIT_CONFIGMAP_SYMLINKS
));
343 cl_assert((entry
= git_index_get_bypath(g_idx
, "README.txt", 0)) != NULL
);
345 memcpy(&new_entry
, entry
, sizeof(git_index_entry
));
346 new_entry
.path
= "README.txt";
347 new_entry
.mode
= GIT_FILEMODE_LINK
;
349 cl_git_pass(git_index_add(g_idx
, &new_entry
));
350 cl_git_pass(git_index_write(g_idx
));
352 cl_git_rewritefile("submod2/README.txt", "Modified but still a (fake) symlink");
354 cl_git_pass(git_index_add_bypath(g_idx
, "README.txt"));
355 cl_git_pass(git_index_write(g_idx
));
357 cl_assert((entry
= git_index_get_bypath(g_idx
, "README.txt", 0)) != NULL
);
358 cl_assert_equal_i(GIT_FILEMODE_LINK
, entry
->mode
);