1 #include "clar_libgit2.h"
6 void test_repo_open__cleanup(void)
8 cl_git_sandbox_cleanup();
10 if (git_path_isdir("alternate"))
11 git_futils_rmdir_r("alternate", NULL
, GIT_RMDIR_REMOVE_FILES
);
14 void test_repo_open__bare_empty_repo(void)
16 git_repository
*repo
= cl_git_sandbox_init("empty_bare.git");
18 cl_assert(git_repository_path(repo
) != NULL
);
19 cl_assert(git__suffixcmp(git_repository_path(repo
), "/") == 0);
20 cl_assert(git_repository_workdir(repo
) == NULL
);
23 void test_repo_open__format_version_1(void)
28 repo
= cl_git_sandbox_init("empty_bare.git");
30 cl_git_pass(git_repository_open(&repo
, "empty_bare.git"));
31 cl_git_pass(git_repository_config(&config
, repo
));
33 cl_git_pass(git_config_set_int32(config
, "core.repositoryformatversion", 1));
35 git_config_free(config
);
36 git_repository_free(repo
);
37 cl_git_fail(git_repository_open(&repo
, "empty_bare.git"));
40 void test_repo_open__standard_empty_repo_through_gitdir(void)
44 cl_git_pass(git_repository_open(&repo
, cl_fixture("empty_standard_repo/.gitted")));
46 cl_assert(git_repository_path(repo
) != NULL
);
47 cl_assert(git__suffixcmp(git_repository_path(repo
), "/") == 0);
49 cl_assert(git_repository_workdir(repo
) != NULL
);
50 cl_assert(git__suffixcmp(git_repository_workdir(repo
), "/") == 0);
52 git_repository_free(repo
);
55 void test_repo_open__standard_empty_repo_through_workdir(void)
57 git_repository
*repo
= cl_git_sandbox_init("empty_standard_repo");
59 cl_assert(git_repository_path(repo
) != NULL
);
60 cl_assert(git__suffixcmp(git_repository_path(repo
), "/") == 0);
62 cl_assert(git_repository_workdir(repo
) != NULL
);
63 cl_assert(git__suffixcmp(git_repository_workdir(repo
), "/") == 0);
67 void test_repo_open__open_with_discover(void)
69 static const char *variants
[] = {
70 "attr", "attr/", "attr/.git", "attr/.git/",
71 "attr/sub", "attr/sub/", "attr/sub/sub", "attr/sub/sub/",
77 cl_fixture_sandbox("attr");
78 cl_git_pass(p_rename("attr/.gitted", "attr/.git"));
80 for (scan
= variants
; *scan
!= NULL
; scan
++) {
81 cl_git_pass(git_repository_open_ext(&repo
, *scan
, 0, NULL
));
82 cl_assert(git__suffixcmp(git_repository_path(repo
), "attr/.git/") == 0);
83 cl_assert(git__suffixcmp(git_repository_workdir(repo
), "attr/") == 0);
84 git_repository_free(repo
);
87 cl_fixture_cleanup("attr");
90 static void make_gitlink_dir(const char *dir
, const char *linktext
)
92 git_buf path
= GIT_BUF_INIT
;
94 cl_git_pass(git_futils_mkdir(dir
, 0777, GIT_MKDIR_VERIFY_DIR
));
95 cl_git_pass(git_buf_joinpath(&path
, dir
, ".git"));
96 cl_git_rewritefile(path
.ptr
, linktext
);
100 void test_repo_open__gitlinked(void)
102 /* need to have both repo dir and workdir set up correctly */
103 git_repository
*repo
= cl_git_sandbox_init("empty_standard_repo");
104 git_repository
*repo2
;
106 make_gitlink_dir("alternate", "gitdir: ../empty_standard_repo/.git");
108 cl_git_pass(git_repository_open(&repo2
, "alternate"));
110 cl_assert(git_repository_path(repo2
) != NULL
);
111 cl_assert_(git__suffixcmp(git_repository_path(repo2
), "empty_standard_repo/.git/") == 0, git_repository_path(repo2
));
112 cl_assert_equal_s(git_repository_path(repo
), git_repository_path(repo2
));
114 cl_assert(git_repository_workdir(repo2
) != NULL
);
115 cl_assert_(git__suffixcmp(git_repository_workdir(repo2
), "alternate/") == 0, git_repository_workdir(repo2
));
117 git_repository_free(repo2
);
120 void test_repo_open__from_git_new_workdir(void)
123 /* The git-new-workdir script that ships with git sets up a bunch of
124 * symlinks to create a second workdir that shares the object db with
125 * another checkout. Libgit2 can open a repo that has been configured
129 git_repository
*repo2
;
130 git_buf link_tgt
= GIT_BUF_INIT
, link
= GIT_BUF_INIT
, body
= GIT_BUF_INIT
;
133 static const char *links
[] = {
134 "config", "refs", "logs/refs", "objects", "info", "hooks",
135 "packed-refs", "remotes", "rr-cache", "svn", NULL
137 static const char *copies
[] = {
141 cl_git_sandbox_init("empty_standard_repo");
143 cl_git_pass(p_mkdir("alternate", 0777));
144 cl_git_pass(p_mkdir("alternate/.git", 0777));
146 for (scan
= links
; *scan
!= NULL
; scan
++) {
147 git_buf_joinpath(&link_tgt
, "empty_standard_repo/.git", *scan
);
148 if (git_path_exists(link_tgt
.ptr
)) {
149 git_buf_joinpath(&link_tgt
, "../../empty_standard_repo/.git", *scan
);
150 git_buf_joinpath(&link
, "alternate/.git", *scan
);
151 if (strchr(*scan
, '/'))
152 git_futils_mkpath2file(link
.ptr
, 0777);
153 cl_assert_(symlink(link_tgt
.ptr
, link
.ptr
) == 0, strerror(errno
));
156 for (scan
= copies
; *scan
!= NULL
; scan
++) {
157 git_buf_joinpath(&link_tgt
, "empty_standard_repo/.git", *scan
);
158 if (git_path_exists(link_tgt
.ptr
)) {
159 git_buf_joinpath(&link
, "alternate/.git", *scan
);
160 cl_git_pass(git_futils_readbuffer(&body
, link_tgt
.ptr
));
162 cl_assert((link_fd
= git_futils_creat_withpath(link
.ptr
, 0777, 0666)) >= 0);
163 cl_must_pass(p_write(link_fd
, body
.ptr
, body
.size
));
168 git_buf_free(&link_tgt
);
173 cl_git_pass(git_repository_open(&repo2
, "alternate"));
175 cl_assert(git_repository_path(repo2
) != NULL
);
176 cl_assert_(git__suffixcmp(git_repository_path(repo2
), "alternate/.git/") == 0, git_repository_path(repo2
));
178 cl_assert(git_repository_workdir(repo2
) != NULL
);
179 cl_assert_(git__suffixcmp(git_repository_workdir(repo2
), "alternate/") == 0, git_repository_workdir(repo2
));
181 git_repository_free(repo2
);
185 void test_repo_open__failures(void)
187 git_repository
*base
, *repo
;
188 git_buf ceiling
= GIT_BUF_INIT
;
190 base
= cl_git_sandbox_init("attr");
191 cl_git_pass(git_buf_sets(&ceiling
, git_repository_workdir(base
)));
193 /* fail with no searching */
194 cl_git_fail(git_repository_open(&repo
, "attr/sub"));
195 cl_git_fail(git_repository_open_ext(
196 &repo
, "attr/sub", GIT_REPOSITORY_OPEN_NO_SEARCH
, NULL
));
198 /* fail with ceiling too low */
199 cl_git_fail(git_repository_open_ext(&repo
, "attr/sub", 0, ceiling
.ptr
));
200 cl_git_pass(git_buf_joinpath(&ceiling
, ceiling
.ptr
, "sub"));
201 cl_git_fail(git_repository_open_ext(&repo
, "attr/sub/sub", 0, ceiling
.ptr
));
203 /* fail with no repo */
204 cl_git_pass(p_mkdir("alternate", 0777));
205 cl_git_pass(p_mkdir("alternate/.git", 0777));
206 cl_git_fail(git_repository_open_ext(&repo
, "alternate", 0, NULL
));
207 cl_git_fail(git_repository_open_ext(&repo
, "alternate/.git", 0, NULL
));
209 git_buf_free(&ceiling
);
212 void test_repo_open__bad_gitlinks(void)
214 git_repository
*repo
;
215 static const char *bad_links
[] = {
216 "garbage\n", "gitdir", "gitdir:\n", "gitdir: foobar",
217 "gitdir: ../invalid", "gitdir: ../invalid2",
218 "gitdir: ../attr/.git with extra stuff",
223 cl_git_sandbox_init("attr");
225 cl_git_pass(p_mkdir("invalid", 0777));
226 cl_git_pass(git_futils_mkdir_r("invalid2/.git", 0777));
228 for (scan
= bad_links
; *scan
!= NULL
; scan
++) {
229 make_gitlink_dir("alternate", *scan
);
230 cl_git_fail(git_repository_open_ext(&repo
, "alternate", 0, NULL
));
233 git_futils_rmdir_r("invalid", NULL
, GIT_RMDIR_REMOVE_FILES
);
234 git_futils_rmdir_r("invalid2", NULL
, GIT_RMDIR_REMOVE_FILES
);
238 static void unposix_path(git_buf
*path
)
242 src
= tgt
= path
->ptr
;
244 /* convert "/d/..." to "d:\..." */
245 if (src
[0] == '/' && isalpha(src
[1]) && src
[2] == '/') {
253 *tgt
++ = (*src
== '/') ? '\\' : *src
;
261 void test_repo_open__win32_path(void)
264 git_repository
*repo
= cl_git_sandbox_init("empty_standard_repo"), *repo2
;
265 git_buf winpath
= GIT_BUF_INIT
;
266 static const char *repo_path
= "empty_standard_repo/.git/";
267 static const char *repo_wd
= "empty_standard_repo/";
269 cl_assert(git__suffixcmp(git_repository_path(repo
), repo_path
) == 0);
270 cl_assert(git__suffixcmp(git_repository_workdir(repo
), repo_wd
) == 0);
272 cl_git_pass(git_buf_sets(&winpath
, git_repository_path(repo
)));
273 unposix_path(&winpath
);
274 cl_git_pass(git_repository_open(&repo2
, winpath
.ptr
));
275 cl_assert(git__suffixcmp(git_repository_path(repo2
), repo_path
) == 0);
276 cl_assert(git__suffixcmp(git_repository_workdir(repo2
), repo_wd
) == 0);
277 git_repository_free(repo2
);
279 cl_git_pass(git_buf_sets(&winpath
, git_repository_path(repo
)));
280 git_buf_truncate(&winpath
, winpath
.size
- 1); /* remove trailing '/' */
281 unposix_path(&winpath
);
282 cl_git_pass(git_repository_open(&repo2
, winpath
.ptr
));
283 cl_assert(git__suffixcmp(git_repository_path(repo2
), repo_path
) == 0);
284 cl_assert(git__suffixcmp(git_repository_workdir(repo2
), repo_wd
) == 0);
285 git_repository_free(repo2
);
287 cl_git_pass(git_buf_sets(&winpath
, git_repository_workdir(repo
)));
288 unposix_path(&winpath
);
289 cl_git_pass(git_repository_open(&repo2
, winpath
.ptr
));
290 cl_assert(git__suffixcmp(git_repository_path(repo2
), repo_path
) == 0);
291 cl_assert(git__suffixcmp(git_repository_workdir(repo2
), repo_wd
) == 0);
292 git_repository_free(repo2
);
294 cl_git_pass(git_buf_sets(&winpath
, git_repository_workdir(repo
)));
295 git_buf_truncate(&winpath
, winpath
.size
- 1); /* remove trailing '/' */
296 unposix_path(&winpath
);
297 cl_git_pass(git_repository_open(&repo2
, winpath
.ptr
));
298 cl_assert(git__suffixcmp(git_repository_path(repo2
), repo_path
) == 0);
299 cl_assert(git__suffixcmp(git_repository_workdir(repo2
), repo_wd
) == 0);
300 git_repository_free(repo2
);
302 git_buf_free(&winpath
);
306 void test_repo_open__opening_a_non_existing_repository_returns_ENOTFOUND(void)
308 git_repository
*repo
;
309 cl_assert_equal_i(GIT_ENOTFOUND
, git_repository_open(&repo
, "i-do-not/exist"));
312 void test_repo_open__no_config(void)
314 git_buf path
= GIT_BUF_INIT
;
315 git_repository
*repo
;
318 cl_fixture_sandbox("empty_standard_repo");
319 cl_git_pass(cl_rename(
320 "empty_standard_repo/.gitted", "empty_standard_repo/.git"));
322 /* remove local config */
323 cl_git_pass(git_futils_rmdir_r(
324 "empty_standard_repo/.git/config", NULL
, GIT_RMDIR_REMOVE_FILES
));
326 /* isolate from system level configs */
327 cl_must_pass(p_mkdir("alternate", 0777));
328 cl_git_pass(git_path_prettify(&path
, "alternate", NULL
));
329 cl_git_pass(git_libgit2_opts(
330 GIT_OPT_SET_SEARCH_PATH
, GIT_CONFIG_LEVEL_GLOBAL
, path
.ptr
));
331 cl_git_pass(git_libgit2_opts(
332 GIT_OPT_SET_SEARCH_PATH
, GIT_CONFIG_LEVEL_SYSTEM
, path
.ptr
));
333 cl_git_pass(git_libgit2_opts(
334 GIT_OPT_SET_SEARCH_PATH
, GIT_CONFIG_LEVEL_XDG
, path
.ptr
));
338 cl_git_pass(git_repository_open(&repo
, "empty_standard_repo"));
339 cl_git_pass(git_repository_config(&config
, repo
));
341 cl_git_pass(git_config_set_string(config
, "test.set", "42"));
343 git_config_free(config
);
344 git_repository_free(repo
);
345 cl_fixture_cleanup("empty_standard_repo");
347 cl_sandbox_set_search_path_defaults();
350 void test_repo_open__force_bare(void)
352 /* need to have both repo dir and workdir set up correctly */
353 git_repository
*repo
= cl_git_sandbox_init("empty_standard_repo");
354 git_repository
*barerepo
;
356 make_gitlink_dir("alternate", "gitdir: ../empty_standard_repo/.git");
358 cl_assert(!git_repository_is_bare(repo
));
360 cl_git_pass(git_repository_open(&barerepo
, "alternate"));
361 cl_assert(!git_repository_is_bare(barerepo
));
362 git_repository_free(barerepo
);
364 cl_git_pass(git_repository_open_bare(
365 &barerepo
, "empty_standard_repo/.git"));
366 cl_assert(git_repository_is_bare(barerepo
));
367 git_repository_free(barerepo
);
369 cl_git_fail(git_repository_open_bare(&barerepo
, "alternate/.git"));
371 cl_git_pass(git_repository_open_ext(
372 &barerepo
, "alternate/.git", GIT_REPOSITORY_OPEN_BARE
, NULL
));
373 cl_assert(git_repository_is_bare(barerepo
));
374 git_repository_free(barerepo
);
376 cl_git_pass(p_mkdir("empty_standard_repo/subdir", 0777));
377 cl_git_mkfile("empty_standard_repo/subdir/something.txt", "something");
379 cl_git_fail(git_repository_open_bare(
380 &barerepo
, "empty_standard_repo/subdir"));
382 cl_git_pass(git_repository_open_ext(
383 &barerepo
, "empty_standard_repo/subdir", GIT_REPOSITORY_OPEN_BARE
, NULL
));
384 cl_assert(git_repository_is_bare(barerepo
));
385 git_repository_free(barerepo
);
387 cl_git_pass(p_mkdir("alternate/subdir", 0777));
388 cl_git_pass(p_mkdir("alternate/subdir/sub2", 0777));
389 cl_git_mkfile("alternate/subdir/sub2/something.txt", "something");
391 cl_git_fail(git_repository_open_bare(&barerepo
, "alternate/subdir/sub2"));
393 cl_git_pass(git_repository_open_ext(
394 &barerepo
, "alternate/subdir/sub2", GIT_REPOSITORY_OPEN_BARE
, NULL
));
395 cl_assert(git_repository_is_bare(barerepo
));
396 git_repository_free(barerepo
);