git/t/t3705-add-sparse-checkout.sh
Matheus Tavares 20141e322c add, rm, mv: fix bug that prevents the update of non-sparse dirs
These three commands recently learned to avoid updating paths outside
the sparse checkout even if they are missing the SKIP_WORKTREE bit. This
is done using path_in_sparse_checkout(), which checks whether a given
path matches the current list of sparsity rules, similar to what
clear_ce_flags() does when we run "git sparse checkout init" or "git
sparse-checkout reapply". However, clear_ce_flags() uses a recursive
approach, applying the match results from parent directories on paths
that get the UNDECIDED result, whereas path_in_sparse_checkout() only
attempts to match the full path and immediately considers UNDECIDED as
NOT_MATCHED. This makes the function miss matches with leading
directories. For example, if the user has the sparsity patterns "!/a"
and "b/", add, rm, and mv will fail to update the path "a/b/c" and end
up displaying a warning about it being outside the sparse checkout even
though it isn't. This problem only occurs in full pattern mode as the
pattern matching functions never return UNDECIDED for cone mode.

To fix this, replicate the recursive behavior of clear_ce_flags() in
path_in_sparse_checkout(), falling back to the parent directory match
when a path gets the UNDECIDED result. (If this turns out to be too
expensive in some cases, we may want to later add some form of caching
to accelerate multiple queries within the same directory. This is not
implemented in this patch, though.) Also add two tests for each affected
command (add, rm, and mv) to check that they behave correctly with the
recursive pattern matching. The first test would previously fail without
this patch while the second already succeeded. It is added mostly to
make sure that we are not breaking the existing pattern matching for
directories that are really sparse, and also as a protection against any
future regressions.

Two other existing tests had to be changed as well: one test in t3602
checks that "git rm -r <dir>" won't remove sparse entries, but it didn't
allow the non-sparse entries inside <dir> to be removed. The other one,
in t7002, tested that "git mv" would correctly display a warning message
for sparse paths, but it accidentally expected the message to include
two non-sparse paths as well.

Signed-off-by: Matheus Tavares <matheus.bernardino@usp.br>
Acked-by: Derrick Stolee <dstolee@microsoft.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2021-10-28 08:46:07 -07:00

234 lines
6.7 KiB
Bash
Executable file

#!/bin/sh
test_description='git add in sparse checked out working trees'
. ./test-lib.sh
SPARSE_ENTRY_BLOB=""
# Optionally take a printf format string to write to the sparse_entry file
setup_sparse_entry () {
# 'sparse_entry' might already be in the index with the skip-worktree
# bit set. Remove it so that the subsequent git add can update it.
git update-index --force-remove sparse_entry &&
if test $# -eq 1
then
printf "$1" >sparse_entry
else
>sparse_entry
fi &&
git add sparse_entry &&
git update-index --skip-worktree sparse_entry &&
git commit --allow-empty -m "ensure sparse_entry exists at HEAD" &&
SPARSE_ENTRY_BLOB=$(git rev-parse :sparse_entry)
}
test_sparse_entry_unchanged () {
echo "100644 $SPARSE_ENTRY_BLOB 0 sparse_entry" >expected &&
git ls-files --stage sparse_entry >actual &&
test_cmp expected actual
}
setup_gitignore () {
test_when_finished rm -f .gitignore &&
cat >.gitignore <<-EOF
*
!/sparse_entry
EOF
}
test_sparse_entry_unstaged () {
git diff --staged -- sparse_entry >diff &&
test_must_be_empty diff
}
test_expect_success 'setup' "
cat >sparse_error_header <<-EOF &&
The following paths and/or pathspecs matched paths that exist
outside of your sparse-checkout definition, so will not be
updated in the index:
EOF
cat >sparse_hint <<-EOF &&
hint: If you intend to update such entries, try one of the following:
hint: * Use the --sparse option.
hint: * Disable or modify the sparsity rules.
hint: Disable this message with \"git config advice.updateSparsePath false\"
EOF
echo sparse_entry | cat sparse_error_header - >sparse_entry_error &&
cat sparse_entry_error sparse_hint >error_and_hint
"
test_expect_success 'git add does not remove sparse entries' '
setup_sparse_entry &&
rm sparse_entry &&
test_must_fail git add sparse_entry 2>stderr &&
test_sparse_entry_unstaged &&
test_cmp error_and_hint stderr &&
test_sparse_entry_unchanged
'
test_expect_success 'git add -A does not remove sparse entries' '
setup_sparse_entry &&
rm sparse_entry &&
setup_gitignore &&
git add -A 2>stderr &&
test_must_be_empty stderr &&
test_sparse_entry_unchanged
'
test_expect_success 'git add . does not remove sparse entries' '
setup_sparse_entry &&
rm sparse_entry &&
setup_gitignore &&
test_must_fail git add . 2>stderr &&
test_sparse_entry_unstaged &&
cat sparse_error_header >expect &&
echo . >>expect &&
cat sparse_hint >>expect &&
test_cmp expect stderr &&
test_sparse_entry_unchanged
'
for opt in "" -f -u --ignore-removal --dry-run
do
test_expect_success "git add${opt:+ $opt} does not update sparse entries" '
setup_sparse_entry &&
echo modified >sparse_entry &&
test_must_fail git add $opt sparse_entry 2>stderr &&
test_sparse_entry_unstaged &&
test_cmp error_and_hint stderr &&
test_sparse_entry_unchanged
'
done
test_expect_success 'git add --refresh does not update sparse entries' '
setup_sparse_entry &&
git ls-files --debug sparse_entry | grep mtime >before &&
test-tool chmtime -60 sparse_entry &&
test_must_fail git add --refresh sparse_entry 2>stderr &&
test_sparse_entry_unstaged &&
test_cmp error_and_hint stderr &&
git ls-files --debug sparse_entry | grep mtime >after &&
test_cmp before after
'
test_expect_success 'git add --chmod does not update sparse entries' '
setup_sparse_entry &&
test_must_fail git add --chmod=+x sparse_entry 2>stderr &&
test_sparse_entry_unstaged &&
test_cmp error_and_hint stderr &&
test_sparse_entry_unchanged &&
! test -x sparse_entry
'
test_expect_success 'git add --renormalize does not update sparse entries' '
test_config core.autocrlf false &&
setup_sparse_entry "LINEONE\r\nLINETWO\r\n" &&
echo "sparse_entry text=auto" >.gitattributes &&
test_must_fail git add --renormalize sparse_entry 2>stderr &&
test_sparse_entry_unstaged &&
test_cmp error_and_hint stderr &&
test_sparse_entry_unchanged
'
test_expect_success 'git add --dry-run --ignore-missing warn on sparse path' '
setup_sparse_entry &&
rm sparse_entry &&
test_must_fail git add --dry-run --ignore-missing sparse_entry 2>stderr &&
test_sparse_entry_unstaged &&
test_cmp error_and_hint stderr &&
test_sparse_entry_unchanged
'
test_expect_success 'do not advice about sparse entries when they do not match the pathspec' '
setup_sparse_entry &&
test_must_fail git add nonexistent 2>stderr &&
grep "fatal: pathspec .nonexistent. did not match any files" stderr &&
! grep -F -f sparse_error_header stderr
'
test_expect_success 'do not warn when pathspec matches dense entries' '
setup_sparse_entry &&
echo modified >sparse_entry &&
>dense_entry &&
git add "*_entry" 2>stderr &&
test_must_be_empty stderr &&
test_sparse_entry_unchanged &&
git ls-files --error-unmatch dense_entry
'
test_expect_success 'git add fails outside of sparse-checkout definition' '
test_when_finished git sparse-checkout disable &&
test_commit a &&
git sparse-checkout init &&
git sparse-checkout set a &&
echo >>sparse_entry &&
git update-index --no-skip-worktree sparse_entry &&
test_must_fail git add sparse_entry &&
test_sparse_entry_unstaged &&
test_must_fail git add --chmod=+x sparse_entry &&
test_sparse_entry_unstaged &&
test_must_fail git add --renormalize sparse_entry &&
test_sparse_entry_unstaged &&
# Avoid munging CRLFs to avoid an error message
git -c core.autocrlf=input add --sparse sparse_entry 2>stderr &&
test_must_be_empty stderr &&
test-tool read-cache --table >actual &&
grep "^100644 blob.*sparse_entry\$" actual &&
git add --sparse --chmod=+x sparse_entry 2>stderr &&
test_must_be_empty stderr &&
test-tool read-cache --table >actual &&
grep "^100755 blob.*sparse_entry\$" actual &&
git reset &&
# This will print a message over stderr on Windows.
git add --sparse --renormalize sparse_entry &&
git status --porcelain >actual &&
grep "^M sparse_entry\$" actual
'
test_expect_success 'add obeys advice.updateSparsePath' '
setup_sparse_entry &&
test_must_fail git -c advice.updateSparsePath=false add sparse_entry 2>stderr &&
test_sparse_entry_unstaged &&
test_cmp sparse_entry_error stderr
'
test_expect_success 'add allows sparse entries with --sparse' '
git sparse-checkout set a &&
echo modified >sparse_entry &&
test_must_fail git add sparse_entry &&
test_sparse_entry_unchanged &&
git add --sparse sparse_entry 2>stderr &&
test_must_be_empty stderr
'
test_expect_success 'can add files from non-sparse dir' '
git sparse-checkout set w !/x y/ &&
mkdir -p w x/y &&
touch w/f x/y/f &&
git add w/f x/y/f 2>stderr &&
test_must_be_empty stderr
'
test_expect_success 'refuse to add non-skip-worktree file from sparse dir' '
git sparse-checkout set !/x y/ !x/y/z &&
mkdir -p x/y/z &&
touch x/y/z/f &&
test_must_fail git add x/y/z/f 2>stderr &&
echo x/y/z/f | cat sparse_error_header - sparse_hint >expect &&
test_cmp expect stderr
'
test_done