v0.28.0 fca9155

commit.go RawHistoryFolder

1// SPDX-License-Identifier: AGPL-3.0-or-later
2
3package web
4
5import (
6	"context"
7	"net/http"
8	"slices"
9	"strings"
10	"time"
11
12	"github.com/a-h/templ"
13
14	"bnema.dev/ermite/internal/domain"
15)
16
17// bothViewsLines is the largest diff, in lines, that is sent in both the
18// unified and the split layout so that the toggle needs no round trip. A
19// larger one is sent in the layout asked for only, and the toggle is a link.
20const bothViewsLines = 4000
21
22// Person is an author or a committer with the date of the action.
23type Person struct {
24	Name, When, WhenAt, Abs string
25}
26
27// CommitPage is a commit with its diff.
28type CommitPage struct {
29	SHA, Short, Subject, Body string
30	Author                    Person
31	// Committer is nil when it is the author, at the same time.
32	Committer *Person
33	Parents   []Link
34	Merge     bool
35	Root      bool
36	Refs      []string
37	// NFiles, Adds and Dels are the totals of the commit, including the files
38	// that the diff omits.
39	NFiles, Adds, Dels int
40	Files              []DiffFile
41	// Note says the diff is incomplete.
42	Note string
43	DiffView
44	// Invisible is true when some line holds an invisible character, shown as a
45	// marker.
46	Invisible bool
47	// Checks is the combined state of the CI checks of the commit.
48	Checks ChecksSummary
49	// Sig is the signature badge, nil when the commit was not checked.
50	Sig *SignatureBadge
51	// Verification is what is known of the signing key, for the details.
52	Verification domain.CommitVerification
53}
54
55// DiffView is how a page lays its diff out: the layouts in the page and the
56// way to switch between them. The commit page and the merge request pages
57// share it.
58type DiffView struct {
59	// View is the layout shown: "unified" or "split". Pinned is true when the
60	// URL chose it, which then wins over the choice remembered by the browser.
61	View   string
62	Pinned bool
63	// Both is true when both layouts are in the page, and the toggle works in
64	// the browser; otherwise only View is sent.
65	Both                   bool
66	ShowUnified, ShowSplit bool
67	UnifiedHref, SplitHref string
68}
69
70// newDiffView reads ?view= and decides which layouts a diff of that many
71// lines sends; the links switch to the other layout.
72func newDiffView(r *http.Request, lines int, unifiedHref, splitHref string) DiffView {
73	v := DiffView{View: "unified", UnifiedHref: unifiedHref, SplitHref: splitHref}
74	switch r.URL.Query().Get("view") {
75	case "split":
76		v.View, v.Pinned = "split", true
77	case "unified":
78		v.Pinned = true
79	}
80	v.Both = lines <= bothViewsLines
81	v.ShowUnified, v.ShowSplit = v.Both || v.View == "unified", v.Both || v.View == "split"
82	return v
83}
84
85// diffLines counts the lines of the hunks of files, and tells whether some
86// line holds a marker.
87func diffLines(files []DiffFile) (lines int, invisible bool) {
88	for _, f := range files {
89		for _, h := range f.Hunks {
90			lines += len(h.Lines)
91			invisible = invisible || slices.ContainsFunc(h.Lines, hasMarker)
92		}
93	}
94	return lines, invisible
95}
96
97// commit shows a commit: its message, its totals, the changed files and the
98// diff against the first parent. ?view=split selects the split layout.
99func (s *Server) commit(ctx context.Context, r *http.Request) (templ.Component, int, error) {
100	sha := r.PathValue("sha")
101	if !isCommitID(sha) {
102		return nil, 0, errNotFound
103	}
104	d, err := s.loadRepo(ctx, r.PathValue("repo"))
105	if err != nil {
106		return nil, 0, err
107	}
108	res, err := s.getCommit(ctx, d.repo.Name, sha)
109	if err != nil {
110		return nil, 0, err
111	}
112	c := res.Commit
113	cid := c.ID.String()
114	now := s.cfg.Now()
115
116	p := CommitPage{
117		SHA: cid, Short: short(cid),
118		Subject: visibleText(c.Summary), Body: commitBody(c),
119		Author: person(c.Author, now),
120		Merge:  len(c.Parents) > 1,
121		Root:   len(c.Parents) == 0,
122		NFiles: res.Stats.FilesChanged,
123		Adds:   res.Stats.Additions,
124		Dels:   res.Stats.Deletions,
125		Files:  s.hl.files(ctx, res.Diff.Files),
126
127		Sig:          signatureBadge(c.Verification, true),
128		Verification: c.Verification,
129	}
130	contextGaps(p.Files, res.Diff.Files, repoHref(d.name(), "context", cid))
131	if cm := person(c.Committer, now); cm != p.Author || c.Committer.Email != c.Author.Email {
132		p.Committer = &cm
133	}
134	for _, pid := range c.Parents {
135		id := pid.String()
136		p.Parents = append(p.Parents, Link{Label: short(id), Href: repoHref(d.name(), "commit", id)})
137	}
138	for _, b := range d.branches {
139		if b.CommitID.String() == cid {
140			p.Refs = append(p.Refs, b.ShortName)
141		}
142	}
143	for _, t := range d.tags {
144		if t.CommitID.String() == cid {
145			p.Refs = append(p.Refs, "tag: "+t.ShortName)
146		}
147	}
148	p.Note = diffNote(res.Diff, len(p.Files), p.NFiles)
149	p.Checks = checksSummary(d.repo.Name, cid, s.checksFor(ctx, d.repo.Name, cid))
150
151	lines, invisible := diffLines(p.Files)
152	p.Invisible = invisible
153	base := repoHref(d.name(), "commit", cid)
154	p.DiffView = newDiffView(r, lines, base+"?view=unified", base+"?view=split")
155
156	l := s.repoLayout(ctx, d, "commits")
157	l.Title = p.Short + " " + p.Subject + " · " + d.name() + " · " + s.instance
158	pinRef(&l, target{rev: cid, name: p.Short, detached: true})
159	history := repoHref(d.name())
160	if d.head != nil {
161		history = repoHref(d.name(), "commits", d.head.ShortName)
162	}
163	l.Crumbs = []Crumb{
164		{Label: d.name(), Href: repoHref(d.name())},
165		{Label: "commit", Href: history},
166		{Label: p.Short, Current: true},
167	}
168	l.HasRail = true
169	return Commit(l, p), http.StatusOK, nil
170}
171
172// person converts a signature.
173func person(s domain.Signature, now time.Time) Person {
174	p := Person{Name: visibleText(s.Name)}
175	p.When, p.WhenAt = whenOf(now, s.Time)
176	if !s.Time.IsZero() {
177		p.Abs = absolute(s.Time)
178	}
179	return p
180}
181
182// commitBody is the message without its first line, for display.
183func commitBody(c domain.Commit) string {
184	msg := strings.ReplaceAll(c.Message, "\r\n", "\n")
185	_, body, _ := strings.Cut(msg, "\n")
186	return messageText(strings.TrimSpace(body))
187}
188
189// diffNote says what is missing from an incomplete diff, "" for a complete one.
190func diffNote(d domain.Diff, shown, total int) string {
191	if !d.Truncated {
192		return ""
193	}
194	var parts []string
195	if shown < total {
196		parts = append(parts, plural(total-shown, "file is", "files are")+" not listed")
197	}
198	cut := 0
199	for _, f := range d.Files {
200		if f.Truncated {
201			cut++
202		}
203	}
204	if cut > 0 {
205		parts = append(parts, plural(cut, "file is", "files are")+" too large to show")
206	}
207	return "this diff is incomplete: " + strings.Join(parts, ", ") + ". the totals above count everything; clone the repository to read the rest."
208}

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 2

Tags 45

↑↓ moveenter openesc close