Skip to content

Commit

Permalink
Merge pull request #748 from bmeneguele/fix-editor
Browse files Browse the repository at this point in the history
git: fix edit command when using extra args
  • Loading branch information
bmeneg authored Oct 6, 2021
2 parents 6747135 + b73579a commit 7540b64
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
34 changes: 21 additions & 13 deletions internal/git/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func EditFile(filePrefix, message string) (string, error) {
dir = "/tmp"
}
filePath := filepath.Join(dir, fmt.Sprintf("%s_EDITMSG", filePrefix))
editorPath, err := editorPath()
editor, err := editor()
if err != nil {
return "", err
}
Expand All @@ -51,7 +51,7 @@ func EditFile(filePrefix, message string) (string, error) {
}
}

cmd := editorCMD(editorPath, filePath)
cmd := editorCMD(editor, filePath)
err = cmd.Run()
if err != nil {
fmt.Printf("ERROR(cmd): Saved file contents written to %s\n", filePath)
Expand All @@ -68,7 +68,7 @@ func EditFile(filePrefix, message string) (string, error) {
return removeComments(string(contents))
}

func editorPath() (string, error) {
func editor() (string, error) {
cmd := New("var", "GIT_EDITOR")
cmd.Stdout = nil
a, err := cmd.Output()
Expand All @@ -88,21 +88,29 @@ func editorPath() (string, error) {
return editor, nil
}

func editorCMD(editorPath, filePath string) *exec.Cmd {
parts := strings.Split(editorPath, " ")
func editorCMD(editor, filePath string) *exec.Cmd {
// make 'vi' the default editor to avoid empty editor configs
if editor == "" {
editor = "vi"
}

r := regexp.MustCompile("[nmg]?vi[m]?$")
args := make([]string, 0, 3)
if r.MatchString(editorPath) {
if r.MatchString(editor) {
args = append(args, "--cmd", "set ft=gitcommit tw=0 wrap lbr")
}
argparts := strings.Join(parts[1:], " ")
if len(argparts) == 0 {
args = append(args, filePath)
} else {
argparts = strings.Replace(argparts, "'", "\"", -1)
args = append(args, argparts, filePath)

parts := strings.Split(editor, " ")
name := parts[0]
if len(parts) > 0 {
for _, arg := range parts[1:] {
arg = strings.Replace(arg, "'", "\"", -1)
args = append(args, arg)
}
}
cmd := exec.Command(parts[0], args...)
args = append(args, filePath)

cmd := exec.Command(name, args...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
Expand Down
6 changes: 3 additions & 3 deletions internal/git/edit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,14 @@ func TestEditor(t *testing.T) {
}

var path string
t.Run("editorPath()", func(t *testing.T) {
t.Run("editor()", func(t *testing.T) {
var err error
path, err = editorPath()
path, err = editor()
if err != nil {
t.Fatal(err)
}

require.NotEmpty(t, editorPath)
require.NotEmpty(t, editor)
})
t.Run("Open Editor", func(t *testing.T) {
cmd := editorCMD(path, filePath)
Expand Down

0 comments on commit 7540b64

Please sign in to comment.