Skip to content

Commit 195d6ea

Browse files
jiangxingitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 15d3af5 commit 195d6ea

7 files changed

+58
-26
lines changed

builtin/receive-pack.c

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,7 @@ static void prepare_push_cert_sha1(struct child_process *proc)
694694

695695
struct receive_hook_feed_state {
696696
struct command *cmd;
697+
struct ref_push_report *report;
697698
int skip_broken;
698699
struct strbuf buf;
699700
const struct string_list *push_options;
@@ -781,11 +782,31 @@ static int feed_receive_hook(void *state_, const char **bufp, size_t *sizep)
781782
cmd = cmd->next;
782783
if (!cmd)
783784
return -1; /* EOF */
785+
if (!bufp)
786+
return 0; /* OK, can feed something. */
784787
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+
}
789810
if (bufp) {
790811
*bufp = state->buf.buf;
791812
*sizep = state->buf.len;
@@ -804,6 +825,7 @@ static int run_receive_hook(struct command *commands,
804825
strbuf_init(&state.buf, 0);
805826
state.cmd = commands;
806827
state.skip_broken = skip_broken;
828+
state.report = NULL;
807829
if (feed_receive_hook(&state, NULL, NULL))
808830
return 0;
809831
state.cmd = commands;

t/t5411/test-0032-report-with-options.sh

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ test_expect_success "proc-receive: report option refname ($PROTOCOL)" '
5454
remote: proc-receive> ok refs/for/master/topic
5555
remote: proc-receive> option refname refs/pull/123/head
5656
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
5858
To <URL/of/upstream.git>
5959
* [new reference] HEAD -> refs/for/master/topic
6060
EOF
@@ -87,7 +87,7 @@ test_expect_success "proc-receive: report option refname and forced-update ($PRO
8787
remote: proc-receive> option refname refs/pull/123/head
8888
remote: proc-receive> option forced-update
8989
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
9191
To <URL/of/upstream.git>
9292
* [new reference] HEAD -> refs/for/master/topic
9393
EOF
@@ -121,7 +121,7 @@ test_expect_success "proc-receive: report option refname and old-oid ($PROTOCOL)
121121
remote: proc-receive> option refname refs/pull/123/head
122122
remote: proc-receive> option old-oid <COMMIT-B>
123123
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
125125
To <URL/of/upstream.git>
126126
* [new reference] HEAD -> refs/for/master/topic
127127
EOF
@@ -153,7 +153,7 @@ test_expect_success "proc-receive: report option old-oid ($PROTOCOL)" '
153153
remote: proc-receive> ok refs/for/master/topic
154154
remote: proc-receive> option old-oid <COMMIT-B>
155155
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
157157
To <URL/of/upstream.git>
158158
* [new reference] HEAD -> refs/for/master/topic
159159
EOF
@@ -187,7 +187,7 @@ test_expect_success "proc-receive: report option old-oid and new-oid ($PROTOCOL)
187187
remote: proc-receive> option old-oid <COMMIT-A>
188188
remote: proc-receive> option new-oid <COMMIT-B>
189189
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
191191
To <URL/of/upstream.git>
192192
* [new reference] HEAD -> refs/for/master/topic
193193
EOF
@@ -237,9 +237,9 @@ test_expect_success "proc-receive: report with multiple rewrites ($PROTOCOL)" '
237237
remote: proc-receive> option forced-update
238238
remote: proc-receive> option new-oid <COMMIT-A>
239239
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
241241
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
243243
To <URL/of/upstream.git>
244244
* [new reference] HEAD -> refs/for/next/topic
245245
* [new reference] HEAD -> refs/for/a/b/c/topic

t/t5411/test-0033-report-with-options--porcelain.sh

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ test_expect_success "proc-receive: report option refname ($PROTOCOL/porcelain)"
5555
remote: proc-receive> ok refs/for/master/topic
5656
remote: proc-receive> option refname refs/pull/123/head
5757
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
5959
To <URL/of/upstream.git>
6060
* HEAD:refs/for/master/topic [new reference]
6161
Done
@@ -90,7 +90,7 @@ test_expect_success "proc-receive: report option refname and forced-update ($PRO
9090
remote: proc-receive> option refname refs/pull/123/head
9191
remote: proc-receive> option forced-update
9292
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
9494
To <URL/of/upstream.git>
9595
* HEAD:refs/for/master/topic [new reference]
9696
Done
@@ -125,7 +125,7 @@ test_expect_success "proc-receive: report option refname and old-oid ($PROTOCOL/
125125
remote: proc-receive> option refname refs/pull/123/head
126126
remote: proc-receive> option old-oid <COMMIT-B>
127127
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
129129
To <URL/of/upstream.git>
130130
* HEAD:refs/for/master/topic [new reference]
131131
Done
@@ -158,7 +158,7 @@ test_expect_success "proc-receive: report option old-oid ($PROTOCOL/porcelain)"
158158
remote: proc-receive> ok refs/for/master/topic
159159
remote: proc-receive> option old-oid <COMMIT-B>
160160
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
162162
To <URL/of/upstream.git>
163163
* HEAD:refs/for/master/topic [new reference]
164164
Done
@@ -193,7 +193,7 @@ test_expect_success "proc-receive: report option old-oid and new-oid ($PROTOCOL/
193193
remote: proc-receive> option old-oid <COMMIT-A>
194194
remote: proc-receive> option new-oid <COMMIT-B>
195195
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
197197
To <URL/of/upstream.git>
198198
* HEAD:refs/for/master/topic [new reference]
199199
Done
@@ -245,9 +245,9 @@ test_expect_success "proc-receive: report with multiple rewrites ($PROTOCOL/porc
245245
remote: proc-receive> option forced-update
246246
remote: proc-receive> option new-oid <COMMIT-A>
247247
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
249249
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
251251
To <URL/of/upstream.git>
252252
* HEAD:refs/for/next/topic [new reference]
253253
* HEAD:refs/for/a/b/c/topic [new reference]

t/t5411/test-0036-report-multi-rewrite-for-one-ref.sh

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ test_expect_success "proc-receive: multiple rewrite for one ref, no refname for
4141
remote: proc-receive> option old-oid <COMMIT-A>
4242
remote: proc-receive> option new-oid <COMMIT-B>
4343
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
4547
To <URL/of/upstream.git>
4648
* [new reference] HEAD -> refs/for/master/topic
4749
EOF
@@ -99,7 +101,9 @@ test_expect_success "proc-receive: multiple rewrites for one ref, no refname for
99101
remote: proc-receive> option new-oid <COMMIT-A>
100102
remote: proc-receive> option forced-update
101103
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
103107
To <URL/of/upstream.git>
104108
* [new reference] HEAD -> refs/for/master/topic
105109
EOF
@@ -145,7 +149,8 @@ test_expect_success "proc-receive: multiple rewrites for one ref ($PROTOCOL)" '
145149
remote: proc-receive> option old-oid <COMMIT-A>
146150
remote: proc-receive> option new-oid <COMMIT-B>
147151
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
149154
To <URL/of/upstream.git>
150155
* [new reference] HEAD -> refs/for/master/topic
151156
EOF

t/t5411/test-0037-report-multi-rewrite-for-one-ref--porcelain.sh

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ test_expect_success "proc-receive: multiple rewrite for one ref, no refname for
4141
remote: proc-receive> option old-oid <COMMIT-A>
4242
remote: proc-receive> option new-oid <COMMIT-B>
4343
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
4547
To <URL/of/upstream.git>
4648
* HEAD:refs/for/master/topic [new reference]
4749
Done
@@ -100,7 +102,9 @@ test_expect_success "proc-receive: multiple rewrites for one ref, no refname for
100102
remote: proc-receive> option new-oid <COMMIT-A>
101103
remote: proc-receive> option forced-update
102104
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
104108
To <URL/of/upstream.git>
105109
* HEAD:refs/for/master/topic [new reference]
106110
Done
@@ -147,7 +151,8 @@ test_expect_success "proc-receive: multiple rewrites for one ref ($PROTOCOL/porc
147151
remote: proc-receive> option old-oid <COMMIT-A>
148152
remote: proc-receive> option new-oid <COMMIT-B>
149153
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
151156
To <URL/of/upstream.git>
152157
* HEAD:refs/for/master/topic [new reference]
153158
Done

t/t5411/test-0038-report-mixed-refs.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ test_expect_success "proc-receive: report update of mixed refs ($PROTOCOL)" '
5353
remote: post-receive< <ZERO-OID> <COMMIT-A> refs/heads/baz
5454
remote: post-receive< <ZERO-OID> <COMMIT-A> refs/for/next/topic2
5555
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
5757
To <URL/of/upstream.git>
5858
<OID-A>..<OID-B> <COMMIT-B> -> master
5959
* [new branch] HEAD -> bar

t/t5411/test-0039-report-mixed-refs--porcelain.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ test_expect_success "proc-receive: report update of mixed refs ($PROTOCOL/porcel
5353
remote: post-receive< <ZERO-OID> <COMMIT-A> refs/heads/baz
5454
remote: post-receive< <ZERO-OID> <COMMIT-A> refs/for/next/topic2
5555
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
5757
To <URL/of/upstream.git>
5858
<COMMIT-B>:refs/heads/master <OID-A>..<OID-B>
5959
* HEAD:refs/heads/bar [new branch]

0 commit comments

Comments
 (0)