init.c 25.3 KB
Newer Older
1
#include "clar_libgit2.h"
2
#include "fileops.h"
3
#include "repository.h"
4
#include "config.h"
5
#include "path.h"
6
#include "config/config_helpers.h"
7
#include "repo/repo_helpers.h"
8 9 10 11 12 13

enum repo_mode {
	STANDARD_REPOSITORY = 0,
	BARE_REPOSITORY = 1
};

14
static git_repository *_repo = NULL;
15
static git_buf _global_path = GIT_BUF_INIT;
16
static mode_t g_umask = 0;
17 18 19 20

void test_repo_init__initialize(void)
{
	_repo = NULL;
21 22

	/* load umask if not already loaded */
23 24 25
	if (!g_umask) {
		g_umask = p_umask(022);
		(void)p_umask(g_umask);
26
	}
27 28 29 30 31 32 33 34 35

    git_libgit2_opts(GIT_OPT_GET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL,
		&_global_path);
}

void test_repo_init__cleanup(void)
{
	git_libgit2_opts(GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL,
		_global_path.ptr);
36
	git_buf_dispose(&_global_path);
37

38
	cl_fixture_cleanup("tmp_global_path");
39 40 41 42 43
}

static void cleanup_repository(void *path)
{
	git_repository_free(_repo);
44 45
	_repo = NULL;

46 47 48 49 50 51 52 53 54 55 56
	cl_fixture_cleanup((const char *)path);
}

static void ensure_repository_init(
	const char *working_directory,
	int is_bare,
	const char *expected_path_repository,
	const char *expected_working_directory)
{
	const char *workdir;

57 58
	cl_assert(!git_path_isdir(working_directory));

59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
	cl_git_pass(git_repository_init(&_repo, working_directory, is_bare));

	workdir = git_repository_workdir(_repo);
	if (workdir != NULL || expected_working_directory != NULL) {
		cl_assert(
			git__suffixcmp(workdir, expected_working_directory) == 0
		);
	}

	cl_assert(
		git__suffixcmp(git_repository_path(_repo), expected_path_repository) == 0
	);

	cl_assert(git_repository_is_bare(_repo) == is_bare);

#ifdef GIT_WIN32
	if (!is_bare) {
76 77
		DWORD fattrs = GetFileAttributes(git_repository_path(_repo));
		cl_assert((fattrs & FILE_ATTRIBUTE_HIDDEN) != 0);
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
	}
#endif

	cl_assert(git_repository_is_empty(_repo));
}

void test_repo_init__standard_repo(void)
{
	cl_set_cleanup(&cleanup_repository, "testrepo");
	ensure_repository_init("testrepo/", 0, "testrepo/.git/", "testrepo/");
}

void test_repo_init__standard_repo_noslash(void)
{
	cl_set_cleanup(&cleanup_repository, "testrepo");
	ensure_repository_init("testrepo", 0, "testrepo/.git/", "testrepo/");
}

void test_repo_init__bare_repo(void)
{
	cl_set_cleanup(&cleanup_repository, "testrepo.git");
	ensure_repository_init("testrepo.git/", 1, "testrepo.git/", NULL);
}

void test_repo_init__bare_repo_noslash(void)
{
	cl_set_cleanup(&cleanup_repository, "testrepo.git");
	ensure_repository_init("testrepo.git", 1, "testrepo.git/", NULL);
}

108 109
void test_repo_init__bare_repo_escaping_current_workdir(void)
{
110
	git_buf path_repository = GIT_BUF_INIT;
111
	git_buf path_current_workdir = GIT_BUF_INIT;
112

113
	cl_git_pass(git_path_prettify_dir(&path_current_workdir, ".", NULL));
114

115
	cl_git_pass(git_buf_joinpath(&path_repository, git_buf_cstr(&path_current_workdir), "a/b/c"));
116
	cl_git_pass(git_futils_mkdir_r(git_buf_cstr(&path_repository), GIT_DIR_MODE));
117

118 119
	/* Change the current working directory */
	cl_git_pass(chdir(git_buf_cstr(&path_repository)));
120

121 122 123
	/* Initialize a bare repo with a relative path escaping out of the current working directory */
	cl_git_pass(git_repository_init(&_repo, "../d/e.git", 1));
	cl_git_pass(git__suffixcmp(git_repository_path(_repo), "/a/b/d/e.git/"));
124

125
	git_repository_free(_repo);
126

127 128
	/* Open a bare repo with a relative path escaping out of the current working directory */
	cl_git_pass(git_repository_open(&_repo, "../d/e.git"));
129

130
	cl_git_pass(chdir(git_buf_cstr(&path_current_workdir)));
131

132 133
	git_buf_dispose(&path_current_workdir);
	git_buf_dispose(&path_repository);
134

135 136
	cleanup_repository("a");
}
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171

void test_repo_init__reinit_bare_repo(void)
{
	cl_set_cleanup(&cleanup_repository, "reinit.git");

	/* Initialize the repository */
	cl_git_pass(git_repository_init(&_repo, "reinit.git", 1));
	git_repository_free(_repo);

	/* Reinitialize the repository */
	cl_git_pass(git_repository_init(&_repo, "reinit.git", 1));
}

void test_repo_init__reinit_too_recent_bare_repo(void)
{
	git_config *config;

	/* Initialize the repository */
	cl_git_pass(git_repository_init(&_repo, "reinit.git", 1));
	git_repository_config(&config, _repo);

	/*
	 * Hack the config of the repository to make it look like it has
	 * been created by a recenter version of git/libgit2
	 */
	cl_git_pass(git_config_set_int32(config, "core.repositoryformatversion", 42));

	git_config_free(config);
	git_repository_free(_repo);

	/* Try to reinitialize the repository */
	cl_git_fail(git_repository_init(&_repo, "reinit.git", 1));

	cl_fixture_cleanup("reinit.git");
}
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193

void test_repo_init__additional_templates(void)
{
	git_buf path = GIT_BUF_INIT;

	cl_set_cleanup(&cleanup_repository, "tester");

	ensure_repository_init("tester", 0, "tester/.git/", "tester/");

	cl_git_pass(
		git_buf_joinpath(&path, git_repository_path(_repo), "description"));
	cl_assert(git_path_isfile(git_buf_cstr(&path)));

	cl_git_pass(
		git_buf_joinpath(&path, git_repository_path(_repo), "info/exclude"));
	cl_assert(git_path_isfile(git_buf_cstr(&path)));

	cl_git_pass(
		git_buf_joinpath(&path, git_repository_path(_repo), "hooks"));
	cl_assert(git_path_isdir(git_buf_cstr(&path)));
	/* won't confirm specific contents of hooks dir since it may vary */

194
	git_buf_dispose(&path);
195
}
196

197 198
static void assert_config_entry_on_init_bytype(
	const char *config_key, int expected_value, bool is_bare)
199 200
{
	git_config *config;
201 202 203
	int error, current_value;
	const char *repo_path = is_bare ?
		"config_entry/test.bare.git" : "config_entry/test.non.bare.git";
204

205
	cl_set_cleanup(&cleanup_repository, "config_entry");
206

207
	cl_git_pass(git_repository_init(&_repo, repo_path, is_bare));
208

209 210 211
	cl_git_pass(git_repository_config(&config, _repo));
	error = git_config_get_bool(&current_value, config, config_key);
	git_config_free(config);
212

213
	if (expected_value >= 0) {
214
		cl_assert_equal_i(0, error);
215 216 217 218 219
		cl_assert_equal_i(expected_value, current_value);
	} else {
		cl_assert_equal_i(expected_value, error);
	}
}
220

221 222
static void assert_config_entry_on_init(
	const char *config_key, int expected_value)
223 224 225 226 227 228 229
{
	assert_config_entry_on_init_bytype(config_key, expected_value, true);
	git_repository_free(_repo);

	assert_config_entry_on_init_bytype(config_key, expected_value, false);
}

230 231
void test_repo_init__detect_filemode(void)
{
232
	assert_config_entry_on_init("core.filemode", cl_is_chmod_supported());
233
}
234 235 236

void test_repo_init__detect_ignorecase(void)
{
237 238 239 240 241 242 243 244 245
	struct stat st;
	bool found_without_match;

	cl_git_write2file("testCAPS", "whatever\n", 0, O_CREAT | O_WRONLY, 0666);
	found_without_match = (p_stat("Testcaps", &st) == 0);
	cl_must_pass(p_unlink("testCAPS"));

	assert_config_entry_on_init(
		"core.ignorecase", found_without_match ? true : GIT_ENOTFOUND);
246 247
}

248 249 250 251 252 253 254 255
/*
 * Windows: if the filesystem supports symlinks (because we're running
 * as administrator, or because the user has opted into it for normal
 * users) then we can also opt-in explicitly by settings `core.symlinks`
 * in the global config.  Symlinks remain off by default.
 */

void test_repo_init__symlinks_win32_enabled_by_global_config(void)
256
{
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
#ifndef GIT_WIN32
	cl_skip();
#else
	git_config *config, *repo_config;
	int val;

	if (!filesystem_supports_symlinks("link"))
		cl_skip();

	create_tmp_global_config("tmp_global_config", "core.symlinks", "true");

	/*
	 * Create a new repository (can't use `assert_config_on_init` since we
	 * want to examine configuration levels with more granularity.)
	 */
	cl_git_pass(git_repository_init(&_repo, "config_entry/test.non.bare.git", false));

	/* Ensure that core.symlinks remains set (via the global config). */
	cl_git_pass(git_repository_config(&config, _repo));
	cl_git_pass(git_config_get_bool(&val, config, "core.symlinks"));
	cl_assert_equal_i(1, val);

	/*
	 * Ensure that the repository config does not set core.symlinks.
	 * It should remain inherited.
	 */
	cl_git_pass(git_config_open_level(&repo_config, config, GIT_CONFIG_LEVEL_LOCAL));
	cl_git_fail_with(GIT_ENOTFOUND, git_config_get_bool(&val, repo_config, "core.symlinks"));
	git_config_free(repo_config);

	git_config_free(config);
#endif
}

void test_repo_init__symlinks_win32_off_by_default(void)
{
#ifndef GIT_WIN32
	cl_skip();
#else
	assert_config_entry_on_init("core.symlinks", false);
#endif
}

void test_repo_init__symlinks_posix_detected(void)
{
#ifdef GIT_WIN32
	cl_skip();
#else
305
	assert_config_entry_on_init(
306
	    "core.symlinks", filesystem_supports_symlinks("link") ? GIT_ENOTFOUND : false);
307
#endif
308 309
}

310 311
void test_repo_init__detect_precompose_unicode_required(void)
{
312
#ifdef GIT_USE_ICONV
313 314 315 316 317 318 319 320 321
	char *composed = "ḱṷṓn", *decomposed = "ḱṷṓn";
	struct stat st;
	bool found_with_nfd;

	cl_git_write2file(composed, "whatever\n", 0, O_CREAT | O_WRONLY, 0666);
	found_with_nfd = (p_stat(decomposed, &st) == 0);
	cl_must_pass(p_unlink(composed));

	assert_config_entry_on_init("core.precomposeunicode", found_with_nfd);
322 323 324 325 326
#else
	assert_config_entry_on_init("core.precomposeunicode", GIT_ENOTFOUND);
#endif
}

327 328 329 330 331 332
void test_repo_init__reinit_doesnot_overwrite_ignorecase(void)
{
	git_config *config;
	int current_value;

	/* Init a new repo */
333
	cl_set_cleanup(&cleanup_repository, "not.overwrite.git");
334
	cl_git_pass(git_repository_init(&_repo, "not.overwrite.git", 1));
335 336 337 338 339 340

	/* Change the "core.ignorecase" config value to something unlikely */
	git_repository_config(&config, _repo);
	git_config_set_int32(config, "core.ignorecase", 42);
	git_config_free(config);
	git_repository_free(_repo);
341
	_repo = NULL;
342 343

	/* Reinit the repository */
344
	cl_git_pass(git_repository_init(&_repo, "not.overwrite.git", 1));
345 346 347 348 349
	git_repository_config(&config, _repo);

	/* Ensure the "core.ignorecase" config value hasn't been updated */
	cl_git_pass(git_config_get_int32(&current_value, config, "core.ignorecase"));
	cl_assert_equal_i(42, current_value);
350 351 352

	git_config_free(config);
}
353

354 355
void test_repo_init__reinit_overwrites_filemode(void)
{
356
	int expected = cl_is_chmod_supported(), current_value;
357

358 359 360
	/* Init a new repo */
	cl_set_cleanup(&cleanup_repository, "overwrite.git");
	cl_git_pass(git_repository_init(&_repo, "overwrite.git", 1));
361

362
	/* Change the "core.filemode" config value to something unlikely */
363 364
	cl_repo_set_bool(_repo, "core.filemode", !expected);

365 366
	git_repository_free(_repo);
	_repo = NULL;
367

368 369
	/* Reinit the repository */
	cl_git_pass(git_repository_init(&_repo, "overwrite.git", 1));
370

371
	/* Ensure the "core.filemode" config value has been reset */
372
	current_value = cl_repo_get_bool(_repo, "core.filemode");
373
	cl_assert_equal_i(expected, current_value);
374 375
}

376 377 378
void test_repo_init__sets_logAllRefUpdates_according_to_type_of_repository(void)
{
	assert_config_entry_on_init_bytype("core.logallrefupdates", GIT_ENOTFOUND, true);
379
	git_repository_free(_repo);
380 381
	assert_config_entry_on_init_bytype("core.logallrefupdates", true, false);
}
382

383 384 385 386 387 388 389 390 391 392 393
void test_repo_init__empty_template_path(void)
{
	git_repository_init_options opts = GIT_REPOSITORY_INIT_OPTIONS_INIT;
	opts.template_path = "";

	cl_git_pass(git_futils_mkdir("foo", 0755, 0));
	cl_git_pass(git_repository_init_ext(&_repo, "foo", &opts));

	cleanup_repository("foo");
}

394 395
void test_repo_init__extended_0(void)
{
396
	git_repository_init_options opts = GIT_REPOSITORY_INIT_OPTIONS_INIT;
397 398 399 400 401

	/* without MKDIR this should fail */
	cl_git_fail(git_repository_init_ext(&_repo, "extended", &opts));

	/* make the directory first, then it should succeed */
402
	cl_git_pass(git_futils_mkdir("extended", 0775, 0));
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
	cl_git_pass(git_repository_init_ext(&_repo, "extended", &opts));

	cl_assert(!git__suffixcmp(git_repository_workdir(_repo), "/extended/"));
	cl_assert(!git__suffixcmp(git_repository_path(_repo), "/extended/.git/"));
	cl_assert(!git_repository_is_bare(_repo));
	cl_assert(git_repository_is_empty(_repo));

	cleanup_repository("extended");
}

void test_repo_init__extended_1(void)
{
	git_reference *ref;
	git_remote *remote;
	struct stat st;
418
	git_repository_init_options opts = GIT_REPOSITORY_INIT_OPTIONS_INIT;
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438

	opts.flags = GIT_REPOSITORY_INIT_MKPATH |
		GIT_REPOSITORY_INIT_NO_DOTGIT_DIR;
	opts.mode = GIT_REPOSITORY_INIT_SHARED_GROUP;
	opts.workdir_path = "../c_wd";
	opts.description = "Awesomest test repository evah";
	opts.initial_head = "development";
	opts.origin_url = "https://github.com/libgit2/libgit2.git";

	cl_git_pass(git_repository_init_ext(&_repo, "root/b/c.git", &opts));

	cl_assert(!git__suffixcmp(git_repository_workdir(_repo), "/c_wd/"));
	cl_assert(!git__suffixcmp(git_repository_path(_repo), "/c.git/"));
	cl_assert(git_path_isfile("root/b/c_wd/.git"));
	cl_assert(!git_repository_is_bare(_repo));
	/* repo will not be counted as empty because we set head to "development" */
	cl_assert(!git_repository_is_empty(_repo));

	cl_git_pass(git_path_lstat(git_repository_path(_repo), &st));
	cl_assert(S_ISDIR(st.st_mode));
439
	if (cl_is_chmod_supported())
440 441 442
		cl_assert((S_ISGID & st.st_mode) == S_ISGID);
	else
		cl_assert((S_ISGID & st.st_mode) == 0);
443 444 445

	cl_git_pass(git_reference_lookup(&ref, _repo, "HEAD"));
	cl_assert(git_reference_type(ref) == GIT_REF_SYMBOLIC);
446
	cl_assert_equal_s("refs/heads/development", git_reference_symbolic_target(ref));
447 448
	git_reference_free(ref);

449
	cl_git_pass(git_remote_lookup(&remote, _repo, "origin"));
450 451 452 453 454 455 456 457
	cl_assert_equal_s("origin", git_remote_name(remote));
	cl_assert_equal_s(opts.origin_url, git_remote_url(remote));
	git_remote_free(remote);

	git_repository_free(_repo);
	cl_fixture_cleanup("root");
}

458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
void test_repo_init__relative_gitdir(void)
{
	git_repository_init_options opts = GIT_REPOSITORY_INIT_OPTIONS_INIT;
	git_buf dot_git_content = GIT_BUF_INIT;

	opts.workdir_path = "../c_wd";
	opts.flags =
		GIT_REPOSITORY_INIT_MKPATH |
		GIT_REPOSITORY_INIT_RELATIVE_GITLINK |
		GIT_REPOSITORY_INIT_NO_DOTGIT_DIR;

	/* make the directory first, then it should succeed */
	cl_git_pass(git_repository_init_ext(&_repo, "root/b/my_repository", &opts));

	cl_assert(!git__suffixcmp(git_repository_workdir(_repo), "root/b/c_wd/"));
	cl_assert(!git__suffixcmp(git_repository_path(_repo), "root/b/my_repository/"));
	cl_assert(!git_repository_is_bare(_repo));
	cl_assert(git_repository_is_empty(_repo));

	/* Verify that the gitlink and worktree entries are relative */

	/* Verify worktree */
480
	assert_config_entry_value(_repo, "core.worktree", "../c_wd/");
481 482 483 484 485

	/* Verify gitlink */
	cl_git_pass(git_futils_readbuffer(&dot_git_content, "root/b/c_wd/.git"));
	cl_assert_equal_s("gitdir: ../my_repository/", dot_git_content.ptr);

486
	git_buf_dispose(&dot_git_content);
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
	cleanup_repository("root");
}

void test_repo_init__relative_gitdir_2(void)
{
	git_repository_init_options opts = GIT_REPOSITORY_INIT_OPTIONS_INIT;
	git_buf dot_git_content = GIT_BUF_INIT;
	git_buf full_path = GIT_BUF_INIT;

	cl_git_pass(git_path_prettify(&full_path, ".", NULL));
	cl_git_pass(git_buf_joinpath(&full_path, full_path.ptr, "root/b/c_wd"));

	opts.workdir_path = full_path.ptr;
	opts.flags =
		GIT_REPOSITORY_INIT_MKPATH |
		GIT_REPOSITORY_INIT_RELATIVE_GITLINK |
		GIT_REPOSITORY_INIT_NO_DOTGIT_DIR;

	/* make the directory first, then it should succeed */
	cl_git_pass(git_repository_init_ext(&_repo, "root/b/my_repository", &opts));
507
	git_buf_dispose(&full_path);
508 509 510 511 512 513 514 515 516

	cl_assert(!git__suffixcmp(git_repository_workdir(_repo), "root/b/c_wd/"));
	cl_assert(!git__suffixcmp(git_repository_path(_repo), "root/b/my_repository/"));
	cl_assert(!git_repository_is_bare(_repo));
	cl_assert(git_repository_is_empty(_repo));

	/* Verify that the gitlink and worktree entries are relative */

	/* Verify worktree */
517
	assert_config_entry_value(_repo, "core.worktree", "../c_wd/");
518 519 520 521 522

	/* Verify gitlink */
	cl_git_pass(git_futils_readbuffer(&dot_git_content, "root/b/c_wd/.git"));
	cl_assert_equal_s("gitdir: ../my_repository/", dot_git_content.ptr);

523
	git_buf_dispose(&dot_git_content);
524 525 526
	cleanup_repository("root");
}

Russell Belfer committed
527 528
#define CLEAR_FOR_CORE_FILEMODE(M) ((M) &= ~0177)

529 530 531 532
static void assert_hooks_match(
	const char *template_dir,
	const char *repo_dir,
	const char *hook_path,
533
	bool core_filemode)
534
{
535 536
	git_buf expected = GIT_BUF_INIT;
	git_buf actual = GIT_BUF_INIT;
537 538 539 540 541 542 543 544
	struct stat expected_st, st;

	cl_git_pass(git_buf_joinpath(&expected, template_dir, hook_path));
	cl_git_pass(git_path_lstat(expected.ptr, &expected_st));

	cl_git_pass(git_buf_joinpath(&actual, repo_dir, hook_path));
	cl_git_pass(git_path_lstat(actual.ptr, &st));

545
	cl_assert(expected_st.st_size == st.st_size);
546

Russell Belfer committed
547 548 549 550
	if (GIT_MODE_TYPE(expected_st.st_mode) != GIT_FILEMODE_LINK) {
		mode_t expected_mode =
			GIT_MODE_TYPE(expected_st.st_mode) |
			(GIT_PERMS_FOR_WRITE(expected_st.st_mode) & ~g_umask);
551

Russell Belfer committed
552 553 554 555
		if (!core_filemode) {
			CLEAR_FOR_CORE_FILEMODE(expected_mode);
			CLEAR_FOR_CORE_FILEMODE(st.st_mode);
		}
556

Russell Belfer committed
557 558
		cl_assert_equal_i_fmt(expected_mode, st.st_mode, "%07o");
	}
559

560 561
	git_buf_dispose(&expected);
	git_buf_dispose(&actual);
562 563
}

564 565 566
static void assert_mode_seems_okay(
	const char *base, const char *path,
	git_filemode_t expect_mode, bool expect_setgid, bool core_filemode)
567 568 569 570 571 572
{
	git_buf full = GIT_BUF_INIT;
	struct stat st;

	cl_git_pass(git_buf_joinpath(&full, base, path));
	cl_git_pass(git_path_lstat(full.ptr, &st));
573
	git_buf_dispose(&full);
574

575
	if (!core_filemode) {
Russell Belfer committed
576 577
		CLEAR_FOR_CORE_FILEMODE(expect_mode);
		CLEAR_FOR_CORE_FILEMODE(st.st_mode);
578 579 580
		expect_setgid = false;
	}

581 582
	if (S_ISGID != 0)
		cl_assert_equal_b(expect_setgid, (st.st_mode & S_ISGID) != 0);
583

Russell Belfer committed
584
	cl_assert_equal_b(
585
		GIT_PERMS_IS_EXEC(expect_mode), GIT_PERMS_IS_EXEC(st.st_mode));
586

Russell Belfer committed
587 588
	cl_assert_equal_i_fmt(
		GIT_MODE_TYPE(expect_mode), GIT_MODE_TYPE(st.st_mode), "%07o");
589 590
}

591 592
static const char *template_sandbox(const char *name)
{
593 594
	git_buf hooks_path = GIT_BUF_INIT, link_path = GIT_BUF_INIT,
		dotfile_path = GIT_BUF_INIT;
595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611
	const char *path = cl_fixture(name);

	cl_fixture_sandbox(name);

	/* create a symlink from link.sample to update.sample if the filesystem
	 * supports it.
	 */

	cl_git_pass(git_buf_joinpath(&hooks_path, name, "hooks"));
	cl_git_pass(git_buf_joinpath(&link_path, hooks_path.ptr, "link.sample"));

#ifdef GIT_WIN32
	cl_git_mkfile(link_path.ptr, "#!/bin/sh\necho hello, world\n");
#else
	cl_must_pass(symlink("update.sample", link_path.ptr));
#endif

612 613 614
	/* create a file starting with a dot */
	cl_git_pass(git_buf_joinpath(&dotfile_path, hooks_path.ptr, ".dotfile"));
	cl_git_mkfile(dotfile_path.ptr, "something\n");
615
	git_buf_dispose(&dotfile_path);
616

617 618 619
	git_buf_dispose(&dotfile_path);
	git_buf_dispose(&link_path);
	git_buf_dispose(&hooks_path);
620 621 622 623

	return path;
}

624 625
static void configure_templatedir(const char *template_path)
{
626
	create_tmp_global_config("tmp_global_path", "init.templatedir", template_path);
627 628 629
}

static void validate_templates(git_repository *repo, const char *template_path)
630
{
631 632
	git_buf template_description = GIT_BUF_INIT;
	git_buf repo_description = GIT_BUF_INIT;
633 634
	git_buf expected = GIT_BUF_INIT;
	git_buf actual = GIT_BUF_INIT;
635
	int filemode;
636

637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656
	cl_git_pass(git_buf_joinpath(&template_description, template_path,
		"description"));
	cl_git_pass(git_buf_joinpath(&repo_description, git_repository_path(repo),
		"description"));

	cl_git_pass(git_futils_readbuffer(&expected, template_description.ptr));
	cl_git_pass(git_futils_readbuffer(&actual, repo_description.ptr));

	cl_assert_equal_s(expected.ptr, actual.ptr);

	filemode = cl_repo_get_bool(repo, "core.filemode");

	assert_hooks_match(
		template_path, git_repository_path(repo),
		"hooks/update.sample", filemode);

	assert_hooks_match(
		template_path, git_repository_path(repo),
		"hooks/link.sample", filemode);

657 658 659 660
	assert_hooks_match(
		template_path, git_repository_path(repo),
		"hooks/.dotfile", filemode);

661 662 663 664
	git_buf_dispose(&expected);
	git_buf_dispose(&actual);
	git_buf_dispose(&repo_description);
	git_buf_dispose(&template_description);
665 666 667 668 669 670
}

void test_repo_init__external_templates_specified_in_options(void)
{
	git_repository_init_options opts = GIT_REPOSITORY_INIT_OPTIONS_INIT;

671
	cl_set_cleanup(&cleanup_repository, "templated.git");
672
	template_sandbox("template");
673 674 675

	opts.flags = GIT_REPOSITORY_INIT_MKPATH | GIT_REPOSITORY_INIT_BARE |
		GIT_REPOSITORY_INIT_EXTERNAL_TEMPLATE;
676
	opts.template_path = "template";
677 678 679 680

	cl_git_pass(git_repository_init_ext(&_repo, "templated.git", &opts));

	cl_assert(git_repository_is_bare(_repo));
681

682 683
	cl_assert(!git__suffixcmp(git_repository_path(_repo), "/templated.git/"));

684 685 686
	validate_templates(_repo, "template");
	cl_fixture_cleanup("template");
}
687

688 689 690
void test_repo_init__external_templates_specified_in_config(void)
{
	git_buf template_path = GIT_BUF_INIT;
691

692
	git_repository_init_options opts = GIT_REPOSITORY_INIT_OPTIONS_INIT;
693

694 695
	cl_set_cleanup(&cleanup_repository, "templated.git");
	template_sandbox("template");
696

697 698
	cl_git_pass(git_buf_joinpath(&template_path, clar_sandbox_path(),
		"template"));
699

700 701 702 703 704 705 706 707
	configure_templatedir(template_path.ptr);

	opts.flags = GIT_REPOSITORY_INIT_MKPATH | GIT_REPOSITORY_INIT_BARE |
		GIT_REPOSITORY_INIT_EXTERNAL_TEMPLATE;

	cl_git_pass(git_repository_init_ext(&_repo, "templated.git", &opts));

	validate_templates(_repo, "template");
708
	cl_fixture_cleanup("template");
709

710
	git_buf_dispose(&template_path);
711 712
}

713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736
void test_repo_init__external_templates_with_leading_dot(void)
{
	git_buf template_path = GIT_BUF_INIT;

	git_repository_init_options opts = GIT_REPOSITORY_INIT_OPTIONS_INIT;

	cl_set_cleanup(&cleanup_repository, "templated.git");
	template_sandbox("template");

	cl_must_pass(p_rename("template", ".template_with_leading_dot"));

	cl_git_pass(git_buf_joinpath(&template_path, clar_sandbox_path(),
		".template_with_leading_dot"));

	configure_templatedir(template_path.ptr);

	opts.flags = GIT_REPOSITORY_INIT_MKPATH | GIT_REPOSITORY_INIT_BARE |
		GIT_REPOSITORY_INIT_EXTERNAL_TEMPLATE;

	cl_git_pass(git_repository_init_ext(&_repo, "templated.git", &opts));

	validate_templates(_repo, ".template_with_leading_dot");
	cl_fixture_cleanup(".template_with_leading_dot");

737
	git_buf_dispose(&template_path);
738 739
}

740 741 742
void test_repo_init__extended_with_template_and_shared_mode(void)
{
	git_repository_init_options opts = GIT_REPOSITORY_INIT_OPTIONS_INIT;
743
	int filemode = true;
744
	const char *repo_path = NULL;
745 746

	cl_set_cleanup(&cleanup_repository, "init_shared_from_tpl");
747
	template_sandbox("template");
748 749 750

	opts.flags = GIT_REPOSITORY_INIT_MKPATH |
		GIT_REPOSITORY_INIT_EXTERNAL_TEMPLATE;
751
	opts.template_path = "template";
752 753 754 755 756 757 758
	opts.mode = GIT_REPOSITORY_INIT_SHARED_GROUP;

	cl_git_pass(git_repository_init_ext(&_repo, "init_shared_from_tpl", &opts));

	cl_assert(!git_repository_is_bare(_repo));
	cl_assert(!git__suffixcmp(git_repository_path(_repo), "/init_shared_from_tpl/.git/"));

759
	filemode = cl_repo_get_bool(_repo, "core.filemode");
760

761 762 763 764 765 766 767
	repo_path = git_repository_path(_repo);
	assert_mode_seems_okay(repo_path, "hooks",
		GIT_FILEMODE_TREE | GIT_REPOSITORY_INIT_SHARED_GROUP, true, filemode);
	assert_mode_seems_okay(repo_path, "info",
		GIT_FILEMODE_TREE | GIT_REPOSITORY_INIT_SHARED_GROUP, true, filemode);
	assert_mode_seems_okay(repo_path, "description",
		GIT_FILEMODE_BLOB, false, filemode);
768

769
	validate_templates(_repo, "template");
770 771

	cl_fixture_cleanup("template");
772
}
773 774 775 776 777

void test_repo_init__can_reinit_an_initialized_repository(void)
{
	git_repository *reinit;

778 779
	cl_set_cleanup(&cleanup_repository, "extended");

780
	cl_git_pass(git_futils_mkdir("extended", 0775, 0));
781 782 783 784 785 786 787 788
	cl_git_pass(git_repository_init(&_repo, "extended", false));

	cl_git_pass(git_repository_init(&reinit, "extended", false));

	cl_assert_equal_s(git_repository_path(_repo), git_repository_path(reinit));

	git_repository_free(reinit);
}
789 790 791 792 793 794 795 796 797 798

void test_repo_init__init_with_initial_commit(void)
{
	git_index *index;

	cl_set_cleanup(&cleanup_repository, "committed");

	/* Initialize the repository */
	cl_git_pass(git_repository_init(&_repo, "committed", 0));

799
	/* Index will be automatically created when requested for a new repo */
800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817
	cl_git_pass(git_repository_index(&index, _repo));

	/* Create a file so we can commit it
	 *
	 * If you are writing code outside the test suite, you can create this
	 * file any way that you like, such as:
	 *      FILE *fp = fopen("committed/file.txt", "w");
	 *      fputs("some stuff\n", fp);
	 *      fclose(fp);
	 * We like to use the help functions because they do error detection
	 * in a way that's easily compatible with our test suite.
	 */
	cl_git_mkfile("committed/file.txt", "some stuff\n");

	/* Add file to the index */
	cl_git_pass(git_index_add_bypath(index, "file.txt"));
	cl_git_pass(git_index_write(index));

818 819 820 821 822
	/* Intentionally not using cl_repo_commit_from_index here so this code
	 * can be used as an example of how an initial commit is typically
	 * made to a repository...
	 */

823 824 825 826 827 828 829 830 831 832 833
	/* Make sure we're ready to use git_signature_default :-) */
	{
		git_config *cfg, *local;
		cl_git_pass(git_repository_config(&cfg, _repo));
		cl_git_pass(git_config_open_level(&local, cfg, GIT_CONFIG_LEVEL_LOCAL));
		cl_git_pass(git_config_set_string(local, "user.name", "Test User"));
		cl_git_pass(git_config_set_string(local, "user.email", "t@example.com"));
		git_config_free(local);
		git_config_free(cfg);
	}

834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853
	/* Create a commit with the new contents of the index */
	{
		git_signature *sig;
		git_oid tree_id, commit_id;
		git_tree *tree;

		cl_git_pass(git_signature_default(&sig, _repo));
		cl_git_pass(git_index_write_tree(&tree_id, index));
		cl_git_pass(git_tree_lookup(&tree, _repo, &tree_id));

		cl_git_pass(git_commit_create_v(
			&commit_id, _repo, "HEAD", sig, sig,
			NULL, "First", tree, 0));

		git_tree_free(tree);
		git_signature_free(sig);
	}

	git_index_free(index);
}
854 855 856 857 858 859 860 861

void test_repo_init__at_filesystem_root(void)
{
	git_repository *repo;
	const char *sandbox = clar_sandbox_path();
	git_buf root = GIT_BUF_INIT;
	int root_len;

862
	if (!cl_is_env_set("GITTEST_INVASIVE_FS_STRUCTURE"))
863 864 865 866 867 868 869 870 871 872 873 874 875 876
		cl_skip();

	root_len = git_path_root(sandbox);
	cl_assert(root_len >= 0);

	git_buf_put(&root, sandbox, root_len+1);
	git_buf_joinpath(&root, root.ptr, "libgit2_test_dir");

	cl_assert(!git_path_exists(root.ptr));

	cl_git_pass(git_repository_init(&repo, root.ptr, 0));
	cl_assert(git_path_isdir(root.ptr));
	cl_git_pass(git_futils_rmdir_r(root.ptr, NULL, GIT_RMDIR_REMOVE_FILES));

877
	git_buf_dispose(&root);
878 879
	git_repository_free(repo);
}