aboutsummaryrefslogtreecommitdiff
path: root/apps/servers/octopus/supergit/src/raw/mod.rs
blob: 80bff3528f3d3b283684650b82378aaf092ae558 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
//! 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!()
    }
}