From 2266baf79a5591a3d686436f444bcc1e4a18d89e Mon Sep 17 00:00:00 2001 From: Hunter Senft-Grupp Date: Mon, 17 Aug 2026 18:32:31 +0000 Subject: [PATCH] fix(cursor-review): resolve PR merge base before review Amp-Thread-ID: https://ampcode.com/threads/T-01a010e7-8414-755e-8422-62dde82b4458 Co-authored-by: Amp --- .github/workflows/cursor-review.yml | 5 ++++ scripts/check-pr-size/main.go | 44 +++++++++++++++++++++-------- scripts/check-pr-size/main_test.go | 31 ++++++++++++++++++++ 3 files changed, 69 insertions(+), 11 deletions(-) diff --git a/.github/workflows/cursor-review.yml b/.github/workflows/cursor-review.yml index fb5f0be2..a704b9d7 100644 --- a/.github/workflows/cursor-review.yml +++ b/.github/workflows/cursor-review.yml @@ -565,6 +565,11 @@ jobs: - name: Classify generated files, check size, and build reviewed diff id: check env: + # This is the current target-branch tip, not necessarily the commit + # the PR forked from. check-pr-size resolves BASE_SHA + HEAD_SHA to + # their merge base once and uses that commit for both classification + # and the reviewed patch, so later main-only commits never enter an + # unrebased PR's review. BASE_SHA: ${{ github.event.pull_request.base.sha }} HEAD_SHA: ${{ github.event.pull_request.head.sha }} # Read by the tool's --ignore-comments default: discount blank/comment diff --git a/scripts/check-pr-size/main.go b/scripts/check-pr-size/main.go index 283470dd..40e61a0f 100644 --- a/scripts/check-pr-size/main.go +++ b/scripts/check-pr-size/main.go @@ -64,27 +64,34 @@ func main() { os.Exit(2) } - files, err := diffFiles(*base, *head) + mergeBase, err := resolveMergeBase(*base, *head) if err != nil { fmt.Fprintf(os.Stderr, "check-pr-size: %v\n", err) os.Exit(2) } + fmt.Fprintf(os.Stderr, "check-pr-size: merge base %s\n", mergeBase) - // The linguist-generated attribute is read from the BASE ref, never the PR - // head, so a PR cannot add `* linguist-generated=true` to .gitattributes and - // shrink its own counted LoC. Two defense-in-depth conditions disable the - // attribute path entirely (unless the bypass label is present): git too old - // to honor `check-attr --source`, or the PR diff touching .gitattributes at - // all. See attrGeneratedBatch / TouchesGitattributes. - attr := attrPolicy{source: *base, useSource: checkAttrSourceSupported(*base)} + files, err := diffFiles(mergeBase, *head) + if err != nil { + fmt.Fprintf(os.Stderr, "check-pr-size: %v\n", err) + os.Exit(2) + } + + // The linguist-generated attribute is read from the merge-base tree, never + // the PR head, so a PR cannot add `* linguist-generated=true` to + // .gitattributes and shrink its own counted LoC. Two defense-in-depth + // conditions disable the attribute path entirely (unless the bypass label is + // present): git too old to honor `check-attr --source`, or the PR diff + // touching .gitattributes at all. See attrGeneratedBatch / TouchesGitattributes. + attr := attrPolicy{source: mergeBase, useSource: checkAttrSourceSupported(mergeBase)} attrModified := TouchesGitattributes(files) attr.trusted = attrTrusted(attr.useSource, attrModified, *bypassFlag) - classify(files, *base, *head, attr, extras, *markerFromBase) + classify(files, mergeBase, *head, attr, extras, *markerFromBase) // Discount blank/comment-only changed lines from the count (opt-in). Runs // after classify so it only ever inspects non-generated, non-binary files. if *ignoreComments { - annotateDiscounts(files, *base, *head) + annotateDiscounts(files, mergeBase, *head) } res := Evaluate(files, Policy{Max: *maxFlag, Bypassed: *bypassFlag, ExcludeTests: *excludeTests}) @@ -117,7 +124,7 @@ func main() { // consumes a diff the gate is about to skip. if *reviewedDiffOut != "" { if res.OK { - if err := writeReviewedDiff(*base, *head, strings.Fields(*diffExcludes), files, *reviewedDiffOut); err != nil { + if err := writeReviewedDiff(mergeBase, *head, strings.Fields(*diffExcludes), files, *reviewedDiffOut); err != nil { os.Remove(*reviewedDiffOut) fmt.Fprintf(os.Stderr, "check-pr-size: could not write --reviewed-diff-out %q: %v\n", *reviewedDiffOut, err) os.Exit(2) @@ -132,6 +139,21 @@ func main() { } } +// resolveMergeBase returns the commit where the PR head diverged from the base +// history. The supplied base may be today's target-branch tip, well ahead of an +// unrebased PR; none of those later base-only commits belong in the PR review. +func resolveMergeBase(base, head string) (string, error) { + out, err := runGit("merge-base", base, head) + if err != nil { + return "", fmt.Errorf("could not resolve merge base: %w", err) + } + mergeBase := strings.TrimSpace(out) + if mergeBase == "" || strings.ContainsAny(mergeBase, "\r\n") { + return "", fmt.Errorf("could not resolve merge base: git returned %q", mergeBase) + } + return mergeBase, nil +} + // writeReviewedDiff streams `git diff base...head` (plus any caller-supplied // verbatim exclude pathspecs) through FilterPatch, writing the patch minus // every generated file's section to outPath. The generated set is passed to diff --git a/scripts/check-pr-size/main_test.go b/scripts/check-pr-size/main_test.go index f3cfebaa..54080f11 100644 --- a/scripts/check-pr-size/main_test.go +++ b/scripts/check-pr-size/main_test.go @@ -57,6 +57,37 @@ func commitAll(t *testing.T, dir, msg string) string { return strings.TrimSpace(gitRun(t, dir, "rev-parse", "HEAD")) } +func TestResolveMergeBaseWithAdvancedBaseBranch(t *testing.T) { + dir := initTestRepo(t) + writeFile(t, dir, "shared.txt", "common\n") + forkPoint := commitAll(t, dir, "fork point") + + gitRun(t, dir, "checkout", "-q", "-b", "pr") + writeFile(t, dir, "pr.txt", "from pr\n") + prHead := commitAll(t, dir, "pr change") + + gitRun(t, dir, "checkout", "-q", "-b", "main", forkPoint) + writeFile(t, dir, "main.txt", "unrelated main change\n") + mainTip := commitAll(t, dir, "main advances") + t.Chdir(dir) + + got, err := resolveMergeBase(mainTip, prHead) + if err != nil { + t.Fatalf("resolveMergeBase: %v", err) + } + if got != forkPoint { + t.Fatalf("resolveMergeBase(%s, %s) = %s, want fork point %s", mainTip, prHead, got, forkPoint) + } + + files, err := diffFiles(got, prHead) + if err != nil { + t.Fatalf("diffFiles: %v", err) + } + if len(files) != 1 || files[0].Path != "pr.txt" { + t.Fatalf("review diff = %+v, want only pr.txt", files) + } +} + func TestTouchesGitattributes(t *testing.T) { t.Parallel() tests := []struct {