Code

ls-remote: the --exit-code option reports "no matching refs"
authorMichael Schubert <mschub@elegosoft.com>
Wed, 18 May 2011 20:06:00 +0000 (22:06 +0200)
committerJunio C Hamano <gitster@pobox.com>
Wed, 18 May 2011 21:37:46 +0000 (14:37 -0700)
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>
Documentation/git-ls-remote.txt
builtin/ls-remote.c
t/t5512-ls-remote.sh

index c3df8c0ebe48af7192f7d593c13af69825b6560f..7a9b86a58a1c00c08803e9ef40bb6a17146125a9 100644 (file)
@@ -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
index 1a1ff87e8f9ed7db84f106960f36888835f7057b..10223092a9ebfed4092db680529a42ca8886a9f2 100644 (file)
@@ -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;
 }
index d1912351db7da4a7c457fd44895b5ea076c84e7e..5c546c99a58854ae0f430e469dad525af52e9292 100755 (executable)
@@ -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