use std::path::{Path, PathBuf};
use crate::RpoError;
#[cfg(feature = "backend-git2")]
pub mod git2;
#[cfg(feature = "backend-gix")]
pub mod gix;
#[cfg(all(feature = "backend-gix", not(feature = "backend-git2")))]
pub type DefaultBackend = self::gix::GixBackend;
#[cfg(all(feature = "backend-git2", not(feature = "backend-gix")))]
pub type DefaultBackend = self::git2::Git2Backend;
#[cfg(all(feature = "backend-gix", feature = "backend-git2"))]
pub type DefaultBackend = self::gix::GixBackend;
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct CommitId(
pub [u8; 20],
);
impl CommitId {
pub fn to_hex(&self) -> String {
let mut out = String::with_capacity(40);
for b in self.0 {
out.push_str(&format!("{b:02x}"));
}
out
}
pub fn short_hex(&self) -> String {
self.to_hex()[..7].to_string()
}
}
#[derive(Clone, Debug)]
pub struct Signature {
pub name: String,
pub email: String,
pub time_ms: i64,
}
#[derive(Clone, Debug)]
pub struct Commit {
pub id: CommitId,
pub author: Signature,
pub committer: Signature,
pub parent_ids: Vec<CommitId>,
pub message_subject: String,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ChangeKind {
Added,
Modified,
Deleted,
Renamed,
Copied,
TypeChange,
}
#[derive(Clone, Debug)]
pub struct FileChange {
pub path: PathBuf,
pub old_path: Option<PathBuf>,
pub kind: ChangeKind,
pub insertions: u64,
pub deletions: u64,
}
#[derive(Clone, Debug)]
pub struct BlameHunk {
pub start_line: u32,
pub line_count: u32,
pub commit_id: CommitId,
}
#[derive(Clone, Copy, Debug)]
pub struct WalkOptions {
pub first_parent_only: bool,
pub include_merges: bool,
}
pub trait GitBackend: Send + Sync {
fn open(path: &Path) -> Result<Self, RpoError>
where
Self: Sized;
fn head_commit(&self) -> Result<CommitId, RpoError>;
fn iter_commits<'a>(
&'a self,
opts: WalkOptions,
) -> Box<dyn Iterator<Item = Result<Commit, RpoError>> + 'a>;
fn diff_tree(
&self,
parent: Option<&CommitId>,
child: &CommitId,
) -> Result<Vec<FileChange>, RpoError>;
fn list_tree_paths(&self, commit: &CommitId) -> Result<Vec<PathBuf>, RpoError>;
fn blame_file(&self, commit: &CommitId, path: &Path) -> Result<Vec<BlameHunk>, RpoError>;
fn resolve_rev(&self, rev: &str) -> Result<CommitId, RpoError>;
fn commit_meta(&self, id: &CommitId) -> Result<Commit, RpoError>;
fn tags(&self) -> Result<Vec<(String, CommitId)>, RpoError>;
fn mailmap_bytes(&self) -> Result<Option<Vec<u8>>, RpoError>;
fn gitattributes_bytes(&self) -> Result<Option<Vec<u8>>, RpoError>;
fn thread_handle(&self) -> Result<Self, RpoError>
where
Self: Sized;
}