Skip to content

Commit 93537d4

Browse files
committed
Merge branch 'git-empty-args'
2 parents 6c63f56 + 3df5526 commit 93537d4

File tree

5 files changed

+29
-7
lines changed

5 files changed

+29
-7
lines changed

cmd/cmd.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,17 @@ type Cmd struct {
2121
}
2222

2323
func (cmd Cmd) String() string {
24-
return fmt.Sprintf("%s %s", cmd.Name, strings.Join(cmd.Args, " "))
24+
args := make([]string, len(cmd.Args))
25+
for i, a := range cmd.Args {
26+
if strings.ContainsRune(a, '"') {
27+
args[i] = fmt.Sprintf(`'%s'`, a)
28+
} else if a == "" || strings.ContainsRune(a, '\'') || strings.ContainsRune(a, ' ') {
29+
args[i] = fmt.Sprintf(`"%s"`, a)
30+
} else {
31+
args[i] = a
32+
}
33+
}
34+
return fmt.Sprintf("%s %s", cmd.Name, strings.Join(args, " "))
2535
}
2636

2737
// WithArg returns the current argument
@@ -138,7 +148,7 @@ func NewWithArray(cmd []string) *Cmd {
138148

139149
func verboseLog(cmd *Cmd) {
140150
if os.Getenv("HUB_VERBOSE") != "" {
141-
msg := fmt.Sprintf("$ %s %s", cmd.Name, strings.Join(cmd.Args, " "))
151+
msg := fmt.Sprintf("$ %s", cmd.String())
142152
if ui.IsTerminal(os.Stderr) {
143153
msg = fmt.Sprintf("\033[35m%s\033[0m", msg)
144154
}

cmd/cmd_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,11 @@ func TestWithArg(t *testing.T) {
1818
assert.Equal(t, "git", execCmd.Name)
1919
assert.Equal(t, 4, len(execCmd.Args))
2020
}
21+
22+
func Test_String(t *testing.T) {
23+
c := Cmd{
24+
Name: "echo",
25+
Args: []string{"hi", "hello world", "don't", `"fake news"`},
26+
}
27+
assert.Equal(t, `echo hi "hello world" "don't" '"fake news"'`, c.String())
28+
}

commands/args.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,7 @@ func (a *Args) ToCmd() *cmd.Cmd {
9292
}
9393

9494
for _, arg := range a.Params {
95-
if arg != "" {
96-
c.WithArg(arg)
97-
}
95+
c.WithArg(arg)
9896
}
9997

10098
return c

commands/args_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,12 @@ func TestArgs_GlobalFlags_Replaced(t *testing.T) {
120120
assert.Equal(t, []string{"-a", "http://example.com"}, cmd.Args)
121121
}
122122

123+
func TestArgs_ToCmd(t *testing.T) {
124+
args := NewArgs([]string{"a", "", "b", ""})
125+
cmd := args.ToCmd()
126+
assert.Equal(t, []string{"a", "", "b", ""}, cmd.Args)
127+
}
128+
123129
func TestArgs_GlobalFlags_BeforeAfterChain(t *testing.T) {
124130
args := NewArgs([]string{"-c", "key=value", "-C", "dir", "status"})
125131
args.Before("git", "remote", "add")

commands/init_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,13 @@ func TestInitInAnotherDir(t *testing.T) {
5757

5858
commands := args.Commands()
5959
assert.Equal(t, 2, len(commands))
60-
assert.Equal(t, "git init --template mytpl --shared=umask my project", commands[0].String())
60+
assert.Equal(t, "git init --template mytpl --shared=umask \"my project\"", commands[0].String())
6161

6262
currentDir, err := os.Getwd()
6363
assert.Equal(t, nil, err)
6464

6565
expected := fmt.Sprintf(
66-
"git --git-dir %s remote add origin [email protected]:jingweno/%s.git",
66+
"git --git-dir \"%s\" remote add origin [email protected]:jingweno/%s.git",
6767
filepath.Join(currentDir, "my project", ".git"),
6868
"my-project",
6969
)

0 commit comments

Comments
 (0)