codeberg_cli/actions/repo/branch/
list.rs

1use forgejo_api::structs::Branch;
2use miette::IntoDiagnostic;
3
4use crate::actions::GlobalArgs;
5use crate::render::json::JsonToStdout;
6use crate::render::option::option_display;
7use crate::render::spinner::spin_until_ready;
8use crate::types::context::BergContext;
9use crate::types::git::OwnerRepo;
10use crate::types::output::OutputMode;
11
12use clap::Parser;
13
14/// List repository branches
15#[derive(Parser, Debug)]
16pub struct RepoBranchesArgs {}
17
18impl RepoBranchesArgs {
19    pub async fn run(self, global_args: GlobalArgs) -> miette::Result<()> {
20        let ctx = BergContext::new(self, global_args).await?;
21
22        let OwnerRepo { repo, owner } = ctx.owner_repo()?;
23
24        let (_, branches) = spin_until_ready(
25            ctx.client
26                .repo_list_branches(owner.as_str(), repo.as_str())
27                .send(),
28        )
29        .await
30        .into_diagnostic()?;
31
32        match ctx.global_args.output_mode {
33            OutputMode::Pretty => {
34                present_branches(&ctx, branches);
35            }
36            OutputMode::Json => branches.print_json()?,
37        }
38
39        Ok(())
40    }
41}
42
43fn present_branches(ctx: &BergContext<RepoBranchesArgs>, branches: Vec<Branch>) {
44    let table = ctx
45        .make_table()
46        .set_header(vec!["Branch", "Commit"])
47        .add_rows(branches.into_iter().map(|branch| {
48            vec![
49                option_display(&branch.name),
50                option_display(&branch.commit.as_ref().and_then(|c| c.id.as_ref())),
51            ]
52        }));
53
54    println!("{table}", table = table.show());
55}