From 397e4bda7b83eb342083a2bb8ab92e45c1b4d71c Mon Sep 17 00:00:00 2001 From: a-kenji Date: Sat, 23 Sep 2023 14:38:44 +0200 Subject: [PATCH] feat: add `hidden` flag to traverse hidden files Add the `hidden` and `no-hidden` flag that allows configuring the traversal of hidden files. Examples: ``` treefmt --hidden ``` will traverse hidden files. ``` treefmt --hidden --no-hidden ``` will not traverse hidden files, same as the default (`treefmt`). ``` treefmt --hidden --no-hidden --hidden ``` will traverse hidden files. --- clippy.toml | 2 +- docs/usage.md | 8 ++++++++ src/command/format.rs | 2 ++ src/command/mod.rs | 14 ++++++++++++-- src/engine.rs | 24 +++++++++++++----------- 5 files changed, 36 insertions(+), 14 deletions(-) diff --git a/clippy.toml b/clippy.toml index 756c7dc2..15906305 100644 --- a/clippy.toml +++ b/clippy.toml @@ -1 +1 @@ -too-many-arguments-threshold = 9 +too-many-arguments-threshold = 10 diff --git a/docs/usage.md b/docs/usage.md index b5a3da74..ec7d239b 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -64,6 +64,14 @@ treefmt [FLAGS] [OPTIONS] [--] [paths]... > Only apply selected formatters. Defaults to all formatters. +`-H, --hidden` + +> Also traverse hidden files (files that start with a .). This behaviour can be overridden with the `no-hidden` flag. + +`--no-hidden` + +> Override the `--hidden` flag. Don't traverse hidden files. + `--tree-root ` > Set the path to the tree root directory where treefmt will look for the files to format. Defaults to the folder holding the `treefmt.toml` file. It’s mostly useful in combination with `--config-file` to specify the project root which won’t coincide with the directory holding `treefmt.toml`. diff --git a/src/command/format.rs b/src/command/format.rs index 409d8098..16b7e97d 100644 --- a/src/command/format.rs +++ b/src/command/format.rs @@ -9,6 +9,7 @@ pub fn format_cmd( work_dir: &Path, config_file: &Path, paths: &[PathBuf], + hidden: bool, no_cache: bool, clear_cache: bool, fail_on_change: bool, @@ -55,6 +56,7 @@ pub fn format_cmd( &cache_dir, config_file, &paths, + hidden, no_cache, clear_cache, fail_on_change, diff --git a/src/command/mod.rs b/src/command/mod.rs index 284c7fb6..a88ce073 100644 --- a/src/command/mod.rs +++ b/src/command/mod.rs @@ -23,7 +23,7 @@ use std::{ #[derive(Parser, Debug)] #[command(author, version, about, long_about = None)] pub struct Cli { - /// Create a new treefmt.toml + /// Create a new treefmt.toml. #[arg(short, long, default_value_t = false)] pub init: bool, @@ -39,6 +39,15 @@ pub struct Cli { #[arg(short, long, default_value_t = false)] pub clear_cache: bool, + #[arg(long = "hidden", short = 'H')] + /// Include hidden files while traversing the tree. + /// Override with the --no-hidden flag. + pub hidden: bool, + /// Overrides the --hidden flag. + /// Don't include hidden files while traversing the tree. + #[arg(long, overrides_with = "hidden", hide = true)] + no_hidden: bool, + /// Exit with error if any changes were made. Useful for CI. #[arg( long, @@ -52,7 +61,7 @@ pub struct Cli { #[arg(long, default_value_t = false)] pub allow_missing_formatter: bool, - /// Log verbosity is based off the number of v used + /// Log verbosity is based off the number of v used. #[clap(flatten)] pub verbose: Verbosity, @@ -158,6 +167,7 @@ pub fn run_cli(cli: &Cli) -> anyhow::Result<()> { &cli.work_dir, config_file, &cli.paths, + cli.hidden, cli.no_cache, cli.clear_cache, cli.fail_on_change, diff --git a/src/engine.rs b/src/engine.rs index 2d973fe9..087b306e 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -29,6 +29,7 @@ pub fn run_treefmt( cache_dir: &Path, treefmt_toml: &Path, paths: &[PathBuf], + hidden: bool, no_cache: bool, clear_cache: bool, fail_on_change: bool, @@ -89,7 +90,7 @@ pub fn run_treefmt( cache.update_formatters(formatters.clone()); } - let walker = build_walker(paths); + let walker = build_walker(paths, hidden); let matches = collect_matches_from_walker(walker, &formatters, &mut stats); stats.timed_debug("tree walk"); @@ -322,7 +323,7 @@ fn collect_matches_from_walker( } /// Configure and build the tree walker -fn build_walker(paths: Vec) -> Walk { +fn build_walker(paths: Vec, hidden: bool) -> Walk { // For some reason the WalkBuilder must start with one path, but can add more paths later. // unwrap: we checked before that there is at least one path in the vector let mut builder = WalkBuilder::new(paths.first().unwrap()); @@ -330,6 +331,7 @@ fn build_walker(paths: Vec) -> Walk { for path in paths[1..].iter() { builder.add(path); } + builder.hidden(!hidden); // TODO: builder has a lot of interesting options. // TODO: use build_parallel with a Visitor. // See https://docs.rs/ignore/0.4.17/ignore/struct.WalkParallel.html#method.visit @@ -981,7 +983,7 @@ mod tests { let formatters = load_formatters(root, tree_root, false, &None, &mut stats).unwrap(); - let walker = build_walker(vec![tree_root.to_path_buf()]); + let walker = build_walker(vec![tree_root.to_path_buf()], false); let _matches = collect_matches_from_walker(walker, &formatters, &mut stats); assert_eq!(stats.traversed_files, 3); @@ -1030,7 +1032,7 @@ mod tests { let formatters = load_formatters(root, tree_root, false, &None, &mut stats).unwrap(); - let walker = build_walker(vec![tree_root.to_path_buf()]); + let walker = build_walker(vec![tree_root.to_path_buf()], false); let _matches = collect_matches_from_walker(walker, &formatters, &mut stats); assert_eq!(stats.traversed_files, 15); @@ -1080,7 +1082,7 @@ mod tests { let formatters = load_formatters(root, tree_root, false, &None, &mut stats).unwrap(); - let walker = build_walker(vec![tree_root.to_path_buf()]); + let walker = build_walker(vec![tree_root.to_path_buf()], false); let _matches = collect_matches_from_walker(walker, &formatters, &mut stats); assert_eq!(stats.traversed_files, 15); @@ -1130,7 +1132,7 @@ mod tests { let formatters = load_formatters(root, tree_root, false, &None, &mut stats).unwrap(); - let walker = build_walker(vec![tree_root.to_path_buf()]); + let walker = build_walker(vec![tree_root.to_path_buf()], false); let matches = collect_matches_from_walker(walker, &formatters, &mut stats); assert_eq!(stats.traversed_files, 12); @@ -1213,7 +1215,7 @@ mod tests { let formatters = load_formatters(root, tree_root, false, &None, &mut stats).unwrap(); - let walker = build_walker(vec![tree_root.to_path_buf()]); + let walker = build_walker(vec![tree_root.to_path_buf()], false); let matches = collect_matches_from_walker(walker, &formatters, &mut stats); assert_eq!(stats.traversed_files, 18); @@ -1285,7 +1287,7 @@ mod tests { let formatters = load_formatters(root, tree_root, false, &None, &mut stats).unwrap(); - let walker = build_walker(vec![tree_root.to_path_buf()]); + let walker = build_walker(vec![tree_root.to_path_buf()], false); let matches = collect_matches_from_walker(walker, &formatters, &mut stats); assert_eq!(stats.traversed_files, 7); @@ -1347,7 +1349,7 @@ mod tests { let formatters = load_formatters(root, tree_root, false, &None, &mut stats).unwrap(); - let walker = build_walker(vec![tree_root.to_path_buf()]); + let walker = build_walker(vec![tree_root.to_path_buf()], false); let matches = collect_matches_from_walker(walker, &formatters, &mut stats); assert_eq!(stats.traversed_files, 7); @@ -1417,7 +1419,7 @@ mod tests { let formatters = load_formatters(root, tree_root, false, &None, &mut stats).unwrap(); - let walker = build_walker(vec![tree_root.to_path_buf()]); + let walker = build_walker(vec![tree_root.to_path_buf()], false); let matches = collect_matches_from_walker(walker, &formatters, &mut stats); assert_eq!(stats.traversed_files, 8); @@ -1486,7 +1488,7 @@ mod tests { let formatters = load_formatters(root, tree_root, false, &None, &mut stats).unwrap(); - let walker = build_walker(vec![tree_root.to_path_buf()]); + let walker = build_walker(vec![tree_root.to_path_buf()], false); let matches = collect_matches_from_walker(walker, &formatters, &mut stats); assert_eq!(stats.traversed_files, 7);