git/t/t7007-show.sh
Ævar Arnfjörð Bjarmason 055e57b7b2 log: fix a memory leak in "git show <revision>..."
Fix a memory leak in code added in 5d7eeee2ac (git-show: grok blobs,
trees and tags, too, 2006-12-14). As we iterate over a "<revision>..."
command-line and encounter ad OBJ_COMMIT we want to use our "struct
rev_info", but with a "pending" array of one element: the one commit
we're showing in the loop.

To do this 5d7eeee2ac saved away a pointer to rev.pending.objects and
rev.pending.nr for its iteration. We'd then clobber those (and alloc)
when we needed to show an OBJ_COMMIT.

We'd therefore leak the "rev.pending" we started out with, and only
free the new "rev.pending" in the "OBJ_COMMIT" case arm as
prepare_revision_walk() would draw it down.

Let's fix this memory leak. Now when we encounter an OBJ_COMMIT we
save away the "rev.pending" before clearing it. We then add a single
commit to it, which our indirect invocation of prepare_revision_walk()
will remove. After that we restore the "rev.pending".

Our "rev.pending" will then get free'd by the release_revisions()
added in f6bfea0ad0 (revisions API users: use release_revisions() in
builtin/log.c, 2022-04-13)

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2022-08-03 10:16:28 -07:00

171 lines
3.8 KiB
Bash
Executable file

#!/bin/sh
test_description='git show'
TEST_PASSES_SANITIZE_LEAK=true
. ./test-lib.sh
test_expect_success setup '
echo hello world >foo &&
H=$(git hash-object -w foo) &&
git tag -a foo-tag -m "Tags $H" $H &&
HH=$(expr "$H" : "\(..\)") &&
H38=$(expr "$H" : "..\(.*\)") &&
rm -f .git/objects/$HH/$H38
'
test_expect_success 'showing a tag that point at a missing object' '
test_must_fail git --no-pager show foo-tag
'
test_expect_success 'set up a bit of history' '
test_commit main1 &&
test_commit main2 &&
test_commit main3 &&
git tag -m "annotated tag" annotated &&
git checkout -b side HEAD^^ &&
test_commit side2 &&
test_commit side3 &&
test_merge merge main3
'
test_expect_success 'showing two commits' '
cat >expect <<-EOF &&
commit $(git rev-parse main2)
commit $(git rev-parse main3)
EOF
git show main2 main3 >actual &&
grep ^commit actual >actual.filtered &&
test_cmp expect actual.filtered
'
test_expect_success 'showing a tree' '
cat >expected <<-EOF &&
tree main1:
main1.t
EOF
git show main1: >actual &&
test_cmp expected actual
'
test_expect_success 'showing two trees' '
cat >expected <<-EOF &&
tree main1^{tree}
main1.t
tree main2^{tree}
main1.t
main2.t
EOF
git show main1^{tree} main2^{tree} >actual &&
test_cmp expected actual
'
test_expect_success 'showing a trees is not recursive' '
git worktree add not-recursive main1 &&
mkdir not-recursive/a &&
test_commit -C not-recursive a/file &&
cat >expected <<-EOF &&
tree HEAD^{tree}
a/
main1.t
EOF
git -C not-recursive show HEAD^{tree} >actual &&
test_cmp expected actual
'
test_expect_success 'showing a range walks (linear)' '
cat >expect <<-EOF &&
commit $(git rev-parse main3)
commit $(git rev-parse main2)
EOF
git show main1..main3 >actual &&
grep ^commit actual >actual.filtered &&
test_cmp expect actual.filtered
'
test_expect_success 'showing a range walks (Y shape, ^ first)' '
cat >expect <<-EOF &&
commit $(git rev-parse main3)
commit $(git rev-parse main2)
EOF
git show ^side3 main3 >actual &&
grep ^commit actual >actual.filtered &&
test_cmp expect actual.filtered
'
test_expect_success 'showing a range walks (Y shape, ^ last)' '
cat >expect <<-EOF &&
commit $(git rev-parse main3)
commit $(git rev-parse main2)
EOF
git show main3 ^side3 >actual &&
grep ^commit actual >actual.filtered &&
test_cmp expect actual.filtered
'
test_expect_success 'showing with -N walks' '
cat >expect <<-EOF &&
commit $(git rev-parse main3)
commit $(git rev-parse main2)
EOF
git show -2 main3 >actual &&
grep ^commit actual >actual.filtered &&
test_cmp expect actual.filtered
'
test_expect_success 'showing annotated tag' '
cat >expect <<-EOF &&
tag annotated
commit $(git rev-parse annotated^{commit})
EOF
git show annotated >actual &&
grep -E "^(commit|tag)" actual >actual.filtered &&
test_cmp expect actual.filtered
'
test_expect_success 'showing annotated tag plus commit' '
cat >expect <<-EOF &&
tag annotated
commit $(git rev-parse annotated^{commit})
commit $(git rev-parse side3)
EOF
git show annotated side3 >actual &&
grep -E "^(commit|tag)" actual >actual.filtered &&
test_cmp expect actual.filtered
'
test_expect_success 'showing range' '
cat >expect <<-EOF &&
commit $(git rev-parse main3)
commit $(git rev-parse main2)
EOF
git show ^side3 annotated >actual &&
grep -E "^(commit|tag)" actual >actual.filtered &&
test_cmp expect actual.filtered
'
test_expect_success '-s suppresses diff' '
cat >expect <<-\EOF &&
merge
main3
EOF
git show -s --format=%s merge main3 >actual &&
test_cmp expect actual
'
test_expect_success '--quiet suppresses diff' '
echo main3 >expect &&
git show --quiet --format=%s main3 >actual &&
test_cmp expect actual
'
test_expect_success 'show --graph is forbidden' '
test_must_fail git show --graph HEAD
'
test_done