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}