Skip to contentSkip to conversation

remote: add meaningful exit code on missing/existing

PATCH Superseded Ævar Arnfjörð Bjarmason 5 participants 7 messages Oct 26, 2020 – Oct 27, 2020 Documentation

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 remote: add meaningful exit code on missing/existing

Change the exit code for the likes of "git remote add/rename" to exit with 2 if the remote in question doesn't exist, and 3 if it does. Before we'd just die() and exit with the general 128 exit code.

This changes the output message from e.g.:

fatal: remote origin already exists.

To:

error: remote origin already exists.

Which I believe is a feature, since we generally use "fatal" for the generic errors, and "error" for the more specific ones with a custom exit code, but this part of the change may break code that already relies on stderr parsing (not that we ever supported that...).

The motivation for this is a discussion around some code in GitLab's gitaly which wanted to check this, and had to parse stderr to do so:
https://gitlab.com/gitlab-org/gitaly/-/merge_requests/2695

It's worth noting as an aside that a method of checking this that doesn't rely on that is to check with "git config" whether the value in question does or doesn't exist. That introduces a TOCTOU race condition, but on the other hand this code (e.g. "git remote add") already has a TOCTOU race.

We go through the config.lock for the actual setting of the config, but the pseudocode logic is:

    read_config();
    check_config_and_arg_sanity();
    save_config();

So e.g. if a sleep() is added right after the remote_is_configured() check in add() we'll clobber rermote.NAME.url, and add another (usually duplicate) remote.NAME.fetch entry (and other values, depending on invocation).

---
 Documentation/git-remote.txt | 11 ++++++++++
 builtin/remote.c             | 42 ++++++++++++++++++++++++------------
 t/t5505-remote.sh            | 16 +++++++-------
 3 files changed, 47 insertions(+), 22 deletions(-)
Documentation/git-remote.txt +11 -0
index 9659abbf8e..c1f1355f52 100644
@@ -203,6 +203,17 @@ The remote configuration is achieved using the `remote.origin.url` and
 `remote.origin.fetch` configuration variables.  (See
 linkgit:git-config[1]).
 
+EXIT STATUS
+-----------
+
+On success, the exit status is `0`.
+
+When subcommands such as 'add', 'rename' and 'remove' can't find the
+remote in question the exit status is `2`, when the remote already
+exists the exit status is `3`.
+
+On any other error, the exit status may be any other non-zero value.
+
 EXAMPLES
 --------
 
builtin/remote.c +28 -14

83 lines not on this page · raw message

t/t5505-remote.sh +8 -8
index 8d62edd98b..c99659d39e 100755
@@ -145,8 +145,8 @@ test_expect_success 'remove remote protects local branches' '
 test_expect_success 'remove errors out early when deleting non-existent branch' '
 	(
 		cd test &&
-		echo "fatal: No such remote: '\''foo'\''" >expect &&
-		test_must_fail git remote rm foo 2>actual &&
+		echo "error: No such remote: '\''foo'\''" >expect &&
+		test_expect_code 2 git remote rm foo 2>actual &&
 		test_i18ncmp expect actual
 	)
 '
@@ -173,24 +173,24 @@ test_expect_success 'remove remote with a branch without configured merge' '
 test_expect_success 'rename errors out early when deleting non-existent branch' '
 	(
 		cd test &&
-		echo "fatal: No such remote: '\''foo'\''" >expect &&
-		test_must_fail git remote rename foo bar 2>actual &&
+		echo "error: No such remote: '\''foo'\''" >expect &&
+		test_expect_code 2 git remote rename foo bar 2>actual &&
 		test_i18ncmp expect actual
 	)
 '
 
 test_expect_success 'add existing foreign_vcs remote' '
 	test_config remote.foo.vcs bar &&
-	echo "fatal: remote foo already exists." >expect &&
-	test_must_fail git remote add foo bar 2>actual &&
+	echo "error: remote foo already exists." >expect &&
+	test_expect_code 3 git remote add foo bar 2>actual &&
 	test_i18ncmp expect actual
 '
 
 test_expect_success 'add existing foreign_vcs remote' '
 	test_config remote.foo.vcs bar &&
 	test_config remote.bar.vcs bar &&
-	echo "fatal: remote bar already exists." >expect &&
-	test_must_fail git remote rename foo bar 2>actual &&
+	echo "error: remote bar already exists." >expect &&
+	test_expect_code 3 git remote rename foo bar 2>actual &&
 	test_i18ncmp expect actual
 '
 
Signature · 1 line
2.28.0.297.g1956fa8f8d
#1 lore raw
Quoted 6 lines · Ævar Arnfjörð Bjarmason

Change the exit code for the likes of "git remote add/rename" to exit with 2 if the remote in question doesn't exist, and 3 if it does. Before we'd just die() and exit with the general 128 exit code. [...]
So e.g. if a sleep() is added right after the remote_is_configured() check in add() we'll clobber rermote.NAME.url, and add

s/rermote/remote/

Quoted 4 lines

another (usually duplicate) remote.NAME.fetch entry (and other values, depending on invocation).

#2 lore raw
In reply to Ævar Arnfjörð Bjarmason
Quoted 8 lines · Ævar Arnfjörð Bjarmason

Change the exit code for the likes of "git remote add/rename" to exit with 2 if the remote in question doesn't exist, and 3 if it does. Before we'd just die() and exit with the general 128 exit code.

This changes the output message from e.g.:

fatal: remote origin already exists.

Quoted 8 lines

To:

error: remote origin already exists.

Which I believe is a feature, since we generally use "fatal" for the generic errors, and "error" for the more specific ones with a custom exit code, but this part of the change may break code that already relies on stderr parsing (not that we ever supported that...).

Sounds like a worthy goal. One thing that disturbs me somewhat, which the time will solve without any of our effort, is how the calling scripts and machinery decides when they can start relying on the exit codes. A "git remote tell-me-what-you-can-do" command that may or may not say "exitcode" to its standard output is added? "test $(git version) -ge 2.30 || old_way"?

The changes to the code all looked quite straight-forward and the tests look sensible, too.

Thanks.

#3 lore raw
In reply to Ævar Arnfjörð Bjarmason
Quoted 3 lines · Ævar Arnfjörð Bjarmason

Change the exit code for the likes of "git remote add/rename" to exit with 2 if the remote in question doesn't exist, and 3 if it does. Before we'd just die() and exit with the general 128 exit code.

I can't think of a particular reason not to do this. Though I suspect anything scripting may generally be better off using git-config directly for more flexibility and power.

Quoted 3 lines

The motivation for this is a discussion around some code in GitLab's gitaly which wanted to check this, and had to parse stderr to do so:
https://gitlab.com/gitlab-org/gitaly/-/merge_requests/2695

I didn't immediately see there the use cases you'd put this to. If it's something like "add an origin remote for fetching from a mirror", then that it is probably reasonable. But if you're thinking of something that may grow without bound (e.g., one remote per fork of the repository), keep in mind that modifying the config file is linear to its size (so creating or deleting N remotes is N^2).

Quoted 4 lines
 Documentation/git-remote.txt | 11 ++++++++++
 builtin/remote.c             | 42 ++++++++++++++++++++++++------------
 t/t5505-remote.sh            | 16 +++++++-------
 3 files changed, 47 insertions(+), 22 deletions(-)

The patch itself looks good to me, with one minor nit:

Documentation/git-remote.txt The remote configuration is achieved using the `remote.origin.url` and · line 213

Quoted 8 lines
Documentation/git-remote.txt +8 -0
+EXIT STATUS
+-----------
+
+On success, the exit status is `0`.
+
+When subcommands such as 'add', 'rename' and 'remove' can't find the
+remote in question the exit status is `2`, when the remote already
+exists the exit status is `3`.

This is a run-on sentence; the comma after 2 should be a period.

I'd also put an Oxford comma after rename, but that's a stylistic choice. :)

-Peff

#6 lore raw
In reply to Ævar Arnfjörð Bjarmason
Quoted 5 lines · Ævar Arnfjörð Bjarmason
Documentation/git-remote.txt +0 -0
index 9659abbf8e..c1f1355f52 100644

Documentation/git-remote.txt The remote configuration is achieved using the `remote.origin.url` and · line 211

Quoted 9 lines
Documentation/git-remote.txt +6 -0
@@ -203,6 +203,17 @@ The remote configuration is achieved using the `remote.origin.url` and
 `remote.origin.fetch` configuration variables.  (See
 linkgit:git-config[1]).
 
+EXIT STATUS
+-----------
+
+On success, the exit status is `0`.
+
+When subcommands such as 'add', 'rename' and 'remove' can't find the

at least a comma before 'and', but I consider 'or' better here anyway:

When subcommands such as 'add', 'rename', or 'remove' can't find the

Bert

Quoted 8 lines
Documentation/git-remote.txt +5 -0
+remote in question the exit status is `2`, when the remote already
+exists the exit status is `3`.
+
+On any other error, the exit status may be any other non-zero value.
+
 EXAMPLES
 --------
 
#4 lore raw
In reply to Jeff King
Quoted 4 lines · Jeff King

This is a run-on sentence; the comma after 2 should be a period.

I'd also put an Oxford comma after rename, but that's a stylistic choice. :)

Will queue with this squashed in.

 Documentation/git-remote.txt | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
Documentation/git-remote.txt +3 -3
index 7c84631343..31c29c9b31 100644
@@ -208,9 +208,9 @@ EXIT STATUS
 
 On success, the exit status is `0`.
 
-When subcommands such as 'add', 'rename' and 'remove' can't find the
-remote in question the exit status is `2`, when the remote already
-exists the exit status is `3`.
+When subcommands such as 'add', 'rename', and 'remove' can't find the
+remote in question, the exit status is `2`. When the remote already
+exists, the exit status is `3`.
 
 On any other error, the exit status may be any other non-zero value.
 
#5 lore raw

PATCH v2 1/1 remote: add meaningful exit code on missing/existing

Change the exit code for the likes of "git remote add/rename" to exit with 2 if the remote in question doesn't exist, and 3 if it does. Before we'd just die() and exit with the general 128 exit code.

This changes the output message from e.g.:

fatal: remote origin already exists.

To:

error: remote origin already exists.

Which I believe is a feature, since we generally use "fatal" for the generic errors, and "error" for the more specific ones with a custom exit code, but this part of the change may break code that already relies on stderr parsing (not that we ever supported that...).

The motivation for this is a discussion around some code in GitLab's gitaly which wanted to check this, and had to parse stderr to do so:
https://gitlab.com/gitlab-org/gitaly/-/merge_requests/2695

It's worth noting as an aside that a method of checking this that doesn't rely on that is to check with "git config" whether the value in question does or doesn't exist. That introduces a TOCTOU race condition, but on the other hand this code (e.g. "git remote add") already has a TOCTOU race.

We go through the config.lock for the actual setting of the config, but the pseudocode logic is:

    read_config();
    check_config_and_arg_sanity();
    save_config();

So e.g. if a sleep() is added right after the remote_is_configured() check in add() we'll clobber remote.NAME.url, and add another (usually duplicate) remote.NAME.fetch entry (and other values, depending on invocation).

---

Quoted 8 lines · Junio C Hamano
Quoted 4 lines · Jeff King

This is a run-on sentence; the comma after 2 should be a period.

I'd also put an Oxford comma after rename, but that's a stylistic choice. :)

Will queue with this squashed in.

Thanks, here's that version plus Eric's suggested typo fix in the commit message.

Range-diff:
1: 26037f6294 ! 1: 4e44aed049 remote: add meaningful exit code on missing/existing
@@ Commit message
save_config();

So e.g. if a sleep() is added right after the remote_is_configured()
- check in add() we'll clobber rermote.NAME.url, and add
- another (usually duplicate) remote.NAME.fetch entry (and other values,
- depending on invocation).
+ check in add() we'll clobber remote.NAME.url, and add another (usually
+ duplicate) remote.NAME.fetch entry (and other values, depending on
+ invocation).

@@ Documentation/git-remote.txt: The remote configuration is achieved using the `re
+
+On success, the exit status is 0.
+
-+When subcommands such as 'add', 'rename' and 'remove' can't find the -+remote in question the exit status is 2, when the remote already -+exists the exit status is 3.
++When subcommands such as 'add', 'rename', and 'remove' can't find the ++remote in question, the exit status is 2. When the remote already ++exists, the exit status is 3.
+
+On any other error, the exit status may be any other non-zero value. +

 Documentation/git-remote.txt | 11 ++++++++++
 builtin/remote.c             | 42 ++++++++++++++++++++++++------------
 t/t5505-remote.sh            | 16 +++++++-------
 3 files changed, 47 insertions(+), 22 deletions(-)
Documentation/git-remote.txt +11 -0
index ea73386c81..31c29c9b31 100644
@@ -203,6 +203,17 @@ The remote configuration is achieved using the `remote.origin.url` and
 `remote.origin.fetch` configuration variables.  (See
 linkgit:git-config[1]).
 
+EXIT STATUS
+-----------
+
+On success, the exit status is `0`.
+
+When subcommands such as 'add', 'rename', and 'remove' can't find the
+remote in question, the exit status is `2`. When the remote already
+exists, the exit status is `3`.
+
+On any other error, the exit status may be any other non-zero value.
+
 EXAMPLES
 --------
 
builtin/remote.c +28 -14

83 lines not on this page · raw message

t/t5505-remote.sh +8 -8
index 8d62edd98b..c99659d39e 100755
@@ -145,8 +145,8 @@ test_expect_success 'remove remote protects local branches' '
 test_expect_success 'remove errors out early when deleting non-existent branch' '
 	(
 		cd test &&
-		echo "fatal: No such remote: '\''foo'\''" >expect &&
-		test_must_fail git remote rm foo 2>actual &&
+		echo "error: No such remote: '\''foo'\''" >expect &&
+		test_expect_code 2 git remote rm foo 2>actual &&
 		test_i18ncmp expect actual
 	)
 '
@@ -173,24 +173,24 @@ test_expect_success 'remove remote with a branch without configured merge' '
 test_expect_success 'rename errors out early when deleting non-existent branch' '
 	(
 		cd test &&
-		echo "fatal: No such remote: '\''foo'\''" >expect &&
-		test_must_fail git remote rename foo bar 2>actual &&
+		echo "error: No such remote: '\''foo'\''" >expect &&
+		test_expect_code 2 git remote rename foo bar 2>actual &&
 		test_i18ncmp expect actual
 	)
 '
 
 test_expect_success 'add existing foreign_vcs remote' '
 	test_config remote.foo.vcs bar &&
-	echo "fatal: remote foo already exists." >expect &&
-	test_must_fail git remote add foo bar 2>actual &&
+	echo "error: remote foo already exists." >expect &&
+	test_expect_code 3 git remote add foo bar 2>actual &&
 	test_i18ncmp expect actual
 '
 
 test_expect_success 'add existing foreign_vcs remote' '
 	test_config remote.foo.vcs bar &&
 	test_config remote.bar.vcs bar &&
-	echo "fatal: remote bar already exists." >expect &&
-	test_must_fail git remote rename foo bar 2>actual &&
+	echo "error: remote bar already exists." >expect &&
+	test_expect_code 3 git remote rename foo bar 2>actual &&
 	test_i18ncmp expect actual
 '
 
Signature · 1 line
2.28.0.297.g1956fa8f8d