Skip to content

Commit 4550297

Browse files
committed
breaking-changes: switch default branch to main
Since 1296cbe (init: document `init.defaultBranch` better, 2020-12-11) "git-init.adoc" has advertized that the default name of the initial branch may change in the future. The name "main" is chosen to match the default used by the big git forge web sites. The advice printed when init.defaultBranch is not set is updated to say that the default will change to "main" in Git 3.0. When git is built with breaking changes enabled the default branch name is "main" and the advice is not shown. In addition support for GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME is removed and the remote code will try "main" before "master" when guessing the default remote branch name. With the expection of the "git-init.adoc" the documentation is left unchanged. I had hoped to parameterize the name of the default branch by using an asciidoc attribute. Unfortunately attribute expansion is inhibited by backticks and we use backticks to mark up ref names so that idea does not work. As the changes to git-init.adoc show inserting ifdef's around each instance of the branch name "master" is cumbersome and makes the documentation sources harder to read. Apart from "git-init.adoc" there are some other files where "master" is used as the name of the initial branch rather than as an example of a branch name such as "user-manual.adoc" and "gitcore-tutorial.adoc". The name appears a lot in those so updating it with ifdef's is not really practical. We can update that document in the 3.0 release cycle. The other documentation where master is used as an example branch name can be gradually converted over time. Signed-off-by: Phillip Wood <[email protected]>
1 parent 595a059 commit 4550297

File tree

9 files changed

+54
-15
lines changed

9 files changed

+54
-15
lines changed

Documentation/BreakingChanges.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,11 @@ A prerequisite for this change is that the ecosystem is ready to support the
165165
"reftable" format. Most importantly, alternative implementations of Git like
166166
JGit, libgit2 and Gitoxide need to support it.
167167

168+
* The default branch name will be `main`. We have been warning that the default
169+
name will change since 675704c74dd (init: provide useful advice about
170+
init.defaultBranch, 2020-12-11). The new name matches the default branch name
171+
used by many of the big git forges.
172+
168173
=== Removals
169174

170175
* Support for grafting commits has long been superseded by git-replace(1).

Documentation/git-init.adoc

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,15 @@ If this is a reinitialization, the repository will be moved to the specified pat
7777
`-b <branch-name>`::
7878
`--initial-branch=<branch-name>`::
7979
Use _<branch-name>_ for the initial branch in the newly created
80-
repository. If not specified, fall back to the default name (currently
81-
`master`, but this is subject to change in the future; the name can be
82-
customized via the `init.defaultBranch` configuration variable).
80+
repository. If not specified, fall back to the default name
81+
ifndef::with-breaking-changes[]
82+
(currently `master`, but this will change to `main` when Git 3.0 is released).
83+
endif::with-breaking-changes[]
84+
ifdef::with-breaking-changes[]
85+
(`main`).
86+
endif::with-breaking-changes[]
87+
The default name can be customized via the `init.defaultBranch` configuration
88+
variable.
8389

8490
`--shared[=(false|true|umask|group|all|world|everybody|<perm>)]`::
8591

advice.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ static struct {
5151
[ADVICE_AM_WORK_DIR] = { "amWorkDir" },
5252
[ADVICE_CHECKOUT_AMBIGUOUS_REMOTE_BRANCH_NAME] = { "checkoutAmbiguousRemoteBranchName" },
5353
[ADVICE_COMMIT_BEFORE_MERGE] = { "commitBeforeMerge" },
54+
#ifndef WITH_BREAKING_CHANGES
5455
[ADVICE_DEFAULT_BRANCH_NAME] = { "defaultBranchName" },
56+
#endif /* WITH_BREAKING_CHANGES */
5557
[ADVICE_DETACHED_HEAD] = { "detachedHead" },
5658
[ADVICE_DIVERGING] = { "diverging" },
5759
[ADVICE_FETCH_SET_HEAD_WARN] = { "fetchRemoteHEADWarn" },

advice.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ enum advice_type {
1818
ADVICE_AM_WORK_DIR,
1919
ADVICE_CHECKOUT_AMBIGUOUS_REMOTE_BRANCH_NAME,
2020
ADVICE_COMMIT_BEFORE_MERGE,
21+
#ifndef WITH_BREAKING_CHANGES
2122
ADVICE_DEFAULT_BRANCH_NAME,
23+
#endif /* WITH_BREAKING_CHANGES */
2224
ADVICE_DETACHED_HEAD,
2325
ADVICE_DIVERGING,
2426
ADVICE_FETCH_SET_HEAD_WARN,

ci/run-build-and-tests.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ run_tests=t
99

1010
case "$jobname" in
1111
linux-breaking-changes)
12-
export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
1312
export WITH_BREAKING_CHANGES=YesPlease
1413
;;
1514
linux-TEST-vars)

refs.c

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -627,10 +627,12 @@ void expand_ref_prefix(struct strvec *prefixes, const char *prefix)
627627
strvec_pushf(prefixes, *p, len, prefix);
628628
}
629629

630+
#ifndef WITH_BREAKING_CHANGES
630631
static const char default_branch_name_advice[] = N_(
631632
"Using '%s' as the name for the initial branch. This default branch name\n"
632-
"is subject to change. To configure the initial branch name to use in all\n"
633-
"of your new repositories, which will suppress this warning, call:\n"
633+
"will change to \"main\" in Git 3.0. To configure the initial branch name\n"
634+
"to use in all of your new repositories, which will suppress this warning,\n"
635+
"call:\n"
634636
"\n"
635637
"\tgit config --global init.defaultBranch <name>\n"
636638
"\n"
@@ -639,24 +641,32 @@ static const char default_branch_name_advice[] = N_(
639641
"\n"
640642
"\tgit branch -m <name>\n"
641643
);
644+
#endif /* WITH_BREAKING_CHANGES */
642645

643646
char *repo_default_branch_name(struct repository *r, int quiet)
644647
{
645648
const char *config_key = "init.defaultbranch";
646649
const char *config_display_key = "init.defaultBranch";
647650
char *ret = NULL, *full_ref;
651+
#ifndef WITH_BREAKING_CHANGES
648652
const char *env = getenv("GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME");
649653

650654
if (env && *env)
651655
ret = xstrdup(env);
652-
else if (repo_config_get_string(r, config_key, &ret) < 0)
656+
#endif /* WITH_BREAKING_CHANGES */
657+
if (!ret && repo_config_get_string(r, config_key, &ret) < 0)
653658
die(_("could not retrieve `%s`"), config_display_key);
654659

655660
if (!ret) {
661+
#ifdef WITH_BREAKING_CHANGES
662+
ret = xstrdup("main");
663+
(void) quiet; /* Silence -Wunused-parameter */
664+
#else
656665
ret = xstrdup("master");
657666
if (!quiet)
658667
advise_if_enabled(ADVICE_DEFAULT_BRANCH_NAME,
659668
_(default_branch_name_advice), ret);
669+
#endif /* WITH_BREAKING_CHANGES */
660670
}
661671

662672
full_ref = xstrfmt("refs/heads/%s", ret);

remote.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2378,6 +2378,13 @@ struct ref *guess_remote_head(const struct ref *head,
23782378
if (r && oideq(&r->old_oid, &head->old_oid))
23792379
return copy_ref(r);
23802380

2381+
#ifdef WITH_BREAKING_CHANGES
2382+
/* Fall back to the hard-coded default */
2383+
r = find_ref_by_name(refs, "refs/heads/main");
2384+
if (r && oideq(&r->old_oid, &head->old_oid))
2385+
return copy_ref(r);
2386+
#endif /* WITH_BREAKING_CHANGES */
2387+
23812388
/* Fall back to the hard-coded historical default */
23822389
r = find_ref_by_name(refs, "refs/heads/master");
23832390
if (r && oideq(&r->old_oid, &head->old_oid))

t/t0001-init.sh

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -868,7 +868,7 @@ test_expect_success 'overridden default initial branch name (config)' '
868868
grep nmb actual
869869
'
870870

871-
test_expect_success 'advice on unconfigured init.defaultBranch' '
871+
test_expect_success !WITH_BREAKING_CHANGES 'advice on unconfigured init.defaultBranch' '
872872
GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME= git -c color.advice=always \
873873
init unconfigured-default-branch-name 2>err &&
874874
test_decode_color <err >decoded &&
@@ -883,16 +883,17 @@ test_expect_success 'advice on unconfigured init.defaultBranch disabled' '
883883
test_grep ! "hint: " err
884884
'
885885

886-
test_expect_success 'overridden default main branch name (env)' '
886+
test_expect_success !WITH_BREAKING_CHANGES 'overridden default main branch name (env)' '
887887
test_config_global init.defaultBranch nmb &&
888888
GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=env git init main-branch-env &&
889889
git -C main-branch-env symbolic-ref HEAD >actual &&
890890
grep env actual
891891
'
892892

893893
test_expect_success 'invalid default branch name' '
894-
test_must_fail env GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME="with space" \
895-
git init initial-branch-invalid 2>err &&
894+
test_must_fail env GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME= \
895+
git -c init.defaultBranch="with space" \
896+
init initial-branch-invalid 2>err &&
896897
test_grep "invalid branch name" err
897898
'
898899

t/test-lib.sh

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,17 @@ then
127127
export GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS
128128
fi
129129

130-
# Explicitly set the default branch name for testing, to avoid the
131-
# transitory "git init" warning under --verbose.
132-
: ${GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME:=master}
133-
export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
130+
if test -z "$WITH_BREAKING_CHANGES"
131+
then
132+
# Explicitly set the default branch name for testing, to avoid the
133+
# transitory "git init" warning under --verbose.
134+
: ${GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME:=master}
135+
export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
136+
else
137+
# Unset the default branch name that is set in many test scripts
138+
# before sourcing this file.
139+
unset GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
140+
fi
134141

135142
################################################################
136143
# It appears that people try to run tests without building...

0 commit comments

Comments
 (0)