forked from git-lfs/git-lfs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathos_tools.go
More file actions
66 lines (58 loc) · 1.36 KB
/
os_tools.go
File metadata and controls
66 lines (58 loc) · 1.36 KB
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
package tools
import (
"bytes"
"fmt"
"os"
"os/exec"
"strings"
"github.com/git-lfs/git-lfs/subprocess"
"github.com/pkg/errors"
)
func Getwd() (dir string, err error) {
dir, err = os.Getwd()
if err != nil {
return
}
if isCygwin() {
dir, err = translateCygwinPath(dir)
if err != nil {
return "", errors.Wrap(err, "convert wd to cygwin")
}
}
return
}
func translateCygwinPath(path string) (string, error) {
cmd := subprocess.ExecCommand("cygpath", "-w", path)
// cygpath uses ISO-8850-1 as the default encoding if the locale is not
// set, resulting in breakage, since we want a UTF-8 path.
env := make([]string, 0, len(cmd.Env)+1)
for _, val := range cmd.Env {
if !strings.HasPrefix(val, "LC_ALL=") {
env = append(env, val)
}
}
cmd.Env = append(env, "LC_ALL=C.UTF-8")
buf := &bytes.Buffer{}
cmd.Stderr = buf
out, err := cmd.Output()
output := strings.TrimSpace(string(out))
if err != nil {
// If cygpath doesn't exist, that's okay: just return the paths
// as we got it.
if _, ok := err.(*exec.Error); ok {
return path, nil
}
return path, fmt.Errorf("failed to translate path from cygwin to windows: %s", buf.String())
}
return output, nil
}
func TranslateCygwinPath(path string) (string, error) {
if isCygwin() {
var err error
path, err = translateCygwinPath(path)
if err != nil {
return "", err
}
}
return path, nil
}