git/list[1] front-page[2] threads[3] people[4] search[5] about
 

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

From
Junio C Hamano <gitster@pobox.com>
Date
May 18, 2011, 21:38 UTC
Message-ID
<7v7h9nbsf6.fsf@alter.siamese.dyndns.org>
In-Reply-To
<m2y623so4l.fsf@igel.home>
Andreas Schwab <schwab@linux-m68k.org> writes:
Show 9 quoted lines
> Michael Schubert <mschub@elegosoft.com> writes:
>
>> +-e::
>> +--exit-code::
>> +	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 <mschub@elegosoft.com>
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.

Signed-off-by: Michael Schubert <mschub@elegosoft.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 t/t5512-ls-remote.sh            |   24 ++++++++++++++++++++++++
 Documentation/git-ls-remote.txt |    8 +++++++-
 builtin/ls-remote.c             |   11 +++++++++--
 3 files changed, 40 insertions(+), 3 deletions(-)
diff --git a/t/t5512-ls-remote.sh b/t/t5512-ls-remote.sh
index d191235..5c546c9 100755
--- a/t/t5512-ls-remote.sh
+++ b/t/t5512-ls-remote.sh
@@ -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
diff --git a/Documentation/git-ls-remote.txt b/Documentation/git-ls-remote.txt
index c3df8c0..7a9b86a 100644
--- a/Documentation/git-ls-remote.txt
+++ b/Documentation/git-ls-remote.txt
@@ -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
diff --git a/builtin/ls-remote.c b/builtin/ls-remote.c
index 1a1ff87..1022309 100644
--- a/builtin/ls-remote.c
+++ b/builtin/ls-remote.c
@@ -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;
 }
-- 
1.7.5.1.414.gb4910
Previous: Andreas SchwabNext: Michael Schubert
Message 9 of 10 in “Exit code of git-ls-remote”
  1. Kacper KornetMay 12, 2011
  2. Junio C HamanoMay 12, 2011
  3. Junio C HamanoMay 12, 2011
  4. ls-remote: optionally return non-zero on non-existing refsMichael Schubert, May 18, 2011
  5. Thiago FarinaMay 18, 2011
  6. Junio C HamanoMay 18, 2011
  7. ls-remote: optionally return non-zero on non-existing refsMichael Schubert, May 18, 2011
  8. Andreas SchwabMay 18, 2011
  9. Junio C HamanoMay 18, 2011
  10. Michael SchubertMay 18, 2011

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.