Skip to content

Commit

Permalink
cmd/gg: use Git Bash for Windows editor shell command
Browse files Browse the repository at this point in the history
Updates #47
  • Loading branch information
zombiezen committed Sep 7, 2020
1 parent 8a53c57 commit ae48dc1
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 14 deletions.
2 changes: 1 addition & 1 deletion cmd/gg/editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/gg/editor_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
22 changes: 11 additions & 11 deletions cmd/gg/editor_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion cmd/gg/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down

0 comments on commit ae48dc1

Please sign in to comment.