From: Karthik Nayak <karthik.188@gmail.com>
To: git@vger.kernel.org
Cc: ps@pks.im, gitster@pobox.com, jltobler@gmail.com,
kristofferhaugsbakk@fastmail.com,
Karthik Nayak <karthik.188@gmail.com>,
Phillip Wood <phillip.wood@dunelm.org.uk>
Subject: [PATCH v5 3/3] hook: introduce the receive-report hook
Date: Tue, 01 Sep 2026 17:19:25 +0200 [thread overview]
Message-ID: <20260901-758-introduce-hook-v5-3-35cdc6be3cc1@gmail.com> (raw)
In-Reply-To: <20260901-758-introduce-hook-v5-0-35cdc6be3cc1@gmail.com>
When running 'git-receive-pack(1)', there is no way for the server to
intercept and modify the status report before it is sent back to the
client. Servers with custom logic may need to transform or gate the
report based on the outcome of external logic post reference updates.
This is specially needed for our usecase at GitLab where we have custom
MVCC logic on top of Git which creates a new version for each push
operation. The new version is only committed when certain external
operations post reference transaction succeed. So reporting the correct
message based on the outcome of these operations is important.
We cannot use any of the existing hooks as:
- The pre-receive hook runs too early, as we haven't updated
references at that point yet and we need to have the full view of
all resulting updates (both objects and references).
- The update hook is too inefficient as it runs once per reference,
and we cannot trivially determine the last update.
- The reference-transaction hook cannot be used by us because we care
about the phase where it was committed already. And while the hook
fires in that phase, it does not allow the caller to modify the
result in any capacity.
- The post-receive and post-update hooks cannot be used as they run
too late, at the point where we have already reported success to the
client.
Introduce a new 'receive-report' hook. The hook receives the complete
pkt-line encoded status report on standard input, after all ref updates
have been applied to the repository by execute_commands() but before the
report is sent to the client. See linkgit:gitprotocol-pack[5] details on
the protocol structure.
The hook's stdout fully replaces the report sent to the client.
receive-pack fully buffers the hook's stdout before acting on the exit
status, so the exit code is known before the client receives anything.
This gives two distinct behaviors depending on exit status:
- Exit 0: the hook's stdout is used as the report. The hook can
rewrite 'ok' lines to 'ng' lines to signal per-ref rejection to the
client while receive-pack itself exits cleanly. The client marks
rejected refs as '[remote rejected]' and exits with a non-zero
status if any ref is 'ng'.
- Non-zero exit: the hook's stdout is discarded, receive-pack modifies
all references to be rejected with a 'receive-report hook failed'
error.
In both cases, any output the hook writes to standard error is
forwarded to the client over the sideband channel and appears as
'remote:' lines on the client terminal. Writing to stderr alone does
not affect the push outcome.
Note that in either failure mode, ref updates already applied by
execute_commands() are not rolled back. The hook can cause the client
to perceive the push as failed, but cannot undo server-side changes.
This hook does not use the config-based hook infrastructure, which
supports running multiple scripts per hook event. This hook is a
bidirectional filter: it receives the report on stdin and writes a
modified version to stdout. Running multiple such scripts sequentially
would require piping the output of one into the input of the next,
which the current hook infrastructure does not support. A single-script
design is therefore a natural fit, and is consistent with how
'proc-receive' is structured for the same reason.
Helped-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
---
Documentation/git-receive-pack.adoc | 9 ++
Documentation/githooks.adoc | 47 ++++++++
builtin/receive-pack.c | 54 +++++++++
t/meson.build | 1 +
t/t5412-receive-report-hook.sh | 224 ++++++++++++++++++++++++++++++++++++
5 files changed, 335 insertions(+)
diff --git a/Documentation/git-receive-pack.adoc b/Documentation/git-receive-pack.adoc
index 5806792ba7..ab668ffa0c 100644
--- a/Documentation/git-receive-pack.adoc
+++ b/Documentation/git-receive-pack.adoc
@@ -245,6 +245,15 @@ commands will be executed by this hook, instead of by the internal
`execute_commands()` function. This hook is responsible for updating
the relevant references and reporting the results back to 'receive-pack'.
+RECEIVE-REPORT HOOK
+-------------------
+This hook is invoked by 'git-receive-pack' after all the ref updates
+have been applied but before the report is sent to the client. The hook
+receives the complete report in pkt-line format on stdin and its stdout
+replaces the report sent to the client, which allows the hook to rewrite
+the outcomes or abort the push completely. See linkgit:githooks[5] for
+the full protocol description.
+
QUARANTINE ENVIRONMENT
----------------------
diff --git a/Documentation/githooks.adoc b/Documentation/githooks.adoc
index ed045940d1..5ac5bf9454 100644
--- a/Documentation/githooks.adoc
+++ b/Documentation/githooks.adoc
@@ -527,6 +527,53 @@ The exit status of the hook is ignored for any state except for the
status will cause the transaction to be aborted. The hook will not be
called with "aborted" state in that case.
+receive-report
+~~~~~~~~~~~~~~
+
+This hook is invoked by linkgit:git-receive-pack[1] when it reacts to
+`git push` and updates references in its repository. It executes on
+the repository once after all refs have been updated and after all
+accepted ref changes are applied to the repository, but before the
+pkt-line encoded status report is sent back to the client.
+
+The hook receives the complete pkt-line encoded status report on
+standard input, see linkgit:gitprotocol-pack[5] for details on the
+structure. The hook's standard output entirely replaces the report
+that is sent to the client. The hook must write a valid pkt-line
+encoded report in the same format it received. The hook's stdout is
+fully buffered by `receive-pack` before any data is sent to the client,
+so the hook's exit status is known before the client receives anything.
+
+There are three distinct ways the hook can affect the push outcome:
+
+* To reject the push, modify the unpack status from `ok` to the required
+ error message. While `git-push` will fail, individual references may
+ still show success messages unless modified.
+
+* To reject individual ref updates while keeping `receive-pack` alive,
+ rewrite the corresponding `ok <refname>` lines to
+ `ng <refname>[ <reason>]` lines in the output and exit with status 0.
+ The client will then mark those specific refs as rejected while
+ treating any `ok` refs as successful. The push as a whole is
+ considered failed if any ref is `ng`, and `git push` will exit with
+ a non-zero status on the client side.
+
+* To abort the entire push unconditionally, exit with a non-zero
+ status. In this case the hook's stdout is discarded, `receive-pack`
+ modifies all references to be rejected with a 'receive-report hook
+ failed' error.
+
+Any output written to standard error is forwarded to the client over
+the sideband channel and will appear as `remote:` lines on clients
+using 'git-push(1)', regardless of the hook's exit status. Writing to
+standard error alone does not affect the push outcome.
+
+Note that by the time this hook runs, all ref updates have already been
+applied to the repository. Neither a non-zero exit nor rewriting refs
+to `ng` rolls back any ref changes that were already committed
+server-side. The hook can cause the client to perceive the push as
+failed, but cannot undo the server-side updates.
+
push-to-checkout
~~~~~~~~~~~~~~~~
diff --git a/builtin/receive-pack.c b/builtin/receive-pack.c
index 34d5e46097..c9f1eb3335 100644
--- a/builtin/receive-pack.c
+++ b/builtin/receive-pack.c
@@ -1004,6 +1004,41 @@ static int run_update_hook(struct command *cmd)
return code;
}
+static int run_receive_report_hook(struct strbuf *report)
+{
+ struct child_process proc = CHILD_PROCESS_INIT;
+ struct async sideband_async;
+ int sideband_async_started = 0;
+ int saved_stderr = -1;
+ struct strbuf out = STRBUF_INIT;
+ const char *hook_path;
+ int ret;
+
+ hook_path = find_hook(the_repository, "receive-report");
+ if (!hook_path)
+ return 0;
+
+ strvec_push(&proc.args, hook_path);
+ proc.trace2_hook_name = "receive-report";
+
+ prepare_sideband_async(&sideband_async, &saved_stderr,
+ &sideband_async_started);
+
+ sigchain_push(SIGPIPE, SIG_IGN);
+ ret = pipe_command(&proc, report->buf, report->len, &out,
+ report->len, NULL, 0);
+ sigchain_pop(SIGPIPE);
+
+ finish_sideband_async(&sideband_async, saved_stderr,
+ sideband_async_started);
+
+ if (!ret)
+ strbuf_swap(&out, report);
+
+ strbuf_release(&out);
+ return ret;
+}
+
static struct command *find_command_by_refname(struct command *list,
const char *refname)
{
@@ -2530,6 +2565,13 @@ static void update_shallow_info(struct command *commands,
free(ref_status);
}
+static void override_cmds_error(struct command *commands, const char *err)
+{
+ for (struct command *cmd = commands; cmd; cmd = cmd->next) {
+ cmd->error_string = err;
+ }
+}
+
/*
* Generate the response to be sent to the client invoking 'git-receive-pack(1)'.
* For v2 protocol, set `detailed_report` to true, which will also add detailed
@@ -2583,6 +2625,12 @@ static void report(struct command *commands, const char *unpack_status)
generate_report(&buf, commands, unpack_status, false);
+ if (run_receive_report_hook(&buf)) {
+ strbuf_reset(&buf);
+ override_cmds_error(commands, "receive-report hook failed");
+ generate_report(&buf, commands, unpack_status, false);
+ }
+
if (use_sideband)
send_sideband(1, 1, buf.buf, buf.len, use_sideband);
else
@@ -2596,6 +2644,12 @@ static void report_v2(struct command *commands, const char *unpack_status)
generate_report(&buf, commands, unpack_status, true);
+ if (run_receive_report_hook(&buf)) {
+ strbuf_reset(&buf);
+ override_cmds_error(commands, "receive-report hook failed");
+ generate_report(&buf, commands, unpack_status, true);
+ }
+
if (use_sideband)
send_sideband(1, 1, buf.buf, buf.len, use_sideband);
else
diff --git a/t/meson.build b/t/meson.build
index a25f37d2f5..7088c2c1c1 100644
--- a/t/meson.build
+++ b/t/meson.build
@@ -651,6 +651,7 @@ integration_tests = [
't5409-colorize-remote-messages.sh',
't5410-receive-pack.sh',
't5411-proc-receive-hook.sh',
+ 't5412-receive-report-hook.sh',
't5500-fetch-pack.sh',
't5501-fetch-push-alternates.sh',
't5502-quickfetch.sh',
diff --git a/t/t5412-receive-report-hook.sh b/t/t5412-receive-report-hook.sh
new file mode 100755
index 0000000000..24679de37b
--- /dev/null
+++ b/t/t5412-receive-report-hook.sh
@@ -0,0 +1,224 @@
+#!/bin/sh
+
+test_description='test receive-report hook'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+. "$TEST_DIRECTORY"/t5411/common-functions.sh
+
+URL_PREFIX="\.\."
+
+test_expect_success "setup workbench" '
+ git init workbench &&
+ create_commits_in workbench A B
+'
+
+test_expect_success "no report hook, push succeeds" '
+ test_when_finished "rm -rf upstream" &&
+ test_when_finished "git -C workbench remote remove origin" &&
+ git init --bare upstream &&
+
+ git -C workbench remote add origin ../upstream &&
+ git -C workbench push origin $A:refs/heads/main &&
+ git -C workbench push origin $B:refs/heads/main >out 2>&1 &&
+
+ make_user_friendly_and_stable_output <out >actual &&
+ cat >expect <<-\EOF &&
+ To ../upstream
+ <COMMIT-A>..<COMMIT-B> <COMMIT-B> -> main
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success "passthrough does not alter report" '
+ test_when_finished "rm -rf upstream" &&
+ test_when_finished "git -C workbench remote remove origin" &&
+ git init --bare upstream &&
+
+ test_hook -C upstream --setup receive-report <<-\EOF &&
+ cat
+ EOF
+
+ git -C workbench remote add origin ../upstream &&
+ git -C workbench push origin $A:refs/heads/main &&
+ git -C workbench push origin $B:refs/heads/main >out 2>&1 &&
+
+ make_user_friendly_and_stable_output <out >actual &&
+ cat >expect <<-\EOF &&
+ To ../upstream
+ <COMMIT-A>..<COMMIT-B> <COMMIT-B> -> main
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success "non-zero exit reports as hook failed" '
+ test_when_finished "rm -rf upstream" &&
+ test_when_finished "git -C workbench remote remove origin" &&
+
+ git init --bare upstream &&
+ git -C workbench remote add origin ../upstream &&
+ git -C workbench push origin $A:refs/heads/main &&
+
+ test_hook -C upstream --setup receive-report <<-\EOF &&
+ exit 1
+ EOF
+
+ test_must_fail git -C workbench push origin $B:refs/heads/main >out 2>&1 &&
+ make_user_friendly_and_stable_output <out >actual &&
+ cat >expect <<-\EOF &&
+ To ../upstream
+ ! [remote rejected] <COMMIT-B> -> main (receive-report hook failed)
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success "hook is invoked and receives report on stdin" '
+ test_when_finished "rm -rf upstream" &&
+ test_when_finished "git -C workbench remote remove origin" &&
+
+ git init --bare upstream &&
+ test_hook -C upstream --setup receive-report <<-EOF &&
+ tee raw
+ EOF
+
+ git -C workbench remote add origin ../upstream &&
+ git -C workbench push origin $A:refs/heads/main &&
+ git -C workbench push origin $B:refs/heads/main >out 2>&1 &&
+
+ make_user_friendly_and_stable_output <out >actual &&
+ cat >expect <<-EOF &&
+ To ../upstream
+ <COMMIT-A>..<COMMIT-B> <COMMIT-B> -> main
+ EOF
+ test_cmp expect actual &&
+
+ test-tool pkt-line unpack <upstream/raw >actual-report &&
+ cat >expect-report <<-EOF &&
+ unpack ok
+ ok refs/heads/main
+ 0000
+ EOF
+ test_cmp expect-report actual-report
+'
+
+test_expect_success "hook can modify the report sent to client" '
+ test_when_finished "rm -rf upstream" &&
+ test_when_finished "git -C workbench remote remove origin" &&
+
+ git init --bare upstream &&
+ git -C workbench remote add origin ../upstream &&
+ git -C workbench push origin $A:refs/heads/main &&
+
+ test_hook -C upstream --setup receive-report <<-\EOF &&
+ test-tool pkt-line unpack |
+ sed "s/^ok /ng /" |
+ test-tool pkt-line pack
+ EOF
+
+ test_must_fail git -C workbench push origin $B:refs/heads/main >out 2>&1 &&
+ make_user_friendly_and_stable_output <out >actual &&
+ cat >expect <<-\EOF &&
+ To ../upstream
+ ! [remote rejected] <COMMIT-B> -> main (failed)
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success "hook can modify the unpack status" '
+ test_when_finished "rm -rf upstream" &&
+ test_when_finished "git -C workbench remote remove origin" &&
+
+ git init --bare upstream &&
+ git -C workbench remote add origin ../upstream &&
+ git -C workbench push origin $A:refs/heads/main &&
+
+ test_hook -C upstream --setup receive-report <<-\EOF &&
+ test-tool pkt-line unpack |
+ sed "s/^unpack ok$/unpack push failed due to server error/" |
+ test-tool pkt-line pack
+ EOF
+
+ test_must_fail git -C workbench push origin $B:refs/heads/main >out 2>&1 &&
+ test_grep "error: remote unpack failed: push failed due to server error" out &&
+ make_user_friendly_and_stable_output <out >actual &&
+ cat >expect <<-\EOF &&
+ To ../upstream
+ <COMMIT-A>..<COMMIT-B> <COMMIT-B> -> main
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success "hook can report a custom failure message" '
+ test_when_finished "rm -rf upstream" &&
+ test_when_finished "git -C workbench remote remove origin" &&
+
+ git init --bare upstream &&
+ git -C workbench remote add origin ../upstream &&
+ git -C workbench push origin $A:refs/heads/main &&
+
+ test_hook -C upstream --setup receive-report <<-\EOF &&
+ echo "push rejected: service X is down" >&2
+ test-tool pkt-line unpack |
+ sed "s/^ok \(.*\)/ng \1 service-x-is-down/" |
+ test-tool pkt-line pack |
+ tee raw
+ EOF
+
+ test_must_fail git -C workbench push origin $B:refs/heads/main >out 2>&1 &&
+ test_grep "push rejected: service X is down" out &&
+
+ test-tool pkt-line unpack <upstream/raw >actual-report &&
+ cat >expect-report <<-\EOF &&
+ unpack ok
+ ng refs/heads/main service-x-is-down
+ 0000
+ EOF
+ test_cmp expect-report actual-report
+'
+
+test_expect_success "hook stderr with zero exit status code" '
+ test_when_finished "rm -rf upstream" &&
+ test_when_finished "git -C workbench remote remove origin" &&
+
+ git init --bare upstream &&
+ git -C workbench remote add origin ../upstream &&
+ git -C workbench push origin $A:refs/heads/main &&
+
+ test_hook -C upstream --setup receive-report <<-\EOF &&
+ echo "push rejected: service X is down" >&2
+ tee raw
+ EOF
+
+ git -C workbench push origin $B:refs/heads/main >out 2>&1 &&
+ test_grep "push rejected: service X is down" out &&
+
+ test-tool pkt-line unpack <upstream/raw >actual-report &&
+ cat >expect-report <<-\EOF &&
+ unpack ok
+ ok refs/heads/main
+ 0000
+ EOF
+ test_cmp expect-report actual-report
+'
+
+test_expect_success "hook stderr is relayed to client via sideband" '
+ test_when_finished "rm -rf upstream" &&
+ test_when_finished "git -C workbench remote remove origin" &&
+
+ git init --bare upstream &&
+ git -C workbench remote add origin ../upstream &&
+ git -C workbench push origin $A:refs/heads/main &&
+
+ test_hook -C upstream --setup receive-report <<-\EOF &&
+ echo "hook-stderr-message" >&2
+ exit 1
+ EOF
+
+ test_must_fail git -C workbench push origin $B:refs/heads/main >out 2>&1 &&
+ test_grep "remote: hook-stderr-message" out
+'
+
+test_done
--
2.55.GIT
next prev parent reply other threads:[~2026-09-01 15:19 UTC|newest]
Thread overview: 45+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-18 7:55 [PATCH] hook: introduce the report hook for git-receive-pack(1) Karthik Nayak
2026-08-18 20:54 ` Junio C Hamano
2026-08-19 7:03 ` Kristoffer Haugsbakk
2026-08-19 12:11 ` Karthik Nayak
2026-08-19 14:47 ` Kristoffer Haugsbakk
2026-08-19 7:39 ` Patrick Steinhardt
2026-08-19 13:13 ` Karthik Nayak
2026-08-19 13:20 ` Patrick Steinhardt
2026-08-19 13:24 ` Karthik Nayak
2026-08-20 9:50 ` Phillip Wood
2026-08-20 15:43 ` Junio C Hamano
2026-08-21 12:51 ` Karthik Nayak
2026-08-21 13:34 ` [PATCH v2] " Karthik Nayak
2026-08-21 13:49 ` Patrick Steinhardt
2026-08-21 16:08 ` Karthik Nayak
2026-08-24 5:32 ` Patrick Steinhardt
2026-08-24 8:14 ` Karthik Nayak
2026-08-21 16:55 ` Junio C Hamano
2026-08-24 10:20 ` [PATCH v3 0/3] " Karthik Nayak
2026-08-24 10:20 ` [PATCH v3 1/3] doc: add proc-receive hook info in 'git-receive-pack.adoc' Karthik Nayak
2026-08-24 10:21 ` [PATCH v3 2/3] receive-pack: move message generation to separate function Karthik Nayak
2026-08-24 10:21 ` [PATCH v3 3/3] hook: introduce the report hook for git-receive-pack(1) Karthik Nayak
2026-08-24 15:35 ` [PATCH v3 0/3] " Junio C Hamano
2026-08-24 15:57 ` Junio C Hamano
2026-08-24 17:00 ` Patrick Steinhardt
2026-08-26 8:35 ` Karthik Nayak
2026-08-26 14:39 ` Junio C Hamano
2026-08-26 10:19 ` [PATCH v4 0/3] hook: introduce the receive-report hook Karthik Nayak
2026-08-26 10:19 ` [PATCH v4 1/3] doc: add proc-receive hook info in 'git-receive-pack.adoc' Karthik Nayak
2026-08-31 6:44 ` Patrick Steinhardt
2026-08-31 18:22 ` Karthik Nayak
2026-08-26 10:19 ` [PATCH v4 2/3] receive-pack: move message generation to separate function Karthik Nayak
2026-08-31 6:44 ` Patrick Steinhardt
2026-08-31 19:05 ` Karthik Nayak
2026-08-26 10:19 ` [PATCH v4 3/3] hook: introduce the receive-report hook Karthik Nayak
2026-08-31 6:45 ` Patrick Steinhardt
2026-09-01 15:19 ` [PATCH v5 0/3] " Karthik Nayak
2026-09-01 15:19 ` [PATCH v5 1/3] doc: add proc-receive hook info in 'git-receive-pack.adoc' Karthik Nayak
2026-09-01 15:19 ` [PATCH v5 2/3] receive-pack: move message generation to separate function Karthik Nayak
2026-09-01 16:23 ` Junio C Hamano
2026-09-02 11:23 ` Karthik Nayak
2026-09-01 15:19 ` Karthik Nayak [this message]
2026-09-01 17:03 ` [PATCH v5 3/3] hook: introduce the receive-report hook Junio C Hamano
2026-09-02 14:42 ` Karthik Nayak
2026-09-02 19:14 ` Junio C Hamano
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260901-758-introduce-hook-v5-3-35cdc6be3cc1@gmail.com \
--to=karthik.188@gmail.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=jltobler@gmail.com \
--cc=kristofferhaugsbakk@fastmail.com \
--cc=phillip.wood@dunelm.org.uk \
--cc=ps@pks.im \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.