git/t/t1409-avoid-packing-refs.sh
Ævar Arnfjörð Bjarmason 03267e8656 commit: discard partial cache before (re-)reading it
The read_cache() in prepare_to_commit() would end up clobbering the
pointer we had for a previously populated "the_index.cache_tree" in
the very common case of "git commit" stressed by e.g. the tests being
changed here.

We'd populate "the_index.cache_tree" by calling
"update_main_cache_tree" in prepare_index(), but would not end up with
a "fully prepared" index. What constitutes an existing index is
clearly overly fuzzy, here we'll check "active_nr" (aka
"the_index.cache_nr"), but our "the_index.cache_tree" might have been
malloc()'d already.

Thus the code added in 11c8a74a64 (commit: write cache-tree data when
writing index anyway, 2011-12-06) would end up allocating the
"cache_tree", and would interact here with code added in
7168624c35 (Do not generate full commit log message if it is not
going to be used, 2007-11-28). The result was a very common memory
leak.

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Signed-off-by: Taylor Blau <me@ttaylorr.com>
2022-11-21 12:32:48 +09:00

120 lines
3.5 KiB
Bash
Executable File

#!/bin/sh
test_description='avoid rewriting packed-refs unnecessarily'
TEST_PASSES_SANITIZE_LEAK=true
. ./test-lib.sh
# Add an identifying mark to the packed-refs file header line. This
# shouldn't upset readers, and it should be omitted if the file is
# ever rewritten.
mark_packed_refs () {
sed -e "s/^\(#.*\)/\1 t1409 /" .git/packed-refs >.git/packed-refs.new &&
mv .git/packed-refs.new .git/packed-refs
}
# Verify that the packed-refs file is still marked.
check_packed_refs_marked () {
grep -q '^#.* t1409 ' .git/packed-refs
}
test_expect_success 'setup' '
git commit --allow-empty -m "Commit A" &&
A=$(git rev-parse HEAD) &&
git commit --allow-empty -m "Commit B" &&
B=$(git rev-parse HEAD) &&
git commit --allow-empty -m "Commit C" &&
C=$(git rev-parse HEAD)
'
test_expect_success 'do not create packed-refs file gratuitously' '
test_path_is_missing .git/packed-refs &&
git update-ref refs/heads/foo $A &&
test_path_is_missing .git/packed-refs &&
git update-ref refs/heads/foo $B &&
test_path_is_missing .git/packed-refs &&
git update-ref refs/heads/foo $C $B &&
test_path_is_missing .git/packed-refs &&
git update-ref -d refs/heads/foo &&
test_path_is_missing .git/packed-refs
'
test_expect_success 'check that marking the packed-refs file works' '
git for-each-ref >expected &&
git pack-refs --all &&
mark_packed_refs &&
check_packed_refs_marked &&
git for-each-ref >actual &&
test_cmp expected actual &&
git pack-refs --all &&
! check_packed_refs_marked &&
git for-each-ref >actual2 &&
test_cmp expected actual2
'
test_expect_success 'leave packed-refs untouched on update of packed' '
git update-ref refs/heads/packed-update $A &&
git pack-refs --all &&
mark_packed_refs &&
git update-ref refs/heads/packed-update $B &&
check_packed_refs_marked
'
test_expect_success 'leave packed-refs untouched on checked update of packed' '
git update-ref refs/heads/packed-checked-update $A &&
git pack-refs --all &&
mark_packed_refs &&
git update-ref refs/heads/packed-checked-update $B $A &&
check_packed_refs_marked
'
test_expect_success 'leave packed-refs untouched on verify of packed' '
git update-ref refs/heads/packed-verify $A &&
git pack-refs --all &&
mark_packed_refs &&
echo "verify refs/heads/packed-verify $A" | git update-ref --stdin &&
check_packed_refs_marked
'
test_expect_success 'touch packed-refs on delete of packed' '
git update-ref refs/heads/packed-delete $A &&
git pack-refs --all &&
mark_packed_refs &&
git update-ref -d refs/heads/packed-delete &&
! check_packed_refs_marked
'
test_expect_success 'leave packed-refs untouched on update of loose' '
git pack-refs --all &&
git update-ref refs/heads/loose-update $A &&
mark_packed_refs &&
git update-ref refs/heads/loose-update $B &&
check_packed_refs_marked
'
test_expect_success 'leave packed-refs untouched on checked update of loose' '
git pack-refs --all &&
git update-ref refs/heads/loose-checked-update $A &&
mark_packed_refs &&
git update-ref refs/heads/loose-checked-update $B $A &&
check_packed_refs_marked
'
test_expect_success 'leave packed-refs untouched on verify of loose' '
git pack-refs --all &&
git update-ref refs/heads/loose-verify $A &&
mark_packed_refs &&
echo "verify refs/heads/loose-verify $A" | git update-ref --stdin &&
check_packed_refs_marked
'
test_expect_success 'leave packed-refs untouched on delete of loose' '
git pack-refs --all &&
git update-ref refs/heads/loose-delete $A &&
mark_packed_refs &&
git update-ref -d refs/heads/loose-delete &&
check_packed_refs_marked
'
test_done