From 9d68515f0f4f12366917a5e3a0e0e02d300cd9bd Mon Sep 17 00:00:00 2001 From: Khanh Duong Quoc Date: Mon, 14 Oct 2024 06:54:00 +0900 Subject: [PATCH 1/3] Add comment about reading config file --- selene/src/main.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/selene/src/main.rs b/selene/src/main.rs index 3e67e471..2a9de0e7 100644 --- a/selene/src/main.rs +++ b/selene/src/main.rs @@ -512,6 +512,7 @@ fn start(mut options: opts::Options) { let (config, config_directory): (CheckerConfig, Option) = match options.config { + // User provides a config file. We must read it and report if it is misconfigured. Some(config_file) => { let config_contents = match fs::read_to_string(&config_file) { Ok(contents) => contents, From f567965412e26abf7c435762eb7d0e28c78b21a0 Mon Sep 17 00:00:00 2001 From: Khanh Duong Quoc Date: Mon, 14 Oct 2024 06:54:00 +0900 Subject: [PATCH 2/3] Getting config directory by absolute path --- selene/src/main.rs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/selene/src/main.rs b/selene/src/main.rs index 2a9de0e7..4e9bc3ac 100644 --- a/selene/src/main.rs +++ b/selene/src/main.rs @@ -523,10 +523,16 @@ fn start(mut options: opts::Options) { }; match toml::from_str(&config_contents) { - Ok(config) => ( - config, - Path::new(&config_file).parent().map(Path::to_path_buf), - ), + Ok(config) => { + // Get config directory to using absolute path. + // There is case where `config = Some("selene.toml")`, + // which returns `""`` when getting parent directory. + let config_directory = Path::new(&config_file) + .canonicalize() + .ok() + .and_then(|path| path.parent().map(|path| path.to_path_buf())); + (config, config_directory) + } Err(error) => { error!("Config file not in correct format: {}", error); std::process::exit(1); From f606559e2923239eb541f14c8aeeaf67a1573b38 Mon Sep 17 00:00:00 2001 From: Khanh Duong Quoc Date: Mon, 14 Oct 2024 06:54:00 +0900 Subject: [PATCH 3/3] Exclude files in absolute path if they match exclude pattern (#593) --- Cargo.lock | 7 +++++++ selene/Cargo.toml | 1 + selene/src/main.rs | 24 ++++++++++++++++++++++-- 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3fd0ec0a..2ca6afc5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -709,6 +709,12 @@ version = "1.0.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d01a5bd0424d00070b0098dd17ebca6f961a959dead1dbcbbbc1d1cd8d3deeba" +[[package]] +name = "pathdiff" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d61c5ce1153ab5b689d0c074c4e7fc613e942dfb7dd9eea5ab202d2ad91fe361" + [[package]] name = "percent-encoding" version = "2.2.0" @@ -933,6 +939,7 @@ dependencies = [ "globset", "lazy_static", "num_cpus", + "pathdiff", "pretty_assertions", "profiling", "selene-lib", diff --git a/selene/Cargo.toml b/selene/Cargo.toml index e5c08751..88424393 100644 --- a/selene/Cargo.toml +++ b/selene/Cargo.toml @@ -35,6 +35,7 @@ tracy-client = { version = "0.14.1", optional = true } threadpool = "1.8" toml.workspace = true ureq = { version = "2.6.2", features = ["json"], optional = true } +pathdiff = "0.2.2" [dev-dependencies] pretty_assertions = "1.3" diff --git a/selene/src/main.rs b/selene/src/main.rs index 4e9bc3ac..5ad0ede8 100644 --- a/selene/src/main.rs +++ b/selene/src/main.rs @@ -643,6 +643,26 @@ fn start(mut options: opts::Options) { let pool = ThreadPool::new(options.num_threads); + // Directory for matching files pattern. + // If user provides a config file, we use that as our working directory, otherwise fallback to the current directory. + // The working directory needs to be an absolute path to match with paths in different directories. + let working_directory = config_directory.or_else(|| current_dir.canonicalize().ok()); + + let is_excluded = |path: &Path| -> bool { + if options.no_exclude { + return false; + } + + // File pattern between the path we are checking and current working directory. + let file_pattern = working_directory.as_ref().and_then(|dir| { + path.canonicalize() + .ok() + .and_then(|path| pathdiff::diff_paths(path, dir)) + }); + + file_pattern.is_some_and(|pattern| exclude_set.is_match(&pattern)) + }; + for filename in &options.files { if filename == "-" { let checker = Arc::clone(&checker); @@ -656,7 +676,7 @@ fn start(mut options: opts::Options) { let checker = Arc::clone(&checker); let filename = filename.to_owned(); - if !options.no_exclude && exclude_set.is_match(&filename) { + if is_excluded(Path::new(&filename)) { continue; } @@ -678,7 +698,7 @@ fn start(mut options: opts::Options) { for entry in glob { match entry { Ok(path) => { - if !options.no_exclude && exclude_set.is_match(&path) { + if is_excluded(&path) { continue; }