Skip to content

Commit

Permalink
Update FZF_DEFAULT_COMMAND
Browse files Browse the repository at this point in the history
- Use bash for `set -o pipefail`
- Fall back to simpler find command when the original command failed

Related: #1061
  • Loading branch information
junegunn committed Sep 28, 2017
1 parent 7f5f6ef commit ee40212
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ var defaultCommand string

func init() {
if !util.IsWindows() {
defaultCommand = `command find -L . -mindepth 1 \( -path '*/\.*' -o -fstype 'sysfs' -o -fstype 'devfs' -o -fstype 'devtmpfs' -o -fstype 'proc' \) -prune -o -type f -print -o -type l -print 2> /dev/null | cut -b3-`
defaultCommand = `set -o pipefail; (command find -L . -mindepth 1 \( -path '*/\.*' -o -fstype 'sysfs' -o -fstype 'devfs' -o -fstype 'devtmpfs' -o -fstype 'proc' \) -prune -o -type f -print -o -type l -print || command find -L . -mindepth 1 -path '*/\.*' -prune -o -type f -print -o -type l -print) 2> /dev/null | cut -b3-`
} else if os.Getenv("TERM") == "cygwin" {
defaultCommand = `sh -c "command find -L . -mindepth 1 -path '*/\.*' -prune -o -type f -print -o -type l -print 2> /dev/null | cut -b3-"`
} else {
Expand Down
10 changes: 6 additions & 4 deletions src/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,11 @@ func (r *Reader) ReadSource() {
if util.IsTty() {
cmd := os.Getenv("FZF_DEFAULT_COMMAND")
if len(cmd) == 0 {
cmd = defaultCommand
// The default command for *nix requires bash
success = r.readFromCommand("bash", defaultCommand)
} else {
success = r.readFromCommand("sh", cmd)
}
success = r.readFromCommand(cmd)
} else {
success = r.readFromStdin()
}
Expand Down Expand Up @@ -100,8 +102,8 @@ func (r *Reader) readFromStdin() bool {
return true
}

func (r *Reader) readFromCommand(cmd string) bool {
listCommand := util.ExecCommand(cmd)
func (r *Reader) readFromCommand(shell string, cmd string) bool {
listCommand := util.ExecCommandWith(shell, cmd)
out, err := listCommand.StdoutPipe()
if err != nil {
return false
Expand Down
4 changes: 2 additions & 2 deletions src/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestReadFromCommand(t *testing.T) {
}

// Normal command
reader.fin(reader.readFromCommand(`echo abc && echo def`))
reader.fin(reader.readFromCommand("sh", `echo abc && echo def`))
if len(strs) != 2 || strs[0] != "abc" || strs[1] != "def" {
t.Errorf("%s", strs)
}
Expand All @@ -48,7 +48,7 @@ func TestReadFromCommand(t *testing.T) {
reader.startEventPoller()

// Failing command
reader.fin(reader.readFromCommand(`no-such-command`))
reader.fin(reader.readFromCommand("sh", `no-such-command`))
strs = []string{}
if len(strs) > 0 {
t.Errorf("%s", strs)
Expand Down
5 changes: 5 additions & 0 deletions src/util/util_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ func ExecCommand(command string) *exec.Cmd {
if len(shell) == 0 {
shell = "sh"
}
return ExecCommandWith(shell, command)
}

// ExecCommandWith executes the given command with the specified shell
func ExecCommandWith(shell string, command string) *exec.Cmd {
return exec.Command(shell, "-c", command)
}

Expand Down
8 changes: 7 additions & 1 deletion src/util/util_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,14 @@ import (
"github.com/mattn/go-shellwords"
)

// ExecCommand executes the given command with $SHELL
// ExecCommand executes the given command with cmd
func ExecCommand(command string) *exec.Cmd {
return ExecCommandWith("cmd", command)
}

// ExecCommandWith executes the given command with cmd. _shell parameter is
// ignored on Windows.
func ExecCommandWith(_shell string, command string) *exec.Cmd {
args, _ := shellwords.Parse(command)
allArgs := make([]string, len(args)+1)
allArgs[0] = "/c"
Expand Down

0 comments on commit ee40212

Please sign in to comment.