//! `tree-sha [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; #[derive(ClapArgs)] pub struct Args { /// Branch name or commit SHA. reference: String, } pub fn run(client: &Client, args: Args) -> Result<()> { 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(()) }