Skip to content

Commit

Permalink
Move trimCurrentPrefix to tools
Browse files Browse the repository at this point in the history
Right now, we have a function which can trim the relative path prefix,
but that is only available in the `commands` package.  In the future,
we'll want to use it elsewhere as well, so let's move it somewhere more
accessible.
  • Loading branch information
bk2204 committed Mar 20, 2023
1 parent 1bbf8ba commit 4049292
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 21 deletions.
2 changes: 1 addition & 1 deletion commands/command_track.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func trackCommand(cmd *cobra.Command, args []string) {
var writeablePatterns []string
ArgsLoop:
for _, unsanitizedPattern := range args {
pattern := trimCurrentPrefix(cleanRootPath(unsanitizedPattern))
pattern := tools.TrimCurrentPrefix(cleanRootPath(unsanitizedPattern))

// Generate the new / changed attrib line for merging
var encodedArg string
Expand Down
5 changes: 3 additions & 2 deletions commands/command_untrack.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"strings"

"github.com/git-lfs/git-lfs/v3/tools"
"github.com/git-lfs/git-lfs/v3/tr"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -57,9 +58,9 @@ func untrackCommand(cmd *cobra.Command, args []string) {
}

func removePath(path string, args []string) bool {
withoutCurrentDir := trimCurrentPrefix(path)
withoutCurrentDir := tools.TrimCurrentPrefix(path)
for _, t := range args {
if withoutCurrentDir == escapeAttrPattern(trimCurrentPrefix(t)) {
if withoutCurrentDir == escapeAttrPattern(tools.TrimCurrentPrefix(t)) {
return true
}
}
Expand Down
18 changes: 0 additions & 18 deletions commands/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,6 @@ func gitLineEnding(git env) string {
}
}

const (
windowsPrefix = `.\`
nixPrefix = `./`
)

// trimCurrentPrefix removes a leading prefix of "./" or ".\" (referring to the
// current directory in a platform independent manner).
//
// It is useful for callers such as "git lfs track" and "git lfs untrack", that
// wish to compare filepaths and/or attributes patterns without cleaning across
// multiple platforms.
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)
}
18 changes: 18 additions & 0 deletions tools/filetools.go
Original file line number Diff line number Diff line change
Expand Up @@ -503,3 +503,21 @@ func CanonicalizePath(path string, missingOk bool) (string, error) {
}
return "", nil
}

const (
windowsPrefix = `.\`
nixPrefix = `./`
)

// TrimCurrentPrefix removes a leading prefix of "./" or ".\" (referring to the
// current directory in a platform independent manner).
//
// It is useful for callers such as "git lfs track" and "git lfs untrack", that
// wish to compare filepaths and/or attributes patterns without cleaning across
// multiple platforms.
func TrimCurrentPrefix(p string) string {
if strings.HasPrefix(p, windowsPrefix) {
return strings.TrimPrefix(p, windowsPrefix)
}
return strings.TrimPrefix(p, nixPrefix)
}

0 comments on commit 4049292

Please sign in to comment.