github.com/stffabi/git-lfs@v2.3.5-0.20180214015214-8eeaa8d88902+incompatible/commands/path_windows.go (about) 1 // +build windows 2 3 package commands 4 5 import ( 6 "path/filepath" 7 "regexp" 8 "strings" 9 "sync" 10 11 "github.com/git-lfs/git-lfs/subprocess" 12 ) 13 14 var ( 15 winBashPrefix string 16 winBashMu sync.Mutex 17 winBashRe *regexp.Regexp 18 ) 19 20 func osLineEnding() string { 21 return "\r\n" 22 } 23 24 // cleanRootPath replaces the windows root path prefix with a unix path prefix: 25 // "/". Git Bash (provided with Git For Windows) expands a path like "/foo" to 26 // the actual Windows directory, but with forward slashes. You can see this 27 // for yourself: 28 // 29 // $ git /foo 30 // git: 'C:/Program Files/Git/foo' is not a git command. See 'git --help'. 31 // 32 // You can check the path with `pwd -W`: 33 // 34 // $ cd / 35 // $ pwd 36 // / 37 // $ pwd -W 38 // c:/Program Files/Git 39 func cleanRootPath(pattern string) string { 40 winBashMu.Lock() 41 defer winBashMu.Unlock() 42 43 // check if path starts with windows drive letter 44 if !winPathHasDrive(pattern) { 45 return pattern 46 } 47 48 if len(winBashPrefix) < 1 { 49 // cmd.Path is something like C:\Program Files\Git\usr\bin\pwd.exe 50 cmd := subprocess.ExecCommand("pwd") 51 winBashPrefix = strings.Replace(filepath.Dir(filepath.Dir(filepath.Dir(cmd.Path))), `\`, "/", -1) + "/" 52 } 53 54 return strings.Replace(pattern, winBashPrefix, "/", 1) 55 } 56 57 func winPathHasDrive(pattern string) bool { 58 if winBashRe == nil { 59 winBashRe = regexp.MustCompile(`\A\w{1}:[/\/]`) 60 } 61 62 return winBashRe.MatchString(pattern) 63 }