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}