mirror of
https://github.com/garrytan/gstack.git
synced 2026-11-23 08:31:44 +01:00
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>
This commit is contained in:
1 parent
faae7f725a
commit
ef4a7f5cbf
5 files changed
+127
-16
No files matched your search
+27
-13
@@ -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` };
|
||||
}
|
||||
+4
-2
@@ -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;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user