working breadcrumb

This commit is contained in:
Hubert 2021-07-24 08:02:16 +02:00
parent dbba11a416
commit f9bc5b2e39
2 changed files with 54 additions and 12 deletions

View File

@ -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<Item = &'a Entry>,
{
repo : Repository,
browse : TS,
root_query : GitWebQ,
breadcrumb: BREADCRUMB,
user_opt : Option<User>,
revisions_nbr : u32,
@ -53,13 +55,50 @@ where for <'a> &'a TS : IntoIterator<Item = &'a Entry>,
}
#[derive(Deserialize)]
#[derive(Clone, Deserialize)]
pub struct GitWebQ {
commit: Option<String>,
path: Option<String>,
branch: Option<String>,
}
impl GitWebQ {
fn clone_with_commit(&self, commit : Option<String>) -> GitWebQ {
let mut res = self.clone();
res.commit = commit;
return res;
}
fn clone_with_path(&self, path : Option<String>) -> GitWebQ {
let mut res = self.clone();
res.path = path;
return res;
}
fn clone_with_branch(&self, branch : Option<String>) -> 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<T : AuthValidator>(
web::Path((ownername, reponame)): web::Path<(String, String)>,
@ -71,32 +110,34 @@ pub async fn git_main<T : AuthValidator>(
) -> Result<GitMainTemplate<Vec<Entry>, 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<T : AuthValidator>(
Ok(GitMainTemplate {
repo,
browse : entries,
root_query : query_struct.clone_with_path(None),
breadcrumb: path,
user_opt : user,
revisions_nbr : gitrepo.get_revisions_nbr(),

View File

@ -60,7 +60,7 @@
<div class="w3-cell-row">
<div class="w3-cell">
<ul class="breadcrumb">
<li><a href="#">{{repo.name}}</a></li>
<li><a href="{{root_query}}">{{repo.name}}.git</a></li>
{% for (dir, prev) in breadcrumb %}
<li><a href="{{prev}}">{{dir}}</a></li>
{% endfor %}