1 #include "clar_libgit2.h"
5 void test_repo_open__cleanup(void)
7 cl_git_sandbox_cleanup();
9 if (git_path_isdir("alternate"))
10 git_futils_rmdir_r("alternate", NULL
, GIT_RMDIR_REMOVE_FILES
);
13 void test_repo_open__bare_empty_repo(void)
15 git_repository
*repo
= cl_git_sandbox_init("empty_bare.git");
17 cl_assert(git_repository_path(repo
) != NULL
);
18 cl_assert(git__suffixcmp(git_repository_path(repo
), "/") == 0);
19 cl_assert(git_repository_workdir(repo
) == NULL
);
22 void test_repo_open__standard_empty_repo_through_gitdir(void)
26 cl_git_pass(git_repository_open(&repo
, cl_fixture("empty_standard_repo/.gitted")));
28 cl_assert(git_repository_path(repo
) != NULL
);
29 cl_assert(git__suffixcmp(git_repository_path(repo
), "/") == 0);
31 cl_assert(git_repository_workdir(repo
) != NULL
);
32 cl_assert(git__suffixcmp(git_repository_workdir(repo
), "/") == 0);
34 git_repository_free(repo
);
37 void test_repo_open__standard_empty_repo_through_workdir(void)
39 git_repository
*repo
= cl_git_sandbox_init("empty_standard_repo");
41 cl_assert(git_repository_path(repo
) != NULL
);
42 cl_assert(git__suffixcmp(git_repository_path(repo
), "/") == 0);
44 cl_assert(git_repository_workdir(repo
) != NULL
);
45 cl_assert(git__suffixcmp(git_repository_workdir(repo
), "/") == 0);
49 void test_repo_open__open_with_discover(void)
51 static const char *variants
[] = {
52 "attr", "attr/", "attr/.git", "attr/.git/",
53 "attr/sub", "attr/sub/", "attr/sub/sub", "attr/sub/sub/",
59 cl_fixture_sandbox("attr");
60 cl_git_pass(p_rename("attr/.gitted", "attr/.git"));
62 for (scan
= variants
; *scan
!= NULL
; scan
++) {
63 cl_git_pass(git_repository_open_ext(&repo
, *scan
, 0, NULL
));
64 cl_assert(git__suffixcmp(git_repository_path(repo
), "attr/.git/") == 0);
65 cl_assert(git__suffixcmp(git_repository_workdir(repo
), "attr/") == 0);
66 git_repository_free(repo
);
69 cl_fixture_cleanup("attr");
72 static void make_gitlink_dir(const char *dir
, const char *linktext
)
74 git_buf path
= GIT_BUF_INIT
;
76 cl_git_pass(git_futils_mkdir(dir
, NULL
, 0777, GIT_MKDIR_VERIFY_DIR
));
77 cl_git_pass(git_buf_joinpath(&path
, dir
, ".git"));
78 cl_git_rewritefile(path
.ptr
, linktext
);
82 void test_repo_open__gitlinked(void)
84 /* need to have both repo dir and workdir set up correctly */
85 git_repository
*repo
= cl_git_sandbox_init("empty_standard_repo");
86 git_repository
*repo2
;
88 make_gitlink_dir("alternate", "gitdir: ../empty_standard_repo/.git");
90 cl_git_pass(git_repository_open(&repo2
, "alternate"));
92 cl_assert(git_repository_path(repo2
) != NULL
);
93 cl_assert_(git__suffixcmp(git_repository_path(repo2
), "empty_standard_repo/.git/") == 0, git_repository_path(repo2
));
94 cl_assert_equal_s(git_repository_path(repo
), git_repository_path(repo2
));
96 cl_assert(git_repository_workdir(repo2
) != NULL
);
97 cl_assert_(git__suffixcmp(git_repository_workdir(repo2
), "alternate/") == 0, git_repository_workdir(repo2
));
99 git_repository_free(repo2
);
102 void test_repo_open__from_git_new_workdir(void)
104 /* The git-new-workdir script that ships with git sets up a bunch of
105 * symlinks to create a second workdir that shares the object db with
106 * another checkout. Libgit2 can open a repo that has been configured
109 cl_git_sandbox_init("empty_standard_repo");
112 git_repository
*repo2
;
113 git_buf link_tgt
= GIT_BUF_INIT
, link
= GIT_BUF_INIT
, body
= GIT_BUF_INIT
;
116 static const char *links
[] = {
117 "config", "refs", "logs/refs", "objects", "info", "hooks",
118 "packed-refs", "remotes", "rr-cache", "svn", NULL
120 static const char *copies
[] = {
124 cl_git_pass(p_mkdir("alternate", 0777));
125 cl_git_pass(p_mkdir("alternate/.git", 0777));
127 for (scan
= links
; *scan
!= NULL
; scan
++) {
128 git_buf_joinpath(&link_tgt
, "empty_standard_repo/.git", *scan
);
129 if (git_path_exists(link_tgt
.ptr
)) {
130 git_buf_joinpath(&link_tgt
, "../../empty_standard_repo/.git", *scan
);
131 git_buf_joinpath(&link
, "alternate/.git", *scan
);
132 if (strchr(*scan
, '/'))
133 git_futils_mkpath2file(link
.ptr
, 0777);
134 cl_assert_(symlink(link_tgt
.ptr
, link
.ptr
) == 0, strerror(errno
));
137 for (scan
= copies
; *scan
!= NULL
; scan
++) {
138 git_buf_joinpath(&link_tgt
, "empty_standard_repo/.git", *scan
);
139 if (git_path_exists(link_tgt
.ptr
)) {
140 git_buf_joinpath(&link
, "alternate/.git", *scan
);
141 cl_git_pass(git_futils_readbuffer(&body
, link_tgt
.ptr
));
143 cl_assert((link_fd
= git_futils_creat_withpath(link
.ptr
, 0777, 0666)) >= 0);
144 cl_must_pass(p_write(link_fd
, body
.ptr
, body
.size
));
149 git_buf_free(&link_tgt
);
154 cl_git_pass(git_repository_open(&repo2
, "alternate"));
156 cl_assert(git_repository_path(repo2
) != NULL
);
157 cl_assert_(git__suffixcmp(git_repository_path(repo2
), "alternate/.git/") == 0, git_repository_path(repo2
));
159 cl_assert(git_repository_workdir(repo2
) != NULL
);
160 cl_assert_(git__suffixcmp(git_repository_workdir(repo2
), "alternate/") == 0, git_repository_workdir(repo2
));
162 git_repository_free(repo2
);
166 void test_repo_open__failures(void)
168 git_repository
*base
, *repo
;
169 git_buf ceiling
= GIT_BUF_INIT
;
171 base
= cl_git_sandbox_init("attr");
172 cl_git_pass(git_buf_sets(&ceiling
, git_repository_workdir(base
)));
174 /* fail with no searching */
175 cl_git_fail(git_repository_open(&repo
, "attr/sub"));
176 cl_git_fail(git_repository_open_ext(
177 &repo
, "attr/sub", GIT_REPOSITORY_OPEN_NO_SEARCH
, NULL
));
179 /* fail with ceiling too low */
180 cl_git_pass(git_buf_joinpath(&ceiling
, ceiling
.ptr
, "sub"));
181 cl_git_fail(git_repository_open_ext(&repo
, "attr/sub", 0, ceiling
.ptr
));
183 /* fail with no repo */
184 cl_git_pass(p_mkdir("alternate", 0777));
185 cl_git_pass(p_mkdir("alternate/.git", 0777));
186 cl_git_fail(git_repository_open_ext(&repo
, "alternate", 0, NULL
));
187 cl_git_fail(git_repository_open_ext(&repo
, "alternate/.git", 0, NULL
));
189 git_buf_free(&ceiling
);
192 void test_repo_open__bad_gitlinks(void)
194 git_repository
*repo
;
195 static const char *bad_links
[] = {
196 "garbage\n", "gitdir", "gitdir:\n", "gitdir: foobar",
197 "gitdir: ../invalid", "gitdir: ../invalid2",
198 "gitdir: ../attr/.git with extra stuff",
203 cl_git_sandbox_init("attr");
205 cl_git_pass(p_mkdir("invalid", 0777));
206 cl_git_pass(git_futils_mkdir_r("invalid2/.git", NULL
, 0777));
208 for (scan
= bad_links
; *scan
!= NULL
; scan
++) {
209 make_gitlink_dir("alternate", *scan
);
210 cl_git_fail(git_repository_open_ext(&repo
, "alternate", 0, NULL
));
213 git_futils_rmdir_r("invalid", NULL
, GIT_RMDIR_REMOVE_FILES
);
214 git_futils_rmdir_r("invalid2", NULL
, GIT_RMDIR_REMOVE_FILES
);
218 static void unposix_path(git_buf
*path
)
222 src
= tgt
= path
->ptr
;
224 /* convert "/d/..." to "d:\..." */
225 if (src
[0] == '/' && isalpha(src
[1]) && src
[2] == '/') {
233 *tgt
++ = (*src
== '/') ? '\\' : *src
;
241 void test_repo_open__win32_path(void)
244 git_repository
*repo
= cl_git_sandbox_init("empty_standard_repo"), *repo2
;
245 git_buf winpath
= GIT_BUF_INIT
;
246 static const char *repo_path
= "empty_standard_repo/.git/";
247 static const char *repo_wd
= "empty_standard_repo/";
249 cl_assert(git__suffixcmp(git_repository_path(repo
), repo_path
) == 0);
250 cl_assert(git__suffixcmp(git_repository_workdir(repo
), repo_wd
) == 0);
252 cl_git_pass(git_buf_sets(&winpath
, git_repository_path(repo
)));
253 unposix_path(&winpath
);
254 cl_git_pass(git_repository_open(&repo2
, winpath
.ptr
));
255 cl_assert(git__suffixcmp(git_repository_path(repo2
), repo_path
) == 0);
256 cl_assert(git__suffixcmp(git_repository_workdir(repo2
), repo_wd
) == 0);
257 git_repository_free(repo2
);
259 cl_git_pass(git_buf_sets(&winpath
, git_repository_path(repo
)));
260 git_buf_truncate(&winpath
, winpath
.size
- 1); /* remove trailing '/' */
261 unposix_path(&winpath
);
262 cl_git_pass(git_repository_open(&repo2
, winpath
.ptr
));
263 cl_assert(git__suffixcmp(git_repository_path(repo2
), repo_path
) == 0);
264 cl_assert(git__suffixcmp(git_repository_workdir(repo2
), repo_wd
) == 0);
265 git_repository_free(repo2
);
267 cl_git_pass(git_buf_sets(&winpath
, git_repository_workdir(repo
)));
268 unposix_path(&winpath
);
269 cl_git_pass(git_repository_open(&repo2
, winpath
.ptr
));
270 cl_assert(git__suffixcmp(git_repository_path(repo2
), repo_path
) == 0);
271 cl_assert(git__suffixcmp(git_repository_workdir(repo2
), repo_wd
) == 0);
272 git_repository_free(repo2
);
274 cl_git_pass(git_buf_sets(&winpath
, git_repository_workdir(repo
)));
275 git_buf_truncate(&winpath
, winpath
.size
- 1); /* remove trailing '/' */
276 unposix_path(&winpath
);
277 cl_git_pass(git_repository_open(&repo2
, winpath
.ptr
));
278 cl_assert(git__suffixcmp(git_repository_path(repo2
), repo_path
) == 0);
279 cl_assert(git__suffixcmp(git_repository_workdir(repo2
), repo_wd
) == 0);
280 git_repository_free(repo2
);
282 git_buf_free(&winpath
);
286 void test_repo_open__opening_a_non_existing_repository_returns_ENOTFOUND(void)
288 git_repository
*repo
;
289 cl_assert_equal_i(GIT_ENOTFOUND
, git_repository_open(&repo
, "i-do-not/exist"));
292 void test_repo_open__no_config(void)
294 git_buf path
= GIT_BUF_INIT
;
295 git_repository
*repo
;
298 cl_fixture_sandbox("empty_standard_repo");
299 cl_git_pass(cl_rename("empty_standard_repo/.gitted", "empty_standard_repo/.git"));
301 /* remove local config */
302 cl_git_pass(git_futils_rmdir_r(
303 "empty_standard_repo/.git/config", NULL
, GIT_RMDIR_REMOVE_FILES
));
305 /* isolate from system level configs */
306 cl_must_pass(p_mkdir("alternate", 0777));
307 cl_git_pass(git_path_prettify(&path
, "alternate", NULL
));
308 cl_git_pass(git_libgit2_opts(
309 GIT_OPT_SET_SEARCH_PATH
, GIT_CONFIG_LEVEL_GLOBAL
, path
.ptr
));
310 cl_git_pass(git_libgit2_opts(
311 GIT_OPT_SET_SEARCH_PATH
, GIT_CONFIG_LEVEL_SYSTEM
, path
.ptr
));
312 cl_git_pass(git_libgit2_opts(
313 GIT_OPT_SET_SEARCH_PATH
, GIT_CONFIG_LEVEL_XDG
, path
.ptr
));
317 cl_git_pass(git_repository_open(&repo
, "empty_standard_repo"));
318 cl_git_pass(git_repository_config(&config
, repo
));
320 cl_git_pass(git_config_set_string(config
, "test.set", "42"));
322 git_config_free(config
);
323 git_repository_free(repo
);
324 cl_fixture_cleanup("empty_standard_repo");
326 git_futils_dirs_global_shutdown();
329 void test_repo_open__force_bare(void)
331 /* need to have both repo dir and workdir set up correctly */
332 git_repository
*repo
= cl_git_sandbox_init("empty_standard_repo");
333 git_repository
*barerepo
;
335 make_gitlink_dir("alternate", "gitdir: ../empty_standard_repo/.git");
337 cl_assert(!git_repository_is_bare(repo
));
339 cl_git_pass(git_repository_open(&barerepo
, "alternate"));
340 cl_assert(!git_repository_is_bare(barerepo
));
341 git_repository_free(barerepo
);
343 cl_git_pass(git_repository_open_bare(
344 &barerepo
, "empty_standard_repo/.git"));
345 cl_assert(git_repository_is_bare(barerepo
));
346 git_repository_free(barerepo
);
348 cl_git_fail(git_repository_open_bare(&barerepo
, "alternate/.git"));
350 cl_git_pass(git_repository_open_ext(
351 &barerepo
, "alternate/.git", GIT_REPOSITORY_OPEN_BARE
, NULL
));
352 cl_assert(git_repository_is_bare(barerepo
));
353 git_repository_free(barerepo
);
355 cl_git_pass(p_mkdir("empty_standard_repo/subdir", 0777));
356 cl_git_mkfile("empty_standard_repo/subdir/something.txt", "something");
358 cl_git_fail(git_repository_open_bare(
359 &barerepo
, "empty_standard_repo/subdir"));
361 cl_git_pass(git_repository_open_ext(
362 &barerepo
, "empty_standard_repo/subdir", GIT_REPOSITORY_OPEN_BARE
, NULL
));
363 cl_assert(git_repository_is_bare(barerepo
));
364 git_repository_free(barerepo
);
366 cl_git_pass(p_mkdir("alternate/subdir", 0777));
367 cl_git_pass(p_mkdir("alternate/subdir/sub2", 0777));
368 cl_git_mkfile("alternate/subdir/sub2/something.txt", "something");
370 cl_git_fail(git_repository_open_bare(&barerepo
, "alternate/subdir/sub2"));
372 cl_git_pass(git_repository_open_ext(
373 &barerepo
, "alternate/subdir/sub2", GIT_REPOSITORY_OPEN_BARE
, NULL
));
374 cl_assert(git_repository_is_bare(barerepo
));
375 git_repository_free(barerepo
);