{"thread":{"id":"12287","subject":"[PATCH 5/6] Teach git-describe --exact-match to avoid expensive tag searches","startedAt":"2008-02-24T08:07:31Z","lastAt":"2008-02-24T10:32:23Z","messageCount":3,"participants":["Shawn O. Pearce","Jakub Narebski"],"isPatch":true,"patchVersion":1,"patchTotal":6},"messages":[{"id":"69767","messageId":"20080224080731.GE22587@spearce.org","threadId":"12287","inReplyTo":null,"subject":"[PATCH 5/6] Teach git-describe --exact-match to avoid expensive tag searches","fromName":"Shawn O. Pearce","fromEmail":"spearce@spearce.org","sentAt":"2008-02-24T08:07:31Z","receivedAt":"2008-02-24T08:07:31Z","isPatch":true,"sender":{"key":"spearce@spearce.org","avatar":"https://avatars.githubusercontent.com/u/34844?v=4"},"body":"Sometimes scripts want (or need) the annotated tag name that exactly\nmatches a specific commit, or no tag at all.  In such cases it can be\ndifficult to determine if the output of `git describe $commit` is a\nreal tag name or a tag+abbreviated commit.  A common idiom is to run\ngit-describe twice:\n\n  if test $(git describe $commit) = $(git describe --abbrev=0 $commit)\n  ...\n\nbut this is a huge waste of time if the caller is just going to pick a\ndifferent method to describe $commit or abort because it is not exactly\nan annotated tag.\n\nSetting the maximum number of candidates to 0 allows the caller to ask\nfor only a tag that directly points at the supplied commit, or to have\ngit-describe abort if no such item exists.\n\nSigned-off-by: Shawn O. Pearce <spearce@spearce.org>\n---\n Documentation/git-describe.txt |    5 +++++\n builtin-describe.c             |    8 ++++++--\n 2 files changed, 11 insertions(+), 2 deletions(-)\n\ndiff --git a/Documentation/git-describe.txt b/Documentation/git-describe.txt\nindex 1c3dfb4..fbb40a2 100644\n--- a/Documentation/git-describe.txt\n+++ b/Documentation/git-describe.txt\n@@ -45,6 +45,11 @@ OPTIONS\n \tcandidates to describe the input committish consider\n \tup to <n> candidates.  Increasing <n> above 10 will take\n \tslightly longer but may produce a more accurate result.\n+\tAn <n> of 0 will cause only exact matches to be output.\n+\n+--exact-match::\n+\tOnly output exact matches (a tag directly references the\n+\tsupplied commit).  This is a synonym for --candidates=0.\n \n --debug::\n \tVerbosely display information about the searching strategy\ndiff --git a/builtin-describe.c b/builtin-describe.c\nindex 9c958bd..05e309f 100644\n--- a/builtin-describe.c\n+++ b/builtin-describe.c\n@@ -174,6 +174,8 @@ static void describe(const char *arg, int last_one)\n \t\treturn;\n \t}\n \n+\tif (!max_candidates)\n+\t\tdie(\"no tag exactly matches '%s'\", sha1_to_hex(cmit->object.sha1));\n \tif (debug)\n \t\tfprintf(stderr, \"searching to describe %s\\n\", arg);\n \n@@ -270,6 +272,8 @@ int cmd_describe(int argc, const char **argv, const char *prefix)\n \t\tOPT_BOOLEAN(0, \"all\",        &all, \"use any ref in .git/refs\"),\n \t\tOPT_BOOLEAN(0, \"tags\",       &tags, \"use any tag in .git/refs/tags\"),\n \t\tOPT__ABBREV(&abbrev),\n+\t\tOPT_SET_INT(0, \"exact-match\", &max_candidates,\n+\t\t\t    \"only output exact matches\", 0),\n \t\tOPT_INTEGER(0, \"candidates\", &max_candidates,\n \t\t\t    \"consider <n> most recent tags (default: 10)\"),\n \t\tOPT_STRING(0, \"match\",       &pattern, \"pattern\",\n@@ -278,8 +282,8 @@ int cmd_describe(int argc, const char **argv, const char *prefix)\n \t};\n \n \targc = parse_options(argc, argv, options, describe_usage, 0);\n-\tif (max_candidates < 1)\n-\t\tmax_candidates = 1;\n+\tif (max_candidates < 0)\n+\t\tmax_candidates = 0;\n \telse if (max_candidates > MAX_TAGS)\n \t\tmax_candidates = MAX_TAGS;\n \n-- \n1.5.4.3.295.g6b554\n"},{"id":"69777","messageId":"m3skzibqjo.fsf@localhost.localdomain","threadId":"12287","inReplyTo":"20080224080731.GE22587@spearce.org","subject":"Re: [PATCH 5/6] Teach git-describe --exact-match to avoid expensive tag searches","fromName":"Jakub Narebski","fromEmail":"jnareb@gmail.com","sentAt":"2008-02-24T10:25:33Z","receivedAt":"2008-02-24T10:25:33Z","isPatch":true,"sender":{"key":"jnareb@gmail.com","avatar":"https://avatars.githubusercontent.com/u/2706?v=4"},"body":"\"Shawn O. Pearce\" <spearce@spearce.org> writes:\n\n>  A common idiom is to run git-describe twice:\n> \n>   if test $(git describe $commit) = $(git describe --abbrev=0 $commit)\n>   ...\n\nWouldn't it be easier to use?\n\n    if test $(git rev-parse $(git describe --abbrev=0 $commit)^0) = $commit\n\nBut this is also one unnecessary, although not as costly, call to git\ncommand.\n-- \nJakub Narebski\nPoland\nShadeHawk on #git\n"},{"id":"69778","messageId":"20080224103223.GI8410@spearce.org","threadId":"12287","inReplyTo":"m3skzibqjo.fsf@localhost.localdomain","subject":"Re: [PATCH 5/6] Teach git-describe --exact-match to avoid expensive tag searches","fromName":"Shawn O. Pearce","fromEmail":"spearce@spearce.org","sentAt":"2008-02-24T10:32:23Z","receivedAt":"2008-02-24T10:32:23Z","isPatch":true,"sender":{"key":"spearce@spearce.org","avatar":"https://avatars.githubusercontent.com/u/34844?v=4"},"body":"Jakub Narebski <jnareb@gmail.com> wrote:\n> \"Shawn O. Pearce\" <spearce@spearce.org> writes:\n> \n> >  A common idiom is to run git-describe twice:\n> > \n> >   if test $(git describe $commit) = $(git describe --abbrev=0 $commit)\n> >   ...\n> \n> Wouldn't it be easier to use?\n> \n>     if test $(git rev-parse $(git describe --abbrev=0 $commit)^0) = $commit\n> \n> But this is also one unnecessary, although not as costly, call to git\n> command.\n\nYea, that's the other way I've done this in a script before.  But\nits obtuse and non-obvious.  --exact-match is a little easier to\nfigure out.  :-)\n\n-- \nShawn.\n"}]}