diff --git a/src/web/repo.rs b/src/web/repo.rs index b7b00ce..40d9675 100644 --- a/src/web/repo.rs +++ b/src/web/repo.rs @@ -17,6 +17,7 @@ use crate::gitutils::gitdir::GitDir; use crate::gitutils::gitfile::GitFile; use crate::gitutils::gitrepo::GitRepo; use crate::ite::SuperIterator; +use std::fmt::{Display, Formatter}; struct User { name : String, @@ -44,6 +45,7 @@ where for <'a> &'a TS : IntoIterator, { repo : Repository, browse : TS, + root_query : GitWebQ, breadcrumb: BREADCRUMB, user_opt : Option, revisions_nbr : u32, @@ -53,13 +55,50 @@ where for <'a> &'a TS : IntoIterator, } -#[derive(Deserialize)] +#[derive(Clone, Deserialize)] pub struct GitWebQ { commit: Option, path: Option, branch: Option, } +impl GitWebQ { + fn clone_with_commit(&self, commit : Option) -> GitWebQ { + let mut res = self.clone(); + res.commit = commit; + return res; + } + fn clone_with_path(&self, path : Option) -> GitWebQ { + let mut res = self.clone(); + res.path = path; + return res; + } + fn clone_with_branch(&self, branch : Option) -> GitWebQ { + let mut res = self.clone(); + res.branch = branch; + return res; + } +} + +impl Display for GitWebQ{ + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + write!(f, "?{}{}{}", + match &self.commit { + None => {"".to_string()} + Some(c) => {format!("commit={}", c)} + }, + match &self.path { + None => {"".to_string()} + Some(p) => {format!("&path={}", p)} + }, + match &self.branch { + None => {"".to_string()} + Some(b) => {format!("&branch={}", b)} + } + ) + } +} + //#[get("/git/{owner}/{repo}.git")] pub async fn git_main( web::Path((ownername, reponame)): web::Path<(String, String)>, @@ -71,32 +110,34 @@ pub async fn git_main( ) -> Result, Vec<(String, String)>>, error::Error> { // let authtorization = auth.ok_or(error::Error::Unauthorized("safe repo".to_string()))?; //let authorization = auth.and_then(|cred| validator.check_basic(&cred)).ok_or(error::Error::Unauthorized("safe repo".to_string()))?; - let web::Query(GitWebQ{commit : commitnameopt, path : pathopt, branch : branchopt}) = query; - let rootname = match pathopt { + let web::Query(query_struct) = query; + // let GitWebQ{commit : commitnameopt, path : pathopt, branch : branchopt} = query_struct; + let rootname = match &query_struct.path { None => {"".to_string()} Some(s) => { if s.starts_with("/") { (&s[1..]).to_string() } else { - s + s.clone() } } }; - let commit = match commitnameopt { - None => {match branchopt { + let commit = match &query_struct.commit { + None => {match &query_struct.branch { None => {GitRef::Head} - Some(s) => {GitRef::Branch(s)} + Some(s) => {GitRef::Branch(s.clone())} }} - Some(s) => {GitRef::Commit(s)} + Some(s) => {GitRef::Commit(s.clone())} }; let owner = Owner { name : ownername}; let repo = Repository {name : reponame, owner}; // let user = User { name : "Hubert".to_string()}; let user = auth.map(|auth| User{name : auth.user_id().to_string()}); // il faut ajouter le commit/branch dans la query - let path = rootname.split("/").map_accum("/git/".to_string() + &repo.owner.name + "/" + &repo.name, |direname, b| { - let href = b + "/" + direname; - ((direname.to_string(), href.clone()), href) + let path = rootname.split("/").map_accum(query_struct.clone_with_path(Some("".to_string())), |direname, b| { + let newpath = (&b.path).as_ref().map(|path| {path.clone() + "/" + direname}); + let newb = b.clone_with_path(newpath); + ((direname.to_string(), format!("{}", newb)), newb) }).collect(); let gitrepo = GitRepo::new(format!("{}/{}/{}.git", &gitust.repo_root_path, &repo.owner.name, &repo.name).as_str())?; let root = gitrepo.get_tree(&commit, Path::new(rootname.as_str()))?; @@ -117,6 +158,7 @@ pub async fn git_main( Ok(GitMainTemplate { repo, browse : entries, + root_query : query_struct.clone_with_path(None), breadcrumb: path, user_opt : user, revisions_nbr : gitrepo.get_revisions_nbr(), diff --git a/templates/git.html b/templates/git.html index 6c3a3cd..c1ad377 100644 --- a/templates/git.html +++ b/templates/git.html @@ -60,7 +60,7 @@