Debian Codegraph @

commands/path_windows.go

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