Skip to content
Snippets Groups Projects
Unverified Commit ee40212e authored by Junegunn Choi's avatar Junegunn Choi
Browse files

Update FZF_DEFAULT_COMMAND

- Use bash for `set -o pipefail`
- Fall back to simpler find command when the original command failed

Related: #1061
parent 7f5f6efb
No related branches found
No related tags found
No related merge requests found
...@@ -55,7 +55,7 @@ var defaultCommand string ...@@ -55,7 +55,7 @@ var defaultCommand string
func init() { func init() {
if !util.IsWindows() { 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" { } 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-"` defaultCommand = `sh -c "command find -L . -mindepth 1 -path '*/\.*' -prune -o -type f -print -o -type l -print 2> /dev/null | cut -b3-"`
} else { } else {
......
...@@ -56,9 +56,11 @@ func (r *Reader) ReadSource() { ...@@ -56,9 +56,11 @@ func (r *Reader) ReadSource() {
if util.IsTty() { if util.IsTty() {
cmd := os.Getenv("FZF_DEFAULT_COMMAND") cmd := os.Getenv("FZF_DEFAULT_COMMAND")
if len(cmd) == 0 { 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 { } else {
success = r.readFromStdin() success = r.readFromStdin()
} }
...@@ -100,8 +102,8 @@ func (r *Reader) readFromStdin() bool { ...@@ -100,8 +102,8 @@ func (r *Reader) readFromStdin() bool {
return true return true
} }
func (r *Reader) readFromCommand(cmd string) bool { func (r *Reader) readFromCommand(shell string, cmd string) bool {
listCommand := util.ExecCommand(cmd) listCommand := util.ExecCommandWith(shell, cmd)
out, err := listCommand.StdoutPipe() out, err := listCommand.StdoutPipe()
if err != nil { if err != nil {
return false return false
......
...@@ -23,7 +23,7 @@ func TestReadFromCommand(t *testing.T) { ...@@ -23,7 +23,7 @@ func TestReadFromCommand(t *testing.T) {
} }
// Normal command // 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" { if len(strs) != 2 || strs[0] != "abc" || strs[1] != "def" {
t.Errorf("%s", strs) t.Errorf("%s", strs)
} }
...@@ -48,7 +48,7 @@ func TestReadFromCommand(t *testing.T) { ...@@ -48,7 +48,7 @@ func TestReadFromCommand(t *testing.T) {
reader.startEventPoller() reader.startEventPoller()
// Failing command // Failing command
reader.fin(reader.readFromCommand(`no-such-command`)) reader.fin(reader.readFromCommand("sh", `no-such-command`))
strs = []string{} strs = []string{}
if len(strs) > 0 { if len(strs) > 0 {
t.Errorf("%s", strs) t.Errorf("%s", strs)
......
...@@ -14,6 +14,11 @@ func ExecCommand(command string) *exec.Cmd { ...@@ -14,6 +14,11 @@ func ExecCommand(command string) *exec.Cmd {
if len(shell) == 0 { if len(shell) == 0 {
shell = "sh" 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) return exec.Command(shell, "-c", command)
} }
......
...@@ -10,8 +10,14 @@ import ( ...@@ -10,8 +10,14 @@ import (
"github.com/mattn/go-shellwords" "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 { 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) args, _ := shellwords.Parse(command)
allArgs := make([]string, len(args)+1) allArgs := make([]string, len(args)+1)
allArgs[0] = "/c" allArgs[0] = "/c"
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment