v0.32.0 53e25a3

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

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