| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "fmt" |
| 6 | "os" |
| 7 | "os/exec" |
| 8 | "sort" |
| 9 | "strings" |
| 10 | ) |
| 11 | |
| 12 | // carried is the approved version's head H₀ carried mechanically onto the |
| 13 | // shown version's base B₁: the tree git merge-tree --write-tree |
| 14 | // --merge-base=B₀ B₁ H₀ writes, the merge the integrator makes, and the |
| 15 | // paths it could not merge. |
| 16 | type carried struct { |
| 17 | tree string |
| 18 | conflicts map[string][]string // each conflicted path → the conflict types merge-tree names it in, in its order |
| 19 | } |
| 20 | |
| 21 | // paths are the conflicted paths, sorted. |
| 22 | func (c carried) paths() []string { |
| 23 | var ps []string |
| 24 | for p := range c.conflicts { |
| 25 | ps = append(ps, p) |
| 26 | } |
| 27 | sort.Strings(ps) |
| 28 | return ps |
| 29 | } |
| 30 | |
| 31 | // carryGit is git run in a scratch git directory that shares g's object |
| 32 | // store and nothing else, so that what the carry merges and the diffs read |
| 33 | // from it depend on the objects alone, as the comment at the top of |
| 34 | // compare.go says. done removes the directory. |
| 35 | func carryGit(g git) (c git, done func(), err error) { |
| 36 | done = func() {} |
| 37 | objects, err := g.output(true, "rev-parse", "--path-format=absolute", "--git-path", "objects") |
| 38 | if err != nil || objects == "" { |
| 39 | return c, done, errors.New("the clone's object store is not found") |
| 40 | } |
| 41 | dir, err := os.MkdirTemp("", "valley-carry") |
| 42 | if err != nil { |
| 43 | return c, done, errors.New("no scratch git directory could be made") |
| 44 | } |
| 45 | done = func() { os.RemoveAll(dir) } |
| 46 | var env []string |
| 47 | for _, kv := range g.env { |
| 48 | if !strings.HasPrefix(kv, "GIT_") { |
| 49 | env = append(env, kv) |
| 50 | } |
| 51 | } |
| 52 | env = append(env, |
| 53 | "GIT_CONFIG_NOSYSTEM=1", "GIT_CONFIG_GLOBAL=/dev/null", "GIT_ATTR_NOSYSTEM=1", "GIT_NO_REPLACE_OBJECTS=1", |
| 54 | "GIT_CONFIG_COUNT=1", "GIT_CONFIG_KEY_0=core.attributesFile", "GIT_CONFIG_VALUE_0=/dev/null") |
| 55 | c = git{env: env, stderr: g.stderr, dir: dir} |
| 56 | if _, err := c.output(true, "init", "--quiet", "--bare", "--template=", dir); err != nil { |
| 57 | return c, done, errors.New("the scratch git directory could not be made") |
| 58 | } |
| 59 | c.env = append(c.env, "GIT_DIR="+dir, "GIT_OBJECT_DIRECTORY="+objects) |
| 60 | return c, done, nil |
| 61 | } |
| 62 | |
| 63 | // carry carries h0 from b0 onto b1 in cg, a carryGit. When b0 is b1, the |
| 64 | // carried tree is h0's and no merge runs. |
| 65 | func carry(cg git, b0, b1, h0 string) (carried, error) { |
| 66 | if b0 == b1 { |
| 67 | tree, err := cg.output(true, "--no-replace-objects", "rev-parse", "--verify", "--quiet", h0+"^{tree}") |
| 68 | if err != nil { |
| 69 | return carried{}, errors.New("its tree does not read") |
| 70 | } |
| 71 | return carried{tree: tree}, nil |
| 72 | } |
| 73 | out, err := cg.output(true, "--no-replace-objects", "merge-tree", "--write-tree", "-z", "--merge-base="+b0, b1, h0) |
| 74 | var exit *exec.ExitError |
| 75 | switch { |
| 76 | case err == nil: |
| 77 | tree, _, _ := strings.Cut(out, "\x00") |
| 78 | if !isObjectID(tree) { |
| 79 | return carried{}, errors.New("git merge-tree wrote no tree") |
| 80 | } |
| 81 | return carried{tree: tree}, nil |
| 82 | case errors.As(err, &exit) && exit.ExitCode() == 1: |
| 83 | c, err := parseConflicts(out) |
| 84 | if err != nil { |
| 85 | return carried{}, err |
| 86 | } |
| 87 | if len(c.conflicts) == 0 { |
| 88 | return carried{}, errors.New("git merge-tree reported a conflict on no path") |
| 89 | } |
| 90 | return c, nil |
| 91 | } |
| 92 | return carried{}, fmt.Errorf("git merge-tree failed: %v", err) |
| 93 | } |
| 94 | |
| 95 | // parseConflicts reads git merge-tree --write-tree -z's output for a |
| 96 | // merge with conflicts: the tree; each conflicted file entry, as |
| 97 | // "<mode> <object> <stage>\t<path>"; an empty field; then each message, |
| 98 | // as its path count, its paths, its type and its text. Every path a |
| 99 | // conflicted entry or a message of any type but Auto-merging names is |
| 100 | // conflicted, so a type this reading does not know shows more, never |
| 101 | // less. |
| 102 | func parseConflicts(out string) (carried, error) { |
| 103 | bad := errors.New("git merge-tree's conflict report does not parse") |
| 104 | fields := strings.Split(out, "\x00") |
| 105 | if len(fields) < 2 || !isObjectID(fields[0]) || fields[len(fields)-1] != "" { |
| 106 | return carried{}, bad |
| 107 | } |
| 108 | c := carried{tree: fields[0], conflicts: map[string][]string{}} |
| 109 | fields = fields[1 : len(fields)-1] |
| 110 | i := 0 |
| 111 | for ; i < len(fields) && fields[i] != ""; i++ { |
| 112 | info, path, ok := strings.Cut(fields[i], "\t") |
| 113 | if !ok || len(strings.Fields(info)) != 3 || path == "" { |
| 114 | return carried{}, bad |
| 115 | } |
| 116 | if _, seen := c.conflicts[path]; !seen { |
| 117 | c.conflicts[path] = nil |
| 118 | } |
| 119 | } |
| 120 | if i == len(fields) { |
| 121 | return carried{}, bad |
| 122 | } |
| 123 | for i++; i < len(fields); { |
| 124 | n := 0 |
| 125 | if _, err := fmt.Sscanf(fields[i], "%d", &n); err != nil || fmt.Sprint(n) != fields[i] || n < 1 || i+n+2 >= len(fields) { |
| 126 | return carried{}, bad |
| 127 | } |
| 128 | paths, kind := fields[i+1:i+1+n], fields[i+1+n] |
| 129 | i += n + 3 |
| 130 | if kind == "Auto-merging" { |
| 131 | continue |
| 132 | } |
| 133 | for _, p := range paths { |
| 134 | if p == "" { |
| 135 | return carried{}, bad |
| 136 | } |
| 137 | if !contains(c.conflicts[p], kind) { |
| 138 | c.conflicts[p] = append(c.conflicts[p], kind) |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | return c, nil |
| 143 | } |