git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] remote-curl: pass on atomic capability to remote side
@ 2019-10-15  1:07 brian m. carlson
  2019-10-15 16:40 ` Jeff King
  2019-10-16 23:45 ` [PATCH v2] " brian m. carlson
  0 siblings, 2 replies; 5+ messages in thread
From: brian m. carlson @ 2019-10-15  1:07 UTC (permalink / raw)
  To: git; +Cc: Duy Nguyen, Jeff King

When pushing more than one reference with the --atomic option, the
server is supposed to perform a single atomic transaction to update the
references, leaving them either all to succeed or all to fail.  This
works fine when pushing locally or over SSH, but when pushing over HTTP,
we fail to pass the atomic capability to the remote side.  In fact, we
have not reported this capability to any remote helpers during the life
of the feature.

Now normally, things happen to work nevertheless, since we actually
check for most types of failures, such as non-fast-forward updates, on
the client side, and just abort the entire attempt.  However, if the
server side reports a problem, such as the inability to lock a ref, the
transaction isn't atomic, because we haven't passed the appropriate
capability over and the remote side has no way of knowing that we wanted
atomic behavior.

Fix this by passing the option from the transport code through to remote
helpers, and from the HTTP remote helper down to send-pack.  With this
change, we can detect if the server side rejects the push and report
back appropriately.  Note the difference in the messages: the remote
side reports "atomic transaction failed", while our own checking rejects
pushes with the message "atomic push failed".

Document the atomic option in the remote helper documentation, so other
implementers can implement it if they like.

Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net>
---
I discovered this at work when implementing support for atomic pushes.
Our equivalent of the pre-receive hook is not all-or-nothing, and
passing the atomic capability through to our backend worked for SSH, but
not HTTP.  I discovered with GIT_TRACE_PACKET=1 that we didn't pass the
atomic capability through during HTTP, so I thought I'd send a patch.

As I mentioned in the commit message, to my knowledge, this
functionality has been broken since the atomic capability was introduced
circa 2.4.0.

 Documentation/gitremote-helpers.txt |  5 ++++
 remote-curl.c                       | 13 +++++++++-
 t/t5541-http-push-smart.sh          | 40 +++++++++++++++++++++++++++--
 transport-helper.c                  |  4 +++
 transport.h                         |  3 +++
 5 files changed, 62 insertions(+), 3 deletions(-)

diff --git a/Documentation/gitremote-helpers.txt b/Documentation/gitremote-helpers.txt
index a5c3c04371..670d72c174 100644
--- a/Documentation/gitremote-helpers.txt
+++ b/Documentation/gitremote-helpers.txt
@@ -509,6 +509,11 @@ set by Git if the remote helper has the 'option' capability.
 	Indicate that only the objects wanted need to be fetched, not
 	their dependents.
 
+'option atomic' {'true'|'false'}::
+  When pushing, request the remote server to update refs in a single atomic
+  transaction.  If successful, all refs will be updated, or none will.  If the
+  remote side does not support this capability, the push will fail.
+
 SEE ALSO
 --------
 linkgit:git-remote[1]
diff --git a/remote-curl.c b/remote-curl.c
index 051f26629d..5232ed84b6 100644
--- a/remote-curl.c
+++ b/remote-curl.c
@@ -40,7 +40,8 @@ struct options {
 		push_cert : 2,
 		deepen_relative : 1,
 		from_promisor : 1,
-		no_dependents : 1;
+		no_dependents : 1,
+		atomic : 1;
 };
 static struct options options;
 static struct string_list cas_options = STRING_LIST_INIT_DUP;
@@ -148,6 +149,14 @@ static int set_option(const char *name, const char *value)
 		else
 			return -1;
 		return 0;
+	} else if (!strcmp(name, "atomic")) {
+		if (!strcmp(value, "true"))
+			options.atomic = 1;
+		else if (!strcmp(value, "false"))
+			options.atomic = 0;
+		else
+			return -1;
+		return 0;
 	} else if (!strcmp(name, "push-option")) {
 		if (*value != '"')
 			string_list_append(&options.push_options, value);
@@ -1196,6 +1205,8 @@ static int push_git(struct discovery *heads, int nr_spec, char **specs)
 		argv_array_push(&args, "--signed=yes");
 	else if (options.push_cert == SEND_PACK_PUSH_CERT_IF_ASKED)
 		argv_array_push(&args, "--signed=if-asked");
+	if (options.atomic)
+		argv_array_push(&args, "--atomic");
 	if (options.verbosity == 0)
 		argv_array_push(&args, "--quiet");
 	else if (options.verbosity > 1)
diff --git a/t/t5541-http-push-smart.sh b/t/t5541-http-push-smart.sh
index 92bac43257..4c970787b0 100755
--- a/t/t5541-http-push-smart.sh
+++ b/t/t5541-http-push-smart.sh
@@ -184,11 +184,12 @@ test_expect_success 'push --atomic also prevents branch creation, reports collat
 	test_config -C "$d" http.receivepack true &&
 	up="$HTTPD_URL"/smart/atomic-branches.git &&
 
-	# Tell "$up" about two branches for now
+	# Tell "$up" about three branches for now
 	test_commit atomic1 &&
 	test_commit atomic2 &&
 	git branch collateral &&
-	git push "$up" master collateral &&
+	git branch other &&
+	git push "$up" master collateral other &&
 
 	# collateral is a valid push, but should be failed by atomic push
 	git checkout collateral &&
@@ -226,6 +227,41 @@ test_expect_success 'push --atomic also prevents branch creation, reports collat
 	grep "^ ! .*rejected.* collateral -> collateral .*atomic push failed" output
 '
 
+test_expect_success 'push --atomic fails on server-side errors' '
+	# Use previously set up repository
+	d=$HTTPD_DOCUMENT_ROOT_PATH/atomic-branches.git &&
+	test_config -C "$d" http.receivepack true &&
+	up="$HTTPD_URL"/smart/atomic-branches.git &&
+
+	# break ref updates for other on the remote site
+	mkdir "$d/refs/heads/other.lock" &&
+
+	# add the new commit to other
+	git branch -f other collateral &&
+
+	# --atomic should cause entire push to be rejected
+	test_must_fail git push --atomic "$up" atomic other 2>output  &&
+
+	# the new branch should not have been created upstream
+	test_must_fail git -C "$d" show-ref --verify refs/heads/atomic &&
+
+	# upstream should still reflect atomic2, the last thing we pushed
+	# successfully
+	git rev-parse atomic2 >expected &&
+	# ...to other.
+	git -C "$d" rev-parse refs/heads/other >actual &&
+	test_cmp expected actual &&
+
+	# the new branch should not have been created upstream
+	test_must_fail git -C "$d" show-ref --verify refs/heads/atomic &&
+
+	# the failed refs should be indicated to the user
+	grep "^ ! .*rejected.* other -> other .*atomic transaction failed" output &&
+
+	# the collateral failure refs should be indicated to the user
+	grep "^ ! .*rejected.* atomic -> atomic .*atomic transaction failed" output
+'
+
 test_expect_success 'push --all can push to empty repo' '
 	d=$HTTPD_DOCUMENT_ROOT_PATH/empty-all.git &&
 	git init --bare "$d" &&
diff --git a/transport-helper.c b/transport-helper.c
index 9e1279b928..a9d690297e 100644
--- a/transport-helper.c
+++ b/transport-helper.c
@@ -854,6 +854,10 @@ static void set_common_push_options(struct transport *transport,
 			die(_("helper %s does not support --signed=if-asked"), name);
 	}
 
+	if (flags & TRANSPORT_PUSH_ATOMIC)
+		if (set_helper_option(transport, TRANS_OPT_ATOMIC, "true") != 0)
+			die(_("helper %s does not support --atomic"), name);
+
 	if (flags & TRANSPORT_PUSH_OPTIONS) {
 		struct string_list_item *item;
 		for_each_string_list_item(item, transport->push_options)
diff --git a/transport.h b/transport.h
index 0b5f7806f6..e0131daab9 100644
--- a/transport.h
+++ b/transport.h
@@ -208,6 +208,9 @@ void transport_check_allowed(const char *type);
 /* Filter objects for partial clone and fetch */
 #define TRANS_OPT_LIST_OBJECTS_FILTER "filter"
 
+/* Request atomic (all-or-nothing) updates when pushing */
+#define TRANS_OPT_ATOMIC "atomic"
+
 /**
  * Returns 0 if the option was used, non-zero otherwise. Prints a
  * message to stderr if the option is not used.

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] remote-curl: pass on atomic capability to remote side
  2019-10-15  1:07 [PATCH] remote-curl: pass on atomic capability to remote side brian m. carlson
@ 2019-10-15 16:40 ` Jeff King
  2019-10-16 23:43   ` brian m. carlson
  2019-10-16 23:45 ` [PATCH v2] " brian m. carlson
  1 sibling, 1 reply; 5+ messages in thread
From: Jeff King @ 2019-10-15 16:40 UTC (permalink / raw)
  To: brian m. carlson; +Cc: git, Duy Nguyen

On Tue, Oct 15, 2019 at 01:07:59AM +0000, brian m. carlson wrote:

> Fix this by passing the option from the transport code through to remote
> helpers, and from the HTTP remote helper down to send-pack.  With this
> change, we can detect if the server side rejects the push and report
> back appropriately.  Note the difference in the messages: the remote
> side reports "atomic transaction failed", while our own checking rejects
> pushes with the message "atomic push failed".

Good find. The patch looks good to me, except for one minor style nit in
the documentation (see below).

> Document the atomic option in the remote helper documentation, so other
> implementers can implement it if they like.

I wondered what would happen for existing helpers that do not implement
the option, but the behavior here:

> +	if (flags & TRANSPORT_PUSH_ATOMIC)
> +		if (set_helper_option(transport, TRANS_OPT_ATOMIC, "true") != 0)
> +			die(_("helper %s does not support --atomic"), name);
> +

looks like the right thing.

> As I mentioned in the commit message, to my knowledge, this
> functionality has been broken since the atomic capability was introduced
> circa 2.4.0.

Yeah, I tried this with v2.4.0 and it had the same problem (plus it's
very clear from your patch that it's not a regression, but just that
nobody bothered to implement it in the first place).

> diff --git a/Documentation/gitremote-helpers.txt b/Documentation/gitremote-helpers.txt
> index a5c3c04371..670d72c174 100644
> --- a/Documentation/gitremote-helpers.txt
> +++ b/Documentation/gitremote-helpers.txt
> @@ -509,6 +509,11 @@ set by Git if the remote helper has the 'option' capability.
>  	Indicate that only the objects wanted need to be fetched, not
>  	their dependents.
>  
> +'option atomic' {'true'|'false'}::
> +  When pushing, request the remote server to update refs in a single atomic
> +  transaction.  If successful, all refs will be updated, or none will.  If the
> +  remote side does not support this capability, the push will fail.
> +

This is implemented with a single space, but the rest of the option
bodies are indented with a tab. Asciidoc seems to format it identically
either way, though.

-Peff

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] remote-curl: pass on atomic capability to remote side
  2019-10-15 16:40 ` Jeff King
@ 2019-10-16 23:43   ` brian m. carlson
  2019-10-17  7:05     ` Junio C Hamano
  0 siblings, 1 reply; 5+ messages in thread
From: brian m. carlson @ 2019-10-16 23:43 UTC (permalink / raw)
  To: Jeff King; +Cc: git, Duy Nguyen, Junio C Hamano

[-- Attachment #1: Type: text/plain, Size: 1375 bytes --]

On 2019-10-15 at 16:40:29, Jeff King wrote:
> On Tue, Oct 15, 2019 at 01:07:59AM +0000, brian m. carlson wrote:
> > diff --git a/Documentation/gitremote-helpers.txt b/Documentation/gitremote-helpers.txt
> > index a5c3c04371..670d72c174 100644
> > --- a/Documentation/gitremote-helpers.txt
> > +++ b/Documentation/gitremote-helpers.txt
> > @@ -509,6 +509,11 @@ set by Git if the remote helper has the 'option' capability.
> >  	Indicate that only the objects wanted need to be fetched, not
> >  	their dependents.
> >  
> > +'option atomic' {'true'|'false'}::
> > +  When pushing, request the remote server to update refs in a single atomic
> > +  transaction.  If successful, all refs will be updated, or none will.  If the
> > +  remote side does not support this capability, the push will fail.
> > +
> 
> This is implemented with a single space, but the rest of the option
> bodies are indented with a tab. Asciidoc seems to format it identically
> either way, though.

Yeah, my default editor configuration for AsciiDoc is two spaces.  I
noticed that Junio's already picked it up for next, but I'll send a v2
with this fixed in case he wants to merge the fixed version to master
instead.

If it's more convenient, I can send a follow-up patch fixing the whitespace.
-- 
brian m. carlson: Houston, Texas, US
OpenPGP: https://keybase.io/bk2204

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 868 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH v2] remote-curl: pass on atomic capability to remote side
  2019-10-15  1:07 [PATCH] remote-curl: pass on atomic capability to remote side brian m. carlson
  2019-10-15 16:40 ` Jeff King
@ 2019-10-16 23:45 ` brian m. carlson
  1 sibling, 0 replies; 5+ messages in thread
From: brian m. carlson @ 2019-10-16 23:45 UTC (permalink / raw)
  To: git; +Cc: Duy Nguyen, Jeff King, Junio C Hamano

When pushing more than one reference with the --atomic option, the
server is supposed to perform a single atomic transaction to update the
references, leaving them either all to succeed or all to fail.  This
works fine when pushing locally or over SSH, but when pushing over HTTP,
we fail to pass the atomic capability to the remote side.  In fact, we
have not reported this capability to any remote helpers during the life
of the feature.

Now normally, things happen to work nevertheless, since we actually
check for most types of failures, such as non-fast-forward updates, on
the client side, and just abort the entire attempt.  However, if the
server side reports a problem, such as the inability to lock a ref, the
transaction isn't atomic, because we haven't passed the appropriate
capability over and the remote side has no way of knowing that we wanted
atomic behavior.

Fix this by passing the option from the transport code through to remote
helpers, and from the HTTP remote helper down to send-pack.  With this
change, we can detect if the server side rejects the push and report
back appropriately.  Note the difference in the messages: the remote
side reports "atomic transaction failed", while our own checking rejects
pushes with the message "atomic push failed".

Document the atomic option in the remote helper documentation, so other
implementers can implement it if they like.

Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net>
---
Changes from v1:
* Use tabs instead of spaces in docs.

 Documentation/gitremote-helpers.txt |  5 ++++
 remote-curl.c                       | 13 +++++++++-
 t/t5541-http-push-smart.sh          | 40 +++++++++++++++++++++++++++--
 transport-helper.c                  |  4 +++
 transport.h                         |  3 +++
 5 files changed, 62 insertions(+), 3 deletions(-)

diff --git a/Documentation/gitremote-helpers.txt b/Documentation/gitremote-helpers.txt
index a5c3c04371..f48a031dc3 100644
--- a/Documentation/gitremote-helpers.txt
+++ b/Documentation/gitremote-helpers.txt
@@ -509,6 +509,11 @@ set by Git if the remote helper has the 'option' capability.
 	Indicate that only the objects wanted need to be fetched, not
 	their dependents.
 
+'option atomic' {'true'|'false'}::
+	When pushing, request the remote server to update refs in a single atomic
+	transaction.  If successful, all refs will be updated, or none will.  If the
+	remote side does not support this capability, the push will fail.
+
 SEE ALSO
 --------
 linkgit:git-remote[1]
diff --git a/remote-curl.c b/remote-curl.c
index 051f26629d..5232ed84b6 100644
--- a/remote-curl.c
+++ b/remote-curl.c
@@ -40,7 +40,8 @@ struct options {
 		push_cert : 2,
 		deepen_relative : 1,
 		from_promisor : 1,
-		no_dependents : 1;
+		no_dependents : 1,
+		atomic : 1;
 };
 static struct options options;
 static struct string_list cas_options = STRING_LIST_INIT_DUP;
@@ -148,6 +149,14 @@ static int set_option(const char *name, const char *value)
 		else
 			return -1;
 		return 0;
+	} else if (!strcmp(name, "atomic")) {
+		if (!strcmp(value, "true"))
+			options.atomic = 1;
+		else if (!strcmp(value, "false"))
+			options.atomic = 0;
+		else
+			return -1;
+		return 0;
 	} else if (!strcmp(name, "push-option")) {
 		if (*value != '"')
 			string_list_append(&options.push_options, value);
@@ -1196,6 +1205,8 @@ static int push_git(struct discovery *heads, int nr_spec, char **specs)
 		argv_array_push(&args, "--signed=yes");
 	else if (options.push_cert == SEND_PACK_PUSH_CERT_IF_ASKED)
 		argv_array_push(&args, "--signed=if-asked");
+	if (options.atomic)
+		argv_array_push(&args, "--atomic");
 	if (options.verbosity == 0)
 		argv_array_push(&args, "--quiet");
 	else if (options.verbosity > 1)
diff --git a/t/t5541-http-push-smart.sh b/t/t5541-http-push-smart.sh
index 92bac43257..4c970787b0 100755
--- a/t/t5541-http-push-smart.sh
+++ b/t/t5541-http-push-smart.sh
@@ -184,11 +184,12 @@ test_expect_success 'push --atomic also prevents branch creation, reports collat
 	test_config -C "$d" http.receivepack true &&
 	up="$HTTPD_URL"/smart/atomic-branches.git &&
 
-	# Tell "$up" about two branches for now
+	# Tell "$up" about three branches for now
 	test_commit atomic1 &&
 	test_commit atomic2 &&
 	git branch collateral &&
-	git push "$up" master collateral &&
+	git branch other &&
+	git push "$up" master collateral other &&
 
 	# collateral is a valid push, but should be failed by atomic push
 	git checkout collateral &&
@@ -226,6 +227,41 @@ test_expect_success 'push --atomic also prevents branch creation, reports collat
 	grep "^ ! .*rejected.* collateral -> collateral .*atomic push failed" output
 '
 
+test_expect_success 'push --atomic fails on server-side errors' '
+	# Use previously set up repository
+	d=$HTTPD_DOCUMENT_ROOT_PATH/atomic-branches.git &&
+	test_config -C "$d" http.receivepack true &&
+	up="$HTTPD_URL"/smart/atomic-branches.git &&
+
+	# break ref updates for other on the remote site
+	mkdir "$d/refs/heads/other.lock" &&
+
+	# add the new commit to other
+	git branch -f other collateral &&
+
+	# --atomic should cause entire push to be rejected
+	test_must_fail git push --atomic "$up" atomic other 2>output  &&
+
+	# the new branch should not have been created upstream
+	test_must_fail git -C "$d" show-ref --verify refs/heads/atomic &&
+
+	# upstream should still reflect atomic2, the last thing we pushed
+	# successfully
+	git rev-parse atomic2 >expected &&
+	# ...to other.
+	git -C "$d" rev-parse refs/heads/other >actual &&
+	test_cmp expected actual &&
+
+	# the new branch should not have been created upstream
+	test_must_fail git -C "$d" show-ref --verify refs/heads/atomic &&
+
+	# the failed refs should be indicated to the user
+	grep "^ ! .*rejected.* other -> other .*atomic transaction failed" output &&
+
+	# the collateral failure refs should be indicated to the user
+	grep "^ ! .*rejected.* atomic -> atomic .*atomic transaction failed" output
+'
+
 test_expect_success 'push --all can push to empty repo' '
 	d=$HTTPD_DOCUMENT_ROOT_PATH/empty-all.git &&
 	git init --bare "$d" &&
diff --git a/transport-helper.c b/transport-helper.c
index 9e1279b928..a9d690297e 100644
--- a/transport-helper.c
+++ b/transport-helper.c
@@ -854,6 +854,10 @@ static void set_common_push_options(struct transport *transport,
 			die(_("helper %s does not support --signed=if-asked"), name);
 	}
 
+	if (flags & TRANSPORT_PUSH_ATOMIC)
+		if (set_helper_option(transport, TRANS_OPT_ATOMIC, "true") != 0)
+			die(_("helper %s does not support --atomic"), name);
+
 	if (flags & TRANSPORT_PUSH_OPTIONS) {
 		struct string_list_item *item;
 		for_each_string_list_item(item, transport->push_options)
diff --git a/transport.h b/transport.h
index 0b5f7806f6..e0131daab9 100644
--- a/transport.h
+++ b/transport.h
@@ -208,6 +208,9 @@ void transport_check_allowed(const char *type);
 /* Filter objects for partial clone and fetch */
 #define TRANS_OPT_LIST_OBJECTS_FILTER "filter"
 
+/* Request atomic (all-or-nothing) updates when pushing */
+#define TRANS_OPT_ATOMIC "atomic"
+
 /**
  * Returns 0 if the option was used, non-zero otherwise. Prints a
  * message to stderr if the option is not used.

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] remote-curl: pass on atomic capability to remote side
  2019-10-16 23:43   ` brian m. carlson
@ 2019-10-17  7:05     ` Junio C Hamano
  0 siblings, 0 replies; 5+ messages in thread
From: Junio C Hamano @ 2019-10-17  7:05 UTC (permalink / raw)
  To: brian m. carlson; +Cc: Jeff King, git, Duy Nguyen

"brian m. carlson" <sandals@crustytoothpaste.net> writes:

> Yeah, my default editor configuration for AsciiDoc is two spaces.  I
> noticed that Junio's already picked it up for next, but I'll send a v2
> with this fixed in case he wants to merge the fixed version to master
> instead.
>
> If it's more convenient, I can send a follow-up patch fixing the whitespace.

Either is fine.  At this point in the cycle where we are very close
to -rc0, I do not mind too much about having a few reverts of 'next'
to give a clean slate to an obvious and trivial fix.

Thanks.

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2019-10-17  7:07 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-10-15  1:07 [PATCH] remote-curl: pass on atomic capability to remote side brian m. carlson
2019-10-15 16:40 ` Jeff King
2019-10-16 23:43   ` brian m. carlson
2019-10-17  7:05     ` Junio C Hamano
2019-10-16 23:45 ` [PATCH v2] " brian m. carlson

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).