Skip to content

Commit 8747ebb

Browse files
Don Goodman-Wilsongitster
authored andcommitted
init: allow setting the default for the initial branch name via the config
We just introduced the command-line option `--initial-branch=<branch-name>` to allow initializing a new repository with a different initial branch than the hard-coded one. To allow users to override the initial branch name more permanently (i.e. without having to specify the name manually for each and every `git init` invocation), let's introduce the `init.defaultBranch` config setting. Helped-by: Johannes Schindelin <[email protected]> Helped-by: Derrick Stolee <[email protected]> Signed-off-by: Don Goodman-Wilson <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 32ba12d commit 8747ebb

File tree

5 files changed

+57
-1
lines changed

5 files changed

+57
-1
lines changed

Documentation/config/init.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
init.templateDir::
22
Specify the directory from which templates will be copied.
33
(See the "TEMPLATE DIRECTORY" section of linkgit:git-init[1].)
4+
5+
init.defaultBranch::
6+
Allows overriding the default branch name when initializing
7+
a new repository.

builtin/init-db.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ static int create_default_files(const char *template_path,
269269
char *ref;
270270

271271
if (!initial_branch)
272-
initial_branch = "master";
272+
initial_branch = git_default_branch_name();
273273

274274
ref = xstrfmt("refs/heads/%s", initial_branch);
275275
if (check_refname_format(ref, 0) < 0)

refs.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,36 @@ void expand_ref_prefix(struct argv_array *prefixes, const char *prefix)
560560
argv_array_pushf(prefixes, *p, len, prefix);
561561
}
562562

563+
char *repo_default_branch_name(struct repository *r)
564+
{
565+
const char *config_key = "init.defaultbranch";
566+
const char *config_display_key = "init.defaultBranch";
567+
char *ret = NULL, *full_ref;
568+
569+
if (repo_config_get_string(r, config_key, &ret) < 0)
570+
die(_("could not retrieve `%s`"), config_display_key);
571+
572+
if (!ret)
573+
ret = xstrdup("master");
574+
575+
full_ref = xstrfmt("refs/heads/%s", ret);
576+
if (check_refname_format(full_ref, 0))
577+
die(_("invalid branch name: %s = %s"), config_display_key, ret);
578+
free(full_ref);
579+
580+
return ret;
581+
}
582+
583+
const char *git_default_branch_name(void)
584+
{
585+
static char *ret;
586+
587+
if (!ret)
588+
ret = repo_default_branch_name(the_repository);
589+
590+
return ret;
591+
}
592+
563593
/*
564594
* *string and *len will only be substituted, and *string returned (for
565595
* later free()ing) if the string passed in is a magic short-hand form

refs.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,15 @@ int repo_dwim_log(struct repository *r, const char *str, int len, struct object_
154154
int dwim_ref(const char *str, int len, struct object_id *oid, char **ref);
155155
int dwim_log(const char *str, int len, struct object_id *oid, char **ref);
156156

157+
/*
158+
* Retrieves the default branch name for newly-initialized repositories.
159+
*
160+
* The return value of `repo_default_branch_name()` is an allocated string. The
161+
* return value of `git_default_branch_name()` is a singleton.
162+
*/
163+
const char *git_default_branch_name(void);
164+
char *repo_default_branch_name(struct repository *r);
165+
157166
/*
158167
* A ref_transaction represents a collection of reference updates that
159168
* should succeed or fail together.

t/t0001-init.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,4 +477,17 @@ test_expect_success '--initial-branch' '
477477
grep hello actual
478478
'
479479

480+
test_expect_success 'overridden default initial branch name (config)' '
481+
test_config_global init.defaultBranch nmb &&
482+
git init initial-branch-config &&
483+
git -C initial-branch-config symbolic-ref HEAD >actual &&
484+
grep nmb actual
485+
'
486+
487+
test_expect_success 'invalid default branch name' '
488+
test_config_global init.defaultBranch "with space" &&
489+
test_must_fail git init initial-branch-invalid 2>err &&
490+
test_i18ngrep "invalid branch name" err
491+
'
492+
480493
test_done

0 commit comments

Comments
 (0)