]> git.proxmox.com Git - libgit2.git/blob - tests/repo/open.c
Merge pull request #3948 from libgit2/cmn/v24-updates
[libgit2.git] / tests / repo / open.c
1 #include "clar_libgit2.h"
2 #include "fileops.h"
3 #include "sysdir.h"
4 #include <ctype.h>
5
6 void test_repo_open__cleanup(void)
7 {
8 cl_git_sandbox_cleanup();
9
10 if (git_path_isdir("alternate"))
11 git_futils_rmdir_r("alternate", NULL, GIT_RMDIR_REMOVE_FILES);
12 }
13
14 void test_repo_open__bare_empty_repo(void)
15 {
16 git_repository *repo = cl_git_sandbox_init("empty_bare.git");
17
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);
21 }
22
23 void test_repo_open__format_version_1(void)
24 {
25 git_repository *repo;
26 git_config *config;
27
28 repo = cl_git_sandbox_init("empty_bare.git");
29
30 cl_git_pass(git_repository_open(&repo, "empty_bare.git"));
31 cl_git_pass(git_repository_config(&config, repo));
32
33 cl_git_pass(git_config_set_int32(config, "core.repositoryformatversion", 1));
34
35 git_config_free(config);
36 git_repository_free(repo);
37 cl_git_fail(git_repository_open(&repo, "empty_bare.git"));
38 }
39
40 void test_repo_open__standard_empty_repo_through_gitdir(void)
41 {
42 git_repository *repo;
43
44 cl_git_pass(git_repository_open(&repo, cl_fixture("empty_standard_repo/.gitted")));
45
46 cl_assert(git_repository_path(repo) != NULL);
47 cl_assert(git__suffixcmp(git_repository_path(repo), "/") == 0);
48
49 cl_assert(git_repository_workdir(repo) != NULL);
50 cl_assert(git__suffixcmp(git_repository_workdir(repo), "/") == 0);
51
52 git_repository_free(repo);
53 }
54
55 void test_repo_open__standard_empty_repo_through_workdir(void)
56 {
57 git_repository *repo = cl_git_sandbox_init("empty_standard_repo");
58
59 cl_assert(git_repository_path(repo) != NULL);
60 cl_assert(git__suffixcmp(git_repository_path(repo), "/") == 0);
61
62 cl_assert(git_repository_workdir(repo) != NULL);
63 cl_assert(git__suffixcmp(git_repository_workdir(repo), "/") == 0);
64 }
65
66
67 void test_repo_open__open_with_discover(void)
68 {
69 static const char *variants[] = {
70 "attr", "attr/", "attr/.git", "attr/.git/",
71 "attr/sub", "attr/sub/", "attr/sub/sub", "attr/sub/sub/",
72 NULL
73 };
74 git_repository *repo;
75 const char **scan;
76
77 cl_fixture_sandbox("attr");
78 cl_git_pass(p_rename("attr/.gitted", "attr/.git"));
79
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);
85 }
86
87 cl_fixture_cleanup("attr");
88 }
89
90 static void make_gitlink_dir(const char *dir, const char *linktext)
91 {
92 git_buf path = GIT_BUF_INIT;
93
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);
97 git_buf_free(&path);
98 }
99
100 void test_repo_open__gitlinked(void)
101 {
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;
105
106 make_gitlink_dir("alternate", "gitdir: ../empty_standard_repo/.git");
107
108 cl_git_pass(git_repository_open(&repo2, "alternate"));
109
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));
113
114 cl_assert(git_repository_workdir(repo2) != NULL);
115 cl_assert_(git__suffixcmp(git_repository_workdir(repo2), "alternate/") == 0, git_repository_workdir(repo2));
116
117 git_repository_free(repo2);
118 }
119
120 void test_repo_open__from_git_new_workdir(void)
121 {
122 #ifndef GIT_WIN32
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
126 * this way.
127 */
128
129 git_repository *repo2;
130 git_buf link_tgt = GIT_BUF_INIT, link = GIT_BUF_INIT, body = GIT_BUF_INIT;
131 const char **scan;
132 int link_fd;
133 static const char *links[] = {
134 "config", "refs", "logs/refs", "objects", "info", "hooks",
135 "packed-refs", "remotes", "rr-cache", "svn", NULL
136 };
137 static const char *copies[] = {
138 "HEAD", NULL
139 };
140
141 cl_git_sandbox_init("empty_standard_repo");
142
143 cl_git_pass(p_mkdir("alternate", 0777));
144 cl_git_pass(p_mkdir("alternate/.git", 0777));
145
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));
154 }
155 }
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));
161
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));
164 p_close(link_fd);
165 }
166 }
167
168 git_buf_free(&link_tgt);
169 git_buf_free(&link);
170 git_buf_free(&body);
171
172
173 cl_git_pass(git_repository_open(&repo2, "alternate"));
174
175 cl_assert(git_repository_path(repo2) != NULL);
176 cl_assert_(git__suffixcmp(git_repository_path(repo2), "alternate/.git/") == 0, git_repository_path(repo2));
177
178 cl_assert(git_repository_workdir(repo2) != NULL);
179 cl_assert_(git__suffixcmp(git_repository_workdir(repo2), "alternate/") == 0, git_repository_workdir(repo2));
180
181 git_repository_free(repo2);
182 #endif
183 }
184
185 void test_repo_open__failures(void)
186 {
187 git_repository *base, *repo;
188 git_buf ceiling = GIT_BUF_INIT;
189
190 base = cl_git_sandbox_init("attr");
191 cl_git_pass(git_buf_sets(&ceiling, git_repository_workdir(base)));
192
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));
197
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));
202
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));
208
209 git_buf_free(&ceiling);
210 }
211
212 void test_repo_open__bad_gitlinks(void)
213 {
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",
219 NULL
220 };
221 const char **scan;
222
223 cl_git_sandbox_init("attr");
224
225 cl_git_pass(p_mkdir("invalid", 0777));
226 cl_git_pass(git_futils_mkdir_r("invalid2/.git", 0777));
227
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));
231 }
232
233 git_futils_rmdir_r("invalid", NULL, GIT_RMDIR_REMOVE_FILES);
234 git_futils_rmdir_r("invalid2", NULL, GIT_RMDIR_REMOVE_FILES);
235 }
236
237 #ifdef GIT_WIN32
238 static void unposix_path(git_buf *path)
239 {
240 char *src, *tgt;
241
242 src = tgt = path->ptr;
243
244 /* convert "/d/..." to "d:\..." */
245 if (src[0] == '/' && isalpha(src[1]) && src[2] == '/') {
246 *tgt++ = src[1];
247 *tgt++ = ':';
248 *tgt++ = '\\';
249 src += 3;
250 }
251
252 while (*src) {
253 *tgt++ = (*src == '/') ? '\\' : *src;
254 src++;
255 }
256
257 *tgt = '\0';
258 }
259 #endif
260
261 void test_repo_open__win32_path(void)
262 {
263 #ifdef GIT_WIN32
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/";
268
269 cl_assert(git__suffixcmp(git_repository_path(repo), repo_path) == 0);
270 cl_assert(git__suffixcmp(git_repository_workdir(repo), repo_wd) == 0);
271
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);
278
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);
286
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);
293
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);
301
302 git_buf_free(&winpath);
303 #endif
304 }
305
306 void test_repo_open__opening_a_non_existing_repository_returns_ENOTFOUND(void)
307 {
308 git_repository *repo;
309 cl_assert_equal_i(GIT_ENOTFOUND, git_repository_open(&repo, "i-do-not/exist"));
310 }
311
312 void test_repo_open__no_config(void)
313 {
314 git_buf path = GIT_BUF_INIT;
315 git_repository *repo;
316 git_config *config;
317
318 cl_fixture_sandbox("empty_standard_repo");
319 cl_git_pass(cl_rename(
320 "empty_standard_repo/.gitted", "empty_standard_repo/.git"));
321
322 /* remove local config */
323 cl_git_pass(git_futils_rmdir_r(
324 "empty_standard_repo/.git/config", NULL, GIT_RMDIR_REMOVE_FILES));
325
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));
335
336 git_buf_free(&path);
337
338 cl_git_pass(git_repository_open(&repo, "empty_standard_repo"));
339 cl_git_pass(git_repository_config(&config, repo));
340
341 cl_git_pass(git_config_set_string(config, "test.set", "42"));
342
343 git_config_free(config);
344 git_repository_free(repo);
345 cl_fixture_cleanup("empty_standard_repo");
346
347 cl_sandbox_set_search_path_defaults();
348 }
349
350 void test_repo_open__force_bare(void)
351 {
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;
355
356 make_gitlink_dir("alternate", "gitdir: ../empty_standard_repo/.git");
357
358 cl_assert(!git_repository_is_bare(repo));
359
360 cl_git_pass(git_repository_open(&barerepo, "alternate"));
361 cl_assert(!git_repository_is_bare(barerepo));
362 git_repository_free(barerepo);
363
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);
368
369 cl_git_fail(git_repository_open_bare(&barerepo, "alternate/.git"));
370
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);
375
376 cl_git_pass(p_mkdir("empty_standard_repo/subdir", 0777));
377 cl_git_mkfile("empty_standard_repo/subdir/something.txt", "something");
378
379 cl_git_fail(git_repository_open_bare(
380 &barerepo, "empty_standard_repo/subdir"));
381
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);
386
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");
390
391 cl_git_fail(git_repository_open_bare(&barerepo, "alternate/subdir/sub2"));
392
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);
397 }