v0.10.1 e4c85c7

media.go RawHistoryFolder

1// SPDX-License-Identifier: AGPL-3.0-or-later
2
3package domain
4
5import (
6	"bytes"
7	"net/http"
8	"path"
9	"strings"
10)
11
12// SniffContent sets Binary and MediaType from the first bytes of the content
13// (Path must be set). Only the first BinarySniffLen bytes count, so a caller
14// may pass the whole content or just its head. Every reader of blobs applies
15// this one rule.
16func (b *BlobInfo) SniffContent(content []byte) {
17	head := content[:min(len(content), BinarySniffLen)]
18	b.Binary = bytes.IndexByte(head, 0) >= 0
19	b.MediaType = mediaType(b.Path, head, b.Binary)
20}
21
22// textTypes maps the extensions whose media type is more precise than what
23// the content shows. It is fixed here so that the answer does not depend on the
24// MIME tables of the host.
25var textTypes = map[string]string{
26	".html": "text/html", ".htm": "text/html", ".css": "text/css",
27	".js": "text/javascript", ".mjs": "text/javascript",
28	".json": "application/json", ".xml": "application/xml",
29	".svg": "image/svg+xml", ".md": "text/markdown", ".csv": "text/csv",
30}
31
32// mediaType guesses a media type without parameters. What the content shows
33// (images, archives, PDF…) wins; for plain text the file name may refine it
34// from textTypes, but a name never makes binary content text.
35func mediaType(name string, head []byte, binary bool) string {
36	sniffed, _, _ := strings.Cut(http.DetectContentType(head), ";")
37	switch {
38	case binary && strings.HasPrefix(sniffed, "text/"):
39		return "application/octet-stream"
40	case sniffed != "text/plain" && sniffed != "text/xml" && sniffed != "application/octet-stream":
41		return sniffed
42	case binary:
43		return "application/octet-stream"
44	}
45	if t, ok := textTypes[strings.ToLower(path.Ext(name))]; ok {
46		return t
47	}
48	return "text/plain"
49}
50
51// ValidGitText replaces the bytes of s that are not valid UTF-8 with U+FFFD:
52// names, emails and messages of commits may hold any bytes.
53func ValidGitText(s string) string { return strings.ToValidUTF8(s, "\uFFFD") }
54
55// SplitCommitMessage turns the raw message of a commit into Commit.Message
56// (valid UTF-8, no trailing line breaks) and Commit.Summary (its first line).
57func SplitCommitMessage(raw string) (message, summary string) {
58	message = strings.TrimRight(ValidGitText(raw), "\n")
59	summary, _, _ = strings.Cut(message, "\n")
60	return message, summary
61}

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 3

Tags 43

↑↓ moveenter openesc close