Skip to content

Commit 11cbda2

Browse files
committed
Merge branch 'js/default-branch-name'
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: contrib: subtree: adjust test to change in fmt-merge-msg 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 docs: add missing diamond brackets submodule: fall back to remote's HEAD for missing remote.<name>.branch send-pack/transport-helper: avoid mentioning a particular branch fmt-merge-msg: stop treating `master` specially
2 parents 480e785 + 508fd8e commit 11cbda2

Some content is hidden

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

52 files changed

+301
-133
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-branch.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ SYNOPSIS
1212
[-v [--abbrev=<length> | --no-abbrev]]
1313
[--column[=<options>] | --no-column] [--sort=<key>]
1414
[(--merged | --no-merged) [<commit>]]
15-
[--contains [<commit]] [--no-contains [<commit>]]
15+
[--contains [<commit>]] [--no-contains [<commit>]]
1616
[--points-at <object>] [--format=<format>]
1717
[(-r | --remotes) | (-a | --all)]
1818
[--list] [<pattern>...]

Documentation/git-clone.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ maintain a branch with no references other than a single cloned
259259
branch. This is useful e.g. to maintain minimal clones of the default
260260
branch of some repository for search indexing.
261261

262-
--recurse-submodules[=<pathspec]::
262+
--recurse-submodules[=<pathspec>]::
263263
After the clone is created, initialize and clone submodules
264264
within based on the provided pathspec. If no pathspec is
265265
provided, all submodules are initialized and cloned.

Documentation/git-init.txt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ SYNOPSIS
1010
--------
1111
[verse]
1212
'git init' [-q | --quiet] [--bare] [--template=<template_directory>]
13-
[--separate-git-dir <git dir>] [--object-format=<format]
13+
[--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: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ set-branch (-d|--default) [--] <path>::
183183
Sets the default remote tracking branch for the submodule. The
184184
`--branch` option allows the remote branch to be specified. The
185185
`--default` option removes the submodule.<name>.branch configuration
186-
key, which causes the tracking branch to default to 'master'.
186+
key, which causes the tracking branch to default to the remote 'HEAD'.
187187

188188
set-url [--] <path> <newurl>::
189189
Sets the URL of the specified submodule to <newurl>. Then, it will
@@ -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 the remote 'HEAD'.
288288

289289
-f::
290290
--force::
@@ -322,10 +322,10 @@ 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
326-
be overridden by setting the `submodule.<name>.branch` option in
327-
either `.gitmodules` or `.git/config` (with `.git/config` taking
328-
precedence).
325+
The remote branch used defaults to the remote `HEAD`, but the branch
326+
name may be overridden by setting the `submodule.<name>.branch`
327+
option in either `.gitmodules` or `.git/config` (with `.git/config`
328+
taking precedence).
329329
+
330330
This works for any of the supported update procedures (`--checkout`,
331331
`--rebase`, etc.). The only change is the source of the target SHA-1.

Documentation/gitmodules.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ submodule.<name>.update::
4949

5050
submodule.<name>.branch::
5151
A remote branch name for tracking updates in the upstream submodule.
52-
If the option is not specified, it defaults to 'master'. A special
53-
value of `.` is used to indicate that the name of the branch in the
54-
submodule should be the same name as the current branch in the
52+
If the option is not specified, it defaults to the remote 'HEAD'.
53+
A special value of `.` is used to indicate that the name of the branch
54+
in the submodule should be the same name as the current branch in the
5555
current repository. See the `--remote` documentation in
5656
linkgit:git-submodule[1] for details.
5757

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 & 6 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,15 +259,26 @@ 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+
* Point the HEAD symref to the initial branch with if HEAD does
263+
* not yet exist.
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);
281+
free(ref);
270282
}
271283

272284
initialize_repository_version(fmt->hash_algo);
@@ -383,7 +395,8 @@ static void validate_hash_algorithm(struct repository_format *repo_fmt, int hash
383395
}
384396

385397
int init_db(const char *git_dir, const char *real_git_dir,
386-
const char *template_dir, int hash, unsigned int flags)
398+
const char *template_dir, int hash, const char *initial_branch,
399+
unsigned int flags)
387400
{
388401
int reinit;
389402
int exist_ok = flags & INIT_DB_EXIST_OK;
@@ -425,7 +438,11 @@ int init_db(const char *git_dir, const char *real_git_dir,
425438

426439
validate_hash_algorithm(&repo_fmt, hash);
427440

428-
reinit = create_default_files(template_dir, original_git_dir, &repo_fmt);
441+
reinit = create_default_files(template_dir, original_git_dir,
442+
initial_branch, &repo_fmt);
443+
if (reinit && initial_branch)
444+
warning(_("re-init: ignored --initial-branch=%s"),
445+
initial_branch);
429446

430447
create_object_directory();
431448

@@ -528,6 +545,7 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)
528545
const char *template_dir = NULL;
529546
unsigned int flags = 0;
530547
const char *object_format = NULL;
548+
const char *initial_branch = NULL;
531549
int hash_algo = GIT_HASH_UNKNOWN;
532550
const struct option init_db_options[] = {
533551
OPT_STRING(0, "template", &template_dir, N_("template-directory"),
@@ -541,6 +559,8 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)
541559
OPT_BIT('q', "quiet", &flags, N_("be quiet"), INIT_DB_QUIET),
542560
OPT_STRING(0, "separate-git-dir", &real_git_dir, N_("gitdir"),
543561
N_("separate git dir from working tree")),
562+
OPT_STRING('b', "initial-branch", &initial_branch, N_("name"),
563+
N_("override the name of the initial branch")),
544564
OPT_STRING(0, "object-format", &object_format, N_("hash"),
545565
N_("specify the hash algorithm to use")),
546566
OPT_END()
@@ -652,5 +672,6 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)
652672
UNLEAK(work_tree);
653673

654674
flags |= INIT_DB_EXIST_OK;
655-
return init_db(git_dir, real_git_dir, template_dir, hash_algo, flags);
675+
return init_db(git_dir, real_git_dir, template_dir, hash_algo,
676+
initial_branch, flags);
656677
}

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);

contrib/subtree/t/t7900-subtree.sh

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,8 @@ test_expect_success 'merge new subproj history into sub dir/ with --prefix' '
196196
cd "$subtree_test_count" &&
197197
git fetch ./"sub proj" master &&
198198
git subtree merge --prefix="sub dir" FETCH_HEAD &&
199-
check_equal "$(last_commit_message)" "Merge commit '\''$(git rev-parse FETCH_HEAD)'\''"
199+
check_equal "$(last_commit_message)" \
200+
"Merge commit '\''$(git rev-parse FETCH_HEAD)'\'' into master"
200201
)
201202
'
202203

@@ -273,7 +274,8 @@ test_expect_success 'merge new subproj history into subdir/ with a slash appende
273274
cd "$test_count" &&
274275
git fetch ./subproj master &&
275276
git subtree merge --prefix=subdir/ FETCH_HEAD &&
276-
check_equal "$(last_commit_message)" "Merge commit '\''$(git rev-parse FETCH_HEAD)'\''"
277+
check_equal "$(last_commit_message)" \
278+
"Merge commit '\''$(git rev-parse FETCH_HEAD)'\'' into master"
277279
)
278280
'
279281

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 default (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",
@@ -2097,8 +2097,16 @@ struct ref *guess_remote_head(const struct ref *head,
20972097
if (head->symref)
20982098
return copy_ref(find_ref_by_name(refs, head->symref));
20992099

2100-
/* If refs/heads/master could be right, it is. */
2100+
/* If a remote branch exists with the default branch name, let's use it. */
21012101
if (!all) {
2102+
char *ref = xstrfmt("refs/heads/%s", git_default_branch_name());
2103+
2104+
r = find_ref_by_name(refs, ref);
2105+
free(ref);
2106+
if (r && oideq(&r->old_oid, &head->old_oid))
2107+
return copy_ref(r);
2108+
2109+
/* Fall back to the hard-coded historical default */
21022110
r = find_ref_by_name(refs, "refs/heads/master");
21032111
if (r && oideq(&r->old_oid, &head->old_oid))
21042112
return copy_ref(r);

send-pack.c

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

411411
if (!remote_refs) {
412412
fprintf(stderr, "No refs in common and none specified; doing nothing.\n"
413-
"Perhaps you should specify a branch such as 'master'.\n");
413+
"Perhaps you should specify a branch.\n");
414414
return 0;
415415
}
416416
if (args->atomic && !atomic_supported)

0 commit comments

Comments
 (0)