mygit

[UNMAINTAINED] A cgit/webgit alternative, written in Rust
Log | Files | Refs | README | LICENSE

commit b4c6d26345cd194dad3c340403bdd91c753ab984
parent d6157cbd3b083204de5250b9ff699d0f6c1f706f
Author: Johann150 <johann@qwertqwefsday.eu>
Date:   Tue, 29 Jun 2021 12:19:13 +0200

move filters to separate file

Diffstat:
Asrc/filters.rs | 75+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/main.rs | 78+-----------------------------------------------------------------------------
2 files changed, 76 insertions(+), 77 deletions(-)

diff --git a/src/filters.rs b/src/filters.rs @@ -0,0 +1,75 @@ + +use super::*; + +pub fn format_datetime(time: &git2::Time, format: &str) -> ::askama::Result<String> { + use chrono::{FixedOffset, TimeZone}; + let offset = FixedOffset::west(time.offset_minutes() * 60); + let datetime = offset.timestamp(time.seconds(), 0); + Ok(datetime.format(format).to_string()) +} + +pub fn unix_perms(m: &i32) -> ::askama::Result<String> { + // https://unix.stackexchange.com/questions/450480/file-permission-with-six-bytes-in-git-what-does-it-mean + // Git doesn’t store arbitrary modes, only a subset of the values are + // allowed. Since the number of possible values is quite small, it is + // easiest to exhaustively match them. + Ok(match m { + 0o040000 => "drwxr-xr-x", // directory + 0o100755 => "-rwxr-xr-x", // regular file, executable + 0o100644 => "-rw-r--r--", // regular file, default umask + 0o120000 => "lrwxrwxrwx", // symlink + 0o160000 => "m---------", // submodule + _ => unreachable!("unknown file mode"), + } + .into()) +} + +pub fn repo_name(repo: &Repository) -> askama::Result<&str> { + repo.workdir() + // use the path for bare repositories + .unwrap_or_else(|| repo.path()) + .file_name() + .unwrap() + .to_str() + .ok_or(askama::Error::Fmt(std::fmt::Error)) +} + +pub fn description(repo: &Repository) -> askama::Result<String> { + Ok(fs::read_to_string(repo.path().join("description")) + .unwrap_or_default() + // only use first line + .lines() + .next() + .unwrap_or_default() + .to_string()) +} + +pub fn last_modified(repo: &Repository) -> askama::Result<git2::Time> { + Ok(repo + .head() + .unwrap() + .peel_to_commit() + .unwrap() + .committer() + .when()) +} + +pub fn repo_owner(repo: &Repository) -> askama::Result<String> { + Ok(repo + .config() + .unwrap() + .get_string("gitweb.owner") + .unwrap_or_default()) +} + +pub fn signature_email_link(signature: &Signature) -> askama::Result<String> { + Ok(if let Some(email) = signature.email() { + format!( + "<a href=\"mailto:{}\">{}</a>", + email, + signature.name().unwrap_or("&#65533;") + ) + } else { + signature.to_string() + }) +} diff --git a/src/main.rs b/src/main.rs @@ -15,6 +15,7 @@ use syntect::{ use tide::{http, Request, Response}; mod errorpage; +mod filters; mod mail; #[derive(Deserialize, Debug)] @@ -892,83 +893,6 @@ async fn repo_refs_feed(req: Request<()>) -> tide::Result { Ok(tmpl.into()) } -mod filters { - use super::*; - - pub fn format_datetime(time: &git2::Time, format: &str) -> ::askama::Result<String> { - use chrono::{FixedOffset, TimeZone}; - let offset = FixedOffset::west(time.offset_minutes() * 60); - let datetime = offset.timestamp(time.seconds(), 0); - Ok(datetime.format(format).to_string()) - } - - pub fn unix_perms(m: &i32) -> ::askama::Result<String> { - // https://unix.stackexchange.com/questions/450480/file-permission-with-six-bytes-in-git-what-does-it-mean - // Git doesn’t store arbitrary modes, only a subset of the values are - // allowed. Since the number of possible values is quite small, it is - // easiest to exhaustively match them. - Ok(match m { - 0o040000 => "drwxr-xr-x", // directory - 0o100755 => "-rwxr-xr-x", // regular file, executable - 0o100644 => "-rw-r--r--", // regular file, default umask - 0o120000 => "lrwxrwxrwx", // symlink - 0o160000 => "m---------", // submodule - _ => unreachable!("unknown file mode"), - } - .into()) - } - - pub fn repo_name(repo: &Repository) -> askama::Result<&str> { - repo.workdir() - // use the path for bare repositories - .unwrap_or_else(|| repo.path()) - .file_name() - .unwrap() - .to_str() - .ok_or(askama::Error::Fmt(std::fmt::Error)) - } - - pub fn description(repo: &Repository) -> askama::Result<String> { - Ok(fs::read_to_string(repo.path().join("description")) - .unwrap_or_default() - // only use first line - .lines() - .next() - .unwrap_or_default() - .to_string()) - } - - pub fn last_modified(repo: &Repository) -> askama::Result<git2::Time> { - Ok(repo - .head() - .unwrap() - .peel_to_commit() - .unwrap() - .committer() - .when()) - } - - pub fn repo_owner(repo: &Repository) -> askama::Result<String> { - Ok(repo - .config() - .unwrap() - .get_string("gitweb.owner") - .unwrap_or_default()) - } - - pub fn signature_email_link(signature: &Signature) -> askama::Result<String> { - Ok(if let Some(email) = signature.email() { - format!( - "<a href=\"mailto:{}\">{}</a>", - email, - signature.name().unwrap_or("&#65533;") - ) - } else { - signature.to_string() - }) - } -} - #[async_std::main] async fn main() -> Result<(), std::io::Error> { tide::log::start();