From: Thiago Farina Date: Wed, 18 May 2011 16:59:53 GMT Subject: Re: [RFC/PATCH] ls-remote: optionally return non-zero on non-existing refs Message-ID: In-Reply-To: <4DD3D490.1090503@elegosoft.com> On Wed, May 18, 2011 at 11:15 AM, Michael Schubert wrote: > 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. > > Signed-off-by: Michael Schubert > --- > > 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(-) > > diff --git a/Documentation/git-ls-remote.txt b/Documentation/git-ls-remote.txt > index c3df8c0..26cf705 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 | --upload-pack ] > -             [...] > +             [-e|--exit-code] [...] > >  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. > + >  :: >        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..99fcf6b 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 | --upload-pack ]\n" > -"                     [-q|--quiet] [ [...]]"; > +"                     [-q|--quiet] [-e|--exit-code] [ [...]]"; > >  /* >  * 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; We know, just a small nitpick. The parentheses around exit_code are unnecessary. >  } > -- > 1.7.5.1 > > -- > To unsubscribe from this list: send the line "unsubscribe git" in > the body of a message to majordomo@vger.kernel.org > More majordomo info at  http://vger.kernel.org/majordomo-info.html >