]> git.proxmox.com Git - libgit2.git/blob - tests/repo/open.c
Merge pull request #1962 from libgit2/rename-tests
[libgit2.git] / tests / repo / open.c
1 #include "clar_libgit2.h"
2 #include "fileops.h"
3 #include <ctype.h>
4
5 void test_repo_open__cleanup(void)
6 {
7 cl_git_sandbox_cleanup();
8
9 if (git_path_isdir("alternate"))
10 git_futils_rmdir_r("alternate", NULL, GIT_RMDIR_REMOVE_FILES);
11 }
12
13 void test_repo_open__bare_empty_repo(void)
14 {
15 git_repository *repo = cl_git_sandbox_init("empty_bare.git");
16
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);
20 }
21
22 void test_repo_open__standard_empty_repo_through_gitdir(void)
23 {
24 git_repository *repo;
25
26 cl_git_pass(git_repository_open(&repo, cl_fixture("empty_standard_repo/.gitted")));
27
28 cl_assert(git_repository_path(repo) != NULL);
29 cl_assert(git__suffixcmp(git_repository_path(repo), "/") == 0);
30
31 cl_assert(git_repository_workdir(repo) != NULL);
32 cl_assert(git__suffixcmp(git_repository_workdir(repo), "/") == 0);
33
34 git_repository_free(repo);
35 }
36
37 void test_repo_open__standard_empty_repo_through_workdir(void)
38 {
39 git_repository *repo = cl_git_sandbox_init("empty_standard_repo");
40
41 cl_assert(git_repository_path(repo) != NULL);
42 cl_assert(git__suffixcmp(git_repository_path(repo), "/") == 0);
43
44 cl_assert(git_repository_workdir(repo) != NULL);
45 cl_assert(git__suffixcmp(git_repository_workdir(repo), "/") == 0);
46 }
47
48
49 void test_repo_open__open_with_discover(void)
50 {
51 static const char *variants[] = {
52 "attr", "attr/", "attr/.git", "attr/.git/",
53 "attr/sub", "attr/sub/", "attr/sub/sub", "attr/sub/sub/",
54 NULL
55 };
56 git_repository *repo;
57 const char **scan;
58
59 cl_fixture_sandbox("attr");
60 cl_git_pass(p_rename("attr/.gitted", "attr/.git"));
61
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);
67 }
68
69 cl_fixture_cleanup("attr");
70 }
71
72 static void make_gitlink_dir(const char *dir, const char *linktext)
73 {
74 git_buf path = GIT_BUF_INIT;
75
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);
79 git_buf_free(&path);
80 }
81
82 void test_repo_open__gitlinked(void)
83 {
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;
87
88 make_gitlink_dir("alternate", "gitdir: ../empty_standard_repo/.git");
89
90 cl_git_pass(git_repository_open(&repo2, "alternate"));
91
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));
95
96 cl_assert(git_repository_workdir(repo2) != NULL);
97 cl_assert_(git__suffixcmp(git_repository_workdir(repo2), "alternate/") == 0, git_repository_workdir(repo2));
98
99 git_repository_free(repo2);
100 }
101
102 void test_repo_open__from_git_new_workdir(void)
103 {
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
107 * this way.
108 */
109 cl_git_sandbox_init("empty_standard_repo");
110
111 #ifndef GIT_WIN32
112 git_repository *repo2;
113 git_buf link_tgt = GIT_BUF_INIT, link = GIT_BUF_INIT, body = GIT_BUF_INIT;
114 const char **scan;
115 int link_fd;
116 static const char *links[] = {
117 "config", "refs", "logs/refs", "objects", "info", "hooks",
118 "packed-refs", "remotes", "rr-cache", "svn", NULL
119 };
120 static const char *copies[] = {
121 "HEAD", NULL
122 };
123
124 cl_git_pass(p_mkdir("alternate", 0777));
125 cl_git_pass(p_mkdir("alternate/.git", 0777));
126
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));
135 }
136 }
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));
142
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));
145 p_close(link_fd);
146 }
147 }
148
149 git_buf_free(&link_tgt);
150 git_buf_free(&link);
151 git_buf_free(&body);
152
153
154 cl_git_pass(git_repository_open(&repo2, "alternate"));
155
156 cl_assert(git_repository_path(repo2) != NULL);
157 cl_assert_(git__suffixcmp(git_repository_path(repo2), "alternate/.git/") == 0, git_repository_path(repo2));
158
159 cl_assert(git_repository_workdir(repo2) != NULL);
160 cl_assert_(git__suffixcmp(git_repository_workdir(repo2), "alternate/") == 0, git_repository_workdir(repo2));
161
162 git_repository_free(repo2);
163 #endif
164 }
165
166 void test_repo_open__failures(void)
167 {
168 git_repository *base, *repo;
169 git_buf ceiling = GIT_BUF_INIT;
170
171 base = cl_git_sandbox_init("attr");
172 cl_git_pass(git_buf_sets(&ceiling, git_repository_workdir(base)));
173
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));
178
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));
182
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));
188
189 git_buf_free(&ceiling);
190 }
191
192 void test_repo_open__bad_gitlinks(void)
193 {
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",
199 NULL
200 };
201 const char **scan;
202
203 cl_git_sandbox_init("attr");
204
205 cl_git_pass(p_mkdir("invalid", 0777));
206 cl_git_pass(git_futils_mkdir_r("invalid2/.git", NULL, 0777));
207
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));
211 }
212
213 git_futils_rmdir_r("invalid", NULL, GIT_RMDIR_REMOVE_FILES);
214 git_futils_rmdir_r("invalid2", NULL, GIT_RMDIR_REMOVE_FILES);
215 }
216
217 #ifdef GIT_WIN32
218 static void unposix_path(git_buf *path)
219 {
220 char *src, *tgt;
221
222 src = tgt = path->ptr;
223
224 /* convert "/d/..." to "d:\..." */
225 if (src[0] == '/' && isalpha(src[1]) && src[2] == '/') {
226 *tgt++ = src[1];
227 *tgt++ = ':';
228 *tgt++ = '\\';
229 src += 3;
230 }
231
232 while (*src) {
233 *tgt++ = (*src == '/') ? '\\' : *src;
234 src++;
235 }
236
237 *tgt = '\0';
238 }
239 #endif
240
241 void test_repo_open__win32_path(void)
242 {
243 #ifdef GIT_WIN32
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/";
248
249 cl_assert(git__suffixcmp(git_repository_path(repo), repo_path) == 0);
250 cl_assert(git__suffixcmp(git_repository_workdir(repo), repo_wd) == 0);
251
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);
258
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);
266
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);
273
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);
281
282 git_buf_free(&winpath);
283 #endif
284 }
285
286 void test_repo_open__opening_a_non_existing_repository_returns_ENOTFOUND(void)
287 {
288 git_repository *repo;
289 cl_assert_equal_i(GIT_ENOTFOUND, git_repository_open(&repo, "i-do-not/exist"));
290 }
291
292 void test_repo_open__no_config(void)
293 {
294 git_buf path = GIT_BUF_INIT;
295 git_repository *repo;
296 git_config *config;
297
298 cl_fixture_sandbox("empty_standard_repo");
299 cl_git_pass(cl_rename("empty_standard_repo/.gitted", "empty_standard_repo/.git"));
300
301 /* remove local config */
302 cl_git_pass(git_futils_rmdir_r(
303 "empty_standard_repo/.git/config", NULL, GIT_RMDIR_REMOVE_FILES));
304
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));
314
315 git_buf_free(&path);
316
317 cl_git_pass(git_repository_open(&repo, "empty_standard_repo"));
318 cl_git_pass(git_repository_config(&config, repo));
319
320 cl_git_pass(git_config_set_string(config, "test.set", "42"));
321
322 git_config_free(config);
323 git_repository_free(repo);
324 cl_fixture_cleanup("empty_standard_repo");
325
326 git_futils_dirs_global_shutdown();
327 }
328
329 void test_repo_open__force_bare(void)
330 {
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;
334
335 make_gitlink_dir("alternate", "gitdir: ../empty_standard_repo/.git");
336
337 cl_assert(!git_repository_is_bare(repo));
338
339 cl_git_pass(git_repository_open(&barerepo, "alternate"));
340 cl_assert(!git_repository_is_bare(barerepo));
341 git_repository_free(barerepo);
342
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);
347
348 cl_git_fail(git_repository_open_bare(&barerepo, "alternate/.git"));
349
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);
354
355 cl_git_pass(p_mkdir("empty_standard_repo/subdir", 0777));
356 cl_git_mkfile("empty_standard_repo/subdir/something.txt", "something");
357
358 cl_git_fail(git_repository_open_bare(
359 &barerepo, "empty_standard_repo/subdir"));
360
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);
365
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");
369
370 cl_git_fail(git_repository_open_bare(&barerepo, "alternate/subdir/sub2"));
371
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);
376 }