Skip to content

Commit 306475e

Browse files
committed
remote: expose the remote's symref mappings
Add a symref_target field to git_remote_head to expose the symref mappings to the user.
1 parent 8156835 commit 306475e

File tree

4 files changed

+43
-0
lines changed

4 files changed

+43
-0
lines changed

include/git2/net.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ struct git_remote_head {
4141
git_oid oid;
4242
git_oid loid;
4343
char *name;
44+
/**
45+
* If the server send a symref mapping for this ref, this will
46+
* point to the target.
47+
*/
48+
char *symref_target;
4449
};
4550

4651
/**

src/transports/smart.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,25 @@ int git_smart__update_heads(transport_smart *t, git_vector *symrefs)
7575
if (pkt->type != GIT_PKT_REF)
7676
continue;
7777

78+
if (symrefs) {
79+
git_refspec *spec;
80+
git_buf buf = GIT_BUF_INIT;
81+
size_t j;
82+
int error;
83+
84+
git_vector_foreach(symrefs, j, spec) {
85+
git_buf_clear(&buf);
86+
if (git_refspec_src_matches(spec, ref->head.name) &&
87+
!(error = git_refspec_transform(&buf, spec, ref->head.name)))
88+
ref->head.symref_target = git_buf_detach(&buf);
89+
}
90+
91+
git_buf_free(&buf);
92+
93+
if (error < 0)
94+
return error;
95+
}
96+
7897
if (git_vector_insert(&t->heads, &ref->head) < 0)
7998
return -1;
8099
}

src/transports/smart_pkt.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,7 @@ void git_pkt_free(git_pkt *pkt)
433433
if (pkt->type == GIT_PKT_REF) {
434434
git_pkt_ref *p = (git_pkt_ref *) pkt;
435435
git__free(p->head.name);
436+
git__free(p->head.symref_target);
436437
}
437438

438439
if (pkt->type == GIT_PKT_OK) {

tests/online/fetch.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,3 +184,21 @@ void test_online_fetch__ls_disconnected(void)
184184

185185
git_remote_free(remote);
186186
}
187+
188+
void test_online_fetch__remote_symrefs(void)
189+
{
190+
const git_remote_head **refs;
191+
size_t refs_len;
192+
git_remote *remote;
193+
194+
cl_git_pass(git_remote_create(&remote, _repo, "test",
195+
"http://github.com/libgit2/TestGitRepository.git"));
196+
cl_git_pass(git_remote_connect(remote, GIT_DIRECTION_FETCH));
197+
git_remote_disconnect(remote);
198+
cl_git_pass(git_remote_ls(&refs, &refs_len, remote));
199+
200+
cl_assert_equal_s("HEAD", refs[0]->name);
201+
cl_assert_equal_s("refs/heads/master", refs[0]->symref_target);
202+
203+
git_remote_free(remote);
204+
}

0 commit comments

Comments
 (0)