send-pack: fix push nego. when remote has refs

Commit 477673d6f3 ("send-pack: support push negotiation", 2021-05-05) did not test the case in which a remote advertises at least one ref. In such a case, "remote_refs" in get_commons_through_negotiation() in send-pack.c would also contain those refs with a zero ref->new_oid (in addition to the refs being pushed with a nonzero ref->new_oid). Passing them as negotiation tips to "git fetch" causes an error, so filter them out. (The exact error that would happen in "git fetch" in this case is a segmentation fault, which is unwanted. This will be fixed in the subsequent commit.) Signed-off-by: Jonathan Tan <jonathantanmy@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jonathan Tan committed Jul 15, 2021 at 10:44 UTC 54a03bc7d9a7f264d511d88166afe8da7f75e90a
3 files changed +8 -3
send-pack.c
+4 -2
@@ -425,8 +425,10 @@ static void get_commons_through_negotiation(const char *url,
425 child.no_stdin = 1;
426 child.out = -1;
427 strvec_pushl(&child.args, "fetch", "--negotiate-only", NULL);
428 - for (ref = remote_refs; ref; ref = ref->next)
429 - strvec_pushf(&child.args, "--negotiation-tip=%s", oid_to_hex(&ref->new_oid));
428 + for (ref = remote_refs; ref; ref = ref->next) {
429 + if (!is_null_oid(&ref->new_oid))
430 + strvec_pushf(&child.args, "--negotiation-tip=%s", oid_to_hex(&ref->new_oid));
431 + }
432 strvec_push(&child.args, url);
433
434 if (start_command(&child))
t/t5516-fetch-push.sh
+3 -1
@@ -201,6 +201,7 @@ test_expect_success 'push with negotiation' '
201 # Without negotiation
202 mk_empty testrepo &&
203 git push testrepo $the_first_commit:refs/remotes/origin/first_commit &&
204 + test_commit -C testrepo unrelated_commit &&
205 git -C testrepo config receive.hideRefs refs/remotes/origin/first_commit &&
206 echo now pushing without negotiation &&
207 GIT_TRACE2_EVENT="$(pwd)/event" git -c protocol.version=2 push testrepo refs/heads/main:refs/remotes/origin/main &&
@@ -210,6 +211,7 @@ test_expect_success 'push with negotiation' '
211 rm event &&
212 mk_empty testrepo &&
213 git push testrepo $the_first_commit:refs/remotes/origin/first_commit &&
214 + test_commit -C testrepo unrelated_commit &&
215 git -C testrepo config receive.hideRefs refs/remotes/origin/first_commit &&
216 GIT_TRACE2_EVENT="$(pwd)/event" git -c protocol.version=2 -c push.negotiate=1 push testrepo refs/heads/main:refs/remotes/origin/main &&
217 grep_wrote 2 event # 1 commit, 1 tree
@@ -219,6 +221,7 @@ test_expect_success 'push with negotiation proceeds anyway even if negotiation f
221 rm event &&
222 mk_empty testrepo &&
223 git push testrepo $the_first_commit:refs/remotes/origin/first_commit &&
224 + test_commit -C testrepo unrelated_commit &&
225 git -C testrepo config receive.hideRefs refs/remotes/origin/first_commit &&
226 GIT_TEST_PROTOCOL_VERSION=0 GIT_TRACE2_EVENT="$(pwd)/event" \
227 git -c push.negotiate=1 push testrepo refs/heads/main:refs/remotes/origin/main 2>err &&
@@ -1767,5 +1770,4 @@ test_expect_success 'denyCurrentBranch and worktrees' '
1770 git -C cloned push origin HEAD:new-wt &&
1771 test_must_fail git -C cloned push --delete origin new-wt
1772 '
1770 -
1773 test_done
t/t5549-fetch-push-http.sh
+1
@@ -27,6 +27,7 @@ setup_client_and_server () {
27 git init "$SERVER" &&
28 test_when_finished 'rm -rf "$SERVER"' &&
29 test_config -C "$SERVER" http.receivepack true &&
30 + test_commit -C "$SERVER" unrelated_commit &&
31 git -C client push "$URI" first_commit:refs/remotes/origin/first_commit &&
32 git -C "$SERVER" config receive.hideRefs refs/remotes/origin/first_commit
33 }