Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions gitoxide-core/src/repository/branch.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use crate::OutputFormat;

pub mod list {
pub enum Kind {
Local,
All,
}

pub struct Options {
pub kind: Kind,
}
}

pub fn list(
repo: gix::Repository,
out: &mut dyn std::io::Write,
format: OutputFormat,
options: list::Options,
) -> anyhow::Result<()> {
if format != OutputFormat::Human {
anyhow::bail!("JSON output isn't supported");
}

let platform = repo.references()?;

let (show_local, show_remotes) = match options.kind {
list::Kind::Local => (true, false),
list::Kind::All => (true, true),
};

if show_local {
let mut branch_names: Vec<String> = platform
.local_branches()?
.flatten()
.map(|branch| branch.name().shorten().to_string())
.collect();

branch_names.sort();

for branch_name in branch_names {
writeln!(out, "{branch_name}")?;
}
}

if show_remotes {
let mut branch_names: Vec<String> = platform
.remote_branches()?
.flatten()
.map(|branch| branch.name().shorten().to_string())
.collect();

branch_names.sort();

for branch_name in branch_names {
writeln!(out, "{branch_name}")?;
}
}

Ok(())
}
1 change: 1 addition & 0 deletions gitoxide-core/src/repository/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use gix::bstr::BString;

#[cfg(feature = "archive")]
pub mod archive;
pub mod branch;
pub mod cat;
pub use cat::function::cat;
pub mod blame;
Expand Down
24 changes: 22 additions & 2 deletions src/plumbing/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ use gix::bstr::{io::BufReadExt, BString};
use crate::{
plumbing::{
options::{
attributes, commit, commitgraph, config, credential, exclude, free, fsck, index, mailmap, merge, odb,
revision, tag, tree, Args, Subcommands,
attributes, branch, commit, commitgraph, config, credential, exclude, free, fsck, index, mailmap, merge,
odb, revision, tag, tree, Args, Subcommands,
},
show_progress,
},
Expand Down Expand Up @@ -509,6 +509,26 @@ pub fn main() -> Result<()> {
)
},
),
Subcommands::Branch(platform) => match platform.cmd {
branch::Subcommands::List { all } => {
use core::repository::branch::list;

let kind = if all { list::Kind::All } else { list::Kind::Local };
let options = list::Options { kind };

prepare_and_run(
"branch-list",
trace,
auto_verbose,
progress,
progress_keep_open,
None,
move |_progress, out, _err| {
core::repository::branch::list(repository(Mode::Lenient)?, out, format, options)
},
)
}
},
#[cfg(feature = "gitoxide-core-tools-corpus")]
Subcommands::Corpus(crate::plumbing::options::corpus::Platform { db, path, cmd }) => {
let reverse_trace_lines = progress;
Expand Down
21 changes: 21 additions & 0 deletions src/plumbing/options/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ pub enum Subcommands {
/// Subcommands for creating worktree archives.
#[cfg(feature = "gitoxide-core-tools-archive")]
Archive(archive::Platform),
/// Interact with branches.
#[clap(visible_alias = "branches")]
Branch(branch::Platform),
/// Remove untracked files from the working tree.
#[cfg(feature = "gitoxide-core-tools-clean")]
Clean(clean::Command),
Expand Down Expand Up @@ -236,6 +239,24 @@ pub mod archive {
}
}

pub mod branch {
#[derive(Debug, clap::Parser)]
pub struct Platform {
#[clap(subcommand)]
pub cmd: Subcommands,
}

#[derive(Debug, clap::Subcommand)]
pub enum Subcommands {
/// List all tags.
List {
/// List remote-tracking as well as local branches.
#[clap(long, short = 'a')]
all: bool,
},
}
}

pub mod status {
use gix::bstr::BString;

Expand Down
2 changes: 1 addition & 1 deletion tests/it/src/commands/blame_copy_royal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ git commit -m {commit_id}
// history’s root being the last element. We reverse the order in place so that all
// methods can rely on the assumption that the root comes first, followed by its
// descendants. That way, we can use a simple `for` loop to iterate through
// `self.blame_path` below.
// `self.blame_infos` below.
self.blame_infos.reverse();

for blame_path_entry in self.blame_infos.clone() {
Expand Down
Loading