Resolve the CLI target (bare hash, hash prefix, or `urn:atomic:change: `) to a change `Hash`.
(repo: &Repository, target: &str)
| 252 | /// Resolve the CLI target (bare hash, hash prefix, or `urn:atomic:change:<b32>`) |
| 253 | /// to a change `Hash`. |
| 254 | pub(crate) fn resolve_change_target(repo: &Repository, target: &str) -> CliResult<Hash> { |
| 255 | const URN_PREFIX: &str = "urn:atomic:change:"; |
| 256 | if let Some(b32) = target.strip_prefix(URN_PREFIX) { |
| 257 | return Hash::from_base32(b32.as_bytes()).ok_or_else(|| CliError::InvalidArgument { |
| 258 | message: format!("invalid change base32 in URN: {target}"), |
| 259 | }); |
| 260 | } |
| 261 | |
| 262 | // Bare full hash or prefix: match against the change store by base32 prefix |
| 263 | // (mirrors ChangeCmd::resolve_hash_prefix). |
| 264 | let mut matches: Vec<Hash> = Vec::new(); |
| 265 | for result in repo.iter_changes() { |
| 266 | let hash = result.map_err(|e| CliError::Internal(anyhow::anyhow!("{}", e)))?; |
| 267 | if hash.to_base32().starts_with(target) { |
| 268 | matches.push(hash); |
| 269 | } |
| 270 | } |
| 271 | match matches.len() { |
| 272 | 0 => { |
| 273 | // No recorded change matched. A graph can explain a change that was |
| 274 | // never recorded/made internal (the REV_DEPS fallback case), so try |
| 275 | // parsing the target as a full base32 change hash directly. |
| 276 | Hash::from_base32(target.as_bytes()).ok_or_else(|| CliError::ChangeNotFound { |
| 277 | hash: target.to_string(), |
| 278 | }) |
| 279 | } |
| 280 | 1 => Ok(matches[0]), |
| 281 | _ => { |
| 282 | let list: Vec<String> = matches.iter().map(|h| h.to_base32()).collect(); |
| 283 | Err(CliError::AmbiguousHash { |
| 284 | hash: format!("{} (matches: {})", target, list.join(", ")), |
| 285 | }) |
| 286 | } |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | /// Resolve the PERSON's signing identity + keypair, exactly as |
| 291 | /// `atomic intent attest` does. |