diff mbox series

[v3,6/8] send-pack: new return code "ERROR_SEND_PACK_BAD_REF_STATUS"

Message ID 77f5e128fc1b66a9c8b0df3ce274a98921800685.1733830410.git.zhiyou.jx@alibaba-inc.com (mailing list archive)
State New
Headers show
Series fix behaviors of git-push --porcelain | expand

Commit Message

Jiang Xin Dec. 10, 2024, 11:36 a.m. UTC
From: Jiang Xin <zhiyou.jx@alibaba-inc.com>

The "push_refs" function in the transport_vtable is the handler for
git-push operation. All the "push_refs" functions for different
transports (protocols) should have the same behavior, but the behavior
of "git_transport_push()" function for builtin_smart_vtable in
"transport.c" (which calls "send_pack()" in "send-pack.c") differs from
the handler of the HTTP protocol.

The "push_refs()" function for the HTTP protocol which calls the
"push_refs_with_push()" function in "transport-helper.c" will return 0
even when a bad REF_STATUS (such as REF_STATUS_REJECT_NONFASTFORWARD)
was found. But "send_pack()" for Git smart protocol will return -1 for
a bad REF_STATUS.

We cannot ignore bad REF_STATUS directly in the "send_pack()" function,
because the function is also used in "builtin/send-pack.c". So we add a
new non-zero error code "SEND_PACK_ERROR_REF_STATUS" for "send_pack()".
We can choose to ignore the specific error code in the
"git_transport_push()" function to have the same behavior as
"push_refs()" for HTTP protocol.

Signed-off-by: Jiang Xin <zhiyou.jx@alibaba-inc.com>
---
 send-pack.c | 9 ++-------
 send-pack.h | 3 +++
 transport.c | 7 +++++++
 3 files changed, 12 insertions(+), 7 deletions(-)

Comments

Patrick Steinhardt Dec. 16, 2024, 8:36 a.m. UTC | #1
On Tue, Dec 10, 2024 at 07:36:26PM +0800, Jiang Xin wrote:
> diff --git a/send-pack.c b/send-pack.c
> index 6677c44e8a..f1556dd53c 100644
> --- a/send-pack.c
> +++ b/send-pack.c
> @@ -630,7 +630,7 @@ int send_pack(struct send_pack_args *args,
>  				reject_atomic_push(remote_refs, args->send_mirror);
>  				error("atomic push failed for ref %s. status: %d",
>  				      ref->name, ref->status);
> -				ret = args->porcelain ? 0 : -1;
> +				ret = ERROR_SEND_PACK_BAD_REF_STATUS;
>  				goto out;
>  			}
>  			/* else fallthrough */

Okay, this change looks sensible -- instead of having different
behaviour with `--porcelain` we now have the exact same behaviour with
and without that flag set, which is good.

> @@ -761,11 +761,6 @@ int send_pack(struct send_pack_args *args,
>  	if (ret < 0)
>  		goto out;
>  
> -	if (args->porcelain) {
> -		ret = 0;
> -		goto out;
> -	}

We don't reset the error code here anymore.

>  	for (ref = remote_refs; ref; ref = ref->next) {
>  		switch (ref->status) {
>  		case REF_STATUS_NONE:
> @@ -773,7 +768,7 @@ int send_pack(struct send_pack_args *args,
>  		case REF_STATUS_OK:
>  			break;
>  		default:
> -			ret = -1;
> +			ret = ERROR_SEND_PACK_BAD_REF_STATUS;
>  			goto out;
>  		}
>  	}

And this is the equivalent change without `--atomic`.

Also, none of the existing code paths used to set `ret = 1`. We do call
`ret = receive_status()` and bubble that up, but neither that nor any of
its transitive calls set `ret = 1`, either. So this should be fine and
we don't seem to have conflicting error codes.

> diff --git a/send-pack.h b/send-pack.h
> index 7edb80596c..ee88f9fe9f 100644
> --- a/send-pack.h
> +++ b/send-pack.h
> @@ -12,6 +12,9 @@ struct ref;
>  #define SEND_PACK_PUSH_CERT_IF_ASKED 1
>  #define SEND_PACK_PUSH_CERT_ALWAYS 2
>  
> +/* Custom exit code from send_pack. */
> +#define ERROR_SEND_PACK_BAD_REF_STATUS 1
> +
>  struct send_pack_args {
>  	const char *url;
>  	unsigned verbose:1,

It might help to document `send_pack()` accordingly and point out the
special return code.

> diff --git a/transport.c b/transport.c
> index 47fda6a773..454d7f21a9 100644
> --- a/transport.c
> +++ b/transport.c
> @@ -914,6 +914,13 @@ static int git_transport_push(struct transport *transport, struct ref *remote_re
>  	case protocol_v0:
>  		ret = send_pack(&args, data->fd, data->conn, remote_refs,
>  				&data->extra_have);
> +		/*
> +		 * Ignore the specific error code to maintain consistent behavior
> +		 * with the "push_refs()" function across different transports,
> +		 * such as "push_refs_with_push()" for HTTP protocol.
> +		 */
> +		if (ret == ERROR_SEND_PACK_BAD_REF_STATUS)
> +			ret = 0;
>  		break;
>  	case protocol_unknown_version:
>  		BUG("unknown protocol version");

And here we ignore the error code to retain compatibility.

Is there any case where this causes a user-visible change in behaviour
already? If so, we can add a test for this?

Patrick
diff mbox series

Patch

diff --git a/send-pack.c b/send-pack.c
index 6677c44e8a..f1556dd53c 100644
--- a/send-pack.c
+++ b/send-pack.c
@@ -630,7 +630,7 @@  int send_pack(struct send_pack_args *args,
 				reject_atomic_push(remote_refs, args->send_mirror);
 				error("atomic push failed for ref %s. status: %d",
 				      ref->name, ref->status);
-				ret = args->porcelain ? 0 : -1;
+				ret = ERROR_SEND_PACK_BAD_REF_STATUS;
 				goto out;
 			}
 			/* else fallthrough */
@@ -761,11 +761,6 @@  int send_pack(struct send_pack_args *args,
 	if (ret < 0)
 		goto out;
 
-	if (args->porcelain) {
-		ret = 0;
-		goto out;
-	}
-
 	for (ref = remote_refs; ref; ref = ref->next) {
 		switch (ref->status) {
 		case REF_STATUS_NONE:
@@ -773,7 +768,7 @@  int send_pack(struct send_pack_args *args,
 		case REF_STATUS_OK:
 			break;
 		default:
-			ret = -1;
+			ret = ERROR_SEND_PACK_BAD_REF_STATUS;
 			goto out;
 		}
 	}
diff --git a/send-pack.h b/send-pack.h
index 7edb80596c..ee88f9fe9f 100644
--- a/send-pack.h
+++ b/send-pack.h
@@ -12,6 +12,9 @@  struct ref;
 #define SEND_PACK_PUSH_CERT_IF_ASKED 1
 #define SEND_PACK_PUSH_CERT_ALWAYS 2
 
+/* Custom exit code from send_pack. */
+#define ERROR_SEND_PACK_BAD_REF_STATUS 1
+
 struct send_pack_args {
 	const char *url;
 	unsigned verbose:1,
diff --git a/transport.c b/transport.c
index 47fda6a773..454d7f21a9 100644
--- a/transport.c
+++ b/transport.c
@@ -914,6 +914,13 @@  static int git_transport_push(struct transport *transport, struct ref *remote_re
 	case protocol_v0:
 		ret = send_pack(&args, data->fd, data->conn, remote_refs,
 				&data->extra_have);
+		/*
+		 * Ignore the specific error code to maintain consistent behavior
+		 * with the "push_refs()" function across different transports,
+		 * such as "push_refs_with_push()" for HTTP protocol.
+		 */
+		if (ret == ERROR_SEND_PACK_BAD_REF_STATUS)
+			ret = 0;
 		break;
 	case protocol_unknown_version:
 		BUG("unknown protocol version");