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

0 commit comments

Comments
 (0)