Skip to content

Commit

Permalink
Add ignore flag
Browse files Browse the repository at this point in the history
Add the ignore flag which make cargo-machete respect .ignore and
.gitignore files when searching for files.
  • Loading branch information
mickvangelderen authored and bnjbvr committed Mar 25, 2024
1 parent 0bc8104 commit 2c20b2e
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 14 deletions.
40 changes: 40 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ argh = "0.1.9"
cargo_metadata = "0.18.0"
cargo_toml = "0.19.1"
grep = "0.3.1"
ignore = "0.4.20"
log = "0.4.16"
pretty_env_logger = "0.5.0"
rayon = "1.5.2"
Expand Down
57 changes: 43 additions & 14 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use rayon::prelude::*;
use std::path::Path;
use std::str::FromStr;
use std::{fs, path::PathBuf};
use walkdir::WalkDir;

#[derive(Clone, Copy)]
pub(crate) enum UseCargoMetadata {
Expand Down Expand Up @@ -62,6 +61,10 @@ struct MacheteArgs {
#[argh(switch)]
fix: bool,

/// respect ignore files (.gitignore, .ignore, etc.) when searching for files.
#[argh(switch)]
ignore: bool,

/// print version.
#[argh(switch)]
version: bool,
Expand All @@ -71,21 +74,26 @@ struct MacheteArgs {
paths: Vec<PathBuf>,
}

fn collect_paths(path: &Path, skip_target_dir: bool) -> Result<Vec<PathBuf>, walkdir::Error> {
struct CollectPathOptions {
skip_target_dir: bool,
respect_ignore_files: bool,
}

fn collect_paths(path: &Path, options: CollectPathOptions) -> Result<Vec<PathBuf>, ignore::Error> {
// Find directory entries.
let walker = WalkDir::new(path).into_iter();
let mut builder = ignore::WalkBuilder::new(path);

let manifest_path_entries = if skip_target_dir {
walker
.filter_entry(|entry| !entry.path().ends_with("target"))
.collect()
} else {
walker.collect::<Vec<_>>()
};
builder.standard_filters(options.respect_ignore_files);

if options.skip_target_dir {
builder.filter_entry(|entry| !entry.path().ends_with("target"));
}

let walker = builder.build();

// Keep only errors and `Cargo.toml` files (filter), then map correct paths into owned
// `PathBuf`.
manifest_path_entries
walker
.into_iter()
.filter(|entry| match entry {
Ok(entry) => entry.file_name() == "Cargo.toml",
Expand Down Expand Up @@ -142,7 +150,13 @@ fn run_machete() -> anyhow::Result<bool> {
let mut walkdir_errors = Vec::new();

for path in args.paths {
let manifest_path_entries = match collect_paths(&path, args.skip_target_dir) {
let manifest_path_entries = match collect_paths(
&path,
CollectPathOptions {
skip_target_dir: args.skip_target_dir,
respect_ignore_files: args.ignore,
},
) {
Ok(entries) => entries,
Err(err) => {
walkdir_errors.push(err);
Expand Down Expand Up @@ -290,13 +304,28 @@ const TOP_LEVEL: &str = concat!(env!("CARGO_MANIFEST_DIR"));
fn test_ignore_target() {
let entries = collect_paths(
&PathBuf::from(TOP_LEVEL).join("./integration-tests/with-target/"),
true,
CollectPathOptions {
skip_target_dir: true,
respect_ignore_files: false,
},
);
assert!(entries.unwrap().is_empty());

let entries = collect_paths(
&PathBuf::from(TOP_LEVEL).join("./integration-tests/with-target/"),
CollectPathOptions {
skip_target_dir: false,
respect_ignore_files: true,
},
);
assert!(entries.unwrap().is_empty());

let entries = collect_paths(
&PathBuf::from(TOP_LEVEL).join("./integration-tests/with-target/"),
false,
CollectPathOptions {
skip_target_dir: false,
respect_ignore_files: false,
},
);
assert!(!entries.unwrap().is_empty());
}

0 comments on commit 2c20b2e

Please sign in to comment.