Skip to content

Commit 675704c

Browse files
dschogitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent cc0f13c commit 675704c

File tree

4 files changed

+29
-4
lines changed

4 files changed

+29
-4
lines changed

refs.c

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,19 @@ void expand_ref_prefix(struct strvec *prefixes, const char *prefix)
562562
strvec_pushf(prefixes, *p, len, prefix);
563563
}
564564

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+
565578
char *repo_default_branch_name(struct repository *r, int quiet)
566579
{
567580
const char *config_key = "init.defaultbranch";
@@ -574,8 +587,11 @@ char *repo_default_branch_name(struct repository *r, int quiet)
574587
else if (repo_config_get_string(r, config_key, &ret) < 0)
575588
die(_("could not retrieve `%s`"), config_display_key);
576589

577-
if (!ret)
590+
if (!ret) {
578591
ret = xstrdup("master");
592+
if (!quiet)
593+
advise(_(default_branch_name_advice), ret);
594+
}
579595

580596
full_ref = xstrfmt("refs/heads/%s", ret);
581597
if (check_refname_format(full_ref, 0))

t/t0001-init.sh

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ test_expect_success 'reinit' '
163163
(
164164
mkdir again &&
165165
cd again &&
166-
git init >out1 2>err1 &&
166+
git -c init.defaultBranch=initial init >out1 2>err1 &&
167167
git init >out2 2>err2
168168
) &&
169169
test_i18ngrep "Initialized empty" again/out1 &&
@@ -558,6 +558,13 @@ test_expect_success 'overridden default initial branch name (config)' '
558558
grep nmb actual
559559
'
560560

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+
561568
test_expect_success 'overridden default main branch name (env)' '
562569
test_config_global init.defaultBranch nmb &&
563570
GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=env git init main-branch-env &&

t/t1510-repo-setup.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ setup_repo () {
7979
name=$1 worktreecfg=$2 gitfile=$3 barecfg=$4 &&
8080
sane_unset GIT_DIR GIT_WORK_TREE &&
8181

82-
git init "$name" &&
82+
git -c init.defaultBranch=initial init "$name" &&
8383
maybe_config "$name/.git/config" core.worktree "$worktreecfg" &&
8484
maybe_config "$name/.git/config" core.bare "$barecfg" &&
8585
mkdir -p "$name/sub/sub" &&

t/test-lib-functions.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1202,7 +1202,9 @@ test_create_repo () {
12021202
mkdir -p "$repo"
12031203
(
12041204
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 \
12061208
"--template=$GIT_BUILD_DIR/templates/blt/" >&3 2>&4 ||
12071209
error "cannot run git init -- have you built things yet?"
12081210
mv .git/hooks .git/hooks-disabled

0 commit comments

Comments
 (0)