Skip to content

Commit

Permalink
Add rough but working version of rev-parse (#427)
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Jun 15, 2022
1 parent 0660588 commit f3f176d
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 3 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion gitoxide-core/src/repository/revision/explain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use git::revision::{
use git_repository as git;
use std::ffi::OsString;

pub fn explain(_repo: git::Repository, spec: OsString, mut out: impl std::io::Write) -> anyhow::Result<()> {
pub fn explain(spec: OsString, mut out: impl std::io::Write) -> anyhow::Result<()> {
let mut explain = Explain::new(&mut out);
let spec = git::path::os_str_into_bstr(&spec)?;
git::revision::spec::parse(spec, &mut explain)?;
Expand Down
3 changes: 3 additions & 0 deletions gitoxide-core/src/repository/revision/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
mod explain;
pub use explain::explain;

pub mod parse;
pub use parse::function::parse;
48 changes: 48 additions & 0 deletions gitoxide-core/src/repository/revision/parse.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use crate::OutputFormat;

pub struct Options {
pub format: OutputFormat,
}

pub(crate) mod function {
use super::Options;
use crate::OutputFormat;
use git_repository as git;
use std::ffi::OsString;

pub fn parse(
repo: git::Repository,
spec: OsString,
mut out: impl std::io::Write,
Options { format }: Options,
) -> anyhow::Result<()> {
let spec = git::path::os_str_into_bstr(&spec)?;
let spec = git::RevSpec::from_bstr(spec.as_ref(), &repo)?.detach();

match format {
OutputFormat::Human => {
if let Some((kind, from, to)) = spec.range() {
writeln!(&mut out, "{}", from)?;
writeln!(
&mut out,
"{}{}",
matches!(kind, git::revision::spec::Kind::Range)
.then(|| "^")
.unwrap_or_default(),
to
)?;
if matches!(kind, git::revision::spec::Kind::Range) {
writeln!(out, "^TBD: compute and display merge base hash")?;
}
} else if let Some(rev) = spec.single() {
writeln!(&mut out, "{}", rev)?;
}
}
#[cfg(feature = "serde1")]
OutputFormat::Json => {
serde_json::to_writer_pretty(&mut out, &spec)?;
}
}
Ok(())
}
}
19 changes: 17 additions & 2 deletions src/plumbing/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,28 @@ pub fn main() -> Result<()> {
let repository = git::ThreadSafeRepository::discover(repository)?;
match cmd {
repo::Subcommands::Revision { cmd } => match cmd {
repo::revision::Subcommands::Parse { spec } => prepare_and_run(
"repository-revision-parse",
verbose,
progress,
progress_keep_open,
None,
move |_progress, out, _err| {
core::repository::revision::parse(
repository.into(),
spec,
out,
core::repository::revision::parse::Options { format },
)
},
),
repo::revision::Subcommands::Explain { spec } => prepare_and_run(
"repository-commit-describe",
"repository-revision-explain",
verbose,
progress,
progress_keep_open,
None,
move |_progress, out, _err| core::repository::revision::explain(repository.into(), spec, out),
move |_progress, out, _err| core::repository::revision::explain(spec, out),
),
},
repo::Subcommands::Commit { cmd } => match cmd {
Expand Down
3 changes: 3 additions & 0 deletions src/plumbing/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,9 @@ pub mod repo {
pub enum Subcommands {
/// Provide the revision specification like `@~1` to explain.
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 },
}
}

Expand Down

0 comments on commit f3f176d

Please sign in to comment.