MCPcopy Create free account
hub / github.com/atomicdotdev/atomic / resolve_change_target

Function resolve_change_target

atomic-cli/src/commands/provenance/command.rs:176–210  ·  view source on GitHub ↗

Resolve the CLI target (bare hash, hash prefix, or `urn:atomic:change: `) to a change `Hash`.

(repo: &Repository, target: &str)

Source from the content-addressed store, hash-verified

174/// Resolve the CLI target (bare hash, hash prefix, or `urn:atomic:change:<b32>`)
175/// to a change `Hash`.
176pub(crate) fn resolve_change_target(repo: &Repository, target: &str) -> CliResult<Hash> {
177 const URN_PREFIX: &str = "urn:atomic:change:";
178 if let Some(b32) = target.strip_prefix(URN_PREFIX) {
179 return Hash::from_base32(b32.as_bytes()).ok_or_else(|| CliError::InvalidArgument {
180 message: format!("invalid change base32 in URN: {target}"),
181 });
182 }
183
184 // Bare full hash or prefix: match against the change store by base32 prefix
185 // (mirrors ChangeCmd::resolve_hash_prefix).
186 let mut matches: Vec<Hash> = Vec::new();
187 for result in repo.iter_changes() {
188 let hash = result.map_err(|e| CliError::Internal(anyhow::anyhow!("{}", e)))?;
189 if hash.to_base32().starts_with(target) {
190 matches.push(hash);
191 }
192 }
193 match matches.len() {
194 0 => {
195 // No recorded change matched. A graph can explain a change that was
196 // never recorded/made internal (the REV_DEPS fallback case), so try
197 // parsing the target as a full base32 change hash directly.
198 Hash::from_base32(target.as_bytes()).ok_or_else(|| CliError::ChangeNotFound {
199 hash: target.to_string(),
200 })
201 }
202 1 => Ok(matches[0]),
203 _ => {
204 let list: Vec<String> = matches.iter().map(|h| h.to_base32()).collect();
205 Err(CliError::AmbiguousHash {
206 hash: format!("{} (matches: {})", target, list.join(", ")),
207 })
208 }
209 }
210}
211
212/// Resolve the PERSON's signing identity + keypair, exactly as
213/// `atomic intent attest` does.

Callers 1

runMethod · 0.85

Calls 6

as_bytesMethod · 0.45
iter_changesMethod · 0.45
to_base32Method · 0.45
pushMethod · 0.45
lenMethod · 0.45
iterMethod · 0.45

Tested by

no test coverage detected