gunk-dev/the-valley

git.go

source47a03bb70112main's published tip history
3.6 KiB
1package main
2
3import (
4 "bytes"
5 "fmt"
6 "os"
7 "os/exec"
8 "strings"
9)
10
11// git runs a git command in repo and returns its stdout. Stderr is folded
12// into the error, because a git failure that does not say what git said is
13// a failure the reader has to reproduce by hand.
14func git(repo string, args ...string) (string, error) {
15 cmd := exec.Command("git", args...)
16 cmd.Dir = repo
17 var out, errb bytes.Buffer
18 cmd.Stdout = &out
19 cmd.Stderr = &errb
20 if err := cmd.Run(); err != nil {
21 return "", fmt.Errorf("git %s: %w: %s", strings.Join(args, " "), err, strings.TrimSpace(errb.String()))
22 }
23 return out.String(), nil
24}
25
26func gitLine(repo string, args ...string) (string, error) {
27 out, err := git(repo, args...)
28 return strings.TrimSpace(out), err
29}
30
31// gitTry runs a git command that is allowed to fail, and reports both what
32// it wrote and whether it succeeded. Used where the failure is the answer:
33// a merge that conflicts, a compare-and-swap that lost.
34func gitTry(repo string, args ...string) (string, bool) {
35 cmd := exec.Command("git", args...)
36 cmd.Dir = repo
37 var out bytes.Buffer
38 cmd.Stdout = &out
39 cmd.Stderr = &out
40 err := cmd.Run()
41 return strings.TrimSpace(out.String()), err == nil
42}
43
44// worktree is a throwaway checkout attached to the served repository. The
45// integrator needs one to read the policy at the target tip and another to
46// evaluate check closures over the tree that would land; the served repo is
47// bare, and both of those want a tree on disk.
48type worktree struct {
49 repo string
50 dir string
51}
52
53func addWorktree(repo, rev string) (*worktree, error) {
54 dir, err := os.MkdirTemp("", "valley-integrator-wt")
55 if err != nil {
56 return nil, err
57 }
58 // git worktree add refuses an existing directory; the mkdtemp is only
59 // to reserve a name nothing else will take.
60 if err := os.RemoveAll(dir); err != nil {
61 return nil, err
62 }
63 if _, err := git(repo, "worktree", "add", "--quiet", "--detach", dir, rev); err != nil {
64 return nil, err
65 }
66 return &worktree{repo: repo, dir: dir}, nil
67}
68
69func (w *worktree) remove() {
70 if w == nil {
71 return
72 }
73 _, _ = gitTry(w.repo, "worktree", "remove", "--force", w.dir)
74 _ = os.RemoveAll(w.dir)
75}
76
77// mergeDelta applies a change's delta onto a tip without a working tree:
78// a three-way merge of tip and head against the base the delta was authored
79// against. It writes the resulting tree into the object store and returns
80// its id, or the conflict.
81//
82// This is the whole of the first part of the commit rule. It is deliberately
83// not `git rebase`: the serialized section at the commit point is a tree
84// computation and a ref write, and a rebase would want a checkout, a
85// sequencer state, and a cleanup path for every way it can be interrupted.
86func mergeDelta(repo, base, tip, head string) (tree string, conflict string, err error) {
87 out, ok := gitTry(repo, "merge-tree", "--write-tree", "--merge-base="+base, tip, head)
88 if !ok {
89 return "", out, nil
90 }
91 lines := strings.SplitN(out, "\n", 2)
92 tree = strings.TrimSpace(lines[0])
93 if len(tree) < 40 {
94 return "", "", fmt.Errorf("merge-tree wrote no tree: %s", out)
95 }
96 return tree, "", nil
97}
98
99// diffPaths lists every side of a diff — an added, modified or deleted
100// path, and both the old and the new path of a rename. Only used to answer
101// "did this range change anything at all"; the policy's own matching is the
102// deriver's, never this program's.
103func diffPaths(repo, from, to string) ([]string, error) {
104 out, err := git(repo, "diff", "--no-relative", "--name-only", "-M", "-z", from, to)
105 if err != nil {
106 return nil, err
107 }
108 var paths []string
109 for _, p := range strings.Split(out, "\x00") {
110 if p != "" {
111 paths = append(paths, p)
112 }
113 }
114 return paths, nil
115}