diff options
author | Andrew Gallant <jamslam@gmail.com> | 2020-02-17 14:43:22 -0500 |
---|---|---|
committer | Andrew Gallant <jamslam@gmail.com> | 2020-02-17 17:16:28 -0500 |
commit | 711426a632aee0affb7009cd8e3492c4ff7273e8 (patch) | |
tree | c208b97d618f7867a55171fb2d7c2928a6ccfe37 /ignore/src | |
parent | 01eeec56bb0a67dd402aed0f5650525076b26657 (diff) |
cli: add --no-require-git flag
This flag prevents ripgrep from requiring one to search a git repository
in order to respect git-related ignore rules (global, .gitignore and
local excludes). This actually corresponds to behavior ripgrep had long
ago, but #934 changed that. It turns out that users were relying on this
buggy behavior. In most cases, fixing it as simple as converting one's
rules to .ignore or .rgignore files. Unfortunately, there are other use
cases---like Perforce automatically respecting .gitignore files---that
make a strong case for ripgrep to at least support this.
The UX of a flag like this is absolutely atrocious. It's so obscure that
it's really not worth explicitly calling it out anywhere. Moreover, the
error cases that occur when this flag isn't used (but its behavior is
desirable) will not be intuitive, do not seem easily detectable and will
not guide users to this flag. Nevertheless, the motivation for this is
just barely strong enough for me to begrudgingly accept this.
Fixes #1414, Closes #1416
Diffstat (limited to 'ignore/src')
-rw-r--r-- | ignore/src/dir.rs | 33 | ||||
-rw-r--r-- | ignore/src/walk.rs | 10 |
2 files changed, 42 insertions, 1 deletions
diff --git a/ignore/src/dir.rs b/ignore/src/dir.rs index cf957891..3f4d10bb 100644 --- a/ignore/src/dir.rs +++ b/ignore/src/dir.rs @@ -78,6 +78,9 @@ struct IgnoreOptions { git_exclude: bool, /// Whether to ignore files case insensitively ignore_case_insensitive: bool, + /// Whether a git repository must be present in order to apply any + /// git-related ignore rules. + require_git: bool, } /// Ignore is a matcher useful for recursively walking one or more directories. @@ -385,7 +388,9 @@ impl Ignore { Match::None, Match::None, ); - let any_git = self.parents().any(|ig| ig.0.has_git); + let any_git = + !self.0.opts.require_git + || self.parents().any(|ig| ig.0.has_git); let mut saw_git = false; for ig in self.parents().take_while(|ig| !ig.0.is_absolute_parent) { if m_custom_ignore.is_none() { @@ -537,6 +542,7 @@ impl IgnoreBuilder { git_ignore: true, git_exclude: true, ignore_case_insensitive: false, + require_git: true, }, } } @@ -686,6 +692,16 @@ impl IgnoreBuilder { self } + /// Whether a git repository is required to apply git-related ignore + /// rules (global rules, .gitignore and local exclude rules). + /// + /// When disabled, git-related ignore rules are applied even when searching + /// outside a git repository. + pub fn require_git(&mut self, yes: bool) -> &mut IgnoreBuilder { + self.opts.require_git = yes; + self + } + /// Process ignore files case insensitively /// /// This is disabled by default. @@ -883,6 +899,21 @@ mod tests { } #[test] + fn gitignore_allowed_no_git() { + let td = tmpdir(); + wfile(td.path().join(".gitignore"), "foo\n!bar"); + + let (ig, err) = IgnoreBuilder::new() + .require_git(false) + .build() + .add_child(td.path()); + assert!(err.is_none()); + assert!(ig.matched("foo", false).is_ignore()); + assert!(ig.matched("bar", false).is_whitelist()); + assert!(ig.matched("baz", false).is_none()); + } + + #[test] fn ignore() { let td = tmpdir(); wfile(td.path().join(".ignore"), "foo\n!bar"); diff --git a/ignore/src/walk.rs b/ignore/src/walk.rs index bbe6d7d7..331268a3 100644 --- a/ignore/src/walk.rs +++ b/ignore/src/walk.rs @@ -780,6 +780,16 @@ impl WalkBuilder { self } + /// Whether a git repository is required to apply git-related ignore + /// rules (global rules, .gitignore and local exclude rules). + /// + /// When disabled, git-related ignore rules are applied even when searching + /// outside a git repository. + pub fn require_git(&mut self, yes: bool) -> &mut WalkBuilder { + self.ig_builder.require_git(yes); + self + } + /// Process ignore files case insensitively /// /// This is disabled by default. |