Skip to content

Instantly share code, notes, and snippets.

@thekid
Created August 7, 2018 21:01
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save thekid/83b8ac613dba829d0c6cacdc33e50cdb to your computer and use it in GitHub Desktop.
Save thekid/83b8ac613dba829d0c6cacdc33e50cdb to your computer and use it in GitHub Desktop.
Fixing "Invalid path" for go + cygwin git
package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"syscall"
)
func resolve(executable, self string) (string, error) {
path := os.Getenv("PATH")
exts := strings.Split(os.Getenv("PATHEXT"), ";")
for _, dir := range filepath.SplitList(path) {
if dir != self {
path := filepath.Join(dir, executable)
for _, ext := range exts {
d, err := os.Stat(path + ext)
if err == nil && !d.IsDir() {
return path + ext, nil
}
}
}
}
return "", fmt.Errorf("Could not find %s in path", executable)
}
func main() {
exe, err := resolve("git", filepath.Dir(os.Args[0]))
if err != nil {
fmt.Println(err.Error())
os.Exit(127)
}
// Translate arguments
args := make([]string, len(os.Args)-1)
for i, arg := range os.Args[1:] {
if strings.HasPrefix(strings.ToLower(arg), "c:") {
args[i] = strings.Replace("/cygdrive/c"+arg[2:], "\\", "/", -1)
} else {
args[i] = arg
}
}
// Setup command to pipe through
cmd := exec.Command(exe, args...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
// Run command
if err := cmd.Start(); err != nil {
fmt.Printf("Could not start %v: %v", cmd, err)
os.Exit(127)
}
if err := cmd.Wait(); err != nil {
if exiterr, ok := err.(*exec.ExitError); ok {
if status, ok := exiterr.Sys().(syscall.WaitStatus); ok {
os.Exit(status.ExitStatus())
}
}
}
os.Exit(0)
}

When running go get from a Cygwin shell on Windows using git from Cygwin, you will see:

$ go get github.com/golang/example/hello
# cd .; git clone https://github.com/golang/example C:\tools\cygwin\home\friebe\devel\go\src\github.com\golang\example
Klone nach 'C:\tools\cygwin\home\friebe\devel\go\src\github.com\golang\example' ...
fatal: Invalid path '/home/friebe/C:\tools\cygwin\home\friebe\devel\go\src\github.com\golang\example': No such file or directory
package github.com/golang/example/hello: exit status 128

To fix this, place this file in ~/bin, run go build -o git.exe git.go and add $HOME/bin to the front of your $PATH environment variable.

@thekid
Copy link
Author

thekid commented Sep 7, 2018

@senkrad76
Copy link

senkrad76 commented Oct 10, 2018

Thank you... however when running with a -u flag I'm still getting errors:
$: go get -u github.com/AlekSi/gocoverutil

# cd c:\Users\me\go\src\github.com\AlekSi\gocoverutil; git pull --ff-only
exec: "c:\\Users\\me\\go\\src\\github.com\\AlekSi\\gocoverutil\\git.exe": file does not existpackage github.com/AlekSi/gocoverutil: exec: "c:\\Users\\me\\go\\src\\github.com\\AlekSi\\gocoverutil\\git.exe": file does not exist
$: go get github.com/AlekSi/gocoverutil
$:

Wonder if you had suggestions.

@christianboyle
Copy link

You are a legend for releasing this, spent hours trying to get this working without any luck until finding your program. Many thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment