Skip to content

Commit

Permalink
feat: --cat-file flag for gix rev parse to cat instead of resolvi…
Browse files Browse the repository at this point in the history
…ng. (#427)
  • Loading branch information
Byron committed Aug 3, 2022
1 parent b47bbb7 commit b83f6bd
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 7 deletions.
30 changes: 26 additions & 4 deletions gitoxide-core/src/repository/revision/resolve.rs
Expand Up @@ -3,10 +3,11 @@ use crate::OutputFormat;
pub struct Options {
pub format: OutputFormat,
pub explain: bool,
pub cat_file: bool,
}

pub(crate) mod function {
use anyhow::bail;
use anyhow::{bail, Context};
use std::ffi::OsString;

use git_repository as git;
Expand All @@ -19,7 +20,11 @@ pub(crate) mod function {
mut repo: git::Repository,
specs: Vec<OsString>,
mut out: impl std::io::Write,
Options { format, explain }: Options,
Options {
format,
explain,
cat_file,
}: Options,
) -> anyhow::Result<()> {
repo.object_cache_size_if_unset(1024 * 1024);

Expand All @@ -30,8 +35,11 @@ pub(crate) mod function {
return revision::explain(spec, out);
}
let spec = git::path::os_str_into_bstr(&spec)?;
let spec = repo.rev_parse(spec)?.detach();
writeln!(out, "{spec}")?;
let spec = repo.rev_parse(spec)?;
if cat_file {
return display_object(spec, out);
}
writeln!(out, "{spec}", spec = spec.detach())?;
}
}
#[cfg(feature = "serde1")]
Expand All @@ -55,4 +63,18 @@ pub(crate) mod function {
}
Ok(())
}

fn display_object(spec: git::revision::Spec<'_>, mut out: impl std::io::Write) -> anyhow::Result<()> {
let id = spec.single().context("rev-spec must resolve to a single object")?;
let object = id.object()?;
match object.kind {
git::object::Kind::Tree => {
for entry in object.into_tree().iter() {
writeln!(out, "{}", entry?)?;
}
}
_ => out.write_all(&object.data)?,
}
Ok(())
}
}
12 changes: 10 additions & 2 deletions src/plumbing/main.rs
Expand Up @@ -532,7 +532,11 @@ pub fn main() -> Result<()> {
None,
move |_progress, out, _err| core::repository::revision::explain(spec, out),
),
revision::Subcommands::Resolve { specs, explain } => prepare_and_run(
revision::Subcommands::Resolve {
specs,
explain,
cat_file,
} => prepare_and_run(
"revision-parse",
verbose,
progress,
Expand All @@ -543,7 +547,11 @@ pub fn main() -> Result<()> {
repository()?,
specs,
out,
core::repository::revision::resolve::Options { format, explain },
core::repository::revision::resolve::Options {
format,
explain,
cat_file,
},
)
},
),
Expand Down
6 changes: 5 additions & 1 deletion src/plumbing/options.rs
Expand Up @@ -187,10 +187,14 @@ pub mod revision {
/// Try to resolve the given revspec and print the object names.
#[clap(visible_alias = "query", visible_alias = "parse")]
Resolve {
/// If set, instead of resolving a rev-spec, explain what would be done for the first spec.
/// Instead of resolving a rev-spec, explain what would be done for the first spec.
///
/// Equivalent to the `explain` subcommand.
#[clap(short = 'e', long)]
explain: bool,
/// Show the first resulting object similar to how `git cat-file` would, but don't show the resolved spec.
#[clap(short = 'c', long, conflicts_with = "explain")]
cat_file: bool,
/// rev-specs like `@`, `@~1` or `HEAD^2`.
#[clap(required = true)]
specs: Vec<std::ffi::OsString>,
Expand Down

0 comments on commit b83f6bd

Please sign in to comment.