mygit

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

commit 2d551dea33c5161ac867026bc675f78e4c209695
parent b9a32cbdd89e835b506ac38483b5997da4456ab6
Author: Johann150 <johann@qwertqwefsday.eu>
Date:   Fri,  2 Apr 2021 15:10:26 +0200

enable viewing older log

The reference in the requested URL is now parsed in such a way that
you can specify something like HEAD~100. This is used to show pagination
of older commits. The number of commits per page is configurable.

Diffstat:
Mmygit.toml | 7++++++-
Msrc/main.rs | 45+++++++++++++++++++++++++++++++++++----------
Mtemplates/log.html | 6++++++
3 files changed, 47 insertions(+), 11 deletions(-)

diff --git a/mygit.toml b/mygit.toml @@ -1,9 +1,14 @@ +# the port the server will listen on port = 8081 # Directory to find git repos projectroot = "repos" +# Specify an emoji to be used as a favicon emoji_favicon = "💻" +# the text shown in a browsers title bar site_name = "My git repositories" # file to check for in the .git directory to decide wether to publicly show a repo export_ok = "git-daemon-export-ok" -# base directory to clone repositories from +# base URL to clone repositories from (without trailing slash) clone_base = "https://git.alexwennerberg.com" +# the number of commits to be shown when paginating the log +log_per_page = 100 diff --git a/src/main.rs b/src/main.rs @@ -32,6 +32,8 @@ pub struct Config { export_ok: String, #[serde(default = "String::new")] clone_base: String, + #[serde(default = "defaults::log_per_page")] + log_per_page: usize, } /// Defaults for the configuration options @@ -52,6 +54,10 @@ mod defaults { pub fn export_ok() -> String { "git-daemon-export-ok".to_string() } + + pub fn log_per_page() -> usize { + 100 + } } const HELP: &str = "\ @@ -222,6 +228,8 @@ struct RepoLogTemplate<'a> { repo: &'a Repository, commits: Vec<Commit<'a>>, branch: &'a str, + // the spec the user should be linked to to see the next page of commits + next_page: Option<String>, } async fn repo_log(req: Request<()>) -> tide::Result { @@ -232,25 +240,41 @@ async fn repo_log(req: Request<()>) -> tide::Result { url.path_segments_mut().unwrap().pop(); return Ok(tide::Redirect::temporary(url.to_string()).into()); } - let commits = if repo.is_shallow() { + + let next_page_spec; + let mut commits = if repo.is_shallow() { tide::log::warn!("repository {:?} is only a shallow clone", repo.path()); + next_page_spec = "".into(); vec![repo.head()?.peel_to_commit().unwrap()] } else { let mut revwalk = repo.revwalk()?; - match req.param("ref") { - Ok(r) => { - revwalk.push_ref(&format!("refs/heads/{}", r))?; - } - _ => { - revwalk.push_head()?; - } - }; + let r = req.param("ref").unwrap_or("HEAD"); + revwalk.push(repo.revparse_single(r)?.peel_to_commit()?.id())?; + + if let Some(i) = r.rfind('~') { + // there is a tilde, try to find a number too + let n = r[i + 1..].parse::<usize>().ok().unwrap_or(1); + next_page_spec = format!("{}~{}", &r[..i], n + CONFIG.log_per_page); + } else { + // there was no tilde + next_page_spec = format!("{}~{}", r, CONFIG.log_per_page); + } + revwalk.set_sorting(git2::Sort::TIME).unwrap(); revwalk .filter_map(|oid| repo.find_commit(oid.unwrap()).ok()) // TODO error handling - .take(100) + .take(CONFIG.log_per_page + 1) .collect() }; + // check if there even is a next page + let next_page = if commits.len() < CONFIG.log_per_page + 1 { + None + } else { + // remove additional commit from next page check + commits.pop(); + Some(next_page_spec) + }; + let head_branch = repo.head()?; let branch = req .param("ref") @@ -261,6 +285,7 @@ async fn repo_log(req: Request<()>) -> tide::Result { repo: &repo, commits, branch, + next_page, }; Ok(tmpl.into()) } diff --git a/templates/log.html b/templates/log.html @@ -2,6 +2,9 @@ {% block content %} {% include "repo-navbar.html" %} + {% if next_page.is_some() %} + <a href="{{next_page.as_ref().unwrap()}}" style="float:right;">older commits &rarr;</a> + {% endif %} <h3>{{branch}}</h3> <table> {% for commit in commits %} @@ -14,5 +17,8 @@ </tr> {% endfor %} </table> + {% if next_page.is_some() %} + <a href="{{next_page.as_ref().unwrap()}}">older commits &rarr;</a> + {% endif %} {% endblock %}