Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions gitoxide-core/src/repository/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,66 @@ pub fn describe(
Ok(())
}

pub fn name_rev(
repo: gix::Repository,
rev_spec: Option<&str>,
mut out: impl std::io::Write,
name_rev::Options {
tags,
refs,
exclude,
always,
no_undefined,
name_only,
}: name_rev::Options,
) -> Result<()> {
let commit = match rev_spec {
Some(spec) => repo
.rev_parse_single(format!("{spec}^{{commit}}").as_str())?
.object()?
.into_commit(),
None => repo.head_commit()?,
};
let mut platform = commit
.name_rev()
.names(if tags {
gix::commit::name_rev::SelectRef::AllTags
} else {
gix::commit::name_rev::SelectRef::AllRefs
})
.id_as_fallback(always);

for pattern in refs {
platform = platform.include_ref(pattern);
}
for pattern in exclude {
platform = platform.exclude_ref(pattern);
}

match platform.try_format()? {
Some(format) => {
if name_only {
let mut name = format.to_string();
if tags {
name = name.strip_prefix("tags/").unwrap_or(&name).to_owned();
}
writeln!(out, "{name}")?;
} else {
let id_or_spec = rev_spec.map_or_else(|| commit.id.to_string(), ToOwned::to_owned);
writeln!(out, "{id_or_spec} {format}")?;
}
}
None if no_undefined => anyhow::bail!("Cannot describe '{}'", commit.id),
None if name_only => writeln!(out, "undefined")?,
None => {
let id_or_spec = rev_spec.map_or_else(|| commit.id.to_string(), ToOwned::to_owned);
writeln!(out, "{id_or_spec} undefined")?;
}
}

Ok(())
}

pub mod describe {
#[derive(Debug, Clone)]
pub struct Options {
Expand All @@ -154,3 +214,15 @@ pub mod describe {
pub dirty_suffix: Option<String>,
}
}

pub mod name_rev {
#[derive(Debug, Clone)]
pub struct Options {
pub tags: bool,
pub refs: Vec<String>,
pub exclude: Vec<String>,
pub always: bool,
pub no_undefined: bool,
pub name_only: bool,
}
}
5 changes: 4 additions & 1 deletion gix-revision/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ rust-version = "1.85"
doctest = false

[features]
default = ["describe", "merge_base"]
default = ["describe", "merge_base", "name_rev"]
## Enable support for the SHA-1 hash by enabling the respective feature in the `gix-hash` crate.
sha1 = ["gix-hash/sha1"]
## Enable support for the SHA-256 hash by enabling the respective feature in the `gix-hash` crate.
Expand All @@ -24,6 +24,9 @@ sha256 = ["gix-hash/sha256"]
## `git describe` functionality
describe = ["dep:gix-trace", "dep:gix-hashtable"]

## `git name-rev` functionality
name_rev = ["dep:gix-trace"]

## `git merge-base` functionality
merge_base = ["dep:gix-trace", "dep:bitflags"]

Expand Down
5 changes: 5 additions & 0 deletions gix-revision/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ pub mod describe;
#[cfg(feature = "describe")]
pub use describe::function::describe;
///
#[cfg(feature = "name_rev")]
pub mod name_rev;
#[cfg(feature = "name_rev")]
pub use name_rev::function::name_rev;
///
#[allow(clippy::empty_docs)]
#[cfg(feature = "merge_base")]
pub mod merge_base;
Expand Down
Loading
Loading