Skip to content

Commit 54a03bc

Browse files
jonathantanmygitster
authored andcommitted
send-pack: fix push nego. when remote has refs
Commit 477673d ("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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 74fab8f commit 54a03bc

File tree

3 files changed

+8
-3
lines changed

3 files changed

+8
-3
lines changed

send-pack.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -425,8 +425,10 @@ static void get_commons_through_negotiation(const char *url,
425425
child.no_stdin = 1;
426426
child.out = -1;
427427
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+
}
430432
strvec_push(&child.args, url);
431433

432434
if (start_command(&child))

t/t5516-fetch-push.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ test_expect_success 'push with negotiation' '
201201
# Without negotiation
202202
mk_empty testrepo &&
203203
git push testrepo $the_first_commit:refs/remotes/origin/first_commit &&
204+
test_commit -C testrepo unrelated_commit &&
204205
git -C testrepo config receive.hideRefs refs/remotes/origin/first_commit &&
205206
echo now pushing without negotiation &&
206207
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' '
210211
rm event &&
211212
mk_empty testrepo &&
212213
git push testrepo $the_first_commit:refs/remotes/origin/first_commit &&
214+
test_commit -C testrepo unrelated_commit &&
213215
git -C testrepo config receive.hideRefs refs/remotes/origin/first_commit &&
214216
GIT_TRACE2_EVENT="$(pwd)/event" git -c protocol.version=2 -c push.negotiate=1 push testrepo refs/heads/main:refs/remotes/origin/main &&
215217
grep_wrote 2 event # 1 commit, 1 tree
@@ -219,6 +221,7 @@ test_expect_success 'push with negotiation proceeds anyway even if negotiation f
219221
rm event &&
220222
mk_empty testrepo &&
221223
git push testrepo $the_first_commit:refs/remotes/origin/first_commit &&
224+
test_commit -C testrepo unrelated_commit &&
222225
git -C testrepo config receive.hideRefs refs/remotes/origin/first_commit &&
223226
GIT_TEST_PROTOCOL_VERSION=0 GIT_TRACE2_EVENT="$(pwd)/event" \
224227
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' '
17671770
git -C cloned push origin HEAD:new-wt &&
17681771
test_must_fail git -C cloned push --delete origin new-wt
17691772
'
1770-
17711773
test_done

t/t5549-fetch-push-http.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ setup_client_and_server () {
2727
git init "$SERVER" &&
2828
test_when_finished 'rm -rf "$SERVER"' &&
2929
test_config -C "$SERVER" http.receivepack true &&
30+
test_commit -C "$SERVER" unrelated_commit &&
3031
git -C client push "$URI" first_commit:refs/remotes/origin/first_commit &&
3132
git -C "$SERVER" config receive.hideRefs refs/remotes/origin/first_commit
3233
}

0 commit comments

Comments
 (0)