Skip to content

Commit c41871e

Browse files
committed
fetch: test when HEAD points to nonexistent branch
When HEAD points to a nonexistent or invalid branch - for example, to `refs/heads/` - the fetch should be permitted to continue, but we should not use it when creating the `for merge` option in the FETCH_HEAD file. (This emulates git's behavior.)
1 parent c31032a commit c41871e

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

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");

0 commit comments

Comments
 (0)