* [PATCH 5/6] Teach git-describe --exact-match to avoid expensive tag searches
@ 2008-02-24 8:07 Shawn O. Pearce
2008-02-24 10:25 ` Jakub Narebski
0 siblings, 1 reply; 3+ messages in thread
From: Shawn O. Pearce @ 2008-02-24 8:07 UTC (permalink / raw)
To: Junio C Hamano; +Cc: git
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>
---
Documentation/git-describe.txt | 5 +++++
builtin-describe.c | 8 ++++++--
2 files changed, 11 insertions(+), 2 deletions(-)
diff --git a/Documentation/git-describe.txt b/Documentation/git-describe.txt
index 1c3dfb4..fbb40a2 100644
--- a/Documentation/git-describe.txt
+++ b/Documentation/git-describe.txt
@@ -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
diff --git a/builtin-describe.c b/builtin-describe.c
index 9c958bd..05e309f 100644
--- a/builtin-describe.c
+++ b/builtin-describe.c
@@ -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;
--
1.5.4.3.295.g6b554
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH 5/6] Teach git-describe --exact-match to avoid expensive tag searches
2008-02-24 8:07 [PATCH 5/6] Teach git-describe --exact-match to avoid expensive tag searches Shawn O. Pearce
@ 2008-02-24 10:25 ` Jakub Narebski
2008-02-24 10:32 ` Shawn O. Pearce
0 siblings, 1 reply; 3+ messages in thread
From: Jakub Narebski @ 2008-02-24 10:25 UTC (permalink / raw)
To: Shawn O. Pearce; +Cc: Junio C Hamano, git
"Shawn O. Pearce" <spearce@spearce.org> writes:
> A common idiom is to run git-describe twice:
>
> if test $(git describe $commit) = $(git describe --abbrev=0 $commit)
> ...
Wouldn't it be easier to use?
if test $(git rev-parse $(git describe --abbrev=0 $commit)^0) = $commit
But this is also one unnecessary, although not as costly, call to git
command.
--
Jakub Narebski
Poland
ShadeHawk on #git
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH 5/6] Teach git-describe --exact-match to avoid expensive tag searches
2008-02-24 10:25 ` Jakub Narebski
@ 2008-02-24 10:32 ` Shawn O. Pearce
0 siblings, 0 replies; 3+ messages in thread
From: Shawn O. Pearce @ 2008-02-24 10:32 UTC (permalink / raw)
To: Jakub Narebski; +Cc: Junio C Hamano, git
Jakub Narebski <jnareb@gmail.com> wrote:
> "Shawn O. Pearce" <spearce@spearce.org> writes:
>
> > A common idiom is to run git-describe twice:
> >
> > if test $(git describe $commit) = $(git describe --abbrev=0 $commit)
> > ...
>
> Wouldn't it be easier to use?
>
> if test $(git rev-parse $(git describe --abbrev=0 $commit)^0) = $commit
>
> But this is also one unnecessary, although not as costly, call to git
> command.
Yea, that's the other way I've done this in a script before. But
its obtuse and non-obvious. --exact-match is a little easier to
figure out. :-)
--
Shawn.
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2008-02-24 10:33 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-02-24 8:07 [PATCH 5/6] Teach git-describe --exact-match to avoid expensive tag searches Shawn O. Pearce
2008-02-24 10:25 ` Jakub Narebski
2008-02-24 10:32 ` Shawn O. Pearce
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).