]> git.proxmox.com Git - libgit2.git/blob - tests/repo/init.c
Merge branch 'cmn/zlib-update' into cmn/update-zlib
[libgit2.git] / tests / repo / init.c
1 #include "clar_libgit2.h"
2 #include "fileops.h"
3 #include "repository.h"
4 #include "config.h"
5 #include "path.h"
6
7 enum repo_mode {
8 STANDARD_REPOSITORY = 0,
9 BARE_REPOSITORY = 1
10 };
11
12 static git_repository *_repo = NULL;
13 static mode_t g_umask = 0;
14
15 void test_repo_init__initialize(void)
16 {
17 _repo = NULL;
18
19 /* load umask if not already loaded */
20 if (!g_umask) {
21 g_umask = p_umask(022);
22 (void)p_umask(g_umask);
23 }
24 }
25
26 static void cleanup_repository(void *path)
27 {
28 git_repository_free(_repo);
29 _repo = NULL;
30
31 cl_fixture_cleanup((const char *)path);
32 }
33
34 static void ensure_repository_init(
35 const char *working_directory,
36 int is_bare,
37 const char *expected_path_repository,
38 const char *expected_working_directory)
39 {
40 const char *workdir;
41
42 cl_assert(!git_path_isdir(working_directory));
43
44 cl_git_pass(git_repository_init(&_repo, working_directory, is_bare));
45
46 workdir = git_repository_workdir(_repo);
47 if (workdir != NULL || expected_working_directory != NULL) {
48 cl_assert(
49 git__suffixcmp(workdir, expected_working_directory) == 0
50 );
51 }
52
53 cl_assert(
54 git__suffixcmp(git_repository_path(_repo), expected_path_repository) == 0
55 );
56
57 cl_assert(git_repository_is_bare(_repo) == is_bare);
58
59 #ifdef GIT_WIN32
60 if (!is_bare) {
61 DWORD fattrs = GetFileAttributes(git_repository_path(_repo));
62 cl_assert((fattrs & FILE_ATTRIBUTE_HIDDEN) != 0);
63 }
64 #endif
65
66 cl_assert(git_repository_is_empty(_repo));
67 }
68
69 void test_repo_init__standard_repo(void)
70 {
71 cl_set_cleanup(&cleanup_repository, "testrepo");
72 ensure_repository_init("testrepo/", 0, "testrepo/.git/", "testrepo/");
73 }
74
75 void test_repo_init__standard_repo_noslash(void)
76 {
77 cl_set_cleanup(&cleanup_repository, "testrepo");
78 ensure_repository_init("testrepo", 0, "testrepo/.git/", "testrepo/");
79 }
80
81 void test_repo_init__bare_repo(void)
82 {
83 cl_set_cleanup(&cleanup_repository, "testrepo.git");
84 ensure_repository_init("testrepo.git/", 1, "testrepo.git/", NULL);
85 }
86
87 void test_repo_init__bare_repo_noslash(void)
88 {
89 cl_set_cleanup(&cleanup_repository, "testrepo.git");
90 ensure_repository_init("testrepo.git", 1, "testrepo.git/", NULL);
91 }
92
93 void test_repo_init__bare_repo_escaping_current_workdir(void)
94 {
95 git_buf path_repository = GIT_BUF_INIT;
96 git_buf path_current_workdir = GIT_BUF_INIT;
97
98 cl_git_pass(git_path_prettify_dir(&path_current_workdir, ".", NULL));
99
100 cl_git_pass(git_buf_joinpath(&path_repository, git_buf_cstr(&path_current_workdir), "a/b/c"));
101 cl_git_pass(git_futils_mkdir_r(git_buf_cstr(&path_repository), NULL, GIT_DIR_MODE));
102
103 /* Change the current working directory */
104 cl_git_pass(chdir(git_buf_cstr(&path_repository)));
105
106 /* Initialize a bare repo with a relative path escaping out of the current working directory */
107 cl_git_pass(git_repository_init(&_repo, "../d/e.git", 1));
108 cl_git_pass(git__suffixcmp(git_repository_path(_repo), "/a/b/d/e.git/"));
109
110 git_repository_free(_repo);
111
112 /* Open a bare repo with a relative path escaping out of the current working directory */
113 cl_git_pass(git_repository_open(&_repo, "../d/e.git"));
114
115 cl_git_pass(chdir(git_buf_cstr(&path_current_workdir)));
116
117 git_buf_free(&path_current_workdir);
118 git_buf_free(&path_repository);
119
120 cleanup_repository("a");
121 }
122
123 void test_repo_init__reinit_bare_repo(void)
124 {
125 cl_set_cleanup(&cleanup_repository, "reinit.git");
126
127 /* Initialize the repository */
128 cl_git_pass(git_repository_init(&_repo, "reinit.git", 1));
129 git_repository_free(_repo);
130
131 /* Reinitialize the repository */
132 cl_git_pass(git_repository_init(&_repo, "reinit.git", 1));
133 }
134
135 void test_repo_init__reinit_too_recent_bare_repo(void)
136 {
137 git_config *config;
138
139 /* Initialize the repository */
140 cl_git_pass(git_repository_init(&_repo, "reinit.git", 1));
141 git_repository_config(&config, _repo);
142
143 /*
144 * Hack the config of the repository to make it look like it has
145 * been created by a recenter version of git/libgit2
146 */
147 cl_git_pass(git_config_set_int32(config, "core.repositoryformatversion", 42));
148
149 git_config_free(config);
150 git_repository_free(_repo);
151
152 /* Try to reinitialize the repository */
153 cl_git_fail(git_repository_init(&_repo, "reinit.git", 1));
154
155 cl_fixture_cleanup("reinit.git");
156 }
157
158 void test_repo_init__additional_templates(void)
159 {
160 git_buf path = GIT_BUF_INIT;
161
162 cl_set_cleanup(&cleanup_repository, "tester");
163
164 ensure_repository_init("tester", 0, "tester/.git/", "tester/");
165
166 cl_git_pass(
167 git_buf_joinpath(&path, git_repository_path(_repo), "description"));
168 cl_assert(git_path_isfile(git_buf_cstr(&path)));
169
170 cl_git_pass(
171 git_buf_joinpath(&path, git_repository_path(_repo), "info/exclude"));
172 cl_assert(git_path_isfile(git_buf_cstr(&path)));
173
174 cl_git_pass(
175 git_buf_joinpath(&path, git_repository_path(_repo), "hooks"));
176 cl_assert(git_path_isdir(git_buf_cstr(&path)));
177 /* won't confirm specific contents of hooks dir since it may vary */
178
179 git_buf_free(&path);
180 }
181
182 static void assert_config_entry_on_init_bytype(
183 const char *config_key, int expected_value, bool is_bare)
184 {
185 git_config *config;
186 int error, current_value;
187 const char *repo_path = is_bare ?
188 "config_entry/test.bare.git" : "config_entry/test.non.bare.git";
189
190 cl_set_cleanup(&cleanup_repository, "config_entry");
191
192 cl_git_pass(git_repository_init(&_repo, repo_path, is_bare));
193
194 cl_git_pass(git_repository_config(&config, _repo));
195 error = git_config_get_bool(&current_value, config, config_key);
196 git_config_free(config);
197
198 if (expected_value >= 0) {
199 cl_assert_equal_i(0, error);
200 cl_assert_equal_i(expected_value, current_value);
201 } else {
202 cl_assert_equal_i(expected_value, error);
203 }
204 }
205
206 static void assert_config_entry_on_init(
207 const char *config_key, int expected_value)
208 {
209 assert_config_entry_on_init_bytype(config_key, expected_value, true);
210 git_repository_free(_repo);
211
212 assert_config_entry_on_init_bytype(config_key, expected_value, false);
213 }
214
215 void test_repo_init__detect_filemode(void)
216 {
217 assert_config_entry_on_init("core.filemode", cl_is_chmod_supported());
218 }
219
220 void test_repo_init__detect_ignorecase(void)
221 {
222 struct stat st;
223 bool found_without_match;
224
225 cl_git_write2file("testCAPS", "whatever\n", 0, O_CREAT | O_WRONLY, 0666);
226 found_without_match = (p_stat("Testcaps", &st) == 0);
227 cl_must_pass(p_unlink("testCAPS"));
228
229 assert_config_entry_on_init(
230 "core.ignorecase", found_without_match ? true : GIT_ENOTFOUND);
231 }
232
233 void test_repo_init__detect_precompose_unicode_required(void)
234 {
235 #ifdef GIT_USE_ICONV
236 char *composed = "ḱṷṓn", *decomposed = "ḱṷṓn";
237 struct stat st;
238 bool found_with_nfd;
239
240 cl_git_write2file(composed, "whatever\n", 0, O_CREAT | O_WRONLY, 0666);
241 found_with_nfd = (p_stat(decomposed, &st) == 0);
242 cl_must_pass(p_unlink(composed));
243
244 assert_config_entry_on_init("core.precomposeunicode", found_with_nfd);
245 #else
246 assert_config_entry_on_init("core.precomposeunicode", GIT_ENOTFOUND);
247 #endif
248 }
249
250 void test_repo_init__reinit_doesnot_overwrite_ignorecase(void)
251 {
252 git_config *config;
253 int current_value;
254
255 /* Init a new repo */
256 cl_set_cleanup(&cleanup_repository, "not.overwrite.git");
257 cl_git_pass(git_repository_init(&_repo, "not.overwrite.git", 1));
258
259 /* Change the "core.ignorecase" config value to something unlikely */
260 git_repository_config(&config, _repo);
261 git_config_set_int32(config, "core.ignorecase", 42);
262 git_config_free(config);
263 git_repository_free(_repo);
264 _repo = NULL;
265
266 /* Reinit the repository */
267 cl_git_pass(git_repository_init(&_repo, "not.overwrite.git", 1));
268 git_repository_config(&config, _repo);
269
270 /* Ensure the "core.ignorecase" config value hasn't been updated */
271 cl_git_pass(git_config_get_int32(&current_value, config, "core.ignorecase"));
272 cl_assert_equal_i(42, current_value);
273
274 git_config_free(config);
275 }
276
277 void test_repo_init__reinit_overwrites_filemode(void)
278 {
279 int expected = cl_is_chmod_supported(), current_value;
280
281 /* Init a new repo */
282 cl_set_cleanup(&cleanup_repository, "overwrite.git");
283 cl_git_pass(git_repository_init(&_repo, "overwrite.git", 1));
284
285 /* Change the "core.filemode" config value to something unlikely */
286 cl_repo_set_bool(_repo, "core.filemode", !expected);
287
288 git_repository_free(_repo);
289 _repo = NULL;
290
291 /* Reinit the repository */
292 cl_git_pass(git_repository_init(&_repo, "overwrite.git", 1));
293
294 /* Ensure the "core.filemode" config value has been reset */
295 current_value = cl_repo_get_bool(_repo, "core.filemode");
296 cl_assert_equal_i(expected, current_value);
297 }
298
299 void test_repo_init__sets_logAllRefUpdates_according_to_type_of_repository(void)
300 {
301 assert_config_entry_on_init_bytype("core.logallrefupdates", GIT_ENOTFOUND, true);
302 git_repository_free(_repo);
303 assert_config_entry_on_init_bytype("core.logallrefupdates", true, false);
304 }
305
306 void test_repo_init__extended_0(void)
307 {
308 git_repository_init_options opts = GIT_REPOSITORY_INIT_OPTIONS_INIT;
309
310 /* without MKDIR this should fail */
311 cl_git_fail(git_repository_init_ext(&_repo, "extended", &opts));
312
313 /* make the directory first, then it should succeed */
314 cl_git_pass(git_futils_mkdir("extended", NULL, 0775, 0));
315 cl_git_pass(git_repository_init_ext(&_repo, "extended", &opts));
316
317 cl_assert(!git__suffixcmp(git_repository_workdir(_repo), "/extended/"));
318 cl_assert(!git__suffixcmp(git_repository_path(_repo), "/extended/.git/"));
319 cl_assert(!git_repository_is_bare(_repo));
320 cl_assert(git_repository_is_empty(_repo));
321
322 cleanup_repository("extended");
323 }
324
325 void test_repo_init__extended_1(void)
326 {
327 git_reference *ref;
328 git_remote *remote;
329 struct stat st;
330 git_repository_init_options opts = GIT_REPOSITORY_INIT_OPTIONS_INIT;
331
332 opts.flags = GIT_REPOSITORY_INIT_MKPATH |
333 GIT_REPOSITORY_INIT_NO_DOTGIT_DIR;
334 opts.mode = GIT_REPOSITORY_INIT_SHARED_GROUP;
335 opts.workdir_path = "../c_wd";
336 opts.description = "Awesomest test repository evah";
337 opts.initial_head = "development";
338 opts.origin_url = "https://github.com/libgit2/libgit2.git";
339
340 cl_git_pass(git_repository_init_ext(&_repo, "root/b/c.git", &opts));
341
342 cl_assert(!git__suffixcmp(git_repository_workdir(_repo), "/c_wd/"));
343 cl_assert(!git__suffixcmp(git_repository_path(_repo), "/c.git/"));
344 cl_assert(git_path_isfile("root/b/c_wd/.git"));
345 cl_assert(!git_repository_is_bare(_repo));
346 /* repo will not be counted as empty because we set head to "development" */
347 cl_assert(!git_repository_is_empty(_repo));
348
349 cl_git_pass(git_path_lstat(git_repository_path(_repo), &st));
350 cl_assert(S_ISDIR(st.st_mode));
351 if (cl_is_chmod_supported())
352 cl_assert((S_ISGID & st.st_mode) == S_ISGID);
353 else
354 cl_assert((S_ISGID & st.st_mode) == 0);
355
356 cl_git_pass(git_reference_lookup(&ref, _repo, "HEAD"));
357 cl_assert(git_reference_type(ref) == GIT_REF_SYMBOLIC);
358 cl_assert_equal_s("refs/heads/development", git_reference_symbolic_target(ref));
359 git_reference_free(ref);
360
361 cl_git_pass(git_remote_load(&remote, _repo, "origin"));
362 cl_assert_equal_s("origin", git_remote_name(remote));
363 cl_assert_equal_s(opts.origin_url, git_remote_url(remote));
364 git_remote_free(remote);
365
366 git_repository_free(_repo);
367 cl_fixture_cleanup("root");
368 }
369
370 #define CLEAR_FOR_CORE_FILEMODE(M) ((M) &= ~0177)
371
372 static void assert_hooks_match(
373 const char *template_dir,
374 const char *repo_dir,
375 const char *hook_path,
376 bool core_filemode)
377 {
378 git_buf expected = GIT_BUF_INIT;
379 git_buf actual = GIT_BUF_INIT;
380 struct stat expected_st, st;
381
382 cl_git_pass(git_buf_joinpath(&expected, template_dir, hook_path));
383 cl_git_pass(git_path_lstat(expected.ptr, &expected_st));
384
385 cl_git_pass(git_buf_joinpath(&actual, repo_dir, hook_path));
386 cl_git_pass(git_path_lstat(actual.ptr, &st));
387
388 cl_assert(expected_st.st_size == st.st_size);
389
390 if (GIT_MODE_TYPE(expected_st.st_mode) != GIT_FILEMODE_LINK) {
391 mode_t expected_mode =
392 GIT_MODE_TYPE(expected_st.st_mode) |
393 (GIT_PERMS_FOR_WRITE(expected_st.st_mode) & ~g_umask);
394
395 if (!core_filemode) {
396 CLEAR_FOR_CORE_FILEMODE(expected_mode);
397 CLEAR_FOR_CORE_FILEMODE(st.st_mode);
398 }
399
400 cl_assert_equal_i_fmt(expected_mode, st.st_mode, "%07o");
401 }
402
403 git_buf_free(&expected);
404 git_buf_free(&actual);
405 }
406
407 static void assert_mode_seems_okay(
408 const char *base, const char *path,
409 git_filemode_t expect_mode, bool expect_setgid, bool core_filemode)
410 {
411 git_buf full = GIT_BUF_INIT;
412 struct stat st;
413
414 cl_git_pass(git_buf_joinpath(&full, base, path));
415 cl_git_pass(git_path_lstat(full.ptr, &st));
416 git_buf_free(&full);
417
418 if (!core_filemode) {
419 CLEAR_FOR_CORE_FILEMODE(expect_mode);
420 CLEAR_FOR_CORE_FILEMODE(st.st_mode);
421 expect_setgid = false;
422 }
423
424 if (S_ISGID != 0)
425 cl_assert_equal_b(expect_setgid, (st.st_mode & S_ISGID) != 0);
426
427 cl_assert_equal_b(
428 GIT_PERMS_IS_EXEC(expect_mode), GIT_PERMS_IS_EXEC(st.st_mode));
429
430 cl_assert_equal_i_fmt(
431 GIT_MODE_TYPE(expect_mode), GIT_MODE_TYPE(st.st_mode), "%07o");
432 }
433
434 void test_repo_init__extended_with_template(void)
435 {
436 git_buf expected = GIT_BUF_INIT;
437 git_buf actual = GIT_BUF_INIT;
438 git_repository_init_options opts = GIT_REPOSITORY_INIT_OPTIONS_INIT;
439 int filemode;
440
441 cl_set_cleanup(&cleanup_repository, "templated.git");
442
443 opts.flags = GIT_REPOSITORY_INIT_MKPATH | GIT_REPOSITORY_INIT_BARE |
444 GIT_REPOSITORY_INIT_EXTERNAL_TEMPLATE;
445 opts.template_path = cl_fixture("template");
446
447 cl_git_pass(git_repository_init_ext(&_repo, "templated.git", &opts));
448
449 cl_assert(git_repository_is_bare(_repo));
450
451 cl_assert(!git__suffixcmp(git_repository_path(_repo), "/templated.git/"));
452
453 cl_git_pass(git_futils_readbuffer(
454 &expected, cl_fixture("template/description")));
455 cl_git_pass(git_futils_readbuffer(
456 &actual, "templated.git/description"));
457
458 cl_assert_equal_s(expected.ptr, actual.ptr);
459
460 git_buf_free(&expected);
461 git_buf_free(&actual);
462
463 filemode = cl_repo_get_bool(_repo, "core.filemode");
464
465 assert_hooks_match(
466 cl_fixture("template"), git_repository_path(_repo),
467 "hooks/update.sample", filemode);
468
469 assert_hooks_match(
470 cl_fixture("template"), git_repository_path(_repo),
471 "hooks/link.sample", filemode);
472 }
473
474 void test_repo_init__extended_with_template_and_shared_mode(void)
475 {
476 git_buf expected = GIT_BUF_INIT;
477 git_buf actual = GIT_BUF_INIT;
478 git_repository_init_options opts = GIT_REPOSITORY_INIT_OPTIONS_INIT;
479 int filemode = true;
480 const char *repo_path = NULL;
481
482 cl_set_cleanup(&cleanup_repository, "init_shared_from_tpl");
483
484 opts.flags = GIT_REPOSITORY_INIT_MKPATH |
485 GIT_REPOSITORY_INIT_EXTERNAL_TEMPLATE;
486 opts.template_path = cl_fixture("template");
487 opts.mode = GIT_REPOSITORY_INIT_SHARED_GROUP;
488
489 cl_git_pass(git_repository_init_ext(&_repo, "init_shared_from_tpl", &opts));
490
491 cl_assert(!git_repository_is_bare(_repo));
492 cl_assert(!git__suffixcmp(git_repository_path(_repo), "/init_shared_from_tpl/.git/"));
493
494 filemode = cl_repo_get_bool(_repo, "core.filemode");
495
496 cl_git_pass(git_futils_readbuffer(
497 &expected, cl_fixture("template/description")));
498 cl_git_pass(git_futils_readbuffer(
499 &actual, "init_shared_from_tpl/.git/description"));
500
501 cl_assert_equal_s(expected.ptr, actual.ptr);
502
503 git_buf_free(&expected);
504 git_buf_free(&actual);
505
506 repo_path = git_repository_path(_repo);
507 assert_mode_seems_okay(repo_path, "hooks",
508 GIT_FILEMODE_TREE | GIT_REPOSITORY_INIT_SHARED_GROUP, true, filemode);
509 assert_mode_seems_okay(repo_path, "info",
510 GIT_FILEMODE_TREE | GIT_REPOSITORY_INIT_SHARED_GROUP, true, filemode);
511 assert_mode_seems_okay(repo_path, "description",
512 GIT_FILEMODE_BLOB, false, filemode);
513
514 /* for a non-symlinked hook, it should have shared permissions now */
515 assert_hooks_match(
516 cl_fixture("template"), git_repository_path(_repo),
517 "hooks/update.sample", filemode);
518
519 /* for a symlinked hook, the permissions still should match the
520 * source link, not the GIT_REPOSITORY_INIT_SHARED_GROUP value
521 */
522 assert_hooks_match(
523 cl_fixture("template"), git_repository_path(_repo),
524 "hooks/link.sample", filemode);
525 }
526
527 void test_repo_init__can_reinit_an_initialized_repository(void)
528 {
529 git_repository *reinit;
530
531 cl_set_cleanup(&cleanup_repository, "extended");
532
533 cl_git_pass(git_futils_mkdir("extended", NULL, 0775, 0));
534 cl_git_pass(git_repository_init(&_repo, "extended", false));
535
536 cl_git_pass(git_repository_init(&reinit, "extended", false));
537
538 cl_assert_equal_s(git_repository_path(_repo), git_repository_path(reinit));
539
540 git_repository_free(reinit);
541 }
542
543 void test_repo_init__init_with_initial_commit(void)
544 {
545 git_index *index;
546
547 cl_set_cleanup(&cleanup_repository, "committed");
548
549 /* Initialize the repository */
550 cl_git_pass(git_repository_init(&_repo, "committed", 0));
551
552 /* Init will be automatically created when requested for a new repo */
553 cl_git_pass(git_repository_index(&index, _repo));
554
555 /* Create a file so we can commit it
556 *
557 * If you are writing code outside the test suite, you can create this
558 * file any way that you like, such as:
559 * FILE *fp = fopen("committed/file.txt", "w");
560 * fputs("some stuff\n", fp);
561 * fclose(fp);
562 * We like to use the help functions because they do error detection
563 * in a way that's easily compatible with our test suite.
564 */
565 cl_git_mkfile("committed/file.txt", "some stuff\n");
566
567 /* Add file to the index */
568 cl_git_pass(git_index_add_bypath(index, "file.txt"));
569 cl_git_pass(git_index_write(index));
570
571 /* Intentionally not using cl_repo_commit_from_index here so this code
572 * can be used as an example of how an initial commit is typically
573 * made to a repository...
574 */
575
576 /* Make sure we're ready to use git_signature_default :-) */
577 {
578 git_config *cfg, *local;
579 cl_git_pass(git_repository_config(&cfg, _repo));
580 cl_git_pass(git_config_open_level(&local, cfg, GIT_CONFIG_LEVEL_LOCAL));
581 cl_git_pass(git_config_set_string(local, "user.name", "Test User"));
582 cl_git_pass(git_config_set_string(local, "user.email", "t@example.com"));
583 git_config_free(local);
584 git_config_free(cfg);
585 }
586
587 /* Create a commit with the new contents of the index */
588 {
589 git_signature *sig;
590 git_oid tree_id, commit_id;
591 git_tree *tree;
592
593 cl_git_pass(git_signature_default(&sig, _repo));
594 cl_git_pass(git_index_write_tree(&tree_id, index));
595 cl_git_pass(git_tree_lookup(&tree, _repo, &tree_id));
596
597 cl_git_pass(git_commit_create_v(
598 &commit_id, _repo, "HEAD", sig, sig,
599 NULL, "First", tree, 0));
600
601 git_tree_free(tree);
602 git_signature_free(sig);
603 }
604
605 git_index_free(index);
606 }