auths_cli/commands/
git_helpers.rs1use anyhow::{Context, Result, anyhow};
2
3use crate::subprocess::{git_command, git_silent};
4
5pub fn resolve_commit_sha(commit_ref: &str) -> Result<String> {
16 let output = git_command(&["rev-parse", commit_ref])
17 .output()
18 .context("Failed to resolve commit reference")?;
19
20 if !output.status.success() {
21 let stderr = String::from_utf8_lossy(&output.stderr).to_string();
22 let lower = stderr.to_lowercase();
23
24 if lower.contains("unknown revision") || lower.contains("bad revision") {
25 let hint = if commit_ref.contains('~') || commit_ref.contains('^') {
26 "This repository may not have enough commits. \
27 Try `git log --oneline` to see available history."
28 } else {
29 "Verify the ref exists with `git branch -a` or `git tag -l`."
30 };
31 return Err(anyhow!(
32 "Cannot resolve '{}': {}\n\nHint: {}",
33 commit_ref,
34 stderr.trim(),
35 hint
36 ));
37 }
38
39 return Err(anyhow!(
40 "Invalid commit reference '{}': {}",
41 commit_ref,
42 stderr.trim()
43 ));
44 }
45
46 Ok(String::from_utf8_lossy(&output.stdout).trim().to_string())
47}
48
49pub fn resolve_head_silent() -> Option<String> {
58 git_silent(&["rev-parse", "--verify", "--quiet", "HEAD"])
59}