Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add global fdignore support #575

Merged
merged 1 commit into from
May 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,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`:
Expand Down
15 changes: 11 additions & 4 deletions doc/fd.1
Original file line number Diff line number Diff line change
Expand Up @@ -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'.
Expand Down Expand Up @@ -273,6 +272,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 "':"
Expand Down
8 changes: 7 additions & 1 deletion src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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")
Expand Down
3 changes: 3 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,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"),
Expand Down
3 changes: 3 additions & 0 deletions src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,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,

Expand Down
29 changes: 29 additions & 0 deletions src/walk.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::borrow::Cow;
use std::env;
use std::ffi::OsStr;
use std::fs::{FileType, Metadata};
use std::io;
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions tests/testenv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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");
Expand Down