Files
git/t/t4007-rename-3.sh
René Scharfe f293bdcc29 diff-files: fix copy detection
Copy detection cannot work when comparing the index to the working tree
because Git ignores files that it is not explicitly told to track.  It
should work in the other direction, though, i.e. for a reverse diff of
the deletion of a copy from the index.

d1f2d7e8ca (Make run_diff_index() use unpack_trees(), not read_tree(),
2008-01-19) broke it with a seemingly stray change to run_diff_files().

We didn't notice because there's no test for that.  But even if we had
one, it might have gone unnoticed because the breakage only happens
with index preloading, which requires at least 1000 entries (more than
most test repos have) and is racy because it runs in parallel with the
actual command.

Fix copy detection by queuing up-to-date and skip-worktree entries using
diff_same().

While at it, use diff_same() also for queuing unchanged files not
flagged as up-to-date, i.e. clean submodules and entries where
preloading was not done at all or not quickly enough.  It uses less
memory than diff_change() and doesn't unnecessarily set the diff flag
has_changes.

Add two tests to cover running both without and with preloading.  The
first one passes reliably with the original code.  The second one
enables preloading and thus is racy.  It has a good chance to pass even
without the fix, but fails within seconds when running the test script
with --stress.  With the fix it runs fine for several minutes, until
my patience runs out.

Signed-off-by: René Scharfe <l.s.r@web.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2025-12-16 10:23:26 +09:00

113 lines
3.3 KiB
Bash
Executable File

#!/bin/sh
#
# Copyright (c) 2005 Junio C Hamano
#
test_description='Rename interaction with pathspec.
'
. ./test-lib.sh
. "$TEST_DIRECTORY"/lib-diff.sh ;# test-lib chdir's into trash
test_expect_success 'prepare reference tree' '
mkdir path0 path1 &&
COPYING_test_data >path0/COPYING &&
git update-index --add path0/COPYING &&
tree=$(git write-tree) &&
blob=$(git rev-parse :path0/COPYING)
'
test_expect_success 'prepare work tree' '
cp path0/COPYING path1/COPYING &&
git update-index --add --remove path0/COPYING path1/COPYING
'
# In the tree, there is only path0/COPYING. In the cache, path0 and
# path1 both have COPYING and the latter is a copy of path0/COPYING.
# Comparing the full tree with cache should tell us so.
cat >expected <<EOF
:100644 100644 $blob $blob C100 path0/COPYING path1/COPYING
EOF
test_expect_success 'copy detection' '
git diff-index -C --find-copies-harder $tree >current &&
compare_diff_raw current expected
'
test_expect_success 'copy detection, cached' '
git diff-index -C --find-copies-harder --cached $tree >current &&
compare_diff_raw current expected
'
# In the tree, there is only path0/COPYING. In the cache, path0 and
# path1 both have COPYING and the latter is a copy of path0/COPYING.
# However when we say we care only about path1, we should just see
# path1/COPYING suddenly appearing from nowhere, not detected as
# a copy from path0/COPYING.
cat >expected <<EOF
:000000 100644 $ZERO_OID $blob A path1/COPYING
EOF
test_expect_success 'copy, limited to a subtree' '
git diff-index -C --find-copies-harder $tree path1 >current &&
compare_diff_raw current expected
'
test_expect_success 'tweak work tree' '
rm -f path0/COPYING
'
cat >expected <<EOF
:100644 100644 $blob $blob C100 path1/COPYING path0/COPYING
EOF
# The cache has path0/COPYING and path1/COPYING, the working tree only
# path1/COPYING. This is a deletion -- we don't treat deduplication
# specially. In reverse it should be detected as a copy, though.
test_expect_success 'copy detection, files to index' '
git diff-files -C --find-copies-harder -R >current &&
compare_diff_raw current expected
'
test_expect_success 'copy detection, files to preloaded index' '
GIT_TEST_PRELOAD_INDEX=1 \
git diff-files -C --find-copies-harder -R >current &&
compare_diff_raw current expected
'
test_expect_success 'tweak index' '
git update-index --remove path0/COPYING
'
# In the tree, there is only path0/COPYING. In the cache, path0 does
# not have COPYING anymore and path1 has COPYING which is a copy of
# path0/COPYING. Showing the full tree with cache should tell us about
# the rename.
cat >expected <<EOF
:100644 100644 $blob $blob R100 path0/COPYING path1/COPYING
EOF
test_expect_success 'rename detection' '
git diff-index -C --find-copies-harder $tree >current &&
compare_diff_raw current expected
'
# In the tree, there is only path0/COPYING. In the cache, path0 does
# not have COPYING anymore and path1 has COPYING which is a copy of
# path0/COPYING. When we say we care only about path1, we should just
# see path1/COPYING appearing from nowhere.
cat >expected <<EOF
:000000 100644 $ZERO_OID $blob A path1/COPYING
EOF
test_expect_success 'rename, limited to a subtree' '
git diff-index -C --find-copies-harder $tree path1 >current &&
compare_diff_raw current expected
'
test_done