2016-10-07 22:43:18 +00:00
|
|
|
// +build windows
|
|
|
|
|
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"path/filepath"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
|
2016-11-15 17:01:18 +00:00
|
|
|
"github.com/git-lfs/git-lfs/subprocess"
|
2016-10-07 22:43:18 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
winBashPrefix string
|
|
|
|
winBashMu sync.Mutex
|
|
|
|
winBashRe *regexp.Regexp
|
|
|
|
)
|
|
|
|
|
2017-02-03 22:26:32 +00:00
|
|
|
func lineEnding() string {
|
2017-02-04 00:18:42 +00:00
|
|
|
return "\r\n"
|
2017-02-03 22:26:32 +00:00
|
|
|
}
|
|
|
|
|
2016-10-07 22:43:18 +00:00
|
|
|
// cleanRootPath replaces the windows root path prefix with a unix path prefix:
|
|
|
|
// "/". Git Bash (provided with Git For Windows) expands a path like "/foo" to
|
|
|
|
// the actual Windows directory, but with forward slashes. You can see this
|
|
|
|
// for yourself:
|
|
|
|
//
|
|
|
|
// $ git /foo
|
|
|
|
// git: 'C:/Program Files/Git/foo' is not a git command. See 'git --help'.
|
|
|
|
//
|
|
|
|
// You can check the path with `pwd -W`:
|
|
|
|
//
|
|
|
|
// $ cd /
|
|
|
|
// $ pwd
|
|
|
|
// /
|
|
|
|
// $ pwd -W
|
|
|
|
// c:/Program Files/Git
|
|
|
|
func cleanRootPath(pattern string) string {
|
|
|
|
winBashMu.Lock()
|
|
|
|
defer winBashMu.Unlock()
|
|
|
|
|
|
|
|
// check if path starts with windows drive letter
|
|
|
|
if !winPathHasDrive(pattern) {
|
|
|
|
return pattern
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(winBashPrefix) < 1 {
|
|
|
|
// cmd.Path is something like C:\Program Files\Git\usr\bin\pwd.exe
|
|
|
|
cmd := subprocess.ExecCommand("pwd")
|
|
|
|
winBashPrefix = strings.Replace(filepath.Dir(filepath.Dir(filepath.Dir(cmd.Path))), `\`, "/", -1) + "/"
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.Replace(pattern, winBashPrefix, "/", 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func winPathHasDrive(pattern string) bool {
|
|
|
|
if winBashRe == nil {
|
|
|
|
winBashRe = regexp.MustCompile(`\A\w{1}:[/\/]`)
|
|
|
|
}
|
|
|
|
|
|
|
|
return winBashRe.MatchString(pattern)
|
|
|
|
}
|