-
Notifications
You must be signed in to change notification settings - Fork 35
Add glob support for directory and file pattern matching #306
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
karthiknadig
wants to merge
2
commits into
main
Choose a base branch
from
glob-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+288
−4
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,236 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| use glob::glob; | ||
| use std::path::PathBuf; | ||
|
|
||
| /// Characters that indicate a path contains glob pattern metacharacters. | ||
| const GLOB_METACHARACTERS: &[char] = &['*', '?', '[', ']']; | ||
|
|
||
| /// Checks whether a path string contains glob metacharacters. | ||
| /// | ||
| /// # Examples | ||
| /// - `"/home/user/*"` → `true` | ||
| /// - `"/home/user/envs"` → `false` | ||
| /// - `"**/*.py"` → `true` | ||
| /// - `"/home/user/[abc]"` → `true` | ||
| pub fn is_glob_pattern(path: &str) -> bool { | ||
| path.contains(GLOB_METACHARACTERS) | ||
| } | ||
|
|
||
| /// Expands a single glob pattern to matching paths. | ||
| /// | ||
| /// If the path does not contain glob metacharacters, returns it unchanged (if it exists) | ||
| /// or as-is (to let downstream code handle non-existent paths). | ||
| /// | ||
| /// If the path is a glob pattern, expands it and returns all matching paths. | ||
| /// Pattern errors and unreadable paths are logged and skipped. | ||
| /// | ||
| /// # Examples | ||
| /// - `"/home/user/envs"` → `["/home/user/envs"]` | ||
| /// - `"/home/user/*/venv"` → `["/home/user/project1/venv", "/home/user/project2/venv"]` | ||
| /// - `"**/.venv"` → All `.venv` directories recursively | ||
| pub fn expand_glob_pattern(pattern: &str) -> Vec<PathBuf> { | ||
| if !is_glob_pattern(pattern) { | ||
| // Not a glob pattern, return as-is | ||
| return vec![PathBuf::from(pattern)]; | ||
| } | ||
|
|
||
| match glob(pattern) { | ||
| Ok(paths) => { | ||
| let mut result = Vec::new(); | ||
| for entry in paths { | ||
| match entry { | ||
| Ok(path) => result.push(path), | ||
| Err(e) => { | ||
| log::debug!("Failed to read glob entry: {}", e); | ||
| } | ||
| } | ||
| } | ||
| if result.is_empty() { | ||
| log::debug!("Glob pattern '{}' matched no paths", pattern); | ||
| } | ||
| result | ||
| } | ||
| Err(e) => { | ||
| log::warn!("Invalid glob pattern '{}': {}", pattern, e); | ||
| Vec::new() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Expands a list of paths, where each path may be a glob pattern. | ||
| /// | ||
| /// Non-glob paths are passed through as-is. | ||
| /// Glob patterns are expanded to all matching paths. | ||
| /// Duplicate paths are preserved (caller should deduplicate if needed). | ||
| /// | ||
| /// # Examples | ||
| /// ```ignore | ||
| /// let paths = vec![ | ||
| /// PathBuf::from("/home/user/project"), | ||
| /// PathBuf::from("/home/user/*/venv"), | ||
| /// ]; | ||
| /// let expanded = expand_glob_patterns(&paths); | ||
| /// // expanded contains "/home/user/project" plus all matching venv dirs | ||
| /// ``` | ||
| pub fn expand_glob_patterns(paths: &[PathBuf]) -> Vec<PathBuf> { | ||
| let mut result = Vec::new(); | ||
| for path in paths { | ||
| let path_str = path.to_string_lossy(); | ||
| let expanded = expand_glob_pattern(&path_str); | ||
| result.extend(expanded); | ||
| } | ||
| result | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use std::fs; | ||
|
|
||
| #[test] | ||
| fn test_is_glob_pattern_with_asterisk() { | ||
| assert!(is_glob_pattern("/home/user/*")); | ||
| assert!(is_glob_pattern("**/*.py")); | ||
| assert!(is_glob_pattern("*.txt")); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_is_glob_pattern_with_question_mark() { | ||
| assert!(is_glob_pattern("/home/user/file?.txt")); | ||
| assert!(is_glob_pattern("test?")); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_is_glob_pattern_with_brackets() { | ||
| assert!(is_glob_pattern("/home/user/[abc]")); | ||
| assert!(is_glob_pattern("file[0-9].txt")); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_is_glob_pattern_no_metacharacters() { | ||
| assert!(!is_glob_pattern("/home/user/envs")); | ||
| assert!(!is_glob_pattern("simple_path")); | ||
| assert!(!is_glob_pattern("/usr/local/bin/python3")); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_expand_non_glob_path() { | ||
| let path = "/some/literal/path"; | ||
| let result = expand_glob_pattern(path); | ||
| assert_eq!(result.len(), 1); | ||
| assert_eq!(result[0], PathBuf::from(path)); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_expand_glob_pattern_no_matches() { | ||
| let pattern = "/this/path/definitely/does/not/exist/*"; | ||
| let result = expand_glob_pattern(pattern); | ||
| assert!(result.is_empty()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_expand_glob_pattern_with_matches() { | ||
| // Create temp directories for testing | ||
| let temp_dir = std::env::temp_dir().join("pet_glob_test"); | ||
| let _ = fs::remove_dir_all(&temp_dir); | ||
| fs::create_dir_all(temp_dir.join("project1")).unwrap(); | ||
| fs::create_dir_all(temp_dir.join("project2")).unwrap(); | ||
| fs::create_dir_all(temp_dir.join("other")).unwrap(); | ||
|
|
||
| let pattern = format!("{}/project*", temp_dir.to_string_lossy()); | ||
| let result = expand_glob_pattern(&pattern); | ||
|
|
||
| assert_eq!(result.len(), 2); | ||
| assert!(result.iter().any(|p| p.ends_with("project1"))); | ||
| assert!(result.iter().any(|p| p.ends_with("project2"))); | ||
| assert!(!result.iter().any(|p| p.ends_with("other"))); | ||
|
|
||
| // Cleanup | ||
| let _ = fs::remove_dir_all(&temp_dir); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_expand_glob_patterns_mixed() { | ||
| let temp_dir = std::env::temp_dir().join("pet_glob_test_mixed"); | ||
| let _ = fs::remove_dir_all(&temp_dir); | ||
| fs::create_dir_all(temp_dir.join("dir1")).unwrap(); | ||
| fs::create_dir_all(temp_dir.join("dir2")).unwrap(); | ||
|
|
||
| let paths = vec![ | ||
| PathBuf::from("/literal/path"), | ||
| PathBuf::from(format!("{}/dir*", temp_dir.to_string_lossy())), | ||
| ]; | ||
|
|
||
| let result = expand_glob_patterns(&paths); | ||
|
|
||
| // Should have literal path + 2 expanded directories | ||
| assert_eq!(result.len(), 3); | ||
| assert!(result.contains(&PathBuf::from("/literal/path"))); | ||
|
|
||
| // Cleanup | ||
| let _ = fs::remove_dir_all(&temp_dir); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_expand_glob_pattern_recursive() { | ||
| // Create nested temp directories for testing ** | ||
| let temp_dir = std::env::temp_dir().join("pet_glob_test_recursive"); | ||
| let _ = fs::remove_dir_all(&temp_dir); | ||
| fs::create_dir_all(temp_dir.join("a/b/.venv")).unwrap(); | ||
| fs::create_dir_all(temp_dir.join("c/.venv")).unwrap(); | ||
| fs::create_dir_all(temp_dir.join(".venv")).unwrap(); | ||
|
|
||
| let pattern = format!("{}/**/.venv", temp_dir.to_string_lossy()); | ||
| let result = expand_glob_pattern(&pattern); | ||
|
|
||
| // Should find .venv at multiple levels (behavior depends on glob crate version) | ||
| assert!(!result.is_empty()); | ||
| assert!(result.iter().all(|p| p.ends_with(".venv"))); | ||
|
|
||
| // Cleanup | ||
| let _ = fs::remove_dir_all(&temp_dir); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_expand_glob_pattern_filename_patterns() { | ||
| // Create temp files for testing filename patterns like python_* and python.* | ||
| let temp_dir = std::env::temp_dir().join("pet_glob_test_filenames"); | ||
| let _ = fs::remove_dir_all(&temp_dir); | ||
| fs::create_dir_all(&temp_dir).unwrap(); | ||
|
|
||
| // Create files matching python_* pattern | ||
| fs::write(temp_dir.join("python_foo"), "").unwrap(); | ||
| fs::write(temp_dir.join("python_bar"), "").unwrap(); | ||
| fs::write(temp_dir.join("python_3.12"), "").unwrap(); | ||
| fs::write(temp_dir.join("other_file"), "").unwrap(); | ||
|
|
||
| // Test python_* pattern | ||
| let pattern = format!("{}/python_*", temp_dir.to_string_lossy()); | ||
| let result = expand_glob_pattern(&pattern); | ||
|
|
||
| assert_eq!(result.len(), 3); | ||
| assert!(result.iter().any(|p| p.ends_with("python_foo"))); | ||
| assert!(result.iter().any(|p| p.ends_with("python_bar"))); | ||
| assert!(result.iter().any(|p| p.ends_with("python_3.12"))); | ||
| assert!(!result.iter().any(|p| p.ends_with("other_file"))); | ||
|
|
||
| // Create files matching python.* pattern | ||
| fs::write(temp_dir.join("python.exe"), "").unwrap(); | ||
| fs::write(temp_dir.join("python.sh"), "").unwrap(); | ||
| fs::write(temp_dir.join("pythonrc"), "").unwrap(); | ||
|
|
||
| // Test python.* pattern | ||
| let pattern = format!("{}/python.*", temp_dir.to_string_lossy()); | ||
| let result = expand_glob_pattern(&pattern); | ||
|
|
||
| assert_eq!(result.len(), 2); | ||
| assert!(result.iter().any(|p| p.ends_with("python.exe"))); | ||
| assert!(result.iter().any(|p| p.ends_with("python.sh"))); | ||
| assert!(!result.iter().any(|p| p.ends_with("pythonrc"))); | ||
|
|
||
| // Cleanup | ||
| let _ = fs::remove_dir_all(&temp_dir); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| pub mod glob; | ||
| pub mod path; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what does having glob patterns supported in the workspace dirs get us?