mirror of
https://github.com/git/git.git
synced 2026-09-28 17:12:46 +02:00
verify_cache() checks that the index does not contain both "path" and
"path/file" before writing a tree. It does this by comparing only
adjacent entries, relying on the assumption that "path/file" would
immediately follow "path" in sorted order. Unfortunately, this
assumption does not always hold. For example:
docs <-- submodule entry
docs-internal/README.md <-- intervening entry
docs/requirements.txt <-- D/F conflict, NOT adjacent to "docs"
When this happens, verify_cache() silently misses the D/F conflict and
write-tree produces a corrupt tree object containing duplicate entries
(one for the submodule "docs" and one for the tree "docs").
I could not find any caller in current git that both allows the index to
get into this state and then tries to write it out without doing other
checks beyond the verify_cache() call in cache_tree_update(), but
verify_cache() is documented as a safety net for preventing corrupt
trees and should actually provide that guarantee. A downstream consumer
that relied solely on cache_tree_update()'s internal checking via
verify_cache() to prevent duplicate tree entries was bitten by the gap.
Add a test that constructs a corrupt index directly (bypassing the D/F
checks in add_index_entry) and verifies that write-tree now rejects it.
Signed-off-by: Elijah Newren <newren@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
60 lines
1.6 KiB
Bash
Executable File
60 lines
1.6 KiB
Bash
Executable File
#!/bin/sh
|
|
|
|
test_description='verify_cache() must catch non-adjacent D/F conflicts
|
|
|
|
Ensure that verify_cache() can complain about bad entries like:
|
|
|
|
docs <-- submodule
|
|
docs-internal/... <-- sorts here because "-" < "/"
|
|
docs/... <-- D/F conflict with "docs" above, not adjacent
|
|
|
|
In order to test verify_cache, we directly construct a corrupt index
|
|
(bypassing the D/F safety checks in add_index_entry) and verify that
|
|
write-tree rejects it.
|
|
'
|
|
|
|
. ./test-lib.sh
|
|
|
|
if ! test_have_prereq PERL
|
|
then
|
|
skip_all='skipping verify_cache D/F tests; Perl not available'
|
|
test_done
|
|
fi
|
|
|
|
# Build a v2 index from entries on stdin, bypassing D/F checks.
|
|
# Each line: "octalmode hex-oid name" (entries must be pre-sorted).
|
|
build_corrupt_index () {
|
|
perl "$TEST_DIRECTORY/t0093-direct-index-write.pl" >"$1"
|
|
}
|
|
|
|
test_expect_success 'setup objects' '
|
|
test_commit base &&
|
|
BLOB=$(git rev-parse HEAD:base.t) &&
|
|
SUB_COMMIT=$(git rev-parse HEAD)
|
|
'
|
|
|
|
test_expect_success 'adjacent D/F conflict is caught by verify_cache' '
|
|
cat >index-entries <<-EOF &&
|
|
0160000 $SUB_COMMIT docs
|
|
0100644 $BLOB docs/requirements.txt
|
|
EOF
|
|
build_corrupt_index .git/index <index-entries &&
|
|
|
|
test_must_fail git write-tree 2>err &&
|
|
test_grep "You have both docs and docs/requirements.txt" err
|
|
'
|
|
|
|
test_expect_success 'non-adjacent D/F conflict is caught by verify_cache' '
|
|
cat >index-entries <<-EOF &&
|
|
0160000 $SUB_COMMIT docs
|
|
0100644 $BLOB docs-internal/README.md
|
|
0100644 $BLOB docs/requirements.txt
|
|
EOF
|
|
build_corrupt_index .git/index <index-entries &&
|
|
|
|
test_must_fail git write-tree 2>err &&
|
|
test_grep "You have both docs and docs/requirements.txt" err
|
|
'
|
|
|
|
test_done
|