Resolve a change reference (full hash or prefix) to a full hash.
(
&self,
repo: &Repository,
change_ref: &str,
)
| 1045 | |
| 1046 | /// Resolve a change reference (full hash or prefix) to a full hash. |
| 1047 | pub(super) fn resolve_change_ref( |
| 1048 | &self, |
| 1049 | repo: &Repository, |
| 1050 | change_ref: &str, |
| 1051 | ) -> CliResult<Hash> { |
| 1052 | // Try to parse as a full hash first |
| 1053 | if let Some(hash) = Hash::from_base32(change_ref.as_bytes()) { |
| 1054 | if repo.has_change(&hash) { |
| 1055 | return Ok(hash); |
| 1056 | } |
| 1057 | } |
| 1058 | |
| 1059 | // Search for matching changes by prefix |
| 1060 | let mut matches: Vec<Hash> = Vec::new(); |
| 1061 | let prefix_upper = change_ref.to_uppercase(); |
| 1062 | |
| 1063 | for result in repo.iter_changes() { |
| 1064 | let hash = result.map_err(|e| CliError::Internal(anyhow::anyhow!("{}", e)))?; |
| 1065 | let hash_str = hash.to_base32(); |
| 1066 | if hash_str.starts_with(&prefix_upper) { |
| 1067 | matches.push(hash); |
| 1068 | } |
| 1069 | } |
| 1070 | |
| 1071 | match matches.len() { |
| 1072 | 0 => Err(CliError::ChangeNotFound { |
| 1073 | hash: change_ref.to_string(), |
| 1074 | }), |
| 1075 | 1 => Ok(matches[0]), |
| 1076 | _ => { |
| 1077 | let match_list: Vec<String> = matches.iter().map(|h| h.to_base32()).collect(); |
| 1078 | Err(CliError::AmbiguousHash { |
| 1079 | hash: format!("{} (matches: {})", change_ref, match_list.join(", ")), |
| 1080 | }) |
| 1081 | } |
| 1082 | } |
| 1083 | } |
| 1084 | } |
no test coverage detected