1 #include "clar_libgit2.h"
2 #include "checkout_helpers.h"
4 #include "git2/checkout.h"
5 #include "repository.h"
8 static const char *repo_name
= "nasty";
9 static git_repository
*repo
;
10 static git_checkout_options checkout_opts
;
12 void test_checkout_nasty__initialize(void)
14 repo
= cl_git_sandbox_init(repo_name
);
16 GIT_INIT_STRUCTURE(&checkout_opts
, GIT_CHECKOUT_OPTIONS_VERSION
);
17 checkout_opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
20 void test_checkout_nasty__cleanup(void)
22 cl_git_sandbox_cleanup();
25 static void test_checkout_passes(const char *refname
, const char *filename
)
29 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
30 git_str path
= GIT_STR_INIT
;
32 cl_git_pass(git_str_joinpath(&path
, repo_name
, filename
));
34 cl_git_pass(git_reference_name_to_id(&commit_id
, repo
, refname
));
35 cl_git_pass(git_commit_lookup(&commit
, repo
, &commit_id
));
37 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
|
38 GIT_CHECKOUT_DONT_UPDATE_INDEX
;
40 cl_git_pass(git_checkout_tree(repo
, (const git_object
*)commit
, &opts
));
41 cl_assert(!git_fs_path_exists(path
.ptr
));
43 git_commit_free(commit
);
44 git_str_dispose(&path
);
47 static void test_checkout_fails(const char *refname
, const char *filename
)
51 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
52 git_str path
= GIT_STR_INIT
;
54 cl_git_pass(git_str_joinpath(&path
, repo_name
, filename
));
56 cl_git_pass(git_reference_name_to_id(&commit_id
, repo
, refname
));
57 cl_git_pass(git_commit_lookup(&commit
, repo
, &commit_id
));
59 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
61 cl_git_fail(git_checkout_tree(repo
, (const git_object
*)commit
, &opts
));
62 cl_assert(!git_fs_path_exists(path
.ptr
));
64 git_commit_free(commit
);
65 git_str_dispose(&path
);
68 /* A tree that contains ".git" as a tree, with a blob inside
71 void test_checkout_nasty__dotgit_tree(void)
73 test_checkout_fails("refs/heads/dotgit_tree", ".git/foobar");
76 /* A tree that contains ".GIT" as a tree, with a blob inside
79 void test_checkout_nasty__dotcapitalgit_tree(void)
81 test_checkout_fails("refs/heads/dotcapitalgit_tree", ".GIT/foobar");
84 /* A tree that contains a tree ".", with a blob inside ("./foobar").
86 void test_checkout_nasty__dot_tree(void)
88 test_checkout_fails("refs/heads/dot_tree", "foobar");
91 /* A tree that contains a tree ".", with a tree ".git", with a blob
92 * inside ("./.git/foobar").
94 void test_checkout_nasty__dot_dotgit_tree(void)
96 test_checkout_fails("refs/heads/dot_dotgit_tree", ".git/foobar");
99 /* A tree that contains a tree, with a tree "..", with a tree ".git", with a
100 * blob inside ("foo/../.git/foobar").
102 void test_checkout_nasty__dotdot_dotgit_tree(void)
104 test_checkout_fails("refs/heads/dotdot_dotgit_tree", ".git/foobar");
107 /* A tree that contains a tree, with a tree "..", with a blob inside
110 void test_checkout_nasty__dotdot_tree(void)
112 test_checkout_fails("refs/heads/dotdot_tree", "foobar");
115 /* A tree that contains a blob with the rogue name ".git/foobar" */
116 void test_checkout_nasty__dotgit_path(void)
118 test_checkout_fails("refs/heads/dotgit_path", ".git/foobar");
121 /* A tree that contains a blob with the rogue name ".GIT/foobar" */
122 void test_checkout_nasty__dotcapitalgit_path(void)
124 test_checkout_fails("refs/heads/dotcapitalgit_path", ".GIT/foobar");
127 /* A tree that contains a blob with the rogue name "./.git/foobar" */
128 void test_checkout_nasty__dot_dotgit_path(void)
130 test_checkout_fails("refs/heads/dot_dotgit_path", ".git/foobar");
133 /* A tree that contains a blob with the rogue name "./.GIT/foobar" */
134 void test_checkout_nasty__dot_dotcapitalgit_path(void)
136 test_checkout_fails("refs/heads/dot_dotcapitalgit_path", ".GIT/foobar");
139 /* A tree that contains a blob with the rogue name "foo/../.git/foobar" */
140 void test_checkout_nasty__dotdot_dotgit_path(void)
142 test_checkout_fails("refs/heads/dotdot_dotgit_path", ".git/foobar");
145 /* A tree that contains a blob with the rogue name "foo/../.GIT/foobar" */
146 void test_checkout_nasty__dotdot_dotcapitalgit_path(void)
148 test_checkout_fails("refs/heads/dotdot_dotcapitalgit_path", ".GIT/foobar");
151 /* A tree that contains a blob with the rogue name "foo/." */
152 void test_checkout_nasty__dot_path(void)
154 test_checkout_fails("refs/heads/dot_path", "./foobar");
157 /* A tree that contains a blob with the rogue name "foo/." */
158 void test_checkout_nasty__dot_path_two(void)
160 test_checkout_fails("refs/heads/dot_path_two", "foo/.");
163 /* A tree that contains a blob with the rogue name "foo/../foobar" */
164 void test_checkout_nasty__dotdot_path(void)
166 test_checkout_fails("refs/heads/dotdot_path", "foobar");
169 /* A tree that contains an entry with a backslash ".git\foobar" */
170 void test_checkout_nasty__dotgit_backslash_path(void)
173 test_checkout_fails("refs/heads/dotgit_backslash_path", ".git/foobar");
177 /* A tree that contains an entry with a backslash ".GIT\foobar" */
178 void test_checkout_nasty__dotcapitalgit_backslash_path(void)
181 test_checkout_fails("refs/heads/dotcapitalgit_backslash_path", ".GIT/foobar");
185 /* A tree that contains an entry with a backslash ".\.GIT\foobar" */
186 void test_checkout_nasty__dot_backslash_dotcapitalgit_path(void)
189 test_checkout_fails("refs/heads/dot_backslash_dotcapitalgit_path", ".GIT/foobar");
193 /* A tree that contains an entry ".git.", because Win32 APIs will drop the
196 void test_checkout_nasty__dot_git_dot(void)
199 test_checkout_fails("refs/heads/dot_git_dot", ".git/foobar");
203 /* A tree that contains an entry "git~1", because that is typically the
204 * short name for ".git".
206 void test_checkout_nasty__git_tilde1(void)
208 test_checkout_fails("refs/heads/git_tilde1", ".git/foobar");
209 test_checkout_fails("refs/heads/git_tilde1", "git~1/foobar");
212 /* A tree that contains an entry "git~2", when we have forced the short
213 * name for ".git" into "GIT~2".
215 void test_checkout_nasty__git_custom_shortname(void)
218 if (!cl_sandbox_supports_8dot3())
221 cl_must_pass(p_rename("nasty/.git", "nasty/_temp"));
222 cl_git_write2file("nasty/git~1", "", 0, O_RDWR
|O_CREAT
, 0666);
223 cl_must_pass(p_rename("nasty/_temp", "nasty/.git"));
224 test_checkout_fails("refs/heads/git_tilde2", ".git/foobar");
228 /* A tree that contains an entry "git~3", which should be allowed, since
229 * it is not the typical short name ("GIT~1") or the actual short name
230 * ("GIT~2") for ".git".
232 void test_checkout_nasty__only_looks_like_a_git_shortname(void)
237 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
239 cl_must_pass(p_rename("nasty/.git", "nasty/_temp"));
240 cl_git_write2file("nasty/git~1", "", 0, O_RDWR
|O_CREAT
, 0666);
241 cl_must_pass(p_rename("nasty/_temp", "nasty/.git"));
243 cl_git_pass(git_reference_name_to_id(&commit_id
, repo
, "refs/heads/git_tilde3"));
244 cl_git_pass(git_commit_lookup(&commit
, repo
, &commit_id
));
246 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
248 cl_git_pass(git_checkout_tree(repo
, (const git_object
*)commit
, &opts
));
249 cl_assert(git_fs_path_exists("nasty/git~3/foobar"));
251 git_commit_free(commit
);
255 /* A tree that contains an entry "git:", because Win32 APIs will reject
256 * that as looking too similar to a drive letter.
258 void test_checkout_nasty__dot_git_colon(void)
261 test_checkout_fails("refs/heads/dot_git_colon", ".git/foobar");
265 /* A tree that contains an entry "git:foo", because Win32 APIs will turn
268 void test_checkout_nasty__dot_git_colon_stuff(void)
271 test_checkout_fails("refs/heads/dot_git_colon_stuff", ".git/foobar");
275 /* A tree that contains an entry ".git::$INDEX_ALLOCATION" because NTFS
276 * will interpret that as a synonym to ".git", even when mounted via SMB
279 void test_checkout_nasty__dotgit_alternate_data_stream(void)
281 test_checkout_fails("refs/heads/dotgit_alternate_data_stream", ".git/dummy-file");
282 test_checkout_fails("refs/heads/dotgit_alternate_data_stream", ".git::$INDEX_ALLOCATION/dummy-file");
285 /* Trees that contains entries with a tree ".git" that contain
287 * { 0xe2, 0x80, 0x8c }
288 * { 0xe2, 0x80, 0x8d }
289 * { 0xe2, 0x80, 0x8e }
290 * { 0xe2, 0x80, 0x8f }
291 * { 0xe2, 0x80, 0xaa }
292 * { 0xe2, 0x80, 0xab }
293 * { 0xe2, 0x80, 0xac }
294 * { 0xe2, 0x80, 0xad }
295 * { 0xe2, 0x81, 0xae }
296 * { 0xe2, 0x81, 0xaa }
297 * { 0xe2, 0x81, 0xab }
298 * { 0xe2, 0x81, 0xac }
299 * { 0xe2, 0x81, 0xad }
300 * { 0xe2, 0x81, 0xae }
301 * { 0xe2, 0x81, 0xaf }
302 * { 0xef, 0xbb, 0xbf }
303 * Because these map to characters that HFS filesystems "ignore". Thus
304 * ".git<U+200C>" will map to ".git".
306 void test_checkout_nasty__dot_git_hfs_ignorable(void)
309 test_checkout_fails("refs/heads/dotgit_hfs_ignorable_1", ".git/foobar");
310 test_checkout_fails("refs/heads/dotgit_hfs_ignorable_2", ".git/foobar");
311 test_checkout_fails("refs/heads/dotgit_hfs_ignorable_3", ".git/foobar");
312 test_checkout_fails("refs/heads/dotgit_hfs_ignorable_4", ".git/foobar");
313 test_checkout_fails("refs/heads/dotgit_hfs_ignorable_5", ".git/foobar");
314 test_checkout_fails("refs/heads/dotgit_hfs_ignorable_6", ".git/foobar");
315 test_checkout_fails("refs/heads/dotgit_hfs_ignorable_7", ".git/foobar");
316 test_checkout_fails("refs/heads/dotgit_hfs_ignorable_8", ".git/foobar");
317 test_checkout_fails("refs/heads/dotgit_hfs_ignorable_9", ".git/foobar");
318 test_checkout_fails("refs/heads/dotgit_hfs_ignorable_10", ".git/foobar");
319 test_checkout_fails("refs/heads/dotgit_hfs_ignorable_11", ".git/foobar");
320 test_checkout_fails("refs/heads/dotgit_hfs_ignorable_12", ".git/foobar");
321 test_checkout_fails("refs/heads/dotgit_hfs_ignorable_13", ".git/foobar");
322 test_checkout_fails("refs/heads/dotgit_hfs_ignorable_14", ".git/foobar");
323 test_checkout_fails("refs/heads/dotgit_hfs_ignorable_15", ".git/foobar");
324 test_checkout_fails("refs/heads/dotgit_hfs_ignorable_16", ".git/foobar");
328 void test_checkout_nasty__honors_core_protecthfs(void)
330 cl_repo_set_bool(repo
, "core.protectHFS", true);
332 test_checkout_fails("refs/heads/dotgit_hfs_ignorable_1", ".git/foobar");
333 test_checkout_fails("refs/heads/dotgit_hfs_ignorable_2", ".git/foobar");
334 test_checkout_fails("refs/heads/dotgit_hfs_ignorable_3", ".git/foobar");
335 test_checkout_fails("refs/heads/dotgit_hfs_ignorable_4", ".git/foobar");
336 test_checkout_fails("refs/heads/dotgit_hfs_ignorable_5", ".git/foobar");
337 test_checkout_fails("refs/heads/dotgit_hfs_ignorable_6", ".git/foobar");
338 test_checkout_fails("refs/heads/dotgit_hfs_ignorable_7", ".git/foobar");
339 test_checkout_fails("refs/heads/dotgit_hfs_ignorable_8", ".git/foobar");
340 test_checkout_fails("refs/heads/dotgit_hfs_ignorable_9", ".git/foobar");
341 test_checkout_fails("refs/heads/dotgit_hfs_ignorable_10", ".git/foobar");
342 test_checkout_fails("refs/heads/dotgit_hfs_ignorable_11", ".git/foobar");
343 test_checkout_fails("refs/heads/dotgit_hfs_ignorable_12", ".git/foobar");
344 test_checkout_fails("refs/heads/dotgit_hfs_ignorable_13", ".git/foobar");
345 test_checkout_fails("refs/heads/dotgit_hfs_ignorable_14", ".git/foobar");
346 test_checkout_fails("refs/heads/dotgit_hfs_ignorable_15", ".git/foobar");
347 test_checkout_fails("refs/heads/dotgit_hfs_ignorable_16", ".git/foobar");
350 void test_checkout_nasty__honors_core_protectntfs(void)
352 cl_repo_set_bool(repo
, "core.protectNTFS", true);
354 test_checkout_fails("refs/heads/dotgit_backslash_path", ".git/foobar");
355 test_checkout_fails("refs/heads/dotcapitalgit_backslash_path", ".GIT/foobar");
356 test_checkout_fails("refs/heads/dot_git_dot", ".git/foobar");
357 test_checkout_fails("refs/heads/git_tilde1", ".git/foobar");
360 void test_checkout_nasty__symlink1(void)
362 test_checkout_passes("refs/heads/symlink1", ".git/foobar");
365 void test_checkout_nasty__symlink2(void)
367 test_checkout_passes("refs/heads/symlink2", ".git/foobar");
370 void test_checkout_nasty__symlink3(void)
372 test_checkout_passes("refs/heads/symlink3", ".git/foobar");
375 void test_checkout_nasty__gitmodules_symlink(void)
377 cl_repo_set_bool(repo
, "core.protectHFS", true);
378 test_checkout_fails("refs/heads/gitmodules-symlink", ".gitmodules");
379 cl_repo_set_bool(repo
, "core.protectHFS", false);
381 cl_repo_set_bool(repo
, "core.protectNTFS", true);
382 test_checkout_fails("refs/heads/gitmodules-symlink", ".gitmodules");
383 cl_repo_set_bool(repo
, "core.protectNTFS", false);
385 test_checkout_fails("refs/heads/gitmodules-symlink", ".gitmodules");