From 80ffeb94f4c9eae4e099fec47e39105fc1e19132 Mon Sep 17 00:00:00 2001 From: Jiang Xin Date: Wed, 11 Nov 2020 19:32:02 +0800 Subject: receive-pack: use default version 0 for proc-receive In the verison negotiation phase between "receive-pack" and "proc-receive", "proc-receive" can send an empty flush-pkt to end the negotiation and use default version 0. Capabilities (such as "push-options") are not supported in version 0. Signed-off-by: Jiang Xin Signed-off-by: Junio C Hamano diff --git a/builtin/receive-pack.c b/builtin/receive-pack.c index 2bd7365..f1f0f7b 100644 --- a/builtin/receive-pack.c +++ b/builtin/receive-pack.c @@ -1192,7 +1192,12 @@ static int run_proc_receive_hook(struct command *commands, goto cleanup; } - if (version != 1) { + switch (version) { + case 0: + /* fallthrough */ + case 1: + break; + default: strbuf_addf(&errmsg, "proc-receive version '%d' is not supported", version); code = -1; diff --git a/t/helper/test-proc-receive.c b/t/helper/test-proc-receive.c index 6652ced..cc08506 100644 --- a/t/helper/test-proc-receive.c +++ b/t/helper/test-proc-receive.c @@ -45,8 +45,14 @@ static void proc_receive_verison(struct packet_reader *reader) { if (packet_reader_read(reader) != PACKET_READ_NORMAL) break; + /* Ignore version negotiation for version 0 */ + if (version == 0) + continue; + if (reader->pktlen > 8 && starts_with(reader->line, "version=")) { server_version = atoi(reader->line+8); + if (server_version != 1) + die("bad protocol version: %d", server_version); linelen = strlen(reader->line); if (linelen < reader->pktlen) { const char *feature_list = reader->line + linelen + 1; @@ -58,15 +64,13 @@ static void proc_receive_verison(struct packet_reader *reader) { } } - if (server_version != 1) - die("bad protocol version: %d", server_version); - if (die_write_version) die("die with the --die-write-version option"); - packet_write_fmt(1, "version=%d%c%s\n", - version, '\0', - use_push_options && !no_push_options ? "push-options": ""); + if (version != 0) + packet_write_fmt(1, "version=%d%c%s\n", + version, '\0', + use_push_options && !no_push_options ? "push-options": ""); packet_flush(1); } diff --git a/t/t5411/test-0026-push-options.sh b/t/t5411/test-0026-push-options.sh index d414be8..e88edb1 100644 --- a/t/t5411/test-0026-push-options.sh +++ b/t/t5411/test-0026-push-options.sh @@ -32,6 +32,66 @@ test_expect_success "enable push options ($PROTOCOL)" ' git -C "$upstream" config receive.advertisePushOptions true ' +test_expect_success "setup version=0 for proc-receive hook ($PROTOCOL)" ' + write_script "$upstream/hooks/proc-receive" <<-EOF + printf >&2 "# proc-receive hook\n" + test-tool proc-receive -v \ + --version 0 \ + -r "ok refs/for/main/topic" + EOF +' + +# Refs of upstream : main(A) +# Refs of workbench: main(A) tags/v123 +# git push -o ... : next(A) refs/for/main/topic +test_expect_success "proc-receive: ignore push-options for version 0 ($PROTOCOL)" ' + git -C workbench push \ + --atomic \ + -o issue=123 \ + -o reviewer=user1 \ + origin \ + HEAD:refs/heads/next \ + HEAD:refs/for/main/topic \ + >out 2>&1 && + make_user_friendly_and_stable_output actual && + cat >expect <<-EOF && + remote: # pre-receive hook + remote: pre-receive< refs/heads/next + remote: pre-receive< refs/for/main/topic + remote: # proc-receive hook + remote: proc-receive< refs/for/main/topic + remote: proc-receive> ok refs/for/main/topic + remote: # post-receive hook + remote: post-receive< refs/heads/next + remote: post-receive< refs/for/main/topic + To + * [new branch] HEAD -> next + * [new reference] HEAD -> refs/for/main/topic + EOF + test_cmp expect actual && + git -C "$upstream" show-ref >out && + make_user_friendly_and_stable_output actual && + cat >expect <<-EOF && + refs/heads/main + refs/heads/next + EOF + test_cmp expect actual +' + +test_expect_success "restore proc-receive hook ($PROTOCOL)" ' + write_script "$upstream/hooks/proc-receive" <<-EOF + printf >&2 "# proc-receive hook\n" + test-tool proc-receive -v \ + -r "ok refs/for/main/topic" + EOF +' + +# Refs of upstream : main(A) next(A) +# Refs of workbench: main(A) tags/v123 +test_expect_success "cleanup ($PROTOCOL)" ' + git -C "$upstream" update-ref -d refs/heads/next +' + # Refs of upstream : main(A) # Refs of workbench: main(A) tags/v123 # git push -o ... : next(A) refs/for/main/topic diff --git a/t/t5411/test-0027-push-options--porcelain.sh b/t/t5411/test-0027-push-options--porcelain.sh index d5d0dcb..3a6561b 100644 --- a/t/t5411/test-0027-push-options--porcelain.sh +++ b/t/t5411/test-0027-push-options--porcelain.sh @@ -33,6 +33,68 @@ test_expect_success "enable push options ($PROTOCOL/porcelain)" ' git -C "$upstream" config receive.advertisePushOptions true ' +test_expect_success "setup version=0 for proc-receive hook ($PROTOCOL/porcelain)" ' + write_script "$upstream/hooks/proc-receive" <<-EOF + printf >&2 "# proc-receive hook\n" + test-tool proc-receive -v \ + --version 0 \ + -r "ok refs/for/main/topic" + EOF +' + +# Refs of upstream : main(A) +# Refs of workbench: main(A) tags/v123 +# git push -o ... : next(A) refs/for/main/topic +test_expect_success "proc-receive: ignore push-options for version 0 ($PROTOCOL/porcelain)" ' + git -C workbench push \ + --porcelain \ + --atomic \ + -o issue=123 \ + -o reviewer=user1 \ + origin \ + HEAD:refs/heads/next \ + HEAD:refs/for/main/topic \ + >out 2>&1 && + make_user_friendly_and_stable_output actual && + cat >expect <<-EOF && + remote: # pre-receive hook + remote: pre-receive< refs/heads/next + remote: pre-receive< refs/for/main/topic + remote: # proc-receive hook + remote: proc-receive< refs/for/main/topic + remote: proc-receive> ok refs/for/main/topic + remote: # post-receive hook + remote: post-receive< refs/heads/next + remote: post-receive< refs/for/main/topic + To + * HEAD:refs/heads/next [new branch] + * HEAD:refs/for/main/topic [new reference] + Done + EOF + test_cmp expect actual && + git -C "$upstream" show-ref >out && + make_user_friendly_and_stable_output actual && + cat >expect <<-EOF && + refs/heads/main + refs/heads/next + EOF + test_cmp expect actual +' + +test_expect_success "restore proc-receive hook ($PROTOCOL/porcelain)" ' + write_script "$upstream/hooks/proc-receive" <<-EOF + printf >&2 "# proc-receive hook\n" + test-tool proc-receive -v \ + -r "ok refs/for/main/topic" + EOF +' + +# Refs of upstream : main(A) next(A) +# Refs of workbench: main(A) tags/v123 +test_expect_success "cleanup ($PROTOCOL/porcelain)" ' + git -C "$upstream" update-ref -d refs/heads/next +' + # Refs of upstream : main(A) # Refs of workbench: main(A) tags/v123 # git push -o ... : next(A) refs/for/main/topic -- cgit v0.10.2-6-g49f6