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 <gitster@pobox.com>

Junio C Hamano committed Sep 17, 2013 at 19:10 UTC a45b5f0552eec3c8800edae44e071cf1d647bf96
2 files changed +64 -11
connect.c
+60
@@ -6,6 +6,7 @@
6 #include "run-command.h"
7 #include "remote.h"
8 #include "url.h"
9 +#include "string-list.h"
10
11 static char *server_capabilities;
12 static const char *parse_feature_value(const char *, const char *, int *);
@@ -60,6 +61,61 @@ static void die_initial_contact(int got_at_least_one_head)
61 "and the repository exists.");
62 }
63
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 +
119 /*
120 * Read all the refs from the other end
121 */
@@ -67,6 +123,7 @@ struct ref **get_remote_heads(int in, char *src_buf, size_t src_len,
123 struct ref **list, unsigned int flags,
124 struct extra_have_objects *extra_have)
125 {
126 + struct ref **orig_list = list;
127 int got_at_least_one_head = 0;
128
129 *list = NULL;
@@ -114,6 +171,9 @@ struct ref **get_remote_heads(int in, char *src_buf, size_t src_len,
171 list = &ref->next;
172 got_at_least_one_head = 1;
173 }
174 +
175 + annotate_refs_with_symref_info(*orig_list);
176 +
177 return list;
178 }
179
t/t5505-remote.sh
+4 -11
@@ -160,9 +160,7 @@ cat >test/expect <<EOF
160 * remote two
161 Fetch URL: ../two
162 Push URL: ../three
163 - HEAD branch (remote HEAD is ambiguous, may be one of the following):
164 - another
165 - master
163 + HEAD branch: master
164 Local refs configured for 'git push':
165 ahead forces to master (fast-forwardable)
166 master pushes to another (up to date)
@@ -262,17 +260,12 @@ test_expect_success 'set-head --auto' '
260 )
261 '
262
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' '
264 (
265 cd test &&
266 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 &&
269 test_i18ncmp expect output
270 )
271 '