Skip to content

Commit

Permalink
feat: gix rev previous-branches subcommand (#427)
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Aug 3, 2022
1 parent 727768a commit e972aad
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -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)
Expand Down
26 changes: 26 additions & 0 deletions gitoxide-core/src/repository/revision/mod.rs
Expand Up @@ -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;
10 changes: 9 additions & 1 deletion src/plumbing/main.rs
Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions src/plumbing/options.rs
Expand Up @@ -190,6 +190,8 @@ pub mod revision {
#[clap(min_values = 1)]
specs: Vec<std::ffi::OsString>,
},
/// Return the names and hashes of all previously checked-out branches.
PreviousBranches,
}
}

Expand Down

0 comments on commit e972aad

Please sign in to comment.