Skip to content

Commit 2ee6c66

Browse files
committed
Merge branch 'js/default-branch-name' into seen
The name of the primary branch in existing repositories, and the default name used for the first branch in newly created repositories, is made configurable, so that we can eventually wean ourselves off of the hardcoded 'master'. * js/default-branch-name: testsvn: respect `init.defaultBranch` remote: use the configured default branch name when appropriate clone: use configured default branch name when appropriate init: allow setting the default for the initial branch name via the config init: allow specifying the initial branch name for the new repository submodule: use a better fall-back for missing remote.<name>.branch send-pack/transport-helper: avoid mentioning a particular branch fmt-merge-msg: stop treating `master` specially
2 parents 0dcf939 + 3a50e5b commit 2ee6c66

Some content is hidden

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

48 files changed

+287
-122
lines changed

Documentation/config/init.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
init.templateDir::
22
Specify the directory from which templates will be copied.
33
(See the "TEMPLATE DIRECTORY" section of linkgit:git-init[1].)
4+
5+
init.defaultBranch::
6+
Allows overriding the default branch name e.g. when initializing
7+
a new repository or when cloning an empty repository.

Documentation/git-init.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ SYNOPSIS
1111
[verse]
1212
'git init' [-q | --quiet] [--bare] [--template=<template_directory>]
1313
[--separate-git-dir <git dir>] [--object-format=<format]
14+
[-b <branch-name> | --initial-branch=<branch-name>]
1415
[--shared[=<permissions>]] [directory]
1516

1617

@@ -67,6 +68,12 @@ repository.
6768
+
6869
If this is reinitialization, the repository will be moved to the specified path.
6970

71+
-b <branch-name::
72+
--initial-branch=<branch-name>::
73+
74+
Use the specified name for the initial branch in the newly created repository.
75+
If not specified, fall back to the default name: `master`.
76+
7077
--shared[=(false|true|umask|group|all|world|everybody|0xxx)]::
7178

7279
Specify that the Git repository is to be shared amongst several users. This

Documentation/git-submodule.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ OPTIONS
284284
`.gitmodules` for `update --remote`. A special value of `.` is used to
285285
indicate that the name of the branch in the submodule should be the
286286
same name as the current branch in the current repository. If the
287-
option is not specified, it defaults to 'master'.
287+
option is not specified, it defaults to 'HEAD'.
288288

289289
-f::
290290
--force::
@@ -322,7 +322,7 @@ OPTIONS
322322
the superproject's recorded SHA-1 to update the submodule, use the
323323
status of the submodule's remote-tracking branch. The remote used
324324
is branch's remote (`branch.<name>.remote`), defaulting to `origin`.
325-
The remote branch used defaults to `master`, but the branch name may
325+
The remote branch used defaults to `HEAD`, but the branch name may
326326
be overridden by setting the `submodule.<name>.branch` option in
327327
either `.gitmodules` or `.git/config` (with `.git/config` taking
328328
precedence).

builtin/clone.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,7 +1111,8 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
11111111
}
11121112
}
11131113

1114-
init_db(git_dir, real_git_dir, option_template, GIT_HASH_UNKNOWN, INIT_DB_QUIET);
1114+
init_db(git_dir, real_git_dir, option_template, GIT_HASH_UNKNOWN, NULL,
1115+
INIT_DB_QUIET);
11151116

11161117
if (real_git_dir)
11171118
git_dir = real_git_dir;
@@ -1275,9 +1276,13 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
12751276
remote_head_points_at = NULL;
12761277
remote_head = NULL;
12771278
option_no_checkout = 1;
1278-
if (!option_bare)
1279-
install_branch_config(0, "master", option_origin,
1280-
"refs/heads/master");
1279+
if (!option_bare) {
1280+
const char *branch = git_default_branch_name();
1281+
char *ref = xstrfmt("refs/heads/%s", branch);
1282+
1283+
install_branch_config(0, branch, option_origin, ref);
1284+
free(ref);
1285+
}
12811286
}
12821287

12831288
write_refspec_config(src_ref_prefix, our_head_points_at,

builtin/init-db.c

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ void initialize_repository_version(int hash_algo)
203203

204204
static int create_default_files(const char *template_path,
205205
const char *original_git_dir,
206+
const char *initial_branch,
206207
const struct repository_format *fmt)
207208
{
208209
struct stat st1;
@@ -258,16 +259,29 @@ static int create_default_files(const char *template_path,
258259
die("failed to set up refs db: %s", err.buf);
259260

260261
/*
261-
* Create the default symlink from ".git/HEAD" to the "master"
262-
* branch, if it does not exist yet.
262+
* Create the default symlink from ".git/HEAD" to the default
263+
* branch name, if it does not exist yet.
263264
*/
264265
path = git_path_buf(&buf, "HEAD");
265266
reinit = (!access(path, R_OK)
266267
|| readlink(path, junk, sizeof(junk)-1) != -1);
267268
if (!reinit) {
268-
if (create_symref("HEAD", "refs/heads/master", NULL) < 0)
269+
char *ref;
270+
271+
if (!initial_branch)
272+
initial_branch = git_default_branch_name();
273+
274+
ref = xstrfmt("refs/heads/%s", initial_branch);
275+
if (check_refname_format(ref, 0) < 0)
276+
die(_("invalid initial branch name: '%s'"),
277+
initial_branch);
278+
279+
if (create_symref("HEAD", ref, NULL) < 0)
269280
exit(1);
270-
}
281+
free(ref);
282+
} else if (initial_branch)
283+
warning(_("re-init: ignoring --initial-branch=%s"),
284+
initial_branch);
271285

272286
initialize_repository_version(fmt->hash_algo);
273287

@@ -383,7 +397,8 @@ static void validate_hash_algorithm(struct repository_format *repo_fmt, int hash
383397
}
384398

385399
int init_db(const char *git_dir, const char *real_git_dir,
386-
const char *template_dir, int hash, unsigned int flags)
400+
const char *template_dir, int hash, const char *initial_branch,
401+
unsigned int flags)
387402
{
388403
int reinit;
389404
int exist_ok = flags & INIT_DB_EXIST_OK;
@@ -425,7 +440,8 @@ int init_db(const char *git_dir, const char *real_git_dir,
425440

426441
validate_hash_algorithm(&repo_fmt, hash);
427442

428-
reinit = create_default_files(template_dir, original_git_dir, &repo_fmt);
443+
reinit = create_default_files(template_dir, original_git_dir,
444+
initial_branch, &repo_fmt);
429445

430446
create_object_directory();
431447

@@ -528,6 +544,7 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)
528544
const char *template_dir = NULL;
529545
unsigned int flags = 0;
530546
const char *object_format = NULL;
547+
const char *initial_branch = NULL;
531548
int hash_algo = GIT_HASH_UNKNOWN;
532549
const struct option init_db_options[] = {
533550
OPT_STRING(0, "template", &template_dir, N_("template-directory"),
@@ -541,6 +558,8 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)
541558
OPT_BIT('q', "quiet", &flags, N_("be quiet"), INIT_DB_QUIET),
542559
OPT_STRING(0, "separate-git-dir", &real_git_dir, N_("gitdir"),
543560
N_("separate git dir from working tree")),
561+
OPT_STRING('b', "initial-branch", &initial_branch, N_("name"),
562+
N_("override the name of the initial branch")),
544563
OPT_STRING(0, "object-format", &object_format, N_("hash"),
545564
N_("specify the hash algorithm to use")),
546565
OPT_END()
@@ -652,5 +671,6 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)
652671
UNLEAK(work_tree);
653672

654673
flags |= INIT_DB_EXIST_OK;
655-
return init_db(git_dir, real_git_dir, template_dir, hash_algo, flags);
674+
return init_db(git_dir, real_git_dir, template_dir, hash_algo,
675+
initial_branch, flags);
656676
}

builtin/submodule--helper.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1981,7 +1981,7 @@ static const char *remote_submodule_branch(const char *path)
19811981
free(key);
19821982

19831983
if (!branch)
1984-
return "master";
1984+
return "HEAD";
19851985

19861986
if (!strcmp(branch, ".")) {
19871987
const char *refname = resolve_ref_unsafe("HEAD", 0, NULL, NULL);

cache.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,7 @@ int path_inside_repo(const char *prefix, const char *path);
628628

629629
int init_db(const char *git_dir, const char *real_git_dir,
630630
const char *template_dir, int hash_algo,
631-
unsigned int flags);
631+
const char *initial_branch, unsigned int flags);
632632
void initialize_repository_version(int hash_algo);
633633

634634
void sanitize_stdfds(void);

fmt-merge-msg.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -451,10 +451,7 @@ static void fmt_merge_msg_title(struct strbuf *out,
451451
strbuf_addf(out, " of %s", srcs.items[i].string);
452452
}
453453

454-
if (!strcmp("master", current_branch))
455-
strbuf_addch(out, '\n');
456-
else
457-
strbuf_addf(out, " into %s\n", current_branch);
454+
strbuf_addf(out, " into %s\n", current_branch);
458455
}
459456

460457
static void fmt_tag_signature(struct strbuf *tagbuf,

refs.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,36 @@ void expand_ref_prefix(struct argv_array *prefixes, const char *prefix)
562562
argv_array_pushf(prefixes, *p, len, prefix);
563563
}
564564

565+
char *repo_default_branch_name(struct repository *r)
566+
{
567+
const char *config_key = "init.defaultbranch";
568+
const char *config_display_key = "init.defaultBranch";
569+
char *ret = NULL, *full_ref;
570+
571+
if (repo_config_get_string(r, config_key, &ret) < 0)
572+
die(_("could not retrieve `%s`"), config_display_key);
573+
574+
if (!ret)
575+
ret = xstrdup("master");
576+
577+
full_ref = xstrfmt("refs/heads/%s", ret);
578+
if (check_refname_format(full_ref, 0))
579+
die(_("invalid branch name: %s = %s"), config_display_key, ret);
580+
free(full_ref);
581+
582+
return ret;
583+
}
584+
585+
const char *git_default_branch_name(void)
586+
{
587+
static char *ret;
588+
589+
if (!ret)
590+
ret = repo_default_branch_name(the_repository);
591+
592+
return ret;
593+
}
594+
565595
/*
566596
* *string and *len will only be substituted, and *string returned (for
567597
* later free()ing) if the string passed in is a magic short-hand form

refs.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,15 @@ int repo_dwim_log(struct repository *r, const char *str, int len, struct object_
154154
int dwim_ref(const char *str, int len, struct object_id *oid, char **ref);
155155
int dwim_log(const char *str, int len, struct object_id *oid, char **ref);
156156

157+
/*
158+
* Retrieves the default branch name for newly-initialized repositories.
159+
*
160+
* The return value of `repo_default_branch_name()` is an allocated string. The
161+
* return value of `git_default_branch_name()` is a singleton.
162+
*/
163+
const char *git_default_branch_name(void);
164+
char *repo_default_branch_name(struct repository *r);
165+
157166
/*
158167
* A ref_transaction represents a collection of reference updates that
159168
* should succeed or fail together.

remote-testsvn.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
static const char *url;
1414
static int dump_from_file;
1515
static const char *private_ref;
16-
static const char *remote_ref = "refs/heads/master";
16+
static char *remote_ref;
1717
static const char *marksfilename, *notes_ref;
1818
struct rev_note { unsigned int rev_nr; };
1919

@@ -286,14 +286,17 @@ int cmd_main(int argc, const char **argv)
286286
private_ref_sb = STRBUF_INIT, marksfilename_sb = STRBUF_INIT,
287287
notes_ref_sb = STRBUF_INIT;
288288
static struct remote *remote;
289-
const char *url_in;
289+
const char *url_in, *remote_ref_short;
290290

291291
setup_git_directory();
292292
if (argc < 2 || argc > 3) {
293293
usage("git-remote-svn <remote-name> [<url>]");
294294
return 1;
295295
}
296296

297+
remote_ref_short = git_default_branch_name();
298+
remote_ref = xstrfmt("refs/heads/%s", remote_ref_short);
299+
297300
remote = remote_get(argv[1]);
298301
url_in = (argc == 3) ? argv[2] : remote->url[0];
299302

@@ -306,7 +309,8 @@ int cmd_main(int argc, const char **argv)
306309
url = url_sb.buf;
307310
}
308311

309-
strbuf_addf(&private_ref_sb, "refs/svn/%s/master", remote->name);
312+
strbuf_addf(&private_ref_sb, "refs/svn/%s/%s",
313+
remote->name, remote_ref_short);
310314
private_ref = private_ref_sb.buf;
311315

312316
strbuf_addf(&notes_ref_sb, "refs/notes/%s/revs", remote->name);

remote.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -276,15 +276,15 @@ static void read_branches_file(struct remote *remote)
276276

277277
/*
278278
* The branches file would have URL and optionally
279-
* #branch specified. The "master" (or specified) branch is
279+
* #branch specified. The main (or specified) branch is
280280
* fetched and stored in the local branch matching the
281281
* remote name.
282282
*/
283283
frag = strchr(buf.buf, '#');
284284
if (frag)
285285
*(frag++) = '\0';
286286
else
287-
frag = "master";
287+
frag = (char *)git_default_branch_name();
288288

289289
add_url_alias(remote, strbuf_detach(&buf, NULL));
290290
strbuf_addf(&buf, "refs/heads/%s:refs/heads/%s",
@@ -2147,8 +2147,16 @@ struct ref *guess_remote_head(const struct ref *head,
21472147
if (head->symref)
21482148
return copy_ref(find_ref_by_name(refs, head->symref));
21492149

2150-
/* If refs/heads/master could be right, it is. */
2150+
/* If a remote branch exists with the default branch name, let's use it. */
21512151
if (!all) {
2152+
char *ref = xstrfmt("refs/heads/%s", git_default_branch_name());
2153+
2154+
r = find_ref_by_name(refs, ref);
2155+
free(ref);
2156+
if (r && oideq(&r->old_oid, &head->old_oid))
2157+
return copy_ref(r);
2158+
2159+
/* Fall back to the hard-coded historical default */
21522160
r = find_ref_by_name(refs, "refs/heads/master");
21532161
if (r && oideq(&r->old_oid, &head->old_oid))
21542162
return copy_ref(r);

send-pack.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ int send_pack(struct send_pack_args *args,
471471

472472
if (!remote_refs) {
473473
fprintf(stderr, "No refs in common and none specified; doing nothing.\n"
474-
"Perhaps you should specify a branch such as 'master'.\n");
474+
"Perhaps you should specify a specific branch.\n");
475475
return 0;
476476
}
477477
if (args->atomic && !atomic_supported)

t/t0001-init.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,4 +464,30 @@ test_expect_success MINGW 'redirect std handles' '
464464
grep "Needed a single revision" output.txt
465465
'
466466

467+
test_expect_success '--initial-branch' '
468+
git init --initial-branch=hello initial-branch-option &&
469+
git -C initial-branch-option symbolic-ref HEAD >actual &&
470+
echo refs/heads/hello >expect &&
471+
test_cmp expect actual &&
472+
473+
: re-initializing should not change the branch name &&
474+
git init --initial-branch=ignore initial-branch-option 2>err &&
475+
test_i18ngrep "ignoring --initial-branch" err &&
476+
git -C initial-branch-option symbolic-ref HEAD >actual &&
477+
grep hello actual
478+
'
479+
480+
test_expect_success 'overridden default initial branch name (config)' '
481+
test_config_global init.defaultBranch nmb &&
482+
git init initial-branch-config &&
483+
git -C initial-branch-config symbolic-ref HEAD >actual &&
484+
grep nmb actual
485+
'
486+
487+
test_expect_success 'invalid default branch name' '
488+
test_config_global init.defaultBranch "with space" &&
489+
test_must_fail git init initial-branch-invalid 2>err &&
490+
test_i18ngrep "invalid branch name" err
491+
'
492+
467493
test_done

t/t1507-rev-parse-upstream.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ test_expect_success 'merge my-side@{u} records the correct name' '
137137
git branch -t new my-side@{u} &&
138138
git merge -s ours new@{u} &&
139139
git show -s --pretty=tformat:%s >actual &&
140-
echo "Merge remote-tracking branch ${SQ}origin/side${SQ}" >expect &&
140+
echo "Merge remote-tracking branch ${SQ}origin/side${SQ} into master" >expect &&
141141
test_cmp expect actual
142142
)
143143
'

t/t4013-diff-various.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,12 @@ test_expect_success setup '
117117

118118
: <<\EOF
119119
! [initial] Initial
120-
* [master] Merge branch 'side'
120+
* [master] Merge branch 'side' into master
121121
! [rearrange] Rearranged lines in dir/sub
122122
! [side] Side
123123
----
124124
+ [rearrange] Rearranged lines in dir/sub
125-
- [master] Merge branch 'side'
125+
- [master] Merge branch 'side' into master
126126
* + [side] Side
127127
* [master^] Third
128128
* [master~2] Second

t/t4013/diff.log_--decorate=full_--all

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Merge: 9a6d494 c7a2ab9
3131
Author: A U Thor <author@example.com>
3232
Date: Mon Jun 26 00:04:00 2006 +0000
3333

34-
Merge branch 'side'
34+
Merge branch 'side' into master
3535

3636
commit c7a2ab9e8eac7b117442a607d5a9b3950ae34d5a (refs/heads/side)
3737
Author: A U Thor <author@example.com>

t/t4013/diff.log_--decorate_--all

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Merge: 9a6d494 c7a2ab9
3131
Author: A U Thor <author@example.com>
3232
Date: Mon Jun 26 00:04:00 2006 +0000
3333

34-
Merge branch 'side'
34+
Merge branch 'side' into master
3535

3636
commit c7a2ab9e8eac7b117442a607d5a9b3950ae34d5a (side)
3737
Author: A U Thor <author@example.com>

0 commit comments

Comments
 (0)