toolcrib 0.3.0 → 0.4.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/package.json +1 -1
- package/src/commands/merge.js +56 -2
- package/src/lib/patchFallback.js +17 -1
- package/src/lib/patches.js +16 -5
package/package.json
CHANGED
package/src/commands/merge.js
CHANGED
|
@@ -3,7 +3,7 @@ import * as p from '@clack/prompts';
|
|
|
3
3
|
import { fetchRelease } from '../lib/release.js';
|
|
4
4
|
import { PendingChanges, normalize, joinPatchPath } from '../lib/patches.js';
|
|
5
5
|
import { mergeImportsField } from '../lib/deps.js';
|
|
6
|
-
import { readJsonIfExists, readLock, readTextIfExists, proposeLockUpdate } from '../lib/project.js';
|
|
6
|
+
import { fileExists, readJsonIfExists, readLock, readTextIfExists, proposeLockUpdate } from '../lib/project.js';
|
|
7
7
|
import { MANAGED_DOCS, KNOWN_TARGET_FILES, listManagedBlocks, upsertManagedBlock } from '../lib/managedDocs.js';
|
|
8
8
|
|
|
9
9
|
const TOOLKIT_DIR = './toolcrib';
|
|
@@ -155,6 +155,7 @@ export async function mergeCommand(options) {
|
|
|
155
155
|
const conflicts = [];
|
|
156
156
|
let updatedCount = 0;
|
|
157
157
|
let keptCount = 0;
|
|
158
|
+
let deletedCount = 0;
|
|
158
159
|
|
|
159
160
|
for (const relPath of newRelease.allFiles()) {
|
|
160
161
|
const targetPath = path.join(projectRoot, TOOLKIT_DIR, relPath);
|
|
@@ -180,6 +181,48 @@ export async function mergeCommand(options) {
|
|
|
180
181
|
}
|
|
181
182
|
}
|
|
182
183
|
|
|
184
|
+
// A file present in oldRelease but absent from newRelease was removed
|
|
185
|
+
// upstream — e.g. useAnimatedMount.ts when Drawer moved to Radix
|
|
186
|
+
// Presence in v0.10.0. Without this pass, a vendored copy of a file
|
|
187
|
+
// upstream no longer ships just sits there forever, unreferenced but
|
|
188
|
+
// never cleaned up, since the loop above only ever visits paths that
|
|
189
|
+
// still exist in the *new* release. classify(original, local, '')
|
|
190
|
+
// reuses the same four-way logic as the update loop, treating "removed
|
|
191
|
+
// upstream" as updated content of '': local unmodified from original ->
|
|
192
|
+
// safe to delete; local modified -> a real conflict (don't silently
|
|
193
|
+
// discard someone's customization just because upstream dropped the
|
|
194
|
+
// file), same as any other conflicting edit.
|
|
195
|
+
for (const relPath of oldRelease.allFiles()) {
|
|
196
|
+
if (newRelease.allFiles().includes(relPath)) continue;
|
|
197
|
+
|
|
198
|
+
const targetPath = path.join(projectRoot, TOOLKIT_DIR, relPath);
|
|
199
|
+
if (!fileExists(targetPath)) continue; // already absent locally — nothing to propose
|
|
200
|
+
const local = readTextIfExists(targetPath);
|
|
201
|
+
|
|
202
|
+
const original = oldRelease.readFile(relPath);
|
|
203
|
+
const status = classify(original, local, '');
|
|
204
|
+
|
|
205
|
+
switch (status) {
|
|
206
|
+
case 'safe-update':
|
|
207
|
+
changes.propose(joinPatchPath(TOOLKIT_DIR, relPath), local, '', relPath);
|
|
208
|
+
deletedCount++;
|
|
209
|
+
break;
|
|
210
|
+
case 'conflict':
|
|
211
|
+
conflicts.push({
|
|
212
|
+
relPath,
|
|
213
|
+
patchPath: joinPatchPath(TOOLKIT_DIR, relPath),
|
|
214
|
+
original,
|
|
215
|
+
local,
|
|
216
|
+
updated: '',
|
|
217
|
+
removedUpstream: true,
|
|
218
|
+
});
|
|
219
|
+
break;
|
|
220
|
+
// 'unchanged'/'keep-local' can't occur here: original is always
|
|
221
|
+
// non-empty (relPath came from oldRelease.allFiles()) and updated is
|
|
222
|
+
// always '', so upstreamChanged is unconditionally true.
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
183
226
|
const managedBlockCounts = await mergeManagedBlocks(projectRoot, oldRelease, newRelease, changes, conflicts);
|
|
184
227
|
updatedCount += managedBlockCounts.updatedCount;
|
|
185
228
|
keptCount += managedBlockCounts.keptCount;
|
|
@@ -230,7 +273,11 @@ export async function mergeCommand(options) {
|
|
|
230
273
|
await changes.writeAll(projectRoot);
|
|
231
274
|
}
|
|
232
275
|
|
|
233
|
-
p.log.success(
|
|
276
|
+
p.log.success(
|
|
277
|
+
`${updatedCount} file(s) updated cleanly, ${keptCount} local edit(s) preserved untouched` +
|
|
278
|
+
(deletedCount > 0 ? `, ${deletedCount} file(s) removed upstream and proposed for deletion` : '') +
|
|
279
|
+
'.'
|
|
280
|
+
);
|
|
234
281
|
if (conflicts.length > 0) {
|
|
235
282
|
p.log.warn(
|
|
236
283
|
`${conflicts.length} conflict(s) — both local and upstream changed:\n` +
|
|
@@ -243,6 +290,13 @@ export async function mergeCommand(options) {
|
|
|
243
290
|
}
|
|
244
291
|
|
|
245
292
|
function buildConflictNote(conflict) {
|
|
293
|
+
if (conflict.removedUpstream) {
|
|
294
|
+
return (
|
|
295
|
+
`This file was modified locally, but removed upstream in the new version.\n` +
|
|
296
|
+
`Automatic deletion was skipped — review manually or ask your AI assistant:\n` +
|
|
297
|
+
`"reconcile my local changes to ${conflict.relPath}, which was removed upstream"\n`
|
|
298
|
+
);
|
|
299
|
+
}
|
|
246
300
|
return (
|
|
247
301
|
`This file was modified locally AND changed upstream.\n` +
|
|
248
302
|
`Automatic merge was skipped — review manually or ask your AI assistant:\n` +
|
package/src/lib/patchFallback.js
CHANGED
|
@@ -13,7 +13,13 @@ export function applyPatchFallback(patchPath, projectRoot) {
|
|
|
13
13
|
const patchText = fs.readFileSync(patchPath, 'utf-8');
|
|
14
14
|
const [parsed] = parsePatch(patchText);
|
|
15
15
|
|
|
16
|
-
const
|
|
16
|
+
const isDeletedFile = parsed.newFileName === '/dev/null';
|
|
17
|
+
// A deletion's real target is its *source* name (oldFileName) — newFileName
|
|
18
|
+
// is /dev/null itself for a deletion, so falling through to the same
|
|
19
|
+
// newFileName-based path resolution used for create/update would target
|
|
20
|
+
// a literal "/dev/null" path inside the project instead of the file
|
|
21
|
+
// actually being removed.
|
|
22
|
+
const targetRelPath = (isDeletedFile ? parsed.oldFileName : parsed.newFileName).replace(/^[ab]\//, '');
|
|
17
23
|
const targetPath = path.join(projectRoot, targetRelPath);
|
|
18
24
|
|
|
19
25
|
// Defense in depth: every relPath this CLI itself writes into a patch
|
|
@@ -31,6 +37,16 @@ export function applyPatchFallback(patchPath, projectRoot) {
|
|
|
31
37
|
return { success: false, error: `Refusing to apply patch: target path "${targetRelPath}" resolves outside the project root.` };
|
|
32
38
|
}
|
|
33
39
|
|
|
40
|
+
if (isDeletedFile) {
|
|
41
|
+
if (!fs.existsSync(targetPath)) {
|
|
42
|
+
// Already gone (e.g. a re-run after a partial previous apply) —
|
|
43
|
+
// the end state this patch wants is already true, not a failure.
|
|
44
|
+
return { success: true };
|
|
45
|
+
}
|
|
46
|
+
fs.rmSync(targetPath);
|
|
47
|
+
return { success: true };
|
|
48
|
+
}
|
|
49
|
+
|
|
34
50
|
const isNewFile = parsed.oldFileName === '/dev/null';
|
|
35
51
|
const currentContent = isNewFile ? '' : fs.readFileSync(targetPath, 'utf-8');
|
|
36
52
|
|
package/src/lib/patches.js
CHANGED
|
@@ -62,7 +62,9 @@ export class PendingChanges {
|
|
|
62
62
|
* built, regardless of whether the caller already went through
|
|
63
63
|
* joinPatchPath() or constructed relPath some other way.
|
|
64
64
|
* @param {string} currentContent - '' if the file doesn't exist yet
|
|
65
|
-
* @param {string} proposedContent -
|
|
65
|
+
* @param {string} proposedContent - '' to propose deleting the file
|
|
66
|
+
* (only meaningful when currentContent is non-empty — '' vs '' is
|
|
67
|
+
* already a no-op via the check below)
|
|
66
68
|
* @param {string} [label] - display label for the patch header
|
|
67
69
|
*/
|
|
68
70
|
propose(relPath, currentContent, proposedContent, label) {
|
|
@@ -74,16 +76,25 @@ export class PendingChanges {
|
|
|
74
76
|
if (normalize(currentContent) === normalize(proposedContent)) return; // no-op, nothing to propose
|
|
75
77
|
|
|
76
78
|
const isNewFile = currentContent === '';
|
|
79
|
+
// Symmetric with isNewFile above: a deletion is just a patch whose
|
|
80
|
+
// *target* is /dev/null instead of its *source* — git apply (confirmed
|
|
81
|
+
// directly against a real repo, not assumed) recognizes this from a
|
|
82
|
+
// plain unified diff exactly the same way it recognizes a new file from
|
|
83
|
+
// an empty source, no git-extended `diff --git`/`deleted file mode`
|
|
84
|
+
// headers required. The `!isNewFile` guard only rules out the
|
|
85
|
+
// '' -> '' case, already caught by the no-op check above; kept for
|
|
86
|
+
// clarity that a file can't be simultaneously new and deleted.
|
|
87
|
+
const isDeletedFile = proposedContent === '' && !isNewFile;
|
|
77
88
|
const patch = createTwoFilesPatch(
|
|
78
89
|
isNewFile ? '/dev/null' : `a/${relPath}`,
|
|
79
|
-
`b/${relPath}`,
|
|
90
|
+
isDeletedFile ? '/dev/null' : `b/${relPath}`,
|
|
80
91
|
currentContent,
|
|
81
92
|
proposedContent,
|
|
82
93
|
isNewFile ? undefined : `${label} (current)`,
|
|
83
|
-
isNewFile ? `${label} (new file)` : `${label} (proposed)`
|
|
94
|
+
isDeletedFile ? undefined : isNewFile ? `${label} (new file)` : `${label} (proposed)`
|
|
84
95
|
);
|
|
85
96
|
|
|
86
|
-
this.entries.push({ relPath, patch, isNewFile });
|
|
97
|
+
this.entries.push({ relPath, patch, isNewFile, isDeletedFile });
|
|
87
98
|
}
|
|
88
99
|
|
|
89
100
|
isEmpty() {
|
|
@@ -96,7 +107,7 @@ export class PendingChanges {
|
|
|
96
107
|
|
|
97
108
|
summarize() {
|
|
98
109
|
return this.entries
|
|
99
|
-
.map((e) => ` ${e.isNewFile ? '+ (new) ' : '~ '}${e.relPath}`)
|
|
110
|
+
.map((e) => ` ${e.isDeletedFile ? '- (deleted) ' : e.isNewFile ? '+ (new) ' : '~ '}${e.relPath}`)
|
|
100
111
|
.join('\n');
|
|
101
112
|
}
|
|
102
113
|
|