1
8use askama::Template;
9use askama_web::WebTemplate;
10use axum::extract::{Path, State};
11use axum::response::IntoResponse;
12use gix::bstr::ByteSlice;
13use serde::Deserialize;
14
15use crate::error::{Error, Result};
16use crate::routes::{AppState, RepoName, commit_date, commit_timestamp};
17
18#[derive(Deserialize)]
19pub(super) struct TagPath {
20 tag: String,
21}
22
23struct TagSummary {
25 name: String,
26 date: String,
27 timestamp: i64,
28}
29
30#[derive(Template, WebTemplate)]
32#[template(path = "tag_list.html")]
33struct TagList {
34 repo: String,
35 tags: Vec<TagSummary>,
36}
37
38#[derive(Template, WebTemplate)]
40#[template(path = "tag_detail.html")]
41struct TagDetail {
42 repo: String,
43 tag: String,
44 tagger: Option<String>,
46 tag_date: Option<String>,
47 message: Option<String>,
49 commit_sha: String,
50 commit_author: String,
51 commit_date: String,
52}
53
54pub async fn list(
56 State(state): State<AppState>,
57 RepoName(repo): RepoName,
58) -> Result<impl IntoResponse> {
59 let git_repo =
60 gix::open(state.root.join(&repo)).map_err(|_| Error::RepoNotFound(repo.clone()))?;
61
62 let mut tags: Vec<TagSummary> = git_repo
66 .references()
67 .map_err(|e| Error::GitCorrupt(e.to_string()))?
68 .tags()
69 .into_iter()
70 .flatten()
71 .flatten()
72 .filter_map(|mut tag| {
73 let commit = tag.peel_to_commit().ok()?;
74 let date = commit_date(&commit)?;
75 let timestamp = commit_timestamp(&commit)?;
76 let name = tag.name().shorten().to_string();
77
78 Some(TagSummary { name, date, timestamp })
79 })
80 .collect();
81
82 tags.sort_by(|a, b| b.timestamp.cmp(&a.timestamp));
83
84 Ok(TagList { repo, tags })
85}
86
87pub async fn detail(
89 State(state): State<AppState>,
90 RepoName(repo): RepoName,
91 Path(params): Path<TagPath>,
92) -> Result<impl IntoResponse> {
93 let tag_name = params.tag;
94
95 let git_repo =
96 gix::open(state.root.join(&repo)).map_err(|_| Error::RepoNotFound(repo.clone()))?;
97
98 let ref_name = format!("refs/tags/{tag_name}");
99 let mut reference = git_repo
100 .find_reference(&ref_name)
101 .map_err(|_| Error::NotFound(format!("tag {tag_name}")))?;
102
103 let (tagger, tag_date, message) = match reference.peel_to_tag().ok() {
105 None => (None, None, None),
106 Some(t) => match t.decode() {
107 Err(_) => (None, None, None),
108 Ok(decoded) => {
109 let sig = decoded.tagger().ok().flatten();
110 let tagger = sig.as_ref().map(|s| s.name.to_str_lossy().into_owned());
111 let tag_date = sig.as_ref().and_then(|s| {
112 let mut parts = s.time.splitn(2, ' ');
114 let seconds: i64 = parts.next()?.parse().ok()?;
115 let offset_str = parts.next().unwrap_or("+0000");
116 let sign = if offset_str.starts_with('-') {
117 -1i32
118 } else {
119 1
120 };
121 let hhmm = offset_str.trim_start_matches(['+', '-']);
122 let h: i32 = hhmm.get(..2)?.parse().ok()?;
123 let m: i32 = hhmm.get(2..4)?.parse().ok()?;
124 let offset_secs = sign * (h * 3600 + m * 60);
125 let zone = jiff::tz::TimeZone::fixed(
126 jiff::tz::Offset::from_seconds(offset_secs).ok()?,
127 );
128 jiff::Timestamp::from_second(seconds)
129 .map(|ts| ts.to_zoned(zone).date().to_string())
130 .ok()
131 });
132 let message = decoded.message.to_str_lossy().trim().to_string();
133 let message = if message.is_empty() {
134 None
135 } else {
136 Some(message)
137 };
138 (tagger, tag_date, message)
139 }
140 },
141 };
142
143 let commit = reference
144 .peel_to_commit()
145 .map_err(|e| Error::GitCorrupt(e.to_string()))?;
146
147 let commit_sha = commit.id().to_string();
148 let commit_author = commit
149 .author()
150 .map(|a| a.name.to_string())
151 .unwrap_or_default();
152 let commit_date = commit_date(&commit).unwrap_or_default();
153
154 Ok(TagDetail {
155 repo,
156 tag: tag_name,
157 tagger,
158 tag_date,
159 message,
160 commit_sha,
161 commit_author,
162 commit_date,
163 })
164}