gitoxide_core/repository/
branch.rs1use crate::OutputFormat;
2
3pub mod list {
4 pub enum Kind {
5 Local,
6 All,
7 }
8
9 pub struct Options {
10 pub kind: Kind,
11 }
12}
13
14pub fn list(
15 repo: gix::Repository,
16 out: &mut dyn std::io::Write,
17 format: OutputFormat,
18 options: list::Options,
19) -> anyhow::Result<()> {
20 if format != OutputFormat::Human {
21 anyhow::bail!("JSON output isn't supported");
22 }
23
24 let platform = repo.references()?;
25
26 let (show_local, show_remotes) = match options.kind {
27 list::Kind::Local => (true, false),
28 list::Kind::All => (true, true),
29 };
30
31 if show_local {
32 let mut branch_names: Vec<String> = platform
33 .local_branches()?
34 .flatten()
35 .map(|branch| branch.name().shorten().to_string())
36 .collect();
37
38 branch_names.sort();
39
40 for branch_name in branch_names {
41 writeln!(out, "{branch_name}")?;
42 }
43 }
44
45 if show_remotes {
46 let mut branch_names: Vec<String> = platform
47 .remote_branches()?
48 .flatten()
49 .map(|branch| branch.name().shorten().to_string())
50 .collect();
51
52 branch_names.sort();
53
54 for branch_name in branch_names {
55 writeln!(out, "{branch_name}")?;
56 }
57 }
58
59 Ok(())
60}