From 84b5448deb7b87f67a1b7461f00793e7ae33ef31 Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Tue, 2 Aug 2022 14:10:18 +0800 Subject: [PATCH] support for parsing multiple specs in one invocation (#427) --- .../src/repository/revision/parse.rs | 23 +++++++++++++++---- src/plumbing/main.rs | 4 ++-- src/plumbing/options.rs | 5 +++- 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/gitoxide-core/src/repository/revision/parse.rs b/gitoxide-core/src/repository/revision/parse.rs index ec66c1c626e..bd0d89e91fe 100644 --- a/gitoxide-core/src/repository/revision/parse.rs +++ b/gitoxide-core/src/repository/revision/parse.rs @@ -12,21 +12,34 @@ pub(crate) mod function { pub fn parse( mut repo: git::Repository, - spec: OsString, + specs: Vec, mut out: impl std::io::Write, Options { format }: Options, ) -> anyhow::Result<()> { repo.object_cache_size_if_unset(1024 * 1024); - let spec = git::path::os_str_into_bstr(&spec)?; - let spec = repo.rev_parse(spec)?.detach(); match format { OutputFormat::Human => { - writeln!(out, "{spec}")?; + for spec in specs { + let spec = git::path::os_str_into_bstr(&spec)?; + let spec = repo.rev_parse(spec)?.detach(); + writeln!(out, "{spec}")?; + } } #[cfg(feature = "serde1")] OutputFormat::Json => { - serde_json::to_writer_pretty(&mut out, &spec)?; + serde_json::to_writer_pretty( + &mut out, + &specs + .into_iter() + .map(|spec| { + git::path::os_str_into_bstr(&spec) + .map_err(anyhow::Error::from) + .and_then(|spec| repo.rev_parse(spec).map_err(Into::into)) + .map(|spec| spec.detach()) + }) + .collect::, _>>()?, + )?; } } Ok(()) diff --git a/src/plumbing/main.rs b/src/plumbing/main.rs index cacd7cf0e65..3c42b2b642b 100644 --- a/src/plumbing/main.rs +++ b/src/plumbing/main.rs @@ -524,7 +524,7 @@ pub fn main() -> Result<()> { None, move |_progress, out, _err| core::repository::revision::explain(spec, out), ), - revision::Subcommands::Parse { spec } => prepare_and_run( + revision::Subcommands::Parse { specs } => prepare_and_run( "revision-parse", verbose, progress, @@ -533,7 +533,7 @@ pub fn main() -> Result<()> { move |_progress, out, _err| { core::repository::revision::parse( repository()?, - spec, + specs, out, core::repository::revision::parse::Options { format }, ) diff --git a/src/plumbing/options.rs b/src/plumbing/options.rs index 5e78e731bbe..a7b8ce7714c 100644 --- a/src/plumbing/options.rs +++ b/src/plumbing/options.rs @@ -186,7 +186,10 @@ pub mod revision { Explain { spec: std::ffi::OsString }, /// Try to resolve the given revspec and print the object names. #[clap(visible_alias = "query")] - Parse { spec: std::ffi::OsString }, + Parse { + #[clap(min_values = 1)] + specs: Vec, + }, } }