{"thread":{"id":"29765","subject":"[PATCH 2/2] builtin/symbolic-ref.c: add option to output shortened ref","startedAt":"2012-02-27T22:10:38Z","lastAt":"2012-02-27T23:54:49Z","messageCount":5,"participants":["Jan Krüger","Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":2},"messages":[{"id":"185574","messageId":"1330380638-9738-1-git-send-email-jk@jk.gs","threadId":"29765","inReplyTo":null,"subject":"[PATCH 2/2] builtin/symbolic-ref.c: add option to output shortened ref","fromName":"Jan Krüger","fromEmail":"jk@jk.gs","sentAt":"2012-02-27T22:10:38Z","receivedAt":"2012-02-27T22:10:38Z","isPatch":true,"sender":{"key":"jk@jk.gs","avatar":"https://avatars.githubusercontent.com/u/1774?v=4"},"body":"In scripts meant to generate user-consumable output, it can be helpful\nto resolve a symbolic ref and output the result in a shortened form,\nsuch as for use in shell prompts. Add a new -s option to allow this.\n\nSigned-off-by: Jan Krüger <jk@jk.gs>\n---\n Documentation/git-symbolic-ref.txt |    6 +++++-\n builtin/symbolic-ref.c             |    5 +++++\n 2 files changed, 10 insertions(+), 1 deletion(-)\n\ndiff --git a/Documentation/git-symbolic-ref.txt b/Documentation/git-symbolic-ref.txt\nindex a05819b..7f108ce 100644\n--- a/Documentation/git-symbolic-ref.txt\n+++ b/Documentation/git-symbolic-ref.txt\n@@ -8,7 +8,7 @@ git-symbolic-ref - Read and modify symbolic refs\n SYNOPSIS\n --------\n [verse]\n-'git symbolic-ref' [-q] [-f] [-m <reason>] <name> [<ref>]\n+'git symbolic-ref' [-q] [-f] [-s] [-m <reason>] <name> [<ref>]\n \n DESCRIPTION\n -----------\n@@ -38,6 +38,10 @@ OPTIONS\n \tnot a symbolic ref; instead output the SHA1 value referenced by\n \t<name>.\n \n+-s::\n+\tWhen showing the value of <name> as a symbolic ref, try to shorten the\n+\tvalue, e.g. from `refs/heads/master` to `master`.\n+\n -m::\n \tUpdate the reflog for <name> with <reason>.  This is valid only\n \twhen creating or updating a symbolic ref.\ndiff --git a/builtin/symbolic-ref.c b/builtin/symbolic-ref.c\nindex 2e0a86f..df8da11 100644\n--- a/builtin/symbolic-ref.c\n+++ b/builtin/symbolic-ref.c\n@@ -9,6 +9,7 @@ static const char * const git_symbolic_ref_usage[] = {\n };\n \n static int fallback_regular_ref;\n+static int shorten;\n \n static void check_symref(const char *HEAD, int quiet)\n {\n@@ -32,6 +33,9 @@ static void check_symref(const char *HEAD, int quiet)\n \t\t\texit(1);\n \t\t}\n \t}\n+\tif (shorten)\n+\t\trefs_heads_master = shorten_unambiguous_ref(\n+\t\t\trefs_heads_master, 0);\n \tputs(refs_heads_master);\n }\n \n@@ -44,6 +48,7 @@ int cmd_symbolic_ref(int argc, const char **argv, const char *prefix)\n \t\t\t\"suppress error message for non-symbolic (detached) refs\"),\n \t\tOPT_BOOLEAN('f', NULL, &fallback_regular_ref,\n \t\t\t\t\t\"fall back to showing as a regular ref\"),\n+\t\tOPT_BOOLEAN('s', NULL, &shorten, \"shorten ref output\"),\n \t\tOPT_STRING('m', NULL, &msg, \"reason\", \"reason of the update\"),\n \t\tOPT_END(),\n \t};\n-- \n1.7.9.2.302.g3724c.dirty\n"},{"id":"185578","messageId":"7vwr7752s8.fsf@alter.siamese.dyndns.org","threadId":"29765","inReplyTo":"1330380638-9738-1-git-send-email-jk@jk.gs","subject":"Re: [PATCH 2/2] builtin/symbolic-ref.c: add option to output shortened ref","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2012-02-27T22:28:55Z","receivedAt":"2012-02-27T22:28:55Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Jan Krüger <jk@jk.gs> writes:\n\n> In scripts meant to generate user-consumable output, it can be helpful\n> to resolve a symbolic ref and output the result in a shortened form,\n> such as for use in shell prompts. Add a new -s option to allow this.\n\nI think this one (unlike 1/2) makes sense, but a single letter -s feels a\nbit too vague.  Always spelling in long option \"--short\" so that it\nmatches \"%(refname:short)\" in for-each-ref might be better, I would think.\n\nEspecially given that the expected use case is primarily in scripts not\nfrom the command line, being more explicit and easier to read has value\nover being short and easier to (mis)type.\n"},{"id":"185580","messageId":"4F4C07AD.3050404@jk.gs","threadId":"29765","inReplyTo":"7vwr7752s8.fsf@alter.siamese.dyndns.org","subject":"Re: [PATCH 2/2] builtin/symbolic-ref.c: add option to output shortened ref","fromName":"Jan Krüger","fromEmail":"jk@jk.gs","sentAt":"2012-02-27T22:46:05Z","receivedAt":"2012-02-27T22:46:05Z","isPatch":true,"sender":{"key":"jk@jk.gs","avatar":"https://avatars.githubusercontent.com/u/1774?v=4"},"body":"On 02/27/2012 11:28 PM, Junio C Hamano wrote:\n> I think this one (unlike 1/2) makes sense, but a single letter -s feels a\n> bit too vague.  Always spelling in long option \"--short\" so that it\n> matches \"%(refname:short)\" in for-each-ref might be better, I would think.\n> \n> Especially given that the expected use case is primarily in scripts not\n> from the command line, being more explicit and easier to read has value\n> over being short and easier to (mis)type.\n\nGood point. If we can agree on what to do with the first patch, I'll\nchange that in the next iteration.\n\n-Jan\n"},{"id":"185581","messageId":"7vpqcz514d.fsf@alter.siamese.dyndns.org","threadId":"29765","inReplyTo":"4F4C07AD.3050404@jk.gs","subject":"Re: [PATCH 2/2] builtin/symbolic-ref.c: add option to output shortened ref","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2012-02-27T23:04:50Z","receivedAt":"2012-02-27T23:04:50Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Jan Krüger <jk@jk.gs> writes:\n\n> On 02/27/2012 11:28 PM, Junio C Hamano wrote:\n>> I think this one (unlike 1/2) makes sense, but a single letter -s feels a\n>> bit too vague.  Always spelling in long option \"--short\" so that it\n>> matches \"%(refname:short)\" in for-each-ref might be better, I would think.\n>> \n>> Especially given that the expected use case is primarily in scripts not\n>> from the command line, being more explicit and easier to read has value\n>> over being short and easier to (mis)type.\n>\n> Good point. If we can agree on what to do with the first patch, I'll\n> change that in the next iteration.\n\nDropping the first patch would be my preference.  I do not think this\nchange deserves to be taken hostage to it.\n"},{"id":"185582","messageId":"7vlinn4yt2.fsf@alter.siamese.dyndns.org","threadId":"29765","inReplyTo":"7vpqcz514d.fsf@alter.siamese.dyndns.org","subject":"Re: [PATCH 2/2] builtin/symbolic-ref.c: add option to output shortened ref","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2012-02-27T23:54:49Z","receivedAt":"2012-02-27T23:54:49Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Junio C Hamano <gitster@pobox.com> writes:\n\n> ... I do not think this change deserves to be taken hostage to it.\n\nSo here is a trivial rebase of the patch.\n\nThe --short option should apply only when querying (-q also shares this, I\nthink), so I split the synopsis into two.\n\nI was the guity one who named the variable refs_heads_master, hoping that\nit would be easier to name it after representative value it would contain\n(so that it would be clear for other code that may want to strip the\nleading component what to expect), but now it has happened with your\npatch, the variable name looks silly.\n\nOPT_BOOLEAN() is deprecated; OPT_BOOL() has a much saner semantics for\nmost callers' needs and we should consider using it when adding new\noptions.\n\nPerhaps we would also want some tests?\n\n Documentation/git-symbolic-ref.txt |    7 ++++++-\n builtin/symbolic-ref.c             |   11 ++++++++---\n 2 files changed, 14 insertions(+), 4 deletions(-)\n\ndiff --git a/Documentation/git-symbolic-ref.txt b/Documentation/git-symbolic-ref.txt\nindex a45d4c4..89e7707 100644\n--- a/Documentation/git-symbolic-ref.txt\n+++ b/Documentation/git-symbolic-ref.txt\n@@ -8,7 +8,8 @@ git-symbolic-ref - Read and modify symbolic refs\n SYNOPSIS\n --------\n [verse]\n-'git symbolic-ref' [-q] [-m <reason>] <name> [<ref>]\n+'git symbolic-ref' [-m <reason>] <name> <ref>\n+'git symbolic-ref' [-q] [--short] <name>\n \n DESCRIPTION\n -----------\n@@ -33,6 +34,10 @@ OPTIONS\n \tsymbolic ref but a detached HEAD; instead exit with\n \tnon-zero status silently.\n \n+--short::\n+\tWhen showing the value of <name> as a symbolic ref, try to shorten the\n+\tvalue, e.g. from `refs/heads/master` to `master`.\n+\n -m::\n \tUpdate the reflog for <name> with <reason>.  This is valid only\n \twhen creating or updating a symbolic ref.\ndiff --git a/builtin/symbolic-ref.c b/builtin/symbolic-ref.c\nindex 2ef5962..801d62e 100644\n--- a/builtin/symbolic-ref.c\n+++ b/builtin/symbolic-ref.c\n@@ -8,13 +8,15 @@ static const char * const git_symbolic_ref_usage[] = {\n \tNULL\n };\n \n+static int shorten;\n+\n static void check_symref(const char *HEAD, int quiet)\n {\n \tunsigned char sha1[20];\n \tint flag;\n-\tconst char *refs_heads_master = resolve_ref_unsafe(HEAD, sha1, 0, &flag);\n+\tconst char *refname = resolve_ref_unsafe(HEAD, sha1, 0, &flag);\n \n-\tif (!refs_heads_master)\n+\tif (!refname)\n \t\tdie(\"No such ref: %s\", HEAD);\n \telse if (!(flag & REF_ISSYMREF)) {\n \t\tif (!quiet)\n@@ -22,7 +24,9 @@ static void check_symref(const char *HEAD, int quiet)\n \t\telse\n \t\t\texit(1);\n \t}\n-\tputs(refs_heads_master);\n+\tif (shorten)\n+\t\trefname = shorten_unambiguous_ref(refname, 0);\n+\tputs(refname);\n }\n \n int cmd_symbolic_ref(int argc, const char **argv, const char *prefix)\n@@ -32,6 +36,7 @@ int cmd_symbolic_ref(int argc, const char **argv, const char *prefix)\n \tstruct option options[] = {\n \t\tOPT__QUIET(&quiet,\n \t\t\t\"suppress error message for non-symbolic (detached) refs\"),\n+\t\tOPT_BOOL(0, \"short\", &shorten, \"shorten ref output\"),\n \t\tOPT_STRING('m', NULL, &msg, \"reason\", \"reason of the update\"),\n \t\tOPT_END(),\n \t};\n"}]}