Skip to content

Commit 964c1c6

Browse files
authored
Merge pull request libgit2#5176 from pks-t/pks/repo-template-head
repository: do not initialize HEAD if it's provided by templates
2 parents 3424c21 + 9d46f16 commit 964c1c6

File tree

3 files changed

+335
-301
lines changed

3 files changed

+335
-301
lines changed

src/repository.c

Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1360,10 +1360,11 @@ int git_repository_create_head(const char *git_dir, const char *ref_name)
13601360
git_buf ref_path = GIT_BUF_INIT;
13611361
git_filebuf ref = GIT_FILEBUF_INIT;
13621362
const char *fmt;
1363+
int error;
13631364

1364-
if (git_buf_joinpath(&ref_path, git_dir, GIT_HEAD_FILE) < 0 ||
1365-
git_filebuf_open(&ref, ref_path.ptr, 0, GIT_REFS_FILE_MODE) < 0)
1366-
goto fail;
1365+
if ((error = git_buf_joinpath(&ref_path, git_dir, GIT_HEAD_FILE)) < 0 ||
1366+
(error = git_filebuf_open(&ref, ref_path.ptr, 0, GIT_REFS_FILE_MODE)) < 0)
1367+
goto out;
13671368

13681369
if (!ref_name)
13691370
ref_name = GIT_BRANCH_MASTER;
@@ -1373,17 +1374,14 @@ int git_repository_create_head(const char *git_dir, const char *ref_name)
13731374
else
13741375
fmt = "ref: " GIT_REFS_HEADS_DIR "%s\n";
13751376

1376-
if (git_filebuf_printf(&ref, fmt, ref_name) < 0 ||
1377-
git_filebuf_commit(&ref) < 0)
1378-
goto fail;
1379-
1380-
git_buf_dispose(&ref_path);
1381-
return 0;
1377+
if ((error = git_filebuf_printf(&ref, fmt, ref_name)) < 0 ||
1378+
(error = git_filebuf_commit(&ref)) < 0)
1379+
goto out;
13821380

1383-
fail:
1381+
out:
13841382
git_buf_dispose(&ref_path);
13851383
git_filebuf_cleanup(&ref);
1386-
return -1;
1384+
return error;
13871385
}
13881386

13891387
static bool is_chmod_supported(const char *file_path)
@@ -2058,53 +2056,59 @@ int git_repository_init_ext(
20582056
const char *given_repo,
20592057
git_repository_init_options *opts)
20602058
{
2061-
int error;
20622059
git_buf repo_path = GIT_BUF_INIT, wd_path = GIT_BUF_INIT,
2063-
common_path = GIT_BUF_INIT;
2060+
common_path = GIT_BUF_INIT, head_path = GIT_BUF_INIT;
20642061
const char *wd;
2062+
int error;
20652063

20662064
assert(out && given_repo && opts);
20672065

20682066
GIT_ERROR_CHECK_VERSION(opts, GIT_REPOSITORY_INIT_OPTIONS_VERSION, "git_repository_init_options");
20692067

2070-
error = repo_init_directories(&repo_path, &wd_path, given_repo, opts);
2071-
if (error < 0)
2072-
goto cleanup;
2068+
if ((error = repo_init_directories(&repo_path, &wd_path, given_repo, opts)) < 0)
2069+
goto out;
20732070

20742071
wd = (opts->flags & GIT_REPOSITORY_INIT_BARE) ? NULL : git_buf_cstr(&wd_path);
2075-
if (valid_repository_path(&repo_path, &common_path)) {
20762072

2073+
if (valid_repository_path(&repo_path, &common_path)) {
20772074
if ((opts->flags & GIT_REPOSITORY_INIT_NO_REINIT) != 0) {
20782075
git_error_set(GIT_ERROR_REPOSITORY,
20792076
"attempt to reinitialize '%s'", given_repo);
20802077
error = GIT_EEXISTS;
2081-
goto cleanup;
2078+
goto out;
20822079
}
20832080

20842081
opts->flags |= GIT_REPOSITORY_INIT__IS_REINIT;
20852082

2086-
error = repo_init_config(
2087-
repo_path.ptr, wd, opts->flags, opts->mode);
2083+
if ((error = repo_init_config(repo_path.ptr, wd, opts->flags, opts->mode)) < 0)
2084+
goto out;
20882085

20892086
/* TODO: reinitialize the templates */
2087+
} else {
2088+
if ((error = repo_init_structure(repo_path.ptr, wd, opts)) < 0 ||
2089+
(error = repo_init_config(repo_path.ptr, wd, opts->flags, opts->mode)) < 0 ||
2090+
(error = git_buf_joinpath(&head_path, repo_path.ptr, GIT_HEAD_FILE)) < 0)
2091+
goto out;
2092+
2093+
/*
2094+
* Only set the new HEAD if the file does not exist already via
2095+
* a template or if the caller has explicitly supplied an
2096+
* initial HEAD value.
2097+
*/
2098+
if ((!git_path_exists(head_path.ptr) || opts->initial_head) &&
2099+
(error = git_repository_create_head(repo_path.ptr, opts->initial_head)) < 0)
2100+
goto out;
20902101
}
2091-
else {
2092-
if (!(error = repo_init_structure(
2093-
repo_path.ptr, wd, opts)) &&
2094-
!(error = repo_init_config(
2095-
repo_path.ptr, wd, opts->flags, opts->mode)))
2096-
error = git_repository_create_head(
2097-
repo_path.ptr, opts->initial_head);
2098-
}
2099-
if (error < 0)
2100-
goto cleanup;
21012102

2102-
error = git_repository_open(out, repo_path.ptr);
2103+
if ((error = git_repository_open(out, repo_path.ptr)) < 0)
2104+
goto out;
21032105

2104-
if (!error && opts->origin_url)
2105-
error = repo_init_create_origin(*out, opts->origin_url);
2106+
if (opts->origin_url &&
2107+
(error = repo_init_create_origin(*out, opts->origin_url)) < 0)
2108+
goto out;
21062109

2107-
cleanup:
2110+
out:
2111+
git_buf_dispose(&head_path);
21082112
git_buf_dispose(&common_path);
21092113
git_buf_dispose(&repo_path);
21102114
git_buf_dispose(&wd_path);

0 commit comments

Comments
 (0)