works a bit

This commit is contained in:
Hubert 2021-07-18 14:34:06 +02:00
parent 2278b46a88
commit 82757ef9eb
3 changed files with 44 additions and 46 deletions

View File

@ -1,20 +1,30 @@
use std::fmt::{Display, Formatter};
#[derive(Debug)]
pub struct GitCommit (String);
pub enum GitRef{
Commit(String),
Branch(String),
}
impl GitCommit {
pub fn new(s : String) -> GitCommit {
GitCommit(s)
impl GitRef {
pub fn new_commit(s : String) -> GitRef {
GitRef::Commit(s)
}
pub fn value(&self) -> &String {
&self.0
pub fn new_branch(s : String) -> GitRef {
GitRef::Branch(s)
}
fn value(&self) -> &String {
match self {
GitRef::Commit(s) => s,
GitRef::Branch(s) => s
}
}
}
impl Display for GitCommit {
impl Display for GitRef {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
write!(f, "{}", self.value())
}
}

View File

@ -1,9 +1,9 @@
use std::ops::Add;
use git2::{ObjectType, Oid, Repository};
use git2::{ObjectType, Oid, Repository, BranchType};
use crate::git::GitBrowseEntry;
use crate::gitcommit::GitCommit;
use crate::gitcommit::GitRef;
use crate::gitdir::GitDir;
use crate::gitfile::GitFile;
use std::path::PathBuf;
@ -19,9 +19,18 @@ impl GitRepo {
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.value().as_str())?;
let commit = self.git2.find_commit(oid)?;
pub fn get_root_tree<'a, 'b> (&'a self, commit : &'b GitRef) -> Result<GitDir<'a>, git2::Error> {
let commit = match commit {
GitRef::Commit(commit) => {
let oid = Oid::from_str(commit.as_str())?;
self.git2.find_commit(oid)?
}
GitRef::Branch(branch) => {
let branch = self.git2.find_branch(branch.as_str(), BranchType::Local)?;
branch.get().peel_to_commit()?
}
};
let tree = commit.tree()?;
let fullname = PathBuf::from("/");
Ok(GitDir { fullname, tree })

View File

@ -27,7 +27,7 @@ use serde::Deserialize;
use tokio::io::{AsyncBufReadExt, AsyncReadExt, AsyncWriteExt, BufReader};
use tokio::process::{Child, ChildStdout, Command};
use gitcommit::GitCommit;
use gitcommit::GitRef;
use gitdir::GitDir;
use gitrepo::GitRepo;
@ -124,6 +124,7 @@ async fn hello_auth(req : HttpRequest) -> impl Responder {
struct GitWebQ {
commit: Option<String>,
path: Option<String>,
branch: Option<String>,
}
@ -148,52 +149,30 @@ async fn chunk() -> HttpResponse {
#[get("/git/{owner}/{repo}.git")]
async fn git_main(
web::Path((ownername, reponame)): web::Path<(String, String)>,
web::Query(GitWebQ{commit : commitnameopt, path : pathopt}) : web::Query<GitWebQ>,
web::Query(GitWebQ{commit : commitnameopt, path : pathopt, branch : branchopt}) : web::Query<GitWebQ>,
gitust : web::Data<Gitust>
) -> Result<GitMainTemplate<Vec<Entry>, Vec<(String, String)>>, error::Error> {
let commitname = match commitnameopt {
None => {"master".to_string()}
Some(s) => {s}
};
let rootname = match pathopt {
None => {"/".to_string()}
Some(s) => {s}
};
let commit = GitCommit::new(commitname);
let commit = match commitnameopt {
None => {match branchopt {
None => {GitRef::Branch("master".to_string())}
Some(s) => {GitRef::Branch(s)}
}}
Some(s) => {GitRef::Commit(s)}
};
let owner = Owner { name : ownername};
let repo = Repository {name : reponame, owner};
let user = User { name : "Hubert".to_string()};
let path = rootname.split("/").map_accum("/git/".to_string() + &repo.owner.name + "/" + &repo.name + "/" + commit.value(), |str_ref, b| {
// il faut ajouter le commit/branch dans la query
let path = rootname.split("/").map_accum("/git/".to_string() + &repo.owner.name + "/" + &repo.name, |str_ref, b| {
let href = b + "/" + str_ref;
((str_ref.to_string(), href.clone()), href)
}).collect();
let gitrepo = GitRepo::new(format!("{}/{}/{}.git", &gitust.repo_root_path, &repo.owner.name, &repo.name).as_str())?;
// let repogit = match git2::Repository::open(format!("{}/{}/{}.git", &gitust.repo_root_path, &repo.owner.name, &repo.name)) {
// Ok(repo) => repo,
// Err(e) => panic!("failed to open: {}", e),
// };
// let head = match repogit.head() {
// Ok(head) => head,
// Err(e) => panic!("failed to get head: {}", e)
// };
// let commitgit = match head.peel_to_commit() {
// Ok(commit) => commit,
// Err(e) => panic!("failed to get commit: {}", e)
// };
// let tree = match commitgit.tree() {
// Ok(tree) => tree,
// Err(e) => panic!("failed to get tree: {}", e)
// };
// for entry in tree.iter() {
// println!("{:?}", entry.name());
// 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?;
let mut entries : Vec<Entry> = Vec::new();
for entry in browse {