{"thread":{"id":"27336","subject":"Exit code of git-ls-remote","startedAt":"2011-05-12T12:05:36Z","lastAt":"2011-05-18T21:53:54Z","messageCount":10,"participants":["Kacper Kornet","Junio C Hamano","Michael Schubert","Thiago Farina","Andreas Schwab"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"167726","messageId":"20110512120536.GA22033@camk.edu.pl","threadId":"27336","inReplyTo":null,"subject":"Exit code of git-ls-remote","fromName":"Kacper Kornet","fromEmail":"draenog@pld-linux.org","sentAt":"2011-05-12T12:05:36Z","receivedAt":"2011-05-12T12:05:36Z","isPatch":false,"sender":{"key":"draenog@pld-linux.org","avatar":"https://avatars.githubusercontent.com/u/608762?v=4"},"body":"git-ls-remote behaves differently then git-show-ref when it cannot find\nany matching refs. While the latter returns non zero exit code in this\ncase, the former always returns 0. Is there any specific reason for this\nbehaviour?\n\n-- \n  Kacper Kornet\n"},{"id":"167743","messageId":"7voc37dh4l.fsf@alter.siamese.dyndns.org","threadId":"27336","inReplyTo":"20110512120536.GA22033@camk.edu.pl","subject":"Re: Exit code of git-ls-remote","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2011-05-12T16:21:30Z","receivedAt":"2011-05-12T16:21:30Z","isPatch":false,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Kacper Kornet <draenog@pld-linux.org> writes:\n\n> git-ls-remote behaves differently then git-show-ref when it cannot find\n> any matching refs. While the latter returns non zero exit code in this\n> case, the former always returns 0. Is there any specific reason for this\n> behaviour?\n\nThere is no specific reason other than \"they happened to be implemented\nlike so\".  These commands have always behaved that way and people are\nrelying on their exit status, so unless there is a compelling reason, they\nwill not change.\n\nIt is just a matter of opinion to consider that it is an error condition\nor just a normal case to see an empty set for a \"List 'em and filter with\nthese criteria\" request. Outside git, \"find /there -name no-such-file\"\nexits with zero status, while \"grep no-such-pattern file\" exits with\nnon-zero status.\n\nYou can rely on your knowledge of the commands and write your tests like\nthis:\n\n\ttest $(git ls-remote $there $pattern | wc -l) != 0 || die \"none\"\n\tgit show-ref -q $pattern || die \"none\"\n\nAlternatively, you can defend yourself against the next person who asks\nthe same question by writing the last one as:\n\n\ttest $(git show-ref $pattern | wc -l) != 0 || die \"none\"\n\nEither would work.\n"},{"id":"167745","messageId":"7vk4dvdg6x.fsf@alter.siamese.dyndns.org","threadId":"27336","inReplyTo":"7voc37dh4l.fsf@alter.siamese.dyndns.org","subject":"Re: Exit code of git-ls-remote","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2011-05-12T16:41:42Z","receivedAt":"2011-05-12T16:41:42Z","isPatch":false,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Junio C Hamano <gitster@pobox.com> writes:\n\n> Kacper Kornet <draenog@pld-linux.org> writes:\n>\n>> git-ls-remote behaves differently then git-show-ref when it cannot find\n>> any matching refs. While the latter returns non zero exit code in this\n>> case, the former always returns 0. Is there any specific reason for this\n>> behaviour?\n>\n> There is no specific reason other than \"they happened to be implemented\n> like so\".  These commands have always behaved that way and people are\n> relying on their exit status, so unless there is a compelling reason, they\n> will not change.\n>\n> It is just a matter of opinion to consider that it is an error condition\n> or just a normal case to see an empty set for a \"List 'em and filter with\n> these criteria\" request. Outside git, \"find /there -name no-such-file\"\n> exits with zero status, while \"grep no-such-pattern file\" exits with\n> non-zero status.\n>\n> You can rely on your knowledge of the commands and write your tests like\n> this:\n>\n> \ttest $(git ls-remote $there $pattern | wc -l) != 0 || die \"none\"\n> \tgit show-ref -q $pattern || die \"none\"\n>\n> Alternatively, you can defend yourself against the next person who asks\n> the same question by writing the last one as:\n>\n> \ttest $(git show-ref $pattern | wc -l) != 0 || die \"none\"\n>\n> Either would work.\n\nHaving said all that, I would not oppose if you want to teach ls-remote an\noption, e.g. --exit-code, to tell it to indicate if it found any ref with\nits exit status. You need to be careful when picking what code to use so\nthat the caller can tell if the error was because there was no network\nconnection or there was nothing to be listed, though.\n"},{"id":"168138","messageId":"4DD3D490.1090503@elegosoft.com","threadId":"27336","inReplyTo":"7vk4dvdg6x.fsf@alter.siamese.dyndns.org","subject":"[RFC/PATCH] ls-remote: optionally return non-zero on non-existing refs","fromName":"Michael Schubert","fromEmail":"mschub@elegosoft.com","sentAt":"2011-05-18T14:15:44Z","receivedAt":"2011-05-18T14:15:44Z","isPatch":true,"sender":{"key":"mschub@elegosoft.com","avatar":null},"body":"git ls-remote returns zero no matter if the given references were found\nor not. Teach ls-remote an option --exit-status to make it optionally\nreturning a non-zero status.\n\nSigned-off-by: Michael Schubert <mschub@elegosoft.com>\n---\n\nIf there is just one existing ref in a list of non-exising refs, this will\nreturn zero though - as \"git show-ref\" does.\n\n\n Documentation/git-ls-remote.txt |    7 ++++++-\n builtin/ls-remote.c             |   12 ++++++++++--\n 2 files changed, 16 insertions(+), 3 deletions(-)\n\ndiff --git a/Documentation/git-ls-remote.txt b/Documentation/git-ls-remote.txt\nindex c3df8c0..26cf705 100644\n--- a/Documentation/git-ls-remote.txt\n+++ b/Documentation/git-ls-remote.txt\n@@ -10,7 +10,7 @@ SYNOPSIS\n --------\n [verse]\n 'git ls-remote' [--heads] [--tags]  [-u <exec> | --upload-pack <exec>]\n-\t      <repository> [<refs>...]\n+\t      [-e|--exit-code] <repository> [<refs>...]\n \n DESCRIPTION\n -----------\n@@ -36,6 +36,11 @@ OPTIONS\n \tSSH and where the SSH daemon does not use the PATH configured by the\n \tuser.\n \n+-e::\n+--exit-code::\n+\tExit with a non-zero status code when the specified references don't\n+\texist.\n+\n <repository>::\n \tLocation of the repository.  The shorthand defined in\n \t$GIT_DIR/branches/ can be used. Use \".\" (dot) to list references in\ndiff --git a/builtin/ls-remote.c b/builtin/ls-remote.c\nindex 1a1ff87..99fcf6b 100644\n--- a/builtin/ls-remote.c\n+++ b/builtin/ls-remote.c\n@@ -5,7 +5,7 @@\n \n static const char ls_remote_usage[] =\n \"git ls-remote [--heads] [--tags]  [-u <exec> | --upload-pack <exec>]\\n\"\n-\"                     [-q|--quiet] [<repository> [<refs>...]]\";\n+\"                     [-q|--quiet] [-e|--exit-code] [<repository> [<refs>...]]\";\n \n /*\n  * Is there one among the list of patterns that match the tail part\n@@ -35,6 +35,7 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)\n \tunsigned flags = 0;\n \tint get_url = 0;\n \tint quiet = 0;\n+\tint exit_code = 0;\n \tconst char *uploadpack = NULL;\n \tconst char **pattern = NULL;\n \n@@ -74,6 +75,10 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)\n \t\t\t\tget_url = 1;\n \t\t\t\tcontinue;\n \t\t\t}\n+\t\t\tif (!strcmp(\"--exit-code\", arg) || !strcmp(\"-e\", arg)) {\n+\t\t\t\texit_code = 1;\n+\t\t\t\tcontinue;\n+\t\t\t}\n \t\t\tusage(ls_remote_usage);\n \t\t}\n \t\tdest = arg;\n@@ -115,12 +120,15 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)\n \n \tif (!dest && !quiet)\n \t\tfprintf(stderr, \"From %s\\n\", *remote->url);\n+\n \tfor ( ; ref; ref = ref->next) {\n \t\tif (!check_ref_type(ref, flags))\n \t\t\tcontinue;\n \t\tif (!tail_match(pattern, ref->name))\n \t\t\tcontinue;\n \t\tprintf(\"%s\t%s\\n\", sha1_to_hex(ref->old_sha1), ref->name);\n+\t\texit_code = 0;\n \t}\n-\treturn 0;\n+\n+\treturn (exit_code) ? 2 : 0;\n }\n-- \n1.7.5.1\n"},{"id":"168145","messageId":"BANLkTi=juUtyPFNZQQ7h0jWHgmK2D1hFbA@mail.gmail.com","threadId":"27336","inReplyTo":"4DD3D490.1090503@elegosoft.com","subject":"Re: [RFC/PATCH] ls-remote: optionally return non-zero on non-existing refs","fromName":"Thiago Farina","fromEmail":"tfransosi@gmail.com","sentAt":"2011-05-18T16:59:53Z","receivedAt":"2011-05-18T16:59:53Z","isPatch":true,"sender":{"key":"tfransosi@gmail.com","avatar":"https://avatars.githubusercontent.com/u/970071?v=4"},"body":"On Wed, May 18, 2011 at 11:15 AM, Michael Schubert <mschub@elegosoft.com> wrote:\n> git ls-remote returns zero no matter if the given references were found\n> or not. Teach ls-remote an option --exit-status to make it optionally\n> returning a non-zero status.\n>\n> Signed-off-by: Michael Schubert <mschub@elegosoft.com>\n> ---\n>\n> If there is just one existing ref in a list of non-exising refs, this will\n> return zero though - as \"git show-ref\" does.\n>\n>\n>  Documentation/git-ls-remote.txt |    7 ++++++-\n>  builtin/ls-remote.c             |   12 ++++++++++--\n>  2 files changed, 16 insertions(+), 3 deletions(-)\n>\n> diff --git a/Documentation/git-ls-remote.txt b/Documentation/git-ls-remote.txt\n> index c3df8c0..26cf705 100644\n> --- a/Documentation/git-ls-remote.txt\n> +++ b/Documentation/git-ls-remote.txt\n> @@ -10,7 +10,7 @@ SYNOPSIS\n>  --------\n>  [verse]\n>  'git ls-remote' [--heads] [--tags]  [-u <exec> | --upload-pack <exec>]\n> -             <repository> [<refs>...]\n> +             [-e|--exit-code] <repository> [<refs>...]\n>\n>  DESCRIPTION\n>  -----------\n> @@ -36,6 +36,11 @@ OPTIONS\n>        SSH and where the SSH daemon does not use the PATH configured by the\n>        user.\n>\n> +-e::\n> +--exit-code::\n> +       Exit with a non-zero status code when the specified references don't\n> +       exist.\n> +\n>  <repository>::\n>        Location of the repository.  The shorthand defined in\n>        $GIT_DIR/branches/ can be used. Use \".\" (dot) to list references in\n> diff --git a/builtin/ls-remote.c b/builtin/ls-remote.c\n> index 1a1ff87..99fcf6b 100644\n> --- a/builtin/ls-remote.c\n> +++ b/builtin/ls-remote.c\n> @@ -5,7 +5,7 @@\n>\n>  static const char ls_remote_usage[] =\n>  \"git ls-remote [--heads] [--tags]  [-u <exec> | --upload-pack <exec>]\\n\"\n> -\"                     [-q|--quiet] [<repository> [<refs>...]]\";\n> +\"                     [-q|--quiet] [-e|--exit-code] [<repository> [<refs>...]]\";\n>\n>  /*\n>  * Is there one among the list of patterns that match the tail part\n> @@ -35,6 +35,7 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)\n>        unsigned flags = 0;\n>        int get_url = 0;\n>        int quiet = 0;\n> +       int exit_code = 0;\n>        const char *uploadpack = NULL;\n>        const char **pattern = NULL;\n>\n> @@ -74,6 +75,10 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)\n>                                get_url = 1;\n>                                continue;\n>                        }\n> +                       if (!strcmp(\"--exit-code\", arg) || !strcmp(\"-e\", arg)) {\n> +                               exit_code = 1;\n> +                               continue;\n> +                       }\n>                        usage(ls_remote_usage);\n>                }\n>                dest = arg;\n> @@ -115,12 +120,15 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)\n>\n>        if (!dest && !quiet)\n>                fprintf(stderr, \"From %s\\n\", *remote->url);\n> +\n>        for ( ; ref; ref = ref->next) {\n>                if (!check_ref_type(ref, flags))\n>                        continue;\n>                if (!tail_match(pattern, ref->name))\n>                        continue;\n>                printf(\"%s      %s\\n\", sha1_to_hex(ref->old_sha1), ref->name);\n> +               exit_code = 0;\n>        }\n> -       return 0;\n> +\n> +       return (exit_code) ? 2 : 0;\n\nWe know, just a small nitpick. The parentheses around exit_code are unnecessary.\n\n>  }\n> --\n> 1.7.5.1\n>\n> --\n> To unsubscribe from this list: send the line \"unsubscribe git\" in\n> the body of a message to majordomo@vger.kernel.org\n> More majordomo info at  http://vger.kernel.org/majordomo-info.html\n>\n"},{"id":"168150","messageId":"7v7h9ndfs9.fsf@alter.siamese.dyndns.org","threadId":"27336","inReplyTo":"4DD3D490.1090503@elegosoft.com","subject":"Re: [RFC/PATCH] ls-remote: optionally return non-zero on non-existing refs","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2011-05-18T18:28:38Z","receivedAt":"2011-05-18T18:28:38Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Michael Schubert <mschub@elegosoft.com> writes:\n\n> git ls-remote returns zero no matter if the given references were found\n> or not. Teach ls-remote an option --exit-status to make it optionally\n> returning a non-zero status.\n>\n> Signed-off-by: Michael Schubert <mschub@elegosoft.com>\n> ---\n>\n> If there is just one existing ref in a list of non-exising refs, this will\n> return zero though - as \"git show-ref\" does.\n\nThat's Ok, but...\n\n> @@ -35,6 +35,7 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)\n> ...\n> +\tint exit_code = 0;\n> ...\n> @@ -74,6 +75,10 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)\n> ...\n> +\t\t\tif (!strcmp(\"--exit-code\", arg) || !strcmp(\"-e\", arg)) {\n> +\t\t\t\texit_code = 1;\n> +\t\t\t\tcontinue;\n> ...\n> @@ -115,12 +120,15 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)\n> ...\n> +\treturn (exit_code) ? 2 : 0;\n>  }\n\nWhat is this insanity?\n\nWouldn't it be a lot more straightforward to write it like this?\n\n\tint status = 0;\n\n\t...\n\n\t\tif (!strcmp(\"--exit-code\", arg)) {\n\t\t\t/* when we do not find any, return this value */\n \t\t\tstatus = 2;\n                        continue;\n\t\t}\n\n\t...\n\n\treturn status;\n"},{"id":"168158","messageId":"4DD426A8.3010402@elegosoft.com","threadId":"27336","inReplyTo":"7v7h9ndfs9.fsf@alter.siamese.dyndns.org","subject":"[PATCH v2] ls-remote: optionally return non-zero on non-existing refs","fromName":"Michael Schubert","fromEmail":"mschub@elegosoft.com","sentAt":"2011-05-18T20:06:00Z","receivedAt":"2011-05-18T20:06:00Z","isPatch":true,"sender":{"key":"mschub@elegosoft.com","avatar":null},"body":"On 05/18/2011 08:28 PM, Junio C Hamano wrote:\n> Michael Schubert <mschub@elegosoft.com> writes:\n> \n>> @@ -35,6 +35,7 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)\n>> ...\n>> +\tint exit_code = 0;\n>> ...\n>> @@ -74,6 +75,10 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)\n>> ...\n>> +\t\t\tif (!strcmp(\"--exit-code\", arg) || !strcmp(\"-e\", arg)) {\n>> +\t\t\t\texit_code = 1;\n>> +\t\t\t\tcontinue;\n>> ...\n>> @@ -115,12 +120,15 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)\n>> ...\n>> +\treturn (exit_code) ? 2 : 0;\n>>  }\n> \n> What is this insanity?\n> \n> Wouldn't it be a lot more straightforward to write it like this?\n> \n> \tint status = 0;\n> \n> \t...\n> \n> \t\tif (!strcmp(\"--exit-code\", arg)) {\n> \t\t\t/* when we do not find any, return this value */\n>  \t\t\tstatus = 2;\n>                         continue;\n> \t\t}\n> \n> \t...\n> \n> \treturn status;\n\nSure, got confused. Fixed.\n\n-- >8 --\n\nSubject: [PATCH] ls-remote: optionally return non-zero on non-existing refs\n\ngit ls-remote returns zero no matter if the given references were found\nor not. Teach ls-remote an option --exit-status to make it optionally\nreturning a non-zero status.\n\nSigned-off-by: Michael Schubert <mschub@elegosoft.com>\n---\n Documentation/git-ls-remote.txt |    7 ++++++-\n builtin/ls-remote.c             |   12 ++++++++++--\n 2 files changed, 16 insertions(+), 3 deletions(-)\n\ndiff --git a/Documentation/git-ls-remote.txt b/Documentation/git-ls-remote.txt\nindex c3df8c0..26cf705 100644\n--- a/Documentation/git-ls-remote.txt\n+++ b/Documentation/git-ls-remote.txt\n@@ -10,7 +10,7 @@ SYNOPSIS\n --------\n [verse]\n 'git ls-remote' [--heads] [--tags]  [-u <exec> | --upload-pack <exec>]\n-\t      <repository> [<refs>...]\n+\t      [-e|--exit-code] <repository> [<refs>...]\n \n DESCRIPTION\n -----------\n@@ -36,6 +36,11 @@ OPTIONS\n \tSSH and where the SSH daemon does not use the PATH configured by the\n \tuser.\n \n+-e::\n+--exit-code::\n+\tExit with a non-zero status code when the specified references don't\n+\texist.\n+\n <repository>::\n \tLocation of the repository.  The shorthand defined in\n \t$GIT_DIR/branches/ can be used. Use \".\" (dot) to list references in\ndiff --git a/builtin/ls-remote.c b/builtin/ls-remote.c\nindex 1a1ff87..01e362f 100644\n--- a/builtin/ls-remote.c\n+++ b/builtin/ls-remote.c\n@@ -5,7 +5,7 @@\n \n static const char ls_remote_usage[] =\n \"git ls-remote [--heads] [--tags]  [-u <exec> | --upload-pack <exec>]\\n\"\n-\"                     [-q|--quiet] [<repository> [<refs>...]]\";\n+\"                     [-q|--quiet] [-e|--exit-code] [<repository> [<refs>...]]\";\n \n /*\n  * Is there one among the list of patterns that match the tail part\n@@ -35,6 +35,7 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)\n \tunsigned flags = 0;\n \tint get_url = 0;\n \tint quiet = 0;\n+\tint status = 0;\n \tconst char *uploadpack = NULL;\n \tconst char **pattern = NULL;\n \n@@ -74,6 +75,10 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)\n \t\t\t\tget_url = 1;\n \t\t\t\tcontinue;\n \t\t\t}\n+\t\t\tif (!strcmp(\"--exit-code\", arg) || !strcmp(\"-e\", arg)) {\n+\t\t\t\tstatus = 2;\n+\t\t\t\tcontinue;\n+\t\t\t}\n \t\t\tusage(ls_remote_usage);\n \t\t}\n \t\tdest = arg;\n@@ -115,12 +120,15 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)\n \n \tif (!dest && !quiet)\n \t\tfprintf(stderr, \"From %s\\n\", *remote->url);\n+\n \tfor ( ; ref; ref = ref->next) {\n \t\tif (!check_ref_type(ref, flags))\n \t\t\tcontinue;\n \t\tif (!tail_match(pattern, ref->name))\n \t\t\tcontinue;\n \t\tprintf(\"%s\t%s\\n\", sha1_to_hex(ref->old_sha1), ref->name);\n+\t\tstatus = 0;\n \t}\n-\treturn 0;\n+\n+\treturn status;\n }\n-- \n1.7.5.1\n"},{"id":"168160","messageId":"m2y623so4l.fsf@igel.home","threadId":"27336","inReplyTo":"4DD426A8.3010402@elegosoft.com","subject":"Re: [PATCH v2] ls-remote: optionally return non-zero on non-existing refs","fromName":"Andreas Schwab","fromEmail":"schwab@linux-m68k.org","sentAt":"2011-05-18T21:19:22Z","receivedAt":"2011-05-18T21:19:22Z","isPatch":true,"sender":{"key":"schwab@linux-m68k.org","avatar":"https://avatars.githubusercontent.com/u/2175493?v=4"},"body":"Michael Schubert <mschub@elegosoft.com> writes:\n\n> +-e::\n> +--exit-code::\n> +\tExit with a non-zero status code when the specified references don't\n> +\texist.\n\nAll or any of them?  Also, the <refs> are patterns, so this should\nprobably be talking about \"not matching\".\n\nAndreas.\n\n-- \nAndreas Schwab, schwab@linux-m68k.org\nGPG Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5\n\"And now for something completely different.\"\n"},{"id":"168161","messageId":"7v7h9nbsf6.fsf@alter.siamese.dyndns.org","threadId":"27336","inReplyTo":"m2y623so4l.fsf@igel.home","subject":"Re: [PATCH v2] ls-remote: optionally return non-zero on non-existing refs","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2011-05-18T21:38:37Z","receivedAt":"2011-05-18T21:38:37Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Andreas Schwab <schwab@linux-m68k.org> writes:\n\n> Michael Schubert <mschub@elegosoft.com> writes:\n>\n>> +-e::\n>> +--exit-code::\n>> +\tExit with a non-zero status code when the specified references don't\n>> +\texist.\n>\n> All or any of them?  Also, the <refs> are patterns, so this should\n> probably be talking about \"not matching\".\n\nFWIW, here is what I am going to queue:\n\nPoints are:\n\n (1) explain that the command indicates success in the normal case when we\n     successfully talked with the remote end;\n\n (2) help people who want to use this option by explicitly mentioning \"2\",\n     not \"non-zero\", so that they can write:\n\n\tgit ls-remote $there $pattern\n        case $? in\n        0)\n        \t: success! ;;\n\t2)\n        \t: successfully talked, but nothing found ;;\n\t*)\n        \t: we couldn't even talk to them ;;\n\tesac\n\n (3) test!\n\n-- >8 --\nFrom: Michael Schubert <mschub@elegosoft.com>\nDate: Wed, 18 May 2011 22:06:00 +0200\nSubject: [PATCH] ls-remote: the --exit-code option reports \"no matching refs\"\n\nThe \"git ls-remote\" uses its exit status to indicate if it successfully\ntalked with the remote repository. A new option \"--exit-code\" makes the\ncommand exit with status \"2\" when there is no refs to be listed, even when\nthe command successfully talked with the remote repository.\n\nThis way, the caller can tell if we failed to contact the remote, or the\nremote did not have what we wanted to see. Of course, you can inspect the\noutput from the command, which has been and will continue to be a valid\nway to check the same thing.\n\nSigned-off-by: Michael Schubert <mschub@elegosoft.com>\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\n---\n t/t5512-ls-remote.sh            |   24 ++++++++++++++++++++++++\n Documentation/git-ls-remote.txt |    8 +++++++-\n builtin/ls-remote.c             |   11 +++++++++--\n 3 files changed, 40 insertions(+), 3 deletions(-)\n\ndiff --git a/t/t5512-ls-remote.sh b/t/t5512-ls-remote.sh\nindex d191235..5c546c9 100755\n--- a/t/t5512-ls-remote.sh\n+++ b/t/t5512-ls-remote.sh\n@@ -123,4 +123,28 @@ test_expect_success 'confuses pattern as remote when no remote specified' '\n \n '\n \n+test_expect_success 'die with non-2 for wrong repository even with --exit-code' '\n+\tgit ls-remote --exit-code ./no-such-repository ;# not &&\n+\tstatus=$? &&\n+\ttest $status != 2 && test $status != 0\n+'\n+\n+test_expect_success 'Report success even when nothing matches' '\n+\tgit ls-remote other.git \"refs/nsn/*\" >actual &&\n+\t>expect &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'Report no-match with --exit-code' '\n+\ttest_expect_code 2 git ls-remote --exit-code other.git \"refs/nsn/*\" >actual &&\n+\t>expect &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'Report match with --exit-code' '\n+\tgit ls-remote --exit-code other.git \"refs/tags/*\" >actual &&\n+\tgit ls-remote . tags/mark >expect &&\n+\ttest_cmp expect actual\n+'\n+\n test_done\ndiff --git a/Documentation/git-ls-remote.txt b/Documentation/git-ls-remote.txt\nindex c3df8c0..7a9b86a 100644\n--- a/Documentation/git-ls-remote.txt\n+++ b/Documentation/git-ls-remote.txt\n@@ -10,7 +10,7 @@ SYNOPSIS\n --------\n [verse]\n 'git ls-remote' [--heads] [--tags]  [-u <exec> | --upload-pack <exec>]\n-\t      <repository> [<refs>...]\n+\t      [--exit-code] <repository> [<refs>...]\n \n DESCRIPTION\n -----------\n@@ -36,6 +36,12 @@ OPTIONS\n \tSSH and where the SSH daemon does not use the PATH configured by the\n \tuser.\n \n+--exit-code::\n+\tExit with status \"2\" when no matching refs are found in the remote\n+\trepository. Usually the command exits with status \"0\" to indicate\n+\tit successfully talked with the remote repository, whether it\n+\tfound any matching refs.\n+\n <repository>::\n \tLocation of the repository.  The shorthand defined in\n \t$GIT_DIR/branches/ can be used. Use \".\" (dot) to list references in\ndiff --git a/builtin/ls-remote.c b/builtin/ls-remote.c\nindex 1a1ff87..1022309 100644\n--- a/builtin/ls-remote.c\n+++ b/builtin/ls-remote.c\n@@ -5,7 +5,7 @@\n \n static const char ls_remote_usage[] =\n \"git ls-remote [--heads] [--tags]  [-u <exec> | --upload-pack <exec>]\\n\"\n-\"                     [-q|--quiet] [<repository> [<refs>...]]\";\n+\"                     [-q|--quiet] [--exit-code] [<repository> [<refs>...]]\";\n \n /*\n  * Is there one among the list of patterns that match the tail part\n@@ -35,6 +35,7 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)\n \tunsigned flags = 0;\n \tint get_url = 0;\n \tint quiet = 0;\n+\tint status = 0;\n \tconst char *uploadpack = NULL;\n \tconst char **pattern = NULL;\n \n@@ -74,6 +75,11 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)\n \t\t\t\tget_url = 1;\n \t\t\t\tcontinue;\n \t\t\t}\n+\t\t\tif (!strcmp(\"--exit-code\", arg)) {\n+\t\t\t\t/* return this code if no refs are reported */\n+\t\t\t\tstatus = 2;\n+\t\t\t\tcontinue;\n+\t\t\t}\n \t\t\tusage(ls_remote_usage);\n \t\t}\n \t\tdest = arg;\n@@ -121,6 +127,7 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)\n \t\tif (!tail_match(pattern, ref->name))\n \t\t\tcontinue;\n \t\tprintf(\"%s\t%s\\n\", sha1_to_hex(ref->old_sha1), ref->name);\n+\t\tstatus = 0; /* we found something */\n \t}\n-\treturn 0;\n+\treturn status;\n }\n-- \n1.7.5.1.414.gb4910\n"},{"id":"168162","messageId":"4DD43FF2.7080509@elegosoft.com","threadId":"27336","inReplyTo":"7v7h9nbsf6.fsf@alter.siamese.dyndns.org","subject":"Re: [PATCH v2] ls-remote: optionally return non-zero on non-existing refs","fromName":"Michael Schubert","fromEmail":"mschub@elegosoft.com","sentAt":"2011-05-18T21:53:54Z","receivedAt":"2011-05-18T21:53:54Z","isPatch":true,"sender":{"key":"mschub@elegosoft.com","avatar":null},"body":"Thanks!\n"}]}