lychee icon indicating copy to clipboard operation
lychee copied to clipboard

Glob patterns to exclude directory?

Open mxpv opened this issue 4 years ago • 6 comments

I have a fairly large repository with md files spread across many directories. So I'd like to periodically check for broken links in *.md files. Something like lychee --verbose --no-progress **/*.md works well, however I'd like to exclude vendor/ directory to avoid unnecessary checks. Would something like lychee --verbose --no-progress **/*.md !./vendor/* be within scope of this project?

mxpv avatar Jul 15 '21 01:07 mxpv

I thought that already works. I was wrong. We should support that, yes. If anyone wants to give it a try, comment here.

mre avatar Jul 19 '21 15:07 mre

Workaround inspired from https://stackoverflow.com/a/16595367 : lychee --verbose -- $(find . -not \( -path ./tests -prune \) -name '*.py' -print0 |xargs --null)

ajoga avatar Aug 17 '21 08:08 ajoga

I'm astonished by that ingenuity.😄 It does make sense, though. Thanks for mentioning that!

mre avatar Aug 17 '21 09:08 mre

thanks hehe =) I've spent a bit of time trying to reuse that snippet in a github action with no luck. I found more useful to rm -fr the part of the code I didn't want to check then glob everything than use that snippet

ajoga avatar Aug 17 '21 15:08 ajoga

Implementation-wise, this can be done with a GlobSet:

use globset::{Glob, GlobSetBuilder};

let mut builder = GlobSetBuilder::new();
// A GlobBuilder can be used to configure each glob's match semantics
// independently.
builder.add(Glob::new("**/*.md")?);
builder.add(Glob::new("!./vendor/*")?);
let set = builder.build()?;

assert_eq!(set.matches("foo.md"), vec![0, 1]);
assert_eq!(set.matches("./vendor/bar.md"), vec![0]);

So I think we can check if set.matches(input).len() == set.len().

Docs: https://docs.rs/globset/latest/globset/

mre avatar Nov 22 '21 11:11 mre

"It's not a bug it's a feature." 😆

mre avatar Dec 03 '21 12:12 mre

Revisiting this, I just realized there is an easier way:

lychee --exclude-path vendor --verbose --no-progress './**/*.md'

Sorry for missing that. I focused too much on the glob pattern and not the original problem. It probably won't help you anymore, but maybe someone else who runs into the same issue.

In any case we should still tackle glob exclusions at some point.

mre avatar Nov 11 '22 11:11 mre

Thanks, that worked!

mxpv avatar Feb 21 '24 19:02 mxpv