mirror of
https://forge.pointfixe.fr/hubert/gitust.git
synced 2026-02-04 16:27:28 +01:00
save
This commit is contained in:
152
src/main.rs
152
src/main.rs
@@ -1,41 +1,53 @@
|
||||
use std::collections::HashMap;
|
||||
use std::io;
|
||||
use std::io::{BufRead, ErrorKind, Read, Write};
|
||||
use std::ops::Add;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::process::Stdio;
|
||||
|
||||
use actix_files::Files;
|
||||
use actix_session::{CookieSession, Session};
|
||||
use actix_web::{App, Error, get, HttpMessage, HttpRequest, HttpResponse, HttpServer, post, Responder, web};
|
||||
use actix_web::client::PayloadError;
|
||||
use actix_web::dev::ServiceRequest;
|
||||
use actix_web::http::{header, StatusCode};
|
||||
use actix_web::http::header::Header;
|
||||
use actix_web::http::header::IntoHeaderValue;
|
||||
use actix_web::middleware::Logger;
|
||||
use actix_web::web::{Buf, Bytes};
|
||||
use actix_web_httpauth::extractors::AuthenticationError;
|
||||
use actix_web_httpauth::extractors::basic::{BasicAuth, Config};
|
||||
use actix_web_httpauth::headers::authorization::{Authorization, Basic};
|
||||
use actix_web_httpauth::middleware::HttpAuthentication;
|
||||
use askama_actix::Template;
|
||||
use env_logger::Env;
|
||||
use futures::{future, pin_mut, Stream, stream, StreamExt, TryFutureExt, TryStreamExt};
|
||||
use git2::ObjectType;
|
||||
use serde::Deserialize;
|
||||
use tokio::io::{AsyncBufReadExt, AsyncReadExt, AsyncWriteExt, BufReader};
|
||||
use tokio::process::{Child, ChildStdout, Command};
|
||||
|
||||
use gitcommit::GitCommit;
|
||||
use gitdir::GitDir;
|
||||
use gitrepo::GitRepo;
|
||||
|
||||
use crate::git::GitBrowseEntry;
|
||||
use crate::gitust::Gitust;
|
||||
use crate::ite::SuperIterator;
|
||||
use crate::reader::ToStream;
|
||||
use crate::writer::Writer;
|
||||
use crate::gitfile::GitFile;
|
||||
|
||||
mod git;
|
||||
mod ite;
|
||||
mod reader;
|
||||
mod writer;
|
||||
mod gitust;
|
||||
|
||||
use actix_files::Files;
|
||||
use actix_web::{get, post, web, App, HttpResponse, HttpServer, Responder, Error, HttpRequest, HttpMessage};
|
||||
use askama_actix::Template;
|
||||
use actix_session::{Session, CookieSession};
|
||||
use actix_web_httpauth::headers::authorization::{Authorization, Basic};
|
||||
use actix_web::http::header::Header;
|
||||
use actix_web_httpauth::middleware::HttpAuthentication;
|
||||
use actix_web::dev::ServiceRequest;
|
||||
use actix_web_httpauth::extractors::basic::{BasicAuth, Config};
|
||||
use actix_web_httpauth::extractors::AuthenticationError;
|
||||
use crate::git::{GitBrowseEntry, GitRepo, GitCommit, GitDir};
|
||||
use actix_web::middleware::Logger;
|
||||
use env_logger::Env;
|
||||
use crate::ite::SuperIterator;
|
||||
use std::ops::Add;
|
||||
use std::path::{PathBuf, Path};
|
||||
use serde::Deserialize;
|
||||
use tokio::process::{Command, Child, ChildStdout};
|
||||
use tokio::io::{AsyncWriteExt, AsyncBufReadExt, AsyncReadExt, BufReader};
|
||||
use std::process::Stdio;
|
||||
use actix_web::http::{header, StatusCode};
|
||||
use std::io;
|
||||
use std::collections::HashMap;
|
||||
use std::io::{Read, BufRead, Write, ErrorKind};
|
||||
use futures::{Stream, StreamExt, TryStreamExt, future, stream, TryFutureExt, pin_mut};
|
||||
use actix_web::web::{Buf, Bytes};
|
||||
use actix_web::http::header::IntoHeaderValue;
|
||||
use actix_web::client::PayloadError;
|
||||
use crate::reader::ToStream;
|
||||
use crate::writer::Writer;
|
||||
use crate::gitust::Gitust;
|
||||
use git2::ObjectType;
|
||||
mod gitdir;
|
||||
mod gitrepo;
|
||||
mod gitcommit;
|
||||
mod gitfile;
|
||||
mod error;
|
||||
|
||||
#[derive(Template)]
|
||||
#[template(path = "hello.html")]
|
||||
@@ -56,10 +68,15 @@ struct Repository {
|
||||
owner : Owner,
|
||||
}
|
||||
|
||||
enum Entry {
|
||||
Dir(String),
|
||||
File(String),
|
||||
}
|
||||
|
||||
#[derive(Template)]
|
||||
#[template(path = "git.html")]
|
||||
struct GitMainTemplate<TS, ROOT>
|
||||
where for <'a> &'a TS : IntoIterator<Item = &'a GitBrowseEntry>,
|
||||
where for <'a> &'a TS : IntoIterator<Item = &'a Entry>,
|
||||
for <'a> &'a ROOT : IntoIterator<Item = &'a (String, String)>,
|
||||
{
|
||||
repo : Repository,
|
||||
@@ -133,7 +150,7 @@ async fn git_main(
|
||||
web::Path((ownername, reponame)): web::Path<(String, String)>,
|
||||
web::Query(GitWebQ{commit : commitnameopt, path : pathopt}) : web::Query<GitWebQ>,
|
||||
gitust : web::Data<Gitust>
|
||||
) -> impl Responder {
|
||||
) -> Result<GitMainTemplate<Vec<Entry>, Vec<(String, String)>>, error::Error> {
|
||||
let commitname = match commitnameopt {
|
||||
None => {"master".to_string()}
|
||||
Some(s) => {s}
|
||||
@@ -151,34 +168,47 @@ async fn git_main(
|
||||
let href = b + "/" + str_ref;
|
||||
((str_ref.to_string(), href.clone()), href)
|
||||
}).collect();
|
||||
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 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;
|
||||
Ok(GitMainTemplate { repo, browse : browse, root : path, user_opt : Some(user)})
|
||||
let browse = gitrepo.browse(&root).await?;
|
||||
let mut entries : Vec<Entry> = Vec::new();
|
||||
for entry in browse {
|
||||
match entry {
|
||||
GitBrowseEntry::EGitFile(GitFile(fullname)) => {
|
||||
let osstr = fullname.file_name().ok_or(git2::Error::from_str("file name not defined"))?;
|
||||
entries.push(Entry::File(format!("{}",osstr.to_string_lossy())));
|
||||
}
|
||||
GitBrowseEntry::EGitDir(GitDir{fullname, .. }) => {
|
||||
let osstr = fullname.file_name().ok_or(git2::Error::from_str("file name not defined"))?;
|
||||
entries.push(Entry::Dir(format!("{}",osstr.to_string_lossy())));
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(GitMainTemplate { repo, browse : entries, root : path, user_opt : Some(user)})
|
||||
}
|
||||
//#[get("/git/{owner}/{repo}.git/{path:.*}")]
|
||||
async fn git_proto(
|
||||
|
||||
Reference in New Issue
Block a user