Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .github/workflows/cursor-review.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
44 changes: 33 additions & 11 deletions scripts/check-pr-size/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down
31 changes: 31 additions & 0 deletions scripts/check-pr-size/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading