Skip to content

Commit a9a7967

Browse files
committed
fetch: support OID refspec without dst
Support the ability to create a refspec that is a single object ID without a destination.
1 parent 8420ac4 commit a9a7967

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

src/libgit2/fetch.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,11 @@ static int maybe_want_oid(git_remote *remote, git_refspec *spec)
7676
GIT_ERROR_CHECK_ALLOC(oid_head);
7777

7878
git_oid_fromstr(&oid_head->oid, spec->src);
79-
oid_head->name = git__strdup(spec->dst);
80-
GIT_ERROR_CHECK_ALLOC(oid_head->name);
79+
80+
if (spec->dst) {
81+
oid_head->name = git__strdup(spec->dst);
82+
GIT_ERROR_CHECK_ALLOC(oid_head->name);
83+
}
8184

8285
if (git_vector_insert(&remote->local_heads, oid_head) < 0 ||
8386
git_vector_insert(&remote->refs, oid_head) < 0)

src/libgit2/remote.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1895,8 +1895,11 @@ static int update_tips_for_spec(
18951895
if (git_oid__is_hexstr(spec->src)) {
18961896
git_oid id;
18971897

1898-
if ((error = git_oid_fromstr(&id, spec->src)) < 0 ||
1899-
(error = update_ref(remote, spec->dst, &id, log_message, callbacks)) < 0)
1898+
if ((error = git_oid_fromstr(&id, spec->src)) < 0)
1899+
goto on_error;
1900+
1901+
if (spec->dst &&
1902+
(error = update_ref(remote, spec->dst, &id, log_message, callbacks)) < 0)
19001903
goto on_error;
19011904

19021905
git_oid_cpy(&oid_head.oid, &id);

tests/libgit2/online/fetch.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,3 +321,32 @@ void test_online_fetch__reachable_commit(void)
321321
git_object_free(obj);
322322
git_remote_free(remote);
323323
}
324+
325+
void test_online_fetch__reachable_commit_without_destination(void)
326+
{
327+
git_remote *remote;
328+
git_strarray refspecs;
329+
git_object *obj;
330+
git_oid expected_id;
331+
git_str fetchhead = GIT_STR_INIT;
332+
char *refspec = "2c349335b7f797072cf729c4f3bb0914ecb6dec9";
333+
334+
refspecs.strings = &refspec;
335+
refspecs.count = 1;
336+
337+
git_oid_fromstr(&expected_id, "2c349335b7f797072cf729c4f3bb0914ecb6dec9");
338+
339+
cl_git_pass(git_remote_create(&remote, _repo, "test",
340+
"https://github.com/libgit2/TestGitRepository"));
341+
cl_git_pass(git_remote_fetch(remote, &refspecs, NULL, NULL));
342+
343+
cl_git_fail_with(GIT_ENOTFOUND, git_revparse_single(&obj, _repo, "refs/success"));
344+
345+
cl_git_pass(git_futils_readbuffer(&fetchhead, "./fetch/.git/FETCH_HEAD"));
346+
cl_assert_equal_s(fetchhead.ptr,
347+
"2c349335b7f797072cf729c4f3bb0914ecb6dec9\t\t'2c349335b7f797072cf729c4f3bb0914ecb6dec9' of https://github.com/libgit2/TestGitRepository\n");
348+
349+
git_str_dispose(&fetchhead);
350+
git_object_free(obj);
351+
git_remote_free(remote);
352+
}

0 commit comments

Comments
 (0)