Skip to content

Commit

Permalink
support for parsing multiple specs in one invocation (#427)
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Aug 2, 2022
1 parent df83e23 commit 84b5448
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
23 changes: 18 additions & 5 deletions gitoxide-core/src/repository/revision/parse.rs
Expand Up @@ -12,21 +12,34 @@ pub(crate) mod function {

pub fn parse(
mut repo: git::Repository,
spec: OsString,
specs: Vec<OsString>,
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::<Result<Vec<_>, _>>()?,
)?;
}
}
Ok(())
Expand Down
4 changes: 2 additions & 2 deletions src/plumbing/main.rs
Expand Up @@ -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,
Expand All @@ -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 },
)
Expand Down
5 changes: 4 additions & 1 deletion src/plumbing/options.rs
Expand Up @@ -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<std::ffi::OsString>,
},
}
}

Expand Down

0 comments on commit 84b5448

Please sign in to comment.