This commit is contained in:
Hubert
2021-07-18 07:46:14 +02:00
parent a2dcab2ea8
commit a3bf4afe51
8 changed files with 229 additions and 143 deletions

View File

@@ -1,88 +1,10 @@
use std::fmt::{Display, Formatter};
use git2::{Reference, Commit, Tree, ObjectType, Repository, Oid};
use std::ops::Add;
use git2::{Commit, ObjectType, Oid, Reference, Repository, Tree};
pub struct GitRepo {
git2 : Repository,
}
impl GitRepo {
pub fn new(path : &str) -> Result<GitRepo, git2::Error> {
Repository::open(path).map(|git2| GitRepo{git2})
}
pub fn get_root_tree<'a, 'b> (&'a self, commit : &'b GitCommit) -> Result<GitDir<'a>, git2::Error> {
let oid = Oid::from_str(&commit.0)?;
let commit = self.git2.find_commit(oid)?;
let tree = commit.tree()?;
let fullname = "/".to_string();
Ok(GitDir { fullname, tree })
}
pub async fn browse<'a>(&'a self, dir: &GitDir<'a>) -> Result<Vec<GitBrowseEntry<'a>>, git2::Error> {
let mut res = Vec::new();
for entry in dir.tree.iter() {
match entry.kind() {
None => {Err(git2::Error::from_str("each tree entry must be well defined"))?;}
Some(kind) => match kind {
ObjectType::Any => {Err(git2::Error::from_str("tree entry cannot be of kind Any"))?;}
ObjectType::Commit => {Err(git2::Error::from_str("tree entry cannot be of kind Commit"))?;}
ObjectType::Tree => {
let name = entry.name().ok_or(git2::Error::from_str("entry must have valid utf8 name"))?;
let fullname = dir.fullname.clone().add(name);
let subtree = self.git2.find_tree(entry.id())?;
res.push(GitBrowseEntry::EGitDir(GitDir {fullname, tree : subtree}));
}
ObjectType::Blob => {
let name = entry.name().ok_or(git2::Error::from_str("entry must have valid utf8 name"))?;
let fullname = dir.fullname.clone().add(name);
res.push(GitBrowseEntry::EGitFile(GitFile(fullname)));
}
ObjectType::Tag => {Err(git2::Error::from_str("tree entry cannot be of kind tag"))?;}
}
};
}
return Ok(res);
}
}
#[derive(Debug)]
pub struct GitCommit (String);
impl GitCommit {
pub fn new(s : String) -> GitCommit {
GitCommit(s)
}
}
impl Display for GitCommit {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
#[derive(Debug)]
pub struct GitFile(String);
#[derive(Debug)]
pub struct GitDir<'a>{
fullname: String,
tree : git2::Tree<'a>,
}
impl Display for GitFile {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
impl Display for GitDir<'_> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.fullname)
}
}
use crate::gitdir::GitDir;
use crate::gitfile::GitFile;
#[derive(Debug)]
pub enum GitBrowseEntry<'a> {