From ef4a7f5cbf64ea2580ee2a0a8dcdc1856755262f Mon Sep 17 00:00:00 2001 From: Garry Tan Date: Fri, 20 Nov 2026 16:04:26 -0800 Subject: [PATCH] Reviews stay valid after merging main when a stacked parent was already on main (GST-3315) (#4580) /ship keeps a PR's review when the PR merges main and its stacked parent had already landed before the review. It used to refuse over the parent's files. Why: yc-software/qm#3457 was asked to re-review files it never changed. Nothing for readers to do. Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> --- changelog.d/receipt-carry-held-parent.md | 1 + docs/ship-policy.md | 6 +- lib/ship-receipt-patch.ts | 40 +++++++---- lib/ship-receipt.ts | 6 +- test/ship-receipt-carry-squash.test.ts | 90 ++++++++++++++++++++++++ 5 files changed, 127 insertions(+), 16 deletions(-) create mode 100644 changelog.d/receipt-carry-held-parent.md diff --git a/changelog.d/receipt-carry-held-parent.md b/changelog.d/receipt-carry-held-parent.md new file mode 100644 index 000000000..10f6179b7 --- /dev/null +++ b/changelog.d/receipt-carry-held-parent.md @@ -0,0 +1 @@ +**/ship no longer asks for a fresh review because of a stacked parent that main already had.** When a PR was reviewed after its parent had squash-merged into main, and then merged main again, /ship measured from the parent's old commits and refused over the parent's files. It now measures from the merge base, so an unchanged PR keeps its review, and context that main moved next to the PR's own lines gets one scoped review of just that file (yc-software/qm#3457). The patch comparison also always uses three lines of context, so a `diff.context` setting in your git config can't hide moved lines. diff --git a/docs/ship-policy.md b/docs/ship-policy.md index 82106eaa8..2731f446f 100644 --- a/docs/ship-policy.md +++ b/docs/ship-policy.md @@ -2335,7 +2335,11 @@ difference refuses with `RECEIPT_CARRY_PRODUCT_CHANGED`. When merging the commit into the current base conflicts because the base edited the landed lines afterwards, the base commits since the fork that touched every path of its change are tried newest first (at most 200), so the squash itself still counts -(GST-1399). +(GST-1399). A commit the reviewed range's own base already held is no parent: it +landed before the review, so the reviewed diff never held its hunks, and +measuring from it would compare the wrong range and name the parent's files +(yc-software/qm#3457, GST-3315). Step 11.5's base-merge check measures a +reviewed range from a landed parent by the same rule. **finish after a stack parent lands (GST-1399).** A child reviewed against its parent branch passes the parent's head to `gstack-ship-release finish diff --git a/lib/ship-receipt-patch.ts b/lib/ship-receipt-patch.ts index 3eaa7b5e7..a8219e08e 100644 --- a/lib/ship-receipt-patch.ts +++ b/lib/ship-receipt-patch.ts @@ -12,7 +12,7 @@ import { isRegistryUnavailable, loadRegistryFromBase } from './regen'; import { readPregateFile } from './pregate/index'; import { isUnavailable, loadPolicyFromBase, matchesAny, type ShipPolicy } from './ship-policy'; import { stampOnlyLine, type BranchSide } from './stamp-only'; -import { CARRY_METADATA_GLOBS, CARRY_TEST_GLOBS, classifyCarryPath } from './ship-receipt'; +import { CARRY_METADATA_GLOBS, CARRY_TEST_GLOBS, classifyCarryPath, landedStackParent } from './ship-receipt'; function git(args: string[], cwd?: string): string | undefined { const r = spawnSync('git', args, { cwd, encoding: 'utf8', timeout: 30_000, maxBuffer: 256 * 1024 * 1024 }); @@ -23,7 +23,8 @@ function git(args: string[], cwd?: string): string | undefined { * One diff's patch-id over a pathspec; empty when the diff is empty. `--verbatim` * keeps whitespace in the hash (the default and `--stable` strip it, so a space * moved in a shell line would read as the same patch) while still ignoring the - * hunk line numbers a base merge shifts. + * hunk line numbers a base merge shifts. `--unified=3` pins the context a user's + * diff.context could shrink, so base edits next to a hunk always reach the hash. */ export function patchId(repoRoot: string, from: string, to: string, pathspec: string[]): string | { error: string } { const firstLine = (s: string | null | undefined, fallback: string) => (s ?? '').trim().split('\n')[0]?.slice(0, 200) || fallback; @@ -34,7 +35,7 @@ export function patchId(repoRoot: string, from: string, to: string, pathspec: st const file = path.join(dir, 'diff.patch'); let fd: number | undefined; try { - const diff = spawnSync('git', ['diff', '--no-color', '--no-ext-diff', '--full-index', `--output=${file}`, from, to, '--', ...pathspec], { cwd: repoRoot, encoding: 'utf8', timeout: 120_000 }); + const diff = spawnSync('git', ['diff', '--no-color', '--no-ext-diff', '--full-index', '--unified=3', `--output=${file}`, from, to, '--', ...pathspec], { cwd: repoRoot, encoding: 'utf8', timeout: 120_000 }); if (diff.status !== 0) return { error: `git diff ${from.slice(0, 12)} ${to.slice(0, 12)}: ${firstLine(diff.stderr, diff.signal ? `killed by ${diff.signal}` : `exit ${diff.status}`)}` }; fd = fs.openSync(file, 'r'); const id = spawnSync('git', ['patch-id', '--verbatim'], { cwd: repoRoot, stdio: [fd, 'pipe', 'pipe'], encoding: 'utf8', timeout: 120_000 }); @@ -156,7 +157,8 @@ export type BaseMergeOnly = { ok: true; note: string } | { ok: false; why: strin * it is a two-parent merge whose second parent the base holds and whose tree is exactly what `git merge-tree` writes for * its parents (no conflict resolution, no edit inside the merge), the working tree is HEAD's, and the branch's patch * against the base is the reviewed one: the product patch-id the carry compares, and the patch-id over every path, so a - * test, release-metadata or gate path the product patch-id leaves out stays covered too. + * test, release-metadata or gate path the product patch-id leaves out stays covered too. A reviewed range that held a stack + * parent which has since landed on the base is measured from that parent, as the carry measures it (landedStackParent). */ export function baseMergeOnly(cwd: string, base: string, reviewed: unknown, reviewedTree: string, currentWtree: string): BaseMergeOnly { const no = (why: string): BaseMergeOnly => ({ ok: false, why }); @@ -183,13 +185,25 @@ export function baseMergeOnly(cwd: string, base: string, reviewed: unknown, revi if (replay.stdout.split('\n')[0]!.trim() !== git(['rev-parse', `${commit}^{tree}`], repoRoot)) return no(`merge ${c} holds changes its clean merge does not (an edit inside the merge)`); } const rules = productRules(repoRoot, base, []); - const product = [productPatchAgainstBase(repoRoot, rules, baseRef, reviewed), productPatchAgainstBase(repoRoot, rules, baseRef, head)]; - for (const id of product) if (typeof id !== 'string') return no(`git could not compute the product patch-id: ${id.error}`); - if (product[0] !== product[1]) return no(`the branch's product patch changed (patch-id ${(product[0] as string).slice(0, 12) || 'none'} reviewed, ${(product[1] as string).slice(0, 12) || 'none'} now)`); - const bases = [git(['merge-base', baseRef, reviewed], repoRoot), git(['merge-base', baseRef, head], repoRoot)]; - if (!bases[0] || !bases[1]) return no(`cannot resolve the merge base with ${baseRef}`); - const whole = [patchId(repoRoot, bases[0], reviewed, ['.']), patchId(repoRoot, bases[1], head, ['.'])]; - for (const id of whole) if (typeof id !== 'string') return no(`git could not compute the branch's patch-id: ${id.error}`); - if (whole[0] !== whole[1]) return no('the branch\'s patch on a path the product patch-id leaves out (tests, release metadata) changed'); - return { ok: true, note: `HEAD ${head.slice(0, 12)} adds only ${chain.length} clean merge(s) of ${baseRef} to the reviewed commit ${reviewed.slice(0, 12)}, and the branch's patch is unchanged (product patch-id ${(product[1] as string).slice(0, 12) || 'none'}); save the reviewed base ${bases[0]} for finish's --reviewed-base` }; + const [reviewedBase, headBase] = [git(['merge-base', baseRef, reviewed], repoRoot), git(['merge-base', baseRef, head], repoRoot)]; + if (!reviewedBase || !headBase) return no(`cannot resolve the merge base with ${baseRef}`); + const patches = (from: string, to: string): { product: string; whole: string; spec: string[] } | string => { + const spec = productSpec(repoRoot, rules, from, to); + if (!spec) return `git diff ${from.slice(0, 12)} ${to.slice(0, 12)} failed`; + const product = patchId(repoRoot, from, to, spec.product), whole = patchId(repoRoot, from, to, ['.']); + if (typeof product !== 'string') return product.error; + return typeof whole === 'string' ? { product, whole, spec: spec.product } : whole.error; + }; + const now = patches(headBase, head); + let rangeBase = reviewedBase; + let then = patches(rangeBase, reviewed); + if (typeof then !== 'string' && typeof now !== 'string' && (then.product !== now.product || then.whole !== now.whole)) { + const parent = landedStackParent(repoRoot, { rangeBase, reviewed, currentBase: headBase }, then.spec); + if (parent) { rangeBase = parent; then = patches(parent, reviewed); } + } + if (typeof then === 'string' || typeof now === 'string') return no(`git could not compute the branch's patch-id: ${typeof then === 'string' ? then : now}`); + if (then.product !== now.product) return no(`the branch's product patch changed (patch-id ${then.product.slice(0, 12) || 'none'} reviewed, ${now.product.slice(0, 12) || 'none'} now)`); + if (then.whole !== now.whole) return no('the branch\'s patch on a path the product patch-id leaves out (tests, release metadata) changed'); + const landed = rangeBase === reviewedBase ? '' : `, its stack parent ${rangeBase.slice(0, 12)} having landed on ${baseRef}`; + return { ok: true, note: `HEAD ${head.slice(0, 12)} adds only ${chain.length} clean merge(s) of ${baseRef} to the reviewed commit ${reviewed.slice(0, 12)}${landed}, and the branch's patch is unchanged (product patch-id ${now.product.slice(0, 12) || 'none'}); save the reviewed base ${rangeBase} for finish's --reviewed-base` }; } diff --git a/lib/ship-receipt.ts b/lib/ship-receipt.ts index 8c6ae22f3..e3fae912e 100644 --- a/lib/ship-receipt.ts +++ b/lib/ship-receipt.ts @@ -440,7 +440,9 @@ export const STACK_PARENT_WALK = 100; * change the current base already holds: merging it into currentBase, or into the base commit that landed it when the * base edited those lines since (heldBy, GST-1399), changes no product path. That is a stack parent * that squash-merged (or merged) into the base after the receipt was written against the base (GST-1199). A commit with no - * product change of its own (a test-first commit) is no parent, since any base trivially holds it. Null when no + * product change of its own (a test-first commit) is no parent, since any base trivially holds it, and neither is a commit + * rangeBase already held: a parent that landed before the review is part of the reviewed base, so measuring from it drops + * nothing and compares the wrong range (yc-software/qm#3457, GST-3315). Null when no * commit within STACK_PARENT_WALK qualifies or git cannot tell; a conflicted product path is never held. */ export function landedStackParent(repoRoot: string, s: { rangeBase: string; reviewed: string; currentBase: string }, product: readonly string[]): string | null { @@ -449,7 +451,7 @@ export function landedStackParent(repoRoot: string, s: { rangeBase: string; revi if (commits.status !== 0) return null; for (const commit of commits.stdout.split('\n').filter(c => c && c !== s.reviewed)) { if (run(['diff', '--quiet', s.rangeBase, commit, '--', ...product]).status !== 1) continue; - if (heldBy(repoRoot, commit, s.currentBase, product)) return commit; + if (heldBy(repoRoot, commit, s.currentBase, product) && !heldBy(repoRoot, commit, s.rangeBase, product)) return commit; } return null; } diff --git a/test/ship-receipt-carry-squash.test.ts b/test/ship-receipt-carry-squash.test.ts index 20db3a387..35c6cf425 100644 --- a/test/ship-receipt-carry-squash.test.ts +++ b/test/ship-receipt-carry-squash.test.ts @@ -10,6 +10,7 @@ import * as path from 'node:path'; import { EXIT } from '../lib/headless-artifacts'; import { extractReceipt, landedStackParent, renderReceiptBlock } from '../lib/ship-receipt'; import { bindShippedReceipt } from '../lib/ship-receipt-depth'; +import { baseMergeOnly } from '../lib/ship-receipt-patch'; import { cleanup, commit, makeFixtureRepo, runBin, write, type FixtureRepo } from './helpers/pregate-fixture'; const repos: FixtureRepo[] = []; @@ -305,3 +306,92 @@ describe('carry after a stack parent lands, the child having merged main before expect(receiptIn(body).head).toBe(reviewed); }, 90_000); }); + +// yc-software/qm#3457 (GST-3315): the parent squash-landed on main and the child merged main before its receipt was +// written against main. A later main merge moved context next to one of the child's hunks, so the carry looked for a +// landed parent, found the parent's commits (which the reviewed base already held) and measured from them, refusing +// RECEIPT_CARRY_PRODUCT_CHANGED on the parent's files. +/** Main moves, the parent squash-lands, the child merges main, its receipt is written against main, then main moves `files` and the child merges it. */ +function landedFirst(files: Record) { + const repo = makeFixtureRepo({ baseFiles: { 'lib/config.js': CONFIG } }); repos.push(repo); + repo.git('checkout', '-q', '-b', 'parent'); + const parent = commit(repo, { 'lib/util.js': SUB }, 'parent: sub'); + repo.git('checkout', '-q', 'feature'); + repo.git('reset', '-q', '--hard', parent); + commit(repo, { 'lib/config.js': configWith({ 'd = 4': 'd = 40' }) }, 'child: d'); + repo.git('checkout', '-q', 'main'); + commit(repo, { 'lib/other.js': 'exports.other = 1;\n' }, 'main moves before the parent lands'); + commit(repo, { 'lib/util.js': SUB }, 'squash: parent (#7)'); + repo.git('push', '-q', 'origin', 'main'); + repo.git('checkout', '-q', 'feature'); + repo.git('merge', '-q', '--no-edit', 'origin/main'); + const reviewed = repo.git('rev-parse', 'HEAD'); + const body = path.join(repo.root, 'body.md'); + expect(runBin(repo, 'gstack-ship-receipt', ['write', '--pr', '8', '--base', 'main', '--version', '1.0.0', '--session-kind', 'unattended', '--body', body]).status).toBe(EXIT.ok); + const rangeBase = repo.git('merge-base', 'origin/main', 'HEAD'); + repo.git('checkout', '-q', 'main'); + commit(repo, files, 'main moves on'); + repo.git('push', '-q', 'origin', 'main'); + repo.git('checkout', '-q', 'feature'); + repo.git('merge', '-q', '--no-edit', 'origin/main'); + return { repo, parent, reviewed, rangeBase, body }; +} + +describe('a stack parent the reviewed base already held is no landed parent (qm#3457, GST-3315)', () => { + // Value: protects=qm#3457: base context moved beside the child's own hunk takes one scoped review of that file, not a + // refusal naming the parent's files; fails_when=landedStackParent counts a parent the reviewed base already held and the + // carry measures from it; seam=gstack-ship-receipt carry; no_claim=the scoped review itself + test('#3457: context moved next to the child\'s hunk routes to a scoped review of that file alone', () => { + const { repo, reviewed, rangeBase, body } = landedFirst({ 'lib/config.js': configWith({ 'f = 6': 'f = 60' }) }); + expect(landedStackParent(repo.work, { rangeBase, reviewed, currentBase: repo.git('merge-base', 'origin/main', 'HEAD') }, ['.', ':(exclude,glob)test/**'])).toBeNull(); + const out = carry(repo, body); + expect([out.status, out.out]).toEqual([EXIT.refused, expect.stringContaining('CARRY: refused (RECEIPT_CARRY_SCOPED_REVIEW)')]); + expect(out.err).toContain('lib/config.js'); + expect(out.err).not.toContain('lib/util.js'); + expect(receiptIn(body).head).toBe(reviewed); + }, 90_000); + + // Value: protects=the patch-id keeps three lines of context whatever the user's git config says; fails_when=diff.context=0 + // drops the moved context from both hashes and the carry passes unread base edits; seam=patchId; no_claim=- + test('diff.context=0 in the user\'s git config still routes moved context to a scoped review', () => { + const { repo, body } = landedFirst({ 'lib/config.js': configWith({ 'f = 6': 'f = 60' }) }); + repo.git('config', 'diff.context', '0'); + const out = carry(repo, body); + expect([out.status, out.out]).toEqual([EXIT.refused, expect.stringContaining('CARRY: refused (RECEIPT_CARRY_SCOPED_REVIEW)')]); + }, 90_000); + + test('an unrelated main change carries', () => { + const { repo, body } = landedFirst({ 'docs/README.md': '# main moves on\n' }); + const out = carry(repo, body); + expect(out.status, out.err).toBe(EXIT.ok); + expect(receiptIn(body).carry!.reason).toBe('base merge'); + }, 90_000); + + // Value: protects=fail closed: an own edit after the merge still refuses, naming the child's file; fails_when=dropping the + // parent hides an own edit; seam=gstack-ship-receipt carry; no_claim=- + test('an own edit after the merge refuses with RECEIPT_CARRY_PRODUCT_CHANGED on the child\'s file', () => { + const { repo, reviewed, body } = landedFirst({ 'docs/README.md': '# main moves on\n' }); + commit(repo, { 'lib/config.js': configWith({ 'd = 4': 'd = 41' }) }, 'child: d again'); + const out = carry(repo, body); + expect([out.status, out.out]).toEqual([EXIT.refused, expect.stringContaining('CARRY: refused (RECEIPT_CARRY_PRODUCT_CHANGED)')]); + expect(out.err).toContain('lib/config.js'); + expect(receiptIn(body).head).toBe(reviewed); + }, 90_000); +}); + +// Step 11.5 measures a reviewed range the way the carry does (GST-3315): a child reviewed while stacked whose parent then +// squash-landed is CURRENT after merging main, from that parent; the qm#3457 shape is not. +describe('Step 11.5\'s base-merge check uses the carry\'s stack-parent rule (GST-3315)', () => { + const at = (repo: FixtureRepo, ref: string) => repo.git('rev-parse', `${ref}^{tree}`); + test('a parent that landed after the review is measured from; one the reviewed base held is not', () => { + const repo = makeFixtureRepo(); repos.push(repo); + const { parent, child } = stack(repo, undefined, undefined, 'main'); + landOnMain(repo, { 'lib/util.js': SUB }); + const ok = baseMergeOnly(repo.work, 'main', child, at(repo, child), at(repo, 'HEAD')); + expect(ok).toEqual({ ok: true, note: expect.stringContaining(`, its stack parent ${parent.slice(0, 12)} having landed on origin/main, and the branch's patch is unchanged`) }); + expect(ok.ok && ok.note.endsWith(`save the reviewed base ${parent} for finish's --reviewed-base`)).toBe(true); + + const { repo: qm, reviewed } = landedFirst({ 'lib/config.js': configWith({ 'f = 6': 'f = 60' }) }); + expect(baseMergeOnly(qm.work, 'main', reviewed, at(qm, reviewed), at(qm, 'HEAD'))).toEqual({ ok: false, why: expect.stringMatching(/^the branch's product patch changed/) }); + }, 120_000); +});