mygit

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

commit f33cda5c3566e96c805586984d9905b5c03be861
parent 7410f5d964eb9b5b7403823f80f0be5500a58d07
Author: alex wennerberg <alex@alexwennerberg.com>
Date:   Sat, 13 Mar 2021 17:01:29 -0800

Add basic refs page

Diffstat:
Msrc/main.rs | 36++++++++++++++++++++++++++++++++++--
Mtemplates/refs.html | 22++++++++++++++++++++++
Mtemplates/static/style.css | 2+-
3 files changed, 57 insertions(+), 3 deletions(-)

diff --git a/src/main.rs b/src/main.rs @@ -1,6 +1,6 @@ use anyhow::Result; use askama::Template; -use git2::{Commit, Repository}; +use git2::{Commit, Reference, Repository}; use once_cell::sync::OnceCell; use pico_args; use pulldown_cmark::{html, Options, Parser}; @@ -98,7 +98,8 @@ async fn repo_log(req: Request<()>) -> tide::Result { _ => revwalk.push_head()?, }; let commits = revwalk - .map(|oid| repo.find_commit(oid.unwrap()).unwrap().clone()) // TODO error handling + .filter_map(|oid| repo.find_commit(oid.unwrap()).ok().clone()) // TODO error handling + .take(100) // Only get first 100 commits .collect(); let tmpl = RepoLogTemplate { repo: &repo, @@ -108,6 +109,36 @@ async fn repo_log(req: Request<()>) -> tide::Result { Ok(tmpl.into()) } +#[derive(Template)] +#[template(path = "refs.html")] // using the template in this path, relative +struct RepoRefTemplate<'a> { + repo: &'a Repository, + config: &'a Config, + branches: Vec<Reference<'a>>, + tags: Vec<Reference<'a>>, +} +async fn repo_refs(req: Request<()>) -> tide::Result { + let config = &Config::global(); + let repo = repo_from_request(&req.param("repo_name")?)?; + let branches = repo + .references()? + .filter_map(|x| x.ok()) + .filter(|x| x.is_branch()) + .collect(); + let tags = repo + .references()? + .filter_map(|x| x.ok()) + .filter(|x| x.is_tag()) + .collect(); + let tmpl = RepoRefTemplate { + repo: &repo, + config, + branches, + tags, + }; + Ok(tmpl.into()) +} + const HELP: &str = "\ mygit @@ -142,6 +173,7 @@ async fn main() -> Result<(), std::io::Error> { app.at("/:repo_name").get(repo_home); // ALSO do git pull at this url somehow ^ // app.at("/:repo_name/commit/:hash").get(repo_log); + app.at("/:repo_name/refs").get(repo_refs); app.at("/:repo_name/log").get(repo_log); app.at("/:repo_name/log/:ref").get(repo_log); // ref optional // app.at("/:repo_name/tree/:ref").get(repo_log); ref = master/main when not present diff --git a/templates/refs.html b/templates/refs.html @@ -0,0 +1,22 @@ +{% extends "base.html" %} + +{% block content %} + {% let name = repo.workdir().unwrap().file_name().unwrap().to_str().unwrap() %} + {% include "repo-navbar.html" %} + <h2>Branches</h2> + <table> + {% for branch in branches %} + <tr> + <td class="git-reference"> + <a href="/{{name}}/log/{{branch.shorthand().unwrap()}}">{{ branch.shorthand().unwrap() }}</a> + </td> + </tr> + {% endfor %} + </table> + <h2>Tags</h2> + <table> + {% for tag in tags %} + <tr><td class="git-reference">{{ tag.shorthand().unwrap() }}</td></tr> + {% endfor %} + </table> +{% endblock %} diff --git a/templates/static/style.css b/templates/static/style.css @@ -12,7 +12,7 @@ body { font-style: italic; } -.repo-link { +.repo-link, .git-reference { font-family: "Roboto Mono", monospace; }