Skip to content

Commit

Permalink
Merge branch 'jk/describe-perf' into seen
Browse files Browse the repository at this point in the history
* jk/describe-perf:
  describe: stop traversing when we run out of names
  describe: stop digging for max_candidates+1
  t/perf: add tests for git-describe
  t6120: demonstrate weakness in disjoint-root handling
  • Loading branch information
gitster committed Nov 8, 2024
2 parents cf16a00 + b8150bf commit 6a0c12f
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 10 deletions.
17 changes: 10 additions & 7 deletions builtin/describe.c
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,12 @@ static void describe_commit(struct object_id *oid, struct strbuf *dst)
struct commit_name **slot;

seen_commits++;

if (match_cnt == max_candidates) {
gave_up_on = c;
break;
}

slot = commit_names_peek(&commit_names, c);
n = slot ? *slot : NULL;
if (n) {
Expand All @@ -381,10 +387,6 @@ static void describe_commit(struct object_id *oid, struct strbuf *dst)
if (n->prio == 2)
annotated_cnt++;
}
else {
gave_up_on = c;
break;
}
}
for (cur_match = 0; cur_match < match_cnt; cur_match++) {
struct possible_tag *t = &all_matches[cur_match];
Expand Down Expand Up @@ -470,9 +472,8 @@ static void describe_commit(struct object_id *oid, struct strbuf *dst)
fprintf(stderr, _("traversed %lu commits\n"), seen_commits);
if (gave_up_on) {
fprintf(stderr,
_("more than %i tags found; listed %i most recent\n"
"gave up search at %s\n"),
max_candidates, max_candidates,
_("found %i tags; gave up search at %s\n"),
max_candidates,
oid_to_hex(&gave_up_on->object.oid));
}
}
Expand Down Expand Up @@ -666,6 +667,8 @@ int cmd_describe(int argc,
NULL);
if (!hashmap_get_size(&names) && !always)
die(_("No names found, cannot describe anything."));
if (hashmap_get_size(&names) < max_candidates)
max_candidates = hashmap_get_size(&names);

if (argc == 0) {
if (broken) {
Expand Down
30 changes: 30 additions & 0 deletions t/perf/p6100-describe.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/bin/sh

test_description='performance of git-describe'
. ./perf-lib.sh

test_perf_default_repo

# clear out old tags and give us a known state
test_expect_success 'set up tags' '
git for-each-ref --format="delete %(refname)" refs/tags >to-delete &&
git update-ref --stdin <to-delete &&
new=$(git rev-list -1000 HEAD | tail -n 1) &&
git tag -m new new $new &&
old=$(git rev-list HEAD | tail -n 1) &&
git tag -m old old $old
'

test_perf 'describe HEAD' '
git describe HEAD
'

test_perf 'describe HEAD with one max candidate' '
git describe --candidates=1 HEAD
'

test_perf 'describe HEAD with one tag' '
git describe --match=new HEAD
'

test_done
14 changes: 11 additions & 3 deletions t/t6120-describe.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,17 @@ export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME

check_describe () {
indir= &&
outcome=success &&
while test $# != 0
do
case "$1" in
-C)
indir="$2"
shift
;;
--expect-failure)
outcome=failure
;;
*)
break
;;
Expand All @@ -35,7 +39,7 @@ check_describe () {
expect="$1"
shift
describe_opts="$@"
test_expect_success "describe $describe_opts" '
test_expect_${outcome} "describe $describe_opts" '
git ${indir:+ -C "$indir"} describe $describe_opts >raw &&
sed -e "s/-g[0-9a-f]*\$/-gHASH/" <raw >actual &&
echo "$expect" >expect &&
Expand Down Expand Up @@ -616,7 +620,7 @@ test_expect_success 'name-rev --annotate-stdin works with commitGraph' '

# B
# o
# \
# H \
# o-----o---o----x
# A
#
Expand All @@ -626,6 +630,7 @@ test_expect_success 'setup: describe commits with disjoint bases' '
cd disjoint1 &&
echo o >> file && git add file && git commit -m o &&
git tag H -a -m H &&
echo A >> file && git add file && git commit -m A &&
git tag A -a -m A &&
echo o >> file && git add file && git commit -m o &&
Expand All @@ -638,8 +643,9 @@ test_expect_success 'setup: describe commits with disjoint bases' '
'

check_describe -C disjoint1 "A-3-gHASH" HEAD
check_describe -C disjoint1 --expect-failure "A-3-gHASH" --candidates=2 HEAD

# B
# H B
# o---o---o------------.
# \
# o---o---x
Expand All @@ -657,13 +663,15 @@ test_expect_success 'setup: describe commits with disjoint bases 2' '
git checkout --orphan branch &&
echo o >> file2 && git add file2 && GIT_COMMITTER_DATE="2020-01-01 15:00" git commit -m o &&
echo o >> file2 && git add file2 && GIT_COMMITTER_DATE="2020-01-01 15:01" git commit -m o &&
git tag H -a -m H &&
echo B >> file2 && git add file2 && GIT_COMMITTER_DATE="2020-01-01 15:02" git commit -m B &&
git tag B -a -m B &&
git merge --no-ff --allow-unrelated-histories main -m x
)
'

check_describe -C disjoint2 "B-3-gHASH" HEAD
check_describe -C disjoint2 --expect-failure "B-3-gHASH" --candidates=2 HEAD

test_expect_success 'setup misleading taggerdates' '
GIT_COMMITTER_DATE="2006-12-12 12:31" git tag -a -m "another tag" newer-tag-older-commit unique-file~1
Expand Down

0 comments on commit 6a0c12f

Please sign in to comment.