Skip to content

Commit

Permalink
git: fix editor command split rule
Browse files Browse the repository at this point in the history
Editor commands with quoting, like:

vim -c 'set tw=74' -c 'set noai' -c 'set wrap'

were being splitted in the wrong way: only spaces were being considered and
each part was placed in a different slot of the command arg array:

[vim, -c, 'set, tw=74', -c, 'set, noai', -c, 'set, wrap']

With that, lab ended up calling `vim` with strings like `'set` and `tw=74'`
as separated args and, thus, vim was creating files with these names instead
of using them as real arguments for the `-c` flag.

This commit fixes it by using Google's shlex module, which respects
shell-style rules for quoting and commenting in the command line. The
previous example is now splitted as follows:

[vim, -c, 'set tw=74', -c, 'set noai', -c, 'set wrap']

So the quoted values are kept together.

Fixes: b73579a ("git: fix edit command when using extra args")
Signed-off-by: Bruno Meneguele <bmeneg@redhat.com>
  • Loading branch information
bmeneg committed Oct 19, 2021
1 parent 5ee7e23 commit f838ac3
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 6 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ require (
github.com/gdamore/tcell/v2 v2.4.1-0.20210905002822-f057f0a857a1
github.com/go-git/go-git/v5 v5.4.2
github.com/google/go-querystring v1.1.0 // indirect
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-retryablehttp v0.7.0 // indirect
github.com/jaytaylor/html2text v0.0.0-20200412013138-3577fbdbcff7
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,8 @@ github.com/google/pprof v0.0.0-20210601050228-01bbb1931b22/go.mod h1:kpwsk12EmLe
github.com/google/pprof v0.0.0-20210609004039-a478d1d731e9/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4=
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ=
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
Expand Down
20 changes: 16 additions & 4 deletions internal/git/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"path/filepath"
"regexp"
"strings"

"github.com/google/shlex"
)

// Edit opens a file in the users editor and returns the title and body.
Expand Down Expand Up @@ -51,7 +53,12 @@ func EditFile(filePrefix, message string) (string, error) {
}
}

cmd := editorCMD(editor, filePath)
cmd, err := editorCMD(editor, filePath)
if err != nil {
fmt.Printf("ERROR(editorCMD): failed to get editor command \"%s\"\n", editor)
return "", err
}

err = cmd.Run()
if err != nil {
fmt.Printf("ERROR(cmd): Saved file contents written to %s\n", filePath)
Expand Down Expand Up @@ -88,7 +95,7 @@ func editor() (string, error) {
return editor, nil
}

func editorCMD(editor, filePath string) *exec.Cmd {
func editorCMD(editor, filePath string) (*exec.Cmd, error) {
// make 'vi' the default editor to avoid empty editor configs
if editor == "" {
editor = "vi"
Expand All @@ -100,7 +107,12 @@ func editorCMD(editor, filePath string) *exec.Cmd {
args = append(args, "--cmd", "set ft=gitcommit tw=0 wrap lbr")
}

parts := strings.Split(editor, " ")
// Split editor command using shell rules for quoting and commenting
parts, err := shlex.Split(editor)
if err != nil {
return nil, err
}

name := parts[0]
if len(parts) > 0 {
for _, arg := range parts[1:] {
Expand All @@ -114,7 +126,7 @@ func editorCMD(editor, filePath string) *exec.Cmd {
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd
return cmd, nil
}

func removeComments(message string) (string, error) {
Expand Down
7 changes: 5 additions & 2 deletions internal/git/edit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,11 @@ func TestEditor(t *testing.T) {
require.NotEmpty(t, editor)
})
t.Run("Open Editor", func(t *testing.T) {
cmd := editorCMD(path, filePath)
err := cmd.Start()
cmd, err := editorCMD(path, filePath)
if err != nil {
t.Fatal(err)
}
err = cmd.Start()
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit f838ac3

Please sign in to comment.