use std::collections::{HashMap, HashSet, VecDeque};
use gix::ObjectId;
use time::OffsetDateTime;
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum Error {
#[error("workspace is dirty (use --dirty to produce a dirty version)")]
DirtyWorkspace,
#[error("{subject} is not on the default branch ({branch})")]
NotOnDefaultBranch { subject: String, branch: String },
#[error("{subject} has no common history with the default branch ({branch})")]
NotTraceable { subject: String, branch: String },
#[error("cannot determine default branch")]
NoDefaultBranch,
#[error("committer dates are not non-decreasing (found {earlier} after {head} in history)")]
DecreasingDate { head: String, earlier: String },
#[error("no commits in repository")]
EmptyRepository,
#[error("not a git repository")]
NotARepository,
#[error("not a git repository: {} is not a valid gitfile", .0.display())]
InvalidGitFile(std::path::PathBuf),
#[error("unsupported object format {0}: only SHA-1 and SHA-256 repositories are supported")]
UnsupportedObjectFormat(String),
#[error("--prefix must not contain a newline")]
InvalidPrefix,
#[error("--short is only valid in reverse lookup mode")]
ShortRequiresReverse,
#[error("not a gitcalver version or git revision: {0}")]
RevisionNotFound(String),
#[error("version {version} is missing required prefix \"{prefix}\"")]
MissingPrefix { version: String, prefix: String },
#[error("invalid date in version: {0}")]
InvalidVersionDate(String),
#[error("invalid count in version: {0}")]
InvalidVersionCount(String),
#[error("version not found: {0}")]
VersionNotFound(String),
#[error("local history cannot prove the result: {0}")]
IncompleteHistory(String),
#[error("{}", error_chain(.0.as_ref()))]
Git(Box<dyn std::error::Error + Send + Sync>),
}
pub struct Options<'a> {
pub dir: &'a std::path::Path,
pub target: Option<&'a str>,
pub prefix: &'a str,
pub dirty_suffix: Option<&'a str>,
pub include_dirty_hash: bool,
pub branch: Option<&'a str>,
pub remote: Option<&'a str>,
pub short: bool,
}
impl Default for Options<'_> {
fn default() -> Self {
Self {
dir: std::path::Path::new("."),
target: None,
prefix: "",
dirty_suffix: None,
include_dirty_hash: true,
branch: None,
remote: None,
short: false,
}
}
}
pub fn run(opts: &Options<'_>) -> Result<String, Error> {
if opts.prefix.contains('\n') {
return Err(Error::InvalidPrefix);
}
let repo = open_repo(opts.dir)?;
if graft_file_path(&repo).is_file() {
return Err(Error::IncompleteHistory(format!(
"commit graft file is not supported: {}",
graft_file_path(&repo).display()
)));
}
if let Some(target) = opts.target
&& let Some((date_str, n)) = reverse_input(target, opts.prefix)?
{
return reverse(&repo, opts, date_str, n);
}
forward(&repo, opts)
}
fn open_repo(dir: &std::path::Path) -> Result<gix::Repository, Error> {
const NO_REPLACE_OVERRIDES: [&str; 2] = [
"core.useReplaceRefs=true",
"gitoxide.objects.replaceRefBase=refs/gitcalver/replace-disabled/",
];
let mut trust_map = gix::sec::trust::Mapping::<gix::open::Options>::default();
trust_map.full = trust_map.full.config_overrides(NO_REPLACE_OVERRIDES);
trust_map.reduced = trust_map.reduced.config_overrides(NO_REPLACE_OVERRIDES);
trust_map.full.permissions.env.objects = gix::sec::Permission::Deny;
trust_map.reduced.permissions.env.objects = gix::sec::Permission::Deny;
let options = gix::discover::upwards::Options {
match_ceiling_dir_or_error: false,
..Default::default()
};
let repo = gix::ThreadSafeRepository::discover_with_environment_overrides_opts(
dir, options, trust_map,
)
.map_err(open_error)?
.to_thread_local();
if std::env::var_os("GIT_DIR").is_none() {
let ceilings = std::env::var_os("GIT_CEILING_DIRECTORIES");
check_discovery(dir, &repo, ceilings.as_deref())?;
}
Ok(repo)
}
fn open_error(error: gix::Error) -> Error {
use gix::discover::upwards::Error as Upwards;
use gix::error::{Class, Message, MetadataValue};
for values in error.metadata() {
if values.get("key") == Some(&MetadataValue::from("extensions.objectFormat"))
&& let Some(MetadataValue::Bytes(value)) = values.get("input")
{
return Error::UnsupportedObjectFormat(String::from_utf8_lossy(value).into_owned());
}
}
let not_a_repository = error
.iter_errors()
.filter_map(|cause| cause.downcast_ref::<Message>())
.any(|message| {
message.class == Some(Class::NotFound)
&& message
.message
.ends_with("does not appear to be a git repository")
});
if not_a_repository
|| matches!(
error.downcast_any_ref::<Upwards>(),
Some(
Upwards::NoGitRepository { .. }
| Upwards::NoGitRepositoryWithinCeiling { .. }
| Upwards::NoGitRepositoryWithinFs { .. }
)
)
{
return Error::NotARepository;
}
git_err(error)
}
fn check_discovery(
dir: &std::path::Path,
repo: &gix::Repository,
ceiling_dirs: Option<&std::ffi::OsStr>,
) -> Result<(), Error> {
let mut roots = vec![std::fs::canonicalize(repo.git_dir()).map_err(git_err)?];
if let Some(workdir) = repo.workdir() {
roots.push(std::fs::canonicalize(workdir).map_err(git_err)?);
}
let ceilings: Vec<_> = ceiling_dirs
.into_iter()
.flat_map(|dirs| std::env::split_paths(dirs))
.filter(|entry| entry.is_absolute())
.filter_map(|entry| std::fs::canonicalize(entry).ok())
.collect();
let start = std::fs::canonicalize(dir).map_err(git_err)?;
let mut skipped = None;
for ancestor in start.ancestors() {
if ancestor != start && ceilings.iter().any(|ceiling| ceiling == ancestor) {
return Err(Error::NotARepository);
}
if roots.iter().any(|root| root == ancestor) {
return skipped.map_or(Ok(()), |path| Err(Error::InvalidGitFile(path)));
}
let candidate = ancestor.join(".git");
if skipped.is_none() && std::fs::metadata(&candidate).is_ok_and(|m| m.is_file()) {
skipped = Some(candidate);
}
}
Ok(())
}
fn graft_file_path(repo: &gix::Repository) -> std::path::PathBuf {
repo.common_dir().join("info/grafts")
}
fn forward(repo: &gix::Repository, opts: &Options<'_>) -> Result<String, Error> {
if opts.short {
return Err(Error::ShortRequiresReverse);
}
let is_head = opts.target.is_none();
let subject = opts.target.unwrap_or("HEAD");
let target_id = resolve_target(repo, subject)?;
let remote = opts.remote.unwrap_or("origin");
let branch_name = detect_branch_name(repo, opts.branch, remote)?;
let branch_tip = resolve_branch_tip(repo, &branch_name, remote)?;
let anchor = locate_anchor(repo, target_id, branch_tip, subject, &branch_name)?;
let (version_commit, off_branch) = match anchor {
Anchor::OnChain => (target_id, false),
Anchor::OffChain(anchor_id) => (anchor_id, true),
};
let workspace_dirty = if is_head && repo.workdir().is_some() {
check_dirty(repo)?
} else {
false
};
let dirty = workspace_dirty || off_branch;
if dirty && opts.dirty_suffix.is_none() {
return if off_branch {
Err(Error::NotOnDefaultBranch {
subject: subject.to_owned(),
branch: branch_name,
})
} else {
Err(Error::DirtyWorkspace)
};
}
let (date, count) = cohort(repo, version_commit)?;
let hash = if dirty && opts.include_dirty_hash {
short_hash(target_id)
} else {
String::new()
};
Ok(format_version(
opts.prefix,
&date,
count,
dirty,
opts.dirty_suffix.unwrap_or(""),
&hash,
))
}
fn resolve_target(repo: &gix::Repository, subject: &str) -> Result<ObjectId, Error> {
let id = if subject == "HEAD" {
head_id(repo)?
} else {
match repo.rev_parse_single(expand_short_id(repo, subject)?.as_ref()) {
Ok(id) => id.detach(),
Err(_) => ObjectId::from_hex(subject.as_bytes())
.ok()
.filter(|id| id.kind() == repo.object_hash())
.or_else(|| try_resolve_ref(repo, subject))
.ok_or_else(|| Error::RevisionNotFound(subject.to_owned()))?,
}
};
peel_to_commit(repo, id, subject)
}
fn expand_short_id<'a>(
repo: &gix::Repository,
rev: &'a str,
) -> Result<std::borrow::Cow<'a, str>, Error> {
if repo.object_hash() != gix::hash::Kind::Sha256 {
return Ok(std::borrow::Cow::Borrowed(rev));
}
let hex_len = rev.bytes().take_while(u8::is_ascii_hexdigit).count();
let (hex, suffix) = rev.split_at(hex_len);
let hash_len = repo.object_hash().len_in_hex();
if !(gix::hash::Prefix::MIN_HEX_LEN..hash_len).contains(&hex_len)
|| !(suffix.is_empty() || suffix.starts_with(['^', '~', ':']))
|| repo.try_find_reference(hex).map_err(git_err)?.is_some()
{
return Ok(std::borrow::Cow::Borrowed(rev));
}
let padded = format!("{hex:0<hash_len$}");
let id = ObjectId::from_hex(padded.as_bytes()).expect("validated hexadecimal prefix");
let prefix = gix::hash::Prefix::new(&id, hex_len).expect("validated prefix length");
let Some(Ok(full)) = repo.objects.lookup_prefix(prefix, None).map_err(git_err)? else {
return Err(Error::RevisionNotFound(rev.to_owned()));
};
Ok(std::borrow::Cow::Owned(format!("{full}{suffix}")))
}
fn head_id(repo: &gix::Repository) -> Result<ObjectId, Error> {
use gix::head::Kind;
use gix::prelude::ReferenceExt as _;
match repo.head().map_err(git_err)?.kind {
Kind::Unborn(_) => Err(Error::EmptyRepository),
Kind::Detached { target, peeled } => Ok(peeled.unwrap_or(target)),
Kind::Symbolic(referent) => {
follow_to_id(referent.attach(repo)).ok_or(Error::EmptyRepository)
}
}
}
fn peel_to_commit(
repo: &gix::Repository,
mut id: ObjectId,
subject: &str,
) -> Result<ObjectId, Error> {
let mut seen = HashSet::new();
loop {
if !seen.insert(id) {
return Err(Error::RevisionNotFound(subject.to_owned()));
}
let object = repo.try_find_object(id).map_err(git_err)?.ok_or_else(|| {
Error::IncompleteHistory(format!("{subject} is missing from local history"))
})?;
match object.kind {
gix::object::Kind::Commit => return Ok(id),
gix::object::Kind::Tag => {
id = object.to_tag_ref_iter().target_id().map_err(git_err)?;
}
gix::object::Kind::Tree | gix::object::Kind::Blob => {
return Err(Error::RevisionNotFound(subject.to_owned()));
}
}
}
}
fn reverse(
repo: &gix::Repository,
opts: &Options<'_>,
date_str: &str,
n: usize,
) -> Result<String, Error> {
let remote = opts.remote.unwrap_or("origin");
let branch_name = detect_branch_name(repo, opts.branch, remote)?;
let branch_tip = resolve_branch_tip(repo, &branch_name, remote)?;
let mut members = date_block_members(repo, branch_tip, date_str)?;
members.reverse();
for member in members {
let (_, count) = cohort(repo, member)?;
match count.cmp(&n) {
std::cmp::Ordering::Equal => {
return Ok(if opts.short {
short_hash(member)
} else {
member.to_string()
});
}
std::cmp::Ordering::Greater => break,
std::cmp::Ordering::Less => {}
}
}
Err(Error::VersionNotFound(
opts.target.unwrap_or_default().to_owned(),
))
}
fn cohort(repo: &gix::Repository, start: ObjectId) -> Result<(String, usize), Error> {
let start_commit = repo.find_commit(start).map_err(git_err)?;
let date = committer_date(&start_commit)?;
let shallow = shallow_cuts(repo)?;
let mut visited = HashSet::new();
visited.insert(start);
let mut queue = VecDeque::new();
queue.push_back(start_commit);
let mut count = 0usize;
while let Some(commit) = queue.pop_front() {
count += 1;
if commit.parent_ids().next().is_some() && shallow.contains(&commit.id().detach()) {
return Err(Error::IncompleteHistory(format!(
"local history ended inside the {date} date block"
)));
}
for parent in commit.parent_ids() {
let parent_id = parent.detach();
if !visited.insert(parent_id) {
continue;
}
let parent_commit = repo.find_commit(parent_id).map_err(|_| {
Error::IncompleteHistory(format!(
"local history ended inside the {date} date block"
))
})?;
let parent_date = committer_date(&parent_commit)?;
match parent_date.as_str().cmp(date.as_str()) {
std::cmp::Ordering::Equal => queue.push_back(parent_commit),
std::cmp::Ordering::Less => {}
std::cmp::Ordering::Greater => {
return Err(Error::DecreasingDate {
head: date,
earlier: parent_date,
});
}
}
}
}
Ok((date, count))
}
fn date_block_members(
repo: &gix::Repository,
chain_tip: ObjectId,
target_date: &str,
) -> Result<Vec<ObjectId>, Error> {
let mut members = Vec::new();
let mut current = chain_tip;
let mut prev_date: Option<String> = None;
let shallow = shallow_cuts(repo)?;
loop {
let commit = repo.find_commit(current).map_err(|_| {
Error::IncompleteHistory(
"local history ended before the date block's boundary could be proved".to_owned(),
)
})?;
let commit_date = committer_date(&commit)?;
if let Some(ref prev) = prev_date
&& commit_date.as_str() > prev.as_str()
{
return Err(Error::DecreasingDate {
head: prev.clone(),
earlier: commit_date,
});
}
if commit_date == target_date {
members.push(current);
} else if commit_date.as_str() < target_date {
return Ok(members);
}
prev_date = Some(commit_date);
if first_parent_id(&commit).is_some() && shallow.contains(¤t) {
return Err(Error::IncompleteHistory(
"local history ended before the date block's boundary could be proved".to_owned(),
));
}
match first_parent_id(&commit) {
Some(parent) => current = parent,
None => return Ok(members),
}
}
}
fn shallow_cuts(repo: &gix::Repository) -> Result<HashSet<ObjectId>, Error> {
Ok(repo
.shallow_commits()
.map_err(git_err)?
.map(|cuts| cuts.iter().copied().collect())
.unwrap_or_default())
}
fn committer_date(commit: &gix::Commit<'_>) -> Result<String, Error> {
let time = commit.time().map_err(git_err)?;
epoch_to_date(time.seconds)
}
fn first_parent_id(commit: &gix::Commit<'_>) -> Option<ObjectId> {
commit.parent_ids().next().map(gix::Id::detach)
}
struct Chain {
members: Vec<ObjectId>,
index: HashMap<ObjectId, usize>,
incomplete: bool,
}
fn build_chain(repo: &gix::Repository, tip: ObjectId) -> Chain {
let mut members = Vec::new();
let mut index = HashMap::new();
let mut incomplete = false;
let mut current = tip;
loop {
if let Ok(commit) = repo.find_commit(current) {
index.insert(current, members.len());
members.push(current);
match first_parent_id(&commit) {
Some(parent) => current = parent,
None => break,
}
} else {
incomplete = true;
break;
}
}
Chain {
members,
index,
incomplete,
}
}
enum Anchor {
OnChain,
OffChain(ObjectId),
}
fn locate_anchor(
repo: &gix::Repository,
target: ObjectId,
branch_tip: ObjectId,
subject: &str,
branch_name: &str,
) -> Result<Anchor, Error> {
let chain = build_chain(repo, branch_tip);
let mut visited = HashSet::new();
visited.insert(target);
let mut queue = VecDeque::new();
queue.push_back(target);
let mut best: Option<usize> = None;
let mut walk_incomplete = false;
while let Some(id) = queue.pop_front() {
if let Some(&idx) = chain.index.get(&id) {
best = Some(best.map_or(idx, |current_best| current_best.min(idx)));
continue;
}
match repo.find_commit(id) {
Ok(commit) => {
for parent in commit.parent_ids() {
let parent_id = parent.detach();
if visited.insert(parent_id) {
queue.push_back(parent_id);
}
}
}
Err(_) => walk_incomplete = true,
}
}
match best {
Some(idx) if chain.members[idx] == target => Ok(Anchor::OnChain),
Some(_) if walk_incomplete => Err(Error::IncompleteHistory(format!(
"local history cannot prove {subject}'s newest reachable anchor on the default branch ({branch_name})"
))),
Some(idx) => Ok(Anchor::OffChain(chain.members[idx])),
None if walk_incomplete || chain.incomplete => Err(Error::IncompleteHistory(format!(
"local history cannot prove {subject}'s relationship to the default branch ({branch_name})"
))),
None => Err(Error::NotTraceable {
subject: subject.to_owned(),
branch: branch_name.to_owned(),
}),
}
}
fn detect_branch_name(
repo: &gix::Repository,
override_name: Option<&str>,
remote: &str,
) -> Result<String, Error> {
if let Some(name) = override_name {
return Ok(name.to_owned());
}
let remote_prefix = format!("refs/remotes/{remote}/");
let symbolic_head = format!("{remote_prefix}HEAD");
if let Ok(r) = repo.find_reference(symbolic_head.as_str())
&& let Some(target_name) = r.target().try_name()
&& let Some(short) = target_name
.as_bstr()
.to_string()
.strip_prefix(&remote_prefix)
{
return Ok(short.to_owned());
}
for prefix in [remote_prefix.as_str(), "refs/heads/"] {
for name in ["main", "master"] {
if try_resolve_ref(repo, &format!("{prefix}{name}")).is_some() {
return Ok(name.to_owned());
}
}
}
Err(Error::NoDefaultBranch)
}
fn resolve_branch_tip(repo: &gix::Repository, name: &str, remote: &str) -> Result<ObjectId, Error> {
try_resolve_ref(repo, &format!("refs/heads/{name}"))
.or_else(|| try_resolve_ref(repo, &format!("refs/remotes/{remote}/{name}")))
.ok_or(Error::NoDefaultBranch)
}
fn try_resolve_ref(repo: &gix::Repository, ref_name: &str) -> Option<ObjectId> {
follow_to_id(repo.find_reference(ref_name).ok()?)
}
fn follow_to_id(mut reference: gix::Reference<'_>) -> Option<ObjectId> {
reference.follow_to_object().ok().map(gix::Id::detach)
}
fn check_dirty(repo: &gix::Repository) -> Result<bool, Error> {
let mut changes = repo
.status(gix::progress::Discard)
.map_err(git_err)?
.tree_index_track_renames(gix::status::tree_index::TrackRenames::Disabled)
.index_worktree_rewrites(None)
.index_worktree_submodules(gix::status::Submodule::AsConfigured { check_dirty: true })
.into_iter(Vec::new())
.map_err(git_err)?;
Ok(changes.next().transpose().map_err(git_err)?.is_some())
}
fn short_hash(id: ObjectId) -> String {
id.to_string()[..7].to_owned()
}
fn epoch_to_date(seconds: gix::date::SecondsSinceUnixEpoch) -> Result<String, Error> {
let dt = OffsetDateTime::from_unix_timestamp(seconds).map_err(git_err)?;
Ok(format!(
"{:04}{:02}{:02}",
dt.year(),
dt.month() as u8,
dt.day()
))
}
fn git_err(e: impl std::error::Error + Send + Sync + 'static) -> Error {
Error::Git(Box::new(e))
}
fn error_chain(error: &(dyn std::error::Error + 'static)) -> String {
let mut message = error.to_string();
let mut last = message.clone();
let mut source = error.source();
while let Some(cause) = source {
let text = cause.to_string();
if !cause.is::<gix::error::ClassificationMarker>() && text != last {
message.push_str(": ");
message.push_str(&text);
last = text;
}
source = cause.source();
}
message
}
fn format_version(
prefix: &str,
date: &str,
n: usize,
dirty: bool,
dirty_string: &str,
hash: &str,
) -> String {
let mut version = format!("{prefix}{date}.{n}");
if dirty {
version.push_str(dirty_string);
if !hash.is_empty() {
version.push('.');
version.push_str(hash);
}
}
version
}
fn reverse_input<'a>(target: &'a str, prefix: &str) -> Result<Option<(&'a str, usize)>, Error> {
let stripped = target.strip_prefix(prefix);
let Some((date, count)) = split_version(stripped.unwrap_or(target)) else {
return Ok(None);
};
if stripped.is_none() {
return Err(Error::MissingPrefix {
version: target.to_owned(),
prefix: prefix.to_owned(),
});
}
if !is_gregorian_date(date) {
return Err(Error::InvalidVersionDate(target.to_owned()));
}
let n = count
.parse()
.map_err(|_| Error::InvalidVersionCount(target.to_owned()))?;
Ok(Some((date, n)))
}
fn split_version(s: &str) -> Option<(&str, &str)> {
let (date, count) = s.split_once('.')?;
let all_digits = |part: &str| part.bytes().all(|b| b.is_ascii_digit());
(date.len() == 8
&& all_digits(date)
&& !count.is_empty()
&& all_digits(count)
&& !count.starts_with('0'))
.then_some((date, count))
}
fn is_gregorian_date(date: &str) -> bool {
let year = date[..4].parse::<i32>();
let month = date[4..6]
.parse::<u8>()
.ok()
.and_then(|m| time::Month::try_from(m).ok());
let day = date[6..].parse::<u8>();
matches!(
(year, month, day),
(Ok(y), Some(m), Ok(d)) if y >= 1 && time::Date::from_calendar_date(y, m, d).is_ok()
)
}
#[derive(Debug, thiserror::Error)]
pub enum PatchError {
#[error("invalid TOML: {0}")]
Parse(Box<toml_edit::TomlError>),
#[error("manifest has no `[package]` section")]
MissingPackage,
#[error("workspace version inheritance not supported; set version in package manifest")]
WorkspaceInheritance,
#[error(
"manifest version `{existing}` differs from computed `{computed}`; refusing to overwrite"
)]
VersionMismatch { existing: String, computed: String },
#[error("manifest sets `publish = false`; refusing to overwrite")]
PublishFalse,
}
impl From<toml_edit::TomlError> for PatchError {
fn from(e: toml_edit::TomlError) -> Self {
Self::Parse(Box::new(e))
}
}
pub fn patch_manifest(toml_src: &str, version: &str) -> Result<String, PatchError> {
let mut doc: toml_edit::DocumentMut = toml_src.parse()?;
let package = doc
.get_mut("package")
.and_then(toml_edit::Item::as_table_like_mut)
.ok_or(PatchError::MissingPackage)?;
match package.get("version") {
None => {
package.insert("version", toml_edit::value(version));
}
Some(item) => match item.as_value().and_then(toml_edit::Value::as_str) {
Some(existing) if existing == version => {}
Some(existing) => {
return Err(PatchError::VersionMismatch {
existing: existing.to_owned(),
computed: version.to_owned(),
});
}
None => return Err(PatchError::WorkspaceInheritance),
},
}
match package.get("publish") {
None => {
package.insert("publish", toml_edit::value(true));
}
Some(item) => match item.as_value() {
Some(toml_edit::Value::Boolean(b)) if *b.value() => {}
Some(toml_edit::Value::Boolean(_)) => return Err(PatchError::PublishFalse),
Some(toml_edit::Value::Array(_)) => {}
_ => return Err(PatchError::WorkspaceInheritance),
},
}
Ok(doc.to_string())
}
#[cfg(test)]
mod tests;