Skip to content
Open
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
8 changes: 8 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,14 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let exclude_dir_globs = {
let mut patterns = GlobSet::with_capacity(exclude_dir.len());
for pattern in exclude_dir {
// GNU grep ignores trailing slashes in --exclude-dir (NEWS 2.19),
// because the match is against the directory's bare name.
let normalized = pattern.trim_end_matches('/');
let pattern = if normalized.is_empty() {
pattern.as_str()
} else {
normalized
};
patterns.add(false, pattern)?;
}
patterns
Expand Down
18 changes: 13 additions & 5 deletions tests/test_grep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1593,11 +1593,19 @@ fn recursive_with_include_exclude() {
.stdout_contains("a.txt")
.stdout_does_not_contain("b.log");

// exclude-dir filters whole directories.
let mut c = scene.cmd(env!("CARGO_BIN_EXE_grep"));
c.args(&["-r", "-l", "--exclude-dir=sub", "grep", "tree"])
.succeeds()
.stdout_does_not_contain("c.txt");
// exclude-dir filters whole directories; like GNU grep, trailing
// slashes in the name are ignored.
for value in [
"--exclude-dir=sub",
"--exclude-dir=sub/",
"--exclude-dir=sub//",
] {
let mut c = scene.cmd(env!("CARGO_BIN_EXE_grep"));
c.args(&["-r", "-l", value, "grep", "tree"])
.succeeds()
.stdout_contains("a.txt")
.stdout_does_not_contain("c.txt");
}

// include + exclude both apply (exclude wins on conflict).
let mut c = scene.cmd(env!("CARGO_BIN_EXE_grep"));
Expand Down
Loading