Resolve a change reference (full hash or prefix) to a full hash.
(
&self,
repo: &Repository,
change_ref: &str,
)
| 741 | |
| 742 | /// Resolve a change reference (full hash or prefix) to a full hash. |
| 743 | pub(super) fn resolve_change_ref( |
| 744 | &self, |
| 745 | repo: &Repository, |
| 746 | change_ref: &str, |
| 747 | ) -> CliResult<Hash> { |
| 748 | // Try to parse as a full hash first |
| 749 | if let Some(hash) = Hash::from_base32(change_ref.as_bytes()) { |
| 750 | if repo.has_change(&hash) { |
| 751 | return Ok(hash); |
| 752 | } |
| 753 | } |
| 754 | |
| 755 | // Search for matching changes by prefix |
| 756 | let mut matches: Vec<Hash> = Vec::new(); |
| 757 | let prefix_upper = change_ref.to_uppercase(); |
| 758 | |
| 759 | for result in repo.iter_changes() { |
| 760 | let hash = result.map_err(|e| CliError::Internal(anyhow::anyhow!("{}", e)))?; |
| 761 | let hash_str = hash.to_base32(); |
| 762 | if hash_str.starts_with(&prefix_upper) { |
| 763 | matches.push(hash); |
| 764 | } |
| 765 | } |
| 766 | |
| 767 | match matches.len() { |
| 768 | 0 => Err(CliError::ChangeNotFound { |
| 769 | hash: change_ref.to_string(), |
| 770 | }), |
| 771 | 1 => Ok(matches[0]), |
| 772 | _ => { |
| 773 | let match_list: Vec<String> = matches.iter().map(|h| h.to_base32()).collect(); |
| 774 | Err(CliError::AmbiguousHash { |
| 775 | hash: format!("{} (matches: {})", change_ref, match_list.join(", ")), |
| 776 | }) |
| 777 | } |
| 778 | } |
| 779 | } |
| 780 | } |
no test coverage detected