Watch
0
0
Fork
You've already forked hyperhive
0
hyperhive/hive-forge/src/verbs/tree_sha.rs

50 lines
1.3 KiB
Rust

//! `tree-sha <ref> [repo]` — print the tree SHA of the commit at a
//! branch name or commit SHA.
use anyhow::Result;
use clap::Args as ClapArgs;
use forgejo_api::structs::RepoGetSingleCommitQuery;
use crate::client::Client;
use crate::verbs::refuse_json;
#[derive(ClapArgs)]
pub struct Args {
/// Branch name or commit SHA.
reference: String,
}
pub fn run(client: &Client, args: Args) -> Result<()> {
refuse_json(client.json_mode(), "tree-sha", "only the plain sha")?;
let (owner, name) = client.owner_repo()?;
// Try branch first; the bash helper silently treats failure as
// "not a branch, use the reference as a commit sha directly".
let commit_sha = match client
.api()
.repo_get_branch(owner, name, &args.reference)
.send()
{
Ok(branch) => branch
.commit
.and_then(|c| c.id)
.unwrap_or_else(|| args.reference.clone()),
Err(_) => args.reference.clone(),
};
let commit = client
.api()
.repo_get_single_commit(
owner,
name,
&commit_sha,
RepoGetSingleCommitQuery::default(),
)
.send()?;
let sha = commit
.commit
.and_then(|c| c.tree)
.and_then(|t| t.sha)
.or(commit.sha)
.unwrap_or_default();
println!("{sha}");
Ok(())
}