@git.zone/cli 6.16.3 → 6.17.1

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,6 +1,12 @@
1
1
  import * as plugins from "./mod.plugins.js";
2
2
  import type { IResolvedReleaseWorkflow } from "../helpers.workflow.js";
3
+ import { logger } from "../gitzone.logging.js";
3
4
  import { readPendingChangelog } from "../helpers.changelog.js";
5
+ import {
6
+ detectProjectType,
7
+ projectTypeHasPackageJson,
8
+ type ProjectType,
9
+ } from "../mod_commit/mod.helpers.js";
4
10
  import { InterProcessLock } from "../mod_services/classes.interprocesslock.js";
5
11
  import { ReleaseJournalStore } from "./classes.releasejournal.js";
6
12
  import {
@@ -32,12 +38,12 @@ export interface IReleasePreparationRecovery {
32
38
  dockerRequest?: IReleaseQualificationRequest;
33
39
  }
34
40
 
35
- const git = async (
36
- options: IReleasePreparationRecovery,
41
+ const releaseGit = async (
42
+ smartshell: plugins.smartshell.Smartshell,
37
43
  args: string[],
38
- cwd = options.branch.sourceCwd,
44
+ cwd: string,
39
45
  ) => {
40
- const result = await options.smartshell.execSpawn("git", args, {
46
+ const result = await smartshell.execSpawn("git", args, {
41
47
  cwd,
42
48
  env: releaseGitEnv,
43
49
  silent: true,
@@ -45,10 +51,16 @@ const git = async (
45
51
  timeoutKillGraceMs: 5_000,
46
52
  });
47
53
  if (result.exitCode !== 0)
48
- throw new Error(`Release recovery Git validation failed: ${args[0]}.`);
54
+ throw new Error(`Release Git validation failed: ${args[0]}.`);
49
55
  return result.stdout.trim();
50
56
  };
51
57
 
58
+ const git = async (
59
+ options: IReleasePreparationRecovery,
60
+ args: string[],
61
+ cwd = options.branch.sourceCwd,
62
+ ) => releaseGit(options.smartshell, args, cwd);
63
+
52
64
  export const assertPackageReleaseRecovery = async (
53
65
  options: IReleasePreparationRecovery,
54
66
  ): Promise<string> => {
@@ -211,6 +223,211 @@ const bindPreparation = async (
211
223
  }
212
224
  };
213
225
 
226
+ const releaseLockfileName = "pnpm-lock.yaml";
227
+
228
+ /** The commit the release commit is built on top of, and its clean worktree. */
229
+ export interface IReleaseSourceCommit {
230
+ smartshell: plugins.smartshell.Smartshell;
231
+ /** Clean source worktree the release is prepared from. */
232
+ cwd: string;
233
+ /** Commit the release commit is built on top of. */
234
+ sourceOid: string;
235
+ }
236
+
237
+ const commitCarriesBlob = async (
238
+ options: IReleaseSourceCommit,
239
+ fileArg: string,
240
+ ): Promise<boolean> => {
241
+ const result = await options.smartshell.execSpawn(
242
+ "git",
243
+ ["cat-file", "-t", `${options.sourceOid}:./${fileArg}`],
244
+ {
245
+ cwd: options.cwd,
246
+ env: releaseGitEnv,
247
+ silent: true,
248
+ timeout: 60_000,
249
+ timeoutKillGraceMs: 5_000,
250
+ },
251
+ );
252
+ return result.exitCode === 0 && result.stdout.trim() === "blob";
253
+ };
254
+
255
+ /**
256
+ * The release checkout holds the release commit, so what it needs is decided by
257
+ * what that commit tracks rather than by what the working folder happens to
258
+ * contain: an ignored package.json or deno.json is not part of the release, and
259
+ * `detectProjectType` would still report it. For every tracked manifest both
260
+ * answers agree, because a release requires a clean worktree.
261
+ */
262
+ export const detectReleaseCommitProjectType = async (
263
+ options: IReleaseSourceCommit,
264
+ ): Promise<ProjectType> => {
265
+ const hasPackageJson = await commitCarriesBlob(options, "package.json");
266
+ const hasDenoJson = await commitCarriesBlob(options, "deno.json");
267
+ if (hasPackageJson && hasDenoJson) return "both";
268
+ if (hasPackageJson) return "npm";
269
+ if (hasDenoJson) return "deno";
270
+ throw new Error(
271
+ `Commit ${options.sourceOid} tracks neither package.json nor deno.json, so it carries no release version file.`,
272
+ );
273
+ };
274
+
275
+ /**
276
+ * The isolated release checkout of a project with a package.json installs with
277
+ * `pnpm install --frozen-lockfile`, so a lockfile that the release commit does
278
+ * not carry, or that no longer matches its manifest, would fail a release that
279
+ * already created its commit and tag. Preflight proves both before anything is
280
+ * created. A Deno-only commit installs nothing, so it is asked for nothing.
281
+ *
282
+ * The release commit only adds the version bump, the baked commit info, and the
283
+ * changelog section on top of the source commit, and a version change never
284
+ * changes a dependency specifier, so what installs here installs there.
285
+ */
286
+ export const assertReleaseCheckoutInstallable = async (
287
+ options: IReleaseSourceCommit,
288
+ ): Promise<void> => {
289
+ const projectType = await detectReleaseCommitProjectType(options);
290
+ if (!projectTypeHasPackageJson(projectType)) {
291
+ logger.log(
292
+ "info",
293
+ "The release commit carries no package.json, so its checkout runs the configured commands against the committed tree without installing npm dependencies",
294
+ );
295
+ return;
296
+ }
297
+ logger.log(
298
+ "info",
299
+ "Verifying the committed lockfile installs the release checkout",
300
+ );
301
+ const tracked = await commitCarriesBlob(options, releaseLockfileName);
302
+ if (!tracked) {
303
+ throw new Error(
304
+ `The release builds and packs a checkout of its release commit and installs that checkout with \`pnpm install --frozen-lockfile\`, but commit ${options.sourceOid} carries no ${releaseLockfileName}. Commit the lockfile — it must not be ignored — and release again.`,
305
+ );
306
+ }
307
+ // `--frozen-lockfile --lockfile-only` is pnpm's no-install, no-write form of
308
+ // this exact check: an up-to-date lockfile exits 0 without touching the
309
+ // lockfile or node_modules, a stale one exits 1 with ERR_PNPM_OUTDATED_LOCKFILE
310
+ // before resolving anything. `--dry-run` cannot replace it: it prints the
311
+ // difference and still exits 0.
312
+ const frozen = await options.smartshell.execSpawn(
313
+ "pnpm",
314
+ ["install", "--frozen-lockfile", "--lockfile-only"],
315
+ {
316
+ cwd: options.cwd,
317
+ silent: true,
318
+ timeout: 5 * 60_000,
319
+ timeoutKillGraceMs: 5_000,
320
+ },
321
+ );
322
+ if (frozen.exitCode !== 0 || frozen.timedOut) {
323
+ throw new Error(
324
+ `\`pnpm install --frozen-lockfile --lockfile-only\` rejects the committed ${releaseLockfileName}, so the release checkout could not install it. Run \`pnpm install\`, commit the updated lockfile, and release again.\n${frozen.combinedOutput.trim()}`,
325
+ );
326
+ }
327
+ };
328
+
329
+ export interface IIsolatedReleaseCheckout {
330
+ /** Disposable detached worktree that holds exactly the release commit. */
331
+ path: string;
332
+ /** Re-assert that the checkout still is that exact commit, unmodified. */
333
+ assertExact: () => Promise<void>;
334
+ }
335
+
336
+ export interface IIsolatedReleaseCheckoutOptions {
337
+ smartshell: plugins.smartshell.Smartshell;
338
+ /** Repository that owns the release commit; the worktree registers there. */
339
+ repositoryCwd: string;
340
+ /** Exact release commit the checkout must contain. */
341
+ mainOid: string;
342
+ /** Configured test command, when the checkout must run tests. */
343
+ testCommand?: string;
344
+ /** Configured build command, when the checkout must build. */
345
+ buildCommand?: string;
346
+ }
347
+
348
+ /**
349
+ * A release publishes what its release commit contains, so the release build
350
+ * and every artifact are produced in a disposable detached checkout of that
351
+ * commit instead of the user's working folder. Untracked files, ignored runtime
352
+ * data, and local project secrets are not part of it, so they can neither reach
353
+ * an artifact nor break packing. The checkout is removed on success and on
354
+ * failure.
355
+ */
356
+ export const withIsolatedReleaseCheckout = async <T>(
357
+ options: IIsolatedReleaseCheckoutOptions,
358
+ use: (checkout: IIsolatedReleaseCheckout) => Promise<T>,
359
+ ): Promise<T> => {
360
+ const root = await plugins.fs.mkdtemp(
361
+ plugins.path.join(plugins.os.tmpdir(), "gitzone-release-source-"),
362
+ );
363
+ const checkout = plugins.path.join(root, "source");
364
+ const assertExact = async () => {
365
+ if (
366
+ (await releaseGit(
367
+ options.smartshell,
368
+ ["rev-parse", "HEAD"],
369
+ checkout,
370
+ )) !== options.mainOid ||
371
+ (await releaseGit(
372
+ options.smartshell,
373
+ ["status", "--porcelain", "--untracked-files=all"],
374
+ checkout,
375
+ ))
376
+ ) {
377
+ throw new Error(
378
+ "The release checkout no longer holds the exact release commit. Aborting release.",
379
+ );
380
+ }
381
+ };
382
+ let added = false;
383
+ try {
384
+ console.log(
385
+ `\nPreparing the release checkout of ${options.mainOid} at ${checkout}`,
386
+ );
387
+ await releaseGit(
388
+ options.smartshell,
389
+ ["worktree", "add", "--detach", checkout, options.mainOid],
390
+ options.repositoryCwd,
391
+ );
392
+ added = true;
393
+ // The checkout is the release commit, so its own tree decides whether npm
394
+ // dependencies are installed. A Deno-only commit carries no package.json
395
+ // and no pnpm lockfile: its configured commands run against the committed
396
+ // tree, and pinning dependencies is the project's deno.lock or import map.
397
+ if (projectTypeHasPackageJson(await detectProjectType(checkout))) {
398
+ const install = await options.smartshell.execSpawn(
399
+ "pnpm",
400
+ ["install", "--frozen-lockfile"],
401
+ {
402
+ cwd: checkout,
403
+ timeout: 10 * 60_000,
404
+ timeoutKillGraceMs: 5_000,
405
+ },
406
+ );
407
+ if (install.exitCode !== 0)
408
+ throw new Error("Release checkout dependency installation failed.");
409
+ }
410
+ if (options.testCommand)
411
+ await run(options.smartshell, checkout, options.testCommand, "tests");
412
+ if (options.buildCommand)
413
+ await run(options.smartshell, checkout, options.buildCommand, "build");
414
+ await assertExact();
415
+ const result = await use({ path: checkout, assertExact });
416
+ await assertExact();
417
+ return result;
418
+ } finally {
419
+ // This path is a uniquely owned disposable checkout, never the user's
420
+ // source tree or runtime data. Keep registration evidence if cleanup fails.
421
+ if (added)
422
+ await releaseGit(
423
+ options.smartshell,
424
+ ["worktree", "remove", "--force", checkout],
425
+ options.repositoryCwd,
426
+ );
427
+ await plugins.fs.rm(root, { recursive: true, force: true });
428
+ }
429
+ };
430
+
214
431
  /** Build and seal one unpublished, already tagged package without changing refs. */
215
432
  export const withReleasePreparationRecovery = async <T>(
216
433
  options: IReleasePreparationRecovery,
@@ -243,76 +460,36 @@ export const withReleasePreparationRecovery = async <T>(
243
460
  `${directory}.preparation.json`,
244
461
  `${canonicalReleaseJson(preparationBinding(options))}\n`,
245
462
  );
246
- const root = await plugins.fs.mkdtemp(
247
- plugins.path.join(plugins.os.tmpdir(), "gitzone-release-prepare-"),
463
+ return withIsolatedReleaseCheckout(
464
+ {
465
+ smartshell: options.smartshell,
466
+ repositoryCwd: options.branch.sourceCwd,
467
+ mainOid: options.refs.mainOid,
468
+ // Recovery has no preflight run behind it, so it runs the configured
469
+ // checks itself.
470
+ ...(options.workflow.runTests
471
+ ? { testCommand: options.workflow.testCommand }
472
+ : {}),
473
+ ...(options.workflow.runBuild
474
+ ? { buildCommand: options.workflow.buildCommand }
475
+ : {}),
476
+ },
477
+ async (checkout) => {
478
+ await ensureState();
479
+ return prepare(checkout.path, checkout.assertExact);
480
+ },
248
481
  );
249
- const checkout = plugins.path.join(root, "source");
250
- let added = false;
251
- try {
252
- await git(options, [
253
- "worktree",
254
- "add",
255
- "--detach",
256
- checkout,
257
- options.refs.mainOid,
258
- ]);
259
- added = true;
260
- const install = await options.smartshell.execSpawn(
261
- "pnpm",
262
- ["install", "--frozen-lockfile"],
263
- {
264
- cwd: checkout,
265
- timeout: 10 * 60_000,
266
- timeoutKillGraceMs: 5_000,
267
- },
268
- );
269
- if (install.exitCode !== 0)
270
- throw new Error("Release recovery dependency installation failed.");
271
- if (options.workflow.runTests)
272
- await run(options, checkout, options.workflow.testCommand, "tests");
273
- if (options.workflow.runBuild)
274
- await run(options, checkout, options.workflow.buildCommand, "build");
275
- await assertCheckout(options, checkout);
276
- await ensureState();
277
- const result = await prepare(checkout, () =>
278
- assertCheckout(options, checkout),
279
- );
280
- await assertCheckout(options, checkout);
281
- return result;
282
- } finally {
283
- // This path is a uniquely owned disposable checkout, never the user's
284
- // source tree or runtime data. Keep registration evidence if cleanup fails.
285
- if (added)
286
- await git(options, ["worktree", "remove", "--force", checkout]);
287
- await plugins.fs.rm(root, { recursive: true, force: true });
288
- }
289
482
  });
290
483
  };
291
484
 
292
485
  const run = async (
293
- options: IReleasePreparationRecovery,
486
+ smartshell: plugins.smartshell.Smartshell,
294
487
  cwd: string,
295
488
  command: string,
296
489
  phase: string,
297
490
  ) => {
298
- const result = await options.smartshell.exec(command, { cwd });
491
+ console.log(`\nRunning the release ${phase} in the release checkout`);
492
+ const result = await smartshell.exec(command, { cwd });
299
493
  if (result.exitCode !== 0)
300
- throw new Error(`Release recovery ${phase} failed.`);
301
- };
302
-
303
- const assertCheckout = async (
304
- options: IReleasePreparationRecovery,
305
- checkout: string,
306
- ) => {
307
- if (
308
- (await git(options, ["rev-parse", "HEAD"], checkout)) !==
309
- options.refs.mainOid ||
310
- (await git(
311
- options,
312
- ["status", "--porcelain", "--untracked-files=all"],
313
- checkout,
314
- ))
315
- ) {
316
- throw new Error("Release recovery build changed its exact tracked source.");
317
- }
494
+ throw new Error(`Release ${phase} failed in the release checkout.`);
318
495
  };
@@ -8,6 +8,7 @@ import {
8
8
  readPendingChangelog,
9
9
  } from "../helpers.changelog.js";
10
10
  import {
11
+ releaseNeedsIsolatedCheckout,
11
12
  resolveReleaseDockerConfiguration,
12
13
  resolveReleaseWorkflow,
13
14
  resolveReleaseResumeConfiguration,
@@ -63,6 +64,8 @@ import { runTstestCleanup } from "../helpers.tstest.js";
63
64
  import {
64
65
  assertPackageReleaseRecovery,
65
66
  assertRecoveryUnpublished,
67
+ assertReleaseCheckoutInstallable,
68
+ withIsolatedReleaseCheckout,
66
69
  withReleasePreparationRecovery,
67
70
  } from "./helpers.releasepreparation.js";
68
71
  import {
@@ -153,12 +156,9 @@ const runInternal = async (argvArg: any): Promise<void> => {
153
156
  plugins.path.join(branchContext.sourceCwd, workflow.changelogFile),
154
157
  workflow.changelogPendingSection,
155
158
  );
156
- const projectType = await commitHelpers.detectProjectType(
157
- branchContext.sourceCwd,
158
- );
159
159
  const versionResolution = resolveReleaseVersion({
160
160
  currentVersion: await commitHelpers.readCurrentVersion(
161
- projectType,
161
+ workflow.projectType,
162
162
  branchContext.sourceCwd,
163
163
  ),
164
164
  pendingChangelog: pending.block,
@@ -219,6 +219,17 @@ const runInternal = async (argvArg: any): Promise<void> => {
219
219
  ),
220
220
  );
221
221
  }
222
+ // A release that builds or packs does so in a checkout of its release commit.
223
+ // When that commit carries a package.json, the checkout installs from the
224
+ // committed lockfile: prove that install can succeed while nothing has been
225
+ // created yet.
226
+ if (releaseNeedsIsolatedCheckout(workflow)) {
227
+ await assertReleaseCheckoutInstallable({
228
+ smartshell: smartshellInstance,
229
+ cwd: branchContext.sourceCwd,
230
+ sourceOid: branchContext.sourceOid,
231
+ });
232
+ }
222
233
 
223
234
  if (workflow.confirmation === "prompt") {
224
235
  if (!mode.interactive) {
@@ -265,7 +276,7 @@ const runInternal = async (argvArg: any): Promise<void> => {
265
276
  expectedRemoteMainOid = integration.expectedRemoteMainOid;
266
277
  }
267
278
 
268
- await runVersionStep(projectType, versionResolution, releaseCwd);
279
+ await runVersionStep(workflow.projectType, versionResolution, releaseCwd);
269
280
  await runChangelogStep(workflow, newVersion, releaseCwd);
270
281
  const releaseCommitOid = await runReleaseCommitStep(
271
282
  smartshellInstance,
@@ -285,7 +296,11 @@ const runInternal = async (argvArg: any): Promise<void> => {
285
296
  releaseCommitOid,
286
297
  );
287
298
 
288
- if (workflow.runBuild) {
299
+ // The release build belongs to the release commit and runs in its isolated
300
+ // checkout. Docker qualification is bound to the project directory by the
301
+ // tSDocker protocol, so an active Docker target additionally builds the
302
+ // release worktree that qualification reads.
303
+ if (workflow.runBuild && workflow.targets.includes("docker")) {
289
304
  await runCommandStep(
290
305
  smartshellInstance,
291
306
  releaseCwd,
@@ -329,7 +344,7 @@ const runInternal = async (argvArg: any): Promise<void> => {
329
344
  releaseRefs.mainOid,
330
345
  )
331
346
  : undefined;
332
- const journalContext = await createReleaseJournal({
347
+ const journalOptions = {
333
348
  pnpmCapability,
334
349
  smartshell: smartshellInstance,
335
350
  workflow,
@@ -341,8 +356,27 @@ const runInternal = async (argvArg: any): Promise<void> => {
341
356
  refs: releaseRefs,
342
357
  dockerRequest,
343
358
  tsdocker,
344
- ensurePublicationState,
345
- });
359
+ };
360
+ const journalContext = releaseNeedsIsolatedCheckout(workflow)
361
+ ? await withIsolatedReleaseCheckout(
362
+ {
363
+ smartshell: smartshellInstance,
364
+ repositoryCwd: releaseCwd,
365
+ mainOid: releaseRefs.mainOid,
366
+ // Preflight already ran the configured tests on this exact source.
367
+ ...(workflow.runBuild ? { buildCommand: workflow.buildCommand } : {}),
368
+ },
369
+ async (checkout) =>
370
+ createReleaseJournal({
371
+ ...journalOptions,
372
+ npmSourceCwd: checkout.path,
373
+ ensurePublicationState: async () => {
374
+ await checkout.assertExact();
375
+ await ensurePublicationState();
376
+ },
377
+ }),
378
+ )
379
+ : await createReleaseJournal({ ...journalOptions, ensurePublicationState });
346
380
  const finalJournal = await executeReleasePublication({
347
381
  pnpmCapability,
348
382
  smartshell: smartshellInstance,
@@ -487,7 +521,10 @@ interface ICreateReleaseJournalOptions {
487
521
  smartshell: plugins.smartshell.Smartshell;
488
522
  workflow: IResolvedReleaseWorkflow;
489
523
  releaseCwd: string;
490
- /** Exact detached checkout used only while recovering pre-journal packaging. */
524
+ /**
525
+ * Exact disposable checkout of the release commit that every build output,
526
+ * npm tarball, and frozen Gitea asset is taken from.
527
+ */
491
528
  npmSourceCwd?: string;
492
529
  releasePushUrl?: string;
493
530
  expectedRemoteMainOid?: string;