Add global fdignore support

This commit is contained in:
Bobbie Soedirgo 2020-04-26 03:32:17 +08:00 committed by David Peter
parent a9dc45ecb1
commit 79d5a5bdc5
8 changed files with 60 additions and 7 deletions

View File

@ -46,6 +46,7 @@ humantime = "2.0"
lscolors = "0.7"
globset = "0.4"
anyhow = "1.0"
dirs = "2.0"
[dependencies.clap]
version = "2.31.2"

View File

@ -458,6 +458,10 @@ To make exclude-patterns like these permanent, you can create a `.fdignore` file
```
Note: `fd` also supports `.ignore` files that are used by other programs such as `rg` or `ag`.
If you want `fd` to ignore these patterns globally, you can put them in `fd`'s global ignore file.
This is usually located in `~/.config/fd/ignore` in macOS or Linux, and `%APPDATA%\fd\ignore` in
Windows.
### Using fd with `xargs` or `parallel`
If we want to run a command on all search results, we can pipe the output to `xargs`:

15
doc/fd.1 vendored
View File

@ -33,10 +33,9 @@ Include hidden files and directories in the search results
.B \-I, \-\-no\-ignore
Show search results from files and directories that would otherwise be ignored by
.IR .gitignore ,
.I .ignore
or
.I .fdignore
files.
.IR .ignore ,
.IR .fdignore ,
or the global ignore file.
.TP
.B \-u, \-\-unrestricted
Alias for '--no-ignore'. Can be repeated; '-uu' is an alias for '--no-ignore --hidden'.
@ -282,6 +281,14 @@ The glob syntax is documented here:
.B LS_COLORS
Determines how to colorize search results, see
.BR dircolors (1) .
.TP
.B XDG_CONFIG_HOME, HOME
Used to locate the global ignore file. If
.B XDG_CONFIG_HOME
is set, use
.IR $XDG_CONFIG_HOME/fd/ignore .
Otherwise, use
.IR $HOME/.config/fd/ignore .
.SH EXAMPLES
.TP
.RI "Find files and directories that match the pattern '" needle "':"

View File

@ -30,7 +30,7 @@ pub fn build_app() -> App<'static, 'static> {
.help("Do not respect .(git|fd)ignore files")
.long_help(
"Show search results from files and directories that would otherwise be \
ignored by '.gitignore', '.ignore' or '.fdignore' files.",
ignored by '.gitignore', '.ignore', '.fdignore', or the global ignore file.",
),
)
.arg(
@ -43,6 +43,12 @@ pub fn build_app() -> App<'static, 'static> {
ignored by '.gitignore' files.",
),
)
.arg(
Arg::with_name("no-global-ignore-file")
.long("no-global-ignore-file")
.hidden(true)
.long_help("Do not respect the global ignore file."),
)
.arg(
Arg::with_name("rg-alias-hidden-ignore")
.short("u")

View File

@ -294,6 +294,9 @@ fn run() -> Result<ExitCode> {
read_vcsignore: !(matches.is_present("no-ignore")
|| matches.is_present("rg-alias-hidden-ignore")
|| matches.is_present("no-ignore-vcs")),
read_global_ignore: !(matches.is_present("no-ignore")
|| matches.is_present("rg-alias-hidden-ignore")
|| matches.is_present("no-global-ignore-file")),
follow_links: matches.is_present("follow"),
one_file_system: matches.is_present("one-file-system"),
null_separator: matches.is_present("null_separator"),

View File

@ -27,6 +27,9 @@ pub struct Options {
/// Whether to respect VCS ignore files (`.gitignore`, ..) or not.
pub read_vcsignore: bool,
/// Whether to respect the global ignore file or not.
pub read_global_ignore: bool,
/// Whether to follow symlinks or not.
pub follow_links: bool,

View File

@ -1,4 +1,5 @@
use std::borrow::Cow;
use std::env;
use std::ffi::OsStr;
use std::fs::{FileType, Metadata};
use std::io;
@ -82,6 +83,34 @@ pub fn scan(path_vec: &[PathBuf], pattern: Arc<Regex>, config: Arc<Options>) ->
walker.add_custom_ignore_filename(".fdignore");
}
if config.read_global_ignore {
#[cfg(target_os = "macos")]
let config_dir_op = env::var_os("XDG_CONFIG_HOME")
.map(PathBuf::from)
.filter(|p| p.is_absolute())
.or_else(|| dirs::home_dir().map(|d| d.join(".config")));
#[cfg(not(target_os = "macos"))]
let config_dir_op = dirs::config_dir();
if let Some(global_ignore_file) = config_dir_op
.map(|p| p.join("fd").join("ignore"))
.filter(|p| p.is_file())
{
let result = walker.add_ignore(global_ignore_file);
match result {
Some(ignore::Error::Partial(_)) => (),
Some(err) => {
print_error(format!(
"Malformed pattern in global ignore file. {}.",
err.to_string()
));
}
None => (),
}
}
}
for ignore_file in &config.ignore_files {
let result = walker.add_ignore(ignore_file);
match result {

View File

@ -190,7 +190,7 @@ impl TestEnv {
// Setup *fd* command.
let mut cmd = process::Command::new(&self.fd_exe);
cmd.current_dir(self.temp_dir.path().join(path));
cmd.args(args);
cmd.arg("--no-global-ignore-file").args(args);
// Run *fd*.
let output = cmd.output().expect("fd output");
@ -251,7 +251,7 @@ impl TestEnv {
// Setup *fd* command.
let mut cmd = process::Command::new(&self.fd_exe);
cmd.current_dir(self.temp_dir.path().join(path));
cmd.args(args);
cmd.arg("--no-global-ignore-file").args(args);
// Run *fd*.
let output = cmd.output().expect("fd output");