git: match gitattributes patterns strictly

When we look for patterns in the .gitattributes file, we want to match
these patterns strictly: that is, exactly the way that Git does.  We
don't want to use a different algorithm because that will lead to
confusing behavior when our code behaves differently from Git does.

Let's fix this by using our new strict matching when reading patterns
from the gitattributes files and add a test to make sure we don't
regress anything.
This commit is contained in:
brian m. carlson 2020-11-12 18:41:32 +00:00
parent fecaf2da30
commit 6e8b16667a
No known key found for this signature in database
GPG Key ID: 2D0C9BC12F82B3A1
2 changed files with 39 additions and 1 deletions

@ -159,7 +159,7 @@ func GetAttributeFilter(workingDir, gitDir string) *filepathfilter.Filter {
for _, path := range paths {
// Convert all separators to `/` before creating a pattern to
// avoid characters being escaped in situations like `subtree\*.md`
patterns = append(patterns, filepathfilter.NewPattern(filepath.ToSlash(path.Path)))
patterns = append(patterns, filepathfilter.NewPattern(filepath.ToSlash(path.Path), filepathfilter.Strict(true)))
}
return filepathfilter.NewFromPatterns(patterns, nil)

@ -225,3 +225,41 @@ begin_test "migrate import --no-rewrite (with empty commit message)"
fi
)
end_test
begin_test "migrate import --no-rewrite (strict .gitattributes)"
(
set -e
reponame="$(basename "$0" ".sh")-strict-match"
clone_repo "$reponame" repo-strict-match
mkdir -p major-oak/mainst/.yarn-offline-mirror/
mkdir -p major-oak/major-oak/frontend/.yarn-offline-mirror/
foo_contents="foo"
foo_oid=$(calc_oid "$foo_contents")
bar_contents="bar"
bar_oid=$(calc_oid "$bar_contents")
printf "$foo_contents" > major-oak/mainst/.yarn-offline-mirror/typescript-3.4.3.tgz
printf "$bar_contents" > major-oak/major-oak/frontend/.yarn-offline-mirror/typescript-2.9.2.tgz
git add .
git commit -m 'Initial import'
cat >.gitattributes <<EOF
major-oak/mainst/.yarn-offline-mirror/typescript-3.4.3.tgz filter=lfs diff=lfs merge=lfs -text
major-oak/major-oak/frontend/.yarn-offline-mirror/typescript-2.9.2.tgz filter=lfs diff=lfs merge=lfs -text
EOF
git add .
git commit -m '.gitattributes'
git lfs migrate import --yes --no-rewrite \
major-oak/mainst/.yarn-offline-mirror/typescript-3.4.3.tgz \
major-oak/major-oak/frontend/.yarn-offline-mirror/typescript-2.9.2.tgz
assert_pointer "refs/heads/main" "major-oak/mainst/.yarn-offline-mirror/typescript-3.4.3.tgz" "$foo_oid" "3"
assert_pointer "refs/heads/main" "major-oak/major-oak/frontend/.yarn-offline-mirror/typescript-2.9.2.tgz" "$bar_oid" "3"
assert_local_object "$foo_oid" "3"
assert_local_object "$bar_oid" "3"
)
end_test