diff options
author | Mx Kookie <kookie@spacekookie.de> | 2020-11-01 18:25:19 +0100 |
---|---|---|
committer | Mx Kookie <kookie@spacekookie.de> | 2020-12-21 05:19:28 +0100 |
commit | eb1b781fabde92081d9e8ba5ede17cbfbf4f7edf (patch) | |
tree | 7f9b9470622f65bf97bdad84a4a10c2fb285039e /apps/servers/octopus/supergit/src/raw/mod.rs | |
parent | eaf889ee80ddfdb0b89efe9d371369f715dccd89 (diff) |
apps/servers/octopus: implement initial branch parsing
Diffstat (limited to 'apps/servers/octopus/supergit/src/raw/mod.rs')
-rw-r--r-- | apps/servers/octopus/supergit/src/raw/mod.rs | 65 |
1 files changed, 57 insertions, 8 deletions
diff --git a/apps/servers/octopus/supergit/src/raw/mod.rs b/apps/servers/octopus/supergit/src/raw/mod.rs index 7bf6c0a396a4..80bff3528f3d 100644 --- a/apps/servers/octopus/supergit/src/raw/mod.rs +++ b/apps/servers/octopus/supergit/src/raw/mod.rs @@ -1,13 +1,45 @@ //! Raw representation wrappers for libgit2 +mod branch; +pub use branch::RawBranch; + mod branch_walk; mod tree_walk; use crate::{Branch, BranchCommit}; -use git2::{self, Repository}; +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 { @@ -20,15 +52,9 @@ impl From<git2::Error> for RawError { } } -/// Represent a raw branch -pub struct RawBranch { - name: String, - head: String, -} - /// Wrap a libgit2 repository to provide an API fascade pub struct RawRepository { - inner: Repository, + pub inner: Repository, } impl RawRepository { @@ -39,7 +65,30 @@ impl RawRepository { } /// 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!() } } |