@webpieces/pr-gate 0.4.985 → 0.4.986

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,141 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.LandedCopyResolver = exports.LandedResolution = exports.LandedOverride = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const inversify_1 = require("inversify");
6
+ const git_exec_1 = require("./git-exec");
7
+ const dev_deploy_refs_1 = require("./dev-deploy-refs");
8
+ const dev_copy_on_main_1 = require("./dev-copy-on-main");
9
+ /**
10
+ * One conflicted file resolved automatically, and the landed copies whose old version of it was
11
+ * overridden. Data-only per CLAUDE.md.
12
+ */
13
+ class LandedOverride {
14
+ file;
15
+ copies;
16
+ constructor(file,
17
+ /** The copy refs (`dev-include/<branch>`) whose dev-side version of `file` was overridden. */
18
+ copies) {
19
+ this.file = file;
20
+ this.copies = copies;
21
+ }
22
+ }
23
+ exports.LandedOverride = LandedOverride;
24
+ /** What {@link LandedCopyResolver.resolve} did: what it took the publisher's side on, and what is left. Data-only. */
25
+ class LandedResolution {
26
+ overridden;
27
+ remaining;
28
+ constructor(overridden,
29
+ /** The conflicted files nothing could prove safe to resolve automatically — the developer's job. */
30
+ remaining) {
31
+ this.overridden = overridden;
32
+ this.remaining = remaining;
33
+ }
34
+ }
35
+ exports.LandedResolution = LandedResolution;
36
+ /** One live copy and how it relates to `origin/main`, computed once per resolve. Data-only. */
37
+ class CopyOnMain {
38
+ copy;
39
+ landing;
40
+ branchLanded;
41
+ constructor(copy, landing,
42
+ /** The copy's branch has a MERGED PR — it landed, possibly with different content (review, squash). */
43
+ branchLanded) {
44
+ this.copy = copy;
45
+ this.landing = landing;
46
+ this.branchLanded = branchLanded;
47
+ }
48
+ /** Is the copy's version of `file` superseded by main — its branch landed, or main already carries it? */
49
+ supersededOn(file) {
50
+ return this.branchLanded || this.landing.landed.includes(file);
51
+ }
52
+ }
53
+ /**
54
+ * Resolves the conflicts of `wp-build-push-dev`'s dev merge that are caused ONLY by copies already on main
55
+ * (#1405), by taking the publisher's side.
56
+ *
57
+ * THE SHAPE IT CURES: a copy is published to dev, then its branch lands on main with DIFFERENT content
58
+ * (review commits, then a squash). `dev` still holds the published version of those lines, every branch cut
59
+ * from the latest main carries the landed version, and so every new publish conflicts in the same files —
60
+ * the landed copy's old version against its own final version, with nobody's own change involved.
61
+ *
62
+ * WHY THE PUBLISHER'S SIDE IS RIGHT: the publisher's branch contains the latest `origin/main` (stage ②
63
+ * refuses one that does not), so it already carries the landed copy's FINAL version of the file. Taking it
64
+ * loses nothing, and the copy's content is unchanged, so nothing is redeployed. Dropping landed copies when
65
+ * dev is composed would also cure it, but that is a recompose — a full redeploy (#1401).
66
+ *
67
+ * WHEN IT ACTS, per conflicted file: every live copy that changes the file (relative to its merge-base with
68
+ * `origin/main`) is superseded by main — its branch has a merged PR, or main already holds the copy's content
69
+ * of that file. One copy that is NOT on main, or no copy at all explaining the dev side, and the file is left
70
+ * for the developer, exactly as before: that conflict is somebody's live work.
71
+ */
72
+ let LandedCopyResolver = class LandedCopyResolver {
73
+ gitExec;
74
+ refs;
75
+ onMain;
76
+ constructor(gitExec, refs, onMain) {
77
+ this.gitExec = gitExec;
78
+ this.refs = refs;
79
+ this.onMain = onMain;
80
+ }
81
+ /** Resolve what it can (taking HEAD's side, then `git add`), and return what it did and what remains. */
82
+ async resolve(repoRoot, cfg, conflicted) {
83
+ const copies = await this.refs.liveCopies(repoRoot, cfg);
84
+ await this.refs.fetchCopies(repoRoot, copies.map((c) => c.ref));
85
+ const touching = await this.copiesTouching(repoRoot, copies, conflicted);
86
+ const overridden = [];
87
+ const remaining = [];
88
+ for (const file of conflicted) {
89
+ const contributors = touching.filter((c) => c.landing.changed.includes(file));
90
+ const allLanded = contributors.length > 0
91
+ && contributors.every((c) => c.supersededOn(file));
92
+ if (!allLanded) {
93
+ remaining.push(file);
94
+ continue;
95
+ }
96
+ await this.takePublishersSide(repoRoot, file);
97
+ overridden.push(new LandedOverride(file, contributors.map((c) => c.copy.ref)));
98
+ }
99
+ return new LandedResolution(overridden, remaining);
100
+ }
101
+ /** The report line block for what was overridden; '' when nothing was. */
102
+ describe(resolution) {
103
+ if (resolution.overridden.length === 0)
104
+ return '';
105
+ return '\nResolved automatically — the dev side came only from copies already on origin/main, so your\n'
106
+ + "branch (which carries main's final version) was taken:\n"
107
+ + resolution.overridden.map((o) => ` ${o.file} ← overrode ${o.copies.join(', ')}`).join('\n')
108
+ + '\n';
109
+ }
110
+ // Only the copies that change at least one conflicted file matter; the merged-PR lookup is paid for those only.
111
+ async copiesTouching(repoRoot, copies, conflicted) {
112
+ const touching = [];
113
+ for (const copy of copies) {
114
+ const landing = await this.onMain.landing(repoRoot, copy.ref);
115
+ if (!landing.changed.some((f) => conflicted.includes(f)))
116
+ continue;
117
+ const pr = await this.onMain.mergedPr(repoRoot, copy.sourceBranch);
118
+ touching.push(new CopyOnMain(copy, landing, pr !== null));
119
+ }
120
+ return touching;
121
+ }
122
+ // HEAD is the throwaway, i.e. the publisher's branch. A file HEAD deleted stays deleted.
123
+ async takePublishersSide(repoRoot, file) {
124
+ const inHead = await this.gitExec.tryGit(['-C', repoRoot, 'cat-file', '-e', `HEAD:${file}`], repoRoot);
125
+ const args = inHead.ok
126
+ ? ['-C', repoRoot, 'checkout', '--ours', '--', file]
127
+ : ['-C', repoRoot, 'rm', '-q', '--ignore-unmatch', '--', file];
128
+ await this.gitExec.runGitChecked(args, `Failed to take your branch's side of ${file}`);
129
+ if (inHead.ok) {
130
+ await this.gitExec.runGitChecked(['-C', repoRoot, 'add', '--', file], `Failed to stage the resolution of ${file}`);
131
+ }
132
+ }
133
+ };
134
+ exports.LandedCopyResolver = LandedCopyResolver;
135
+ exports.LandedCopyResolver = LandedCopyResolver = tslib_1.__decorate([
136
+ (0, inversify_1.injectable)(inversify_1.bindingScopeValues.Singleton),
137
+ tslib_1.__metadata("design:paramtypes", [git_exec_1.GitExec,
138
+ dev_deploy_refs_1.DevDeployRefs,
139
+ dev_copy_on_main_1.DevCopyOnMain])
140
+ ], LandedCopyResolver);
141
+ //# sourceMappingURL=landed-copy-resolver.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"landed-copy-resolver.js","sourceRoot":"","sources":["../../../../../../../packages/tooling/pr-gate/src/scripts/workflow/landed-copy-resolver.ts"],"names":[],"mappings":";;;;AACA,yCAA2D;AAE3D,yCAAqC;AACrC,uDAA2D;AAC3D,yDAAmE;AAEnE;;;GAGG;AACH,MAAa,cAAc;IAEV;IAEA;IAHb,YACa,IAAY;IACrB,8FAA8F;IACrF,MAAgB;QAFhB,SAAI,GAAJ,IAAI,CAAQ;QAEZ,WAAM,GAAN,MAAM,CAAU;IAC1B,CAAC;CACP;AAND,wCAMC;AAED,sHAAsH;AACtH,MAAa,gBAAgB;IAEZ;IAEA;IAHb,YACa,UAA4B;IACrC,oGAAoG;IAC3F,SAAmB;QAFnB,eAAU,GAAV,UAAU,CAAkB;QAE5B,cAAS,GAAT,SAAS,CAAU;IAC7B,CAAC;CACP;AAND,4CAMC;AAED,+FAA+F;AAC/F,MAAM,UAAU;IAEC;IACA;IAEA;IAJb,YACa,IAAa,EACb,OAAuB;IAChC,uGAAuG;IAC9F,YAAqB;QAHrB,SAAI,GAAJ,IAAI,CAAS;QACb,YAAO,GAAP,OAAO,CAAgB;QAEvB,iBAAY,GAAZ,YAAY,CAAS;IAC/B,CAAC;IAEJ,0GAA0G;IAC1G,YAAY,CAAC,IAAY;QACrB,OAAO,IAAI,CAAC,YAAY,IAAI,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;IACnE,CAAC;CACJ;AAED;;;;;;;;;;;;;;;;;;GAkBG;AAEI,IAAM,kBAAkB,GAAxB,MAAM,kBAAkB;IAEN;IACA;IACA;IAHrB,YACqB,OAAgB,EAChB,IAAmB,EACnB,MAAqB;QAFrB,YAAO,GAAP,OAA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F,KAAK,CAAC,kBAAkB,CAAC,QAAgB,EAAE,IAAY;QAC3D,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,IAAI,EAAE,QAAQ,EAAE,UAAU,EAAE,IAAI,EAAE,QAAQ,IAAI,EAAE,CAAC,EAAE,QAAQ,CAAC,CAAC;QACvG,MAAM,IAAI,GAAG,MAAM,CAAC,EAAE;YAClB,CAAC,CAAC,CAAC,IAAI,EAAE,QAAQ,EAAE,UAAU,EAAE,QAAQ,EAAE,IAAI,EAAE,IAAI,CAAC;YACpD,CAAC,CAAC,CAAC,IAAI,EAAE,QAAQ,EAAE,IAAI,EAAE,IAAI,EAAE,kBAAkB,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC;QACnE,MAAM,IAAI,CAAC,OAAO,CAAC,aAAa,CAAC,IAAI,EAAE,wCAAwC,IAAI,EAAE,CAAC,CAAC;QACvF,IAAI,MAAM,CAAC,EAAE,EAAE,CAAC;YACZ,MAAM,IAAI,CAAC,OAAO,CAAC,aAAa,CAAC,CAAC,IAAI,EAAE,QAAQ,EAAE,KAAK,EAAE,IAAI,EAAE,IAAI,CAAC,EAAE,qCAAqC,IAAI,EAAE,CAAC,CAAC;QACvH,CAAC;IACL,CAAC;CACJ,CAAA;AA5DY,gDAAkB;6BAAlB,kBAAkB;IAD9B,IAAA,sBAAU,EAAC,8BAAkB,CAAC,SAAS,CAAC;6CAGP,kBAAO;QACV,+BAAa;QACX,gCAAa;GAJjC,kBAAkB,CA4D9B","sourcesContent":["import { DevDeployConfig } from '@webpieces/rules-config';\nimport { injectable, bindingScopeValues } from 'inversify';\n\nimport { GitExec } from './git-exec';\nimport { DevCopy, DevDeployRefs } from './dev-deploy-refs';\nimport { DevCopyLanding, DevCopyOnMain } from './dev-copy-on-main';\n\n/**\n * One conflicted file resolved automatically, and the landed copies whose old version of it was\n * overridden. Data-only per CLAUDE.md.\n */\nexport class LandedOverride {\n constructor(\n readonly file: string,\n /** The copy refs (`dev-include/<branch>`) whose dev-side version of `file` was overridden. */\n readonly copies: string[],\n ) {}\n}\n\n/** What {@link LandedCopyResolver.resolve} did: what it took the publisher's side on, and what is left. Data-only. */\nexport class LandedResolution {\n constructor(\n readonly overridden: LandedOverride[],\n /** The conflicted files nothing could prove safe to resolve automatically — the developer's job. */\n readonly remaining: string[],\n ) {}\n}\n\n/** One live copy and how it relates to `origin/main`, computed once per resolve. Data-only. */\nclass CopyOnMain {\n constructor(\n readonly copy: DevCopy,\n readonly landing: DevCopyLanding,\n /** The copy's branch has a MERGED PR — it landed, possibly with different content (review, squash). */\n readonly branchLanded: boolean,\n ) {}\n\n /** Is the copy's version of `file` superseded by main — its branch landed, or main already carries it? */\n supersededOn(file: string): boolean {\n return this.branchLanded || this.landing.landed.includes(file);\n }\n}\n\n/**\n * Resolves the conflicts of `wp-build-push-dev`'s dev merge that are caused ONLY by copies already on main\n * (#1405), by taking the publisher's side.\n *\n * THE SHAPE IT CURES: a copy is published to dev, then its branch lands on main with DIFFERENT content\n * (review commits, then a squash). `dev` still holds the published version of those lines, every branch cut\n * from the latest main carries the landed version, and so every new publish conflicts in the same files —\n * the landed copy's old version against its own final version, with nobody's own change involved.\n *\n * WHY THE PUBLISHER'S SIDE IS RIGHT: the publisher's branch contains the latest `origin/main` (stage ②\n * refuses one that does not), so it already carries the landed copy's FINAL version of the file. Taking it\n * loses nothing, and the copy's content is unchanged, so nothing is redeployed. Dropping landed copies when\n * dev is composed would also cure it, but that is a recompose — a full redeploy (#1401).\n *\n * WHEN IT ACTS, per conflicted file: every live copy that changes the file (relative to its merge-base with\n * `origin/main`) is superseded by main — its branch has a merged PR, or main already holds the copy's content\n * of that file. One copy that is NOT on main, or no copy at all explaining the dev side, and the file is left\n * for the developer, exactly as before: that conflict is somebody's live work.\n */\n@injectable(bindingScopeValues.Singleton)\nexport class LandedCopyResolver {\n constructor(\n private readonly gitExec: GitExec,\n private readonly refs: DevDeployRefs,\n private readonly onMain: DevCopyOnMain,\n ) {}\n\n /** Resolve what it can (taking HEAD's side, then `git add`), and return what it did and what remains. */\n async resolve(repoRoot: string, cfg: DevDeployConfig, conflicted: string[]): Promise<LandedResolution> {\n const copies = await this.refs.liveCopies(repoRoot, cfg);\n await this.refs.fetchCopies(repoRoot, copies.map((c: DevCopy): string => c.ref));\n const touching = await this.copiesTouching(repoRoot, copies, conflicted);\n const overridden: LandedOverride[] = [];\n const remaining: string[] = [];\n for (const file of conflicted) {\n const contributors = touching.filter((c: CopyOnMain): boolean => c.landing.changed.includes(file));\n const allLanded = contributors.length > 0\n && contributors.every((c: CopyOnMain): boolean => c.supersededOn(file));\n if (!allLanded) {\n remaining.push(file);\n continue;\n }\n await this.takePublishersSide(repoRoot, file);\n overridden.push(new LandedOverride(file, contributors.map((c: CopyOnMain): string => c.copy.ref)));\n }\n return new LandedResolution(overridden, remaining);\n }\n\n /** The report line block for what was overridden; '' when nothing was. */\n describe(resolution: LandedResolution): string {\n if (resolution.overridden.length === 0) return '';\n return '\\nResolved automatically — the dev side came only from copies already on origin/main, so your\\n'\n + \"branch (which carries main's final version) was taken:\\n\"\n + resolution.overridden.map((o: LandedOverride): string => ` ${o.file} ← overrode ${o.copies.join(', ')}`).join('\\n')\n + '\\n';\n }\n\n // Only the copies that change at least one conflicted file matter; the merged-PR lookup is paid for those only.\n private async copiesTouching(repoRoot: string, copies: DevCopy[], conflicted: string[]): Promise<CopyOnMain[]> {\n const touching: CopyOnMain[] = [];\n for (const copy of copies) {\n const landing = await this.onMain.landing(repoRoot, copy.ref);\n if (!landing.changed.some((f: string): boolean => conflicted.includes(f))) continue;\n const pr = await this.onMain.mergedPr(repoRoot, copy.sourceBranch);\n touching.push(new CopyOnMain(copy, landing, pr !== null));\n }\n return touching;\n }\n\n // HEAD is the throwaway, i.e. the publisher's branch. A file HEAD deleted stays deleted.\n private async takePublishersSide(repoRoot: string, file: string): Promise<void> {\n const inHead = await this.gitExec.tryGit(['-C', repoRoot, 'cat-file', '-e', `HEAD:${file}`], repoRoot);\n const args = inHead.ok\n ? ['-C', repoRoot, 'checkout', '--ours', '--', file]\n : ['-C', repoRoot, 'rm', '-q', '--ignore-unmatch', '--', file];\n await this.gitExec.runGitChecked(args, `Failed to take your branch's side of ${file}`);\n if (inHead.ok) {\n await this.gitExec.runGitChecked(['-C', repoRoot, 'add', '--', file], `Failed to stage the resolution of ${file}`);\n }\n }\n}\n"]}