]> git.proxmox.com Git - libgit2.git/blob - tests/repo/open.c
New upstream version 1.4.3+dfsg.1
[libgit2.git] / tests / repo / open.c
1 #include "clar_libgit2.h"
2 #include "futils.h"
3 #include "sysdir.h"
4 #include <ctype.h>
5
6 static int validate_ownership = 0;
7 static git_buf config_path = GIT_BUF_INIT;
8
9 void test_repo_open__initialize(void)
10 {
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));
13 }
14
15 void test_repo_open__cleanup(void)
16 {
17 cl_git_sandbox_cleanup();
18 cl_fixture_cleanup("empty_standard_repo");
19 cl_fixture_cleanup("__global_config");
20
21 if (git_fs_path_isdir("alternate"))
22 git_futils_rmdir_r("alternate", NULL, GIT_RMDIR_REMOVE_FILES);
23
24 git_fs_path__set_owner(GIT_FS_PATH_MOCK_OWNER_NONE);
25
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);
28
29 cl_git_pass(git_libgit2_opts(GIT_OPT_SET_OWNER_VALIDATION, validate_ownership));
30 }
31
32 void test_repo_open__bare_empty_repo(void)
33 {
34 git_repository *repo = cl_git_sandbox_init("empty_bare.git");
35
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);
39 }
40
41 void test_repo_open__format_version_1(void)
42 {
43 git_repository *repo;
44 git_config *config;
45
46 repo = cl_git_sandbox_init("empty_bare.git");
47
48 cl_git_pass(git_repository_open(&repo, "empty_bare.git"));
49 cl_git_pass(git_repository_config(&config, repo));
50
51 cl_git_pass(git_config_set_int32(config, "core.repositoryformatversion", 1));
52
53 git_config_free(config);
54 git_repository_free(repo);
55
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);
60 }
61
62 void test_repo_open__standard_empty_repo_through_gitdir(void)
63 {
64 git_repository *repo;
65
66 cl_git_pass(git_repository_open(&repo, cl_fixture("empty_standard_repo/.gitted")));
67
68 cl_assert(git_repository_path(repo) != NULL);
69 cl_assert(git__suffixcmp(git_repository_path(repo), "/") == 0);
70
71 cl_assert(git_repository_workdir(repo) != NULL);
72 cl_assert(git__suffixcmp(git_repository_workdir(repo), "/") == 0);
73
74 git_repository_free(repo);
75 }
76
77 void test_repo_open__standard_empty_repo_through_workdir(void)
78 {
79 git_repository *repo = cl_git_sandbox_init("empty_standard_repo");
80
81 cl_assert(git_repository_path(repo) != NULL);
82 cl_assert(git__suffixcmp(git_repository_path(repo), "/") == 0);
83
84 cl_assert(git_repository_workdir(repo) != NULL);
85 cl_assert(git__suffixcmp(git_repository_workdir(repo), "/") == 0);
86 }
87
88
89 void test_repo_open__open_with_discover(void)
90 {
91 static const char *variants[] = {
92 "attr", "attr/", "attr/.git", "attr/.git/",
93 "attr/sub", "attr/sub/", "attr/sub/sub", "attr/sub/sub/",
94 NULL
95 };
96 git_repository *repo;
97 const char **scan;
98
99 cl_fixture_sandbox("attr");
100 cl_git_pass(p_rename("attr/.gitted", "attr/.git"));
101
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);
107 }
108
109 cl_fixture_cleanup("attr");
110 }
111
112 void test_repo_open__check_if_repository(void)
113 {
114 cl_git_sandbox_init("empty_standard_repo");
115
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));
119
120 cl_fixture_cleanup("empty_standard_repo");
121 }
122
123 static void make_gitlink_dir(const char *dir, const char *linktext)
124 {
125 git_str path = GIT_STR_INIT;
126
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);
131 }
132
133 void test_repo_open__gitlinked(void)
134 {
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;
138
139 make_gitlink_dir("alternate", "gitdir: ../empty_standard_repo/.git");
140
141 cl_git_pass(git_repository_open(&repo2, "alternate"));
142
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));
146
147 cl_assert(git_repository_workdir(repo2) != NULL);
148 cl_assert_(git__suffixcmp(git_repository_workdir(repo2), "alternate/") == 0, git_repository_workdir(repo2));
149
150 git_repository_free(repo2);
151 }
152
153 void test_repo_open__with_symlinked_config(void)
154 {
155 #ifndef GIT_WIN32
156 git_str path = GIT_STR_INIT;
157 git_repository *repo;
158 git_config *cfg;
159 int32_t value;
160
161 cl_git_sandbox_init("empty_standard_repo");
162
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));
169
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);
174
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);
180 #endif
181 }
182
183 void test_repo_open__from_git_new_workdir(void)
184 {
185 #ifndef GIT_WIN32
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
189 * this way.
190 */
191
192 git_repository *repo2;
193 git_str link_tgt = GIT_STR_INIT, link = GIT_STR_INIT, body = GIT_STR_INIT;
194 const char **scan;
195 int link_fd;
196 static const char *links[] = {
197 "config", "refs", "logs/refs", "objects", "info", "hooks",
198 "packed-refs", "remotes", "rr-cache", "svn", NULL
199 };
200 static const char *copies[] = {
201 "HEAD", NULL
202 };
203
204 cl_git_sandbox_init("empty_standard_repo");
205
206 cl_git_pass(p_mkdir("alternate", 0777));
207 cl_git_pass(p_mkdir("alternate/.git", 0777));
208
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));
217 }
218 }
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));
224
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));
227 p_close(link_fd);
228 }
229 }
230
231 git_str_dispose(&link_tgt);
232 git_str_dispose(&link);
233 git_str_dispose(&body);
234
235
236 cl_git_pass(git_repository_open(&repo2, "alternate"));
237
238 cl_assert(git_repository_path(repo2) != NULL);
239 cl_assert_(git__suffixcmp(git_repository_path(repo2), "alternate/.git/") == 0, git_repository_path(repo2));
240
241 cl_assert(git_repository_workdir(repo2) != NULL);
242 cl_assert_(git__suffixcmp(git_repository_workdir(repo2), "alternate/") == 0, git_repository_workdir(repo2));
243
244 git_repository_free(repo2);
245 #else
246 cl_skip();
247 #endif
248 }
249
250 void test_repo_open__failures(void)
251 {
252 git_repository *base, *repo;
253 git_str ceiling = GIT_STR_INIT;
254
255 base = cl_git_sandbox_init("attr");
256 cl_git_pass(git_str_sets(&ceiling, git_repository_workdir(base)));
257
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));
262
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));
267
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));
273
274 /* fail with no searching and no appending .git */
275 cl_git_fail(git_repository_open_ext(
276 &repo, "attr",
277 GIT_REPOSITORY_OPEN_NO_SEARCH | GIT_REPOSITORY_OPEN_NO_DOTGIT,
278 NULL));
279
280 git_str_dispose(&ceiling);
281 }
282
283 void test_repo_open__bad_gitlinks(void)
284 {
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",
290 NULL
291 };
292 const char **scan;
293
294 cl_git_sandbox_init("attr");
295
296 cl_git_pass(p_mkdir("invalid", 0777));
297 cl_git_pass(git_futils_mkdir_r("invalid2/.git", 0777));
298
299 for (scan = bad_links; *scan != NULL; scan++) {
300 make_gitlink_dir("alternate", *scan);
301 repo = NULL;
302 cl_git_fail(git_repository_open_ext(&repo, "alternate", 0, NULL));
303 cl_assert(repo == NULL);
304 }
305
306 git_futils_rmdir_r("invalid", NULL, GIT_RMDIR_REMOVE_FILES);
307 git_futils_rmdir_r("invalid2", NULL, GIT_RMDIR_REMOVE_FILES);
308 }
309
310 #ifdef GIT_WIN32
311 static void unposix_path(git_str *path)
312 {
313 char *src, *tgt;
314
315 src = tgt = path->ptr;
316
317 /* convert "/d/..." to "d:\..." */
318 if (src[0] == '/' && isalpha(src[1]) && src[2] == '/') {
319 *tgt++ = src[1];
320 *tgt++ = ':';
321 *tgt++ = '\\';
322 src += 3;
323 }
324
325 while (*src) {
326 *tgt++ = (*src == '/') ? '\\' : *src;
327 src++;
328 }
329
330 *tgt = '\0';
331 }
332 #endif
333
334 void test_repo_open__win32_path(void)
335 {
336 #ifdef GIT_WIN32
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/";
341
342 cl_assert(git__suffixcmp(git_repository_path(repo), repo_path) == 0);
343 cl_assert(git__suffixcmp(git_repository_workdir(repo), repo_wd) == 0);
344
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);
351
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);
359
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);
366
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);
374
375 git_str_dispose(&winpath);
376 #endif
377 }
378
379 void test_repo_open__opening_a_non_existing_repository_returns_ENOTFOUND(void)
380 {
381 git_repository *repo;
382 cl_assert_equal_i(GIT_ENOTFOUND, git_repository_open(&repo, "i-do-not/exist"));
383 }
384
385 void test_repo_open__no_config(void)
386 {
387 git_str path = GIT_STR_INIT;
388 git_repository *repo;
389 git_config *config;
390
391 cl_fixture_sandbox("empty_standard_repo");
392 cl_git_pass(cl_rename(
393 "empty_standard_repo/.gitted", "empty_standard_repo/.git"));
394
395 /* remove local config */
396 cl_git_pass(git_futils_rmdir_r(
397 "empty_standard_repo/.git/config", NULL, GIT_RMDIR_REMOVE_FILES));
398
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));
408
409 git_str_dispose(&path);
410
411 cl_git_pass(git_repository_open(&repo, "empty_standard_repo"));
412 cl_git_pass(git_repository_config(&config, repo));
413
414 cl_git_pass(git_config_set_string(config, "test.set", "42"));
415
416 git_config_free(config);
417 git_repository_free(repo);
418 cl_fixture_cleanup("empty_standard_repo");
419
420 cl_sandbox_set_search_path_defaults();
421 }
422
423 void test_repo_open__force_bare(void)
424 {
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;
428
429 make_gitlink_dir("alternate", "gitdir: ../empty_standard_repo/.git");
430
431 cl_assert(!git_repository_is_bare(repo));
432
433 cl_git_pass(git_repository_open(&barerepo, "alternate"));
434 cl_assert(!git_repository_is_bare(barerepo));
435 git_repository_free(barerepo);
436
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);
441
442 cl_git_fail(git_repository_open_bare(&barerepo, "alternate/.git"));
443
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);
448
449 cl_git_pass(p_mkdir("empty_standard_repo/subdir", 0777));
450 cl_git_mkfile("empty_standard_repo/subdir/something.txt", "something");
451
452 cl_git_fail(git_repository_open_bare(
453 &barerepo, "empty_standard_repo/subdir"));
454
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);
459
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");
463
464 cl_git_fail(git_repository_open_bare(&barerepo, "alternate/subdir/sub2"));
465
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);
471 }
472
473 void test_repo_open__validates_dir_ownership(void)
474 {
475 git_repository *repo;
476
477 cl_git_pass(git_libgit2_opts(GIT_OPT_SET_OWNER_VALIDATION, 1));
478
479 cl_fixture_sandbox("empty_standard_repo");
480 cl_git_pass(cl_rename("empty_standard_repo/.gitted", "empty_standard_repo/.git"));
481
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);
486
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"));
490
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"));
494 }
495
496 void test_repo_open__can_allowlist_dirs_with_problematic_ownership(void)
497 {
498 git_repository *repo;
499 git_str config_path = GIT_STR_INIT,
500 config_filename = GIT_STR_INIT,
501 config_data = GIT_STR_INIT;
502
503 cl_git_pass(git_libgit2_opts(GIT_OPT_SET_OWNER_VALIDATION, 1));
504
505 cl_fixture_sandbox("empty_standard_repo");
506 cl_git_pass(cl_rename("empty_standard_repo/.gitted", "empty_standard_repo/.git"));
507
508 git_fs_path__set_owner(GIT_FS_PATH_MOCK_OWNER_OTHER);
509 cl_git_fail(git_repository_open(&repo, "empty_standard_repo"));
510
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);
515
516 git_str_joinpath(&config_filename, config_path.ptr, ".gitconfig");
517
518 git_str_printf(&config_data,
519 "[foo]\n" \
520 "\tbar = Foobar\n" \
521 "\tbaz = Baz!\n" \
522 "[safe]\n" \
523 "\tdirectory = /non/existent/path\n" \
524 "\tdirectory = /\n" \
525 "\tdirectory = c:\\\\temp\n" \
526 "\tdirectory = %s/%s\n" \
527 "\tdirectory = /tmp\n" \
528 "[bar]\n" \
529 "\tfoo = barfoo\n",
530 clar_sandbox_path(), "empty_standard_repo");
531 cl_git_rewritefile(config_filename.ptr, config_data.ptr);
532
533 cl_git_pass(git_repository_open(&repo, "empty_standard_repo"));
534 git_repository_free(repo);
535
536 git_str_dispose(&config_path);
537 git_str_dispose(&config_filename);
538 git_str_dispose(&config_data);
539 }
540
541 void test_repo_open__can_reset_safe_directory_list(void)
542 {
543 git_repository *repo;
544 git_str config_path = GIT_STR_INIT,
545 config_filename = GIT_STR_INIT,
546 config_data = GIT_STR_INIT;
547
548 cl_git_pass(git_libgit2_opts(GIT_OPT_SET_OWNER_VALIDATION, 1));
549
550 cl_fixture_sandbox("empty_standard_repo");
551 cl_git_pass(cl_rename("empty_standard_repo/.gitted", "empty_standard_repo/.git"));
552
553 git_fs_path__set_owner(GIT_FS_PATH_MOCK_OWNER_OTHER);
554 cl_git_fail(git_repository_open(&repo, "empty_standard_repo"));
555
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);
560
561 git_str_joinpath(&config_filename, config_path.ptr, ".gitconfig");
562
563 /* The blank resets our sandbox directory and opening fails */
564
565 git_str_printf(&config_data,
566 "[foo]\n" \
567 "\tbar = Foobar\n" \
568 "\tbaz = Baz!\n" \
569 "[safe]\n" \
570 "\tdirectory = %s/%s\n" \
571 "\tdirectory = \n" \
572 "\tdirectory = /tmp\n" \
573 "[bar]\n" \
574 "\tfoo = barfoo\n",
575 clar_sandbox_path(), "empty_standard_repo");
576 cl_git_rewritefile(config_filename.ptr, config_data.ptr);
577
578 cl_git_fail(git_repository_open(&repo, "empty_standard_repo"));
579
580 /* The blank resets tmp and allows subsequent declarations to succeed */
581
582 git_str_clear(&config_data);
583 git_str_printf(&config_data,
584 "[foo]\n" \
585 "\tbar = Foobar\n" \
586 "\tbaz = Baz!\n" \
587 "[safe]\n" \
588 "\tdirectory = /tmp\n" \
589 "\tdirectory = \n" \
590 "\tdirectory = %s/%s\n" \
591 "[bar]\n" \
592 "\tfoo = barfoo\n",
593 clar_sandbox_path(), "empty_standard_repo");
594 cl_git_rewritefile(config_filename.ptr, config_data.ptr);
595
596 cl_git_pass(git_repository_open(&repo, "empty_standard_repo"));
597 git_repository_free(repo);
598
599 git_str_dispose(&config_path);
600 git_str_dispose(&config_filename);
601 git_str_dispose(&config_data);
602 }