From 49bdc2b1b0bde83a6bca29591d6769f286d0203f Mon Sep 17 00:00:00 2001 From: Taylor Blau Date: Thu, 5 Apr 2018 10:51:26 -0700 Subject: [PATCH] commands/{un,}track: local-prefix agnostic comparison This patch use the trimCurrentPrefix utility introduced in the previous patch to make all .gitattributes comparisons agnostic to the "current-prefix" of "./" or ".\". Since we treat both sides of the comparison as having passed through "trimCurrentPrefix", we harden ourselves against "legacy"-style .gitattributes patterns containing prefixes, and "modern"-style ones lacking prefixes. That means that both combinations of "git lfs untrack ./a.dat" and "git lfs untrack a.dat" will remove both of these lines from a .gittatributes file: ./a.dat filter=lfs ... a.dat filter=lfs ... Signed-off-by: Taylor Blau --- commands/command_track.go | 2 +- commands/command_untrack.go | 3 +- test/test-track.sh | 18 +++++++++++ test/test-untrack.sh | 62 +++++++++++++++++++++++++++++++++++++ 4 files changed, 83 insertions(+), 2 deletions(-) diff --git a/commands/command_track.go b/commands/command_track.go index db013dfd..5ba06925 100644 --- a/commands/command_track.go +++ b/commands/command_track.go @@ -67,7 +67,7 @@ func trackCommand(cmd *cobra.Command, args []string) { var writeablePatterns []string ArgsLoop: for _, unsanitizedPattern := range args { - pattern := cleanRootPath(unsanitizedPattern) + pattern := trimCurrentPrefix(cleanRootPath(unsanitizedPattern)) if !trackNoModifyAttrsFlag { for _, known := range knownPatterns { if known.Path == filepath.Join(relpath, pattern) && diff --git a/commands/command_untrack.go b/commands/command_untrack.go index 99c59d4b..f15bfbb3 100644 --- a/commands/command_untrack.go +++ b/commands/command_untrack.go @@ -63,8 +63,9 @@ func untrackCommand(cmd *cobra.Command, args []string) { } func removePath(path string, args []string) bool { + withoutCurrentDir := trimCurrentPrefix(path) for _, t := range args { - if path == escapeAttrPattern(t) { + if withoutCurrentDir == escapeAttrPattern(trimCurrentPrefix(t)) { return true } } diff --git a/test/test-track.sh b/test/test-track.sh index 65ec3423..02ff7ce0 100755 --- a/test/test-track.sh +++ b/test/test-track.sh @@ -540,3 +540,21 @@ begin_test "track (with comments)" [ "0" -eq "$(grep -c "\.png" track.log)" ] ) end_test + +begin_test "track (with current-directory prefix)" +( + set -e + + reponame="track-with-current-directory-prefix" + git init "$reponame" + cd "$reponame" + + git lfs track "./a.dat" + printf "a" > a.dat + + git add .gitattributes a.dat + git commit -m "initial commit" + + grep -e "^a.dat" .gitattributes +) +end_test diff --git a/test/test-untrack.sh b/test/test-untrack.sh index e484c9fe..2e5005ea 100755 --- a/test/test-untrack.sh +++ b/test/test-untrack.sh @@ -72,3 +72,65 @@ begin_test "untrack removes escape sequences" assert_attributes_count "\\#" "filter=lfs" 0 ) end_test + +begin_test "untrack removes prefixed patterns (legacy)" +( + set -e + + reponame="untrack-removes-prefix-patterns-legacy" + git init "$reponame" + cd "$reponame" + + echo "./a.dat filter=lfs diff=lfs merge=lfs" > .gitattributes + printf "a" > a.dat + git add .gitattributes a.dat + git commit -m "initial commit" + + git lfs untrack "./a.dat" + + if [ ! -z "$(cat .gitattributes)" ]; then + echo &>2 "fatal: expected 'git lfs untrack' to clear .gitattributes" + exit 1 + fi + + git checkout -- .gitattributes + + git lfs untrack "a.dat" + + if [ ! -z "$(cat .gitattributes)" ]; then + echo &>2 "fatal: expected 'git lfs untrack' to clear .gitattributes" + exit 1 + fi +) +end_test + +begin_test "untrack removes prefixed patterns (modern)" +( + set -e + + reponame="untrack-removes-prefix-patterns-modern" + git init "$reponame" + cd "$reponame" + + echo "a.dat filter=lfs diff=lfs merge=lfs" > .gitattributes + printf "a" > a.dat + git add .gitattributes a.dat + git commit -m "initial commit" + + git lfs untrack "./a.dat" + + if [ ! -z "$(cat .gitattributes)" ]; then + echo &>2 "fatal: expected 'git lfs untrack' to clear .gitattributes" + exit 1 + fi + + git checkout -- .gitattributes + + git lfs untrack "a.dat" + + if [ ! -z "$(cat .gitattributes)" ]; then + echo &>2 "fatal: expected 'git lfs untrack' to clear .gitattributes" + exit 1 + fi +) +end_test