Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ignore flag #95

Merged
merged 1 commit into from
Mar 25, 2024
Merged
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
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,
mickvangelderen marked this conversation as resolved.
Show resolved Hide resolved
}

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());
}
Loading