Teach git-describe --exact-match to avoid expensive tag searches
authorShawn O. Pearce <spearce@spearce.org>
Sun, 24 Feb 2008 08:07:31 +0000 (03:07 -0500)
committerJunio C Hamano <gitster@pobox.com>
Sun, 24 Feb 2008 18:01:24 +0000 (10:01 -0800)
Sometimes scripts want (or need) the annotated tag name that exactly
matches a specific commit, or no tag at all. In such cases it can be
difficult to determine if the output of `git describe $commit` is a
real tag name or a tag+abbreviated commit. A common idiom is to run
git-describe twice:

if test $(git describe $commit) = $(git describe --abbrev=0 $commit)
...

but this is a huge waste of time if the caller is just going to pick a
different method to describe $commit or abort because it is not exactly
an annotated tag.

Setting the maximum number of candidates to 0 allows the caller to ask
for only a tag that directly points at the supplied commit, or to have
git-describe abort if no such item exists.

Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Documentation/git-describe.txt
builtin-describe.c
index 1c3dfb40c63350651efb570ed540b69c34ccbc81..fbb40a29165a47e627358b5b9f8afb50e84958cd 100644 (file)
@@ -45,6 +45,11 @@ OPTIONS
        candidates to describe the input committish consider
        up to <n> candidates.  Increasing <n> above 10 will take
        slightly longer but may produce a more accurate result.
+       An <n> of 0 will cause only exact matches to be output.
+
+--exact-match::
+       Only output exact matches (a tag directly references the
+       supplied commit).  This is a synonym for --candidates=0.
 
 --debug::
        Verbosely display information about the searching strategy
index 9c958bd84b839cb501c977ecb7a23ce5400c3f2c..05e309f5ad15f9a6f85df34322bb59152b4e37be 100644 (file)
@@ -174,6 +174,8 @@ static void describe(const char *arg, int last_one)
                return;
        }
 
+       if (!max_candidates)
+               die("no tag exactly matches '%s'", sha1_to_hex(cmit->object.sha1));
        if (debug)
                fprintf(stderr, "searching to describe %s\n", arg);
 
@@ -270,6 +272,8 @@ int cmd_describe(int argc, const char **argv, const char *prefix)
                OPT_BOOLEAN(0, "all",        &all, "use any ref in .git/refs"),
                OPT_BOOLEAN(0, "tags",       &tags, "use any tag in .git/refs/tags"),
                OPT__ABBREV(&abbrev),
+               OPT_SET_INT(0, "exact-match", &max_candidates,
+                           "only output exact matches", 0),
                OPT_INTEGER(0, "candidates", &max_candidates,
                            "consider <n> most recent tags (default: 10)"),
                OPT_STRING(0, "match",       &pattern, "pattern",
@@ -278,8 +282,8 @@ int cmd_describe(int argc, const char **argv, const char *prefix)
        };
 
        argc = parse_options(argc, argv, options, describe_usage, 0);
-       if (max_candidates < 1)
-               max_candidates = 1;
+       if (max_candidates < 0)
+               max_candidates = 0;
        else if (max_candidates > MAX_TAGS)
                max_candidates = MAX_TAGS;