* [PATCH] transport-helper: negotiate dry-run before list
@ 2026-12-05 10:17 Daniel Pfeifer
0 siblings, 0 replies; only message in thread
From: Daniel Pfeifer @ 2026-12-05 10:17 UTC (permalink / raw)
To: git
Cc: Daniel Pfeifer, Jiang Xin, Ævar Arnfjörð Bjarmason,
Frantisek Hrbata, Junio C Hamano, Josh Steadmon
When all refs are up to date, push_refs_with_push()
returns before sending the dry-run option. This makes
an ordinary push indistinguishable from a dry run
for the remote helper.
Negotiate dry-run before listing refs. Send false
explicitly so helpers can distinguish ordinary pushes
from sessions initiated by older versions of Git.
Preserve the existing behavior for helpers that do
not support the dry-run option.
Signed-off-by: Daniel Pfeifer <daniel@pfeifer-mail.de>
---
I maintain git-nostr (https://git-nostr.dev), a remote
helper that uses existing Git hosting services for object
storage and publishes repository state to Nostr relays.
A push to the Git server may succeed while publication to
one or more Nostr relays fails. I would like users to be
able to retry publication by simply repeating `git push`,
without having to change any refs. The helper could
reconcile the published state with the actual repository
state at the end of the session.
However, when all refs are already up to date, both
`git push` and `git push --dry-run` produce the same
protocol sequence:
```
capabilities
option progress false
option verbosity 1
list for-push
<blank line>
```
The helper cannot determine whether it is permitted to
publish anything.
Sending an explicit dry-run value before `list for-push`
would resolve this ambiguity. Sending false explicitly also
allows the helper to remain compatible with older Git
versions without relying on version detection.
Documentation/gitremote-helpers.adoc | 4 +
t/t5803-remote-helper-push.sh | 106 +++++++++++++++++++++++++++
transport-helper.c | 20 ++++-
transport.c | 1 +
transport.h | 3 +
5 files changed, 133 insertions(+), 1 deletion(-)
create mode 100755 t/t5803-remote-helper-push.sh
diff --git a/Documentation/gitremote-helpers.adoc b/Documentation/gitremote-helpers.adoc
index 440a55d9c7..faa843834b 100644
--- a/Documentation/gitremote-helpers.adoc
+++ b/Documentation/gitremote-helpers.adoc
@@ -507,6 +507,10 @@ set by Git if the remote helper has the 'option' capability.
If true, pretend the operation completed successfully,
but don't actually change any repository data. For most
helpers this only applies to the 'push', if supported.
+ When preparing a push via the 'push' or 'export' capability,
+ Git sends this option before listing refs when the helper
+ advertises the 'option' capability, even if no ref updates
+ are needed.
'option servpath <c-style-quoted-path>'::
Sets service path (--upload-pack, --receive-pack etc.) for
diff --git a/t/t5803-remote-helper-push.sh b/t/t5803-remote-helper-push.sh
new file mode 100755
index 0000000000..cf1201949f
--- /dev/null
+++ b/t/t5803-remote-helper-push.sh
@@ -0,0 +1,106 @@
+#!/bin/sh
+
+test_description='Test remote-helper push options'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+test_expect_success 'setup remote helper with push capability' '
+ test_commit initial &&
+ HELPER_OID=$(git rev-parse HEAD) &&
+ HELPER_LOG="$PWD/helper.log" &&
+ export HELPER_OID HELPER_LOG &&
+ write_script git-remote-dryrun <<-\EOF &&
+ while read -r line
+ do
+ case "$line" in
+ capabilities)
+ printf "push\noption\nobject-format\n\n"
+ ;;
+ "option dry-run "*)
+ echo "$line" >>"$HELPER_LOG"
+ echo "${HELPER_DRY_RUN_RESPONSE:-ok}"
+ ;;
+ "option object-format true")
+ echo ok
+ ;;
+ option\ *)
+ echo unsupported
+ ;;
+ "list for-push")
+ echo "$line" >>"$HELPER_LOG"
+ echo ":object-format $(git rev-parse --show-object-format)"
+ echo "$HELPER_OID refs/heads/main"
+ echo
+ ;;
+ push\ *)
+ echo "$line" >>"$HELPER_LOG"
+ read -r end && test -z "$end" || exit 1
+ printf "ok refs/heads/main\n\n"
+ ;;
+ "")
+ exit 0
+ ;;
+ *)
+ exit 1
+ ;;
+ esac
+ done
+ EOF
+ PATH="$PWD:$PATH" &&
+ export PATH &&
+ git remote add origin dryrun::test
+'
+
+test_expect_success 'up-to-date push tells helper it is not a dry run' '
+ test_write_lines "option dry-run false" "list for-push" >expect &&
+ >"$HELPER_LOG" &&
+ git push origin HEAD:refs/heads/main &&
+ test_cmp expect "$HELPER_LOG"
+'
+
+test_expect_success 'send dry-run before listing up-to-date refs' '
+ test_write_lines "option dry-run true" "list for-push" >expect &&
+ >"$HELPER_LOG" &&
+ git push --dry-run origin HEAD:refs/heads/main &&
+ test_cmp expect "$HELPER_LOG"
+'
+
+test_expect_success 'up-to-date dry-run tolerates unsupported option' '
+ test_write_lines "option dry-run true" "list for-push" >expect &&
+ >"$HELPER_LOG" &&
+ HELPER_DRY_RUN_RESPONSE=unsupported \
+ git push --dry-run origin HEAD:refs/heads/main &&
+ test_cmp expect "$HELPER_LOG"
+'
+
+test_expect_success 'dry-run option is sent once even if refs need updating' '
+ test_commit second &&
+ test_write_lines "option dry-run true" "list for-push" \
+ "push HEAD:refs/heads/main" >expect &&
+ >"$HELPER_LOG" &&
+ git push --dry-run origin HEAD:refs/heads/main &&
+ test_cmp expect "$HELPER_LOG"
+'
+
+test_expect_success 'dry-run with ref updates requires helper support' '
+ test_write_lines "option dry-run true" "list for-push" >expect &&
+ >"$HELPER_LOG" &&
+ test_must_fail env HELPER_DRY_RUN_RESPONSE=unsupported \
+ git push --dry-run origin HEAD:refs/heads/main 2>err &&
+ test_grep "does not support dry-run" err &&
+ test_cmp expect "$HELPER_LOG"
+'
+
+test_expect_success 'normal push tolerates unsupported dry-run option' '
+ test_write_lines "option dry-run false" "list for-push" \
+ "push HEAD:refs/heads/main" >expect &&
+ >"$HELPER_LOG" &&
+ HELPER_DRY_RUN_RESPONSE=unsupported \
+ git push origin HEAD:refs/heads/main &&
+ test_cmp expect "$HELPER_LOG"
+'
+
+test_done
diff --git a/transport-helper.c b/transport-helper.c
index 14d179e0f6..e1470a4901 100644
--- a/transport-helper.c
+++ b/transport-helper.c
@@ -50,6 +50,8 @@ struct helper_data {
*/
unsigned get_refs_list_called : 1;
+ unsigned dry_run_option_accepted : 1;
+
char *export_marks;
char *import_marks;
/* These go from remote name (as in "list") to private name */
@@ -1007,8 +1009,10 @@ static int push_update_refs_status(struct helper_data *data,
static void set_common_push_options(struct transport *transport,
const char *name, int flags)
{
+ struct helper_data *data = transport->data;
+
if (flags & TRANSPORT_PUSH_DRY_RUN) {
- if (set_helper_option(transport, "dry-run", "true") != 0)
+ if (!data->dry_run_option_accepted)
die(_("helper %s does not support dry-run"), name);
} else if (flags & TRANSPORT_PUSH_CERT_ALWAYS) {
if (set_helper_option(transport, TRANS_OPT_PUSH_CERT, "true") != 0)
@@ -1251,12 +1255,26 @@ static int has_attribute(const char *attrs, const char *attr)
static struct ref *get_refs_list(struct transport *transport, int for_push,
struct transport_ls_refs_options *transport_options)
{
+ struct helper_data *data = transport->data;
+
get_helper(transport);
if (process_connect(transport, for_push))
return transport->vtable->get_refs_list(transport, for_push,
transport_options);
+ /*
+ * A push can finish without sending updates. Communicate dry-run
+ * mode before listing refs, but defer checking whether the helper
+ * supports it until a transfer is requested.
+ */
+ if (for_push && (data->push || data->export)) {
+ const char *dry_run = transport_options->dry_run ? "true" : "false";
+
+ data->dry_run_option_accepted =
+ !set_helper_option(transport, "dry-run", dry_run);
+ }
+
return get_refs_list_using_list(transport, for_push);
}
diff --git a/transport.c b/transport.c
index 4491443b28..398a0079fe 100644
--- a/transport.c
+++ b/transport.c
@@ -1495,6 +1495,7 @@ int transport_push(struct repository *r,
if (check_push_refs(local_refs, rs) < 0)
goto done;
+ transport_options.dry_run = !!pretend;
refspec_ref_prefixes(rs, &transport_options.ref_prefixes);
trace2_region_enter("transport_push", "get_refs_list", r);
diff --git a/transport.h b/transport.h
index cb262ee3da..0e8f87ef29 100644
--- a/transport.h
+++ b/transport.h
@@ -267,6 +267,9 @@ int transport_push(struct repository *repo,
unsigned int * reject_reasons);
struct transport_ls_refs_options {
+ /* Whether the push being prepared is a dry run. */
+ unsigned dry_run : 1;
+
/*
* Optionally, a list of ref prefixes can be provided which can be sent
* to the server (when communicating using protocol v2) to enable it to
--
2.56.1
^ permalink raw reply related [flat|nested] only message in thread
only message in thread, other threads:[~2026-12-05 10:23 UTC | newest]
Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-05 10:17 [PATCH] transport-helper: negotiate dry-run before list Daniel Pfeifer
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox