mygit

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

commit dfe12f1cfe7d547fd01cfa6b57e68cfcb7216c75
parent bfd04934fdae326bccf7c4cb36537bc9bf08853e
Author: Johann150 <johann@qwertqwefsday.eu>
Date:   Mon, 28 Jun 2021 20:37:45 +0200

preview first 3 commits on repo page

Diffstat:
Msrc/main.rs | 28+++++++++++++++++++++++++---
Mtemplates/repo.html | 15+++++++++++++++
2 files changed, 40 insertions(+), 3 deletions(-)

diff --git a/src/main.rs b/src/main.rs @@ -137,8 +137,9 @@ async fn index(_req: Request<()>) -> tide::Result { #[derive(Template)] #[template(path = "repo.html")] // using the template in this path, relative -struct RepoHomeTemplate { - repo: Repository, +struct RepoHomeTemplate<'a> { + repo: &'a Repository, + commits: Vec<Commit<'a>>, readme_text: String, } @@ -218,7 +219,28 @@ async fn repo_home(req: Request<()>) -> tide::Result { }) .unwrap_or_default(); - Ok(RepoHomeTemplate { repo, readme_text }.into()) + // get the first few commits for a preview + let commits = if repo.is_shallow() { + tide::log::warn!("repository {:?} is only a shallow clone", repo.path()); + vec![repo.head()?.peel_to_commit().unwrap()] + } else { + let mut revwalk = repo.revwalk()?; + let r = req.param("ref").unwrap_or("HEAD"); + revwalk.push(repo.revparse_single(r)?.peel_to_commit()?.id())?; + + revwalk.set_sorting(git2::Sort::TIME).unwrap(); + revwalk + .filter_map(|oid| repo.find_commit(oid.unwrap()).ok()) // TODO error handling + .take(3) + .collect() + }; + + Ok(RepoHomeTemplate { + repo: &repo, + commits, + readme_text, + } + .into()) } #[derive(Template)] diff --git a/templates/repo.html b/templates/repo.html @@ -2,6 +2,21 @@ {% block content %} {% include "repo-navbar.html" %} + <table> + {% for commit in commits %} + <tr> + <td><a href="/{{repo|repo_name|urlencode_strict}}/commit/{{commit.id()}}" class="commit-hash">{{commit.id().to_string()[..7]}}</a></td> + {% let summary = commit.summary().unwrap_or("")|truncate(72) %} + <td class="commit-summary">{{summary}}</td> + <td class="commit-author-email">{{commit.author()|signature_email_link|safe}}</td> + <td class="commit-date">{{commit.time()|format_datetime("%Y-%m-%d")}}</td> + </tr> + {% endfor %} + <tr> + <td colspan="4"><a href="/{{repo|repo_name|urlencode_strict}}/log/">...</a></td> + </tr> + </table> + <hr class="thin"/> <div class="readme"> {% if !readme_text.is_empty() -%} {{ readme_text|safe }}