Skip to content

grep: collapse repeated trailing slashes of a -r operand - #187

Merged
sylvestre merged 2 commits into
uutils:mainfrom
sarankumar1325:grep-recursive-trailing-slashes
Nov 10, 2026
Merged

sylvestre merged 2 commits into
uutils:mainfrom
sarankumar1325:grep-recursive-trailing-slashes

Conversation

@sarankumar1325

Copy link
Copy Markdown
Contributor

Fixes #184

With -r, GNU grep (via fts) collapses a run of trailing slashes on a directory operand to one before it builds the names of the files it finds. uutils grep handed the operand to WalkDir unchanged, and Path::join adds nothing after a base that already ends in /:

$ grep -r hit tree//      # GNU: tree/sub/a.txt:hit, before: tree//sub/a.txt:hit
$ grep -r -c hit .//      # GNU: ./tree/sub/a.txt:1, before: .//tree/sub/a.txt:1

process_dir now trims the trailing run to one slash first, using the same as_encoded_bytes slicing as strip_dot_prefix. Like fts it keeps inner slashes (tree///sub// prints tree///sub/a.txt) and leaves an operand of two characters or less such as // alone. Which files get searched and the exit status don't change.

Tests:

  • recursive_collapses_repeated_trailing_slashes (tree//sub//, tree/sub///, tree/sub/); fails on main. It is #[cfg(unix)] because Windows joins walked names with \.
  • cargo test, cargo fmt --check and cargo clippy --all-targets -- -D warnings pass locally

I used an AI assistant (Grok Bot) to help write this change. I reviewed the diff, compared the output with GNU grep 3.11 on my machine and ran the tests above.

GNU grep (via fts) trims a run of trailing slashes on a directory
operand to one, so `grep -r PAT tree//` prints tree/sub/a.txt. We
passed the operand to WalkDir unchanged and printed tree//sub/a.txt.

Fixes uutils#184
Copilot AI balanced review requested due to automatic review settings November 9, 2026 16:18

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

@codspeed

codspeed Bot commented Nov 9, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

1 warning

⚠️ 3 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

✅ 10 untouched benchmarks
⏩ 17 skipped benchmarks1


Comparing sarankumar1325:grep-recursive-trailing-slashes (0dd101e) with main (322d1db)

Open in CodSpeed

Footnotes

  1. 17 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

Comment thread src/searcher.rs Outdated
fn trim_trailing_slashes(path: &Path) -> &Path {
let bytes = path.as_os_str().as_encoded_bytes();
let mut len = bytes.len();
if len > 2 && bytes[len - 1] == b'/' {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is exactly the gnulib fts_open loop (2 < len, then 1 < len && v[len - 2] == '/')
please rewrite it in your own way, e.g. with rposition of the last non / byte

Comment thread tests/test_grep.rs
// to one, but keeps inner slashes and a lone `//`.
let (scene, _) = ucmd();
build_tree(&scene);
for (operand, expected) in [

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please also add the .// case from the issue

Comment thread tests/test_grep.rs Outdated
#[test]
fn recursive_collapses_repeated_trailing_slashes() {
// GNU grep (via fts) collapses the trailing slashes of a directory operand
// to one, but keeps inner slashes and a lone `//`.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the lone // case isn't tested, please drop it from the comment

Comment thread tests/test_grep.rs Outdated
}

// Windows joins walked names with `\`, so the exact output differs there.
#[cfg(unix)]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why only unix? tree/sub/ + c.txt should give the same on windows, no?

Copilot AI balanced review requested due to automatic review settings November 9, 2026 17:22

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

@sylvestre
sylvestre merged commit e123c36 into uutils:main Nov 10, 2026
23 checks passed
@sylvestre

Copy link
Copy Markdown
Contributor

Thanks for your PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

grep - grep -r keeps an operand's repeated trailing slashes in printed file names

3 participants