github.com/openshift/source-to-image@v1.4.1-0.20240516041539-bf52fc02204e/pkg/util/cygpath/cygpath.go (about)

     1  package cygpath
     2  
     3  import (
     4  	"os/exec"
     5  	"path/filepath"
     6  	"runtime"
     7  	"strings"
     8  )
     9  
    10  // UsingCygwinGit indicates whether we believe the host's git utility is from
    11  // Cygwin (expects Windows paths as /cygdrive/c/dir/file) or not (expects paths
    12  // in host-native format).
    13  var UsingCygwinGit = isUsingCygwinGit()
    14  
    15  func isUsingCygwinGit() bool {
    16  	if runtime.GOOS == "windows" {
    17  		// If we find the cygpath utility (which translates between UNIX-style and
    18  		// Windows-style paths) in the same directory as git, assume we're using
    19  		// Cygwin.
    20  		cygpath, err := exec.LookPath("cygpath")
    21  		if err != nil {
    22  			return false
    23  		}
    24  		var git string
    25  		git, err = exec.LookPath("git")
    26  		if err == nil && filepath.Dir(cygpath) == filepath.Dir(git) {
    27  			return true
    28  		}
    29  	}
    30  	return false
    31  }
    32  
    33  // ToSlashCygwin converts a path to a format suitable for sending to a Cygwin
    34  // binary - `/dir/file` on UNIX-style systems; `c:\dir\file` on Windows without
    35  // Cygwin; `/cygdrive/c/dir/file` on Windows with Cygwin.
    36  func ToSlashCygwin(path string) (string, error) {
    37  	cmd := exec.Command("cygpath", path)
    38  	out, err := cmd.Output()
    39  	return strings.TrimRight(string(out), "\n"), err
    40  }