Skip to content

Commit 471ed79

Browse files
committed
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.
1 parent 84d2a03 commit 471ed79

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

src/clone.c

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,31 @@ static int update_head_to_new_branch(
137137
return error;
138138
}
139139

140+
static int update_head_to_default(git_repository *repo)
141+
{
142+
git_buf initialbranch = GIT_BUF_INIT;
143+
const char *branch_name;
144+
int error = 0;
145+
146+
if ((error = git_repository_initialbranch(&initialbranch, repo)) < 0)
147+
goto done;
148+
149+
if (git__prefixcmp(initialbranch.ptr, GIT_REFS_HEADS_DIR) != 0) {
150+
git_error_set(GIT_ERROR_INVALID, "invalid initial branch '%s'", initialbranch.ptr);
151+
error = -1;
152+
goto done;
153+
}
154+
155+
branch_name = initialbranch.ptr + strlen(GIT_REFS_HEADS_DIR);
156+
157+
error = setup_tracking_config(repo, branch_name, GIT_REMOTE_ORIGIN,
158+
initialbranch.ptr);
159+
160+
done:
161+
git_buf_dispose(&initialbranch);
162+
return error;
163+
}
164+
140165
static int update_head_to_remote(
141166
git_repository *repo,
142167
git_remote *remote,
@@ -155,8 +180,7 @@ static int update_head_to_remote(
155180

156181
/* We cloned an empty repository or one with an unborn HEAD */
157182
if (refs_len == 0 || strcmp(refs[0]->name, GIT_HEAD_FILE))
158-
return setup_tracking_config(
159-
repo, "master", GIT_REMOTE_ORIGIN, GIT_REFS_HEADS_MASTER_FILE);
183+
return update_head_to_default(repo);
160184

161185
/* We know we have HEAD, let's see where it points */
162186
remote_head = refs[0];

tests/clone/empty.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include "git2/clone.h"
44
#include "repository.h"
5+
#include "repo/repo_helpers.h"
56

67
static git_clone_options g_options;
78
static git_repository *g_repo;
@@ -22,6 +23,7 @@ void test_clone_empty__initialize(void)
2223

2324
void test_clone_empty__cleanup(void)
2425
{
26+
cl_fixture_cleanup("tmp_global_path");
2527
cl_git_sandbox_cleanup();
2628
}
2729

@@ -66,6 +68,21 @@ void test_clone_empty__can_clone_an_empty_local_repo_barely(void)
6668
expected_tracked_branch_name));
6769
}
6870

71+
void test_clone_empty__respects_initialbranch_config(void)
72+
{
73+
git_buf buf = GIT_BUF_INIT;
74+
75+
create_tmp_global_config("tmp_global_path", "init.defaultbranch", "my_default_branch");
76+
77+
cl_set_cleanup(&cleanup_repository, "./empty");
78+
79+
g_options.bare = true;
80+
cl_git_pass(git_clone(&g_repo_cloned, "./empty_bare.git", "./empty", &g_options));
81+
cl_git_pass(git_branch_upstream_name(&buf, g_repo_cloned, "refs/heads/my_default_branch"));
82+
cl_assert_equal_s("refs/remotes/origin/my_default_branch", buf.ptr);
83+
git_buf_dispose(&buf);
84+
}
85+
6986
void test_clone_empty__can_clone_an_empty_local_repo(void)
7087
{
7188
cl_set_cleanup(&cleanup_repository, "./empty");

0 commit comments

Comments
 (0)