You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
94 lines
2.1 KiB
94 lines
2.1 KiB
//! Raw representation wrappers for libgit2
|
|
|
|
mod branch;
|
|
pub use branch::RawBranch;
|
|
|
|
mod branch_walk;
|
|
mod tree_walk;
|
|
|
|
use crate::{Branch, BranchCommit};
|
|
use git2::{self, Oid, Repository};
|
|
|
|
pub type RawResult<T> = Result<T, RawError>;
|
|
|
|
/// The hex ID of a commit
|
|
#[derive(Debug)]
|
|
pub struct HashId(String);
|
|
|
|
impl From<Oid> for HashId {
|
|
fn from(o: Oid) -> Self {
|
|
Self(o.to_string())
|
|
}
|
|
}
|
|
|
|
impl From<HashId> for Oid {
|
|
fn from(hid: HashId) -> Self {
|
|
Oid::from_str(hid.0.as_str()).expect(&format!(
|
|
"Tried turning an invalid HashId variant into an Oid: {:?}",
|
|
hid
|
|
))
|
|
}
|
|
}
|
|
|
|
impl<'any> From<&'any HashId> for Oid {
|
|
fn from(hid: &'any HashId) -> Self {
|
|
Oid::from_str(hid.0.as_str()).expect(&format!(
|
|
"Tried turning an invalid HashId variant into an Oid: {:?}",
|
|
hid
|
|
))
|
|
}
|
|
}
|
|
|
|
|
|
/// An error abstraction for raw git operations
|
|
#[derive(Debug)]
|
|
pub enum RawError {
|
|
AllBad,
|
|
}
|
|
|
|
impl From<git2::Error> for RawError {
|
|
fn from(_: git2::Error) -> Self {
|
|
Self::AllBad
|
|
}
|
|
}
|
|
|
|
/// Wrap a libgit2 repository to provide an API fascade
|
|
pub struct RawRepository {
|
|
pub inner: Repository,
|
|
}
|
|
|
|
impl RawRepository {
|
|
pub fn open(path: &str) -> RawResult<Self> {
|
|
Ok(Self {
|
|
inner: Repository::open(path)?,
|
|
})
|
|
}
|
|
|
|
/// Parse branch data from repository
|
|
///
|
|
/// ## Panics
|
|
///
|
|
/// If there is an error around getting the name, or head commit.
|
|
pub fn parse_branches(&self) -> RawResult<Vec<RawBranch>> {
|
|
Ok(self
|
|
.inner
|
|
.branches(None)?
|
|
.into_iter()
|
|
.filter_map(|e| e.ok())
|
|
.map(|(branch, _)| {
|
|
let name = branch.name().unwrap().unwrap().into();
|
|
let head = branch.get().peel_to_commit().unwrap().id().into();
|
|
|
|
RawBranch { name, head }
|
|
})
|
|
.collect())
|
|
}
|
|
|
|
/// Get the files touched by a commit
|
|
pub fn get_files_for(&self, id: HashId) -> RawResult<Vec<()>> {
|
|
let c = self.inner.find_commit(id.into())?;
|
|
let tree = c.tree()?;
|
|
|
|
todo!()
|
|
}
|
|
}
|
|
|