just playing with tangled
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

revset: homogenize the logic of various symbol resolution steps into a common trait

authored by

dploch and committed by
Daniel Ploch
7bdf2b39 cf78532b

+73 -33
+73 -33
lib/src/revset.rs
··· 2022 2022 .evaluate_programmatic(repo) 2023 2023 } 2024 2024 2025 - fn resolve_git_ref(repo: &dyn Repo, symbol: &str) -> Option<Vec<CommitId>> { 2026 - let view = repo.view(); 2027 - for git_ref_prefix in &["", "refs/"] { 2028 - let target = view.get_git_ref(&(git_ref_prefix.to_string() + symbol)); 2029 - if target.is_present() { 2030 - return Some(target.added_ids().cloned().collect()); 2031 - } 2032 - } 2033 - None 2034 - } 2035 - 2036 - fn resolve_local_branch(repo: &dyn Repo, symbol: &str) -> Option<Vec<CommitId>> { 2037 - let view = repo.view(); 2038 - let target = view.get_local_branch(symbol); 2039 - target 2040 - .is_present() 2041 - .then(|| target.added_ids().cloned().collect()) 2042 - } 2043 - 2044 2025 fn resolve_remote_branch(repo: &dyn Repo, name: &str, remote: &str) -> Option<Vec<CommitId>> { 2045 2026 let view = repo.view(); 2046 2027 let target = match (name, remote) { ··· 2103 2084 } 2104 2085 } 2105 2086 2087 + /// A symbol resolver for a specific namespace of labels. 2088 + /// 2089 + /// Returns None if it cannot handle the symbol. 2090 + pub trait PartialSymbolResolver { 2091 + fn resolve_symbol( 2092 + &self, 2093 + repo: &dyn Repo, 2094 + symbol: &str, 2095 + ) -> Result<Option<Vec<CommitId>>, RevsetResolutionError>; 2096 + } 2097 + 2098 + struct TagResolver; 2099 + 2100 + impl PartialSymbolResolver for TagResolver { 2101 + fn resolve_symbol( 2102 + &self, 2103 + repo: &dyn Repo, 2104 + symbol: &str, 2105 + ) -> Result<Option<Vec<CommitId>>, RevsetResolutionError> { 2106 + let target = repo.view().get_tag(symbol); 2107 + Ok(target 2108 + .is_present() 2109 + .then(|| target.added_ids().cloned().collect())) 2110 + } 2111 + } 2112 + 2113 + struct BranchResolver; 2114 + 2115 + impl PartialSymbolResolver for BranchResolver { 2116 + fn resolve_symbol( 2117 + &self, 2118 + repo: &dyn Repo, 2119 + symbol: &str, 2120 + ) -> Result<Option<Vec<CommitId>>, RevsetResolutionError> { 2121 + let target = repo.view().get_local_branch(symbol); 2122 + Ok(target 2123 + .is_present() 2124 + .then(|| target.added_ids().cloned().collect())) 2125 + } 2126 + } 2127 + 2128 + struct GitRefResolver; 2129 + 2130 + impl PartialSymbolResolver for GitRefResolver { 2131 + fn resolve_symbol( 2132 + &self, 2133 + repo: &dyn Repo, 2134 + symbol: &str, 2135 + ) -> Result<Option<Vec<CommitId>>, RevsetResolutionError> { 2136 + let view = repo.view(); 2137 + for git_ref_prefix in &["", "refs/"] { 2138 + let target = view.get_git_ref(&(git_ref_prefix.to_string() + symbol)); 2139 + if target.is_present() { 2140 + return Ok(Some(target.added_ids().cloned().collect())); 2141 + } 2142 + } 2143 + 2144 + Ok(None) 2145 + } 2146 + } 2147 + 2148 + const DEFAULT_RESOLVERS: &[&'static dyn PartialSymbolResolver] = 2149 + &[&TagResolver, &BranchResolver, &GitRefResolver]; 2150 + 2106 2151 pub type PrefixResolver<'a, T> = Box<dyn Fn(&dyn Repo, &HexPrefix) -> PrefixResolution<T> + 'a>; 2107 2152 2108 2153 /// Resolves branches, remote branches, tags, git refs, and full and abbreviated ··· 2139 2184 self.change_id_resolver = change_id_resolver; 2140 2185 self 2141 2186 } 2187 + 2188 + fn partial_resolvers(&self) -> impl Iterator<Item = &dyn PartialSymbolResolver> { 2189 + DEFAULT_RESOLVERS.iter().copied() 2190 + } 2142 2191 } 2143 2192 2144 2193 impl SymbolResolver for DefaultSymbolResolver<'_> { ··· 2147 2196 return Err(RevsetResolutionError::EmptyString); 2148 2197 } 2149 2198 2150 - // Try to resolve as a tag 2151 - let target = self.repo.view().get_tag(symbol); 2152 - if target.is_present() { 2153 - return Ok(target.added_ids().cloned().collect()); 2199 + for partial_resolver in self.partial_resolvers() { 2200 + if let Some(ids) = partial_resolver.resolve_symbol(self.repo, symbol)? { 2201 + return Ok(ids); 2202 + } 2154 2203 } 2155 - 2156 - // Try to resolve as a branch 2157 - if let Some(ids) = resolve_local_branch(self.repo, symbol) { 2158 - return Ok(ids); 2159 - } 2160 - 2161 - // Try to resolve as a git ref 2162 - if let Some(ids) = resolve_git_ref(self.repo, symbol) { 2163 - return Ok(ids); 2164 - } 2204 + // TODO: Convert prefix resolvers 2165 2205 2166 2206 // Try to resolve as a commit id. 2167 2207 if let Some(prefix) = HexPrefix::new(symbol) {