init: provide useful advice about init.defaultBranch

To give ample warning for users wishing to override Git's the fall-back for an unconfigured `init.defaultBranch` (in case we decide to change it in a future Git version), let's introduce some advice that is shown upon `git init` when that value is not set. Note: two test cases in Git's test suite want to verify that the `stderr` output of `git init` is empty. It is now necessary to suppress the advice, we now do that via the `init.defaultBranch` setting. While not strictly necessary, we also set this to `false` in `test_create_repo()`. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johannes Schindelin committed Dec 11, 2020 at 11:36 UTC 675704c74dd4476f455bfa91e72eb9e163317c10
4 files changed +29 -4
refs.c
+17 -1
@@ -562,6 +562,19 @@ void expand_ref_prefix(struct strvec *prefixes, const char *prefix)
562 strvec_pushf(prefixes, *p, len, prefix);
563 }
564
565 +static const char default_branch_name_advice[] = N_(
566 +"Using '%s' as the name for the initial branch. This default branch name\n"
567 +"is subject to change. To configure the initial branch name to use in all\n"
568 +"of your new repositories, which will suppress this warning, call:\n"
569 +"\n"
570 +"\tgit config --global init.defaultBranch <name>\n"
571 +"\n"
572 +"Names commonly chosen instead of 'master' are 'main', 'trunk' and\n"
573 +"'development'. The just-created branch can be renamed via this command:\n"
574 +"\n"
575 +"\tgit branch -m <name>\n"
576 +);
577 +
578 char *repo_default_branch_name(struct repository *r, int quiet)
579 {
580 const char *config_key = "init.defaultbranch";
@@ -574,8 +587,11 @@ char *repo_default_branch_name(struct repository *r, int quiet)
587 else if (repo_config_get_string(r, config_key, &ret) < 0)
588 die(_("could not retrieve `%s`"), config_display_key);
589
577 - if (!ret)
590 + if (!ret) {
591 ret = xstrdup("master");
592 + if (!quiet)
593 + advise(_(default_branch_name_advice), ret);
594 + }
595
596 full_ref = xstrfmt("refs/heads/%s", ret);
597 if (check_refname_format(full_ref, 0))
t/t0001-init.sh
+8 -1
@@ -163,7 +163,7 @@ test_expect_success 'reinit' '
163 (
164 mkdir again &&
165 cd again &&
166 - git init >out1 2>err1 &&
166 + git -c init.defaultBranch=initial init >out1 2>err1 &&
167 git init >out2 2>err2
168 ) &&
169 test_i18ngrep "Initialized empty" again/out1 &&
@@ -558,6 +558,13 @@ test_expect_success 'overridden default initial branch name (config)' '
558 grep nmb actual
559 '
560
561 +test_expect_success 'advice on unconfigured init.defaultBranch' '
562 + GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME= git -c color.advice=always \
563 + init unconfigured-default-branch-name 2>err &&
564 + test_decode_color <err >decoded &&
565 + test_i18ngrep "<YELLOW>hint: " decoded
566 +'
567 +
568 test_expect_success 'overridden default main branch name (env)' '
569 test_config_global init.defaultBranch nmb &&
570 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=env git init main-branch-env &&
t/t1510-repo-setup.sh
+1 -1
@@ -79,7 +79,7 @@ setup_repo () {
79 name=$1 worktreecfg=$2 gitfile=$3 barecfg=$4 &&
80 sane_unset GIT_DIR GIT_WORK_TREE &&
81
82 - git init "$name" &&
82 + git -c init.defaultBranch=initial init "$name" &&
83 maybe_config "$name/.git/config" core.worktree "$worktreecfg" &&
84 maybe_config "$name/.git/config" core.bare "$barecfg" &&
85 mkdir -p "$name/sub/sub" &&
t/test-lib-functions.sh
+3 -1
@@ -1202,7 +1202,9 @@ test_create_repo () {
1202 mkdir -p "$repo"
1203 (
1204 cd "$repo" || error "Cannot setup test environment"
1205 - "${GIT_TEST_INSTALLED:-$GIT_EXEC_PATH}/git$X" init \
1205 + "${GIT_TEST_INSTALLED:-$GIT_EXEC_PATH}/git$X" -c \
1206 + init.defaultBranch="${GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME-master}" \
1207 + init \
1208 "--template=$GIT_BUILD_DIR/templates/blt/" >&3 2>&4 ||
1209 error "cannot run git init -- have you built things yet?"
1210 mv .git/hooks .git/hooks-disabled