This commit is contained in:
Hubert 2021-07-16 09:07:08 +02:00
parent 5f89a95587
commit ecd0837490
2 changed files with 66 additions and 29 deletions

View File

@ -1,15 +1,50 @@
use std::fmt::{Display, Formatter, Result}; use std::fmt::{Display, Formatter};
use git2::{Reference, Commit, Tree, ObjectType, Repository, Oid};
use std::ops::Add;
pub struct GitRepo {}
pub struct GitRepo {
git2 : Repository,
}
impl GitRepo { impl GitRepo {
pub fn new() -> GitRepo { pub fn new(path : &str) -> Result<GitRepo, git2::Error> {
GitRepo{} Repository::open(path).map(|git2| GitRepo{git2})
} }
pub async fn browse(&self, commit : &GitCommit, dir: &GitBrowseDir) -> Vec<GitBrowseEntry> { pub fn get_root_tree<'a, 'b> (&'a self, commit : &'b GitCommit) -> Result<GitBrowseDir<'a>, git2::Error> {
vec!(GitBrowseEntry::EGitDir(GitBrowseDir("src/".to_string())), GitBrowseEntry::EGitFile(GitBrowseFile("pom.xml".to_string()))) let oid = Oid::from_str(&commit.0)?;
let commit = self.git2.find_commit(oid)?;
let tree = commit.tree()?;
let fullname = "/".to_string();
Ok(GitBrowseDir{ fullname, tree })
}
pub async fn browse<'a>(&'a self, dir: &GitBrowseDir<'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(GitBrowseDir{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(GitBrowseFile(fullname)));
}
ObjectType::Tag => {Err(git2::Error::from_str("tree entry cannot be of kind tag"))?;}
}
};
}
return Ok(res);
} }
} }
@ -24,7 +59,7 @@ impl GitCommit {
} }
impl Display for GitCommit { impl Display for GitCommit {
fn fmt(&self, f: &mut Formatter<'_>) -> Result { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0) write!(f, "{}", self.0)
} }
} }
@ -32,34 +67,31 @@ impl Display for GitCommit {
#[derive(Debug)] #[derive(Debug)]
pub struct GitBrowseFile (String); pub struct GitBrowseFile (String);
#[derive(Debug)] #[derive(Debug)]
pub struct GitBrowseDir (String); pub struct GitBrowseDir<'a>{
fullname: String,
impl GitBrowseDir { tree : git2::Tree<'a>,
pub fn new(s : String) -> GitBrowseDir {
GitBrowseDir(s)
}
} }
impl Display for GitBrowseFile { impl Display for GitBrowseFile {
fn fmt(&self, f: &mut Formatter<'_>) -> Result { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0) write!(f, "{}", self.0)
} }
} }
impl Display for GitBrowseDir { impl Display for GitBrowseDir<'_> {
fn fmt(&self, f: &mut Formatter<'_>) -> Result { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0) write!(f, "{}", self.fullname)
} }
} }
#[derive(Debug)] #[derive(Debug)]
pub enum GitBrowseEntry { pub enum GitBrowseEntry<'a> {
EGitFile(GitBrowseFile), EGitFile(GitBrowseFile),
EGitDir(GitBrowseDir) EGitDir(GitBrowseDir<'a>)
} }
impl Display for GitBrowseEntry { impl Display for GitBrowseEntry<'_> {
fn fmt(&self, f: &mut Formatter<'_>) -> Result { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self { match self {
GitBrowseEntry::EGitFile(file) => {write!(f, "{}", file)} GitBrowseEntry::EGitFile(file) => {write!(f, "{}", file)}
GitBrowseEntry::EGitDir(dir) => {write!(f, "{}", dir)} GitBrowseEntry::EGitDir(dir) => {write!(f, "{}", dir)}

View File

@ -35,7 +35,7 @@ use actix_web::client::PayloadError;
use crate::reader::ToStream; use crate::reader::ToStream;
use crate::writer::Writer; use crate::writer::Writer;
use crate::gitust::Gitust; use crate::gitust::Gitust;
use git2::{Reference, Commit, Tree}; use git2::ObjectType;
#[derive(Template)] #[derive(Template)]
#[template(path = "hello.html")] #[template(path = "hello.html")]
@ -142,17 +142,15 @@ async fn git_main(
None => {"/".to_string()} None => {"/".to_string()}
Some(s) => {s} Some(s) => {s}
}; };
let repo = GitRepo::new(); let commit = GitCommit::new(commitname);
let owner = Owner { name : owner};
let repo = Repository {name : reponame, owner};
let user = User { name : "Hubert".to_string()};
let gitrepo = GitRepo::new(format!("{}/{}/{}.git", &gitust.repo_root_path, &repo.owner.name, &repo.name).as_str())?;
let path : Vec<(String, String)> = rootname.split("/").map_accum("/git/".to_string() + &owner + "/" + &reponame + "/" + &commitname, |str_ref, b| { let path : Vec<(String, String)> = rootname.split("/").map_accum("/git/".to_string() + &owner + "/" + &reponame + "/" + &commitname, |str_ref, b| {
let href = b + "/" + str_ref; let href = b + "/" + str_ref;
((str_ref.to_string(), href.clone()), href) ((str_ref.to_string(), href.clone()), href)
}).collect(); }).collect();
let commit = GitCommit::new(commitname);
let root = GitBrowseDir::new(rootname);
let browse = repo.browse(&commit, &root).await;
let owner = Owner { name : owner};
let repo = Repository {name : reponame, owner};
let user = User { name : "Hubert".to_string()};
let repogit = match git2::Repository::open(format!("{}/{}/{}.git", &gitust.repo_root_path, &repo.owner.name, &repo.name)) { let repogit = match git2::Repository::open(format!("{}/{}/{}.git", &gitust.repo_root_path, &repo.owner.name, &repo.name)) {
Ok(repo) => repo, Ok(repo) => repo,
Err(e) => panic!("failed to open: {}", e), Err(e) => panic!("failed to open: {}", e),
@ -172,7 +170,14 @@ async fn git_main(
for entry in tree.iter() { for entry in tree.iter() {
println!("{:?}", entry.name()); println!("{:?}", entry.name());
println!("{:?}", entry.kind()); println!("{:?}", entry.kind());
if entry.kind() == Some(ObjectType::Tree) {
let subtree = repogit.find_tree(entry.id()).expect("this pust be a tree");
println!("{:?}", subtree);
} }
}
let root = gitrepo.get_root_tree(&commit)?;
// let root = GitBrowseDir::new(rootname);
let browse = gitrepo.browse(&root).await;
GitMainTemplate { repo, browse : browse, root : path, user_opt : Some(user)} GitMainTemplate { repo, browse : browse, root : path, user_opt : Some(user)}
} }
//#[get("/git/{owner}/{repo}.git/{path:.*}")] //#[get("/git/{owner}/{repo}.git/{path:.*}")]