package thin import ( "context" "errors" "os/exec" "path" "strconv" "strings" "amadan.net/oficina/equipo/internal/runner/workspace" "amadan.net/oficina/equipo/internal/wire" ) // Merge authority (spec 2026-11-11 ยง3): what a reviewed change is, // measured with no model on exactly the reviewed pair, so the instance's // gate can tell a small, ordinary change from one it must ask about. The // instance decides; this only measures, and anything it cannot measure // whole is reported incomplete (the gate then asks). // factsOutput bounds each git listing the facts are read from. const factsOutput = 1 << 20 // signingKeys are the Xcode build settings and Info.plist keys whose // change is a signing or release change. var signingKeys = []string{"CODE_SIGN", "DEVELOPMENT_TEAM", "PROVISIONING_PROFILE", "ENTITLEMENTS", "PRODUCT_BUNDLE_IDENTIFIER", "MARKETING_VERSION", "CURRENT_PROJECT_VERSION", "CFBundleVersion", "CFBundleShortVersionString"} // gitModes are the object modes git writes. var gitModes = map[string]bool{"000000": true, "100644": true, "100755": true, "120000": true, "160000": true, "040000": true} // changeFacts measures base..sha in the tree's private repository. func changeFacts(ctx context.Context, git, gitDir string, r stepRow) *wire.ChangeFacts { f := &wire.ChangeFacts{ReviewStep: r.StepID, Attempt: r.Attempt, Repo: r.Repo, Base: r.BaseSHA, SHA: r.CandidateSHA, Entries: []wire.ChangeEntry{}} if !shaPattern.MatchString(r.BaseSHA) || !shaPattern.MatchString(r.CandidateSHA) { return f } run := func(args ...string) (string, bool) { cmd := exec.CommandContext(ctx, git, append([]string{"--git-dir=" + gitDir, "-c", "core.quotePath=false"}, args...)...) env := workspace.GitEnvironment() env["HOME"] = "/nonexistent" for k, v := range env { cmd.Env = append(cmd.Env, k+"="+v) } out, clipped, err := boundedOutput(cmd, factsOutput) return string(out), err == nil && !clipped } if _, ok := run("merge-base", "--is-ancestor", r.BaseSHA, r.CandidateSHA); !ok { return f } raw, ok1 := run("diff-tree", "-r", "-z", "--raw", "-M", "--no-abbrev", "--no-ext-diff", "--no-textconv", r.BaseSHA, r.CandidateSHA) num, ok2 := run("diff-tree", "-r", "-z", "--numstat", "-M", "--no-ext-diff", "--no-textconv", r.BaseSHA, r.CandidateSHA) if !ok1 || !ok2 { return f } entries, ok := parseRaw(raw) if !ok || len(entries) > wire.MaxChangeEntries { return f } stats, ok := parseNumstat(num) if !ok || len(stats) != len(entries) { return f } var xcode []string for i, e := range entries { s := stats[i] if s.path != e.New || s.old != e.Old { return f } if s.binary { f.Binary++ } else { f.Lines += s.added + s.deleted } if e.OldMode == "120000" || e.NewMode == "120000" || e.OldMode == "160000" || e.NewMode == "160000" || e.OldMode != "000000" && e.NewMode != "000000" && e.OldMode != e.NewMode || e.OldMode == "000000" && e.NewMode == "100755" { f.Special = true } for _, p := range []string{e.Old, e.New} { if p != "" && (path.Ext(p) == ".pbxproj" || path.Ext(p) == ".xcconfig" || path.Base(p) == "Info.plist") { xcode = append(xcode, p) } } } if len(xcode) > 0 { patch, ok := run(append([]string{"diff-tree", "-r", "-p", "-U0", "-M", "--no-ext-diff", "--no-textconv", "--no-color", r.BaseSHA, r.CandidateSHA, "--"}, xcode...)...) if !ok { return f } f.Signing = signingChange(patch) } f.Entries, f.Files = entries, len(entries) if r.RevertSHA != "" { f.Revert = revertCheck(run, r.CandidateSHA, r.RevertSHA) } gates, err := readBaseGates(ctx, git, gitDir, r.BaseSHA) if err != nil { return f } for _, entry := range entries { for _, name := range []string{entry.Old, entry.New} { if gatePathCovered(gates, name) { f.GateFilesChanged = true } } } f.Complete = true return f } // revertCheck says whether candidate sits right on target and has its // first parent's tree: an exact revert of target, nothing more. func revertCheck(run func(...string) (string, bool), candidate, target string) *wire.RevertCheck { c := &wire.RevertCheck{SHA: target} if !shaPattern.MatchString(target) { return c } parent, ok := run("rev-parse", "--verify", "--end-of-options", candidate+"^1") if !ok || strings.TrimSpace(parent) != target { return c } c.OnTop = true got, ok1 := run("rev-parse", "--verify", "--end-of-options", candidate+"^{tree}") want, ok2 := run("rev-parse", "--verify", "--end-of-options", target+"^1^{tree}") c.Exact = ok1 && ok2 && strings.TrimSpace(got) != "" && strings.TrimSpace(got) == strings.TrimSpace(want) return c } // parseRaw reads `diff-tree -z --raw`: ":old new oldsha newsha status" // then one path, or two for a rename or copy. func parseRaw(out string) ([]wire.ChangeEntry, bool) { toks := strings.Split(strings.TrimSuffix(out, "\x00"), "\x00") if out == "" { return []wire.ChangeEntry{}, true } var es []wire.ChangeEntry for i := 0; i < len(toks); { head := toks[i] if !strings.HasPrefix(head, ":") { return nil, false } fs := strings.Fields(head[1:]) if len(fs) != 5 || !gitModes[fs[0]] || !gitModes[fs[1]] || fs[4] == "" { return nil, false } e := wire.ChangeEntry{Status: fs[4], OldMode: fs[0], NewMode: fs[1]} i++ two := e.Status[0] == 'R' || e.Status[0] == 'C' if two { if i+1 >= len(toks) { return nil, false } e.Old, e.New = toks[i], toks[i+1] i += 2 } else { if i >= len(toks) { return nil, false } e.New = toks[i] i++ } if !strings.ContainsRune("ACDMRTUX", rune(e.Status[0])) || e.New == "" { return nil, false } es = append(es, e) } return es, true } type numstat struct { added, deleted int binary bool old, path string } // parseNumstat reads `diff-tree -z --numstat`: "added\tdeleted\tpath", // or "added\tdeleted\t" then old and new paths for a rename or copy. func parseNumstat(out string) ([]numstat, bool) { if out == "" { return []numstat{}, true } toks := strings.Split(strings.TrimSuffix(out, "\x00"), "\x00") var ss []numstat for i := 0; i < len(toks); { parts := strings.SplitN(toks[i], "\t", 3) if len(parts) != 3 { return nil, false } var s numstat if parts[0] == "-" && parts[1] == "-" { s.binary = true } else { a, err1 := strconv.Atoi(parts[0]) d, err2 := strconv.Atoi(parts[1]) if err := errors.Join(err1, err2); err != nil || a < 0 || d < 0 { return nil, false } s.added, s.deleted = a, d } i++ if parts[2] == "" { if i+1 >= len(toks) { return nil, false } s.old, s.path = toks[i], toks[i+1] i += 2 } else { s.path = parts[2] } ss = append(ss, s) } return ss, true } // signingChange reports whether an added or removed line of a patch // names a signing or version key. func signingChange(patch string) bool { for _, line := range strings.Split(patch, "\n") { if !strings.HasPrefix(line, "+") && !strings.HasPrefix(line, "-") || strings.HasPrefix(line, "+++") || strings.HasPrefix(line, "---") { continue } for _, k := range signingKeys { if strings.Contains(line, k) { return true } } } return false }