diff options
author | TrickyPi <33021497+TrickyPi@users.noreply.github.com> | 2022-03-30 02:57:42 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-03-29 14:57:42 -0400 |
commit | 85e16a08c7559f3d0b9a15d5c1de6aa93d30cdd9 (patch) | |
tree | da52d387e36bc14f977c4f2cd4e5b35e6664106e /cli/tools/fmt.rs | |
parent | 03c71a8b4a5e4c451799144eb973402aafe22ea8 (diff) |
feat(cli/fmt): ignore .git folder when formatting files (#14138)
Diffstat (limited to 'cli/tools/fmt.rs')
-rw-r--r-- | cli/tools/fmt.rs | 70 |
1 files changed, 41 insertions, 29 deletions
diff --git a/cli/tools/fmt.rs b/cli/tools/fmt.rs index 3c5ab523e..401fe836e 100644 --- a/cli/tools/fmt.rs +++ b/cli/tools/fmt.rs @@ -90,31 +90,32 @@ pub async fn format( maybe_fmt_config.map(|c| c.options).unwrap_or_default(), ); + let fmt_predicate = + |path: &Path| is_supported_ext_fmt(path) && !is_contain_git(path); + let resolver = |changed: Option<Vec<PathBuf>>| { let files_changed = changed.is_some(); - let result = - collect_files(&include_files, &exclude_files, is_supported_ext_fmt).map( - |files| { - let refmt_files = if let Some(paths) = changed { - if check { - files - .iter() - .any(|path| paths.contains(path)) - .then(|| files) - .unwrap_or_else(|| [].to_vec()) - } else { - files - .into_iter() - .filter(|path| paths.contains(path)) - .collect::<Vec<_>>() - } + let result = collect_files(&include_files, &exclude_files, fmt_predicate) + .map(|files| { + let refmt_files = if let Some(paths) = changed { + if check { + files + .iter() + .any(|path| paths.contains(path)) + .then(|| files) + .unwrap_or_else(|| [].to_vec()) } else { files - }; - (refmt_files, fmt_options.clone()) - }, - ); + .into_iter() + .filter(|path| paths.contains(path)) + .collect::<Vec<_>>() + } + } else { + files + }; + (refmt_files, fmt_options.clone()) + }); let paths_to_watch = include_files.clone(); async move { @@ -150,15 +151,14 @@ pub async fn format( ) .await?; } else { - let files = - collect_files(&include_files, &exclude_files, is_supported_ext_fmt) - .and_then(|files| { - if files.is_empty() { - Err(generic_error("No target files found.")) - } else { - Ok(files) - } - })?; + let files = collect_files(&include_files, &exclude_files, fmt_predicate) + .and_then(|files| { + if files.is_empty() { + Err(generic_error("No target files found.")) + } else { + Ok(files) + } + })?; operation((files, fmt_options.clone())).await?; } @@ -624,6 +624,10 @@ fn is_supported_ext_fmt(path: &Path) -> bool { } } +fn is_contain_git(path: &Path) -> bool { + path.components().any(|c| c.as_os_str() == ".git") +} + #[test] fn test_is_supported_ext_fmt() { assert!(!is_supported_ext_fmt(Path::new("tests/subdir/redirects"))); @@ -650,3 +654,11 @@ fn test_is_supported_ext_fmt() { assert!(is_supported_ext_fmt(Path::new("foo.json"))); assert!(is_supported_ext_fmt(Path::new("foo.JsON"))); } + +#[test] +fn test_is_located_in_git() { + assert!(is_contain_git(Path::new("test/.git"))); + assert!(is_contain_git(Path::new(".git/bad.json"))); + assert!(is_contain_git(Path::new("test/.git/bad.json"))); + assert!(!is_contain_git(Path::new("test/bad.git/bad.json"))); +} |