Skip to content

Commit

Permalink
feat: Allow to print a tree without prettification, using `--tree-sty…
Browse files Browse the repository at this point in the history
…le raw`.

This is mainly useful to generate fixtures for the test-suite, and is assured
to not add extra-bytes to the output either.
  • Loading branch information
Byron committed Nov 9, 2023
1 parent e95bb9f commit 8dfbb4b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
14 changes: 11 additions & 3 deletions gitoxide-core/src/repository/revision/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ pub struct Options {
pub format: OutputFormat,
pub explain: bool,
pub cat_file: bool,
pub tree_mode: TreeMode,
}

pub enum TreeMode {
Raw,
Pretty,
}

pub(crate) mod function {
Expand All @@ -13,6 +19,7 @@ pub(crate) mod function {
use gix::revision::Spec;

use super::Options;
use crate::repository::revision::resolve::TreeMode;
use crate::{repository::revision, OutputFormat};

pub fn resolve(
Expand All @@ -23,6 +30,7 @@ pub(crate) mod function {
format,
explain,
cat_file,
tree_mode,
}: Options,
) -> anyhow::Result<()> {
repo.object_cache_size_if_unset(1024 * 1024);
Expand All @@ -36,7 +44,7 @@ pub(crate) mod function {
let spec = gix::path::os_str_into_bstr(&spec)?;
let spec = repo.rev_parse(spec)?;
if cat_file {
return display_object(spec, out);
return display_object(spec, tree_mode, out);
}
writeln!(out, "{spec}", spec = spec.detach())?;
}
Expand All @@ -63,11 +71,11 @@ pub(crate) mod function {
Ok(())
}

fn display_object(spec: Spec<'_>, mut out: impl std::io::Write) -> anyhow::Result<()> {
fn display_object(spec: Spec<'_>, tree_mode: TreeMode, 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 {
gix::object::Kind::Tree => {
gix::object::Kind::Tree if matches!(tree_mode, TreeMode::Pretty) => {
for entry in object.into_tree().iter() {
writeln!(out, "{}", entry?)?;
}
Expand Down
7 changes: 7 additions & 0 deletions src/plumbing/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -925,6 +925,7 @@ pub fn main() -> Result<()> {
specs,
explain,
cat_file,
tree_mode,
} => prepare_and_run(
"revision-parse",
trace,
Expand All @@ -941,6 +942,12 @@ pub fn main() -> Result<()> {
format,
explain,
cat_file,
tree_mode: match tree_mode.unwrap_or_default() {
revision::resolve::TreeMode::Raw => core::repository::revision::resolve::TreeMode::Raw,
revision::resolve::TreeMode::Pretty => {
core::repository::revision::resolve::TreeMode::Pretty
}
},
},
)
},
Expand Down
12 changes: 12 additions & 0 deletions src/plumbing/options/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,16 @@ pub mod commitgraph {
}

pub mod revision {
pub mod resolve {
#[derive(Default, Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, clap::ValueEnum)]
pub enum TreeMode {
/// Show the raw bytes - only useful for piping into files for use with tooling.
Raw,
/// Display a tree in human-readable form.
#[default]
Pretty,
}
}
#[derive(Debug, clap::Subcommand)]
#[clap(visible_alias = "rev", visible_alias = "r")]
pub enum Subcommands {
Expand Down Expand Up @@ -625,6 +635,8 @@ pub mod revision {
/// 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,
#[clap(short = 't', long)]
tree_mode: Option<resolve::TreeMode>,
/// rev-specs like `@`, `@~1` or `HEAD^2`.
#[clap(required = true)]
specs: Vec<std::ffi::OsString>,
Expand Down

0 comments on commit 8dfbb4b

Please sign in to comment.