github.com/pdmccormick/importable-docker-buildx@v0.0.0-20240426161518-e47091289030/util/osutil/path_unix.go (about)

     1  //go:build !windows
     2  // +build !windows
     3  
     4  package osutil
     5  
     6  import (
     7  	"os"
     8  	"path/filepath"
     9  	"regexp"
    10  	"strings"
    11  )
    12  
    13  // GetLongPathName is a no-op on non-Windows platforms.
    14  func GetLongPathName(path string) (string, error) {
    15  	return path, nil
    16  }
    17  
    18  var windowsPathRegex = regexp.MustCompile(`^[A-Za-z]:[\\/].*$`)
    19  
    20  func SanitizePath(path string) string {
    21  	// If we're running in WSL, we need to convert Windows paths to Unix paths.
    22  	// This is because the git binary can be invoked through `git.exe` and
    23  	// therefore returns Windows paths.
    24  	if os.Getenv("WSL_DISTRO_NAME") != "" && windowsPathRegex.MatchString(path) {
    25  		unixPath := strings.ReplaceAll(path, "\\", "/")
    26  		drive := strings.ToLower(string(unixPath[0]))
    27  		rest := filepath.Clean(unixPath[3:])
    28  		return filepath.Join("/mnt", drive, rest)
    29  	}
    30  	return filepath.Clean(path)
    31  }