diff options
author | Edward Thomson <ethomson@edwardthomson.com> | 2020-07-13 08:47:15 +0100 |
---|---|---|
committer | Edward Thomson <ethomson@edwardthomson.com> | 2020-08-03 11:53:20 +0100 |
commit | e411aae3850c59b5c32b16e339c4ce8af284183a (patch) | |
tree | 536209805f1328f4534c9b75e346ace539881393 | |
parent | c71321a099373753c22055921c8f538afed5ebb6 (diff) | |
download | libgit2-e411aae3850c59b5c32b16e339c4ce8af284183a.tar.gz |
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.
-rw-r--r-- | src/repository.c | 56 | ||||
-rw-r--r-- | tests/repo/init.c | 16 |
2 files changed, 56 insertions, 16 deletions
diff --git a/src/repository.c b/src/repository.c index ebb9daa01..f7789127a 100644 --- a/src/repository.c +++ b/src/repository.c @@ -70,7 +70,7 @@ static int check_extensions(git_config *config, int version); #define GIT_FILE_CONTENT_PREFIX "gitdir:" -#define GIT_BRANCH_MASTER "master" +#define GIT_BRANCH_DEFAULT "master" #define GIT_REPO_VERSION 0 #define GIT_REPO_MAX_VERSION 1 @@ -1408,9 +1408,6 @@ int git_repository_create_head(const char *git_dir, const char *ref_name) (error = git_filebuf_open(&ref, ref_path.ptr, 0, GIT_REFS_FILE_MODE)) < 0) goto out; - if (!ref_name) - ref_name = GIT_BRANCH_MASTER; - if (git__prefixcmp(ref_name, GIT_REFS_DIR) == 0) fmt = "ref: %s\n"; else @@ -2061,6 +2058,43 @@ static int repo_init_directories( return error; } +static int repo_init_head(const char *repo_dir, const char *given) +{ + git_config *cfg = NULL; + git_buf head_path = GIT_BUF_INIT, cfg_branch = GIT_BUF_INIT; + const char *initial_head = NULL; + int error; + + if ((error = git_buf_joinpath(&head_path, repo_dir, GIT_HEAD_FILE)) < 0) + goto out; + + /* + * A template may have set a HEAD; use that unless it's been + * overridden by the caller's given initial head setting. + */ + if (git_path_exists(head_path.ptr) && !given) + goto out; + + if (given) { + initial_head = given; + } else if ((error = git_config_open_default(&cfg)) >= 0 && + (error = git_config_get_string_buf(&cfg_branch, cfg, "init.defaultbranch")) >= 0) { + initial_head = cfg_branch.ptr; + } + + if (!initial_head) + initial_head = GIT_BRANCH_DEFAULT; + + error = git_repository_create_head(repo_dir, initial_head); + +out: + git_config_free(cfg); + git_buf_dispose(&head_path); + git_buf_dispose(&cfg_branch); + + return error; +} + static int repo_init_create_origin(git_repository *repo, const char *url) { int error; @@ -2091,7 +2125,7 @@ int git_repository_init_ext( git_repository_init_options *opts) { git_buf repo_path = GIT_BUF_INIT, wd_path = GIT_BUF_INIT, - common_path = GIT_BUF_INIT, head_path = GIT_BUF_INIT; + common_path = GIT_BUF_INIT; const char *wd; bool is_valid; int error; @@ -2125,16 +2159,7 @@ int git_repository_init_ext( } else { if ((error = repo_init_structure(repo_path.ptr, wd, opts)) < 0 || (error = repo_init_config(repo_path.ptr, wd, opts->flags, opts->mode)) < 0 || - (error = git_buf_joinpath(&head_path, repo_path.ptr, GIT_HEAD_FILE)) < 0) - goto out; - - /* - * Only set the new HEAD if the file does not exist already via - * a template or if the caller has explicitly supplied an - * initial HEAD value. - */ - if ((!git_path_exists(head_path.ptr) || opts->initial_head) && - (error = git_repository_create_head(repo_path.ptr, opts->initial_head)) < 0) + (error = repo_init_head(repo_path.ptr, opts->initial_head)) < 0) goto out; } @@ -2146,7 +2171,6 @@ int git_repository_init_ext( goto out; out: - git_buf_dispose(&head_path); git_buf_dispose(&common_path); git_buf_dispose(&repo_path); git_buf_dispose(&wd_path); diff --git a/tests/repo/init.c b/tests/repo/init.c index 5a95229e6..ba00d2918 100644 --- a/tests/repo/init.c +++ b/tests/repo/init.c @@ -665,3 +665,19 @@ void test_repo_init__unwriteable_directory(void) clar__skip(); #endif } + +void test_repo_init__defaultbranch_config(void) +{ + git_reference *head; + + cl_set_cleanup(&cleanup_repository, "repo"); + + create_tmp_global_config("tmp_global_path", "init.defaultbranch", "my_default_branch"); + + cl_git_pass(git_repository_init(&_repo, "repo", 0)); + cl_git_pass(git_reference_lookup(&head, _repo, "HEAD")); + + cl_assert_equal_s("refs/heads/my_default_branch", git_reference_symbolic_target(head)); + + git_reference_free(head); +} |