Replace "default find command" with built-in directory traversal
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Junegunn Choi · committed by Junegunn Choi · parent c65d11b · Browse files · 9 files·+57 added−82 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
9 files+57 added−82 removed
- other:+57 added−82 removed
Provenance
- Commit
208e556- Tree
d5b3bcb- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
c65d11b
9 files changed+57 added−82 removed
modified:doc/fzf.txt+10 added-21 removed
1-fzf.txt fzf Last change: January 1 20241+fzf.txt fzf Last change: February 15 20242 FZF - TABLE OF CONTENTS *fzf* *fzf-toc*3 ==============================================================================4 5 FZF Vim integration |fzf-vim-integration|257 unmodified lines263 >264 call fzf#run({'sink': 'e'})265 <266 We haven't specified the `source`, so this is equivalent to starting fzf on267-command line without standard input pipe; fzf will use find command (or268-`$FZF_DEFAULT_COMMAND` if defined) to list the files under the current269-directory. When you select one, it will open it with the sink, `:e` command.270-If you want to open it in a new tab, you can pass `:tabedit` command instead271-as the sink.267+command line without standard input pipe; fzf will traverse the file system268+under the current directory to get the list of files. (If269+`$FZF_DEFAULT_COMMAND` is set, fzf will use the output of the command270+instead.) When you select one, it will open it with the sink, `:e` command. If271+you want to open it in a new tab, you can pass `:tabedit` command instead as272+the sink.273 >274 call fzf#run({'sink': 'tabedit'})275 <275-Instead of using the default find command, you can use any shell command as276-the source. The following example will list the files managed by git. It's277-equivalent to running `git ls-files | fzf` on shell.276+You can use any shell command as the source to generate the list. The277+following example will list the files managed by git. It's equivalent to278+running `git ls-files | fzf` on shell.279 >280 call fzf#run({'source': 'git ls-files', 'sink': 'e'})281 <282 fzf options can be specified as `options` entry in spec dictionary.134 unmodified lines417 418 < fzf inside terminal buffer >________________________________________________~419 *fzf-inside-terminal-buffer*420 420-The latest versions of Vim and Neovim include builtin terminal emulator421-(`:terminal`) and fzf will start in a terminal buffer in the following cases:421 423- - On Neovim424- - On GVim425- - On Terminal Vim with a non-default layout426- - `call fzf#run({'left': '30%'})` or `let g:fzf_layout = {'left': '30%'}`427-422 On the latest versions of Vim and Neovim, fzf will start in a terminal buffer.423 If you find the default ANSI colors to be different, consider configuring the424 colors using `g:terminal_ansi_colors` in regular Vim or `g:terminal_color_x`425 in Neovim.426 433- *g:terminal_color_15* *g:terminal_color_14* *g:terminal_color_13*434-*g:terminal_color_12* *g:terminal_color_11* *g:terminal_color_10* *g:terminal_color_9*435- *g:terminal_color_8* *g:terminal_color_7* *g:terminal_color_6* *g:terminal_color_5*436- *g:terminal_color_4* *g:terminal_color_3* *g:terminal_color_2* *g:terminal_color_1*437- *g:terminal_color_0*427 >428 " Terminal colors for seoul256 color scheme429 if has('nvim')430 let g:terminal_color_0 = '#4e4e4e'77 unmodified linesmodified:src/constants.go-11 removed
1 package fzf2 3 import (4 "math"5- "os"5 "time"6 7 "github.com/junegunn/fzf/src/util"8 )43 unmodified lines52 // Jump labels53 defaultJumpLabels string = "asdfghjklqwertyuiopzxcvbnm1234567890ASDFGHJKLQWERTYUIOPZXCVBNM`~;:,<.>/?'\"!@#$%^&*()[{]}-_=+"54 )55 57-var defaultCommand string58-59-func init() {60- if !util.IsWindows() {61- 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 2> /dev/null | cut -b3-`62- } else if os.Getenv("TERM") == "cygwin" {63- defaultCommand = `sh -c "command find -L . -mindepth 1 -path '*/.*' -prune -o -type f -print -o -type l -print 2> /dev/null | cut -b3-"`64- }65-}66-56 // fzf events57 const (58 EvtReadNew util.EventType = iota59 EvtReadFin15 unmodified linesmodified:src/reader.go+11 added-27 removed
4 unmodified lines5 "context"6 "io"7 "os"8 "os/exec"9- "path"9 "path/filepath"10 "sync"11 "sync/atomic"12 "time"13 14+ "github.com/charlievieth/fastwalk"15 "github.com/junegunn/fzf/src/util"16- "github.com/saracen/walker"16 )17 18 // Reader reads from command or standard input19 type Reader struct {55 unmodified lines75 }76 77 func (r *Reader) terminate() {78 r.mutex.Lock()80- defer func() { r.mutex.Unlock() }()81-79 r.killed = true80 if r.exec != nil && r.exec.Process != nil {81 util.KillCommand(r.exec)85- } else if defaultCommand != "" {82+ } else {83 os.Stdin.Close()84 }85+ r.mutex.Unlock()86 }87 88 func (r *Reader) restart(command string) {89 r.event = int32(EvtReady)6 unmodified lines96 func (r *Reader) ReadSource() {97 r.startEventPoller()98 var success bool99 if util.IsTty() {102- // The default command for *nix requires a shell that supports "pipefail"103- // https://unix.stackexchange.com/a/654932/62171104- shell := "bash"105- currentShell := os.Getenv("SHELL")106- currentShellName := path.Base(currentShell)107- for _, shellName := range []string{"bash", "zsh", "ksh", "ash", "hush", "mksh", "yash"} {108- if currentShellName == shellName {109- shell = currentShell110- break111- }112- }100 cmd := os.Getenv("FZF_DEFAULT_COMMAND")101 if len(cmd) == 0 {115- if defaultCommand != "" {116- success = r.readFromCommand(&shell, defaultCommand)117- } else {118- success = r.readFiles()102+ success = r.readFiles()119- }103 } else {104 success = r.readFromCommand(nil, cmd)105 }106 } else {38 unmodified lines145 }146 147 func (r *Reader) readFiles() bool {148 r.killed = false149+ conf := fastwalk.Config{Follow: true}166- fn := func(path string, mode os.FileInfo) error {150+ fn := func(path string, de os.DirEntry, err error) error {151+ if err != nil {152+ return nil153+ }154 path = filepath.Clean(path)155 if path != "." {169- isDir := mode.Mode().IsDir()156+ isDir := de.IsDir()157 if isDir && filepath.Base(path)[0] == '.' {158 return filepath.SkipDir159 }160 if !isDir && r.pusher([]byte(path)) {6 unmodified lines167 return context.Canceled168 }169 return nil170 }184- cb := walker.WithErrorCallback(func(pathname string, err error) error {185- return nil186- })187- return walker.Walk(".", fn, cb) == nil171+ return fastwalk.Walk(&conf, ".", fn) == nil172 }173 174 func (r *Reader) readFromCommand(shell *string, command string) bool {175 r.mutex.Lock()20 unmodified linesmodified:BUILD.md+2 added
40 unmodified lines414243444546474849505140 unmodified lines- [mattn/go-isatty](https://github.com/mattn/go-isatty) - Licensed under [MIT](http://mattn.mit-license.org)- [tcell](https://github.com/gdamore/tcell) - Licensed under [Apache License 2.0](https://github.com/gdamore/tcell/blob/master/LICENSE)- [fastwalk](https://github.com/charlievieth/fastwalk) - Licensed under [MIT](https://raw.githubusercontent.com/charlievieth/fastwalk/master/LICENSE)
License-------
[MIT](LICENSE)modified:CHANGELOG.md+14 added
1 CHANGELOG2 =========3 4+0.47.05+------6+- Replaced ["the default find command"][find] with a built-in directory traversal to simplify the code and to achieve better performance and consistent behavior across platforms.7+ This doesn't affect you if you have `$FZF_DEFAULT_COMMAND` set.8+ - Breaking changes:9+ - Unlike [the previous "find" command][find], the new traversal code will list hidden files, but hidden directories will still be ignored10+ - No filtering of `devtmpfs` or `proc` types11+ - Traversal is parallelized, so the order of the entries will be different each time12+ - You would wonder why fzf implements directory traversal anyway when it's a filter program following the Unix philosophy.13+ But fzf has had [the traversal code for years][walker] to tackle the performance problem on Windows. And I decided to use the same approach on different platforms as well for the benefits listed above.14+15+[find]: https://github.com/junegunn/fzf/blob/0.46.1/src/constants.go#L60-L6416+[walker]: https://github.com/junegunn/fzf/pull/184717+18 0.46.119 ------20 - Bug fixes and improvements21 - Fixed Windows binaries1860 unmodified linesmodified:go.mod+1 added-2 removed
1 module github.com/junegunn/fzf2 3 require (4+ github.com/charlievieth/fastwalk v1.0.15 github.com/gdamore/tcell/v2 v2.7.06 github.com/mattn/go-isatty v0.0.177 github.com/mattn/go-shellwords v1.0.128 github.com/rivo/uniseg v0.4.78- github.com/saracen/walker v0.1.39 golang.org/x/sys v0.17.010 golang.org/x/term v0.17.011 )12 13 require (14 github.com/gdamore/encoding v1.0.0 // indirect15 github.com/lucasb-eyer/go-colorful v1.2.0 // indirect16 github.com/mattn/go-runewidth v0.0.15 // indirect17- golang.org/x/sync v0.5.0 // indirect17 golang.org/x/text v0.14.0 // indirect18 )19 20 go 1.17modified:go.sum+4 added-5 removed
1+github.com/charlievieth/fastwalk v1.0.1 h1:jW01w8OCFdKS9JvAcnI+JHhWU/FuIEmNb24Ri9p7OVg=2+github.com/charlievieth/fastwalk v1.0.1/go.mod h1:dryXgMJyGHbMrAmmnF0/EJNBbZaihlwcNud5IuGyogU=3 github.com/gdamore/encoding v1.0.0 h1:+7OoQ1Bc6eTm5niUzBa0Ctsh6JbMW6Ra+YNuAtDBdko=4 github.com/gdamore/encoding v1.0.0/go.mod h1:alR0ol34c49FCSBLjhosxzcPHQbf2trDkoo5dl+VrEg=5 github.com/gdamore/tcell/v2 v2.7.0 h1:I5LiGTQuwrysAt1KS9wg1yFfOI3arI3ucFrxtd/xqaA=6 github.com/gdamore/tcell/v2 v2.7.0/go.mod h1:hl/KtAANGBecfIPxk+FzKvThTqI84oplgbPEmVX60b8=7+github.com/karrick/godirwalk v1.17.0 h1:b4kY7nqDdioR/6qnbHQyDvmA17u5G1cZ6J+CZXwSWoI=8+github.com/karrick/godirwalk v1.17.0/go.mod h1:j4mkqPuvaLI8mp1DroR3P6ad7cyYd4c1qeJ3RV7ULlk=9 github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=10 github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=11 github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng=12 github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=4 unmodified lines17 github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=18 github.com/rivo/uniseg v0.4.3/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=19 github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=20 github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=17-github.com/saracen/walker v0.1.3 h1:YtcKKmpRPy6XJTHJ75J2QYXXZYWnZNQxPCVqZSHVV/g=18-github.com/saracen/walker v0.1.3/go.mod h1:FU+7qU8DeQQgSZDmmThMJi93kPkLFgy0oVAcLxurjIk=21 github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=22 golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=23 golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=24 golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=2 unmodified lines27 golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=28 golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=29 golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=30 golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=29-golang.org/x/sync v0.0.0-20220601150217-0de741cfad7f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=31 golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=32 golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=32-golang.org/x/sync v0.5.0 h1:60k92dhOjHxJkrqnwsfl8KuaHbn/5dl0lUPUklKo3qE=33-golang.org/x/sync v0.5.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=33 golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=34 golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=35 golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=36 golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=23 unmodified linesmodified:README-VIM.md+9 added-8 removed
236 unmodified lines237 call fzf#run({'sink': 'e'})238 ```239 240 We haven't specified the `source`, so this is equivalent to starting fzf on241-command line without standard input pipe; fzf will use find command (or242-`$FZF_DEFAULT_COMMAND` if defined) to list the files under the current243-directory. When you select one, it will open it with the sink, `:e` command.244-If you want to open it in a new tab, you can pass `:tabedit` command instead245-as the sink.241+command line without standard input pipe; fzf will traverse the file system242+under the current directory to get the list of files. (If243+`$FZF_DEFAULT_COMMAND` is set, fzf will use the output of the command244+instead.) When you select one, it will open it with the sink, `:e` command. If245+you want to open it in a new tab, you can pass `:tabedit` command instead as246+the sink.247 248 ```vim249 call fzf#run({'sink': 'tabedit'})250 ```251 251-Instead of using the default find command, you can use any shell command as252-the source. The following example will list the files managed by git. It's253-equivalent to running `git ls-files | fzf` on shell.252+You can use any shell command as the source to generate the list. The253+following example will list the files managed by git. It's equivalent to254+running `git ls-files | fzf` on shell.255 256 ```vim257 call fzf#run({'source': 'git ls-files', 'sink': 'e'})258 ```235 unmodified linesmodified:README.md+6 added-8 removed
228 unmodified lines229 ```sh230 find * -type f | fzf > selected231 ```232 233-Without STDIN pipe, fzf will use find command to fetch the list of234-files excluding hidden ones. (You can override the default command with235-`FZF_DEFAULT_COMMAND`)233+Without STDIN pipe, fzf will traverse the file system under the current234+directory to get the list of files, skipping hidden directories. (You can235+override the default behavior with `FZF_DEFAULT_COMMAND`)236 237 ```sh238 vim $(fzf)239 ```247 unmodified lines487 488 # Options to fzf command489 export FZF_COMPLETION_OPTS='--border --info=inline'490 491-# Use fd (https://github.com/sharkdp/fd) instead of the default find492-# command for listing path candidates.491+# Use fd (https://github.com/sharkdp/fd) for listing path candidates.492 # - The first argument to the function ($1) is the base path to start traversal493 # - See the source code (completion.{bash,zsh}) for the details.494 _fzf_compgen_path() {495 fd --hidden --follow --exclude ".git" . "$1"276 unmodified lines772 ### Respecting `.gitignore`773 774 You can use [fd](https://github.com/sharkdp/fd),775 [ripgrep](https://github.com/BurntSushi/ripgrep), or [the silver777-searcher](https://github.com/ggreer/the_silver_searcher) instead of the778-default find command to traverse the file system while respecting779-`.gitignore`.776+searcher](https://github.com/ggreer/the_silver_searcher) to traverse the file777+system while respecting `.gitignore`.778 779 ```sh780 # Feed the output of fd into fzf781 fd --type f --strip-cwd-prefix | fzf55 unmodified lines