diff --git a/cmd/gg/editor.go b/cmd/gg/editor.go index 8e7febe..878a5b3 100644 --- a/cmd/gg/editor.go +++ b/cmd/gg/editor.go @@ -61,7 +61,7 @@ func (e *editor) open(ctx context.Context, basename string, initial []byte) ([]b if err := ioutil.WriteFile(path, initial, 0600); err != nil { return nil, fmt.Errorf("open editor: %w", err) } - c, err := shellCommand(string(editor) + " " + escape.Shell(path)) + c, err := shellCommand(e.git.Exe(), string(editor)+" "+escape.Shell(path)) if err != nil { return nil, fmt.Errorf("open editor: %w", err) } diff --git a/cmd/gg/editor_unix.go b/cmd/gg/editor_unix.go index 00b82b0..ffac94d 100644 --- a/cmd/gg/editor_unix.go +++ b/cmd/gg/editor_unix.go @@ -20,6 +20,6 @@ package main import "os/exec" -func shellCommand(line string) (*exec.Cmd, error) { +func shellCommand(gitExe, line string) (*exec.Cmd, error) { return exec.Command("/bin/sh", "-c", line), nil } diff --git a/cmd/gg/editor_windows.go b/cmd/gg/editor_windows.go index c1edd84..3002638 100644 --- a/cmd/gg/editor_windows.go +++ b/cmd/gg/editor_windows.go @@ -18,19 +18,19 @@ package main import ( "os/exec" - - "golang.org/x/sys/windows" + "path/filepath" ) -func shellCommand(line string) (*exec.Cmd, error) { - cmd, err := exec.LookPath("cmd") - if err != nil { +func shellCommand(gitExe, line string) (*exec.Cmd, error) { + // Git for Windows comes with an MSYS2 bash emulation that Git uses to invoke + // shell commands. To preserve the semantics of the Git editor line, we use + // the same shell. + // + // In the default install location, git.exe lives at C:\Program Files\Git\cmd\git.exe. + // bash.exe lives at C:\Program Files\Git\bin\bash.exe and is not on the PATH. + bash := filepath.Join(gitExe, "..", "..", "bin", "bash.exe") + if _, err := exec.LookPath(bash); err != nil { return nil, err } - return &exec.Cmd{ - Path: cmd, - SysProcAttr: &windows.SysProcAttr{ - CmdLine: "cmd /C " + line, - }, - }, nil + return exec.Command(bash, "-c", line), nil } diff --git a/cmd/gg/main_test.go b/cmd/gg/main_test.go index da50662..5aaf59c 100644 --- a/cmd/gg/main_test.go +++ b/cmd/gg/main_test.go @@ -235,7 +235,7 @@ var ( func (env *testEnv) editorCmd(content []byte) (string, error) { cpPathOnce.Do(func() { if runtime.GOOS == "windows" { - cpPath, cpPathError = "copy", nil + cpPath, cpPathError = "cp", nil } else { cpPath, cpPathError = exec.LookPath("cp") }