Skip to content

Commit e411aae

Browse files
committed
repo: honor the init.defaultBranch setting
As part of a push towards more inclusive language, git is reconsidering using "master" as the default branch name. As a first step, this setting will be configurable with the `init.defaultBranch` configuration option. Honor this during repository initialization. During initialization, we will create an initial branch: 1. Using the `initial_head` setting, if specified; 2. Using the `HEAD` configured in a template, if it exists; 3. Using the `init.defaultBranch` configuration option, if it is set; or 4. Using `master` in the absence of additional configuration.
1 parent c71321a commit e411aae

File tree

2 files changed

+56
-16
lines changed

2 files changed

+56
-16
lines changed

src/repository.c

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ static int check_extensions(git_config *config, int version);
7070

7171
#define GIT_FILE_CONTENT_PREFIX "gitdir:"
7272

73-
#define GIT_BRANCH_MASTER "master"
73+
#define GIT_BRANCH_DEFAULT "master"
7474

7575
#define GIT_REPO_VERSION 0
7676
#define GIT_REPO_MAX_VERSION 1
@@ -1408,9 +1408,6 @@ int git_repository_create_head(const char *git_dir, const char *ref_name)
14081408
(error = git_filebuf_open(&ref, ref_path.ptr, 0, GIT_REFS_FILE_MODE)) < 0)
14091409
goto out;
14101410

1411-
if (!ref_name)
1412-
ref_name = GIT_BRANCH_MASTER;
1413-
14141411
if (git__prefixcmp(ref_name, GIT_REFS_DIR) == 0)
14151412
fmt = "ref: %s\n";
14161413
else
@@ -2061,6 +2058,43 @@ static int repo_init_directories(
20612058
return error;
20622059
}
20632060

2061+
static int repo_init_head(const char *repo_dir, const char *given)
2062+
{
2063+
git_config *cfg = NULL;
2064+
git_buf head_path = GIT_BUF_INIT, cfg_branch = GIT_BUF_INIT;
2065+
const char *initial_head = NULL;
2066+
int error;
2067+
2068+
if ((error = git_buf_joinpath(&head_path, repo_dir, GIT_HEAD_FILE)) < 0)
2069+
goto out;
2070+
2071+
/*
2072+
* A template may have set a HEAD; use that unless it's been
2073+
* overridden by the caller's given initial head setting.
2074+
*/
2075+
if (git_path_exists(head_path.ptr) && !given)
2076+
goto out;
2077+
2078+
if (given) {
2079+
initial_head = given;
2080+
} else if ((error = git_config_open_default(&cfg)) >= 0 &&
2081+
(error = git_config_get_string_buf(&cfg_branch, cfg, "init.defaultbranch")) >= 0) {
2082+
initial_head = cfg_branch.ptr;
2083+
}
2084+
2085+
if (!initial_head)
2086+
initial_head = GIT_BRANCH_DEFAULT;
2087+
2088+
error = git_repository_create_head(repo_dir, initial_head);
2089+
2090+
out:
2091+
git_config_free(cfg);
2092+
git_buf_dispose(&head_path);
2093+
git_buf_dispose(&cfg_branch);
2094+
2095+
return error;
2096+
}
2097+
20642098
static int repo_init_create_origin(git_repository *repo, const char *url)
20652099
{
20662100
int error;
@@ -2091,7 +2125,7 @@ int git_repository_init_ext(
20912125
git_repository_init_options *opts)
20922126
{
20932127
git_buf repo_path = GIT_BUF_INIT, wd_path = GIT_BUF_INIT,
2094-
common_path = GIT_BUF_INIT, head_path = GIT_BUF_INIT;
2128+
common_path = GIT_BUF_INIT;
20952129
const char *wd;
20962130
bool is_valid;
20972131
int error;
@@ -2125,16 +2159,7 @@ int git_repository_init_ext(
21252159
} else {
21262160
if ((error = repo_init_structure(repo_path.ptr, wd, opts)) < 0 ||
21272161
(error = repo_init_config(repo_path.ptr, wd, opts->flags, opts->mode)) < 0 ||
2128-
(error = git_buf_joinpath(&head_path, repo_path.ptr, GIT_HEAD_FILE)) < 0)
2129-
goto out;
2130-
2131-
/*
2132-
* Only set the new HEAD if the file does not exist already via
2133-
* a template or if the caller has explicitly supplied an
2134-
* initial HEAD value.
2135-
*/
2136-
if ((!git_path_exists(head_path.ptr) || opts->initial_head) &&
2137-
(error = git_repository_create_head(repo_path.ptr, opts->initial_head)) < 0)
2162+
(error = repo_init_head(repo_path.ptr, opts->initial_head)) < 0)
21382163
goto out;
21392164
}
21402165

@@ -2146,7 +2171,6 @@ int git_repository_init_ext(
21462171
goto out;
21472172

21482173
out:
2149-
git_buf_dispose(&head_path);
21502174
git_buf_dispose(&common_path);
21512175
git_buf_dispose(&repo_path);
21522176
git_buf_dispose(&wd_path);

tests/repo/init.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -665,3 +665,19 @@ void test_repo_init__unwriteable_directory(void)
665665
clar__skip();
666666
#endif
667667
}
668+
669+
void test_repo_init__defaultbranch_config(void)
670+
{
671+
git_reference *head;
672+
673+
cl_set_cleanup(&cleanup_repository, "repo");
674+
675+
create_tmp_global_config("tmp_global_path", "init.defaultbranch", "my_default_branch");
676+
677+
cl_git_pass(git_repository_init(&_repo, "repo", 0));
678+
cl_git_pass(git_reference_lookup(&head, _repo, "HEAD"));
679+
680+
cl_assert_equal_s("refs/heads/my_default_branch", git_reference_symbolic_target(head));
681+
682+
git_reference_free(head);
683+
}

0 commit comments

Comments
 (0)