v0.12.0 dccd128

backend.go RawHistoryFolder

1// SPDX-License-Identifier: AGPL-3.0-or-later
2
3package gitcli
4
5import (
6	"context"
7	"fmt"
8	"os"
9	"path/filepath"
10	"strconv"
11	"strings"
12
13	"bnema.dev/ermite/internal/ports"
14)
15
16var _ ports.GitBackend = (*Git)(nil)
17
18// backendOwner is REMOTE_USER of a push: ermite authorizes before Git runs,
19// so Git only needs to see a user.
20const backendOwner = "owner"
21
22// HooksDir is the directory, below the data root, that holds the hook
23// scripts every receive-pack runs.
24func HooksDir(dataDir string) string { return filepath.Join(dataDir, "run", "hooks") }
25
26// HTTPBackend resolves git-http-backend through `git --exec-path` of the
27// configured git (once), and builds its environment: the isolated one,
28// GIT_PROJECT_ROOT=<data>/repos, GIT_HTTP_EXPORT_ALL and the configuration
29// passed by GIT_CONFIG_COUNT/KEY/VALUE, so that nothing is written to the
30// repositories:
31//
32//	core.hooksPath=<data>/run/hooks
33//	receive.fsckObjects=true
34//	uploadpack.allowFilter=true
35//	pack.threads=1
36//	pack.windowMemory=256m
37//	http.receivepack=true            (push only)
38//	receive.maxInputSize=<max>       (push only, when configured)
39//
40// A push also gets REMOTE_USER=owner.
41func (g *Git) HTTPBackend(ctx context.Context, push bool) (ports.HTTPBackend, error) {
42	path, err := g.backendPath(ctx)
43	if err != nil {
44		return ports.HTTPBackend{}, err
45	}
46	conf := [][2]string{
47		{"core.hooksPath", HooksDir(g.dataDir)},
48		{"receive.fsckObjects", "true"},
49		{"uploadpack.allowFilter", "true"},
50		// pack-objects defaults to one thread per CPU and an unbounded delta
51		// window; with several backends at once that is the memory and CPU
52		// of the host. The backends are bounded by the handler instead.
53		{"pack.threads", "1"},
54		{"pack.windowMemory", "256m"},
55	}
56	if push {
57		conf = append(conf, [2]string{"http.receivepack", "true"})
58		if g.maxRecv > 0 {
59			conf = append(conf, [2]string{"receive.maxInputSize", strconv.FormatInt(g.maxRecv, 10)})
60		}
61	}
62	env := append([]string(nil), g.env...)
63	env = append(env,
64		"GIT_PROJECT_ROOT="+g.reposDir(),
65		"GIT_HTTP_EXPORT_ALL=1",
66		"GIT_CONFIG_COUNT="+strconv.Itoa(len(conf)),
67	)
68	for i, kv := range conf {
69		n := strconv.Itoa(i)
70		env = append(env, "GIT_CONFIG_KEY_"+n+"="+kv[0], "GIT_CONFIG_VALUE_"+n+"="+kv[1])
71	}
72	if push {
73		env = append(env, "REMOTE_USER="+backendOwner)
74	}
75	return ports.HTTPBackend{Path: path, Dir: g.reposDir(), Env: env}, nil
76}
77
78// backendPath returns <git --exec-path>/git-http-backend, cached after the
79// first success.
80func (g *Git) backendPath(ctx context.Context) (string, error) {
81	g.mu.Lock()
82	defer g.mu.Unlock()
83	if g.backend != "" {
84		return g.backend, nil
85	}
86	out, err := g.run(ctx, "--exec-path")
87	if err != nil {
88		return "", fmt.Errorf("find git exec path: %w", err)
89	}
90	execPath := strings.TrimSpace(out)
91	if execPath == "" {
92		return "", fmt.Errorf("find git exec path: git printed nothing")
93	}
94	path := filepath.Join(execPath, "git-http-backend")
95	fi, err := os.Stat(path)
96	if err != nil {
97		return "", fmt.Errorf("find git-http-backend: %w", err)
98	}
99	if fi.IsDir() || fi.Mode().Perm()&0o111 == 0 {
100		return "", fmt.Errorf("find git-http-backend: %s is not an executable", path)
101	}
102	g.backend = path
103	return path, nil
104}

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