v0.4.0 3027a40

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//	http.receivepack=true            (push only)
36//	receive.maxInputSize=<max>       (push only, when configured)
37//
38// A push also gets REMOTE_USER=owner.
39func (g *Git) HTTPBackend(ctx context.Context, push bool) (ports.HTTPBackend, error) {
40	path, err := g.backendPath(ctx)
41	if err != nil {
42		return ports.HTTPBackend{}, err
43	}
44	conf := [][2]string{
45		{"core.hooksPath", HooksDir(g.dataDir)},
46		{"receive.fsckObjects", "true"},
47		{"uploadpack.allowFilter", "true"},
48	}
49	if push {
50		conf = append(conf, [2]string{"http.receivepack", "true"})
51		if g.maxRecv > 0 {
52			conf = append(conf, [2]string{"receive.maxInputSize", strconv.FormatInt(g.maxRecv, 10)})
53		}
54	}
55	env := append([]string(nil), g.env...)
56	env = append(env,
57		"GIT_PROJECT_ROOT="+g.reposDir(),
58		"GIT_HTTP_EXPORT_ALL=1",
59		"GIT_CONFIG_COUNT="+strconv.Itoa(len(conf)),
60	)
61	for i, kv := range conf {
62		n := strconv.Itoa(i)
63		env = append(env, "GIT_CONFIG_KEY_"+n+"="+kv[0], "GIT_CONFIG_VALUE_"+n+"="+kv[1])
64	}
65	if push {
66		env = append(env, "REMOTE_USER="+backendOwner)
67	}
68	return ports.HTTPBackend{Path: path, Dir: g.reposDir(), Env: env}, nil
69}
70
71// backendPath returns <git --exec-path>/git-http-backend, cached after the
72// first success.
73func (g *Git) backendPath(ctx context.Context) (string, error) {
74	g.mu.Lock()
75	defer g.mu.Unlock()
76	if g.backend != "" {
77		return g.backend, nil
78	}
79	out, err := g.run(ctx, "--exec-path")
80	if err != nil {
81		return "", fmt.Errorf("find git exec path: %w", err)
82	}
83	execPath := strings.TrimSpace(out)
84	if execPath == "" {
85		return "", fmt.Errorf("find git exec path: git printed nothing")
86	}
87	path := filepath.Join(execPath, "git-http-backend")
88	fi, err := os.Stat(path)
89	if err != nil {
90		return "", fmt.Errorf("find git-http-backend: %w", err)
91	}
92	if fi.IsDir() || fi.Mode().Perm()&0o111 == 0 {
93		return "", fmt.Errorf("find git-http-backend: %s is not an executable", path)
94	}
95	g.backend = path
96	return path, nil
97}

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