1 #include "clar_libgit2.h"
6 static int validate_ownership
= 0;
7 static git_buf config_path
= GIT_BUF_INIT
;
9 void test_repo_open__initialize(void)
11 cl_git_pass(git_libgit2_opts(GIT_OPT_GET_SEARCH_PATH
, GIT_CONFIG_LEVEL_GLOBAL
, &config_path
));
12 cl_git_pass(git_libgit2_opts(GIT_OPT_GET_OWNER_VALIDATION
, &validate_ownership
));
15 void test_repo_open__cleanup(void)
17 cl_git_sandbox_cleanup();
18 cl_fixture_cleanup("empty_standard_repo");
19 cl_fixture_cleanup("__global_config");
21 if (git_fs_path_isdir("alternate"))
22 git_futils_rmdir_r("alternate", NULL
, GIT_RMDIR_REMOVE_FILES
);
24 git_fs_path__set_owner(GIT_FS_PATH_MOCK_OWNER_NONE
);
26 cl_git_pass(git_libgit2_opts(GIT_OPT_SET_SEARCH_PATH
, GIT_CONFIG_LEVEL_GLOBAL
, config_path
.ptr
));
27 git_buf_dispose(&config_path
);
29 cl_git_pass(git_libgit2_opts(GIT_OPT_SET_OWNER_VALIDATION
, validate_ownership
));
32 void test_repo_open__bare_empty_repo(void)
34 git_repository
*repo
= cl_git_sandbox_init("empty_bare.git");
36 cl_assert(git_repository_path(repo
) != NULL
);
37 cl_assert(git__suffixcmp(git_repository_path(repo
), "/") == 0);
38 cl_assert(git_repository_workdir(repo
) == NULL
);
41 void test_repo_open__format_version_1(void)
46 repo
= cl_git_sandbox_init("empty_bare.git");
48 cl_git_pass(git_repository_open(&repo
, "empty_bare.git"));
49 cl_git_pass(git_repository_config(&config
, repo
));
51 cl_git_pass(git_config_set_int32(config
, "core.repositoryformatversion", 1));
53 git_config_free(config
);
54 git_repository_free(repo
);
56 cl_git_pass(git_repository_open(&repo
, "empty_bare.git"));
57 cl_assert(git_repository_path(repo
) != NULL
);
58 cl_assert(git__suffixcmp(git_repository_path(repo
), "/") == 0);
59 git_repository_free(repo
);
62 void test_repo_open__standard_empty_repo_through_gitdir(void)
66 cl_git_pass(git_repository_open(&repo
, cl_fixture("empty_standard_repo/.gitted")));
68 cl_assert(git_repository_path(repo
) != NULL
);
69 cl_assert(git__suffixcmp(git_repository_path(repo
), "/") == 0);
71 cl_assert(git_repository_workdir(repo
) != NULL
);
72 cl_assert(git__suffixcmp(git_repository_workdir(repo
), "/") == 0);
74 git_repository_free(repo
);
77 void test_repo_open__standard_empty_repo_through_workdir(void)
79 git_repository
*repo
= cl_git_sandbox_init("empty_standard_repo");
81 cl_assert(git_repository_path(repo
) != NULL
);
82 cl_assert(git__suffixcmp(git_repository_path(repo
), "/") == 0);
84 cl_assert(git_repository_workdir(repo
) != NULL
);
85 cl_assert(git__suffixcmp(git_repository_workdir(repo
), "/") == 0);
89 void test_repo_open__open_with_discover(void)
91 static const char *variants
[] = {
92 "attr", "attr/", "attr/.git", "attr/.git/",
93 "attr/sub", "attr/sub/", "attr/sub/sub", "attr/sub/sub/",
99 cl_fixture_sandbox("attr");
100 cl_git_pass(p_rename("attr/.gitted", "attr/.git"));
102 for (scan
= variants
; *scan
!= NULL
; scan
++) {
103 cl_git_pass(git_repository_open_ext(&repo
, *scan
, 0, NULL
));
104 cl_assert(git__suffixcmp(git_repository_path(repo
), "attr/.git/") == 0);
105 cl_assert(git__suffixcmp(git_repository_workdir(repo
), "attr/") == 0);
106 git_repository_free(repo
);
109 cl_fixture_cleanup("attr");
112 void test_repo_open__check_if_repository(void)
114 cl_git_sandbox_init("empty_standard_repo");
116 /* Pass NULL for the output parameter to check for but not open the repo */
117 cl_git_pass(git_repository_open_ext(NULL
, "empty_standard_repo", 0, NULL
));
118 cl_git_fail(git_repository_open_ext(NULL
, "repo_does_not_exist", 0, NULL
));
120 cl_fixture_cleanup("empty_standard_repo");
123 static void make_gitlink_dir(const char *dir
, const char *linktext
)
125 git_str path
= GIT_STR_INIT
;
127 cl_git_pass(git_futils_mkdir(dir
, 0777, GIT_MKDIR_VERIFY_DIR
));
128 cl_git_pass(git_str_joinpath(&path
, dir
, ".git"));
129 cl_git_rewritefile(path
.ptr
, linktext
);
130 git_str_dispose(&path
);
133 void test_repo_open__gitlinked(void)
135 /* need to have both repo dir and workdir set up correctly */
136 git_repository
*repo
= cl_git_sandbox_init("empty_standard_repo");
137 git_repository
*repo2
;
139 make_gitlink_dir("alternate", "gitdir: ../empty_standard_repo/.git");
141 cl_git_pass(git_repository_open(&repo2
, "alternate"));
143 cl_assert(git_repository_path(repo2
) != NULL
);
144 cl_assert_(git__suffixcmp(git_repository_path(repo2
), "empty_standard_repo/.git/") == 0, git_repository_path(repo2
));
145 cl_assert_equal_s(git_repository_path(repo
), git_repository_path(repo2
));
147 cl_assert(git_repository_workdir(repo2
) != NULL
);
148 cl_assert_(git__suffixcmp(git_repository_workdir(repo2
), "alternate/") == 0, git_repository_workdir(repo2
));
150 git_repository_free(repo2
);
153 void test_repo_open__with_symlinked_config(void)
156 git_str path
= GIT_STR_INIT
;
157 git_repository
*repo
;
161 cl_git_sandbox_init("empty_standard_repo");
163 /* Setup .gitconfig as symlink */
164 cl_git_pass(git_futils_mkdir_r("home", 0777));
165 cl_git_mkfile("home/.gitconfig.linked", "[global]\ntest = 4567\n");
166 cl_must_pass(symlink(".gitconfig.linked", "home/.gitconfig"));
167 cl_git_pass(git_fs_path_prettify(&path
, "home", NULL
));
168 cl_git_pass(git_libgit2_opts(GIT_OPT_SET_SEARCH_PATH
, GIT_CONFIG_LEVEL_GLOBAL
, path
.ptr
));
170 cl_git_pass(git_repository_open(&repo
, "empty_standard_repo"));
171 cl_git_pass(git_config_open_default(&cfg
));
172 cl_git_pass(git_config_get_int32(&value
, cfg
, "global.test"));
173 cl_assert_equal_i(4567, value
);
175 git_config_free(cfg
);
176 git_repository_free(repo
);
177 cl_git_pass(git_futils_rmdir_r(git_str_cstr(&path
), NULL
, GIT_RMDIR_REMOVE_FILES
));
178 cl_sandbox_set_search_path_defaults();
179 git_str_dispose(&path
);
183 void test_repo_open__from_git_new_workdir(void)
186 /* The git-new-workdir script that ships with git sets up a bunch of
187 * symlinks to create a second workdir that shares the object db with
188 * another checkout. Libgit2 can open a repo that has been configured
192 git_repository
*repo2
;
193 git_str link_tgt
= GIT_STR_INIT
, link
= GIT_STR_INIT
, body
= GIT_STR_INIT
;
196 static const char *links
[] = {
197 "config", "refs", "logs/refs", "objects", "info", "hooks",
198 "packed-refs", "remotes", "rr-cache", "svn", NULL
200 static const char *copies
[] = {
204 cl_git_sandbox_init("empty_standard_repo");
206 cl_git_pass(p_mkdir("alternate", 0777));
207 cl_git_pass(p_mkdir("alternate/.git", 0777));
209 for (scan
= links
; *scan
!= NULL
; scan
++) {
210 git_str_joinpath(&link_tgt
, "empty_standard_repo/.git", *scan
);
211 if (git_fs_path_exists(link_tgt
.ptr
)) {
212 git_str_joinpath(&link_tgt
, "../../empty_standard_repo/.git", *scan
);
213 git_str_joinpath(&link
, "alternate/.git", *scan
);
214 if (strchr(*scan
, '/'))
215 git_futils_mkpath2file(link
.ptr
, 0777);
216 cl_assert_(symlink(link_tgt
.ptr
, link
.ptr
) == 0, strerror(errno
));
219 for (scan
= copies
; *scan
!= NULL
; scan
++) {
220 git_str_joinpath(&link_tgt
, "empty_standard_repo/.git", *scan
);
221 if (git_fs_path_exists(link_tgt
.ptr
)) {
222 git_str_joinpath(&link
, "alternate/.git", *scan
);
223 cl_git_pass(git_futils_readbuffer(&body
, link_tgt
.ptr
));
225 cl_assert((link_fd
= git_futils_creat_withpath(link
.ptr
, 0777, 0666)) >= 0);
226 cl_must_pass(p_write(link_fd
, body
.ptr
, body
.size
));
231 git_str_dispose(&link_tgt
);
232 git_str_dispose(&link
);
233 git_str_dispose(&body
);
236 cl_git_pass(git_repository_open(&repo2
, "alternate"));
238 cl_assert(git_repository_path(repo2
) != NULL
);
239 cl_assert_(git__suffixcmp(git_repository_path(repo2
), "alternate/.git/") == 0, git_repository_path(repo2
));
241 cl_assert(git_repository_workdir(repo2
) != NULL
);
242 cl_assert_(git__suffixcmp(git_repository_workdir(repo2
), "alternate/") == 0, git_repository_workdir(repo2
));
244 git_repository_free(repo2
);
250 void test_repo_open__failures(void)
252 git_repository
*base
, *repo
;
253 git_str ceiling
= GIT_STR_INIT
;
255 base
= cl_git_sandbox_init("attr");
256 cl_git_pass(git_str_sets(&ceiling
, git_repository_workdir(base
)));
258 /* fail with no searching */
259 cl_git_fail(git_repository_open(&repo
, "attr/sub"));
260 cl_git_fail(git_repository_open_ext(
261 &repo
, "attr/sub", GIT_REPOSITORY_OPEN_NO_SEARCH
, NULL
));
263 /* fail with ceiling too low */
264 cl_git_fail(git_repository_open_ext(&repo
, "attr/sub", 0, ceiling
.ptr
));
265 cl_git_pass(git_str_joinpath(&ceiling
, ceiling
.ptr
, "sub"));
266 cl_git_fail(git_repository_open_ext(&repo
, "attr/sub/sub", 0, ceiling
.ptr
));
268 /* fail with no repo */
269 cl_git_pass(p_mkdir("alternate", 0777));
270 cl_git_pass(p_mkdir("alternate/.git", 0777));
271 cl_git_fail(git_repository_open_ext(&repo
, "alternate", 0, NULL
));
272 cl_git_fail(git_repository_open_ext(&repo
, "alternate/.git", 0, NULL
));
274 /* fail with no searching and no appending .git */
275 cl_git_fail(git_repository_open_ext(
277 GIT_REPOSITORY_OPEN_NO_SEARCH
| GIT_REPOSITORY_OPEN_NO_DOTGIT
,
280 git_str_dispose(&ceiling
);
283 void test_repo_open__bad_gitlinks(void)
285 git_repository
*repo
;
286 static const char *bad_links
[] = {
287 "garbage\n", "gitdir", "gitdir:\n", "gitdir: foobar",
288 "gitdir: ../invalid", "gitdir: ../invalid2",
289 "gitdir: ../attr/.git with extra stuff",
294 cl_git_sandbox_init("attr");
296 cl_git_pass(p_mkdir("invalid", 0777));
297 cl_git_pass(git_futils_mkdir_r("invalid2/.git", 0777));
299 for (scan
= bad_links
; *scan
!= NULL
; scan
++) {
300 make_gitlink_dir("alternate", *scan
);
302 cl_git_fail(git_repository_open_ext(&repo
, "alternate", 0, NULL
));
303 cl_assert(repo
== NULL
);
306 git_futils_rmdir_r("invalid", NULL
, GIT_RMDIR_REMOVE_FILES
);
307 git_futils_rmdir_r("invalid2", NULL
, GIT_RMDIR_REMOVE_FILES
);
311 static void unposix_path(git_str
*path
)
315 src
= tgt
= path
->ptr
;
317 /* convert "/d/..." to "d:\..." */
318 if (src
[0] == '/' && isalpha(src
[1]) && src
[2] == '/') {
326 *tgt
++ = (*src
== '/') ? '\\' : *src
;
334 void test_repo_open__win32_path(void)
337 git_repository
*repo
= cl_git_sandbox_init("empty_standard_repo"), *repo2
;
338 git_str winpath
= GIT_STR_INIT
;
339 static const char *repo_path
= "empty_standard_repo/.git/";
340 static const char *repo_wd
= "empty_standard_repo/";
342 cl_assert(git__suffixcmp(git_repository_path(repo
), repo_path
) == 0);
343 cl_assert(git__suffixcmp(git_repository_workdir(repo
), repo_wd
) == 0);
345 cl_git_pass(git_str_sets(&winpath
, git_repository_path(repo
)));
346 unposix_path(&winpath
);
347 cl_git_pass(git_repository_open(&repo2
, winpath
.ptr
));
348 cl_assert(git__suffixcmp(git_repository_path(repo2
), repo_path
) == 0);
349 cl_assert(git__suffixcmp(git_repository_workdir(repo2
), repo_wd
) == 0);
350 git_repository_free(repo2
);
352 cl_git_pass(git_str_sets(&winpath
, git_repository_path(repo
)));
353 git_str_truncate(&winpath
, winpath
.size
- 1); /* remove trailing '/' */
354 unposix_path(&winpath
);
355 cl_git_pass(git_repository_open(&repo2
, winpath
.ptr
));
356 cl_assert(git__suffixcmp(git_repository_path(repo2
), repo_path
) == 0);
357 cl_assert(git__suffixcmp(git_repository_workdir(repo2
), repo_wd
) == 0);
358 git_repository_free(repo2
);
360 cl_git_pass(git_str_sets(&winpath
, git_repository_workdir(repo
)));
361 unposix_path(&winpath
);
362 cl_git_pass(git_repository_open(&repo2
, winpath
.ptr
));
363 cl_assert(git__suffixcmp(git_repository_path(repo2
), repo_path
) == 0);
364 cl_assert(git__suffixcmp(git_repository_workdir(repo2
), repo_wd
) == 0);
365 git_repository_free(repo2
);
367 cl_git_pass(git_str_sets(&winpath
, git_repository_workdir(repo
)));
368 git_str_truncate(&winpath
, winpath
.size
- 1); /* remove trailing '/' */
369 unposix_path(&winpath
);
370 cl_git_pass(git_repository_open(&repo2
, winpath
.ptr
));
371 cl_assert(git__suffixcmp(git_repository_path(repo2
), repo_path
) == 0);
372 cl_assert(git__suffixcmp(git_repository_workdir(repo2
), repo_wd
) == 0);
373 git_repository_free(repo2
);
375 git_str_dispose(&winpath
);
379 void test_repo_open__opening_a_non_existing_repository_returns_ENOTFOUND(void)
381 git_repository
*repo
;
382 cl_assert_equal_i(GIT_ENOTFOUND
, git_repository_open(&repo
, "i-do-not/exist"));
385 void test_repo_open__no_config(void)
387 git_str path
= GIT_STR_INIT
;
388 git_repository
*repo
;
391 cl_fixture_sandbox("empty_standard_repo");
392 cl_git_pass(cl_rename(
393 "empty_standard_repo/.gitted", "empty_standard_repo/.git"));
395 /* remove local config */
396 cl_git_pass(git_futils_rmdir_r(
397 "empty_standard_repo/.git/config", NULL
, GIT_RMDIR_REMOVE_FILES
));
399 /* isolate from system level configs */
400 cl_must_pass(p_mkdir("alternate", 0777));
401 cl_git_pass(git_fs_path_prettify(&path
, "alternate", NULL
));
402 cl_git_pass(git_libgit2_opts(
403 GIT_OPT_SET_SEARCH_PATH
, GIT_CONFIG_LEVEL_GLOBAL
, path
.ptr
));
404 cl_git_pass(git_libgit2_opts(
405 GIT_OPT_SET_SEARCH_PATH
, GIT_CONFIG_LEVEL_SYSTEM
, path
.ptr
));
406 cl_git_pass(git_libgit2_opts(
407 GIT_OPT_SET_SEARCH_PATH
, GIT_CONFIG_LEVEL_XDG
, path
.ptr
));
409 git_str_dispose(&path
);
411 cl_git_pass(git_repository_open(&repo
, "empty_standard_repo"));
412 cl_git_pass(git_repository_config(&config
, repo
));
414 cl_git_pass(git_config_set_string(config
, "test.set", "42"));
416 git_config_free(config
);
417 git_repository_free(repo
);
418 cl_fixture_cleanup("empty_standard_repo");
420 cl_sandbox_set_search_path_defaults();
423 void test_repo_open__force_bare(void)
425 /* need to have both repo dir and workdir set up correctly */
426 git_repository
*repo
= cl_git_sandbox_init("empty_standard_repo");
427 git_repository
*barerepo
;
429 make_gitlink_dir("alternate", "gitdir: ../empty_standard_repo/.git");
431 cl_assert(!git_repository_is_bare(repo
));
433 cl_git_pass(git_repository_open(&barerepo
, "alternate"));
434 cl_assert(!git_repository_is_bare(barerepo
));
435 git_repository_free(barerepo
);
437 cl_git_pass(git_repository_open_bare(
438 &barerepo
, "empty_standard_repo/.git"));
439 cl_assert(git_repository_is_bare(barerepo
));
440 git_repository_free(barerepo
);
442 cl_git_fail(git_repository_open_bare(&barerepo
, "alternate/.git"));
444 cl_git_pass(git_repository_open_ext(
445 &barerepo
, "alternate/.git", GIT_REPOSITORY_OPEN_BARE
, NULL
));
446 cl_assert(git_repository_is_bare(barerepo
));
447 git_repository_free(barerepo
);
449 cl_git_pass(p_mkdir("empty_standard_repo/subdir", 0777));
450 cl_git_mkfile("empty_standard_repo/subdir/something.txt", "something");
452 cl_git_fail(git_repository_open_bare(
453 &barerepo
, "empty_standard_repo/subdir"));
455 cl_git_pass(git_repository_open_ext(
456 &barerepo
, "empty_standard_repo/subdir", GIT_REPOSITORY_OPEN_BARE
, NULL
));
457 cl_assert(git_repository_is_bare(barerepo
));
458 git_repository_free(barerepo
);
460 cl_git_pass(p_mkdir("alternate/subdir", 0777));
461 cl_git_pass(p_mkdir("alternate/subdir/sub2", 0777));
462 cl_git_mkfile("alternate/subdir/sub2/something.txt", "something");
464 cl_git_fail(git_repository_open_bare(&barerepo
, "alternate/subdir/sub2"));
466 cl_git_pass(git_repository_open_ext(
467 &barerepo
, "alternate/subdir/sub2",
468 GIT_REPOSITORY_OPEN_BARE
|GIT_REPOSITORY_OPEN_CROSS_FS
, NULL
));
469 cl_assert(git_repository_is_bare(barerepo
));
470 git_repository_free(barerepo
);
473 void test_repo_open__validates_dir_ownership(void)
475 git_repository
*repo
;
477 cl_git_pass(git_libgit2_opts(GIT_OPT_SET_OWNER_VALIDATION
, 1));
479 cl_fixture_sandbox("empty_standard_repo");
480 cl_git_pass(cl_rename("empty_standard_repo/.gitted", "empty_standard_repo/.git"));
482 /* When the current user owns the repo config, that's acceptable */
483 git_fs_path__set_owner(GIT_FS_PATH_MOCK_OWNER_CURRENT_USER
);
484 cl_git_pass(git_repository_open(&repo
, "empty_standard_repo"));
485 git_repository_free(repo
);
487 /* When the system user owns the repo config, fail */
488 git_fs_path__set_owner(GIT_FS_PATH_MOCK_OWNER_SYSTEM
);
489 cl_git_fail(git_repository_open(&repo
, "empty_standard_repo"));
491 /* When an unknown user owns the repo config, fail */
492 git_fs_path__set_owner(GIT_FS_PATH_MOCK_OWNER_OTHER
);
493 cl_git_fail(git_repository_open(&repo
, "empty_standard_repo"));
496 void test_repo_open__can_allowlist_dirs_with_problematic_ownership(void)
498 git_repository
*repo
;
499 git_str config_path
= GIT_STR_INIT
,
500 config_filename
= GIT_STR_INIT
,
501 config_data
= GIT_STR_INIT
;
503 cl_git_pass(git_libgit2_opts(GIT_OPT_SET_OWNER_VALIDATION
, 1));
505 cl_fixture_sandbox("empty_standard_repo");
506 cl_git_pass(cl_rename("empty_standard_repo/.gitted", "empty_standard_repo/.git"));
508 git_fs_path__set_owner(GIT_FS_PATH_MOCK_OWNER_OTHER
);
509 cl_git_fail(git_repository_open(&repo
, "empty_standard_repo"));
511 /* Add safe.directory options to the global configuration */
512 git_str_joinpath(&config_path
, clar_sandbox_path(), "__global_config");
513 cl_must_pass(p_mkdir(config_path
.ptr
, 0777));
514 git_libgit2_opts(GIT_OPT_SET_SEARCH_PATH
, GIT_CONFIG_LEVEL_GLOBAL
, config_path
.ptr
);
516 git_str_joinpath(&config_filename
, config_path
.ptr
, ".gitconfig");
518 git_str_printf(&config_data
,
523 "\tdirectory = /non/existent/path\n" \
524 "\tdirectory = /\n" \
525 "\tdirectory = c:\\\\temp\n" \
526 "\tdirectory = %s/%s\n" \
527 "\tdirectory = /tmp\n" \
530 clar_sandbox_path(), "empty_standard_repo");
531 cl_git_rewritefile(config_filename
.ptr
, config_data
.ptr
);
533 cl_git_pass(git_repository_open(&repo
, "empty_standard_repo"));
534 git_repository_free(repo
);
536 git_str_dispose(&config_path
);
537 git_str_dispose(&config_filename
);
538 git_str_dispose(&config_data
);
541 void test_repo_open__can_reset_safe_directory_list(void)
543 git_repository
*repo
;
544 git_str config_path
= GIT_STR_INIT
,
545 config_filename
= GIT_STR_INIT
,
546 config_data
= GIT_STR_INIT
;
548 cl_git_pass(git_libgit2_opts(GIT_OPT_SET_OWNER_VALIDATION
, 1));
550 cl_fixture_sandbox("empty_standard_repo");
551 cl_git_pass(cl_rename("empty_standard_repo/.gitted", "empty_standard_repo/.git"));
553 git_fs_path__set_owner(GIT_FS_PATH_MOCK_OWNER_OTHER
);
554 cl_git_fail(git_repository_open(&repo
, "empty_standard_repo"));
556 /* Add safe.directory options to the global configuration */
557 git_str_joinpath(&config_path
, clar_sandbox_path(), "__global_config");
558 cl_must_pass(p_mkdir(config_path
.ptr
, 0777));
559 git_libgit2_opts(GIT_OPT_SET_SEARCH_PATH
, GIT_CONFIG_LEVEL_GLOBAL
, config_path
.ptr
);
561 git_str_joinpath(&config_filename
, config_path
.ptr
, ".gitconfig");
563 /* The blank resets our sandbox directory and opening fails */
565 git_str_printf(&config_data
,
570 "\tdirectory = %s/%s\n" \
572 "\tdirectory = /tmp\n" \
575 clar_sandbox_path(), "empty_standard_repo");
576 cl_git_rewritefile(config_filename
.ptr
, config_data
.ptr
);
578 cl_git_fail(git_repository_open(&repo
, "empty_standard_repo"));
580 /* The blank resets tmp and allows subsequent declarations to succeed */
582 git_str_clear(&config_data
);
583 git_str_printf(&config_data
,
588 "\tdirectory = /tmp\n" \
590 "\tdirectory = %s/%s\n" \
593 clar_sandbox_path(), "empty_standard_repo");
594 cl_git_rewritefile(config_filename
.ptr
, config_data
.ptr
);
596 cl_git_pass(git_repository_open(&repo
, "empty_standard_repo"));
597 git_repository_free(repo
);
599 git_str_dispose(&config_path
);
600 git_str_dispose(&config_filename
);
601 git_str_dispose(&config_data
);