steid

@jamesgill /

steid/src/infrastructure/web/file_history.rs
Rust8.5 KBCode·Blame·History·Raw
1//! One path's history — `/{handle}/repos/{name}/log/{rev}/-/{*path}`.
2//!
3//! The commit log narrowed to a file or a directory, in the log's own rows so the two
4//! read as one list seen at two widths. The URL is the log's with the tree's `/-/`
5//! separator on the end, which means a reader editing `tree` to `log` in the address
6//! bar lands here — the same trick blame's URL plays.
7//!
8//! **A file's history is followed through renames.** `git log --follow` is what makes
9//! the page worth having: a file's history that begins at the moment it was renamed is
10//! the history of the name, not of the file. A directory cannot be followed — git
11//! refuses — and does not ask to be.
12
13use topcoat::{
14 Result,
15 context::Cx,
16 icon::{icon, iconify::iconify_icon},
17 router::{error::not_found, page, path_param, query_params},
18 view::{attributes, component, view},
19};
20
21use crate::{
22 application::{PathLog, repo_path_log},
23 domain::{RefName, RepoPath},
24};
25
26use super::{
27 blame::{FileTab, view_toggle},
28 browse::{
29 PageQuery, Switch, commit_log, crumbs, is_markdown, log_url, path_log_url, ref_links,
30 refs_for, rev_switcher, tree_url,
31 },
32 context::{current_actor, memberships, orgs, queries, repos, server_error, timed_out},
33 layout::wide,
34 paging::{Pages, list_pages, requested_page, showing_at_most},
35 repo::{Tab, repo_for, repo_header},
36};
37
38/// `{rev}` from the path, raw — validation is [`RefName`]'s job, as on every other
39/// browsing route.
40#[path_param]
41struct Rev(str);
42
43/// `{*path}` from the path: every remaining segment, as one string.
44#[path_param]
45struct Path(str);
46
47#[page("/{handle}/repos/{name}/log/{rev}/-/{*path}")]
48async fn file_history_page(cx: &Cx) -> Result {
49 let rev = RefName::new(path_param::<Rev>(cx)).map_err(|_| not_found())?;
50 let path = RepoPath::new(path_param::<Path>(cx)).map_err(|_| not_found())?;
51
52 view! { history_of(rev: rev, path: path) }
53}
54
55/// The page. A component rather than a plain function because `view!` needs the request
56/// context in scope.
57#[component]
58async fn history_of(cx: &Cx, rev: RefName, path: RepoPath) -> Result {
59 let repo = repo_for(cx).await?;
60
61 let outcome = repo_path_log(
62 &repo.handle,
63 &repo.name,
64 Some(&rev),
65 &path,
66 requested_page(query_params::<PageQuery>(cx)?.page.as_deref()),
67 &current_actor(cx).await?,
68 &orgs(cx),
69 &memberships(cx),
70 &repos(cx),
71 &queries(cx),
72 )
73 .await;
74
75 // A walk that ran out of time is a statement about *this path* — a long history,
76 // or one `--follow` had to read every tree of — and not about the instance being
77 // broken, so it is a state rather than a 500. Blame answers the same way.
78 let log: Option<PathLog> = match outcome {
79 Ok(Some(log)) => Some(log),
80 Ok(None) => return Err(not_found().into()),
81 Err(error) if timed_out(&error) => None,
82 Err(other) => return Err(server_error(std::io::Error::other(other.to_string()))),
83 };
84
85 let handle = repo.handle.as_str();
86 let name = repo.name.as_str();
87 let at = rev.as_str();
88
89 let refs = refs_for(cx, &repo).await?;
90 let known = refs.contains(&rev);
91 let switch = Switch::PathLog(&path);
92 let branches = ref_links(handle, name, &refs.branches, at, &switch);
93 let tags = ref_links(handle, name, &refs.tags, at, &switch);
94
95 // Built out here rather than inside the view: each borrows a formatted string, and
96 // a temporary made inside a `view!` branch does not outlive it.
97 let sentence = match &log {
98 Some(log) => showing_at_most(
99 log.commits.offset(),
100 log.commits.items.len(),
101 log.commits.total,
102 log.capped,
103 "commits",
104 ),
105 None => String::new(),
106 };
107 let pages = match &log {
108 Some(log) => Pages::numbered(log.commits.page, log.commits.pages, |page| {
109 format!("{}?page={page}", path_log_url(handle, name, &rev, &path))
110 }),
111 None => Pages::numbered(1, 1, |page| page.to_string()),
112 };
113
114 view! {
115 wide(
116 repo_header(
117 repo: &repo,
118 rev: at,
119 active: Tab::Commits,
120 rev_switcher(current: at, known: known, branches: &branches, tags: &tags)
121 )
122
123 // The path, above the list rather than in the panel: the rows below are the
124 // log's own and gain nothing from a frame, so the breadcrumb is what says
125 // whose history this is.
126 <div class="mb-3 flex flex-wrap items-center justify-between gap-3">
127 crumbs(handle: handle, name: name, rev: &rev, path: &path)
128 <span class="flex items-center gap-3 font-mono text-xs text-muted-foreground">
129 match &log {
130 // A directory has no blame and no raw bytes, so it gets the one
131 // link that means anything: back to the listing.
132 Some(log) if !log.is_file => back_to_tree(
133 handle: handle,
134 name: name,
135 rev: &rev,
136 path: &path,
137 ),
138 _ => view_toggle(
139 handle: handle,
140 name: name,
141 rev: &rev,
142 path: &path,
143 active: FileTab::History,
144 markdown: is_markdown(&path),
145 ),
146 }
147 </span>
148 </div>
149
150 match &log {
151 Some(log) => {
152 // Said once, above the list, and only where it is true: a directory's
153 // history is not followed and claiming otherwise would be a lie about
154 // what the rows are.
155 if log.is_file {
156 <p class="mb-2 flex items-center gap-1.5 text-xs text-muted-foreground">
157 icon(data: iconify_icon!("feather:corner-right-down"), attrs: attributes! {
158 class="size-3.5 shrink-0"
159 })
160 "Renames are followed, so commits from before this file had its "
161 "current name are included."
162 </p>
163 }
164
165 // Said where the number is, because the number is what it qualifies:
166 // a followed walk stops at 500, and a reader deciding whether they
167 // have seen the whole history needs to know it did.
168 if log.capped {
169 <p class="mb-2 text-xs text-muted-foreground">
170 "Only the most recent 500 commits are counted here — "
171 "following renames reads a tree per commit, and a longer "
172 "walk is one that times out."
173 </p>
174 }
175
176 commit_log(handle: handle, name: name, commits: &log.commits.items)
177 list_pages(summary: sentence.as_str(), pages: &pages)
178 },
179 None => took_too_long(handle: handle, name: name, rev: &rev),
180 }
181 )
182 }
183}
184
185/// The way back to the listing, for a directory's history.
186#[component]
187async fn back_to_tree(handle: &str, name: &str, rev: &RefName, path: &RepoPath) -> Result {
188 view! {
189 <a
190 href=(tree_url(handle, name, rev, path))
191 class="text-muted-foreground hover:text-foreground"
192 >"Code"</a>
193 }
194}
195
196/// What a history that could not be walked in time says.
197///
198/// It offers the whole log rather than a retry: the same walk would take just as long
199/// again, and the unnarrowed log is the cheaper question that still shows history.
200#[component]
201async fn took_too_long(handle: &str, name: &str, rev: &RefName) -> Result {
202 view! {
203 <div class="rounded-lg border border-border px-4 py-10 text-center">
204 <p class="text-sm text-muted-foreground">
205 "Walking this path's history took too long."
206 </p>
207 <p class="mt-1 text-xs text-muted-foreground">
208 "Every commit has to be examined to see whether this path changed in it. "
209 <a
210 href=(log_url(handle, name, rev.as_str()))
211 class="text-primary hover:underline"
212 >"Read the whole log instead"</a>
213 "."
214 </p>
215 </div>
216 }
217}