From 769afa96a4115dd710187eb22b6d3cfe6a534bfa Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Mon, 11 Nov 2024 12:54:35 -0500 Subject: [PATCH] Avoid retraversing filesystem when testing exact glob matches (#9022) ## Summary When testing for exact inclusion, we can just test the glob directly. There's no need to re-traverse the filesystem to find it. --- crates/uv-workspace/src/workspace.rs | 34 ++++++++++------------------ 1 file changed, 12 insertions(+), 22 deletions(-) diff --git a/crates/uv-workspace/src/workspace.rs b/crates/uv-workspace/src/workspace.rs index 7b2fba0f0..ea07f3757 100644 --- a/crates/uv-workspace/src/workspace.rs +++ b/crates/uv-workspace/src/workspace.rs @@ -1312,17 +1312,12 @@ fn is_excluded_from_workspace( let absolute_glob = PathBuf::from(glob::Pattern::escape( workspace_root.simplified().to_string_lossy().as_ref(), )) - .join(exclude_glob.as_str()) - .to_string_lossy() - .to_string(); - for excluded_root in glob(&absolute_glob) - .map_err(|err| WorkspaceError::Pattern(absolute_glob.to_string(), err))? - { - let excluded_root = excluded_root - .map_err(|err| WorkspaceError::Glob(absolute_glob.to_string(), err))?; - if excluded_root == project_path.simplified() { - return Ok(true); - } + .join(exclude_glob.as_str()); + let absolute_glob = absolute_glob.to_string_lossy(); + let exclude_pattern = glob::Pattern::new(&absolute_glob) + .map_err(|err| WorkspaceError::Pattern(absolute_glob.to_string(), err))?; + if exclude_pattern.matches_path(project_path) { + return Ok(true); } } Ok(false) @@ -1338,17 +1333,12 @@ fn is_included_in_workspace( let absolute_glob = PathBuf::from(glob::Pattern::escape( workspace_root.simplified().to_string_lossy().as_ref(), )) - .join(member_glob.as_str()) - .to_string_lossy() - .to_string(); - for member_root in glob(&absolute_glob) - .map_err(|err| WorkspaceError::Pattern(absolute_glob.to_string(), err))? - { - let member_root = - member_root.map_err(|err| WorkspaceError::Glob(absolute_glob.to_string(), err))?; - if member_root == project_path { - return Ok(true); - } + .join(member_glob.as_str()); + let absolute_glob = absolute_glob.to_string_lossy(); + let include_pattern = glob::Pattern::new(&absolute_glob) + .map_err(|err| WorkspaceError::Pattern(absolute_glob.to_string(), err))?; + if include_pattern.matches_path(project_path) { + return Ok(true); } } Ok(false)