From 3f4bc2132c933e1a1719555dde1207d4896f7a12 Mon Sep 17 00:00:00 2001 From: Edward Thomson Date: Thu, 7 Jan 2021 14:05:02 +0000 Subject: repo: ignore empty init.defaultbranch The init.defaultbranch option may be set, but empty. In this case, we should ignore it instead of trying to set our default branch to `refs/heads/`. --- src/repository.c | 3 ++- tests/repo/init.c | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/repository.c b/src/repository.c index 948413d17..de1a89582 100644 --- a/src/repository.c +++ b/src/repository.c @@ -2092,7 +2092,8 @@ static int repo_init_head(const char *repo_dir, const char *given) 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) { + (error = git_config_get_string_buf(&cfg_branch, cfg, "init.defaultbranch")) >= 0 && + *cfg_branch.ptr) { initial_head = cfg_branch.ptr; } diff --git a/tests/repo/init.c b/tests/repo/init.c index b86e09a58..01371ba78 100644 --- a/tests/repo/init.c +++ b/tests/repo/init.c @@ -688,3 +688,19 @@ void test_repo_init__defaultbranch_config(void) git_reference_free(head); } + +void test_repo_init__defaultbranch_config_empty(void) +{ + git_reference *head; + + cl_set_cleanup(&cleanup_repository, "repo"); + + create_tmp_global_config("tmp_global_path", "init.defaultbranch", ""); + + cl_git_pass(git_repository_init(&g_repo, "repo", 0)); + cl_git_pass(git_reference_lookup(&head, g_repo, "HEAD")); + + cl_assert_equal_s("refs/heads/master", git_reference_symbolic_target(head)); + + git_reference_free(head); +} -- cgit v1.2.1