MCPcopy Index your code
hub / github.com/ChrisFeldmeier/OpenCodeRust / glob_up

Method glob_up

crates/opencode-util/src/filesystem.rs:86–139  ·  view source on GitHub ↗
(pattern: &str, start: P, stop: Option<P>)

Source from the content-addressed store, hash-verified

84 }
85
86 pub async fn glob_up<P: AsRef<Path>>(pattern: &str, start: P, stop: Option<P>) -> Vec<PathBuf> {
87 let mut current = start.as_ref().to_path_buf();
88 let stop = stop.map(|s| s.as_ref().to_path_buf());
89 let mut result = Vec::new();
90
91 loop {
92 let glob = match glob::Pattern::new(pattern) {
93 Ok(g) => g,
94 Err(_) => {
95 if let Some(ref s) = stop {
96 if &current == s {
97 break;
98 }
99 }
100 if let Some(parent) = current.parent() {
101 if parent == current {
102 break;
103 }
104 current = parent.to_path_buf();
105 }
106 continue;
107 }
108 };
109
110 for entry in WalkDir::new(&current)
111 .max_depth(1)
112 .follow_links(true)
113 .into_iter()
114 .filter_map(|e| e.ok())
115 {
116 let path = entry.path();
117 if path.is_file() {
118 if let Ok(rel) = path.strip_prefix(&current) {
119 if glob.matches(&rel.to_string_lossy()) {
120 result.push(path.to_path_buf());
121 }
122 }
123 }
124 }
125
126 if stop.as_ref() == Some(&current) {
127 break;
128 }
129 if let Some(parent) = current.parent() {
130 if parent == current {
131 break;
132 }
133 current = parent.to_path_buf();
134 } else {
135 break;
136 }
137 }
138 result
139 }
140}
141
142#[cfg(test)]

Callers

nothing calls this directly

Calls 2

newFunction · 0.85
is_fileMethod · 0.80

Tested by

no test coverage detected