use crate::error::Result;
use crate::objects::ObjectId;
use crate::rev_parse::{self, TreeishBlobAtPath};
use crate::revwalk::RevWalk;
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Peel {
#[default]
Any,
Commit,
Tree,
Blob,
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum IndexDwim {
#[default]
On,
Off,
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum ResolvePurpose {
#[default]
Revision,
RangeEnd,
CheckoutNoGuess,
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub struct ResolveOptions {
pub peel: Peel,
pub index_dwim: IndexDwim,
pub purpose: ResolvePurpose,
}
impl ResolveOptions {
#[must_use]
pub fn with_peel(mut self, peel: Peel) -> Self {
self.peel = peel;
self
}
#[must_use]
pub fn with_index_dwim(mut self, index_dwim: IndexDwim) -> Self {
self.index_dwim = index_dwim;
self
}
#[must_use]
pub fn with_purpose(mut self, purpose: ResolvePurpose) -> Self {
self.purpose = purpose;
self
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum AbbrevLength {
#[default]
Configured,
AtLeast(usize),
}
impl<'repo> RevWalk<'repo> {
pub fn resolve(&self, spec: &str, options: &ResolveOptions) -> Result<ObjectId> {
rev_parse::resolve_revision_with_options(self.repository(), spec, options)
}
pub fn peel_to_commit(&self, oid: ObjectId) -> Result<ObjectId> {
rev_parse::peel_to_commit_for_merge_base(self.repository(), oid)
}
pub fn peel_to_tree(&self, oid: ObjectId) -> Result<ObjectId> {
rev_parse::peel_to_tree(self.repository(), oid)
}
pub fn blob_at_path(&self, spec: &str) -> Result<TreeishBlobAtPath> {
rev_parse::resolve_treeish_blob_at_path(self.repository(), spec)
}
pub fn abbreviate(&self, oid: ObjectId, length: AbbrevLength) -> Result<String> {
let repo = self.repository();
let min_len = match length {
AbbrevLength::AtLeast(len) => len,
AbbrevLength::Configured => {
let config = repo.config().snapshot()?;
crate::commit_pretty::resolve_core_abbrev_len(&config, repo.odb())?
}
};
rev_parse::abbreviate_object_id(repo, oid, min_len)
}
pub fn abbrev_matches(&self, prefix: &str) -> Result<Vec<ObjectId>> {
rev_parse::list_all_abbrev_matches(self.repository(), prefix)
}
#[must_use]
pub fn symbolic_full_name(&self, spec: &str) -> Option<String> {
rev_parse::symbolic_full_name(self.repository(), spec)
}
pub fn upstream_symbolic_name(&self, spec: &str) -> Result<String> {
rev_parse::resolve_upstream_symbolic_name(self.repository(), spec)
}
}