Skip to content

Commit a45b5f0

Browse files
committed
connect: annotate refs with their symref information in get_remote_head()
By doing this, clients of upload-pack can now reliably tell what ref a symbolic ref points at; the updated test in t5505 used to expect failure due to the ambiguity and made sure we give diagnostics, but we no longer need to be so pessimistic. Make sure we correctly learn which branch HEAD points at from the other side instead. Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5d54cff commit a45b5f0

File tree

2 files changed

+64
-11
lines changed

2 files changed

+64
-11
lines changed

connect.c

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "run-command.h"
77
#include "remote.h"
88
#include "url.h"
9+
#include "string-list.h"
910

1011
static char *server_capabilities;
1112
static const char *parse_feature_value(const char *, const char *, int *);
@@ -60,13 +61,69 @@ static void die_initial_contact(int got_at_least_one_head)
6061
"and the repository exists.");
6162
}
6263

64+
static void parse_one_symref_info(struct string_list *symref, const char *val, int len)
65+
{
66+
char *sym, *target;
67+
struct string_list_item *item;
68+
69+
if (!len)
70+
return; /* just "symref" */
71+
/* e.g. "symref=HEAD:refs/heads/master" */
72+
sym = xmalloc(len + 1);
73+
memcpy(sym, val, len);
74+
sym[len] = '\0';
75+
target = strchr(sym, ':');
76+
if (!target)
77+
/* just "symref=something" */
78+
goto reject;
79+
*(target++) = '\0';
80+
if (check_refname_format(sym, REFNAME_ALLOW_ONELEVEL) ||
81+
check_refname_format(target, REFNAME_ALLOW_ONELEVEL))
82+
/* "symref=bogus:pair */
83+
goto reject;
84+
item = string_list_append(symref, sym);
85+
item->util = target;
86+
return;
87+
reject:
88+
free(sym);
89+
return;
90+
}
91+
92+
static void annotate_refs_with_symref_info(struct ref *ref)
93+
{
94+
struct string_list symref = STRING_LIST_INIT_DUP;
95+
const char *feature_list = server_capabilities;
96+
97+
while (feature_list) {
98+
int len;
99+
const char *val;
100+
101+
val = parse_feature_value(feature_list, "symref", &len);
102+
if (!val)
103+
break;
104+
parse_one_symref_info(&symref, val, len);
105+
feature_list = val + 1;
106+
}
107+
sort_string_list(&symref);
108+
109+
for (; ref; ref = ref->next) {
110+
struct string_list_item *item;
111+
item = string_list_lookup(&symref, ref->name);
112+
if (!item)
113+
continue;
114+
ref->symref = xstrdup((char *)item->util);
115+
}
116+
string_list_clear(&symref, 0);
117+
}
118+
63119
/*
64120
* Read all the refs from the other end
65121
*/
66122
struct ref **get_remote_heads(int in, char *src_buf, size_t src_len,
67123
struct ref **list, unsigned int flags,
68124
struct extra_have_objects *extra_have)
69125
{
126+
struct ref **orig_list = list;
70127
int got_at_least_one_head = 0;
71128

72129
*list = NULL;
@@ -114,6 +171,9 @@ struct ref **get_remote_heads(int in, char *src_buf, size_t src_len,
114171
list = &ref->next;
115172
got_at_least_one_head = 1;
116173
}
174+
175+
annotate_refs_with_symref_info(*orig_list);
176+
117177
return list;
118178
}
119179

t/t5505-remote.sh

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,7 @@ cat >test/expect <<EOF
160160
* remote two
161161
Fetch URL: ../two
162162
Push URL: ../three
163-
HEAD branch (remote HEAD is ambiguous, may be one of the following):
164-
another
165-
master
163+
HEAD branch: master
166164
Local refs configured for 'git push':
167165
ahead forces to master (fast-forwardable)
168166
master pushes to another (up to date)
@@ -262,17 +260,12 @@ test_expect_success 'set-head --auto' '
262260
)
263261
'
264262

265-
cat >test/expect <<\EOF
266-
error: Multiple remote HEAD branches. Please choose one explicitly with:
267-
git remote set-head two another
268-
git remote set-head two master
269-
EOF
270-
271-
test_expect_success 'set-head --auto fails w/multiple HEADs' '
263+
test_expect_success 'set-head --auto has no problem w/multiple HEADs' '
272264
(
273265
cd test &&
274266
git fetch two "refs/heads/*:refs/remotes/two/*" &&
275-
test_must_fail git remote set-head --auto two >output 2>&1 &&
267+
git remote set-head --auto two >output 2>&1 &&
268+
echo "two/HEAD set to master" >expect &&
276269
test_i18ncmp expect output
277270
)
278271
'

0 commit comments

Comments
 (0)