Repository: sharkdp/fd
Pull request #1540 (2024-04-28 06:17:54)

Stop ignore .git folders by default
Fixes: #1457
See: #1396, #1403


Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 1717b85..4b84cdb 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -4,6 +4,10 @@
 
 - Add `dir` as an alias to `directory` when using `-t` \ `--type`, see #1460 and #1464 (@Ato2207).
 - Add support for @%s date format in time filters similar to GNU date (seconds since Unix epoch for --older/--newer), see #1493 (@nabellows)
+- Breaking: No longer automatically ignore `.git` when using `--hidden` with vcs ignore enabled. This reverts the change in v9.0.0. While this feature
+  was often useful, it also broke some existing workflows, and there wasn't a good way to opt out of it. And there isn't really a good way for us to add
+  a way to opt out of it. And you can easily get similar behavior by adding `.git/` to your global fdignore file.
+    See #1457.
 
 ## Bugfixes
 
diff --git a/README.md b/README.md
index c52cf40..ddef3d1 100644
--- a/README.md
+++ b/README.md
@@ -282,6 +282,9 @@ If you want `fd` to ignore these patterns globally, you can put them in `fd`'s g
 This is usually located in `~/.config/fd/ignore` in macOS or Linux, and `%APPDATA%\fd\ignore` in
 Windows.
 
+You may wish to include `.git/` in your `fd/ignore` file so that `.git` directories, and their contents
+are not included in output if you use the `--hidden` option.
+
 ### Deleting files
 
 You can use `fd` to remove all files and directories that are matched by your search pattern.
diff --git a/doc/fd.1 b/doc/fd.1
index 1ac63c5..498981f 100644
--- a/doc/fd.1
+++ b/doc/fd.1
@@ -33,16 +33,14 @@ with the '\-\-glob' option.
 By default
 .B fd
 will exclude hidden files and directories, as well as any files that match gitignore rules
-or ignore rules in .ignore or .fdignore files. For convenenience, '.git' is treated as if it
-was always included in gitignore rules. These files can be included with options such as
-'\-\-hidden' and '\-\-no\-ignore'.
+or ignore rules in .ignore or .fdignore files.
 .SH OPTIONS
 .TP
 .B \-H, \-\-hidden
 Include hidden files and directories in the search results
 (default: hidden files and directories are skipped). The flag can be overridden with '--no-hidden'.
 .IP
-Ignored files and .git/ are still excluded unless \-\-no\-ignore or \-\-no\-ignore\-vcs
+Ignored files are still excluded unless \-\-no\-ignore or \-\-no\-ignore\-vcs
 is also used.
 .TP
 .B \-I, \-\-no\-ignore
@@ -79,7 +77,6 @@ and the global gitignore configuration
 .RI ( core.excludesFile
 git setting, which defaults to
 .IR $HOME/.config/git/ignore ).
-The pattern ".git/" is automatically added to the list of VCS ignore rules.
 The flag can be overridden with '--ignore-vcs'.
 .TP
 .B \-\-no\-require\-git
@@ -494,6 +491,17 @@ is set, use
 .IR $XDG_CONFIG_HOME/fd/ignore .
 Otherwise, use
 .IR $HOME/.config/fd/ignore .
+.SH FILES
+.TP
+.B .fdignore
+This file works similarly to a .gitignore file anywhere in the searched tree and specifies patterns
+that should be excluded from the search. However, this file is specific to fd, and will be used even
+if the --no-ignore-vcs option is used.
+.TP
+.B $XDG_CONFIG_HOME/fd/ignore
+Global ignore file. Unless ignore mode is turned off (such as with --no-ignore)
+ignore entries in this file will be ignored, as if it was an .fdignore file in the
+current directory.
 .SH EXAMPLES
 .TP
 .RI "Find files and directories that match the pattern '" needle "':"
@@ -507,6 +515,16 @@ $ fd -e py
 .TP
 .RI "Open all search results with vim:"
 $ fd pattern -X vim
+.SH Tips and Tricks
+.IP \[bu]
+If you add ".git/" to your global ignore file ($XDG_CONFIG_HOME/fd/ignore), then
+".git" folders will be ignored by default, even when the --hidden option is used.
+.IP \[bu]
+You can use a shell alias or a wrapper script in order to pass desired flags to fd
+by default. For example if you do not like the default behavior of respecting gitignore,
+you can use
+`alias fd="/usr/bin/fd --no-ignore-vcs"`
+in your .bashrc to create an alias for fd that doesn't ignore git files by default.
 .SH BUGS
 Bugs can be reported on GitHub: https://github.com/sharkdp/fd/issues
 .SH SEE ALSO
diff --git a/src/cli.rs b/src/cli.rs
index 6e61636..746773a 100644
--- a/src/cli.rs
+++ b/src/cli.rs
@@ -49,8 +49,7 @@ pub struct Opts {
     no_hidden: (),
 
     /// Show search results from files and directories that would otherwise be
-    /// ignored by '.gitignore', '.ignore', '.fdignore', the global ignore file,
-    /// or the default rule that excludes .git/.
+    /// ignored by '.gitignore', '.ignore', '.fdignore', or the global ignore file,
     /// The flag can be overridden with --ignore.
     #[arg(
         long,
@@ -64,7 +63,7 @@ pub struct Opts {
     #[arg(long, overrides_with = "no_ignore", hide = true, action = ArgAction::SetTrue)]
     ignore: (),
 
-    ///Show search results from '.git/' folders and files and directories that
+    ///Show search results from files and directories that
     ///would otherwise be ignored by '.gitignore' files.
     ///The flag can be overridden with --ignore-vcs.
     #[arg(
diff --git a/src/walk.rs b/src/walk.rs
index 08ff3fe..155d329 100644
--- a/src/walk.rs
+++ b/src/walk.rs
@@ -334,10 +334,6 @@ impl WorkerState {
                 .map_err(|e| anyhow!("Malformed exclude pattern: {}", e))?;
         }
 
-        if config.read_vcsignore {
-            builder.add("!.git/").expect("Invalid exclude pattern");
-        }
-
         builder
             .build()
             .map_err(|_| anyhow!("Mismatch in exclude patterns"))


Test patch:
diff --git a/tests/tests.rs b/tests/tests.rs
index ba83141..071d1f0 100644
--- a/tests/tests.rs
+++ b/tests/tests.rs
@@ -2573,7 +2573,14 @@ fn test_git_dir() {
         ],
     );
 
-    te.assert_output(&["--hidden", "foo"], "");
+    te.assert_output(
+        &["--hidden", "foo"],
+        ".git/one/foo.a
+        .git/.foo
+        .git/a.foo
+        other_dir/.git/foo1
+        nested/dir/.git/foo2",
+    );
     te.assert_output(&["--no-ignore", "foo"], "");
     te.assert_output(
         &["--hidden", "--no-ignore", "foo"],