@matheusbsbs / matheus-bagliano / commits / 2c33f75754
Fork 0

Teach git-describe --exact-match to avoid expensive tag searches

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>

Shawn O. Pearce committed Feb 24, 2008 at 03:07 UTC 2c33f7575452f53382dcf77fdc88a2ea5d46f09d
2 files changed +11 -2
Documentation/git-describe.txt
+5
@@ -45,6 +45,11 @@ OPTIONS
45 candidates to describe the input committish consider
46 up to <n> candidates. Increasing <n> above 10 will take
47 slightly longer but may produce a more accurate result.
48 + An <n> of 0 will cause only exact matches to be output.
49 +
50 +--exact-match::
51 + Only output exact matches (a tag directly references the
52 + supplied commit). This is a synonym for --candidates=0.
53
54 --debug::
55 Verbosely display information about the searching strategy
builtin-describe.c
+6 -2
@@ -174,6 +174,8 @@ static void describe(const char *arg, int last_one)
174 return;
175 }
176
177 + if (!max_candidates)
178 + die("no tag exactly matches '%s'", sha1_to_hex(cmit->object.sha1));
179 if (debug)
180 fprintf(stderr, "searching to describe %s\n", arg);
181
@@ -270,6 +272,8 @@ int cmd_describe(int argc, const char **argv, const char *prefix)
272 OPT_BOOLEAN(0, "all", &all, "use any ref in .git/refs"),
273 OPT_BOOLEAN(0, "tags", &tags, "use any tag in .git/refs/tags"),
274 OPT__ABBREV(&abbrev),
275 + OPT_SET_INT(0, "exact-match", &max_candidates,
276 + "only output exact matches", 0),
277 OPT_INTEGER(0, "candidates", &max_candidates,
278 "consider <n> most recent tags (default: 10)"),
279 OPT_STRING(0, "match", &pattern, "pattern",
@@ -278,8 +282,8 @@ int cmd_describe(int argc, const char **argv, const char *prefix)
282 };
283
284 argc = parse_options(argc, argv, options, describe_usage, 0);
281 - if (max_candidates < 1)
282 - max_candidates = 1;
285 + if (max_candidates < 0)
286 + max_candidates = 0;
287 else if (max_candidates > MAX_TAGS)
288 max_candidates = MAX_TAGS;
289