@matheusbsbs / matheus-bagliano / commits / 195d6eaea3
Fork 0

receive-pack: feed report options to post-receive

When commands are fed to the "post-receive" hook, report options will be parsed and the real old-oid, new-oid, reference name will feed to the "post-receive" hook. Signed-off-by: Jiang Xin <zhiyou.jx@alibaba-inc.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jiang Xin committed Aug 27, 2020 at 11:45 UTC 195d6eaea3b886fe13f422ce6675009ea1351224
7 files changed +58 -26
builtin/receive-pack.c
+26 -4
@@ -694,6 +694,7 @@ static void prepare_push_cert_sha1(struct child_process *proc)
694
695 struct receive_hook_feed_state {
696 struct command *cmd;
697 + struct ref_push_report *report;
698 int skip_broken;
699 struct strbuf buf;
700 const struct string_list *push_options;
@@ -781,11 +782,31 @@ static int feed_receive_hook(void *state_, const char **bufp, size_t *sizep)
782 cmd = cmd->next;
783 if (!cmd)
784 return -1; /* EOF */
785 + if (!bufp)
786 + return 0; /* OK, can feed something. */
787 strbuf_reset(&state->buf);
785 - strbuf_addf(&state->buf, "%s %s %s\n",
786 - oid_to_hex(&cmd->old_oid), oid_to_hex(&cmd->new_oid),
787 - cmd->ref_name);
788 - state->cmd = cmd->next;
788 + if (!state->report)
789 + state->report = cmd->report;
790 + if (state->report) {
791 + struct object_id *old_oid;
792 + struct object_id *new_oid;
793 + const char *ref_name;
794 +
795 + old_oid = state->report->old_oid ? state->report->old_oid : &cmd->old_oid;
796 + new_oid = state->report->new_oid ? state->report->new_oid : &cmd->new_oid;
797 + ref_name = state->report->ref_name ? state->report->ref_name : cmd->ref_name;
798 + strbuf_addf(&state->buf, "%s %s %s\n",
799 + oid_to_hex(old_oid), oid_to_hex(new_oid),
800 + ref_name);
801 + state->report = state->report->next;
802 + if (!state->report)
803 + state->cmd = cmd->next;
804 + } else {
805 + strbuf_addf(&state->buf, "%s %s %s\n",
806 + oid_to_hex(&cmd->old_oid), oid_to_hex(&cmd->new_oid),
807 + cmd->ref_name);
808 + state->cmd = cmd->next;
809 + }
810 if (bufp) {
811 *bufp = state->buf.buf;
812 *sizep = state->buf.len;
@@ -804,6 +825,7 @@ static int run_receive_hook(struct command *commands,
825 strbuf_init(&state.buf, 0);
826 state.cmd = commands;
827 state.skip_broken = skip_broken;
828 + state.report = NULL;
829 if (feed_receive_hook(&state, NULL, NULL))
830 return 0;
831 state.cmd = commands;
t/t5411/test-0032-report-with-options.sh
+7 -7
@@ -54,7 +54,7 @@ test_expect_success "proc-receive: report option refname ($PROTOCOL)" '
54 remote: proc-receive> ok refs/for/master/topic
55 remote: proc-receive> option refname refs/pull/123/head
56 remote: # post-receive hook
57 - remote: post-receive< <ZERO-OID> <COMMIT-A> refs/for/master/topic
57 + remote: post-receive< <ZERO-OID> <COMMIT-A> refs/pull/123/head
58 To <URL/of/upstream.git>
59 * [new reference] HEAD -> refs/for/master/topic
60 EOF
@@ -87,7 +87,7 @@ test_expect_success "proc-receive: report option refname and forced-update ($PRO
87 remote: proc-receive> option refname refs/pull/123/head
88 remote: proc-receive> option forced-update
89 remote: # post-receive hook
90 - remote: post-receive< <ZERO-OID> <COMMIT-A> refs/for/master/topic
90 + remote: post-receive< <ZERO-OID> <COMMIT-A> refs/pull/123/head
91 To <URL/of/upstream.git>
92 * [new reference] HEAD -> refs/for/master/topic
93 EOF
@@ -121,7 +121,7 @@ test_expect_success "proc-receive: report option refname and old-oid ($PROTOCOL)
121 remote: proc-receive> option refname refs/pull/123/head
122 remote: proc-receive> option old-oid <COMMIT-B>
123 remote: # post-receive hook
124 - remote: post-receive< <ZERO-OID> <COMMIT-A> refs/for/master/topic
124 + remote: post-receive< <COMMIT-B> <COMMIT-A> refs/pull/123/head
125 To <URL/of/upstream.git>
126 * [new reference] HEAD -> refs/for/master/topic
127 EOF
@@ -153,7 +153,7 @@ test_expect_success "proc-receive: report option old-oid ($PROTOCOL)" '
153 remote: proc-receive> ok refs/for/master/topic
154 remote: proc-receive> option old-oid <COMMIT-B>
155 remote: # post-receive hook
156 - remote: post-receive< <ZERO-OID> <COMMIT-A> refs/for/master/topic
156 + remote: post-receive< <COMMIT-B> <COMMIT-A> refs/for/master/topic
157 To <URL/of/upstream.git>
158 * [new reference] HEAD -> refs/for/master/topic
159 EOF
@@ -187,7 +187,7 @@ test_expect_success "proc-receive: report option old-oid and new-oid ($PROTOCOL)
187 remote: proc-receive> option old-oid <COMMIT-A>
188 remote: proc-receive> option new-oid <COMMIT-B>
189 remote: # post-receive hook
190 - remote: post-receive< <ZERO-OID> <COMMIT-A> refs/for/master/topic
190 + remote: post-receive< <COMMIT-A> <COMMIT-B> refs/for/master/topic
191 To <URL/of/upstream.git>
192 * [new reference] HEAD -> refs/for/master/topic
193 EOF
@@ -237,9 +237,9 @@ test_expect_success "proc-receive: report with multiple rewrites ($PROTOCOL)" '
237 remote: proc-receive> option forced-update
238 remote: proc-receive> option new-oid <COMMIT-A>
239 remote: # post-receive hook
240 - remote: post-receive< <ZERO-OID> <COMMIT-A> refs/for/next/topic
240 + remote: post-receive< <ZERO-OID> <COMMIT-A> refs/pull/123/head
241 remote: post-receive< <ZERO-OID> <COMMIT-A> refs/for/a/b/c/topic
242 - remote: post-receive< <ZERO-OID> <COMMIT-A> refs/for/master/topic
242 + remote: post-receive< <COMMIT-B> <COMMIT-A> refs/pull/124/head
243 To <URL/of/upstream.git>
244 * [new reference] HEAD -> refs/for/next/topic
245 * [new reference] HEAD -> refs/for/a/b/c/topic
t/t5411/test-0033-report-with-options--porcelain.sh
+7 -7
@@ -55,7 +55,7 @@ test_expect_success "proc-receive: report option refname ($PROTOCOL/porcelain)"
55 remote: proc-receive> ok refs/for/master/topic
56 remote: proc-receive> option refname refs/pull/123/head
57 remote: # post-receive hook
58 - remote: post-receive< <ZERO-OID> <COMMIT-A> refs/for/master/topic
58 + remote: post-receive< <ZERO-OID> <COMMIT-A> refs/pull/123/head
59 To <URL/of/upstream.git>
60 * HEAD:refs/for/master/topic [new reference]
61 Done
@@ -90,7 +90,7 @@ test_expect_success "proc-receive: report option refname and forced-update ($PRO
90 remote: proc-receive> option refname refs/pull/123/head
91 remote: proc-receive> option forced-update
92 remote: # post-receive hook
93 - remote: post-receive< <ZERO-OID> <COMMIT-A> refs/for/master/topic
93 + remote: post-receive< <ZERO-OID> <COMMIT-A> refs/pull/123/head
94 To <URL/of/upstream.git>
95 * HEAD:refs/for/master/topic [new reference]
96 Done
@@ -125,7 +125,7 @@ test_expect_success "proc-receive: report option refname and old-oid ($PROTOCOL/
125 remote: proc-receive> option refname refs/pull/123/head
126 remote: proc-receive> option old-oid <COMMIT-B>
127 remote: # post-receive hook
128 - remote: post-receive< <ZERO-OID> <COMMIT-A> refs/for/master/topic
128 + remote: post-receive< <COMMIT-B> <COMMIT-A> refs/pull/123/head
129 To <URL/of/upstream.git>
130 * HEAD:refs/for/master/topic [new reference]
131 Done
@@ -158,7 +158,7 @@ test_expect_success "proc-receive: report option old-oid ($PROTOCOL/porcelain)"
158 remote: proc-receive> ok refs/for/master/topic
159 remote: proc-receive> option old-oid <COMMIT-B>
160 remote: # post-receive hook
161 - remote: post-receive< <ZERO-OID> <COMMIT-A> refs/for/master/topic
161 + remote: post-receive< <COMMIT-B> <COMMIT-A> refs/for/master/topic
162 To <URL/of/upstream.git>
163 * HEAD:refs/for/master/topic [new reference]
164 Done
@@ -193,7 +193,7 @@ test_expect_success "proc-receive: report option old-oid and new-oid ($PROTOCOL/
193 remote: proc-receive> option old-oid <COMMIT-A>
194 remote: proc-receive> option new-oid <COMMIT-B>
195 remote: # post-receive hook
196 - remote: post-receive< <ZERO-OID> <COMMIT-A> refs/for/master/topic
196 + remote: post-receive< <COMMIT-A> <COMMIT-B> refs/for/master/topic
197 To <URL/of/upstream.git>
198 * HEAD:refs/for/master/topic [new reference]
199 Done
@@ -245,9 +245,9 @@ test_expect_success "proc-receive: report with multiple rewrites ($PROTOCOL/porc
245 remote: proc-receive> option forced-update
246 remote: proc-receive> option new-oid <COMMIT-A>
247 remote: # post-receive hook
248 - remote: post-receive< <ZERO-OID> <COMMIT-A> refs/for/next/topic
248 + remote: post-receive< <ZERO-OID> <COMMIT-A> refs/pull/123/head
249 remote: post-receive< <ZERO-OID> <COMMIT-A> refs/for/a/b/c/topic
250 - remote: post-receive< <ZERO-OID> <COMMIT-A> refs/for/master/topic
250 + remote: post-receive< <COMMIT-B> <COMMIT-A> refs/pull/124/head
251 To <URL/of/upstream.git>
252 * HEAD:refs/for/next/topic [new reference]
253 * HEAD:refs/for/a/b/c/topic [new reference]
t/t5411/test-0036-report-multi-rewrite-for-one-ref.sh
+8 -3
@@ -41,7 +41,9 @@ test_expect_success "proc-receive: multiple rewrite for one ref, no refname for
41 remote: proc-receive> option old-oid <COMMIT-A>
42 remote: proc-receive> option new-oid <COMMIT-B>
43 remote: # post-receive hook
44 - remote: post-receive< <ZERO-OID> <COMMIT-A> refs/for/master/topic
44 + remote: post-receive< <COMMIT-A> <COMMIT-B> refs/for/master/topic
45 + remote: post-receive< <ZERO-OID> <COMMIT-A> refs/changes/24/124/1
46 + remote: post-receive< <COMMIT-A> <COMMIT-B> refs/changes/25/125/1
47 To <URL/of/upstream.git>
48 * [new reference] HEAD -> refs/for/master/topic
49 EOF
@@ -99,7 +101,9 @@ test_expect_success "proc-receive: multiple rewrites for one ref, no refname for
101 remote: proc-receive> option new-oid <COMMIT-A>
102 remote: proc-receive> option forced-update
103 remote: # post-receive hook
102 - remote: post-receive< <ZERO-OID> <COMMIT-A> refs/for/master/topic
104 + remote: post-receive< <ZERO-OID> <COMMIT-A> refs/changes/24/124/1
105 + remote: post-receive< <COMMIT-A> <COMMIT-B> refs/for/master/topic
106 + remote: post-receive< <COMMIT-B> <COMMIT-A> refs/changes/25/125/1
107 To <URL/of/upstream.git>
108 * [new reference] HEAD -> refs/for/master/topic
109 EOF
@@ -145,7 +149,8 @@ test_expect_success "proc-receive: multiple rewrites for one ref ($PROTOCOL)" '
149 remote: proc-receive> option old-oid <COMMIT-A>
150 remote: proc-receive> option new-oid <COMMIT-B>
151 remote: # post-receive hook
148 - remote: post-receive< <ZERO-OID> <COMMIT-A> refs/for/master/topic
152 + remote: post-receive< <ZERO-OID> <COMMIT-A> refs/changes/23/123/1
153 + remote: post-receive< <COMMIT-A> <COMMIT-B> refs/changes/24/124/2
154 To <URL/of/upstream.git>
155 * [new reference] HEAD -> refs/for/master/topic
156 EOF
t/t5411/test-0037-report-multi-rewrite-for-one-ref--porcelain.sh
+8 -3
@@ -41,7 +41,9 @@ test_expect_success "proc-receive: multiple rewrite for one ref, no refname for
41 remote: proc-receive> option old-oid <COMMIT-A>
42 remote: proc-receive> option new-oid <COMMIT-B>
43 remote: # post-receive hook
44 - remote: post-receive< <ZERO-OID> <COMMIT-A> refs/for/master/topic
44 + remote: post-receive< <COMMIT-A> <COMMIT-B> refs/for/master/topic
45 + remote: post-receive< <ZERO-OID> <COMMIT-A> refs/changes/24/124/1
46 + remote: post-receive< <COMMIT-A> <COMMIT-B> refs/changes/25/125/1
47 To <URL/of/upstream.git>
48 * HEAD:refs/for/master/topic [new reference]
49 Done
@@ -100,7 +102,9 @@ test_expect_success "proc-receive: multiple rewrites for one ref, no refname for
102 remote: proc-receive> option new-oid <COMMIT-A>
103 remote: proc-receive> option forced-update
104 remote: # post-receive hook
103 - remote: post-receive< <ZERO-OID> <COMMIT-A> refs/for/master/topic
105 + remote: post-receive< <ZERO-OID> <COMMIT-A> refs/changes/24/124/1
106 + remote: post-receive< <COMMIT-A> <COMMIT-B> refs/for/master/topic
107 + remote: post-receive< <COMMIT-B> <COMMIT-A> refs/changes/25/125/1
108 To <URL/of/upstream.git>
109 * HEAD:refs/for/master/topic [new reference]
110 Done
@@ -147,7 +151,8 @@ test_expect_success "proc-receive: multiple rewrites for one ref ($PROTOCOL/porc
151 remote: proc-receive> option old-oid <COMMIT-A>
152 remote: proc-receive> option new-oid <COMMIT-B>
153 remote: # post-receive hook
150 - remote: post-receive< <ZERO-OID> <COMMIT-A> refs/for/master/topic
154 + remote: post-receive< <ZERO-OID> <COMMIT-A> refs/changes/23/123/1
155 + remote: post-receive< <COMMIT-A> <COMMIT-B> refs/changes/24/124/2
156 To <URL/of/upstream.git>
157 * HEAD:refs/for/master/topic [new reference]
158 Done
t/t5411/test-0038-report-mixed-refs.sh
+1 -1
@@ -53,7 +53,7 @@ test_expect_success "proc-receive: report update of mixed refs ($PROTOCOL)" '
53 remote: post-receive< <ZERO-OID> <COMMIT-A> refs/heads/baz
54 remote: post-receive< <ZERO-OID> <COMMIT-A> refs/for/next/topic2
55 remote: post-receive< <ZERO-OID> <COMMIT-A> refs/heads/foo
56 - remote: post-receive< <ZERO-OID> <COMMIT-A> refs/for/master/topic
56 + remote: post-receive< <COMMIT-A> <COMMIT-B> refs/for/master/topic
57 To <URL/of/upstream.git>
58 <OID-A>..<OID-B> <COMMIT-B> -> master
59 * [new branch] HEAD -> bar
t/t5411/test-0039-report-mixed-refs--porcelain.sh
+1 -1
@@ -53,7 +53,7 @@ test_expect_success "proc-receive: report update of mixed refs ($PROTOCOL/porcel
53 remote: post-receive< <ZERO-OID> <COMMIT-A> refs/heads/baz
54 remote: post-receive< <ZERO-OID> <COMMIT-A> refs/for/next/topic2
55 remote: post-receive< <ZERO-OID> <COMMIT-A> refs/heads/foo
56 - remote: post-receive< <ZERO-OID> <COMMIT-A> refs/for/master/topic
56 + remote: post-receive< <COMMIT-A> <COMMIT-B> refs/for/master/topic
57 To <URL/of/upstream.git>
58 <COMMIT-B>:refs/heads/master <OID-A>..<OID-B>
59 * HEAD:refs/heads/bar [new branch]