diff options
author | Edward Thomson <ethomson@edwardthomson.com> | 2020-07-13 10:05:04 +0100 |
---|---|---|
committer | Edward Thomson <ethomson@edwardthomson.com> | 2020-08-03 11:53:20 +0100 |
commit | 471ed794d8bc8ce783964eda180c21234b2fe591 (patch) | |
tree | 225a1f117c1e4714c94208a50fa8993e20f1e26f | |
parent | 84d2a0353d3934e88b08095e0100a408c4b0a42e (diff) | |
download | libgit2-471ed794d8bc8ce783964eda180c21234b2fe591.tar.gz |
clone: respect init.defaultBranch when empty
When cloning an empty repository, we need to guess what the branch
structure should be; instead of hardcoding `master`, use the
`init.defaultBranch` setting it if it provided.
-rw-r--r-- | src/clone.c | 28 | ||||
-rw-r--r-- | tests/clone/empty.c | 17 |
2 files changed, 43 insertions, 2 deletions
diff --git a/src/clone.c b/src/clone.c index 8ca6cea1d..9214fa970 100644 --- a/src/clone.c +++ b/src/clone.c @@ -137,6 +137,31 @@ static int update_head_to_new_branch( return error; } +static int update_head_to_default(git_repository *repo) +{ + git_buf initialbranch = GIT_BUF_INIT; + const char *branch_name; + int error = 0; + + if ((error = git_repository_initialbranch(&initialbranch, repo)) < 0) + goto done; + + if (git__prefixcmp(initialbranch.ptr, GIT_REFS_HEADS_DIR) != 0) { + git_error_set(GIT_ERROR_INVALID, "invalid initial branch '%s'", initialbranch.ptr); + error = -1; + goto done; + } + + branch_name = initialbranch.ptr + strlen(GIT_REFS_HEADS_DIR); + + error = setup_tracking_config(repo, branch_name, GIT_REMOTE_ORIGIN, + initialbranch.ptr); + +done: + git_buf_dispose(&initialbranch); + return error; +} + static int update_head_to_remote( git_repository *repo, git_remote *remote, @@ -155,8 +180,7 @@ static int update_head_to_remote( /* We cloned an empty repository or one with an unborn HEAD */ if (refs_len == 0 || strcmp(refs[0]->name, GIT_HEAD_FILE)) - return setup_tracking_config( - repo, "master", GIT_REMOTE_ORIGIN, GIT_REFS_HEADS_MASTER_FILE); + return update_head_to_default(repo); /* We know we have HEAD, let's see where it points */ remote_head = refs[0]; diff --git a/tests/clone/empty.c b/tests/clone/empty.c index 8e59d2b8c..94847bc73 100644 --- a/tests/clone/empty.c +++ b/tests/clone/empty.c @@ -2,6 +2,7 @@ #include "git2/clone.h" #include "repository.h" +#include "repo/repo_helpers.h" static git_clone_options g_options; static git_repository *g_repo; @@ -22,6 +23,7 @@ void test_clone_empty__initialize(void) void test_clone_empty__cleanup(void) { + cl_fixture_cleanup("tmp_global_path"); cl_git_sandbox_cleanup(); } @@ -66,6 +68,21 @@ void test_clone_empty__can_clone_an_empty_local_repo_barely(void) expected_tracked_branch_name)); } +void test_clone_empty__respects_initialbranch_config(void) +{ + git_buf buf = GIT_BUF_INIT; + + create_tmp_global_config("tmp_global_path", "init.defaultbranch", "my_default_branch"); + + cl_set_cleanup(&cleanup_repository, "./empty"); + + g_options.bare = true; + cl_git_pass(git_clone(&g_repo_cloned, "./empty_bare.git", "./empty", &g_options)); + cl_git_pass(git_branch_upstream_name(&buf, g_repo_cloned, "refs/heads/my_default_branch")); + cl_assert_equal_s("refs/remotes/origin/my_default_branch", buf.ptr); + git_buf_dispose(&buf); +} + void test_clone_empty__can_clone_an_empty_local_repo(void) { cl_set_cleanup(&cleanup_repository, "./empty"); |