remote-curl: don't pass back fake refs

When receive-pack advertises its list of refs, it generally hides the capabilities information after a NUL at the end of the first ref. However, when we have an empty repository, there are no refs, and therefore receive-pack writes a fake ref "capabilities^{}" with the capabilities afterwards. On the client side, git reads the result with get_remote_heads(). We pick the capabilities from the end of the line, and then call check_ref() to make sure the ref name is valid. We see that it isn't, and don't bother adding it to our list of refs. However, the call to check_ref() is enabled by passing the REF_NORMAL flag to get_remote_heads. For the regular git transport, we pass REF_NORMAL in get_refs_via_connect() if we are doing a push (since only receive-pack uses this fake ref). But in remote-curl, we never use this flag, and we accept the fake ref as a real one, passing it back from the helper to the parent git-push. Most of the time this bug goes unnoticed, as the fake ref won't match our refspecs. However, if "--mirror" is used, then we see it as remote cruft to be pruned, and try to pass along a deletion refspec for it. Of course this refspec has bogus syntax (because of the ^{}), and the helper complains, aborting the push. Let's have remote-curl mirror what the builtin get_refs_via_connect() does (at least for the case of using git protocol; we can leave the dumb info/refs reader as it is). This also fixes pushing with --mirror to a smart-http remote that uses alternates. The fake ".have" refs the server gives to avoid unnecessary network transfer has a similar bad interactions with the machinery. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Dec 17, 2011 at 05:45 UTC 02f7914734eb8360cf314d887931c5d5ad0d8046
2 files changed +36 -3
remote-curl.c
+4 -3
@@ -188,7 +188,7 @@ static int write_discovery(int in, int out, void *data)
188 return err;
189 }
190
191 -static struct ref *parse_git_refs(struct discovery *heads)
191 +static struct ref *parse_git_refs(struct discovery *heads, int for_push)
192 {
193 struct ref *list = NULL;
194 struct async async;
@@ -200,7 +200,8 @@ static struct ref *parse_git_refs(struct discovery *heads)
200
201 if (start_async(&async))
202 die("cannot start thread to parse advertised refs");
203 - get_remote_heads(async.out, &list, 0, NULL, 0, NULL);
203 + get_remote_heads(async.out, &list, 0, NULL,
204 + for_push ? REF_NORMAL : 0, NULL);
205 close(async.out);
206 if (finish_async(&async))
207 die("ref parsing thread failed");
@@ -268,7 +269,7 @@ static struct ref *get_refs(int for_push)
269 heads = discover_refs("git-upload-pack");
270
271 if (heads->proto_git)
271 - return parse_git_refs(heads);
272 + return parse_git_refs(heads, for_push);
273 return parse_info_refs(heads);
274 }
275
t/t5541-http-push.sh
+32
@@ -154,5 +154,37 @@ test_expect_success 'push (chunked)' '
154 test $HEAD = $(git rev-parse --verify HEAD))
155 '
156
157 +test_expect_success 'push --all can push to empty repo' '
158 + d=$HTTPD_DOCUMENT_ROOT_PATH/empty-all.git &&
159 + git init --bare "$d" &&
160 + git --git-dir="$d" config http.receivepack true &&
161 + git push --all "$HTTPD_URL"/smart/empty-all.git
162 +'
163 +
164 +test_expect_success 'push --mirror can push to empty repo' '
165 + d=$HTTPD_DOCUMENT_ROOT_PATH/empty-mirror.git &&
166 + git init --bare "$d" &&
167 + git --git-dir="$d" config http.receivepack true &&
168 + git push --mirror "$HTTPD_URL"/smart/empty-mirror.git
169 +'
170 +
171 +test_expect_success 'push --all to repo with alternates' '
172 + s=$HTTPD_DOCUMENT_ROOT_PATH/test_repo.git &&
173 + d=$HTTPD_DOCUMENT_ROOT_PATH/alternates-all.git &&
174 + git clone --bare --shared "$s" "$d" &&
175 + git --git-dir="$d" config http.receivepack true &&
176 + git --git-dir="$d" repack -adl &&
177 + git push --all "$HTTPD_URL"/smart/alternates-all.git
178 +'
179 +
180 +test_expect_success 'push --mirror to repo with alternates' '
181 + s=$HTTPD_DOCUMENT_ROOT_PATH/test_repo.git &&
182 + d=$HTTPD_DOCUMENT_ROOT_PATH/alternates-mirror.git &&
183 + git clone --bare --shared "$s" "$d" &&
184 + git --git-dir="$d" config http.receivepack true &&
185 + git --git-dir="$d" repack -adl &&
186 + git push --mirror "$HTTPD_URL"/smart/alternates-mirror.git
187 +'
188 +
189 stop_httpd
190 test_done