Skip to content

Commit c23823f

Browse files
author
Don Goodman-Wilson
committed
Add configuration option to set the default branch name for new repositories.
No documentation has been added, nor tests written, no any other checks done to see what this could break.
1 parent 2051400 commit c23823f

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

builtin/init-db.c

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -258,15 +258,34 @@ static int create_default_files(const char *template_path,
258258
die("failed to set up refs db: %s", err.buf);
259259

260260
/*
261-
* Create the default symlink from ".git/HEAD" to the "master"
262-
* branch, if it does not exist yet.
261+
* Create the default symlink from ".git/HEAD" to the default
262+
* branch name, if it does not exist yet.
263263
*/
264264
path = git_path_buf(&buf, "HEAD");
265265
reinit = (!access(path, R_OK)
266266
|| readlink(path, junk, sizeof(junk)-1) != -1);
267267
if (!reinit) {
268-
if (create_symref("HEAD", "refs/heads/master", NULL) < 0)
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)
269286
exit(1);
287+
288+
free(prefixed);
270289
}
271290

272291
initialize_repository_version(fmt->hash_algo);

t/t0001-init.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,4 +464,20 @@ test_expect_success MINGW 'redirect std handles' '
464464
grep "Needed a single revision" output.txt
465465
'
466466

467+
test_expect_success 'custom default branch name from config' '
468+
git config --global init.defaultbranchname nmb &&
469+
git init custom-config &&
470+
git config --global --unset init.defaultbranchname &&
471+
grep nmb custom-config/.git/HEAD
472+
'
473+
474+
test_expect_success 'custom default branch name from env' '
475+
GIT_DEFAULT_BRANCH_NAME=nmb git init custom-env &&
476+
grep nmb custom-env/.git/HEAD
477+
'
478+
479+
test_expect_fail 'invalid custom default branch name' '
480+
GIT_DEFAULT_BRANCH_NAME=nmb. git init custom-invalid
481+
'
482+
467483
test_done

0 commit comments

Comments
 (0)