Skip to content

Commit 58b5060

Browse files
committed
feat: add first debug version of gix branch list
1 parent 16973be commit 58b5060

File tree

4 files changed

+112
-2
lines changed

4 files changed

+112
-2
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
use crate::OutputFormat;
2+
3+
pub enum Kind {
4+
Local,
5+
All,
6+
}
7+
8+
impl Kind {
9+
fn includes_local_branches(&self) -> bool {
10+
match self {
11+
Self::Local | Self::All => true,
12+
}
13+
}
14+
15+
fn includes_remote_branches(&self) -> bool {
16+
match self {
17+
Self::Local => false,
18+
Self::All => true,
19+
}
20+
}
21+
}
22+
23+
pub struct Options {
24+
pub kind: Kind,
25+
}
26+
27+
pub fn list(
28+
repo: gix::Repository,
29+
out: &mut dyn std::io::Write,
30+
format: OutputFormat,
31+
options: Options,
32+
) -> anyhow::Result<()> {
33+
if format != OutputFormat::Human {
34+
anyhow::bail!("JSON output isn't supported");
35+
}
36+
37+
let platform = repo.references()?;
38+
39+
if options.kind.includes_local_branches() {
40+
let mut branch_names: Vec<String> = platform
41+
.local_branches()?
42+
.flatten()
43+
.map(|branch| branch.name().shorten().to_string())
44+
.collect();
45+
46+
branch_names.sort();
47+
48+
for branch_name in branch_names {
49+
writeln!(out, "{branch_name}")?;
50+
}
51+
}
52+
53+
if options.kind.includes_remote_branches() {
54+
let mut branch_names: Vec<String> = platform
55+
.remote_branches()?
56+
.flatten()
57+
.map(|branch| branch.name().shorten().to_string())
58+
.collect();
59+
60+
branch_names.sort();
61+
62+
for branch_name in branch_names {
63+
writeln!(out, "{branch_name}")?;
64+
}
65+
}
66+
67+
Ok(())
68+
}

gitoxide-core/src/repository/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use gix::bstr::BString;
66

77
#[cfg(feature = "archive")]
88
pub mod archive;
9+
pub mod branch;
910
pub mod cat;
1011
pub use cat::function::cat;
1112
pub mod blame;

src/plumbing/main.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ use gix::bstr::{io::BufReadExt, BString};
1616
use crate::{
1717
plumbing::{
1818
options::{
19-
attributes, commit, commitgraph, config, credential, exclude, free, fsck, index, mailmap, merge, odb,
20-
revision, tag, tree, Args, Subcommands,
19+
attributes, branch, commit, commitgraph, config, credential, exclude, free, fsck, index, mailmap, merge,
20+
odb, revision, tag, tree, Args, Subcommands,
2121
},
2222
show_progress,
2323
},
@@ -509,6 +509,26 @@ pub fn main() -> Result<()> {
509509
)
510510
},
511511
),
512+
Subcommands::Branch(platform) => match platform.cmds {
513+
Some(branch::Subcommands::List) | None => {
514+
use core::repository::branch::{Kind, Options};
515+
516+
let kind = if platform.all { Kind::All } else { Kind::Local };
517+
let options = Options { kind };
518+
519+
prepare_and_run(
520+
"branch-list",
521+
trace,
522+
auto_verbose,
523+
progress,
524+
progress_keep_open,
525+
None,
526+
move |_progress, out, _err| {
527+
core::repository::branch::list(repository(Mode::Lenient)?, out, format, options)
528+
},
529+
)
530+
}
531+
},
512532
#[cfg(feature = "gitoxide-core-tools-corpus")]
513533
Subcommands::Corpus(crate::plumbing::options::corpus::Platform { db, path, cmd }) => {
514534
let reverse_trace_lines = progress;

src/plumbing/options/mod.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ pub enum Subcommands {
8383
/// Subcommands for creating worktree archives.
8484
#[cfg(feature = "gitoxide-core-tools-archive")]
8585
Archive(archive::Platform),
86+
/// Interact with branches.
87+
#[clap(visible_alias = "branches")]
88+
Branch(branch::Platform),
8689
/// Remove untracked files from the working tree.
8790
#[cfg(feature = "gitoxide-core-tools-clean")]
8891
Clean(clean::Command),
@@ -236,6 +239,24 @@ pub mod archive {
236239
}
237240
}
238241

242+
pub mod branch {
243+
#[derive(Debug, clap::Parser)]
244+
pub struct Platform {
245+
#[clap(subcommand)]
246+
pub cmds: Option<Subcommands>,
247+
248+
/// List remote-tracking as well as local branches.
249+
#[clap(long, short = 'a')]
250+
pub all: bool,
251+
}
252+
253+
#[derive(Debug, clap::Subcommand)]
254+
pub enum Subcommands {
255+
/// List all tags.
256+
List,
257+
}
258+
}
259+
239260
pub mod status {
240261
use gix::bstr::BString;
241262

0 commit comments

Comments
 (0)