From 82757ef9eb9870546737b91948c0e411a725ab27 Mon Sep 17 00:00:00 2001 From: Hubert Date: Sun, 18 Jul 2021 14:34:06 +0200 Subject: [PATCH] works a bit --- src/gitcommit.rs | 26 ++++++++++++++++++-------- src/gitrepo.rs | 19 ++++++++++++++----- src/main.rs | 45 ++++++++++++--------------------------------- 3 files changed, 44 insertions(+), 46 deletions(-) diff --git a/src/gitcommit.rs b/src/gitcommit.rs index 2a2f400..8838bbb 100644 --- a/src/gitcommit.rs +++ b/src/gitcommit.rs @@ -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()) } } diff --git a/src/gitrepo.rs b/src/gitrepo.rs index ac19e77..7b26989 100644 --- a/src/gitrepo.rs +++ b/src/gitrepo.rs @@ -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, 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, 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 }) diff --git a/src/main.rs b/src/main.rs index 231edc5..472925b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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, path: Option, + branch: Option, } @@ -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, + web::Query(GitWebQ{commit : commitnameopt, path : pathopt, branch : branchopt}) : web::Query, gitust : web::Data ) -> Result, 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 = Vec::new(); for entry in browse {