Debian Codegraph @

tools/os_tools.go

raw symbols SCIP
1 package tools
2
3 import (
4 "bytes"
5 "os"
6 "os/exec"
7 "strings"
8
9 "github.com/git-lfs/git-lfs/v3/subprocess"
10 "github.com/git-lfs/git-lfs/v3/tr"
11 "github.com/pkg/errors"
12 )
13
14 func Getwd() (dir string, err error) {
15 dir, err = os.Getwd()
16 if err != nil {
17 return
18 }
19
20 if isCygwin() {
21 dir, err = translateCygwinPath(dir)
22 if err != nil {
23 return "", errors.Wrap(err, tr.Tr.Get("error converting working directory to Cygwin"))
24 }
25 }
26
27 return
28 }
29
30 func translateCygwinPath(path string) (string, error) {
31 cmd, err := subprocess.ExecCommand("cygpath", "-w", path)
32 if err != nil {
33 // If cygpath doesn't exist, that's okay: just return the paths
34 // as we got it.
35 return path, nil
36 }
37 // cygpath uses ISO-8850-1 as the default encoding if the locale is not
38 // set, resulting in breakage, since we want a UTF-8 path.
39 env := make([]string, 0, len(cmd.Env)+1)
40 for _, val := range cmd.Env {
41 if !strings.HasPrefix(val, "LC_ALL=") {
42 env = append(env, val)
43 }
44 }
45 cmd.Env = append(env, "LC_ALL=C.UTF-8")
46 buf := &bytes.Buffer{}
47 cmd.Stderr = buf
48 out, err := cmd.Output()
49 output := strings.TrimSpace(string(out))
50 if err != nil {
51 // If cygpath doesn't exist, that's okay: just return the paths
52 // as we got it.
53 if _, ok := err.(*exec.Error); ok {
54 return path, nil
55 }
56 return path, errors.New(tr.Tr.Get("failed to translate path from Cygwin to Windows: %s", buf.String()))
57 }
58 return output, nil
59 }
60
61 func TranslateCygwinPath(path string) (string, error) {
62 if isCygwin() {
63 var err error
64
65 path, err = translateCygwinPath(path)
66 if err != nil {
67 return "", err
68 }
69 }
70 return path, nil
71 }