commands/path.go: introduce trimCurrentPrefix to remove "./"

There exist cases within Git LFS where we would like to perform a
"pseudo-clean" on a given filepath. It should behave similarly to a call
to filepath.Clean, but should only remove the beginning "./" (macOS,
*nix) or ".\" (Windows, etc).

This patch introduces trimCurrentPrefix, which removes such prefixes.
It will be used in subsequent patches to perform prefix-independent
comparison against arguments to "git lfs track" and "git lfs untrack"
and the contents of a repository's .gitattributes file.

Since we assume that the contents of ".gitattributes" are written on
multiple platforms, each with their own platform-specific prefixes,
trimCurrentPrefix is instructed to remove _all_ prefixes, not just the
current platforms.
This commit is contained in:
Taylor Blau 2018-04-05 10:46:12 -07:00
parent f3a14327dd
commit 1c8209bacf

@ -12,6 +12,18 @@ func gitLineEnding(git env) string {
}
}
const (
windowsPrefix = `.\`
nixPrefix = `./`
)
func trimCurrentPrefix(p string) string {
if strings.HasPrefix(p, windowsPrefix) {
return strings.TrimPrefix(p, windowsPrefix)
}
return strings.TrimPrefix(p, nixPrefix)
}
type env interface {
Get(string) (string, bool)
}