Skip to content

Commit a890fcb

Browse files
committed
fixup! Add configuration option to set the default branch name for new repositories.
Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
1 parent c23823f commit a890fcb

File tree

1 file changed

+23
-20
lines changed

1 file changed

+23
-20
lines changed

builtin/init-db.c

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,26 @@ void initialize_repository_version(int hash_algo)
201201
hash_algos[hash_algo].name);
202202
}
203203

204+
static char *get_default_branch_name(void)
205+
{
206+
const char *branch_name;
207+
char *prefixed;
208+
209+
/* get the default branch name from config, or failing that, env */
210+
if (git_config_get_string_const("init.defaultbranchname", &branch_name))
211+
branch_name = getenv("GIT_DEFAULT_BRANCH_NAME");
212+
213+
if (!branch_name)
214+
branch_name = "master";
215+
216+
/* prepend "refs/heads/" to the branch name */
217+
prefixed = xstrfmt("refs/heads/%s", branch_name);
218+
if (check_refname_format(prefixed, 0))
219+
die(_("Invalid value for default branch name %s"), branch_name);
220+
221+
return prefixed;
222+
}
223+
204224
static int create_default_files(const char *template_path,
205225
const char *original_git_dir,
206226
const struct repository_format *fmt)
@@ -265,27 +285,10 @@ static int create_default_files(const char *template_path,
265285
reinit = (!access(path, R_OK)
266286
|| readlink(path, junk, sizeof(junk)-1) != -1);
267287
if (!reinit) {
268-
const char *branch_name;
269-
char *prefixed;
270-
271-
/* get the default branch name from config, or failing that, env */
272-
if (git_config_get_string_const("init.defaultbranchname", &branch_name))
273-
branch_name = getenv("GIT_DEFAULT_BRANCH_NAME");
274-
275-
if (!branch_name) {
276-
branch_name = "master";
277-
}
278-
279-
/* prepend "refs/heads/" to the branch name */
280-
prefixed = xstrfmt("refs/heads/%s", branch_name);
281-
if(check_refname_format(prefixed, 0)) {
282-
die(_("Invalid value for default branch name %s"), branch_name);
283-
}
284-
285-
if (create_symref("HEAD", prefixed, NULL) < 0)
288+
char *default_ref = get_default_branch_name();
289+
if (create_symref("HEAD", default_ref, NULL) < 0)
286290
exit(1);
287-
288-
free(prefixed);
291+
free(default_ref);
289292
}
290293

291294
initialize_repository_version(fmt->hash_algo);

0 commit comments

Comments
 (0)