@hheei/pi-apply-patch 0.1.1 → 0.3.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.
@@ -1,4 +1,4 @@
1
- import { acquireMutationLock, errorMessage } from "@hheei/pi-ext-core";
1
+ import { errorMessage } from "@hheei/pi-ext-core";
2
2
  import { APPLY_PATCH_MAX_FILE_SIZE, createLocalPatchFs, FsTransportError, fileTooLarge, gcAgentPatchTemps, publishPreparedFile, sftpTimeoutMs, } from "./fs.js";
3
3
  import { runJsDiffUpdate } from "./jsdiff.js";
4
4
  import { findV4aPatchConflicts, operationTouchedPaths, parseV4aPatch, } from "./parser.js";
@@ -274,134 +274,126 @@ export async function applyPatchInWorkspace(options) {
274
274
  setStatus(index, "not_applied");
275
275
  notApplied.push(rejection(index, operationTouchedPaths(operation), "NotApplied"));
276
276
  };
277
- const release = await acquireMutationLock(options.lockKey ?? fs.scope, options.signal);
278
- try {
279
- await gcAgentPatchTemps(fs, patch.operations.flatMap((operation) => operationTouchedPaths(operation).map((path) => {
280
- const separator = path.lastIndexOf("/");
281
- return separator < 0 ? "." : path.slice(0, separator);
282
- })), options.signal);
283
- emit("parsed");
284
- for (const [index, operation] of patch.operations.entries()) {
285
- if (rejectedIndices.has(index))
286
- continue;
287
- if (halt !== undefined) {
288
- markNotApplied(index, operation);
289
- continue;
290
- }
291
- if (options.signal?.aborted) {
292
- if (applied.length === 0 && unconfirmed.length === 0)
293
- throw options.signal.reason ?? new Error("aborted");
294
- halt = "not_applied";
295
- markNotApplied(index, operation);
296
- continue;
277
+ await gcAgentPatchTemps(fs, patch.operations.flatMap((operation) => operationTouchedPaths(operation).map((path) => {
278
+ const separator = path.lastIndexOf("/");
279
+ return separator < 0 ? "." : path.slice(0, separator);
280
+ })), options.signal);
281
+ emit("parsed");
282
+ for (const [index, operation] of patch.operations.entries()) {
283
+ if (rejectedIndices.has(index))
284
+ continue;
285
+ if (halt !== undefined) {
286
+ markNotApplied(index, operation);
287
+ continue;
288
+ }
289
+ if (options.signal?.aborted) {
290
+ if (applied.length === 0 && unconfirmed.length === 0)
291
+ throw options.signal.reason ?? new Error("aborted");
292
+ halt = "not_applied";
293
+ markNotApplied(index, operation);
294
+ continue;
295
+ }
296
+ try {
297
+ if (operation.kind === "add") {
298
+ const outcome = await applyAdd(fs, index, operation, options.signal);
299
+ applied.push(outcome);
300
+ setStatus(index, "applied", outcome);
297
301
  }
298
- try {
299
- if (operation.kind === "add") {
300
- const outcome = await applyAdd(fs, index, operation, options.signal);
301
- applied.push(outcome);
302
- setStatus(index, "applied", outcome);
303
- }
304
- else if (operation.kind === "delete") {
305
- const deleted = await applyDelete(fs, index, operation, options.signal);
306
- applied.push(deleted.outcome);
307
- const current = progressOperations[index];
308
- if (current !== undefined) {
309
- progressOperations[index] = Object.freeze({
310
- ...current,
311
- ...plannedDelta(operation, deleted.before),
312
- status: "applied",
313
- });
314
- }
302
+ else if (operation.kind === "delete") {
303
+ const deleted = await applyDelete(fs, index, operation, options.signal);
304
+ applied.push(deleted.outcome);
305
+ const current = progressOperations[index];
306
+ if (current !== undefined) {
307
+ progressOperations[index] = Object.freeze({
308
+ ...current,
309
+ ...plannedDelta(operation, deleted.before),
310
+ status: "applied",
311
+ });
315
312
  }
316
- else {
317
- const published = await applyUpdate(fs, index, operation, options.policy, options.signal);
318
- if (published.mode === "exact")
319
- exactUpdateCount += 1;
320
- if (published.mode === "fuzzy")
321
- fuzzyUpdateCount += 1;
322
- applied.push(published.outcome);
323
- const partialReason = published.rejectedHunks.length === 0
324
- ? undefined
325
- : published.rejectedHunks[0]?.kind === "context_not_found"
326
- ? "context not found"
327
- : "exact context is ambiguous";
328
- if (published.rejectedHunks.length > 0)
329
- rejected.push(rejection(index, operationTouchedPaths(operation), "One or more update hunks failed", published.rejectedHunks));
330
- if (published.sourceError !== undefined) {
331
- if (published.sourceUnknown === true) {
332
- unconfirmed.push(rejection(index, [operation.path], published.sourceError));
333
- setStatus(index, "unconfirmed", published.outcome, operation, undefined, published.prepared);
334
- halt = "unconfirmed";
335
- }
336
- else {
337
- rejected.push(rejection(index, [operation.path], published.sourceError));
338
- setStatus(index, "partial", published.outcome, operation, partialReason ?? String(published.sourceError), published.prepared);
339
- }
313
+ }
314
+ else {
315
+ const published = await applyUpdate(fs, index, operation, options.policy, options.signal);
316
+ if (published.mode === "exact")
317
+ exactUpdateCount += 1;
318
+ if (published.mode === "fuzzy")
319
+ fuzzyUpdateCount += 1;
320
+ applied.push(published.outcome);
321
+ const partialReason = published.rejectedHunks.length === 0
322
+ ? undefined
323
+ : published.rejectedHunks[0]?.kind === "context_not_found"
324
+ ? "context not found"
325
+ : "exact context is ambiguous";
326
+ if (published.rejectedHunks.length > 0)
327
+ rejected.push(rejection(index, operationTouchedPaths(operation), "One or more update hunks failed", published.rejectedHunks));
328
+ if (published.sourceError !== undefined) {
329
+ if (published.sourceUnknown === true) {
330
+ unconfirmed.push(rejection(index, [operation.path], published.sourceError));
331
+ setStatus(index, "unconfirmed", published.outcome, operation, undefined, published.prepared);
332
+ halt = "unconfirmed";
340
333
  }
341
334
  else {
342
- setStatus(index, partialReason === undefined
343
- ? published.mode === "fuzzy"
344
- ? "fuzzy"
345
- : "applied"
346
- : "partial", published.outcome, operation, partialReason, published.prepared);
335
+ rejected.push(rejection(index, [operation.path], published.sourceError));
336
+ setStatus(index, "partial", published.outcome, operation, partialReason ?? String(published.sourceError), published.prepared);
347
337
  }
348
338
  }
349
- }
350
- catch (error) {
351
- if (options.signal?.aborted && applied.length === 0 && unconfirmed.length === 0)
352
- throw options.signal.reason ?? error;
353
- if (error instanceof FsTransportError) {
354
- const bucket = error.phase === "write" ? notApplied : unconfirmed;
355
- const status = error.phase === "write" ? "not_applied" : "unconfirmed";
356
- bucket.push(rejection(index, operationTouchedPaths(operation), error));
357
- setStatus(index, status);
358
- halt = status;
359
- }
360
- else if (error instanceof PatchUpdateError) {
361
- rejected.push(rejection(index, operationTouchedPaths(operation), error, error.diagnostics));
362
- rejectedIndices.add(index);
363
- setStatus(index, "rejected");
364
- }
365
- else if (options.signal?.aborted) {
366
- notApplied.push(rejection(index, operationTouchedPaths(operation), error));
367
- setStatus(index, "not_applied");
368
- halt = "not_applied";
369
- }
370
339
  else {
371
- rejected.push(rejection(index, operationTouchedPaths(operation), error));
372
- rejectedIndices.add(index);
373
- setStatus(index, "rejected");
340
+ setStatus(index, partialReason === undefined
341
+ ? published.mode === "fuzzy"
342
+ ? "fuzzy"
343
+ : "applied"
344
+ : "partial", published.outcome, operation, partialReason, published.prepared);
374
345
  }
375
346
  }
376
- emit("publishing");
377
- await new Promise((resolve) => setImmediate(resolve));
378
347
  }
379
- if (halt !== undefined) {
380
- for (const [index, operation] of patch.operations.entries()) {
381
- if (progressOperations[index]?.status !== "pending")
382
- continue;
383
- markNotApplied(index, operation);
348
+ catch (error) {
349
+ if (options.signal?.aborted && applied.length === 0 && unconfirmed.length === 0)
350
+ throw options.signal.reason ?? error;
351
+ if (error instanceof FsTransportError) {
352
+ const bucket = error.phase === "write" ? notApplied : unconfirmed;
353
+ const status = error.phase === "write" ? "not_applied" : "unconfirmed";
354
+ bucket.push(rejection(index, operationTouchedPaths(operation), error));
355
+ setStatus(index, status);
356
+ halt = status;
357
+ }
358
+ else if (error instanceof PatchUpdateError) {
359
+ rejected.push(rejection(index, operationTouchedPaths(operation), error, error.diagnostics));
360
+ rejectedIndices.add(index);
361
+ setStatus(index, "rejected");
362
+ }
363
+ else if (options.signal?.aborted) {
364
+ notApplied.push(rejection(index, operationTouchedPaths(operation), error));
365
+ setStatus(index, "not_applied");
366
+ halt = "not_applied";
367
+ }
368
+ else {
369
+ rejected.push(rejection(index, operationTouchedPaths(operation), error));
370
+ rejectedIndices.add(index);
371
+ setStatus(index, "rejected");
384
372
  }
385
373
  }
386
- emit("done");
387
- const totals = progressTotals(progressOperations);
388
- return Object.freeze({
389
- changedPaths: Object.freeze([
390
- ...new Set(applied.flatMap((operation) => [...operation.paths])),
391
- ]),
392
- addedLines: totals.addedLines,
393
- removedLines: totals.removedLines,
394
- operations: Object.freeze([...progressOperations]),
395
- operationCount: patch.operations.length,
396
- exactUpdateCount,
397
- fuzzyUpdateCount,
398
- applied: Object.freeze(applied),
399
- rejected: Object.freeze(rejected),
400
- unconfirmed: Object.freeze(unconfirmed),
401
- notApplied: Object.freeze(notApplied),
402
- });
374
+ emit("publishing");
375
+ await new Promise((resolve) => setImmediate(resolve));
403
376
  }
404
- finally {
405
- await release();
377
+ if (halt !== undefined) {
378
+ for (const [index, operation] of patch.operations.entries()) {
379
+ if (progressOperations[index]?.status !== "pending")
380
+ continue;
381
+ markNotApplied(index, operation);
382
+ }
406
383
  }
384
+ emit("done");
385
+ const totals = progressTotals(progressOperations);
386
+ return Object.freeze({
387
+ changedPaths: Object.freeze([...new Set(applied.flatMap((operation) => [...operation.paths]))]),
388
+ addedLines: totals.addedLines,
389
+ removedLines: totals.removedLines,
390
+ operations: Object.freeze([...progressOperations]),
391
+ operationCount: patch.operations.length,
392
+ exactUpdateCount,
393
+ fuzzyUpdateCount,
394
+ applied: Object.freeze(applied),
395
+ rejected: Object.freeze(rejected),
396
+ unconfirmed: Object.freeze(unconfirmed),
397
+ notApplied: Object.freeze(notApplied),
398
+ });
407
399
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hheei/pi-apply-patch",
3
- "version": "0.1.1",
3
+ "version": "0.3.0",
4
4
  "description": "V4A apply_patch editing tool with worker matching and per-path publishing.",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -36,7 +36,7 @@
36
36
  "access": "public"
37
37
  },
38
38
  "dependencies": {
39
- "@hheei/pi-ext-core": "^0.4.0",
39
+ "@hheei/pi-ext-core": "^0.6.0",
40
40
  "diff": "^9.0.0"
41
41
  },
42
42
  "peerDependencies": {