From: Don Goodman-Wilson <don@xxxxxxxxxxxxxxxxxx> We just introduced the command-line option `--main-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 default main 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 <johannes.schindelin@xxxxxx> Helped-by: Derrick Stolee <dstolee@xxxxxxxxxxxxx> Signed-off-by: Don Goodman-Wilson <don@xxxxxxxxxxxxxxxxxx> --- Documentation/config/init.txt | 4 ++++ builtin/init-db.c | 6 ++++-- refs.c | 7 +++++-- refs.h | 4 ++++ t/t0001-init.sh | 16 ++++++++++++++++ 5 files changed, 33 insertions(+), 4 deletions(-) diff --git a/Documentation/config/init.txt b/Documentation/config/init.txt index 46fa8c6a082..6ae4a38416e 100644 --- a/Documentation/config/init.txt +++ b/Documentation/config/init.txt @@ -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. diff --git a/builtin/init-db.c b/builtin/init-db.c index 287cdafaab1..d09c9dc7845 100644 --- a/builtin/init-db.c +++ b/builtin/init-db.c @@ -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); diff --git a/refs.c b/refs.c index 7da3ac178c4..4b0e5b14062 100644 --- a/refs.c +++ b/refs.c @@ -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; diff --git a/refs.h b/refs.h index 96472f9a9f5..c801d08490c 100644 --- a/refs.h +++ b/refs.h @@ -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); diff --git a/t/t0001-init.sh b/t/t0001-init.sh index 5d8e321a703..fbf02066940 100755 --- a/t/t0001-init.sh +++ b/t/t0001-init.sh @@ -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 -- gitgitgadget