Skip to content

Commit 525873f

Browse files
authored
Merge pull request #2142 from cruessler/add-branch-list
feat: add first debug version of `gix branch list`
2 parents cd148cf + 04650a7 commit 525873f

File tree

5 files changed

+105
-3
lines changed

5 files changed

+105
-3
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
use crate::OutputFormat;
2+
3+
pub mod list {
4+
pub enum Kind {
5+
Local,
6+
All,
7+
}
8+
9+
pub struct Options {
10+
pub kind: Kind,
11+
}
12+
}
13+
14+
pub 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+
}

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.cmd {
513+
branch::Subcommands::List { all } => {
514+
use core::repository::branch::list;
515+
516+
let kind = if all { list::Kind::All } else { list::Kind::Local };
517+
let options = list::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 cmd: Subcommands,
247+
}
248+
249+
#[derive(Debug, clap::Subcommand)]
250+
pub enum Subcommands {
251+
/// List branches.
252+
List {
253+
/// List remote-tracking as well as local branches.
254+
#[clap(long, short = 'a')]
255+
all: bool,
256+
},
257+
}
258+
}
259+
239260
pub mod status {
240261
use gix::bstr::BString;
241262

tests/it/src/commands/blame_copy_royal.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ git commit -m {commit_id}
217217
// history’s root being the last element. We reverse the order in place so that all
218218
// methods can rely on the assumption that the root comes first, followed by its
219219
// descendants. That way, we can use a simple `for` loop to iterate through
220-
// `self.blame_path` below.
220+
// `self.blame_infos` below.
221221
self.blame_infos.reverse();
222222

223223
for blame_path_entry in self.blame_infos.clone() {

0 commit comments

Comments
 (0)