Compare commits

..

No commits in common. "dc26021c43378170ff6070d632bf7ccd91fa457b" and "5f89a9558787c4208e95277efd0379d630216433" have entirely different histories.

2 changed files with 34 additions and 71 deletions

View File

@ -1,50 +1,15 @@
use std::fmt::{Display, Formatter}; use std::fmt::{Display, Formatter, Result};
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(path : &str) -> Result<GitRepo, git2::Error> { pub fn new() -> GitRepo {
Repository::open(path).map(|git2| GitRepo{git2}) GitRepo{}
} }
pub fn get_root_tree<'a, 'b> (&'a self, commit : &'b GitCommit) -> Result<GitBrowseDir<'a>, git2::Error> { pub async fn browse(&self, commit : &GitCommit, dir: &GitBrowseDir) -> Vec<GitBrowseEntry> {
let oid = Oid::from_str(&commit.0)?; vec!(GitBrowseEntry::EGitDir(GitBrowseDir("src/".to_string())), GitBrowseEntry::EGitFile(GitBrowseFile("pom.xml".to_string())))
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);
} }
} }
@ -59,7 +24,7 @@ impl GitCommit {
} }
impl Display for GitCommit { impl Display for GitCommit {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut Formatter<'_>) -> Result {
write!(f, "{}", self.0) write!(f, "{}", self.0)
} }
} }
@ -67,31 +32,34 @@ impl Display for GitCommit {
#[derive(Debug)] #[derive(Debug)]
pub struct GitBrowseFile (String); pub struct GitBrowseFile (String);
#[derive(Debug)] #[derive(Debug)]
pub struct GitBrowseDir<'a>{ pub struct GitBrowseDir (String);
fullname: String,
tree : git2::Tree<'a>, impl GitBrowseDir {
pub fn new(s : String) -> GitBrowseDir {
GitBrowseDir(s)
}
} }
impl Display for GitBrowseFile { impl Display for GitBrowseFile {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut Formatter<'_>) -> Result {
write!(f, "{}", self.0) write!(f, "{}", self.0)
} }
} }
impl Display for GitBrowseDir<'_> { impl Display for GitBrowseDir {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut Formatter<'_>) -> Result {
write!(f, "{}", self.fullname) write!(f, "{}", self.0)
} }
} }
#[derive(Debug)] #[derive(Debug)]
pub enum GitBrowseEntry<'a> { pub enum GitBrowseEntry {
EGitFile(GitBrowseFile), EGitFile(GitBrowseFile),
EGitDir(GitBrowseDir<'a>) EGitDir(GitBrowseDir)
} }
impl Display for GitBrowseEntry<'_> { impl Display for GitBrowseEntry {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut Formatter<'_>) -> 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::ObjectType; use git2::{Reference, Commit, Tree};
#[derive(Template)] #[derive(Template)]
#[template(path = "hello.html")] #[template(path = "hello.html")]
@ -130,7 +130,7 @@ async fn chunk() -> HttpResponse {
#[get("/git/{owner}/{repo}.git")] #[get("/git/{owner}/{repo}.git")]
async fn git_main( async fn git_main(
web::Path((ownername, reponame)): web::Path<(String, String)>, web::Path((owner, reponame)): web::Path<(String, String)>,
web::Query(GitWebQ{commit : commitnameopt, path : pathopt}) : web::Query<GitWebQ>, web::Query(GitWebQ{commit : commitnameopt, path : pathopt}) : web::Query<GitWebQ>,
gitust : web::Data<Gitust> gitust : web::Data<Gitust>
) -> impl Responder { ) -> impl Responder {
@ -142,15 +142,17 @@ async fn git_main(
None => {"/".to_string()} None => {"/".to_string()}
Some(s) => {s} Some(s) => {s}
}; };
let commit = GitCommit::new(commitname); let repo = GitRepo::new();
let owner = Owner { name : ownername}; let path : Vec<(String, String)> = rootname.split("/").map_accum("/git/".to_string() + &owner + "/" + &reponame + "/" + &commitname, |str_ref, b| {
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() + &ownername + "/" + &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),
@ -159,26 +161,19 @@ async fn git_main(
Ok(head) => head, Ok(head) => head,
Err(e) => panic!("failed to get head: {}", e) Err(e) => panic!("failed to get head: {}", e)
}; };
let commitgit = match head.peel_to_commit() { let commit = match head.peel_to_commit() {
Ok(commit) => commit, Ok(commit) => commit,
Err(e) => panic!("failed to get commit: {}", e) Err(e) => panic!("failed to get commit: {}", e)
}; };
let tree = match commitgit.tree() { let tree = match commit.tree() {
Ok(tree) => tree, Ok(tree) => tree,
Err(e) => panic!("failed to get tree: {}", e) Err(e) => panic!("failed to get tree: {}", e)
}; };
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)?; GitMainTemplate { repo, browse : browse, root : path, user_opt : Some(user)}
// let root = GitBrowseDir::new(rootname);
let browse = gitrepo.browse(&root).await;
Ok(GitMainTemplate { repo, browse : browse, root : path, user_opt : Some(user)})
} }
//#[get("/git/{owner}/{repo}.git/{path:.*}")] //#[get("/git/{owner}/{repo}.git/{path:.*}")]
async fn git_proto( async fn git_proto(