Skip to content

Commit e50c3ca

Browse files
committed
Merge branch 'pw/3.0-default-initial-branch-to-main'
Declare that "git init" that is not otherwise configured uses 'main' as the initial branch, not 'master', starting Git 3.0. * pw/3.0-default-initial-branch-to-main: t0613: stop setting default initial branch t9902: switch default branch name to main t4013: switch default branch name to main breaking-changes: switch default branch to main
2 parents d235f69 + 5590b4e commit e50c3ca

File tree

107 files changed

+283
-240
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

107 files changed

+283
-240
lines changed

Documentation/BreakingChanges.adoc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,12 @@ 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+
* In new repositories, the default branch name will be `main`. We have been
169+
warning that the default name will change since 675704c74dd (init:
170+
provide useful advice about init.defaultBranch, 2020-12-11). The new name
171+
matches the default branch name used in new repositories by many of the
172+
big Git forges.
173+
168174
=== Removals
169175

170176
* 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: 11 additions & 4 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,8 +641,9 @@ static const char default_branch_name_advice[] = N_(
639641
"\n"
640642
"\tgit branch -m <name>\n"
641643
);
644+
#endif /* WITH_BREAKING_CHANGES */
642645

643-
char *repo_default_branch_name(struct repository *r, int quiet)
646+
char *repo_default_branch_name(struct repository *r, MAYBE_UNUSED int quiet)
644647
{
645648
const char *config_key = "init.defaultbranch";
646649
const char *config_display_key = "init.defaultBranch";
@@ -649,14 +652,18 @@ char *repo_default_branch_name(struct repository *r, int quiet)
649652

650653
if (env && *env)
651654
ret = xstrdup(env);
652-
else if (repo_config_get_string(r, config_key, &ret) < 0)
655+
if (!ret && repo_config_get_string(r, config_key, &ret) < 0)
653656
die(_("could not retrieve `%s`"), config_display_key);
654657

655658
if (!ret) {
659+
#ifdef WITH_BREAKING_CHANGES
660+
ret = xstrdup("main");
661+
#else
656662
ret = xstrdup("master");
657663
if (!quiet)
658664
advise_if_enabled(ADVICE_DEFAULT_BRANCH_NAME,
659665
_(default_branch_name_advice), ret);
666+
#endif /* WITH_BREAKING_CHANGES */
660667
}
661668

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

t/t0001-init.sh

Lines changed: 17 additions & 1 deletion
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,6 +883,22 @@ test_expect_success 'advice on unconfigured init.defaultBranch disabled' '
883883
test_grep ! "hint: " err
884884
'
885885

886+
test_expect_success 'default branch name' '
887+
if test_have_prereq WITH_BREAKING_CHANGES
888+
then
889+
expect=main
890+
else
891+
expect=master
892+
fi &&
893+
echo "refs/heads/$expect" >expect &&
894+
(
895+
sane_unset GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME &&
896+
git init default-initial-branch-name
897+
) &&
898+
git -C default-initial-branch-name symbolic-ref HEAD >actual &&
899+
test_cmp expect actual
900+
'
901+
886902
test_expect_success 'overridden default main branch name (env)' '
887903
test_config_global init.defaultBranch nmb &&
888904
GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=env git init main-branch-env &&

t/t0613-reftable-write-options.sh

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,18 @@ export GIT_TEST_REFTABLE_AUTOCOMPACTION
1111
# Block sizes depend on the hash function, so we force SHA1 here.
1212
GIT_TEST_DEFAULT_HASH=sha1
1313
export GIT_TEST_DEFAULT_HASH
14-
# Block sizes also depend on the actual refs we write, so we force "master" to
15-
# be the default initial branch name.
16-
GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=master
17-
export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
1814

1915
. ./test-lib.sh
2016

17+
# Block sizes depend on the actual refs we write, so, for tests
18+
# that check block size, we force the initial branch name to be "master".
19+
init_repo () {
20+
git init --initial-branch master repo
21+
}
22+
2123
test_expect_success 'default write options' '
2224
test_when_finished "rm -rf repo" &&
23-
git init repo &&
25+
init_repo &&
2426
(
2527
cd repo &&
2628
test_commit initial &&
@@ -43,7 +45,7 @@ test_expect_success 'default write options' '
4345
test_expect_success 'disabled reflog writes no log blocks' '
4446
test_config_global core.logAllRefUpdates false &&
4547
test_when_finished "rm -rf repo" &&
46-
git init repo &&
48+
init_repo &&
4749
(
4850
cd repo &&
4951
test_commit initial &&
@@ -62,7 +64,7 @@ test_expect_success 'disabled reflog writes no log blocks' '
6264

6365
test_expect_success 'many refs results in multiple blocks' '
6466
test_when_finished "rm -rf repo" &&
65-
git init repo &&
67+
init_repo &&
6668
(
6769
cd repo &&
6870
test_commit initial &&
@@ -115,7 +117,7 @@ test_expect_success 'tiny block size leads to error' '
115117
test_expect_success 'small block size leads to multiple ref blocks' '
116118
test_config_global core.logAllRefUpdates false &&
117119
test_when_finished "rm -rf repo" &&
118-
git init repo &&
120+
init_repo &&
119121
(
120122
cd repo &&
121123
test_commit A &&
@@ -172,7 +174,7 @@ test_expect_success 'block size exceeding maximum supported size' '
172174

173175
test_expect_success 'restart interval at every single record' '
174176
test_when_finished "rm -rf repo" &&
175-
git init repo &&
177+
init_repo &&
176178
(
177179
cd repo &&
178180
test_commit initial &&
@@ -212,7 +214,7 @@ test_expect_success 'restart interval exceeding maximum supported interval' '
212214
test_expect_success 'object index gets written by default with ref index' '
213215
test_config_global core.logAllRefUpdates false &&
214216
test_when_finished "rm -rf repo" &&
215-
git init repo &&
217+
init_repo &&
216218
(
217219
cd repo &&
218220
test_commit initial &&
@@ -247,7 +249,7 @@ test_expect_success 'object index gets written by default with ref index' '
247249
test_expect_success 'object index can be disabled' '
248250
test_config_global core.logAllRefUpdates false &&
249251
test_when_finished "rm -rf repo" &&
250-
git init repo &&
252+
init_repo &&
251253
(
252254
cd repo &&
253255
test_commit initial &&

0 commit comments

Comments
 (0)