| 153 | } |
| 154 | |
| 155 | fn resolve_change(repo: &Repository, prefix: &str) -> CliResult<Hash> { |
| 156 | // Validate input shape first for a precise error — the shared resolver |
| 157 | // treats malformed input as "no match". |
| 158 | let prefix = prefix.to_ascii_uppercase(); |
| 159 | if prefix.is_empty() |
| 160 | || prefix.len() > 52 |
| 161 | || !prefix |
| 162 | .bytes() |
| 163 | .all(|b| b.is_ascii_uppercase() || (b'2'..=b'7').contains(&b)) |
| 164 | { |
| 165 | return Err(CliError::InvalidArgument { |
| 166 | message: "Expected a Base32 change hash or a non-empty unique prefix".into(), |
| 167 | }); |
| 168 | } |
| 169 | |
| 170 | // The repository's shared, case-insensitive prefix resolver (the same |
| 171 | // one `atomic insert` uses) matches against the whole change store, so a |
| 172 | // hash that exists only on another view still resolves here and then |
| 173 | // hits the repository's explicit membership guard in `unrecord`. |
| 174 | match repo.find_change_by_prefix(&prefix) { |
| 175 | Ok(Some(hash)) => Ok(hash), |
| 176 | Ok(None) => Err(CliError::ChangeNotFound { hash: prefix }), |
| 177 | Err(RepositoryError::AmbiguousHash { prefix, matches }) => Err(CliError::AmbiguousHash { |
| 178 | hash: format!("{prefix} (matches: {})", matches.join(", ")), |
| 179 | }), |
| 180 | Err(e) => Err(CliError::Internal(anyhow::anyhow!("{e}"))), |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | #[cfg(test)] |
| 185 | mod tests { |