File: path_windows.go

package info (click to toggle)
git-lfs 3.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,808 kB
  • sloc: sh: 21,256; makefile: 507; ruby: 417
file content (67 lines) | stat: -rw-r--r-- 1,450 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//go:build windows
// +build windows

package commands

import (
	"path/filepath"
	"regexp"
	"strings"
	"sync"

	"github.com/git-lfs/git-lfs/v3/subprocess"
)

var (
	winBashPrefix string
	winBashMu     sync.Mutex
	winBashRe     *regexp.Regexp
)

func osLineEnding() string {
	return "\r\n"
}

// 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, err := subprocess.ExecCommand("pwd")
		if err != nil {
			return pattern
		}
		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)
}