diff --git a/README.md b/README.md index 31d8dc0ecc..4f079e5353 100644 --- a/README.md +++ b/README.md @@ -44,6 +44,7 @@ Please see _'Development Status'_ for a listing of all crates and their capabili * **revision** * [x] **explain** - show what would be done while parsing a revision specification like `HEAD~1` * [x] **parse** - Show which objects a revspec resolves to + * [x] **previous-branches** - list all previously checked out branches, powered by the ref-log. * **free** - no git repository necessary * **pack** * [x] [verify](https://asciinema.org/a/352942) diff --git a/gitoxide-core/src/repository/revision/mod.rs b/gitoxide-core/src/repository/revision/mod.rs index d60ba7eb43..5ee8940c93 100644 --- a/gitoxide-core/src/repository/revision/mod.rs +++ b/gitoxide-core/src/repository/revision/mod.rs @@ -3,3 +3,29 @@ pub use explain::explain; pub mod parse; pub use parse::function::parse; + +mod previous_branches { + use crate::OutputFormat; + use anyhow::Context; + use git_repository as git; + + pub fn function(repo: git::Repository, mut out: impl std::io::Write, format: OutputFormat) -> anyhow::Result<()> { + let branches = repo + .head()? + .prior_checked_out_branches()? + .context("The reflog for HEAD is required")?; + match format { + OutputFormat::Human => { + for (name, id) in branches { + writeln!(out, "{} {}", id, name)?; + } + } + #[cfg(feature = "serde1")] + OutputFormat::Json => { + serde_json::to_writer_pretty(&mut out, &branches)?; + } + } + Ok(()) + } +} +pub use previous_branches::function as previous_branches; diff --git a/src/plumbing/main.rs b/src/plumbing/main.rs index 3c42b2b642..1ee83043e9 100644 --- a/src/plumbing/main.rs +++ b/src/plumbing/main.rs @@ -516,8 +516,16 @@ pub fn main() -> Result<()> { }, ), Subcommands::Revision(cmd) => match cmd { + revision::Subcommands::PreviousBranches => prepare_and_run( + "revision-previousbranches", + verbose, + progress, + progress_keep_open, + None, + move |_progress, out, _err| core::repository::revision::previous_branches(repository()?, out, format), + ), revision::Subcommands::Explain { spec } => prepare_and_run( - "commit-describe", + "revision-explain", verbose, progress, progress_keep_open, diff --git a/src/plumbing/options.rs b/src/plumbing/options.rs index a7b8ce7714..98faa84832 100644 --- a/src/plumbing/options.rs +++ b/src/plumbing/options.rs @@ -190,6 +190,8 @@ pub mod revision { #[clap(min_values = 1)] specs: Vec, }, + /// Return the names and hashes of all previously checked-out branches. + PreviousBranches, } }