mygit

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

commit d81a4c63997dfaefea0b46290b9def6778609f93
parent 4984c7cca22b868286d001f009228cebcba97af6
Author: Johann150 <johann@qwertqwefsday.eu>
Date:   Thu, 15 Jul 2021 16:01:20 +0200

add tag and branch badges to commits

Diffstat:
Msrc/main.rs | 41+++++++++++++++++++++++++++++++++++++++++
Mtemplates/commit.html | 1+
Mtemplates/static/style.css | 22++++++++++++++++++++++
3 files changed, 64 insertions(+), 0 deletions(-)

diff --git a/src/main.rs b/src/main.rs @@ -452,6 +452,47 @@ impl RepoCommitTemplate<'_> { .for_each(|line| highlighter.parse_html_for_line_which_includes_newline(line)); highlighter.finalize() } + + fn refs(&self) -> String { + use git2::{BranchType, DescribeFormatOptions, DescribeOptions}; + + let mut html = String::new(); + + // add badge if this commit is a tag + let descr = self.commit.as_object().describe( + &DescribeOptions::new() + .describe_tags() + .max_candidates_tags(0), + ); + if let Ok(descr) = descr { + // this can be a tag or lightweight tag, the refs path will redirect + html += &format!( + r#"<a href="/{0}/refs/{1}" class="badge tag">{1}</a>"#, + filters::repo_name(self.repo).unwrap(), + descr + .format(Some(DescribeFormatOptions::new().abbreviated_size(0))) + .unwrap(), + ); + } + + // also add badge if this is the tip of a branch + let branches = self + .repo + .branches(Some(BranchType::Local)) + .unwrap() + .filter_map(|x| if let Ok(x) = x { Some(x.0) } else { None }) + .filter(|branch| branch.get().peel_to_commit().unwrap().id() == self.commit.id()); + for branch in branches { + // branch is not a reference, just a fancy name for a commit + html += &format!( + r#" <a href="/{0}/commit/{1}" class="badge branch">{1}</a>"#, + filters::repo_name(self.repo).unwrap(), + branch.name().unwrap().unwrap(), + ); + } + + html + } } async fn repo_commit(req: Request<()>) -> tide::Result { diff --git a/templates/commit.html b/templates/commit.html @@ -5,6 +5,7 @@ {% block content %} {% include "repo-navbar.html" %} <b>Commit:</b> <span class="commit-hash">{{commit.id()}}</span> (<a href="/{{repo|repo_name|urlencode_strict}}/tree/{{commit.id()}}">tree</a>) + {{self.refs()|safe}} <br> {% for parent_id in self.parent_ids() %} <b>Parent:</b> <a href="/{{repo|repo_name|urlencode_strict}}/commit/{{parent_id}}" class="commit-hash">{{parent_id}}</a> (<a href="/{{repo|repo_name|urlencode_strict}}/tree/{{parent_id}}">tree</a>) diff --git a/templates/static/style.css b/templates/static/style.css @@ -84,6 +84,10 @@ table.core { border-collapse: collapse; } +.badge { + padding: .2em; +} + @media only screen and (max-width: 600px) { body { font-size: 16px; @@ -169,6 +173,15 @@ a, .variable, .entity.other.inherited-class, .entity.name.tag, .storage.type, .m color: #697807; /* SolAArized green-light */ } +.badge { + color: #fdf6e3; /* SolAArized background-light */ + background-color: #697807; /* SolAArized green-light */ +} + +.badge.tag { + background-color: #c64710; /* SolAArized orange-light */ +} + /* Dark colour scheme for browsers that support it. */ @media (prefers-color-scheme: dark) { @@ -227,4 +240,13 @@ a, .variable, .entity.other.inherited-class, .entity.name.tag, .storage.type, .m .keyword, .storage, .support, .support.type, .support.class, .punctuation.definition.variable, .support.function, .keyword.other.special-method.ruby, .support.function.be.latex, .storage.type.class.python, .storage.type.function.python, .storage.modifier.global.python, .storage.modifier.cpp, .markup.inserted, .markup.quote, .markup.inserted.git_gutter, .meta.diff.header.to-file { color: #859900; /* SolAArized green-dark */ } + + .badge { + color: #002b36; /* SolAArized background-dark */ + background-color: #859900; /* SolAArized green-dark */ + } + + .badge.tag { + background-color: #b58900; /* SolAArized orange-dark */ + } }