Skip to content

Commit 772bdcd

Browse files
committed
Merge branch 'js/init-defaultbranch-advice'
Our users are going to be trained to prepare for future change of init.defaultBranch configuration variable. * js/init-defaultbranch-advice: init: provide useful advice about init.defaultBranch get_default_branch_name(): prepare for showing some advice branch -m: allow renaming a yet-unborn branch init: document `init.defaultBranch` better
2 parents f4d8e19 + 675704c commit 772bdcd

File tree

10 files changed

+61
-20
lines changed

10 files changed

+61
-20
lines changed

Documentation/git-init.txt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ DESCRIPTION
2020

2121
This command creates an empty Git repository - basically a `.git`
2222
directory with subdirectories for `objects`, `refs/heads`,
23-
`refs/tags`, and template files. An initial `HEAD` file that
24-
references the HEAD of the master branch is also created.
23+
`refs/tags`, and template files. An initial branch without any
24+
commits will be created (see the `--initial-branch` option below
25+
for its name).
2526

2627
If the `$GIT_DIR` environment variable is set then it specifies a path
2728
to use instead of `./.git` for the base of the repository.
@@ -73,8 +74,10 @@ If this is reinitialization, the repository will be moved to the specified path.
7374
-b <branch-name>::
7475
--initial-branch=<branch-name>::
7576

76-
Use the specified name for the initial branch in the newly created repository.
77-
If not specified, fall back to the default name: `master`.
77+
Use the specified name for the initial branch in the newly created
78+
repository. If not specified, fall back to the default name (currently
79+
`master`, but this is subject to change in the future; the name can be
80+
customized via the `init.defaultBranch` configuration variable).
7881

7982
--shared[=(false|true|umask|group|all|world|everybody|0xxx)]::
8083

builtin/branch.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,9 @@ static void copy_or_rename_branch(const char *oldname, const char *newname, int
538538
strbuf_addf(&logmsg, "Branch: renamed %s to %s",
539539
oldref.buf, newref.buf);
540540

541-
if (!copy && rename_ref(oldref.buf, newref.buf, logmsg.buf))
541+
if (!copy &&
542+
(!head || strcmp(oldname, head) || !is_null_oid(&head_oid)) &&
543+
rename_ref(oldref.buf, newref.buf, logmsg.buf))
542544
die(_("Branch rename failed"));
543545
if (copy && copy_existing_ref(oldref.buf, newref.buf, logmsg.buf))
544546
die(_("Branch copy failed"));

builtin/clone.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1326,7 +1326,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
13261326
remote_head = NULL;
13271327
option_no_checkout = 1;
13281328
if (!option_bare) {
1329-
const char *branch = git_default_branch_name();
1329+
const char *branch = git_default_branch_name(0);
13301330
char *ref = xstrfmt("refs/heads/%s", branch);
13311331

13321332
install_branch_config(0, branch, remote_name, ref);

builtin/init-db.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,8 @@ void initialize_repository_version(int hash_algo, int reinit)
202202
static int create_default_files(const char *template_path,
203203
const char *original_git_dir,
204204
const char *initial_branch,
205-
const struct repository_format *fmt)
205+
const struct repository_format *fmt,
206+
int quiet)
206207
{
207208
struct stat st1;
208209
struct strbuf buf = STRBUF_INIT;
@@ -267,7 +268,7 @@ static int create_default_files(const char *template_path,
267268
char *ref;
268269

269270
if (!initial_branch)
270-
initial_branch = git_default_branch_name();
271+
initial_branch = git_default_branch_name(quiet);
271272

272273
ref = xstrfmt("refs/heads/%s", initial_branch);
273274
if (check_refname_format(ref, 0) < 0)
@@ -438,7 +439,8 @@ int init_db(const char *git_dir, const char *real_git_dir,
438439
validate_hash_algorithm(&repo_fmt, hash);
439440

440441
reinit = create_default_files(template_dir, original_git_dir,
441-
initial_branch, &repo_fmt);
442+
initial_branch, &repo_fmt,
443+
flags & INIT_DB_QUIET);
442444
if (reinit && initial_branch)
443445
warning(_("re-init: ignored --initial-branch=%s"),
444446
initial_branch);

refs.c

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,20 @@ void expand_ref_prefix(struct strvec *prefixes, const char *prefix)
562562
strvec_pushf(prefixes, *p, len, prefix);
563563
}
564564

565-
char *repo_default_branch_name(struct repository *r)
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)
566579
{
567580
const char *config_key = "init.defaultbranch";
568581
const char *config_display_key = "init.defaultBranch";
@@ -574,8 +587,11 @@ char *repo_default_branch_name(struct repository *r)
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))
@@ -585,12 +601,12 @@ char *repo_default_branch_name(struct repository *r)
585601
return ret;
586602
}
587603

588-
const char *git_default_branch_name(void)
604+
const char *git_default_branch_name(int quiet)
589605
{
590606
static char *ret;
591607

592608
if (!ret)
593-
ret = repo_default_branch_name(the_repository);
609+
ret = repo_default_branch_name(the_repository, quiet);
594610

595611
return ret;
596612
}

refs.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,8 @@ int dwim_log(const char *str, int len, struct object_id *oid, char **ref);
170170
* The return value of `repo_default_branch_name()` is an allocated string. The
171171
* return value of `git_default_branch_name()` is a singleton.
172172
*/
173-
const char *git_default_branch_name(void);
174-
char *repo_default_branch_name(struct repository *r);
173+
const char *git_default_branch_name(int quiet);
174+
char *repo_default_branch_name(struct repository *r, int quiet);
175175

176176
/*
177177
* A ref_transaction represents a collection of reference updates that

remote.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ static void read_branches_file(struct remote *remote)
284284
if (frag)
285285
*(frag++) = '\0';
286286
else
287-
frag = (char *)git_default_branch_name();
287+
frag = (char *)git_default_branch_name(0);
288288

289289
add_url_alias(remote, strbuf_detach(&buf, NULL));
290290
refspec_appendf(&remote->fetch, "refs/heads/%s:refs/heads/%s",
@@ -2206,7 +2206,8 @@ struct ref *guess_remote_head(const struct ref *head,
22062206

22072207
/* If a remote branch exists with the default branch name, let's use it. */
22082208
if (!all) {
2209-
char *ref = xstrfmt("refs/heads/%s", git_default_branch_name());
2209+
char *ref = xstrfmt("refs/heads/%s",
2210+
git_default_branch_name(0));
22102211

22112212
r = find_ref_by_name(refs, ref);
22122213
free(ref);

t/t0001-init.sh

Lines changed: 16 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 &&
@@ -571,4 +578,12 @@ test_expect_success 'invalid default branch name' '
571578
test_i18ngrep "invalid branch name" err
572579
'
573580

581+
test_expect_success 'branch -m with the initial branch' '
582+
git init rename-initial &&
583+
git -C rename-initial branch -m renamed &&
584+
test renamed = $(git -C rename-initial symbolic-ref --short HEAD) &&
585+
git -C rename-initial branch -m renamed again &&
586+
test again = $(git -C rename-initial symbolic-ref --short HEAD)
587+
'
588+
574589
test_done

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)