Show a file's history and link to its lines at a fixed commit
Browse filesFiles and folders now have a History link listing only the commits that changed them. A file's Permalink opens it at the commit being viewed, so a shared link keeps showing the same lines after the branch moves. Line numbers link to themselves (#L12), and lines=12-20 marks a range, which the permalink keeps. The browser runs no scripts, so selection is marked by the server and banded by the stylesheet. Highlighted code stays cached per file; only the line-number column is built per request, since only it depends on the selection. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Author
- Um9i vulgarmessiah@gmail.com
- Date
- 22 Nov 2026, 23:21
- Commit
- a4d3c9a3c483
- Parent
- a76c350
12 files changed +227−69
+2−2
| @@ -10,8 +10,8 @@ Deferred until a second person pushes to the server: individual accounts and rep | ||
| ## Items, in order | ||
| - [ ] **SSH key list and revoke.** `gitv keys [owner]` lists every active key with its owner, SHA-256 fingerprint (as `ssh-keygen -l` prints it) and label. `gitv add-key --label <text>` records a label for a new key. `gitv remove-key <fingerprint>` removes exactly that key, atomically and under the same lock as `add-key`. Verify that a revoked key no longer reaches anything, that a second key for the same owner still works, that an unknown fingerprint changes nothing, and that hand-written lines (options, comments, tabs) survive a removal. | ||
| - [ ] **File history and line links.** A file's page links to the commits that changed it (`log` limited to that path, paged like branch history). Source lines carry `#L<n>` anchors, and a "Permalink" link pins the current file to its commit id, so a copied link opens the same lines after the branch moves. A range (`?lines=10-20`) is highlighted by the server, with no JavaScript. | ||
| - [x] **SSH key list and revoke.** `gitv keys [owner]` lists every active key with its owner, SHA-256 fingerprint (as `ssh-keygen -l` prints it) and label. `gitv add-key --label <text>` records a label for a new key. `gitv remove-key <fingerprint>` removes exactly that key, atomically and under the same lock as `add-key`. Verify that a revoked key no longer reaches anything, that a second key for the same owner still works, that an unknown fingerprint changes nothing, and that hand-written lines (options, comments, tabs) survive a removal. | ||
| - [x] **File history and line links.** A file's page links to the commits that changed it (`log` limited to that path, paged like branch history). Source lines carry `#L<n>` anchors, and a "Permalink" link pins the current file to its commit id, so a copied link opens the same lines after the branch moves. A range (`?lines=10-20`) is highlighted by the server, with no JavaScript. | ||
| - [ ] **Compare revisions.** `/<owner>/<name>/compare?from=<rev>&to=<rev>` shows the commits in `from..to`, the changed files, and the diff against the merge base. It reuses the commit page's diff rendering, output caps and cache. Cover a release-to-release comparison and a feature branch that has diverged from its target, and the access rules for private repositories. | ||
| - [ ] **Description, default branch and index filter.** Over SSH (and as root): `description <name> <text>` writes git's `description` file, and `default-branch <name> <branch>` points `HEAD` at an existing branch. The index shows descriptions and can be filtered by name with a plain GET form (`?q=`). The repository header shows its visibility to the signed-in operator. | ||
| - [ ] **Tagged source archives.** The tags page links a `.tar.gz` of each tag, made by `git archive` and served through the download pool, under the repository's access rules. Tag annotations remain the release notes. Verify with a tagged GITv release that the archive matches `git archive` run locally. | ||
+2−0
| @@ -5,6 +5,8 @@ Git hosting with SSH pushes, public and private repository browsing, and read-on | ||
| - `gitv`: the SSH forced-command handler (`serve`) and the root admin CLI (`create`, `visibility`, `delete`, `add-key`, `keys`, `remove-key`, `trash`, `restore`, `purge`) | ||
| - `gitv-web`: the read-only browser (repositories, tree, blob, raw, log, commit, tags), with Markdown rendering and syntax highlighting (`crates/gitv-web/src/render.rs`) | ||
| **Linking to code.** A file's History lists the commits that changed it, and a folder's History does the same for everything inside. Permalink opens the file at the commit you're looking at, so the link keeps showing the same content after the branch moves. Click a line number to link that line (`#L12`). To link a range, add `lines=` to the address, for example `…&lines=12-20#L12`; the server marks those lines, and Permalink keeps the range. | ||
| **Who can read what.** Over SSH each key reaches only its owner's repositories. The web browser has one account, and whoever signs in to it can read every repository on the server. It binds to localhost only; put it behind an HTTPS reverse proxy. | ||
| Existing and newly created repositories default to private. Publish one as its SSH owner or as the server administrator: | ||
| @@ -93,7 +93,7 @@ main{max-width:1080px;margin:0 auto;padding:32px 24px 72px} | ||
| .panel{background:var(--surface);border:1px solid var(--rule);border-radius:10px;overflow:hidden;margin:0 0 24px} | ||
| .panel-head{display:flex;align-items:center;justify-content:space-between;gap:8px 16px;flex-wrap:wrap;min-height:48px; | ||
| padding:8px 16px;font-size:14px;background:var(--sunk);border-bottom:1px solid var(--rule)} | ||
| .panel-head .lead{display:flex;align-items:center;gap:10px;min-width:0;flex:1} | ||
| .panel-head .lead{display:flex;align-items:center;gap:10px;min-width:0;flex:1 1 12em} | ||
| .panel-head .lead b{font-weight:600;overflow-wrap:anywhere} | ||
| .panel-head .who{white-space:nowrap} | ||
| .panel-head .side{display:flex;align-items:center;gap:12px;color:var(--ink-2);font-size:13px;white-space:nowrap} | ||
| @@ -171,10 +171,19 @@ a.entry:hover .name{color:var(--accent)} | ||
| .segmented a[aria-current]{color:var(--accent);background:var(--accent-wash)} | ||
| /* Code */ | ||
| .code-view{display:grid;grid-template-columns:auto minmax(0,1fr);font:13.5px/1.6 var(--f-mono);tab-size:4} | ||
| .code-view{display:grid;grid-template-columns:auto minmax(0,1fr);font:13.5px/1.6 var(--f-mono);tab-size:4; | ||
| position:relative;isolation:isolate} | ||
| .code-view pre{margin:0;padding:12px 0;font:inherit} | ||
| .code-view .gutter{padding:12px 14px 12px 18px;text-align:right;color:var(--ink-2);opacity:.7;user-select:none; | ||
| .code-view .gutter{padding:12px 14px 12px 18px;text-align:right;color:var(--ink-2);user-select:none; | ||
| border-right:1px solid var(--rule-2);background:var(--sunk)} | ||
| .code-view .gutter a{color:inherit} | ||
| .code-view .gutter a:hover{color:var(--accent);text-decoration:none} | ||
| /* A linked line (#L12) or range (lines=12-20) is banded across the code. The band sits in the | ||
| line's own place in the gutter's flow and stretches to the whole view, under the text. */ | ||
| .code-view .gutter a:target,.code-view .gutter a.sel{color:var(--ink);font-weight:600} | ||
| .code-view .gutter a:target::after,.code-view .gutter a.sel::after{content:"";position:absolute;left:0;right:0; | ||
| height:1.6em;margin-top:0;background:var(--target);z-index:-1;pointer-events:none} | ||
| .code-view .gutter a:target{scroll-margin-top:30vh} | ||
| .code-view .code{padding-left:18px;padding-right:24px;overflow-x:auto;white-space:pre} | ||
| .note{padding:28px 20px;text-align:center;color:var(--ink-2)} | ||
| @@ -82,12 +82,21 @@ pub(crate) struct Empty<'a> { | ||
| pub name: &'a str, | ||
| } | ||
| /// Highlighted source with a line-number gutter. | ||
| /// Highlighted source with a gutter of line numbers. Each number is an anchor that links to | ||
| /// itself (`#L<n>`), and the `selected` lines are marked, so the stylesheet can highlight a | ||
| /// linked line or range without any script. | ||
| #[derive(Template)] | ||
| #[template(path = "code.html")] | ||
| pub(crate) struct Code { | ||
| pub gutter: String, | ||
| pub code: String, | ||
| pub(crate) struct Code<'a> { | ||
| pub lines: usize, | ||
| pub selected: Option<(usize, usize)>, | ||
| pub code: &'a str, | ||
| } | ||
| impl Code<'_> { | ||
| fn is_selected(&self, n: &usize) -> bool { | ||
| self.selected.is_some_and(|(from, to)| (from..=to).contains(n)) | ||
| } | ||
| } | ||
| /// Preview and Code links for a Markdown file. | ||
| @@ -108,6 +117,8 @@ pub(crate) struct Blob<'a> { | ||
| pub name: &'a str, | ||
| pub facts: &'a str, | ||
| pub views: Option<Views>, | ||
| pub history: String, | ||
| pub permalink: String, | ||
| pub raw: String, | ||
| pub content: &'a str, | ||
| } | ||
| @@ -257,6 +268,7 @@ pub(super) struct TreeHead { | ||
| pub url: String, | ||
| pub sha_link: String, | ||
| pub when: String, | ||
| pub history: String, | ||
| } | ||
| #[derive(Template)] | ||
| @@ -1,10 +1,8 @@ | ||
| //! A file, highlighted or rendered, and its raw download. | ||
| use std::sync::Arc; | ||
| use gitv_web::render; | ||
| use super::tree::render_file; | ||
| use super::tree::{Rendered, render_file}; | ||
| use super::{RAW_LIMIT, Repo, SHOW_LIMIT, entry}; | ||
| use crate::html::{icon, size}; | ||
| use crate::urls::{Q, clean_path, file_name, lossy, param, parent_of}; | ||
| @@ -45,17 +43,15 @@ pub(crate) fn view_blob(repo: &Repo, q: &Q, raw: bool) -> Option<View> { | ||
| let too_big = item.size > SHOW_LIMIT; | ||
| let markdown = !too_big && render::is_markdown(&name); | ||
| let as_code = !markdown || param(q, "view").as_deref() == Some("code"); | ||
| let rendered: Arc<str> = if too_big { | ||
| format!( | ||
| "{}\x1f{}", | ||
| size(item.size), | ||
| r#"<p class="note">This file is too large to show here. Use Raw to open it.</p>"# | ||
| ) | ||
| .into() | ||
| let selected = param(q, "lines").and_then(|value| line_range(&value)); | ||
| let rendered = if too_big { | ||
| Rendered { | ||
| facts: size(item.size), | ||
| html: r#"<p class="note">This file is too large to show here. Use Raw to open it.</p>"#.into(), | ||
| } | ||
| } else { | ||
| render_file(repo, &item.oid, item.size, &name, parent_of(&path), &reference, as_code)? | ||
| render_file(repo, &item.oid, item.size, &name, parent_of(&path), &reference, as_code, selected)? | ||
| }; | ||
| let (facts, content) = rendered.split_once('\x1f').unwrap_or(("", &rendered)); | ||
| let views = markdown.then(|| templates::Views { | ||
| preview: repo.url_at("blob", &reference, &path, ""), | ||
| @@ -70,11 +66,60 @@ pub(crate) fn view_blob(repo: &Repo, q: &Q, raw: bool) -> Option<View> { | ||
| locator: repo.locator("blob", &reference, &path), | ||
| file_icon: icon("file"), | ||
| name: &name, | ||
| facts, | ||
| facts: &rendered.facts, | ||
| views, | ||
| history: repo.url_at("log", &reference, &path, ""), | ||
| permalink: repo.url_at("blob", commit.as_bytes(), &path, &pinned_extra(markdown && as_code, selected)), | ||
| raw: repo.url_at("raw", &reference, &path, ""), | ||
| content, | ||
| content: &rendered.html, | ||
| } | ||
| .to_string(), | ||
| )) | ||
| } | ||
| /// The lines a `lines` parameter selects: `12` or `12-20`, counted from 1. A range given | ||
| /// backwards is read forwards. Anything else selects nothing rather than failing the page. | ||
| fn line_range(value: &str) -> Option<(usize, usize)> { | ||
| let (from, to) = value.split_once('-').unwrap_or((value, value)); | ||
| let (from, to): (usize, usize) = (from.parse().ok()?, to.parse().ok()?); | ||
| (from > 0 && to > 0).then(|| (from.min(to), from.max(to))) | ||
| } | ||
| /// What a permalink keeps of this view: the Code view of a Markdown file, and the selected | ||
| /// lines, scrolled to the first of them. | ||
| fn pinned_extra(code_view: bool, selected: Option<(usize, usize)>) -> String { | ||
| let mut extra = Vec::new(); | ||
| if code_view { | ||
| extra.push("view=code".to_string()); | ||
| } | ||
| if let Some((from, to)) = selected { | ||
| extra.push(if from == to { format!("lines={from}") } else { format!("lines={from}-{to}") }); | ||
| } | ||
| let mut extra = extra.join("&"); | ||
| if let Some((from, _)) = selected { | ||
| extra.push_str(&format!("#L{from}")); | ||
| } | ||
| extra | ||
| } | ||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| #[test] | ||
| fn line_ranges_are_read_leniently() { | ||
| assert_eq!(line_range("12"), Some((12, 12))); | ||
| assert_eq!(line_range("12-20"), Some((12, 20))); | ||
| assert_eq!(line_range("20-12"), Some((12, 20))); | ||
| for bad in ["", "0", "0-3", "a", "3-", "-3", "1-2-3", "99999999999999999999999"] { | ||
| assert_eq!(line_range(bad), None, "{bad}"); | ||
| } | ||
| } | ||
| #[test] | ||
| fn permalinks_keep_the_view_and_the_lines() { | ||
| assert_eq!(pinned_extra(false, None), ""); | ||
| assert_eq!(pinned_extra(true, Some((4, 4))), "view=code&lines=4#L4"); | ||
| assert_eq!(pinned_extra(false, Some((4, 9))), "lines=4-9#L4"); | ||
| } | ||
| } | ||
| @@ -1,25 +1,31 @@ | ||
| //! A branch or tag's history. | ||
| //! A branch or tag's history, or the history of one file or folder on it. | ||
| use std::ffi::OsStr; | ||
| use std::os::unix::ffi::OsStrExt; | ||
| use super::{PAGE, Repo}; | ||
| use crate::html::{ago, icon, subject_or_placeholder}; | ||
| use crate::repository::Head; | ||
| use crate::urls::{Q, lossy, param}; | ||
| use crate::urls::{Q, clean_path, lossy, param}; | ||
| use crate::{View, templates}; | ||
| pub(crate) fn view_log(repo: &Repo, q: &Q) -> Option<View> { | ||
| let reference = repo.reference(q); | ||
| let number: usize = param(q, "page").and_then(|p| p.parse().ok()).unwrap_or(0); | ||
| let skip = number.checked_mul(PAGE)?; | ||
| let path = clean_path(q.get("path").map_or(&[][..], Vec::as_slice))?; | ||
| let commit = repo.reader.resolve(&reference)?; | ||
| let out = repo.reader.git(&[ | ||
| "log", | ||
| &format!("-n{}", PAGE + 1), | ||
| &format!("--skip={skip}"), | ||
| "--date=format:%-d %b %Y", | ||
| Head::FORMAT, | ||
| &commit, | ||
| "--", | ||
| ])?; | ||
| let (count, skip) = (format!("-n{}", PAGE + 1), format!("--skip={skip}")); | ||
| // The path reaches git as bytes after `--`, and git's pathspecs are literal here, so a | ||
| // name like `star*.txt` means only that file. | ||
| let mut args: Vec<&OsStr> = | ||
| [OsStr::new("log"), count.as_ref(), skip.as_ref(), "--date=format:%-d %b %Y".as_ref(), Head::FORMAT.as_ref()] | ||
| .to_vec(); | ||
| args.extend([OsStr::new(&commit), OsStr::new("--")]); | ||
| if !path.is_empty() { | ||
| args.push(OsStr::from_bytes(&path)); | ||
| } | ||
| let out = repo.reader.git(&args)?; | ||
| let lines: Vec<&str> = out.lines().collect(); | ||
| let tags = repo.reader.tags_by_commit(); | ||
| let mut rows = Vec::new(); | ||
| @@ -42,13 +48,18 @@ pub(crate) fn view_log(repo: &Repo, q: &Q) -> Option<View> { | ||
| tags: labels, | ||
| }); | ||
| } | ||
| let page_url = |n: usize| repo.url_at("log", &reference, b"", &format!("page={n}")); | ||
| let page_url = |n: usize| repo.url_at("log", &reference, &path, &format!("page={n}")); | ||
| let title = if path.is_empty() { | ||
| format!("Commits on {}", lossy(&reference)) | ||
| } else { | ||
| format!("History of {} on {}", lossy(&path), lossy(&reference)) | ||
| }; | ||
| Some(View::Html( | ||
| templates::History { | ||
| title: format!("Commits on {}", lossy(&reference)), | ||
| title, | ||
| signed_in: repo.signed_in, | ||
| masthead: repo.masthead("log", &reference, Some(&commit)), | ||
| locator: repo.locator("log", &reference, b""), | ||
| locator: repo.locator("log", &reference, &path), | ||
| rows, | ||
| newer: number.checked_sub(1).map(page_url), | ||
| older: (lines.len() > PAGE).then(|| page_url(number + 1)), | ||
| @@ -179,6 +179,9 @@ impl Repo { | ||
| .strip_prefix(b"refs/heads/") | ||
| .or_else(|| reference.strip_prefix(b"refs/tags/")) | ||
| .unwrap_or(reference); | ||
| // A permalink pins a full commit id; its first twelve digits say enough. | ||
| let pinned = matches!(shown.len(), 40 | 64) && shown.iter().all(u8::is_ascii_hexdigit); | ||
| let shown = if pinned { &shown[..12] } else { shown }; | ||
| templates::Locator { | ||
| branch_icon: icon("branch"), | ||
| chevron_icon: icon("chevron"), | ||
| @@ -1,7 +1,6 @@ | ||
| //! Folder listings, with the latest change to each entry and the README. | ||
| use std::ffi::OsStr; | ||
| use std::sync::Arc; | ||
| use gitv_core::config; | ||
| use gitv_web::render; | ||
| @@ -72,13 +71,11 @@ pub(crate) fn view_tree(repo: &Repo, q: &Q) -> Option<View> { | ||
| let readme = readmes.clone().find(|e| render::is_markdown(&lossy(&e.name))).or_else(|| readmes.clone().next()); | ||
| let readme = readme.filter(|e| e.size <= SHOW_LIMIT).map(|readme| { | ||
| let name = lossy(&readme.name); | ||
| let rendered = | ||
| render_file(repo, &readme.oid, readme.size, &name, &path, &reference, !render::is_markdown(&name)); | ||
| let content = rendered | ||
| .as_deref() | ||
| .and_then(|r| r.split_once('\x1f')) | ||
| .map_or(r#"<p class="note">This README couldn't be read.</p>"#, |(_, html)| html); | ||
| (name.into_owned(), content.to_string()) | ||
| let as_code = !render::is_markdown(&name); | ||
| let rendered = render_file(repo, &readme.oid, readme.size, &name, &path, &reference, as_code, None); | ||
| let content = | ||
| rendered.map_or_else(|| r#"<p class="note">This README couldn't be read.</p>"#.to_string(), |r| r.html); | ||
| (name.into_owned(), content) | ||
| }); | ||
| let title = if path.is_empty() { | ||
| format!("{}/{}", repo.owner, repo.name) | ||
| @@ -91,6 +88,7 @@ pub(crate) fn view_tree(repo: &Repo, q: &Q) -> Option<View> { | ||
| subject: subject_or_placeholder(&head.subject).to_string(), | ||
| author: head.author, | ||
| when: ago(&head.stamp, &head.date), | ||
| history: repo.url_at("log", &reference, &path, ""), | ||
| }); | ||
| Some(View::Html( | ||
| templates::Tree { | ||
| @@ -109,11 +107,21 @@ pub(crate) fn view_tree(repo: &Repo, q: &Q) -> Option<View> { | ||
| )) | ||
| } | ||
| /// A file rendered for display, as `facts` 0x1f `html`: highlighted code, or Markdown with | ||
| /// links into this repository at `reference` (the file lives in `dir`). The file view and the | ||
| /// README panel both use this, so they share one cached value per rendering instead of | ||
| /// storing different shapes under one key. The facts and the rendering are cached together, | ||
| /// so a repeat view reads no blob at all. | ||
| /// A file rendered for display: a summary (`facts`) and its HTML. | ||
| pub(crate) struct Rendered { | ||
| pub(crate) facts: String, | ||
| pub(crate) html: String, | ||
| } | ||
| /// A file rendered for display: highlighted code, or Markdown with links into this repository | ||
| /// at `reference` (the file lives in `dir`). The file view and the README panel both use this, | ||
| /// so they share one cached value per rendering instead of storing different shapes under one | ||
| /// key. The facts and the rendering are cached together, so a repeat view reads no blob at all. | ||
| /// | ||
| /// Code is cached as `facts` 0x1f `line count` 0x1f `highlighted`, and its gutter is built per | ||
| /// request, because only the gutter depends on which lines a link selects. Anything else is | ||
| /// cached as `facts` 0x1f 0x1f `html`. | ||
| #[allow(clippy::too_many_arguments)] | ||
| pub(crate) fn render_file( | ||
| repo: &Repo, | ||
| oid: &str, | ||
| @@ -122,7 +130,8 @@ pub(crate) fn render_file( | ||
| dir: &[u8], | ||
| reference: &[u8], | ||
| as_code: bool, | ||
| ) -> Option<Arc<str>> { | ||
| selected: Option<(usize, usize)>, | ||
| ) -> Option<Rendered> { | ||
| let key = if as_code { | ||
| // Highlighting depends only on the content and the file's type, so it's shared. | ||
| format!("code:{oid}:{name}") | ||
| @@ -130,26 +139,32 @@ pub(crate) fn render_file( | ||
| // Links depend on the repository, folder and ref too. | ||
| repo.key("md", oid, &format!("{}:{}", encode(dir), encode(reference))) | ||
| }; | ||
| cached(key, || { | ||
| let value = cached(key, || { | ||
| let data = repo.reader.git_bytes(&["cat-file", "blob", oid])?; | ||
| if data.iter().take(8000).any(|b| *b == 0) { | ||
| let note = r#"<p class="note">This is a binary file. Use Raw to download it.</p>"#; | ||
| return Some(format!("{}\x1f{note}", size(file_size))); | ||
| return Some(format!("{}\x1f\x1f{note}", size(file_size))); | ||
| } | ||
| let text = String::from_utf8_lossy(&data); | ||
| let facts = format!("{}, {}", plural(text.lines().count(), "line", "lines"), size(file_size)); | ||
| let html = if as_code { | ||
| code_view(name, &text) | ||
| let lines = text.lines().count(); | ||
| let facts = format!("{}, {}", plural(lines, "line", "lines"), size(file_size)); | ||
| Some(if as_code { | ||
| format!("{facts}\x1f{}\x1f{}", lines.max(1), render::highlight(name, &text)) | ||
| } else { | ||
| format!(r#"<article class="markdown">{}</article>"#, repo.markdown(&text, dir, reference)) | ||
| }; | ||
| Some(format!("{facts}\x1f{html}")) | ||
| }) | ||
| let html = repo.markdown(&text, dir, reference); | ||
| format!("{facts}\x1f\x1f<article class=\"markdown\">{html}</article>") | ||
| }) | ||
| })?; | ||
| let mut parts = value.splitn(3, '\x1f'); | ||
| let (facts, lines, html) = (parts.next()?, parts.next()?, parts.next()?); | ||
| let html = match lines.parse() { | ||
| Ok(lines) => code_view(lines, html, selected), | ||
| Err(_) => html.to_string(), | ||
| }; | ||
| Some(Rendered { facts: facts.to_string(), html }) | ||
| } | ||
| /// Highlighted source with a line-number gutter. | ||
| pub(crate) fn code_view(path: &str, text: &str) -> String { | ||
| let lines = text.lines().count().max(1); | ||
| let gutter: String = (1..=lines).map(|n| format!("{n}\n")).collect(); | ||
| templates::Code { gutter, code: render::highlight(path, text) }.to_string() | ||
| /// Highlighted source with its line-number gutter. | ||
| pub(crate) fn code_view(lines: usize, code: &str, selected: Option<(usize, usize)>) -> String { | ||
| templates::Code { lines, selected, code }.to_string() | ||
| } | ||
| @@ -1,5 +1,5 @@ | ||
| {% extends "base.html" %} | ||
| {% block content %} | ||
| {{ masthead|safe }}{{ locator|safe }} | ||
| <section class="panel"><div class="panel-head"><div class="lead">{{ file_icon|safe }}<b>{{ name }}</b><span class="quiet">{{ facts }}</span></div><div class="side">{% if let Some(views) = views %}<span class="segmented"><a href="{{ views.preview }}"{% if !views.as_code %} aria-current="true"{% endif %}>Preview</a><a href="{{ views.code }}"{% if views.as_code %} aria-current="true"{% endif %}>Code</a></span>{% endif %}<a class="button" href="{{ raw }}">Raw</a></div></div>{{ content|safe }}</section> | ||
| <section class="panel"><div class="panel-head"><div class="lead">{{ file_icon|safe }}<b>{{ name }}</b><span class="quiet">{{ facts }}</span></div><div class="side">{% if let Some(views) = views %}<span class="segmented"><a href="{{ views.preview }}"{% if !views.as_code %} aria-current="true"{% endif %}>Preview</a><a href="{{ views.code }}"{% if views.as_code %} aria-current="true"{% endif %}>Code</a></span>{% endif %}<a class="button" href="{{ history }}">History</a><a class="button" href="{{ permalink }}" title="This file at this commit, unchanged when the branch moves">Permalink</a><a class="button" href="{{ raw }}">Raw</a></div></div>{{ content|safe }}</section> | ||
| {% endblock %} | ||
| @@ -1 +1,2 @@ | ||
| <div class="code-view"><pre class="gutter" aria-hidden="true">{{ gutter }}</pre><pre class="code">{{ code|safe }}</pre></div> | ||
| <div class="code-view"><pre class="gutter">{% for n in 1..=lines %}<a id="L{{ n }}" href="#L{{ n }}"{% if self.is_selected(n) %} class="sel"{% endif %}>{{ n }}</a> | ||
| {% endfor %}</pre><pre class="code">{{ code|safe }}</pre></div> | ||
| @@ -3,7 +3,7 @@ | ||
| {{ masthead|safe }}{{ locator|safe }} | ||
| <div class="panel"> | ||
| {% if let Some(h) = head %} | ||
| <div class="panel-head"><div class="lead"><b class="who">{{ h.author }}</b><a class="subject" href="{{ h.url }}">{{ h.subject }}</a></div><div class="side">{{ h.sha_link|safe }}<span>{{ h.when }}</span></div></div> | ||
| <div class="panel-head"><div class="lead"><b class="who">{{ h.author }}</b><a class="subject" href="{{ h.url }}">{{ h.subject }}</a></div><div class="side">{{ h.sha_link|safe }}<span>{{ h.when }}</span><a href="{{ h.history }}">History</a></div></div> | ||
| {% endif %} | ||
| <ul class="rows tree-list"> | ||
| {% if let Some(url) = up %}<li><a class="entry" href="{{ url }}">{{ crate::html::icon("up")|safe }}<span class="name">..</span></a></li>{% endif %} | ||
| @@ -463,7 +463,13 @@ fn file_view_highlights_with_line_numbers() { | ||
| let page = s.get("/ada/site/blob?path=src/main.rs"); | ||
| assert_eq!(page.status, 200); | ||
| let html = page.text(); | ||
| assert!(html.contains("<pre class=\"gutter\" aria-hidden=\"true\">1\n2\n3\n</pre>"), "{html}"); | ||
| assert!( | ||
| html.contains( | ||
| "<pre class=\"gutter\"><a id=\"L1\" href=\"#L1\">1</a>\n<a id=\"L2\" href=\"#L2\">2</a>\n\ | ||
| <a id=\"L3\" href=\"#L3\">3</a>\n</pre>" | ||
| ), | ||
| "every line number links to itself: {html}" | ||
| ); | ||
| assert!(html.contains("hl-"), "highlighted"); | ||
| assert!(html.contains("changed") && !html.contains("<hi>"), "current content, escaped"); | ||
| assert!(html.contains("3 lines, 39 bytes")); | ||
| @@ -977,3 +983,57 @@ fn sigterm_lets_a_download_in_progress_finish() { | ||
| assert!(server.logged().contains("stopping; finishing requests in progress"), "{}", server.logged()); | ||
| assert!(std::net::TcpStream::connect(("127.0.0.1", server.port)).is_err(), "no new connections"); | ||
| } | ||
| #[test] | ||
| fn file_history_lists_only_the_commits_that_changed_it() { | ||
| let s = server(); | ||
| let f = fixture(); | ||
| let html = s.get("/ada/site/log?path=src/main.rs").text(); | ||
| let expected = git(&f.repos.join("ada/site.git"), &["log", "--format=%s", "main", "--", "src/main.rs"]); | ||
| assert_eq!(expected.lines().collect::<Vec<_>>(), ["Remove old.txt", "First commit"]); | ||
| for subject in expected.lines() { | ||
| assert!(html.contains(subject), "{subject} missing: {html}"); | ||
| } | ||
| assert!(!html.contains("Merge side") && !html.contains("Add a submodule"), "{html}"); | ||
| assert!(html.contains("<title>History of src/main.rs on main"), "{html}"); | ||
| // Literal paths: star*.txt is one file, not a pattern matching globber.txt's commit. | ||
| let star = s.get("/ada/site/log?path=star%2A.txt").text(); | ||
| assert_eq!(star.matches("class=\"commit-row\"").count(), 1, "{star}"); | ||
| // A folder's history, linked from its listing, and pages that keep the path. | ||
| let folder = s.get("/ada/site/tree?path=glob%2A").text(); | ||
| assert!(folder.contains("href=\"/ada/site/log?ref=main&path=glob%2A\">History"), "{folder}"); | ||
| let long = s.get("/ada/site/log?ref=long&path=README.md").text(); | ||
| assert_eq!(long.matches("class=\"commit-row\"").count(), 1, "the empty commits didn't touch README.md"); | ||
| assert_eq!(s.get("/ada/site/log?path=../README.md").status, 404); | ||
| } | ||
| #[test] | ||
| fn file_links_pin_the_commit_and_the_lines() { | ||
| let s = server(); | ||
| let f = fixture(); | ||
| let tip = f.tip.as_str(); | ||
| let html = s.get("/ada/site/blob?path=src/main.rs").text(); | ||
| assert!(html.contains("href=\"/ada/site/log?ref=main&path=src/main.rs\">History"), "{html}"); | ||
| let permalink = format!("href=\"/ada/site/blob?ref={tip}&path=src/main.rs\""); | ||
| assert!(html.contains(&permalink), "the permalink names the commit: {html}"); | ||
| // After the branch moves, the pinned link still shows the old content. | ||
| let pinned = s.get(&format!("/ada/site/blob?ref={}&path=src/main.rs", f.first)).text(); | ||
| assert!(pinned.contains("<hi>") && !pinned.contains("changed&"), "{pinned}"); | ||
| assert!(pinned.contains(&format!(">{}<", &f.first[..12])), "the menu shows a short id: {pinned}"); | ||
| // A selected range is marked by the server, and the permalink keeps it. | ||
| let range = s.get("/ada/site/blob?path=src/main.rs&lines=3-2").text(); | ||
| assert!(range.contains("<a id=\"L1\" href=\"#L1\">1</a>"), "{range}"); | ||
| assert!(range.contains("<a id=\"L2\" href=\"#L2\" class=\"sel\">2</a>"), "{range}"); | ||
| assert!(range.contains("<a id=\"L3\" href=\"#L3\" class=\"sel\">3</a>"), "{range}"); | ||
| let kept = format!("href=\"/ada/site/blob?ref={tip}&path=src/main.rs&lines=2-3#L2\""); | ||
| assert!(range.contains(&kept), "{range}"); | ||
| // A Markdown file's Code view stays the Code view when pinned. | ||
| let code = s.get("/ada/site/blob?path=docs/guide.md&view=code&lines=1").text(); | ||
| assert!(code.contains(&format!("blob?ref={tip}&path=docs/guide.md&view=code&lines=1#L1\"")), "{code}"); | ||
| // A selection that makes no sense selects nothing. | ||
| let bad = s.get("/ada/site/blob?path=src/main.rs&lines=x").text(); | ||
| assert!(!bad.contains("class=\"sel\""), "{bad}"); | ||
| } | ||