| 1 | package thin |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "os/exec" |
| 7 | "path" |
| 8 | "strconv" |
| 9 | "strings" |
| 10 | |
| 11 | "amadan.net/oficina/equipo/internal/runner/workspace" |
| 12 | "amadan.net/oficina/equipo/internal/wire" |
| 13 | ) |
| 14 | |
| 15 | // Merge authority (spec 2026-11-11 §3): what a reviewed change is, |
| 16 | // measured with no model on exactly the reviewed pair, so the instance's |
| 17 | // gate can tell a small, ordinary change from one it must ask about. The |
| 18 | // instance decides; this only measures, and anything it cannot measure |
| 19 | // whole is reported incomplete (the gate then asks). |
| 20 | |
| 21 | // factsOutput bounds each git listing the facts are read from. |
| 22 | const factsOutput = 1 << 20 |
| 23 | |
| 24 | // signingKeys are the Xcode build settings and Info.plist keys whose |
| 25 | // change is a signing or release change. |
| 26 | var signingKeys = []string{"CODE_SIGN", "DEVELOPMENT_TEAM", "PROVISIONING_PROFILE", "ENTITLEMENTS", "PRODUCT_BUNDLE_IDENTIFIER", "MARKETING_VERSION", "CURRENT_PROJECT_VERSION", "CFBundleVersion", "CFBundleShortVersionString"} |
| 27 | |
| 28 | // gitModes are the object modes git writes. |
| 29 | var gitModes = map[string]bool{"000000": true, "100644": true, "100755": true, "120000": true, "160000": true, "040000": true} |
| 30 | |
| 31 | // changeFacts measures base..sha in the tree's private repository. |
| 32 | func changeFacts(ctx context.Context, git, gitDir string, r stepRow) *wire.ChangeFacts { |
| 33 | f := &wire.ChangeFacts{ReviewStep: r.StepID, Attempt: r.Attempt, Repo: r.Repo, Base: r.BaseSHA, SHA: r.CandidateSHA, Entries: []wire.ChangeEntry{}} |
| 34 | if !shaPattern.MatchString(r.BaseSHA) || !shaPattern.MatchString(r.CandidateSHA) { |
| 35 | return f |
| 36 | } |
| 37 | run := func(args ...string) (string, bool) { |
| 38 | cmd := exec.CommandContext(ctx, git, append([]string{"--git-dir=" + gitDir, "-c", "core.quotePath=false"}, args...)...) |
| 39 | env := workspace.GitEnvironment() |
| 40 | env["HOME"] = "/nonexistent" |
| 41 | for k, v := range env { |
| 42 | cmd.Env = append(cmd.Env, k+"="+v) |
| 43 | } |
| 44 | out, clipped, err := boundedOutput(cmd, factsOutput) |
| 45 | return string(out), err == nil && !clipped |
| 46 | } |
| 47 | if _, ok := run("merge-base", "--is-ancestor", r.BaseSHA, r.CandidateSHA); !ok { |
| 48 | return f |
| 49 | } |
| 50 | raw, ok1 := run("diff-tree", "-r", "-z", "--raw", "-M", "--no-abbrev", "--no-ext-diff", "--no-textconv", r.BaseSHA, r.CandidateSHA) |
| 51 | num, ok2 := run("diff-tree", "-r", "-z", "--numstat", "-M", "--no-ext-diff", "--no-textconv", r.BaseSHA, r.CandidateSHA) |
| 52 | if !ok1 || !ok2 { |
| 53 | return f |
| 54 | } |
| 55 | entries, ok := parseRaw(raw) |
| 56 | if !ok || len(entries) > wire.MaxChangeEntries { |
| 57 | return f |
| 58 | } |
| 59 | stats, ok := parseNumstat(num) |
| 60 | if !ok || len(stats) != len(entries) { |
| 61 | return f |
| 62 | } |
| 63 | var xcode []string |
| 64 | for i, e := range entries { |
| 65 | s := stats[i] |
| 66 | if s.path != e.New || s.old != e.Old { |
| 67 | return f |
| 68 | } |
| 69 | if s.binary { |
| 70 | f.Binary++ |
| 71 | } else { |
| 72 | f.Lines += s.added + s.deleted |
| 73 | } |
| 74 | if e.OldMode == "120000" || e.NewMode == "120000" || e.OldMode == "160000" || e.NewMode == "160000" || |
| 75 | e.OldMode != "000000" && e.NewMode != "000000" && e.OldMode != e.NewMode || e.OldMode == "000000" && e.NewMode == "100755" { |
| 76 | f.Special = true |
| 77 | } |
| 78 | for _, p := range []string{e.Old, e.New} { |
| 79 | if p != "" && (path.Ext(p) == ".pbxproj" || path.Ext(p) == ".xcconfig" || path.Base(p) == "Info.plist") { |
| 80 | xcode = append(xcode, p) |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | if len(xcode) > 0 { |
| 85 | patch, ok := run(append([]string{"diff-tree", "-r", "-p", "-U0", "-M", "--no-ext-diff", "--no-textconv", "--no-color", r.BaseSHA, r.CandidateSHA, "--"}, xcode...)...) |
| 86 | if !ok { |
| 87 | return f |
| 88 | } |
| 89 | f.Signing = signingChange(patch) |
| 90 | } |
| 91 | f.Entries, f.Files = entries, len(entries) |
| 92 | if r.RevertSHA != "" { |
| 93 | f.Revert = revertCheck(run, r.CandidateSHA, r.RevertSHA) |
| 94 | } |
| 95 | gates, err := readBaseGates(ctx, git, gitDir, r.BaseSHA) |
| 96 | if err != nil { |
| 97 | return f |
| 98 | } |
| 99 | for _, entry := range entries { |
| 100 | for _, name := range []string{entry.Old, entry.New} { |
| 101 | if gatePathCovered(gates, name) { |
| 102 | f.GateFilesChanged = true |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | f.Complete = true |
| 107 | return f |
| 108 | } |
| 109 | |
| 110 | // revertCheck says whether candidate sits right on target and has its |
| 111 | // first parent's tree: an exact revert of target, nothing more. |
| 112 | func revertCheck(run func(...string) (string, bool), candidate, target string) *wire.RevertCheck { |
| 113 | c := &wire.RevertCheck{SHA: target} |
| 114 | if !shaPattern.MatchString(target) { |
| 115 | return c |
| 116 | } |
| 117 | parent, ok := run("rev-parse", "--verify", "--end-of-options", candidate+"^1") |
| 118 | if !ok || strings.TrimSpace(parent) != target { |
| 119 | return c |
| 120 | } |
| 121 | c.OnTop = true |
| 122 | got, ok1 := run("rev-parse", "--verify", "--end-of-options", candidate+"^{tree}") |
| 123 | want, ok2 := run("rev-parse", "--verify", "--end-of-options", target+"^1^{tree}") |
| 124 | c.Exact = ok1 && ok2 && strings.TrimSpace(got) != "" && strings.TrimSpace(got) == strings.TrimSpace(want) |
| 125 | return c |
| 126 | } |
| 127 | |
| 128 | // parseRaw reads `diff-tree -z --raw`: ":old new oldsha newsha status" |
| 129 | // then one path, or two for a rename or copy. |
| 130 | func parseRaw(out string) ([]wire.ChangeEntry, bool) { |
| 131 | toks := strings.Split(strings.TrimSuffix(out, "\x00"), "\x00") |
| 132 | if out == "" { |
| 133 | return []wire.ChangeEntry{}, true |
| 134 | } |
| 135 | var es []wire.ChangeEntry |
| 136 | for i := 0; i < len(toks); { |
| 137 | head := toks[i] |
| 138 | if !strings.HasPrefix(head, ":") { |
| 139 | return nil, false |
| 140 | } |
| 141 | fs := strings.Fields(head[1:]) |
| 142 | if len(fs) != 5 || !gitModes[fs[0]] || !gitModes[fs[1]] || fs[4] == "" { |
| 143 | return nil, false |
| 144 | } |
| 145 | e := wire.ChangeEntry{Status: fs[4], OldMode: fs[0], NewMode: fs[1]} |
| 146 | i++ |
| 147 | two := e.Status[0] == 'R' || e.Status[0] == 'C' |
| 148 | if two { |
| 149 | if i+1 >= len(toks) { |
| 150 | return nil, false |
| 151 | } |
| 152 | e.Old, e.New = toks[i], toks[i+1] |
| 153 | i += 2 |
| 154 | } else { |
| 155 | if i >= len(toks) { |
| 156 | return nil, false |
| 157 | } |
| 158 | e.New = toks[i] |
| 159 | i++ |
| 160 | } |
| 161 | if !strings.ContainsRune("ACDMRTUX", rune(e.Status[0])) || e.New == "" { |
| 162 | return nil, false |
| 163 | } |
| 164 | es = append(es, e) |
| 165 | } |
| 166 | return es, true |
| 167 | } |
| 168 | |
| 169 | type numstat struct { |
| 170 | added, deleted int |
| 171 | binary bool |
| 172 | old, path string |
| 173 | } |
| 174 | |
| 175 | // parseNumstat reads `diff-tree -z --numstat`: "added\tdeleted\tpath", |
| 176 | // or "added\tdeleted\t" then old and new paths for a rename or copy. |
| 177 | func parseNumstat(out string) ([]numstat, bool) { |
| 178 | if out == "" { |
| 179 | return []numstat{}, true |
| 180 | } |
| 181 | toks := strings.Split(strings.TrimSuffix(out, "\x00"), "\x00") |
| 182 | var ss []numstat |
| 183 | for i := 0; i < len(toks); { |
| 184 | parts := strings.SplitN(toks[i], "\t", 3) |
| 185 | if len(parts) != 3 { |
| 186 | return nil, false |
| 187 | } |
| 188 | var s numstat |
| 189 | if parts[0] == "-" && parts[1] == "-" { |
| 190 | s.binary = true |
| 191 | } else { |
| 192 | a, err1 := strconv.Atoi(parts[0]) |
| 193 | d, err2 := strconv.Atoi(parts[1]) |
| 194 | if err := errors.Join(err1, err2); err != nil || a < 0 || d < 0 { |
| 195 | return nil, false |
| 196 | } |
| 197 | s.added, s.deleted = a, d |
| 198 | } |
| 199 | i++ |
| 200 | if parts[2] == "" { |
| 201 | if i+1 >= len(toks) { |
| 202 | return nil, false |
| 203 | } |
| 204 | s.old, s.path = toks[i], toks[i+1] |
| 205 | i += 2 |
| 206 | } else { |
| 207 | s.path = parts[2] |
| 208 | } |
| 209 | ss = append(ss, s) |
| 210 | } |
| 211 | return ss, true |
| 212 | } |
| 213 | |
| 214 | // signingChange reports whether an added or removed line of a patch |
| 215 | // names a signing or version key. |
| 216 | func signingChange(patch string) bool { |
| 217 | for _, line := range strings.Split(patch, "\n") { |
| 218 | if !strings.HasPrefix(line, "+") && !strings.HasPrefix(line, "-") || strings.HasPrefix(line, "+++") || strings.HasPrefix(line, "---") { |
| 219 | continue |
| 220 | } |
| 221 | for _, k := range signingKeys { |
| 222 | if strings.Contains(line, k) { |
| 223 | return true |
| 224 | } |
| 225 | } |
| 226 | } |
| 227 | return false |
| 228 | } |
| 229 | |