| 1 | package main |
| 2 | |
| 3 | import ( |
| 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. |
| 14 | func 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 | |
| 26 | func 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. |
| 34 | func 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. |
| 48 | type worktree struct { |
| 49 | repo string |
| 50 | dir string |
| 51 | } |
| 52 | |
| 53 | func 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 | |
| 69 | func (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. |
| 86 | func 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 "", conflictMessage(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 | // conflictMessage picks the sentence out of a conflicted merge-tree's |
| 100 | // report. That report opens with the tree oid and the conflicted entries |
| 101 | // and ends with the informational messages, which are the part naming what |
| 102 | // could not be merged. |
| 103 | func conflictMessage(out string) string { |
| 104 | var last string |
| 105 | for _, line := range strings.Split(out, "\n") { |
| 106 | line = strings.TrimSpace(line) |
| 107 | if strings.HasPrefix(line, "CONFLICT") { |
| 108 | return line |
| 109 | } |
| 110 | if line != "" { |
| 111 | last = line |
| 112 | } |
| 113 | } |
| 114 | return last |
| 115 | } |