Skip to content

Commit 8462f7f

Browse files
authored
Merge pull request libgit2#5770 from libgit2/ethomson/empty_default_branch
Cope with empty default branch
2 parents ba6824d + 3f4bc21 commit 8462f7f

File tree

4 files changed

+57
-1
lines changed

4 files changed

+57
-1
lines changed

src/remote.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1141,6 +1141,16 @@ static int remote_head_for_ref(git_remote_head **out, git_remote *remote, git_re
11411141
ref_name = git_reference_name(resolved_ref);
11421142
}
11431143

1144+
/*
1145+
* The ref name may be unresolvable - perhaps it's pointing to
1146+
* something invalid. In this case, there is no remote head for
1147+
* this ref.
1148+
*/
1149+
if (!ref_name) {
1150+
error = 0;
1151+
goto cleanup;
1152+
}
1153+
11441154
if ((error = ref_to_update(&update, &remote_name, remote, spec, ref_name)) < 0)
11451155
goto cleanup;
11461156

src/repository.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2092,7 +2092,8 @@ static int repo_init_head(const char *repo_dir, const char *given)
20922092
if (given) {
20932093
initial_head = given;
20942094
} else if ((error = git_config_open_default(&cfg)) >= 0 &&
2095-
(error = git_config_get_string_buf(&cfg_branch, cfg, "init.defaultbranch")) >= 0) {
2095+
(error = git_config_get_string_buf(&cfg_branch, cfg, "init.defaultbranch")) >= 0 &&
2096+
*cfg_branch.ptr) {
20962097
initial_head = cfg_branch.ptr;
20972098
}
20982099

tests/fetchhead/nonetwork.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,16 @@ static int assert_master_for_merge(const char *ref, const char *url, const git_o
319319
return 0;
320320
}
321321

322+
static int assert_none_for_merge(const char *ref, const char *url, const git_oid *id, unsigned int is_merge, void *data)
323+
{
324+
GIT_UNUSED(ref);
325+
GIT_UNUSED(url);
326+
GIT_UNUSED(id);
327+
GIT_UNUSED(data);
328+
329+
return is_merge ? -1 : 0;
330+
}
331+
322332
void test_fetchhead_nonetwork__unborn_with_upstream(void)
323333
{
324334
git_repository *repo;
@@ -366,6 +376,25 @@ void test_fetchhead_nonetwork__fetch_into_repo_with_symrefs(void)
366376
cl_git_sandbox_cleanup();
367377
}
368378

379+
void test_fetchhead_nonetwork__fetch_into_repo_with_invalid_head(void)
380+
{
381+
git_remote *remote;
382+
char *strings[] = { "refs/heads/*:refs/remotes/origin/*" };
383+
git_strarray refspecs = { strings, 1 };
384+
385+
cl_set_cleanup(&cleanup_repository, "./test1");
386+
cl_git_pass(git_repository_init(&g_repo, "./test1", 0));
387+
388+
/* HEAD pointing to nonexistent branch */
389+
cl_git_rewritefile("./test1/.git/HEAD", "ref: refs/heads/\n");
390+
391+
cl_git_pass(git_remote_create_anonymous(&remote, g_repo, cl_fixture("testrepo.git")));
392+
cl_git_pass(git_remote_fetch(remote, &refspecs, NULL, NULL));
393+
cl_git_pass(git_repository_fetchhead_foreach(g_repo, assert_none_for_merge, NULL));
394+
395+
git_remote_free(remote);
396+
}
397+
369398
void test_fetchhead_nonetwork__quote_in_branch_name(void)
370399
{
371400
cl_set_cleanup(&cleanup_repository, "./test1");

tests/repo/init.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -688,3 +688,19 @@ void test_repo_init__defaultbranch_config(void)
688688

689689
git_reference_free(head);
690690
}
691+
692+
void test_repo_init__defaultbranch_config_empty(void)
693+
{
694+
git_reference *head;
695+
696+
cl_set_cleanup(&cleanup_repository, "repo");
697+
698+
create_tmp_global_config("tmp_global_path", "init.defaultbranch", "");
699+
700+
cl_git_pass(git_repository_init(&g_repo, "repo", 0));
701+
cl_git_pass(git_reference_lookup(&head, g_repo, "HEAD"));
702+
703+
cl_assert_equal_s("refs/heads/master", git_reference_symbolic_target(head));
704+
705+
git_reference_free(head);
706+
}

0 commit comments

Comments
 (0)