]> git.proxmox.com Git - libgit2.git/blob - tests/libgit2/index/bypath.c
New upstream version 1.5.0+ds
[libgit2.git] / tests / libgit2 / index / bypath.c
1 #include "clar_libgit2.h"
2 #include "repository.h"
3 #include "../submodule/submodule_helpers.h"
4
5 static git_repository *g_repo;
6 static git_index *g_idx;
7
8 void test_index_bypath__initialize(void)
9 {
10 g_repo = setup_fixture_submod2();
11 cl_git_pass(git_repository_index__weakptr(&g_idx, g_repo));
12 }
13
14 void test_index_bypath__cleanup(void)
15 {
16 g_repo = NULL;
17 g_idx = NULL;
18 }
19
20 void test_index_bypath__add_directory(void)
21 {
22 cl_git_fail_with(GIT_EDIRECTORY, git_index_add_bypath(g_idx, "just_a_dir"));
23 }
24
25 void test_index_bypath__add_submodule(void)
26 {
27 unsigned int status;
28 const char *sm_name = "sm_changed_head";
29
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);
35 }
36
37 void test_index_bypath__add_submodule_unregistered(void)
38 {
39 const char *sm_name = "not-submodule";
40 const char *sm_head = "68e92c611b80ee1ed8f38314ff9577f0d15b2444";
41 const git_index_entry *entry;
42
43 cl_git_pass(git_index_add_bypath(g_idx, sm_name));
44
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);
48 }
49
50 void test_index_bypath__add_hidden(void)
51 {
52 #ifdef GIT_WIN32
53 const git_index_entry *entry;
54 bool hidden;
55
56 cl_git_mkfile("submod2/hidden_file", "you can't see me");
57
58 cl_git_pass(git_win32__hidden(&hidden, "submod2/hidden_file"));
59 cl_assert(!hidden);
60
61 cl_git_pass(git_win32__set_hidden("submod2/hidden_file", true));
62
63 cl_git_pass(git_win32__hidden(&hidden, "submod2/hidden_file"));
64 cl_assert(hidden);
65
66 cl_git_pass(git_index_add_bypath(g_idx, "hidden_file"));
67
68 cl_assert(entry = git_index_get_bypath(g_idx, "hidden_file", 0));
69 cl_assert_equal_i(GIT_FILEMODE_BLOB, entry->mode);
70 #endif
71 }
72
73 void test_index_bypath__add_keeps_existing_case(void)
74 {
75 const git_index_entry *entry;
76
77 if (!cl_repo_get_bool(g_repo, "core.ignorecase"))
78 clar__skip();
79
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"));
82
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);
85
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"));
88
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);
91 }
92
93 void test_index_bypath__add_honors_existing_case(void)
94 {
95 const git_index_entry *entry;
96
97 if (!cl_repo_get_bool(g_repo, "core.ignorecase"))
98 clar__skip();
99
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");
104
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"));
108
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);
111
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);
114
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);
117
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"));
120
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);
123 }
124
125 void test_index_bypath__add_honors_existing_case_2(void)
126 {
127 git_index_entry dummy = { { 0 } };
128 const git_index_entry *entry;
129
130 if (!cl_repo_get_bool(g_repo, "core.ignorecase"))
131 clar__skip();
132
133 dummy.mode = GIT_FILEMODE_BLOB;
134 cl_git_pass(git_oid_fromstr(&dummy.id, "f990a25a74d1a8281ce2ab018ea8df66795cd60b"));
135
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));
139
140 dummy.path = "just_a_dir/fileA.txt";
141 cl_git_pass(git_index_add(g_idx, &dummy));
142
143 dummy.path = "Just_A_Dir/fileB.txt";
144 cl_git_pass(git_index_add(g_idx, &dummy));
145
146 dummy.path = "JUST_A_DIR/fileC.txt";
147 cl_git_pass(git_index_add(g_idx, &dummy));
148
149 dummy.path = "just_A_dir/fileD.txt";
150 cl_git_pass(git_index_add(g_idx, &dummy));
151
152 dummy.path = "JUST_a_DIR/fileE.txt";
153 cl_git_pass(git_index_add(g_idx, &dummy));
154
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");
159
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"));
164
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);
167
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);
170
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);
173
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"));
176
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);
179 }
180
181 void test_index_bypath__add_honors_existing_case_3(void)
182 {
183 git_index_entry dummy = { { 0 } };
184 const git_index_entry *entry;
185
186 if (!cl_repo_get_bool(g_repo, "core.ignorecase"))
187 clar__skip();
188
189 dummy.mode = GIT_FILEMODE_BLOB;
190 cl_git_pass(git_oid_fromstr(&dummy.id, "f990a25a74d1a8281ce2ab018ea8df66795cd60b"));
191
192 dummy.path = "just_a_dir/filea.txt";
193 cl_git_pass(git_index_add(g_idx, &dummy));
194
195 dummy.path = "Just_A_Dir/fileB.txt";
196 cl_git_pass(git_index_add(g_idx, &dummy));
197
198 dummy.path = "just_A_DIR/FILEC.txt";
199 cl_git_pass(git_index_add(g_idx, &dummy));
200
201 dummy.path = "Just_a_DIR/FileD.txt";
202 cl_git_pass(git_index_add(g_idx, &dummy));
203
204 cl_git_mkfile("submod2/JuSt_A_DiR/fILEE.txt", "This is a file");
205
206 cl_git_pass(git_index_add_bypath(g_idx, "just_a_dir/fILEE.txt"));
207
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);
210 }
211
212 void test_index_bypath__add_honors_existing_case_4(void)
213 {
214 git_index_entry dummy = { { 0 } };
215 const git_index_entry *entry;
216
217 if (!cl_repo_get_bool(g_repo, "core.ignorecase"))
218 clar__skip();
219
220 dummy.mode = GIT_FILEMODE_BLOB;
221 cl_git_pass(git_oid_fromstr(&dummy.id, "f990a25a74d1a8281ce2ab018ea8df66795cd60b"));
222
223 dummy.path = "just_a_dir/a/b/c/d/e/file1.txt";
224 cl_git_pass(git_index_add(g_idx, &dummy));
225
226 dummy.path = "just_a_dir/a/B/C/D/E/file2.txt";
227 cl_git_pass(git_index_add(g_idx, &dummy));
228
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));
234
235 cl_git_mkfile("submod2/just_a_dir/a/b/z/y/x/FOO.txt", "This is a file");
236
237 cl_git_pass(git_index_add_bypath(g_idx, "just_a_dir/A/b/Z/y/X/foo.txt"));
238
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);
241 }
242
243 void test_index_bypath__add_honors_mode(void)
244 {
245 const git_index_entry *entry;
246 git_index_entry new_entry;
247
248 cl_assert((entry = git_index_get_bypath(g_idx, "README.txt", 0)) != NULL);
249
250 memcpy(&new_entry, entry, sizeof(git_index_entry));
251 new_entry.path = "README.txt";
252 new_entry.mode = GIT_FILEMODE_BLOB_EXECUTABLE;
253
254 cl_must_pass(p_chmod("submod2/README.txt", GIT_FILEMODE_BLOB_EXECUTABLE));
255
256 cl_git_pass(git_index_add(g_idx, &new_entry));
257 cl_git_pass(git_index_write(g_idx));
258
259 cl_git_rewritefile("submod2/README.txt", "Modified but still executable");
260
261 cl_git_pass(git_index_add_bypath(g_idx, "README.txt"));
262 cl_git_pass(git_index_write(g_idx));
263
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);
266 }
267
268 void test_index_bypath__add_honors_conflict_mode(void)
269 {
270 const git_index_entry *entry;
271 git_index_entry new_entry;
272 int stage = 0;
273
274 cl_assert((entry = git_index_get_bypath(g_idx, "README.txt", 0)) != NULL);
275
276 memcpy(&new_entry, entry, sizeof(git_index_entry));
277 new_entry.path = "README.txt";
278 new_entry.mode = GIT_FILEMODE_BLOB_EXECUTABLE;
279
280 cl_must_pass(p_chmod("submod2/README.txt", GIT_FILEMODE_BLOB_EXECUTABLE));
281
282 cl_git_pass(git_index_remove_bypath(g_idx, "README.txt"));
283
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));
287 }
288
289 cl_git_pass(git_index_write(g_idx));
290
291 cl_git_rewritefile("submod2/README.txt", "Modified but still executable");
292
293 cl_git_pass(git_index_add_bypath(g_idx, "README.txt"));
294 cl_git_pass(git_index_write(g_idx));
295
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);
298 }
299
300 void test_index_bypath__add_honors_conflict_case(void)
301 {
302 const git_index_entry *entry;
303 git_index_entry new_entry;
304 int stage = 0;
305
306 cl_assert((entry = git_index_get_bypath(g_idx, "README.txt", 0)) != NULL);
307
308 memcpy(&new_entry, entry, sizeof(git_index_entry));
309 new_entry.path = "README.txt";
310 new_entry.mode = GIT_FILEMODE_BLOB_EXECUTABLE;
311
312 cl_must_pass(p_chmod("submod2/README.txt", GIT_FILEMODE_BLOB_EXECUTABLE));
313
314 cl_git_pass(git_index_remove_bypath(g_idx, "README.txt"));
315
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));
319 }
320
321 cl_git_pass(git_index_write(g_idx));
322
323 cl_git_rewritefile("submod2/README.txt", "Modified but still executable");
324
325 cl_git_pass(git_index_add_bypath(g_idx, "README.txt"));
326 cl_git_pass(git_index_write(g_idx));
327
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);
330 }
331
332 void test_index_bypath__add_honors_symlink(void)
333 {
334 const git_index_entry *entry;
335 git_index_entry new_entry;
336 int symlinks;
337
338 cl_git_pass(git_repository__configmap_lookup(&symlinks, g_repo, GIT_CONFIGMAP_SYMLINKS));
339
340 if (symlinks)
341 cl_skip();
342
343 cl_assert((entry = git_index_get_bypath(g_idx, "README.txt", 0)) != NULL);
344
345 memcpy(&new_entry, entry, sizeof(git_index_entry));
346 new_entry.path = "README.txt";
347 new_entry.mode = GIT_FILEMODE_LINK;
348
349 cl_git_pass(git_index_add(g_idx, &new_entry));
350 cl_git_pass(git_index_write(g_idx));
351
352 cl_git_rewritefile("submod2/README.txt", "Modified but still a (fake) symlink");
353
354 cl_git_pass(git_index_add_bypath(g_idx, "README.txt"));
355 cl_git_pass(git_index_write(g_idx));
356
357 cl_assert((entry = git_index_get_bypath(g_idx, "README.txt", 0)) != NULL);
358 cl_assert_equal_i(GIT_FILEMODE_LINK, entry->mode);
359 }