Skip to contentSkip to conversation

Exit code of git-ls-remote

PATCH In review Kacper Kornet 5 participants 10 messages Jun 15, 2016 Documentation

lore
Share & tools
lore
#0 lore raw

git-ls-remote behaves differently then git-show-ref when it cannot find any matching refs. While the latter returns non zero exit code in this case, the former always returns 0. Is there any specific reason for this behaviour?

Signature · 1 line
  Kacper Kornet
#1 lore raw
Quoted 4 lines · Kacper Kornet

git-ls-remote behaves differently then git-show-ref when it cannot find any matching refs. While the latter returns non zero exit code in this case, the former always returns 0. Is there any specific reason for this behaviour?

There is no specific reason other than "they happened to be implemented like so". These commands have always behaved that way and people are relying on their exit status, so unless there is a compelling reason, they will not change.

It is just a matter of opinion to consider that it is an error condition or just a normal case to see an empty set for a "List 'em and filter with these criteria" request. Outside git, "find /there -name no-such-file" exits with zero status, while "grep no-such-pattern file" exits with non-zero status.

You can rely on your knowledge of the commands and write your tests like this:

	test $(git ls-remote $there $pattern | wc -l) != 0 || die "none"
	git show-ref -q $pattern || die "none"

Alternatively, you can defend yourself against the next person who asks the same question by writing the last one as:

	test $(git show-ref $pattern | wc -l) != 0 || die "none"

Either would work.

#2 lore raw
Quoted 24 lines · Junio C Hamano
Quoted 4 lines · Kacper Kornet

git-ls-remote behaves differently then git-show-ref when it cannot find any matching refs. While the latter returns non zero exit code in this case, the former always returns 0. Is there any specific reason for this behaviour?

There is no specific reason other than "they happened to be implemented like so". These commands have always behaved that way and people are relying on their exit status, so unless there is a compelling reason, they will not change.

It is just a matter of opinion to consider that it is an error condition or just a normal case to see an empty set for a "List 'em and filter with these criteria" request. Outside git, "find /there -name no-such-file" exits with zero status, while "grep no-such-pattern file" exits with non-zero status.

You can rely on your knowledge of the commands and write your tests like this:

	test $(git ls-remote $there $pattern | wc -l) != 0 || die "none"
	git show-ref -q $pattern || die "none"
Quoted 6 lines

Alternatively, you can defend yourself against the next person who asks the same question by writing the last one as:

	test $(git show-ref $pattern | wc -l) != 0 || die "none"

Either would work.

Having said all that, I would not oppose if you want to teach ls-remote an option, e.g. --exit-code, to tell it to indicate if it found any ref with its exit status. You need to be careful when picking what code to use so that the caller can tell if the error was because there was no network connection or there was nothing to be listed, though.

#3 lore raw

[RFC/PATCH] ls-remote: optionally return non-zero on non-existing refs

git ls-remote returns zero no matter if the given references were found or not. Teach ls-remote an option --exit-status to make it optionally returning a non-zero status.

---

If there is just one existing ref in a list of non-exising refs, this will return zero though - as "git show-ref" does.

 Documentation/git-ls-remote.txt |    7 ++++++-
 builtin/ls-remote.c             |   12 ++++++++++--
 2 files changed, 16 insertions(+), 3 deletions(-)
Documentation/git-ls-remote.txt +6 -1
index c3df8c0..26cf705 100644
@@ -10,7 +10,7 @@ SYNOPSIS
 --------
 [verse]
 'git ls-remote' [--heads] [--tags]  [-u <exec> | --upload-pack <exec>]
-	      <repository> [<refs>...]
+	      [-e|--exit-code] <repository> [<refs>...]
 
 DESCRIPTION
 -----------
@@ -36,6 +36,11 @@ OPTIONS
 	SSH and where the SSH daemon does not use the PATH configured by the
 	user.
 
+-e::
+--exit-code::
+	Exit with a non-zero status code when the specified references don't
+	exist.
+
 <repository>::
 	Location of the repository.  The shorthand defined in
 	$GIT_DIR/branches/ can be used. Use "." (dot) to list references in
builtin/ls-remote.c +10 -2
index 1a1ff87..99fcf6b 100644
@@ -5,7 +5,7 @@
 
 static const char ls_remote_usage[] =
 "git ls-remote [--heads] [--tags]  [-u <exec> | --upload-pack <exec>]\n"
-"                     [-q|--quiet] [<repository> [<refs>...]]";
+"                     [-q|--quiet] [-e|--exit-code] [<repository> [<refs>...]]";
 
 /*
  * Is there one among the list of patterns that match the tail part
@@ -35,6 +35,7 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
 	unsigned flags = 0;
 	int get_url = 0;
 	int quiet = 0;
+	int exit_code = 0;
 	const char *uploadpack = NULL;
 	const char **pattern = NULL;
 
@@ -74,6 +75,10 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
 				get_url = 1;
 				continue;
 			}
+			if (!strcmp("--exit-code", arg) || !strcmp("-e", arg)) {
+				exit_code = 1;
+				continue;
+			}
 			usage(ls_remote_usage);
 		}
 		dest = arg;
@@ -115,12 +120,15 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
 
 	if (!dest && !quiet)
 		fprintf(stderr, "From %s\n", *remote->url);
+
 	for ( ; ref; ref = ref->next) {
 		if (!check_ref_type(ref, flags))
 			continue;
 		if (!tail_match(pattern, ref->name))
 			continue;
 		printf("%s	%s\n", sha1_to_hex(ref->old_sha1), ref->name);
+		exit_code = 0;
 	}
-	return 0;
+
+	return (exit_code) ? 2 : 0;
 }
Signature · 1 line
1.7.5.1
#4 lore raw

Re: [RFC/PATCH] ls-remote: optionally return non-zero on non-existing refs

Quoted 9 lines · Michael Schubert

git ls-remote returns zero no matter if the given references were found or not. Teach ls-remote an option --exit-status to make it optionally returning a non-zero status.

---

If there is just one existing ref in a list of non-exising refs, this will return zero though - as "git show-ref" does.

That's Ok, but...

Quoted 11 lines
builtin/ls-remote.c +4 -0
@@ -35,6 +35,7 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
...
+	int exit_code = 0;
...
@@ -74,6 +75,10 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
...
+			if (!strcmp("--exit-code", arg) || !strcmp("-e", arg)) {
+				exit_code = 1;
+				continue;
...

builtin/ls-remote.c int cmd_ls_remote(int argc, const char **argv, const char *prefix) · line 134

Quoted 3 lines
builtin/ls-remote.c +1 -0
@@ -115,12 +120,15 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
...
+	return (exit_code) ? 2 : 0;
 }

What is this insanity?

Wouldn't it be a lot more straightforward to write it like this?

	int status = 0;

	...

		if (!strcmp("--exit-code", arg)) {
			/* when we do not find any, return this value */
 			status = 2;
                        continue;
		}

	...

	return status;
#5 lore raw

PATCH v2 1/1 ls-remote: optionally return non-zero on non-existing refs

Quoted 21 lines · Junio C Hamano
Quoted 11 lines · Michael Schubert
(quoted diff) +4 -0
@@ -35,6 +35,7 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
...
+	int exit_code = 0;
...
@@ -74,6 +75,10 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
...
+			if (!strcmp("--exit-code", arg) || !strcmp("-e", arg)) {
+				exit_code = 1;
+				continue;
...

Review int cmd_ls_remote(int argc, const char **argv, const char *prefix)

Quoted 3 lines
(quoted diff) +1 -0
@@ -115,12 +120,15 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
...
+	return (exit_code) ? 2 : 0;
 }

What is this insanity?

Wouldn't it be a lot more straightforward to write it like this?

Quoted 13 lines
	int status = 0;

	...

		if (!strcmp("--exit-code", arg)) {
			/* when we do not find any, return this value */
 			status = 2;
                        continue;
		}

	...

	return status;

Sure, got confused. Fixed.

-- >8 --

Subject: [PATCH] ls-remote: optionally return non-zero on non-existing refs

git ls-remote returns zero no matter if the given references were found or not. Teach ls-remote an option --exit-status to make it optionally returning a non-zero status.

---
 Documentation/git-ls-remote.txt |    7 ++++++-
 builtin/ls-remote.c             |   12 ++++++++++--
 2 files changed, 16 insertions(+), 3 deletions(-)
Documentation/git-ls-remote.txt +6 -1
index c3df8c0..26cf705 100644
@@ -10,7 +10,7 @@ SYNOPSIS
 --------
 [verse]
 'git ls-remote' [--heads] [--tags]  [-u <exec> | --upload-pack <exec>]
-	      <repository> [<refs>...]
+	      [-e|--exit-code] <repository> [<refs>...]
 
 DESCRIPTION
 -----------
@@ -36,6 +36,11 @@ OPTIONS
 	SSH and where the SSH daemon does not use the PATH configured by the
 	user.
 
+-e::
+--exit-code::
+	Exit with a non-zero status code when the specified references don't
+	exist.
+
 <repository>::
 	Location of the repository.  The shorthand defined in
 	$GIT_DIR/branches/ can be used. Use "." (dot) to list references in
builtin/ls-remote.c +10 -2
index 1a1ff87..01e362f 100644
@@ -5,7 +5,7 @@
 
 static const char ls_remote_usage[] =
 "git ls-remote [--heads] [--tags]  [-u <exec> | --upload-pack <exec>]\n"
-"                     [-q|--quiet] [<repository> [<refs>...]]";
+"                     [-q|--quiet] [-e|--exit-code] [<repository> [<refs>...]]";
 
 /*
  * Is there one among the list of patterns that match the tail part
@@ -35,6 +35,7 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
 	unsigned flags = 0;
 	int get_url = 0;
 	int quiet = 0;
+	int status = 0;
 	const char *uploadpack = NULL;
 	const char **pattern = NULL;
 
@@ -74,6 +75,10 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
 				get_url = 1;
 				continue;
 			}
+			if (!strcmp("--exit-code", arg) || !strcmp("-e", arg)) {
+				status = 2;
+				continue;
+			}
 			usage(ls_remote_usage);
 		}
 		dest = arg;
@@ -115,12 +120,15 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
 
 	if (!dest && !quiet)
 		fprintf(stderr, "From %s\n", *remote->url);
+
 	for ( ; ref; ref = ref->next) {
 		if (!check_ref_type(ref, flags))
 			continue;
 		if (!tail_match(pattern, ref->name))
 			continue;
 		printf("%s	%s\n", sha1_to_hex(ref->old_sha1), ref->name);
+		status = 0;
 	}
-	return 0;
+
+	return status;
 }
Signature · 1 line
1.7.5.1
#6 lore raw

Re: [PATCH v2] ls-remote: optionally return non-zero on non-existing refs

Documentation/git-ls-remote.txt OPTIONS · line 42

Quoted 4 lines · Michael Schubert
+-e::
+--exit-code::
Documentation/git-ls-remote.txt +2 -0
+	Exit with a non-zero status code when the specified references don't
+	exist.

All or any of them? Also, the <refs> are patterns, so this should probably be talking about "not matching".

Andreas.

Signature · 3 lines
Andreas Schwab, [email protected]
GPG Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."
#7 lore raw

Re: [PATCH v2] ls-remote: optionally return non-zero on non-existing refs

Quoted 9 lines · Andreas Schwab

Review

Quoted 4 lines · Michael Schubert
+-e::
+--exit-code::
(quoted diff) +2 -0
+	Exit with a non-zero status code when the specified references don't
+	exist.

All or any of them? Also, the <refs> are patterns, so this should probably be talking about "not matching".

FWIW, here is what I am going to queue:

Points are:

(1) explain that the command indicates success in the normal case when we

     successfully talked with the remote end;

(2) help people who want to use this option by explicitly mentioning "2",

     not "non-zero", so that they can write:

	git ls-remote $there $pattern
        case $? in
        0)
        	: success! ;;
	2)
        	: successfully talked, but nothing found ;;
	*)
        	: we couldn't even talk to them ;;
	esac

(3) test!

-- >8 --
From: Michael Schubert <[email protected]>
Date: Wed, 18 May 2011 22:06:00 +0200
Subject: [PATCH] ls-remote: the --exit-code option reports "no matching refs"

The "git ls-remote" uses its exit status to indicate if it successfully talked with the remote repository. A new option "--exit-code" makes the command exit with status "2" when there is no refs to be listed, even when the command successfully talked with the remote repository.

This way, the caller can tell if we failed to contact the remote, or the remote did not have what we wanted to see. Of course, you can inspect the output from the command, which has been and will continue to be a valid way to check the same thing.

---
 t/t5512-ls-remote.sh            |   24 ++++++++++++++++++++++++
 Documentation/git-ls-remote.txt |    8 +++++++-
 builtin/ls-remote.c             |   11 +++++++++--
 3 files changed, 40 insertions(+), 3 deletions(-)
t/t5512-ls-remote.sh +24 -0
index d191235..5c546c9 100755
@@ -123,4 +123,28 @@ test_expect_success 'confuses pattern as remote when no remote specified' '
 
 '
 
+test_expect_success 'die with non-2 for wrong repository even with --exit-code' '
+	git ls-remote --exit-code ./no-such-repository ;# not &&
+	status=$? &&
+	test $status != 2 && test $status != 0
+'
+
+test_expect_success 'Report success even when nothing matches' '
+	git ls-remote other.git "refs/nsn/*" >actual &&
+	>expect &&
+	test_cmp expect actual
+'
+
+test_expect_success 'Report no-match with --exit-code' '
+	test_expect_code 2 git ls-remote --exit-code other.git "refs/nsn/*" >actual &&
+	>expect &&
+	test_cmp expect actual
+'
+
+test_expect_success 'Report match with --exit-code' '
+	git ls-remote --exit-code other.git "refs/tags/*" >actual &&
+	git ls-remote . tags/mark >expect &&
+	test_cmp expect actual
+'
+
 test_done
Documentation/git-ls-remote.txt +7 -1
index c3df8c0..7a9b86a 100644
@@ -10,7 +10,7 @@ SYNOPSIS
 --------
 [verse]
 'git ls-remote' [--heads] [--tags]  [-u <exec> | --upload-pack <exec>]
-	      <repository> [<refs>...]
+	      [--exit-code] <repository> [<refs>...]
 
 DESCRIPTION
 -----------
@@ -36,6 +36,12 @@ OPTIONS
 	SSH and where the SSH daemon does not use the PATH configured by the
 	user.
 
+--exit-code::
+	Exit with status "2" when no matching refs are found in the remote
+	repository. Usually the command exits with status "0" to indicate
+	it successfully talked with the remote repository, whether it
+	found any matching refs.
+
 <repository>::
 	Location of the repository.  The shorthand defined in
 	$GIT_DIR/branches/ can be used. Use "." (dot) to list references in
builtin/ls-remote.c +9 -2
index 1a1ff87..1022309 100644
@@ -5,7 +5,7 @@
 
 static const char ls_remote_usage[] =
 "git ls-remote [--heads] [--tags]  [-u <exec> | --upload-pack <exec>]\n"
-"                     [-q|--quiet] [<repository> [<refs>...]]";
+"                     [-q|--quiet] [--exit-code] [<repository> [<refs>...]]";
 
 /*
  * Is there one among the list of patterns that match the tail part
@@ -35,6 +35,7 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
 	unsigned flags = 0;
 	int get_url = 0;
 	int quiet = 0;
+	int status = 0;
 	const char *uploadpack = NULL;
 	const char **pattern = NULL;
 
@@ -74,6 +75,11 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
 				get_url = 1;
 				continue;
 			}
+			if (!strcmp("--exit-code", arg)) {
+				/* return this code if no refs are reported */
+				status = 2;
+				continue;
+			}
 			usage(ls_remote_usage);
 		}
 		dest = arg;
@@ -121,6 +127,7 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
 		if (!tail_match(pattern, ref->name))
 			continue;
 		printf("%s	%s\n", sha1_to_hex(ref->old_sha1), ref->name);
+		status = 0; /* we found something */
 	}
-	return 0;
+	return status;
 }
Signature · 1 line
1.7.5.1.414.gb4910
#8 lore raw

Re: [PATCH v2] ls-remote: optionally return non-zero on non-existing refs

Thanks!

#9 lore raw

Re: [RFC/PATCH] ls-remote: optionally return non-zero on non-existing refs

In reply to Michael Schubert
Quoted 78 lines · Michael Schubert

git ls-remote returns zero no matter if the given references were found or not. Teach ls-remote an option --exit-status to make it optionally returning a non-zero status.

---

If there is just one existing ref in a list of non-exising refs, this will return zero though - as "git show-ref" does.

 Documentation/git-ls-remote.txt |    7 ++++++- builtin/ls-remote.c             |   12 ++++++++++--

 2 files changed, 16 insertions(+), 3 deletions(-)
Documentation/git-ls-remote.txt +0 -0
index c3df8c0..26cf705 100644
@@ -10,7 +10,7 @@ SYNOPSIS

 --------
 [verse]
 'git ls-remote' [--heads] [--tags]  [-u <exec> | --upload-pack <exec>]
-             <repository> [<refs>...]
+             [-e|--exit-code] <repository> [<refs>...]

 DESCRIPTION
 -----------

Documentation/git-ls-remote.txt +0 -0
@@ -36,6 +36,11 @@ OPTIONS

       SSH and where the SSH daemon does not use the PATH configured by the user.

+-e::
+--exit-code::

+       Exit with a non-zero status code when the specified references don't
+       exist.
+
 <repository>::
       Location of the repository.  The shorthand defined in $GIT_DIR/branches/ can be used. Use "." (dot) to list references in

builtin/ls-remote.c +0 -0
index 1a1ff87..99fcf6b 100644
@@ -5,7 +5,7 @@
 

 static const char ls_remote_usage[] =
 "git ls-remote [--heads] [--tags]  [-u <exec> | --upload-pack <exec>]\n" -"                     [-q|--quiet] [<repository> [<refs>...]]";
+"                     [-q|--quiet] [-e|--exit-code] [<repository> [<refs>...]]";

 /*
 * Is there one among the list of patterns that match the tail part

builtin/ls-remote.c +6 -0
@@ -35,6 +35,7 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
        unsigned flags = 0;
        int get_url = 0;
        int quiet = 0;
+       int exit_code = 0;
        const char *uploadpack = NULL;
        const char **pattern = NULL;
 
@@ -74,6 +75,10 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
                                get_url = 1;
                                continue;
                        }
+                       if (!strcmp("--exit-code", arg) || !strcmp("-e", arg)) {
+                               exit_code = 1;
+                               continue;
+                       }
                        usage(ls_remote_usage);
                }
                dest = arg;
@@ -115,12 +120,15 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
 
        if (!dest && !quiet)
                fprintf(stderr, "From %s\n", *remote->url);
+
        for ( ; ref; ref = ref->next) {

builtin/ls-remote.c int cmd_ls_remote(int argc, const char **argv, const char *prefix) · line 133

Quoted 10 lines
builtin/ls-remote.c +3 -1
                if (!check_ref_type(ref, flags))
                        continue;
                if (!tail_match(pattern, ref->name))
                        continue;
                printf("%s      %s\n", sha1_to_hex(ref->old_sha1), ref->name);
+               exit_code = 0;
        }
-       return 0;
+
+       return (exit_code) ? 2 : 0;

We know, just a small nitpick. The parentheses around exit_code are unnecessary.

Quoted 9 lines
builtin/ls-remote.c +0 -1
  }
--

1.7.5.1

--
To unsubscribe from this list: send the line "unsubscribe git" in the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html