From 3ad1222cf1552988402f9c87201ee06070adb730 Mon Sep 17 00:00:00 2001 From: Taylor Blau Date: Tue, 5 Jul 2016 15:01:30 -0600 Subject: [PATCH] commands/track: delay .gitattributes and touch until filters are passed --- commands/command_track.go | 56 +++++++++++++++++++++++++++------------ 1 file changed, 39 insertions(+), 17 deletions(-) diff --git a/commands/command_track.go b/commands/command_track.go index 6ea579e1..a8461912 100644 --- a/commands/command_track.go +++ b/commands/command_track.go @@ -78,40 +78,62 @@ ArgsLoop: } } + // Make sure any existing git tracked files have their timestamp updated + // so they will now show as modifed + // note this is relative to current dir which is how we write .gitattributes + // deliberately not done in parallel as a chan because we'll be marking modified + // + // NOTE: `git ls-files` does not do well with leading slashes. + // Since all `git-lfs track` calls are relative to the root of + // the repository, the leading slash is simply removed for its + // implicit counterpart. + gittracked, err := git.GetTrackedFiles(gitPath(pattern)) + if err != nil { + LoggedError(err, "Error getting git tracked files") + continue + } + now := time.Now() + + var blacklisted bool + for _, f := range gittracked { + if forbidden := blacklistItem(f); forbidden != "" { + Print("Pattern %s matches forbidden file %s. If you would like to track %s, modify .gitattributes manually.", pattern, f, f) + blacklisted = true + } + + } + if blacklisted { + continue + } + encodedArg := strings.Replace(pattern, " ", "[[:space:]]", -1) - _, err := attributesFile.WriteString(fmt.Sprintf("%s filter=lfs diff=lfs merge=lfs -text\n", encodedArg)) + _, err = attributesFile.WriteString(fmt.Sprintf("%s filter=lfs diff=lfs merge=lfs -text\n", encodedArg)) if err != nil { Print("Error adding path %s", pattern) continue } Print("Tracking %s", pattern) - // Make sure any existing git tracked files have their timestamp updated - // so they will now show as modifed - // note this is relative to current dir which is how we write .gitattributes - // deliberately not done in parallel as a chan because we'll be marking modified - gittracked, err := git.GetTrackedFiles(pattern) - if err != nil { - LoggedError(err, "Error getting git tracked files") - continue - } - now := time.Now() for _, f := range gittracked { - if forbidden := blacklistItem(f); forbidden != "" { - Print("Pattern %s matches forbidden file %s. If you would like to track %s, modify .gitattributes manually.", pattern, f, f) - continue - } - err := os.Chtimes(f, now, now) if err != nil { LoggedError(err, "Error marking %q modified", f) continue } } - } } +// gitPath returns the given string "p", stripped of its leading slash if it +// contains one. Otherwise, it returns "p" in its entirety. +func gitPath(p string) string { + if strings.HasPrefix(p, "/") { + return p[1:] + } + + return p +} + type mediaPath struct { Path string Source string