From feededd05b551a02a95fe2a736e9d5f688b86119 Mon Sep 17 00:00:00 2001 From: "Shawn O. Pearce" Date: Sun, 24 Feb 2008 03:07:25 -0500 Subject: Teach git-describe to use peeled ref information when scanning tags By using the peeled ref information inside of the packed-refs file we can avoid opening tag objects to obtain the commits they reference. This speeds up git-describe when there are a large number of tags in the repository as we have less objects to parse before we can start commit matching. Signed-off-by: Shawn O. Pearce Signed-off-by: Junio C Hamano --- builtin-describe.c | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) (limited to 'builtin-describe.c') diff --git a/builtin-describe.c b/builtin-describe.c index 3428483134..bfd25e2324 100644 --- a/builtin-describe.c +++ b/builtin-describe.c @@ -46,19 +46,30 @@ static void add_to_known_names(const char *path, static int get_name(const char *path, const unsigned char *sha1, int flag, void *cb_data) { - struct commit *commit = lookup_commit_reference_gently(sha1, 1); + struct commit *commit; struct object *object; - int prio; + unsigned char peeled[20]; + int is_tag, prio; + + if (!peel_ref(path, peeled) && !is_null_sha1(peeled)) { + commit = lookup_commit_reference_gently(peeled, 1); + if (!commit) + return 0; + is_tag = !!hashcmp(sha1, commit->object.sha1); + } else { + commit = lookup_commit_reference_gently(sha1, 1); + object = parse_object(sha1); + if (!commit || !object) + return 0; + is_tag = object->type == OBJ_TAG; + } - if (!commit) - return 0; - object = parse_object(sha1); /* If --all, then any refs are used. * If --tags, then any tags are used. * Otherwise only annotated tags are used. */ if (!prefixcmp(path, "refs/tags/")) { - if (object->type == OBJ_TAG) { + if (is_tag) { prio = 2; if (pattern && fnmatch(pattern, path + 10, 0)) prio = 0; -- cgit v1.2.1 From 8a5a1884e93564cb1f46a73184d083a5181d573b Mon Sep 17 00:00:00 2001 From: "Shawn O. Pearce" Date: Sun, 24 Feb 2008 03:07:28 -0500 Subject: Avoid accessing non-tag refs in git-describe unless --all is requested If we aren't going to use a ref there is no reason for us to open its object from the object database. This avoids opening any of the head commits reachable from refs/heads/ unless they are also reachable through the commit we have been asked to describe and we need to walk through it to find a tag. Signed-off-by: Shawn O. Pearce Signed-off-by: Junio C Hamano --- builtin-describe.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'builtin-describe.c') diff --git a/builtin-describe.c b/builtin-describe.c index bfd25e2324..9c958bd84b 100644 --- a/builtin-describe.c +++ b/builtin-describe.c @@ -46,11 +46,15 @@ static void add_to_known_names(const char *path, static int get_name(const char *path, const unsigned char *sha1, int flag, void *cb_data) { + int might_be_tag = !prefixcmp(path, "refs/tags/"); struct commit *commit; struct object *object; unsigned char peeled[20]; int is_tag, prio; + if (!all && !might_be_tag) + return 0; + if (!peel_ref(path, peeled) && !is_null_sha1(peeled)) { commit = lookup_commit_reference_gently(peeled, 1); if (!commit) @@ -68,7 +72,7 @@ static int get_name(const char *path, const unsigned char *sha1, int flag, void * If --tags, then any tags are used. * Otherwise only annotated tags are used. */ - if (!prefixcmp(path, "refs/tags/")) { + if (might_be_tag) { if (is_tag) { prio = 2; if (pattern && fnmatch(pattern, path + 10, 0)) -- cgit v1.2.1 From 2c33f7575452f53382dcf77fdc88a2ea5d46f09d Mon Sep 17 00:00:00 2001 From: "Shawn O. Pearce" Date: Sun, 24 Feb 2008 03:07:31 -0500 Subject: 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 Signed-off-by: Junio C Hamano --- builtin-describe.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'builtin-describe.c') diff --git a/builtin-describe.c b/builtin-describe.c index 9c958bd84b..05e309f5ad 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 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; -- cgit v1.2.1