Skip search for ignores when not needed (#644)

This commit is contained in:
Félix Saparelli 2023-08-30 16:01:23 +12:00 committed by GitHub
parent 4c3b9f0960
commit 345eda871b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -93,60 +93,73 @@ pub async fn vcs_types(origin: &Path) -> Vec<ProjectType> {
} }
pub async fn ignores(args: &Args, vcs_types: &[ProjectType], origin: &Path) -> Vec<IgnoreFile> { pub async fn ignores(args: &Args, vcs_types: &[ProjectType], origin: &Path) -> Vec<IgnoreFile> {
let (mut ignores, errors) = ignore_files::from_origin(origin).await;
for err in errors {
warn!("while discovering project-local ignore files: {}", err);
}
debug!(?ignores, "discovered ignore files from project origin");
let mut skip_git_global_excludes = false; let mut skip_git_global_excludes = false;
if !vcs_types.is_empty() {
ignores = ignores
.into_iter()
.filter(|ig| match ig.applies_to {
Some(pt) if pt.is_vcs() => vcs_types.contains(&pt),
_ => true,
})
.inspect(|ig| {
if let IgnoreFile {
applies_to: Some(ProjectType::Git),
applies_in: None,
..
} = ig
{
warn!("project git config overrides the global excludes");
skip_git_global_excludes = true;
}
})
.collect::<Vec<_>>();
debug!(?ignores, "filtered ignores to only those for project vcs");
}
let (mut global_ignores, errors) = ignore_files::from_environment(Some("watchexec")).await; let mut ignores = if args.no_project_ignore {
for err in errors { Vec::new()
warn!("while discovering global ignore files: {}", err); } else {
} let (mut ignores, errors) = ignore_files::from_origin(origin).await;
debug!(?global_ignores, "discovered ignore files from environment"); for err in errors {
warn!("while discovering project-local ignore files: {}", err);
}
debug!(?ignores, "discovered ignore files from project origin");
if skip_git_global_excludes { if !vcs_types.is_empty() {
global_ignores = global_ignores ignores = ignores
.into_iter() .into_iter()
.filter(|gig| { .filter(|ig| match ig.applies_to {
!matches!( Some(pt) if pt.is_vcs() => vcs_types.contains(&pt),
gig, _ => true,
IgnoreFile { })
.inspect(|ig| {
if let IgnoreFile {
applies_to: Some(ProjectType::Git), applies_to: Some(ProjectType::Git),
applies_in: None, applies_in: None,
.. ..
} = ig
{
warn!("project git config overrides the global excludes");
skip_git_global_excludes = true;
} }
) })
}) .collect::<Vec<_>>();
.collect::<Vec<_>>(); debug!(?ignores, "filtered ignores to only those for project vcs");
debug!( }
?global_ignores,
"filtered global ignores to exclude global git ignores" ignores
); };
}
let global_ignores = if args.no_global_ignore {
Vec::new()
} else {
let (mut global_ignores, errors) = ignore_files::from_environment(Some("watchexec")).await;
for err in errors {
warn!("while discovering global ignore files: {}", err);
}
debug!(?global_ignores, "discovered ignore files from environment");
if skip_git_global_excludes {
global_ignores = global_ignores
.into_iter()
.filter(|gig| {
!matches!(
gig,
IgnoreFile {
applies_to: Some(ProjectType::Git),
applies_in: None,
..
}
)
})
.collect::<Vec<_>>();
debug!(
?global_ignores,
"filtered global ignores to exclude global git ignores"
);
}
global_ignores
};
ignores.extend(global_ignores.into_iter().filter(|ig| match ig.applies_to { ignores.extend(global_ignores.into_iter().filter(|ig| match ig.applies_to {
Some(pt) if pt.is_vcs() => vcs_types.contains(&pt), Some(pt) if pt.is_vcs() => vcs_types.contains(&pt),