junegunn / fzf-git.sh

bash and zsh key bindings for Git objects, powered by fzf
612 stars 53 forks source link

Fix _fzf_git_files not showing anything in clean repo on macOS #9

Closed mathieu-lemay closed 2 years ago

mathieu-lemay commented 2 years ago

On both Linux and macOS, grep -f will match nothing when provided with an empty file. However, unlike on Linux, when combined with -v, grep on macOS will not match everything when provided with an empty file while the -v flag is active. This results in _fzf_git_files being empty when in a clean repository.

This fixes this behaviour by bypassing the grep command if there is nothing to exclude.

mathieu-lemay commented 2 years ago

This feels like an ugly hack, I'm open to improvements!

You can see the difference in behavior with this command: echo foo | grep -vf /dev/null; echo "exit code: $?"

# Linux
$ echo foo | grep -vf /dev/null; echo "exit code: $?"
foo
exit code: 0

# macOS
$ echo foo | grep -vf /dev/null; echo "exit code: $?"
exit code: 1
junegunn commented 2 years ago

Is this also because of an old macOS version?

$ sw_vers
ProductName:    macOS
ProductVersion: 12.4
BuildVersion:   21F79

$ echo foo | grep -vf /dev/null; echo "exit code: $?"
foo
exit code: 0
mathieu-lemay commented 2 years ago

I wasn't sure but if it works for you in macOS 12.4, then it must be.

I have the issue in macOS 10.15.

junegunn commented 2 years ago

I think we should just tell the users running old macOS to install GNU grep (brew install grep) instead of special-casing each invocation of it, then the script can pick up ggrep if it's found.

junegunn commented 2 years ago

Anyway, I've realized the current code needs fixing.

# 10 is not printed because if matches '1'
seq 10 | grep -vFf <(seq 5)

Let me see what I can do.

junegunn commented 2 years ago

So we need -x as well.

seq 10 | grep -vxFf <(seq 5)
 -x, --line-regexp
        Only input lines selected against an entire fixed string or regular expression are considered to be matching lines.