git-lfs/tools/os_tools.go
Chris Darroch dd8e306e31 all: update go.mod module path with explicit v2
When our go.mod file was introduced in commit
114e85c2002091eb415040923d872f8e4a4bc636 in PR #3208, the module
path chosen did not include a trailing /v2 component.  However,
the Go modules specification now advises that module paths must
have a "major version suffix" which matches the release version.

We therefore add a /v2 suffix to our module path and all its
instances in import paths.

See also https://golang.org/ref/mod#major-version-suffixes for
details regarding the Go module system's major version suffix rule.
2021-08-09 23:18:38 -07:00

67 lines
1.4 KiB
Go

package tools
import (
"bytes"
"fmt"
"os"
"os/exec"
"strings"
"github.com/git-lfs/git-lfs/v2/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
}