mirror of
https://forge.pointfixe.fr/hubert/gitust.git
synced 2026-02-04 22:07:30 +01:00
Compare commits
10 Commits
9832f30360
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| 6beae6629f | |||
|
|
ba19d67f66 | ||
|
|
f9bc5b2e39 | ||
|
|
dbba11a416 | ||
|
|
edf6f9f81a | ||
|
|
5f6f7b7efe | ||
|
|
1f4d08aff1 | ||
|
|
f71e8ff1b3 | ||
|
|
4134982739 | ||
|
|
1e7c773c19 |
@@ -78,4 +78,28 @@ impl GitRepo {
|
|||||||
return Ok(res);
|
return Ok(res);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn get_revisions_nbr(&self) -> u32 {
|
||||||
|
let mut res = 0;
|
||||||
|
let mut revwalk = self.git2.revwalk().expect("revisions walk is not defined");
|
||||||
|
revwalk.push_head(); // todo : voir ce qu'il faut réellement pousser si on veux avoir TOUS les commits (genre par exemple si le HEAD n'est pas fils de tous les commits)
|
||||||
|
for _ in revwalk{
|
||||||
|
res = res + 1;
|
||||||
|
}
|
||||||
|
res
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_branches_nbr(&self) -> u32 {
|
||||||
|
let mut res = 0;
|
||||||
|
for _ in self.git2.branches(Some(BranchType::Local)){
|
||||||
|
res = res + 1;
|
||||||
|
}
|
||||||
|
res
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_tags_nbr(&self) -> u32 {
|
||||||
|
let mut res = 0;
|
||||||
|
self.git2.tag_foreach(|_,_| {res = res + 1;true});
|
||||||
|
res
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
29
src/main.rs
29
src/main.rs
@@ -9,14 +9,14 @@ use actix_files::Files;
|
|||||||
use actix_session::{CookieSession, Session};
|
use actix_session::{CookieSession, Session};
|
||||||
use actix_web::{App, Error, get, HttpMessage, HttpRequest, HttpResponse, HttpServer, post, Responder};
|
use actix_web::{App, Error, get, HttpMessage, HttpRequest, HttpResponse, HttpServer, post, Responder};
|
||||||
use actix_web::client::PayloadError;
|
use actix_web::client::PayloadError;
|
||||||
use actix_web::dev::ServiceRequest;
|
use actix_web::dev::{ServiceRequest, Service};
|
||||||
use actix_web::http::{header, StatusCode};
|
use actix_web::http::{header, StatusCode};
|
||||||
use actix_web::http::header::Header;
|
use actix_web::http::header::Header;
|
||||||
use actix_web::http::header::IntoHeaderValue;
|
use actix_web::http::header::IntoHeaderValue;
|
||||||
use actix_web::middleware::Logger;
|
use actix_web::middleware::Logger;
|
||||||
use actix_web::web as webx;
|
use actix_web::web as webx;
|
||||||
use actix_web::web::{Buf, Bytes};
|
use actix_web::web::{Buf, Bytes};
|
||||||
use actix_web_httpauth::extractors::AuthenticationError;
|
use actix_web_httpauth::extractors::{AuthenticationError, AuthExtractor};
|
||||||
use actix_web_httpauth::extractors::basic::{BasicAuth, Config};
|
use actix_web_httpauth::extractors::basic::{BasicAuth, Config};
|
||||||
use actix_web_httpauth::headers::authorization::{Authorization, Basic};
|
use actix_web_httpauth::headers::authorization::{Authorization, Basic};
|
||||||
use actix_web_httpauth::middleware::HttpAuthentication;
|
use actix_web_httpauth::middleware::HttpAuthentication;
|
||||||
@@ -41,6 +41,8 @@ use crate::gitust::Gitust;
|
|||||||
use crate::ite::SuperIterator;
|
use crate::ite::SuperIterator;
|
||||||
use crate::reader::ToStream;
|
use crate::reader::ToStream;
|
||||||
use crate::writer::Writer;
|
use crate::writer::Writer;
|
||||||
|
use crate::web::repo::GitWebQ;
|
||||||
|
use crate::webutils::auth::{TestValidator, AuthValidator, User};
|
||||||
|
|
||||||
mod git;
|
mod git;
|
||||||
mod ite;
|
mod ite;
|
||||||
@@ -150,6 +152,11 @@ fn validate_credentials(user_id: &str, user_password: Option<&str>) -> Result<bo
|
|||||||
return Err(std::io::Error::new(std::io::ErrorKind::Other, "Authentication failed!"));
|
return Err(std::io::Error::new(std::io::ErrorKind::Other, "Authentication failed!"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
struct LoginQ {
|
||||||
|
login: Option<bool>,
|
||||||
|
}
|
||||||
|
|
||||||
#[actix_web::main]
|
#[actix_web::main]
|
||||||
async fn main() -> std::io::Result<()> {
|
async fn main() -> std::io::Result<()> {
|
||||||
std::env::set_var("RUST_LOG", "actix_web=info");
|
std::env::set_var("RUST_LOG", "actix_web=info");
|
||||||
@@ -174,6 +181,24 @@ async fn main() -> std::io::Result<()> {
|
|||||||
.service(askama)
|
.service(askama)
|
||||||
.service(
|
.service(
|
||||||
webx::resource("/git/{owner}/{repo}.git")
|
webx::resource("/git/{owner}/{repo}.git")
|
||||||
|
.wrap_fn(|req, serv| {
|
||||||
|
let query : Result<webx::Query<LoginQ>, _> = webx::Query::from_query(req.query_string());
|
||||||
|
let authFut = BasicAuth::from_service_request(&req);
|
||||||
|
let resFut = serv.call(req);
|
||||||
|
async {
|
||||||
|
let webx::Query(LoginQ{login}) = query?;
|
||||||
|
match login {
|
||||||
|
Some(true) => {
|
||||||
|
let auth = authFut.await?;
|
||||||
|
match TestValidator.check_basic(&auth) {
|
||||||
|
None => {Err(AuthenticationError::from(Config::default().realm("auth")).into())}
|
||||||
|
Some(_) => {resFut.await}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {resFut.await}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
.route(webx::get().to(repo::git_main::<auth::TestValidator>))
|
.route(webx::get().to(repo::git_main::<auth::TestValidator>))
|
||||||
)
|
)
|
||||||
.service(hello_session)
|
.service(hello_session)
|
||||||
|
|||||||
105
src/web/repo.rs
105
src/web/repo.rs
@@ -17,6 +17,7 @@ use crate::gitutils::gitdir::GitDir;
|
|||||||
use crate::gitutils::gitfile::GitFile;
|
use crate::gitutils::gitfile::GitFile;
|
||||||
use crate::gitutils::gitrepo::GitRepo;
|
use crate::gitutils::gitrepo::GitRepo;
|
||||||
use crate::ite::SuperIterator;
|
use crate::ite::SuperIterator;
|
||||||
|
use std::fmt::{Display, Formatter};
|
||||||
|
|
||||||
struct User {
|
struct User {
|
||||||
name : String,
|
name : String,
|
||||||
@@ -38,28 +39,87 @@ pub enum Entry {
|
|||||||
|
|
||||||
#[derive(Template)]
|
#[derive(Template)]
|
||||||
#[template(path = "git.html")]
|
#[template(path = "git.html")]
|
||||||
pub struct GitMainTemplate<TS, ROOT>
|
pub struct GitMainTemplate<TS, BREADCRUMB>
|
||||||
where for <'a> &'a TS : IntoIterator<Item = &'a Entry>,
|
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,
|
repo : Repository,
|
||||||
browse : TS,
|
browse : TS,
|
||||||
root : ROOT,
|
query : GitWebQ,
|
||||||
|
breadcrumb: BREADCRUMB,
|
||||||
user_opt : Option<User>,
|
user_opt : Option<User>,
|
||||||
|
revisions_nbr : u32,
|
||||||
|
branches_nbr : u32,
|
||||||
|
tags_nbr : u32,
|
||||||
|
size : String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Clone, Deserialize)]
|
||||||
pub struct GitWebQ {
|
pub struct GitWebQ {
|
||||||
commit: Option<String>,
|
commit: Option<String>,
|
||||||
path: Option<String>,
|
path: Option<String>,
|
||||||
branch: 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) => {if path.ends_with("/") {
|
||||||
|
self.clone_with_path(Some(path.clone() + name))
|
||||||
|
} else {
|
||||||
|
self.clone_with_path(Some(path.clone() + "/" + name))
|
||||||
|
}}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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")]
|
//#[get("/git/{owner}/{repo}.git")]
|
||||||
pub async fn git_main<T : AuthValidator>(
|
pub async fn git_main<T : AuthValidator>(
|
||||||
web::Path((ownername, reponame)): web::Path<(String, String)>,
|
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>,
|
gitust : web::Data<Gitust>,
|
||||||
auth : Option<basic::BasicAuth>,
|
auth : Option<basic::BasicAuth>,
|
||||||
validator : web::Data<T>,
|
validator : web::Data<T>,
|
||||||
@@ -67,31 +127,34 @@ pub async fn git_main<T : AuthValidator>(
|
|||||||
) -> Result<GitMainTemplate<Vec<Entry>, Vec<(String, String)>>, error::Error> {
|
) -> Result<GitMainTemplate<Vec<Entry>, Vec<(String, String)>>, error::Error> {
|
||||||
// let authtorization = auth.ok_or(error::Error::Unauthorized("safe repo".to_string()))?;
|
// 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 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()}
|
None => {"".to_string()}
|
||||||
Some(s) => {
|
Some(s) => {
|
||||||
if s.starts_with("/") {
|
if s.starts_with("/") {
|
||||||
(&s[1..]).to_string()
|
(&s[1..]).to_string()
|
||||||
} else {
|
} else {
|
||||||
s
|
s.clone()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
let commit = match commitnameopt {
|
let commit = match &query_struct.commit {
|
||||||
None => {match branchopt {
|
None => {match &query_struct.branch {
|
||||||
None => {GitRef::Head}
|
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 owner = Owner { name : ownername};
|
||||||
let repo = Repository {name : reponame, owner};
|
let repo = Repository {name : reponame, owner};
|
||||||
// let user = User { name : "Hubert".to_string()};
|
// let user = User { name : "Hubert".to_string()};
|
||||||
let user = auth.map(|auth| User{name : auth.user_id().to_string()});
|
let user = auth.map(|auth| User{name : auth.user_id().to_string()});
|
||||||
// il faut ajouter le commit/branch dans la query
|
// 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 path = rootname.split("/").map_accum(query_struct.clone_with_path(Some("".to_string())), |direname, b| {
|
||||||
let href = b + "/" + str_ref;
|
let newpath = (&b.path).as_ref().map(|path| {path.clone() + "/" + direname});
|
||||||
((str_ref.to_string(), href.clone()), href)
|
let newb = b.clone_with_path(newpath);
|
||||||
|
((direname.to_string(), format!("{}", newb)), newb)
|
||||||
}).collect();
|
}).collect();
|
||||||
let gitrepo = GitRepo::new(format!("{}/{}/{}.git", &gitust.repo_root_path, &repo.owner.name, &repo.name).as_str())?;
|
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()))?;
|
let root = gitrepo.get_tree(&commit, Path::new(rootname.as_str()))?;
|
||||||
@@ -109,5 +172,17 @@ pub async fn git_main<T : AuthValidator>(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(GitMainTemplate { repo, browse : entries, root : path, user_opt : user})
|
Ok(GitMainTemplate {
|
||||||
|
repo,
|
||||||
|
browse : entries,
|
||||||
|
query : query_struct,
|
||||||
|
breadcrumb: path,
|
||||||
|
user_opt : user,
|
||||||
|
revisions_nbr : gitrepo.get_revisions_nbr(),
|
||||||
|
branches_nbr : gitrepo.get_branches_nbr(),
|
||||||
|
tags_nbr : gitrepo.get_tags_nbr(),
|
||||||
|
size : "16 KiB".to_string(),
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -26,7 +26,7 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
{% when None %}
|
{% when None %}
|
||||||
<a href="#" class="w3-bar-item w3-button w3-right"><i class="fa fa-sign-in"></i> Log in</a>
|
<a href="?login=true" class="w3-bar-item w3-button w3-right"><i class="fa fa-sign-in"></i> Log in</a>
|
||||||
{% endmatch %}
|
{% endmatch %}
|
||||||
</div>
|
</div>
|
||||||
{% block content %}{% endblock %}
|
{% block content %}{% endblock %}
|
||||||
|
|||||||
@@ -26,16 +26,16 @@
|
|||||||
|
|
||||||
<div class="w3-panel w3-border w3-cell-row">
|
<div class="w3-panel w3-border w3-cell-row">
|
||||||
<div class="w3-center w3-cell">
|
<div class="w3-center w3-cell">
|
||||||
<i class="fa fa-history"></i> 112 revisions
|
<i class="fa fa-history"></i> {{ revisions_nbr }} revisions
|
||||||
</div>
|
</div>
|
||||||
<div class="w3-center w3-cell">
|
<div class="w3-center w3-cell">
|
||||||
<i class="fa fa-code-branch"></i> 10 branches
|
<i class="fa fa-code-branch"></i> {{ branches_nbr }} branches
|
||||||
</div>
|
</div>
|
||||||
<div class="w3-center w3-cell">
|
<div class="w3-center w3-cell">
|
||||||
<i class="fa fa-tag"></i> 5 tags
|
<i class="fa fa-tag"></i> {{ tags_nbr }} tags
|
||||||
</div>
|
</div>
|
||||||
<div class="w3-center w3-cell">
|
<div class="w3-center w3-cell">
|
||||||
<i class="fa fa-database"></i> 10 KiB
|
<i class="fa fa-database"></i> {{ size }}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
@@ -60,8 +60,8 @@
|
|||||||
<div class="w3-cell-row">
|
<div class="w3-cell-row">
|
||||||
<div class="w3-cell">
|
<div class="w3-cell">
|
||||||
<ul class="breadcrumb">
|
<ul class="breadcrumb">
|
||||||
<li><a href="#">{{repo.name}}</a></li>
|
<li><a href="{{query.root_query()}}">{{repo.name}}.git</a></li>
|
||||||
{% for (dir, prev) in root %}
|
{% for (dir, prev) in breadcrumb %}
|
||||||
<li><a href="{{prev}}">{{dir}}</a></li>
|
<li><a href="{{prev}}">{{dir}}</a></li>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</ul>
|
</ul>
|
||||||
@@ -77,7 +77,7 @@
|
|||||||
{% for entry in browse %}
|
{% for entry in browse %}
|
||||||
{% match entry %}
|
{% match entry %}
|
||||||
{% when Entry::Dir with (name) %}
|
{% when Entry::Dir with (name) %}
|
||||||
<li><i class="fa fa-folder"></i> {{ name }}</li>
|
<li><i class="fa fa-folder"></i> <a href="{{query.add_path(name)}}">{{ name }}</a></li>
|
||||||
{% when Entry::File with (name) %}
|
{% when Entry::File with (name) %}
|
||||||
<li><i class="fa fa-file"></i> {{ name }}</li>
|
<li><i class="fa fa-file"></i> {{ name }}</li>
|
||||||
{% endmatch %}
|
{% endmatch %}
|
||||||
|
|||||||
Reference in New Issue
Block a user