toolcrib 0.9.0 → 0.11.0
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.
- package/README.md +95 -17
- package/package.json +1 -1
- package/src/commands/apply.js +34 -9
- package/src/commands/exportPatches.js +283 -0
- package/src/commands/init.js +7 -1
- package/src/commands/merge.js +248 -86
- package/src/index.js +11 -0
- package/src/lib/discussions.js +183 -0
- package/src/lib/exportFormat.js +224 -0
- package/src/lib/exportPatches.js +582 -0
- package/src/lib/git.js +95 -6
- package/src/lib/importScan.js +139 -0
- package/src/lib/lineMerge.js +190 -0
- package/src/lib/patches.js +80 -27
- package/src/lib/release.js +3 -0
- package/src/lib/zip.js +61 -0
package/src/commands/merge.js
CHANGED
|
@@ -1,8 +1,13 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
1
2
|
import path from 'node:path';
|
|
2
3
|
import * as p from '@clack/prompts';
|
|
4
|
+
import { createTwoFilesPatch } from 'diff';
|
|
3
5
|
import { fetchRelease, fetchTestsRelease } from '../lib/release.js';
|
|
4
6
|
import { resolveVersion } from '../lib/github.js';
|
|
5
|
-
import {
|
|
7
|
+
import { isGitInstalled } from '../lib/git.js';
|
|
8
|
+
import { listFilesRecursive } from '../lib/zip.js';
|
|
9
|
+
import { isBinaryBuffer, detectEol, toEol, mergeThreeWay } from '../lib/lineMerge.js';
|
|
10
|
+
import { PendingChanges, normalize, joinPatchPath, hasStagedWork, STAGED_WORK_MESSAGE } from '../lib/patches.js';
|
|
6
11
|
import { resolveDependencyDecisions, buildProposedPackageJson, mergeImportsField } from '../lib/deps.js';
|
|
7
12
|
import { fileExists, readJsonIfExists, readLock, readTextIfExists, proposeLockUpdate } from '../lib/project.js';
|
|
8
13
|
import { MANAGED_DOCS, KNOWN_TARGET_FILES, listManagedBlocks, upsertManagedBlock } from '../lib/managedDocs.js';
|
|
@@ -11,6 +16,12 @@ const TOOLKIT_DIR = './toolcrib';
|
|
|
11
16
|
// See init.js for why this exists — installs predating this entry need it
|
|
12
17
|
// added on `merge` too, not just on a fresh `init`.
|
|
13
18
|
const TOOLKIT_IMPORTS_ENTRY = { '#toolcrib': './toolcrib/index.ts' };
|
|
19
|
+
// Conflict notes and binary upstream copies are staged here, under the
|
|
20
|
+
// staging folder (never under ./toolcrib/, and never as `.patch` files, so
|
|
21
|
+
// `apply` cannot write them into the kit).
|
|
22
|
+
const NOTES_DIR = 'conflicts';
|
|
23
|
+
// Meta files inside ./toolcrib/ that the CLI itself owns; never "project-added".
|
|
24
|
+
const KIT_META_FILES = new Set(['.toolcrib-lock.json', '.toolcrib-tests-config.json']);
|
|
14
25
|
|
|
15
26
|
/**
|
|
16
27
|
* Four-way classification per vendored file, comparing:
|
|
@@ -20,6 +31,11 @@ const TOOLKIT_IMPORTS_ENTRY = { '#toolcrib': './toolcrib/index.ts' };
|
|
|
20
31
|
*
|
|
21
32
|
* All three are read fresh, on demand, every time — nothing here trusts
|
|
22
33
|
* a previously-cached hash (see design notes on why that was dropped).
|
|
34
|
+
*
|
|
35
|
+
* Returns one of five outcomes: 'unchanged', 'safe-update', 'keep-local',
|
|
36
|
+
* 'conflict', or 'converged' (both sides changed to identical content).
|
|
37
|
+
* 'conflict' means both sides changed differently; the caller then tries a
|
|
38
|
+
* line-level three-way merge before reporting anything as a conflict.
|
|
23
39
|
*/
|
|
24
40
|
function classify(original, local, updated) {
|
|
25
41
|
const localModified = normalize(local) !== normalize(original);
|
|
@@ -28,9 +44,123 @@ function classify(original, local, updated) {
|
|
|
28
44
|
if (!localModified && !upstreamChanged) return 'unchanged';
|
|
29
45
|
if (!localModified && upstreamChanged) return 'safe-update';
|
|
30
46
|
if (localModified && !upstreamChanged) return 'keep-local';
|
|
47
|
+
// Both sides changed, but to the same content (e.g. upstream adopted the
|
|
48
|
+
// consumer's own fix): nothing to propose and nothing to resolve.
|
|
49
|
+
if (normalize(local) === normalize(updated)) return 'converged';
|
|
31
50
|
return 'conflict';
|
|
32
51
|
}
|
|
33
52
|
|
|
53
|
+
/** Raw bytes of a release file; test fakes without readBuffer fall back to their text. */
|
|
54
|
+
function readBytes(release, relPath) {
|
|
55
|
+
return release.readBuffer ? release.readBuffer(relPath) : Buffer.from(release.readFile(relPath), 'utf8');
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function newResults() {
|
|
59
|
+
return {
|
|
60
|
+
updated: 0, // clean update patches proposed
|
|
61
|
+
merged: [], // both sides changed, line merge was clean
|
|
62
|
+
absorbed: [], // local change is already contained in the new release
|
|
63
|
+
kept: 0, // local edit, upstream untouched
|
|
64
|
+
deleted: 0, // removed upstream, proposed for deletion
|
|
65
|
+
projectAdded: [], // in ./toolcrib/ but in neither release
|
|
66
|
+
conflicts: [], // { relPath, displayPath, kind, collision, notePath }
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const fenced = (text) => `~~~~diff\n${text.trimEnd()}\n~~~~\n`;
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Stage a conflict's note (and, for a binary file, the untouched upstream
|
|
74
|
+
* copy) in the staging folder and record it for the summary. `noteName` is
|
|
75
|
+
* the toolkit-relative path the note is filed under.
|
|
76
|
+
*/
|
|
77
|
+
function recordConflict(ctx, { relPath, noteName = relPath, displayPath, kind, collision = false, body, upstreamBytes }) {
|
|
78
|
+
const notePath = `${NOTES_DIR}/${noteName}.md`;
|
|
79
|
+
const copyPath = upstreamBytes ? `${NOTES_DIR}/${noteName}.upstream` : null;
|
|
80
|
+
const heading = collision ? `Add/add collision: ${displayPath}` : `Conflict: ${displayPath}`;
|
|
81
|
+
const copyLine = copyPath ? `\nThe untouched upstream copy (v${ctx.newVersion}) is staged beside this note as \`${path.posix.basename(copyPath)}\`.\n` : '';
|
|
82
|
+
ctx.changes.addStagedFile(notePath, `# ${heading}\n\n${body}\n${copyLine}`);
|
|
83
|
+
if (copyPath) ctx.changes.addStagedFile(copyPath, upstreamBytes);
|
|
84
|
+
ctx.results.conflicts.push({ relPath, displayPath, kind, collision, notePath: `./toolcrib-patches/${notePath}` });
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function upstreamDiff(ctx, relPath, original, updated) {
|
|
88
|
+
return (
|
|
89
|
+
`## What upstream changed (v${ctx.oldVersion} to v${ctx.newVersion})\n\n` +
|
|
90
|
+
fenced(createTwoFilesPatch(`a/${relPath}`, `b/${relPath}`, normalize(original), normalize(updated)))
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* The file changed on both sides, differently: try a line-level three-way
|
|
96
|
+
* merge (original = base, local = ours, updated = theirs). A clean merge is
|
|
97
|
+
* proposed as an ordinary patch; overlapping edits are proposed with conflict
|
|
98
|
+
* markers and reported; anything that can't be line-merged (binary, deleted
|
|
99
|
+
* locally) gets a note in the staging folder and the local file is untouched.
|
|
100
|
+
*/
|
|
101
|
+
async function resolveBothChanged(ctx, { relPath, targetPath, hasOriginal, oldRelease, newRelease, original, updated, collision }) {
|
|
102
|
+
const patchPath = joinPatchPath(TOOLKIT_DIR, relPath);
|
|
103
|
+
const displayPath = patchPath;
|
|
104
|
+
const base = { relPath, displayPath, collision };
|
|
105
|
+
|
|
106
|
+
if (!fileExists(targetPath)) {
|
|
107
|
+
// In the old release, deleted from the project, changed upstream: nothing to merge into.
|
|
108
|
+
recordConflict(ctx, {
|
|
109
|
+
...base,
|
|
110
|
+
kind: 'deleted-locally',
|
|
111
|
+
body:
|
|
112
|
+
`This file was deleted from the project, but upstream changed it in v${ctx.newVersion}.\n` +
|
|
113
|
+
`It was not recreated. Decide whether to restore it (take the upstream version below) or keep it deleted.\n\n` +
|
|
114
|
+
upstreamDiff(ctx, relPath, original, updated),
|
|
115
|
+
});
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const localBytes = fs.readFileSync(targetPath);
|
|
120
|
+
const originalBytes = hasOriginal ? readBytes(oldRelease, relPath) : Buffer.alloc(0);
|
|
121
|
+
const updatedBytes = readBytes(newRelease, relPath);
|
|
122
|
+
|
|
123
|
+
if (isBinaryBuffer(localBytes) || isBinaryBuffer(originalBytes) || isBinaryBuffer(updatedBytes)) {
|
|
124
|
+
recordConflict(ctx, {
|
|
125
|
+
...base,
|
|
126
|
+
kind: 'binary',
|
|
127
|
+
body:
|
|
128
|
+
`This is a binary file (not valid UTF-8 text), so it was not line-merged. ` +
|
|
129
|
+
`Both your project and upstream changed it${collision ? '' : ` since v${ctx.oldVersion}`}.\n` +
|
|
130
|
+
`Your copy in ./toolcrib/ is untouched. Pick one, or reconcile them with a tool that understands the format.`,
|
|
131
|
+
upstreamBytes: updatedBytes,
|
|
132
|
+
});
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
const localRaw = localBytes.toString('utf8'); // exact disk content (BOM and CRLF intact): what the patch must match
|
|
137
|
+
const hasBom = localRaw.charCodeAt(0) === 0xfeff;
|
|
138
|
+
const eol = detectEol(localRaw);
|
|
139
|
+
const ours = normalize(hasBom ? localRaw.slice(1) : localRaw);
|
|
140
|
+
const theirs = normalize(updated);
|
|
141
|
+
const result = await mergeThreeWay({ base: normalize(original), ours, theirs }, { engine: ctx.engine, newLabel: `v${ctx.newVersion}` });
|
|
142
|
+
|
|
143
|
+
const proposed = (hasBom ? '' : '') + toEol(result.merged, eol);
|
|
144
|
+
|
|
145
|
+
if (result.conflicts === 0) {
|
|
146
|
+
if (result.merged === theirs) ctx.results.absorbed.push(displayPath);
|
|
147
|
+
else ctx.results.merged.push(displayPath);
|
|
148
|
+
ctx.changes.propose(patchPath, localRaw, proposed, relPath);
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
ctx.changes.propose(patchPath, localRaw, proposed, `${relPath} (conflict markers)`);
|
|
153
|
+
recordConflict(ctx, {
|
|
154
|
+
...base,
|
|
155
|
+
kind: 'markers',
|
|
156
|
+
body:
|
|
157
|
+
`${collision ? 'The project added this file and the new release now ships one at the same path. ' : 'Both your project and upstream changed this file. '}` +
|
|
158
|
+
`${result.conflicts} region(s) overlap. The patch for ${displayPath} writes the merged file with ` +
|
|
159
|
+
`\`<<<<<<< local\` / \`=======\` / \`>>>>>>> v${ctx.newVersion}\` markers; resolve each one in place and delete the markers.\n\n` +
|
|
160
|
+
(collision ? '' : upstreamDiff(ctx, relPath, original, updated)),
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
|
|
34
164
|
/**
|
|
35
165
|
* The full four-way diff, applied per-file across one (oldRelease,
|
|
36
166
|
* newRelease) pair — extracted so both the core toolkit files and (when
|
|
@@ -41,14 +171,11 @@ function classify(original, local, updated) {
|
|
|
41
171
|
* scripts/build-tests-release.js), so no separate target-directory
|
|
42
172
|
* parameter is needed here.
|
|
43
173
|
*
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
* fold into its own running totals.
|
|
174
|
+
* Records everything in `ctx` (`changes` for patches/notes, `results` for the
|
|
175
|
+
* summary).
|
|
47
176
|
*/
|
|
48
|
-
function diffReleaseFiles(projectRoot, oldRelease, newRelease,
|
|
49
|
-
|
|
50
|
-
let keptCount = 0;
|
|
51
|
-
let deletedCount = 0;
|
|
177
|
+
async function diffReleaseFiles(projectRoot, oldRelease, newRelease, ctx) {
|
|
178
|
+
const { changes, results } = ctx;
|
|
52
179
|
|
|
53
180
|
// Found by review: allFiles() re-walks its temp directory recursively on
|
|
54
181
|
// every call (listFilesRecursive is not cached), so calling
|
|
@@ -65,8 +192,12 @@ function diffReleaseFiles(projectRoot, oldRelease, newRelease, changes, conflict
|
|
|
65
192
|
const targetPath = path.join(projectRoot, TOOLKIT_DIR, relPath);
|
|
66
193
|
const local = readTextIfExists(targetPath);
|
|
67
194
|
const updated = newRelease.readFile(relPath);
|
|
68
|
-
|
|
69
|
-
|
|
195
|
+
const hasOriginal = oldFiles.has(relPath);
|
|
196
|
+
// A file that's new in this release won't exist in oldRelease — treat as
|
|
197
|
+
// an empty original. If the project already has a file at that path it is
|
|
198
|
+
// an add/add collision: the same three-way merge, with an empty base.
|
|
199
|
+
const original = hasOriginal ? oldRelease.readFile(relPath) : '';
|
|
200
|
+
const collision = !hasOriginal && fileExists(targetPath);
|
|
70
201
|
|
|
71
202
|
const status = classify(original, local, updated);
|
|
72
203
|
|
|
@@ -74,13 +205,18 @@ function diffReleaseFiles(projectRoot, oldRelease, newRelease, changes, conflict
|
|
|
74
205
|
case 'unchanged':
|
|
75
206
|
case 'safe-update':
|
|
76
207
|
changes.propose(joinPatchPath(TOOLKIT_DIR, relPath), local, updated, relPath);
|
|
77
|
-
|
|
208
|
+
results.updated++;
|
|
209
|
+
break;
|
|
210
|
+
case 'converged':
|
|
211
|
+
// Local already matches the new release, so there's nothing to
|
|
212
|
+
// propose: the change was absorbed upstream.
|
|
213
|
+
results.absorbed.push(joinPatchPath(TOOLKIT_DIR, relPath));
|
|
78
214
|
break;
|
|
79
215
|
case 'keep-local':
|
|
80
|
-
|
|
216
|
+
results.kept++;
|
|
81
217
|
break;
|
|
82
218
|
case 'conflict':
|
|
83
|
-
|
|
219
|
+
await resolveBothChanged(ctx, { relPath, targetPath, hasOriginal, oldRelease, newRelease, original, updated, collision });
|
|
84
220
|
break;
|
|
85
221
|
}
|
|
86
222
|
}
|
|
@@ -95,7 +231,8 @@ function diffReleaseFiles(projectRoot, oldRelease, newRelease, changes, conflict
|
|
|
95
231
|
// upstream" as updated content of '': local unmodified from original ->
|
|
96
232
|
// safe to delete; local modified -> a real conflict (don't silently
|
|
97
233
|
// discard someone's customization just because upstream dropped the
|
|
98
|
-
// file), same as any other conflicting edit.
|
|
234
|
+
// file), same as any other conflicting edit. No merge is possible here, so
|
|
235
|
+
// it's a note in the staging folder.
|
|
99
236
|
for (const relPath of oldFiles) {
|
|
100
237
|
if (newFiles.has(relPath)) continue;
|
|
101
238
|
|
|
@@ -109,25 +246,45 @@ function diffReleaseFiles(projectRoot, oldRelease, newRelease, changes, conflict
|
|
|
109
246
|
switch (status) {
|
|
110
247
|
case 'safe-update':
|
|
111
248
|
changes.propose(joinPatchPath(TOOLKIT_DIR, relPath), local, '', relPath);
|
|
112
|
-
|
|
249
|
+
results.deleted++;
|
|
113
250
|
break;
|
|
114
251
|
case 'conflict':
|
|
115
|
-
|
|
252
|
+
recordConflict(ctx, {
|
|
116
253
|
relPath,
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
254
|
+
displayPath: joinPatchPath(TOOLKIT_DIR, relPath),
|
|
255
|
+
kind: 'removed-upstream',
|
|
256
|
+
body:
|
|
257
|
+
`This file was modified locally, but removed upstream in v${ctx.newVersion}.\n` +
|
|
258
|
+
`Automatic deletion was skipped and your copy is untouched. Review it, or ask your AI assistant:\n` +
|
|
259
|
+
`"reconcile my local changes to ${relPath}, which was removed upstream"\n`,
|
|
122
260
|
});
|
|
123
261
|
break;
|
|
124
262
|
// 'unchanged'/'keep-local' can't occur here: original is always
|
|
125
263
|
// non-empty (relPath came from oldRelease.allFiles()) and updated is
|
|
126
|
-
// always '', so upstreamChanged is unconditionally true.
|
|
264
|
+
// always '', so upstreamChanged is unconditionally true. 'converged'
|
|
265
|
+
// only happens for a locally-emptied file (local === updated === ''),
|
|
266
|
+
// which this switch deliberately has no case for: nothing is
|
|
267
|
+
// proposed, so the empty file is left on disk as-is.
|
|
127
268
|
}
|
|
128
269
|
}
|
|
270
|
+
}
|
|
129
271
|
|
|
130
|
-
|
|
272
|
+
/**
|
|
273
|
+
* Files under ./toolcrib/ that neither the old nor the new release ships:
|
|
274
|
+
* the project's own additions. Reported, never touched. `known` is every
|
|
275
|
+
* release-relative path of every release involved; `testsInstalled` decides
|
|
276
|
+
* whether `__tests__/` is the kit's (and so already covered by `known`) or
|
|
277
|
+
* is simply not looked at.
|
|
278
|
+
*/
|
|
279
|
+
function findProjectAdded(projectRoot, known, testsInstalled) {
|
|
280
|
+
const toolkitRoot = path.join(projectRoot, TOOLKIT_DIR);
|
|
281
|
+
if (!fs.existsSync(toolkitRoot)) return [];
|
|
282
|
+
return listFilesRecursive(toolkitRoot)
|
|
283
|
+
.filter((relPath) => !KIT_META_FILES.has(relPath))
|
|
284
|
+
.filter((relPath) => testsInstalled || !relPath.startsWith('__tests__/'))
|
|
285
|
+
.filter((relPath) => !known.has(relPath))
|
|
286
|
+
.map((relPath) => joinPatchPath(TOOLKIT_DIR, relPath))
|
|
287
|
+
.sort();
|
|
131
288
|
}
|
|
132
289
|
|
|
133
290
|
/**
|
|
@@ -142,9 +299,8 @@ function diffReleaseFiles(projectRoot, oldRelease, newRelease, changes, conflict
|
|
|
142
299
|
* the block may be older or newer than the currently-installed toolkit
|
|
143
300
|
* version if it was hand-edited or added out of band.
|
|
144
301
|
*/
|
|
145
|
-
async function mergeManagedBlocks(projectRoot, oldRelease, newRelease,
|
|
146
|
-
|
|
147
|
-
let keptCount = 0;
|
|
302
|
+
async function mergeManagedBlocks(projectRoot, oldRelease, newRelease, ctx) {
|
|
303
|
+
const { changes, results } = ctx;
|
|
148
304
|
|
|
149
305
|
for (const targetFile of KNOWN_TARGET_FILES) {
|
|
150
306
|
const targetPath = path.join(projectRoot, targetFile);
|
|
@@ -190,25 +346,35 @@ async function mergeManagedBlocks(projectRoot, oldRelease, newRelease, changes,
|
|
|
190
346
|
switch (status) {
|
|
191
347
|
case 'unchanged':
|
|
192
348
|
case 'safe-update':
|
|
349
|
+
case 'converged':
|
|
350
|
+
// 'converged': the block's content already matches the new doc, but
|
|
351
|
+
// its version= attribute still names the old version. It must be
|
|
352
|
+
// advanced anyway, or the next merge treats the old version's doc as
|
|
353
|
+
// the "original" and misclassifies this block.
|
|
193
354
|
proposedFileContent = upsertManagedBlock(proposedFileContent, block.docId, newRelease.version, updatedDocContent);
|
|
194
|
-
fileChanged = fileChanged || status
|
|
195
|
-
|
|
355
|
+
fileChanged = fileChanged || status !== 'unchanged';
|
|
356
|
+
if (status === 'converged') results.absorbed.push(`${targetFile} (managed block "${block.docId}")`);
|
|
357
|
+
else results.updated++;
|
|
196
358
|
break;
|
|
197
359
|
case 'keep-local':
|
|
198
|
-
|
|
360
|
+
results.kept++;
|
|
199
361
|
break;
|
|
200
|
-
case 'conflict':
|
|
201
|
-
// Not a vendored file under TOOLKIT_DIR
|
|
202
|
-
//
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
relPath:
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
362
|
+
case 'conflict': {
|
|
363
|
+
// Not a vendored file under TOOLKIT_DIR. The block isn't a whole
|
|
364
|
+
// file, so no line merge: a note with the upstream change.
|
|
365
|
+
const displayPath = `${targetFile} (managed block "${block.docId}")`;
|
|
366
|
+
recordConflict(ctx, {
|
|
367
|
+
relPath: displayPath,
|
|
368
|
+
noteName: `${targetFile}.managed-${block.docId}`,
|
|
369
|
+
displayPath,
|
|
370
|
+
kind: 'managed-block',
|
|
371
|
+
body:
|
|
372
|
+
`The managed block "${block.docId}" in ${targetFile} was edited locally AND changed upstream.\n` +
|
|
373
|
+
`It was left as-is. Reconcile it by hand or ask your AI assistant.\n\n` +
|
|
374
|
+
upstreamDiff(ctx, `${targetFile}.managed-${block.docId}`, originalDocContent, updatedDocContent),
|
|
210
375
|
});
|
|
211
376
|
break;
|
|
377
|
+
}
|
|
212
378
|
}
|
|
213
379
|
}
|
|
214
380
|
|
|
@@ -216,8 +382,6 @@ async function mergeManagedBlocks(projectRoot, oldRelease, newRelease, changes,
|
|
|
216
382
|
changes.propose(targetFile, currentFileContent, proposedFileContent, targetFile);
|
|
217
383
|
}
|
|
218
384
|
}
|
|
219
|
-
|
|
220
|
-
return { updatedCount, keptCount };
|
|
221
385
|
}
|
|
222
386
|
|
|
223
387
|
export async function mergeCommand(options) {
|
|
@@ -231,6 +395,12 @@ export async function mergeCommand(options) {
|
|
|
231
395
|
return;
|
|
232
396
|
}
|
|
233
397
|
|
|
398
|
+
if (hasStagedWork(projectRoot)) {
|
|
399
|
+
p.log.error(STAGED_WORK_MESSAGE);
|
|
400
|
+
process.exitCode = 1;
|
|
401
|
+
return;
|
|
402
|
+
}
|
|
403
|
+
|
|
234
404
|
const spinner = p.spinner();
|
|
235
405
|
spinner.start(`Fetching current (v${lock.version}) and target (${options.version}) releases`);
|
|
236
406
|
// Resolved once, then reused as a concrete version for every fetch below
|
|
@@ -285,23 +455,25 @@ export async function mergeCommand(options) {
|
|
|
285
455
|
}
|
|
286
456
|
|
|
287
457
|
const changes = new PendingChanges();
|
|
288
|
-
const
|
|
289
|
-
|
|
290
|
-
const
|
|
291
|
-
|
|
292
|
-
let keptCount = coreDiff.keptCount;
|
|
293
|
-
let deletedCount = coreDiff.deletedCount;
|
|
458
|
+
const results = newResults();
|
|
459
|
+
// `mergeEngine` is a test seam ('git' | 'js'), not a CLI flag.
|
|
460
|
+
const engine = options.mergeEngine ?? ((await isGitInstalled()) ? 'git' : 'js');
|
|
461
|
+
const ctx = { changes, results, engine, oldVersion: oldRelease.version, newVersion: newRelease.version };
|
|
294
462
|
|
|
463
|
+
await diffReleaseFiles(projectRoot, oldRelease, newRelease, ctx);
|
|
295
464
|
if (oldTestsRelease && newTestsRelease) {
|
|
296
|
-
|
|
297
|
-
updatedCount += testsDiff.updatedCount;
|
|
298
|
-
keptCount += testsDiff.keptCount;
|
|
299
|
-
deletedCount += testsDiff.deletedCount;
|
|
465
|
+
await diffReleaseFiles(projectRoot, oldTestsRelease, newTestsRelease, ctx);
|
|
300
466
|
}
|
|
301
467
|
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
468
|
+
await mergeManagedBlocks(projectRoot, oldRelease, newRelease, ctx);
|
|
469
|
+
|
|
470
|
+
const known = new Set([
|
|
471
|
+
...oldRelease.allFiles(),
|
|
472
|
+
...newRelease.allFiles(),
|
|
473
|
+
...(oldTestsRelease?.allFiles() ?? []),
|
|
474
|
+
...(newTestsRelease?.allFiles() ?? []),
|
|
475
|
+
]);
|
|
476
|
+
results.projectAdded = findProjectAdded(projectRoot, known, Boolean(lock.testsVersion));
|
|
305
477
|
|
|
306
478
|
await cleanupAll();
|
|
307
479
|
|
|
@@ -375,32 +547,37 @@ export async function mergeCommand(options) {
|
|
|
375
547
|
);
|
|
376
548
|
}
|
|
377
549
|
|
|
378
|
-
// Conflicts get their own patch showing the *upstream* diff (local vs.
|
|
379
|
-
// untouched), so the human/AI can see exactly what upstream changed
|
|
380
|
-
// without it silently overwriting local customizations.
|
|
381
|
-
for (const conflict of conflicts) {
|
|
382
|
-
changes.propose(
|
|
383
|
-
`${conflict.patchPath}.upstream-diff`,
|
|
384
|
-
'',
|
|
385
|
-
buildConflictNote(conflict),
|
|
386
|
-
`${conflict.relPath} (conflict)`
|
|
387
|
-
);
|
|
388
|
-
}
|
|
389
|
-
|
|
390
550
|
if (!changes.isEmpty()) {
|
|
391
551
|
await changes.writeAll(projectRoot);
|
|
392
552
|
}
|
|
393
553
|
|
|
554
|
+
const conflicts = results.conflicts.filter((c) => !c.collision);
|
|
555
|
+
const collisions = results.conflicts.filter((c) => c.collision);
|
|
556
|
+
|
|
394
557
|
p.log.success(
|
|
395
|
-
`${
|
|
396
|
-
|
|
397
|
-
|
|
558
|
+
`${results.updated} updated, ${results.merged.length} merged, ${results.absorbed.length} absorbed upstream, ` +
|
|
559
|
+
`${results.kept} kept-local, ${results.projectAdded.length} project-added, ` +
|
|
560
|
+
`${conflicts.length} conflict(s), ${collisions.length} collision(s), ${results.deleted} removed.`
|
|
398
561
|
);
|
|
562
|
+
const list = (title, items) => {
|
|
563
|
+
if (items.length > 0) p.log.info(`${title}:\n${items.map((x) => ` ${x}`).join('\n')}`);
|
|
564
|
+
};
|
|
565
|
+
list('Merged (both sides changed, no overlap)', results.merged);
|
|
566
|
+
list('Absorbed upstream (the new release already contains your change)', results.absorbed);
|
|
567
|
+
list('Project-added (kept, not part of toolcrib)', results.projectAdded);
|
|
568
|
+
const conflictLine = (c) => ` ${c.displayPath} [${c.kind}] note: ${c.notePath}`;
|
|
399
569
|
if (conflicts.length > 0) {
|
|
400
570
|
p.log.warn(
|
|
401
571
|
`${conflicts.length} conflict(s) — both local and upstream changed:\n` +
|
|
402
|
-
conflicts.map(
|
|
403
|
-
`\n\
|
|
572
|
+
conflicts.map(conflictLine).join('\n') +
|
|
573
|
+
`\n\nFiles marked [markers] are staged with conflict markers to resolve after apply; for the others your file is untouched. ` +
|
|
574
|
+
`Notes live in ./toolcrib-patches/${NOTES_DIR}/ and are never applied into ./toolcrib/.`
|
|
575
|
+
);
|
|
576
|
+
}
|
|
577
|
+
if (collisions.length > 0) {
|
|
578
|
+
p.log.warn(
|
|
579
|
+
`${collisions.length} collision(s) — you added a file at a path the new release now ships:\n` +
|
|
580
|
+
collisions.map(conflictLine).join('\n')
|
|
404
581
|
);
|
|
405
582
|
}
|
|
406
583
|
|
|
@@ -421,18 +598,3 @@ export async function mergeCommand(options) {
|
|
|
421
598
|
|
|
422
599
|
p.outro(changes.isEmpty() ? 'No changes to apply.' : 'Review ./toolcrib-patches/, then run `toolcrib apply`.');
|
|
423
600
|
}
|
|
424
|
-
|
|
425
|
-
function buildConflictNote(conflict) {
|
|
426
|
-
if (conflict.removedUpstream) {
|
|
427
|
-
return (
|
|
428
|
-
`This file was modified locally, but removed upstream in the new version.\n` +
|
|
429
|
-
`Automatic deletion was skipped — review manually or ask your AI assistant:\n` +
|
|
430
|
-
`"reconcile my local changes to ${conflict.relPath}, which was removed upstream"\n`
|
|
431
|
-
);
|
|
432
|
-
}
|
|
433
|
-
return (
|
|
434
|
-
`This file was modified locally AND changed upstream.\n` +
|
|
435
|
-
`Automatic merge was skipped — review manually or ask your AI assistant:\n` +
|
|
436
|
-
`"reconcile my local changes to ${conflict.relPath} with the upstream update"\n`
|
|
437
|
-
);
|
|
438
|
-
}
|
package/src/index.js
CHANGED
|
@@ -6,6 +6,7 @@ import { mergeCommand } from './commands/merge.js';
|
|
|
6
6
|
import { applyCommand } from './commands/apply.js';
|
|
7
7
|
import { doctorCommand } from './commands/doctor.js';
|
|
8
8
|
import { versionsCommand } from './commands/versions.js';
|
|
9
|
+
import { exportPatchesCommand } from './commands/exportPatches.js';
|
|
9
10
|
|
|
10
11
|
// Read at runtime instead of a JSON import assertion: `assert { type: 'json' }`
|
|
11
12
|
// vs `with { type: 'json' }` differs across the Node 18-22 range this CLI's
|
|
@@ -89,6 +90,16 @@ program
|
|
|
89
90
|
await doctorCommand(options).catch(fail);
|
|
90
91
|
});
|
|
91
92
|
|
|
93
|
+
program
|
|
94
|
+
.command('export-patches')
|
|
95
|
+
.description('Export your changes to vendored toolkit source as patches for upstream (read-only)')
|
|
96
|
+
.option('--out <dir>', 'output directory', './toolcrib-upstream')
|
|
97
|
+
.option('--post', 'post an existing export to the toolcrib Consumer Patches discussions')
|
|
98
|
+
.option('--yes', 'with --post: skip the confirmation prompt')
|
|
99
|
+
.action(async (options) => {
|
|
100
|
+
await exportPatchesCommand(options).catch(fail);
|
|
101
|
+
});
|
|
102
|
+
|
|
92
103
|
program
|
|
93
104
|
.command('versions')
|
|
94
105
|
.description('List available toolkit releases')
|