@@ -1,3 +1,7 @@
init.templateDir::
Specify the directory from which templates will be copied.
(See the "TEMPLATE DIRECTORY" section of linkgit:git-init[1].)
+
+init.defaultBranch::
+ Allows overriding the default branch name when initializing
+ a new repository.
@@ -266,10 +266,11 @@ static int create_default_files(const char *template_path,
reinit = (!access(path, R_OK)
|| readlink(path, junk, sizeof(junk)-1) != -1);
if (!reinit) {
- char *ref;
+ char *ref, *fall_back = NULL;
if (!main_branch)
- main_branch = "master";
+ main_branch = fall_back =
+ git_main_branch_name(MAIN_BRANCH_FOR_INIT);
ref = xstrfmt("refs/heads/%s", main_branch);
if (check_refname_format(ref, 0) < 0)
@@ -280,6 +281,7 @@ static int create_default_files(const char *template_path,
free(ref);
git_config_set("core.mainbranch", main_branch);
+ free(fall_back);
} else if (main_branch)
warning(_("re-init: ignoring --main-branch=%s"), main_branch);
@@ -563,8 +563,11 @@ void expand_ref_prefix(struct argv_array *prefixes, const char *prefix)
char *repo_main_branch_name(struct repository *r, int flags)
{
int full_name = flags & MAIN_BRANCH_FULL_NAME;
- const char *config_key = "core.mainbranch";
- const char *config_display_key = "core.mainBranch";
+ int for_init = flags & MAIN_BRANCH_FOR_INIT;
+ const char *config_key = for_init ?
+ "init.defaultbranch" : "core.mainbranch";
+ const char *config_display_key = for_init ?
+ "init.defaultBranch" : "core.mainBranch";
const char *fall_back = "master";
char *name = NULL, *ret;
@@ -158,10 +158,14 @@ int dwim_log(const char *str, int len, struct object_id *oid, char **ref);
* Retrieves the name of the main (or: primary) branch of the given
* repository.
*
+ * To obtain the default for newly-initialized repositories, pass the flag
+ * `MAIN_BRANCH_FOR_INIT`.
+ *
* The result is an allocated string. Unless the flags ask for a short name, it
* will be prefixed with "refs/heads/".
*/
#define MAIN_BRANCH_FULL_NAME (1<<0)
+#define MAIN_BRANCH_FOR_INIT (1<<1)
char *git_main_branch_name(int flags);
char *repo_main_branch_name(struct repository *r, int flags);
@@ -479,4 +479,20 @@ test_expect_success '--main-branch' '
grep hello actual
'
+test_expect_success 'overridden default main branch name (config)' '
+ test_config_global init.defaultBranch nmb &&
+ git init main-branch-config &&
+ git -C main-branch-config symbolic-ref HEAD >actual &&
+ grep nmb actual &&
+ git -C main-branch-config config core.mainBranch >actual &&
+ echo nmb >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'invalid default branch name' '
+ test_config_global init.defaultBranch "with space" &&
+ test_must_fail git init main-branch-invalid 2>err &&
+ test_i18ngrep "invalid branch name" err
+'
+
test_done