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

     1  //go:build !windows
     2  // +build !windows
     3  
     4  package gitutil
     5  
     6  import (
     7  	"os"
     8  	"os/exec"
     9  	"path/filepath"
    10  
    11  	"github.com/moby/sys/mountinfo"
    12  )
    13  
    14  func gitPath(wd string) (string, error) {
    15  	// On WSL2 we need to check if the current working directory is mounted on
    16  	// a Windows drive and if so, we need to use the Windows git executable.
    17  	if os.Getenv("WSL_DISTRO_NAME") != "" && wd != "" {
    18  		// ensure any symlinks are resolved
    19  		wdPath, err := filepath.EvalSymlinks(wd)
    20  		if err != nil {
    21  			return "", err
    22  		}
    23  		mi, err := mountinfo.GetMounts(mountinfo.ParentsFilter(wdPath))
    24  		if err != nil {
    25  			return "", err
    26  		}
    27  		// find the longest mount point
    28  		var idx, maxlen int
    29  		for i := range mi {
    30  			if len(mi[i].Mountpoint) > maxlen {
    31  				maxlen = len(mi[i].Mountpoint)
    32  				idx = i
    33  			}
    34  		}
    35  		if mi[idx].FSType == "9p" {
    36  			if p, err := exec.LookPath("git.exe"); err == nil {
    37  				return p, nil
    38  			}
    39  		}
    40  	}
    41  	return exec.LookPath("git")
    42  }