mygit

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

commit 13cdf125a9fe25764061327e040d1a0095663534
parent 309802d123c2d3b431e16b03f0f5067a7a510131
Author: Johann150 <johann@qwertqwefsday.eu>
Date:   Sun, 14 Mar 2021 23:55:37 +0100

correctly get name for bare repos

workdir is only correct for repositories that have one. Bare repositories
do not have a workdir and so we have to use the directory name instead.

Diffstat:
Msrc/main.rs | 13+++++++++++++
Mtemplates/index.html | 3+--
2 files changed, 14 insertions(+), 2 deletions(-)

diff --git a/src/main.rs b/src/main.rs @@ -246,7 +246,10 @@ async fn repo_commit(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); @@ -282,6 +285,16 @@ mod filters { output[i] = 0x2d; // - return Ok(std::str::from_utf8(&output).unwrap().to_owned()); } + + 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)) + } } #[async_std::main] diff --git a/templates/index.html b/templates/index.html @@ -6,9 +6,8 @@ <div style=> <table> {% for repo in repos %} - {% let name = repo.workdir().unwrap().file_name().unwrap().to_str().unwrap() %} <tr> - <td class="repo-link"><a href="{{name}}">{{ name }}</a></td> + <td class="repo-link"><a href="{{repo|repo_name|urlencode_strict}}">{{repo|repo_name}}</a></td> <td class="repo-description">a cool repository</td> <td class="repo-last-updated">updated 2021-02-01</td> </tr>