|
|
|
@ -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,
|
|
|
|
@ -38,13 +39,14 @@ pub enum Entry {
|
|
|
|
|
|
|
|
|
|
#[derive(Template)]
|
|
|
|
|
#[template(path = "git.html")]
|
|
|
|
|
pub struct GitMainTemplate<TS, ROOT>
|
|
|
|
|
pub struct GitMainTemplate<TS, BREADCRUMB>
|
|
|
|
|
where for <'a> &'a TS : IntoIterator<Item = &'a Entry>,
|
|
|
|
|
for <'a> &'a ROOT : IntoIterator<Item = &'a (String, String)>,
|
|
|
|
|
for <'a> &'a BREADCRUMB: IntoIterator<Item = &'a (String, String)>,
|
|
|
|
|
{
|
|
|
|
|
repo : Repository,
|
|
|
|
|
browse : TS,
|
|
|
|
|
root : ROOT,
|
|
|
|
|
query : GitWebQ,
|
|
|
|
|
breadcrumb: BREADCRUMB,
|
|
|
|
|
user_opt : Option<User>,
|
|
|
|
|
revisions_nbr : u32,
|
|
|
|
|
branches_nbr : u32,
|
|
|
|
@ -53,17 +55,67 @@ 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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn root_query(&self) -> GitWebQ {
|
|
|
|
|
self.clone_with_path(None)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn add_path(&self, name : &&String) -> GitWebQ {
|
|
|
|
|
match &self.path {
|
|
|
|
|
None => {self.clone_with_path(Some(name.to_string()))}
|
|
|
|
|
Some(path) => {self.clone_with_path()}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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)>,
|
|
|
|
|
web::Query(GitWebQ{commit : commitnameopt, path : pathopt, branch : branchopt}) : web::Query<GitWebQ>,
|
|
|
|
|
query : web::Query<GitWebQ>,
|
|
|
|
|
gitust : web::Data<Gitust>,
|
|
|
|
|
auth : Option<basic::BasicAuth>,
|
|
|
|
|
validator : web::Data<T>,
|
|
|
|
@ -71,31 +123,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 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, |str_ref, b| {
|
|
|
|
|
let href = b + "/" + str_ref;
|
|
|
|
|
((str_ref.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()))?;
|
|
|
|
@ -116,7 +171,8 @@ pub async fn git_main<T : AuthValidator>(
|
|
|
|
|
Ok(GitMainTemplate {
|
|
|
|
|
repo,
|
|
|
|
|
browse : entries,
|
|
|
|
|
root : path,
|
|
|
|
|
query : query_struct,
|
|
|
|
|
breadcrumb: path,
|
|
|
|
|
user_opt : user,
|
|
|
|
|
revisions_nbr : gitrepo.get_revisions_nbr(),
|
|
|
|
|
branches_nbr : gitrepo.get_branches_nbr(),
|
|
|
|
|