vcs-patch-deploy 1.2.0 → 1.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.
@@ -5,17 +5,21 @@
5
5
  const fs = require('fs')
6
6
  const path = require('path')
7
7
  const os = require('os')
8
+ const crypto = require('crypto')
8
9
  const fse = require('fs-extra')
9
10
  const { shouldExcludePath } = require('./exclude')
10
11
  const { getBackend } = require('./backends')
11
- const { readDeployMarker, writeDeployMarker } = require('./marker')
12
- const { registerVersion } = require('./version-registry')
12
+ const { readDeployMarker, writeDeployMarker, markerFileName } = require('./marker')
13
+ const { registerVersion, pruneWorktreeRecords } = require('./version-registry')
13
14
  const { sanitizeVersionLabel } = require('./file-filter')
14
15
  const { resolveSelectedFiles, filterFilesBySelection } = require('./file-filter')
15
- const { getSvnInfo, getSvnDiffSummary } = require('./svn')
16
- const { getGitInfo, getGitDiffSummary, resolveGitRef } = require('./git')
16
+ const { getSvnInfo, getSvnDiffSummary, getSvnContentMismatch, getSvnWorktreeDiffEntries, getSvnExecutableFiles } = require('./svn')
17
+ const { getGitInfo, getGitDiffSummary, resolveGitRef, getGitContentMismatch, getGitPrefix, getGitExecutableFiles } = require('./git')
17
18
  const { createZipFromDirectory } = require('./zip')
18
19
  const { buildDeployVersionLabel, archivePatchPackage } = require('./version-archive')
20
+ const { pruneOutDirectory } = require('./out-prune')
21
+ const { writeProtectFile } = require('./protect')
22
+ const { writeModesFile } = require('./modes')
19
23
 
20
24
  /**
21
25
  * @typedef {object} BuildResult
@@ -29,6 +33,7 @@ const { buildDeployVersionLabel, archivePatchPackage } = require('./version-arch
29
33
  * @property {number} [fileCount]
30
34
  * @property {number} [deletedCount]
31
35
  * @property {string[]} [warnings]
36
+ * @property {string[]} [prunedOut] 被清理的 deploy/out 旧产物
32
37
  * @property {'svn'|'git'} vcs
33
38
  * @property {string} fromLabel
34
39
  * @property {string} toLabel
@@ -74,6 +79,26 @@ function classifyDiffEntries (entries, sourceDir, backendId = 'php') {
74
79
  continue
75
80
  }
76
81
 
82
+ let stat
83
+ try {
84
+ stat = fs.lstatSync(src)
85
+ } catch {
86
+ warnings.push(`跳过(无法读取):${rel}`)
87
+ continue
88
+ }
89
+
90
+ // 服务器端拒绝符号链接;打包阶段提前跳过并提示
91
+ if (stat.isSymbolicLink()) {
92
+ warnings.push(`跳过符号链接:${rel}`)
93
+ continue
94
+ }
95
+
96
+ // SVN --summarize 会列出目录本身(属性变更、新增目录);目录内容由子文件条目体现,
97
+ // 直接复制目录会递归带入未版本控制的文件,因此跳过目录条目。
98
+ if (!stat.isFile()) {
99
+ continue
100
+ }
101
+
77
102
  addedOrModified.push(rel)
78
103
  }
79
104
 
@@ -84,33 +109,77 @@ function classifyDiffEntries (entries, sourceDir, backendId = 'php') {
84
109
  }
85
110
  }
86
111
 
112
+ /**
113
+ * 计算包内/服务器使用的版本标签
114
+ *
115
+ * 规则:
116
+ * - 完整包:--version,或 r467 / commit 短哈希
117
+ * - 子集包:追加 -partial-<hash>,并写 partial=1(服务器据此识别部分部署)
118
+ * - 工作副本包:wc-<时间戳>,带 --version 时保留版本前缀
119
+ *
120
+ * @param {object} options
121
+ * @param {'svn'|'git'} options.vcs
122
+ * @param {string} options.toLabel
123
+ * @param {string|null} [options.version]
124
+ * @param {string|null} [options.partialSignature]
125
+ * @param {string|null} [options.worktreeLabel]
126
+ */
127
+ function buildVersionLabels ({
128
+ vcs,
129
+ toLabel,
130
+ version = null,
131
+ partialSignature = null,
132
+ worktreeLabel = null
133
+ }) {
134
+ const versionLabel = version ? sanitizeVersionLabel(version) : null
135
+ const partialSuffix = partialSignature ? `-partial-${partialSignature}` : ''
136
+ // 工作副本包的目标不是某个 revision,base 仅来自 --version(可为空)
137
+ const baseVersionLabel = worktreeLabel
138
+ ? versionLabel
139
+ : buildDeployVersionLabel(vcs, toLabel, versionLabel)
140
+ const deployVersionLabel = worktreeLabel
141
+ ? `${versionLabel ? `${versionLabel}-` : ''}${worktreeLabel}${partialSuffix}`
142
+ : `${baseVersionLabel}${partialSuffix}`
143
+ const artifactVersionLabel = (versionLabel || partialSuffix) ? deployVersionLabel : null
144
+
145
+ return {
146
+ versionLabel,
147
+ partialSuffix,
148
+ baseVersionLabel,
149
+ deployVersionLabel,
150
+ artifactVersionLabel
151
+ }
152
+ }
153
+
87
154
  /**
88
155
  * 生成 patch 目录名与 zip 名后缀
89
156
  * @param {'svn'|'git'} vcs
90
157
  * @param {string} toLabel
91
- * @param {string} [version]
158
+ * @param {string} zipPrefix
159
+ * @param {string|null} [version]
160
+ * @param {string|null} [partialSuffix] 子集包唯一后缀(--only / --file-list)
161
+ * @param {string|null} [worktreeLabel] 工作副本包标识(如 wc-20261118093000)
92
162
  */
93
- function buildArtifactNames (vcs, toLabel, zipPrefix, version = null) {
163
+ function buildArtifactNames (vcs, toLabel, zipPrefix, version = null, partialSuffix = null, worktreeLabel = null) {
94
164
  const versionLabel = version ? sanitizeVersionLabel(version) : null
95
165
 
166
+ /** @type {string} */
167
+ let base
96
168
  if (versionLabel) {
97
- return {
98
- patchName: `patch-v${versionLabel}`,
99
- zipName: `${zipPrefix}-v${versionLabel}.zip`
100
- }
169
+ base = worktreeLabel ? `v${versionLabel}-${worktreeLabel}` : `v${versionLabel}`
170
+ } else if (worktreeLabel) {
171
+ base = worktreeLabel
172
+ } else if (vcs === 'svn') {
173
+ base = `r${toLabel}`
174
+ } else {
175
+ base = toLabel.slice(0, 12)
101
176
  }
102
177
 
103
- if (vcs === 'svn') {
104
- return {
105
- patchName: `patch-r${toLabel}`,
106
- zipName: `${zipPrefix}-r${toLabel}.zip`
107
- }
108
- }
178
+ const suffix = partialSuffix ? `-partial-${partialSuffix}` : ''
109
179
 
110
- const short = toLabel.slice(0, 12)
111
180
  return {
112
- patchName: `patch-${short}`,
113
- zipName: `${zipPrefix}-${short}.zip`
181
+ patchName: `patch-${base}${suffix}`,
182
+ zipName: `${zipPrefix}-${base}${suffix}.zip`
114
183
  }
115
184
  }
116
185
 
@@ -134,7 +203,14 @@ async function buildPatch (config) {
134
203
  onlyFiles,
135
204
  fileList,
136
205
  noArchive,
137
- keepVersions
206
+ keepVersions,
207
+ pruneOut,
208
+ keepOut,
209
+ archiveLink,
210
+ fromWorktree,
211
+ profile,
212
+ protectFiles,
213
+ diffWorktree
138
214
  } = config
139
215
 
140
216
  const backendModule = getBackend(backend)
@@ -156,10 +232,51 @@ async function buildPatch (config) {
156
232
  let toLabel
157
233
  /** @type {Array<{ action: string, relativePath: string, parseError?: string }>} */
158
234
  let diffEntries
235
+ /** git:sourceDir 相对仓库根的路径(sourceDir 不是仓库根时用于路径映射) */
236
+ let gitPrefix = ''
237
+ /** 工作副本包标识(仅 diffWorktree) */
238
+ const worktreeLabel = diffWorktree
239
+ ? `wc-${new Date().toISOString().replace(/[-:T]/g, '').slice(0, 14)}`
240
+ : null
241
+
242
+ const marker = readDeployMarker(deployDir, profile || null)
243
+ const markerHint = markerFileName(profile || null)
244
+
245
+ if (vcs === 'svn' && diffWorktree) {
246
+ if (markDeployed) {
247
+ throw new Error(
248
+ '工作副本模式(--worktree-diff)不能自动更新基准:线上内容并非某个 revision。\n' +
249
+ '请先提交代码,再用 --mark-deployed 指向对应 revision。'
250
+ )
251
+ }
252
+ if (config.toRef != null) {
253
+ throw new Error(
254
+ '--worktree-diff 与 --to 不能同时使用:工作副本模式的目标固定为 working-copy。\n' +
255
+ '若只想打包某个 revision 区间,请去掉 --worktree-diff。'
256
+ )
257
+ }
159
258
 
160
- const marker = readDeployMarker(deployDir)
259
+ let fromRev = config.fromRef != null ? Number(config.fromRef) : null
260
+ if (fromRev == null) {
261
+ if (marker && marker.vcs === 'svn') {
262
+ fromRev = parseInt(marker.value, 10)
263
+ } else {
264
+ throw new Error(`未找到 deploy/${markerHint},请指定 --from=<revision>`)
265
+ }
266
+ }
161
267
 
162
- if (vcs === 'svn') {
268
+ if (!Number.isFinite(fromRev)) {
269
+ throw new Error(`无效的 SVN revision:from=${fromRev},请检查 marker 或 --from`)
270
+ }
271
+
272
+ fromLabel = String(fromRev)
273
+ toLabel = 'working-copy'
274
+ diffEntries = getSvnWorktreeDiffEntries({
275
+ sourceDir,
276
+ fromRev,
277
+ shouldSkip: rel => shouldExcludePath(rel, [], backend).excluded
278
+ })
279
+ } else if (vcs === 'svn') {
163
280
  const svnInfo = getSvnInfo(sourceDir)
164
281
  const toRev = config.toRef != null ? Number(config.toRef) : svnInfo.revision
165
282
  let fromRev = config.fromRef != null ? Number(config.fromRef) : null
@@ -169,7 +286,7 @@ async function buildPatch (config) {
169
286
  fromRev = parseInt(marker.value, 10)
170
287
  } else {
171
288
  throw new Error(
172
- `未找到 deploy/.last-deploy-marker 或 .last-deploy-rev,请指定 --from=<revision>`
289
+ `未找到 deploy/${markerHint} 或 .last-deploy-rev,请指定 --from=<revision>`
173
290
  )
174
291
  }
175
292
  }
@@ -180,7 +297,7 @@ async function buildPatch (config) {
180
297
 
181
298
  if (fromRev >= toRev) {
182
299
  if (markDeployed && fromRev === toRev) {
183
- writeDeployMarker(deployDir, 'svn', String(toRev))
300
+ writeDeployMarker(deployDir, 'svn', String(toRev), profile || null)
184
301
  return {
185
302
  success: true,
186
303
  skipped: true,
@@ -204,6 +321,12 @@ async function buildPatch (config) {
204
321
  toLabel = String(toRev)
205
322
  diffEntries = getSvnDiffSummary({ sourceDir, fromRev, toRev })
206
323
  } else {
324
+ if (diffWorktree) {
325
+ throw new Error(
326
+ '--worktree-diff 目前仅支持 SVN 工作副本;Git 请先提交改动,再按 commit 打包(或用 --from-worktree 放宽一致性校验)'
327
+ )
328
+ }
329
+
207
330
  const gitInfo = getGitInfo(sourceDir)
208
331
  const toCommit = config.toRef != null
209
332
  ? resolveGitRef(sourceDir, String(config.toRef))
@@ -218,14 +341,14 @@ async function buildPatch (config) {
218
341
  fromCommit = resolveGitRef(sourceDir, marker.value)
219
342
  } else {
220
343
  throw new Error(
221
- `未找到 deploy/.last-deploy-marker(git:...),请指定 --from=<commit|tag>`
344
+ `未找到 deploy/${markerHint}(git:...),请指定 --from=<commit|tag>`
222
345
  )
223
346
  }
224
347
  }
225
348
 
226
349
  if (fromCommit === toCommit) {
227
350
  if (markDeployed) {
228
- writeDeployMarker(deployDir, 'git', toCommit)
351
+ writeDeployMarker(deployDir, 'git', toCommit, profile || null)
229
352
  return {
230
353
  success: true,
231
354
  skipped: true,
@@ -247,26 +370,44 @@ async function buildPatch (config) {
247
370
 
248
371
  fromLabel = fromCommit
249
372
  toLabel = toCommit
250
- diffEntries = getGitDiffSummary({ sourceDir, fromRef: fromCommit, toRef: toCommit })
373
+ gitPrefix = getGitPrefix(gitInfo.root, sourceDir)
374
+ diffEntries = getGitDiffSummary({
375
+ sourceDir,
376
+ fromRef: fromCommit,
377
+ toRef: toCommit,
378
+ prefix: gitPrefix
379
+ })
251
380
  }
252
381
 
253
382
  const { addedOrModified, deleted, warnings } = classifyDiffEntries(diffEntries, sourceDir, backend)
254
383
 
384
+ if (worktreeLabel) {
385
+ warnings.push(
386
+ '工作副本模式:包内容来自当前工作副本(含未提交改动),不会更新基准;' +
387
+ '上线成功后请先提交代码,再用 --mark-deployed 指向对应 revision'
388
+ )
389
+ if (!noArchive) {
390
+ warnings.push('工作副本包不写入 deploy/archive(避免挤掉版本归档);产物保留在 deploy/out')
391
+ }
392
+ }
393
+
255
394
  // 按 --only / --file-list 筛选本次打包文件(用于单独提取部署)
256
395
  const selectedFiles = resolveSelectedFiles(onlyFiles || [], fileList || null)
257
396
  let deployFiles = addedOrModified
258
397
  let deployDeleted = deleted
259
398
  if (selectedFiles.length > 0) {
260
399
  const filePick = filterFilesBySelection(addedOrModified, selectedFiles)
261
- if (filePick.missing.length > 0) {
262
- warnings.push(`--only 中以下文件不在本次增量内: ${filePick.missing.join(', ')}`)
263
- }
264
- deployFiles = filePick.matched
265
-
266
400
  const deletedPick = filterFilesBySelection(deleted, selectedFiles)
401
+ deployFiles = filePick.matched
267
402
  deployDeleted = deletedPick.matched
268
- if (deletedPick.missing.length > 0) {
269
- warnings.push(`--only 中以下删除项不在本次增量内: ${deletedPick.missing.join(', ')}`)
403
+
404
+ // 只有同时不在“增改”与“删除”列表中的选择才算未命中(删除项不应被误报)
405
+ const combinedPick = filterFilesBySelection(
406
+ [...addedOrModified, ...deleted],
407
+ selectedFiles
408
+ )
409
+ if (combinedPick.missing.length > 0) {
410
+ warnings.push(`--only 中以下文件不在本次增量内: ${combinedPick.missing.join(', ')}`)
270
411
  }
271
412
  }
272
413
 
@@ -282,12 +423,121 @@ async function buildPatch (config) {
282
423
  }
283
424
  }
284
425
 
285
- const { patchName, zipName } = buildArtifactNames(vcs, toLabel, zipPrefix, version)
426
+ // 打包是从工作区复制文件的:先确认工作区内容与目标版本一致,
427
+ // 否则 zip 内容会与 MANIFEST 声明的 from..to 范围不符。
428
+ // 工作副本模式(--worktree-diff)本身就以工作区为准,无需校验。
429
+ if (!fromWorktree && !diffWorktree && deployFiles.length > 0) {
430
+ const mismatched = vcs === 'git'
431
+ ? getGitContentMismatch({
432
+ sourceDir,
433
+ toRef: toLabel,
434
+ files: deployFiles,
435
+ prefix: gitPrefix
436
+ })
437
+ : getSvnContentMismatch({
438
+ sourceDir,
439
+ toRev: Number(toLabel),
440
+ files: deployFiles
441
+ })
442
+
443
+ if (mismatched.length > 0) {
444
+ const sample = mismatched.slice(0, 10).map(f => ` - ${f}`).join('\n')
445
+ const more = mismatched.length > 10 ? `\n … 另有 ${mismatched.length - 10} 个` : ''
446
+ const syncHint = vcs === 'git'
447
+ ? `git checkout ${toLabel}(或先把工作区同步到该 commit)`
448
+ : `svn update -r ${toLabel}`
449
+ throw new Error(
450
+ `工作区内容与目标版本不一致,打包会与 MANIFEST 声明不符:\n${sample}${more}\n\n` +
451
+ `请先执行 ${syncHint};若确实要用当前工作区内容打包,请加 --from-worktree 明确允许。`
452
+ )
453
+ }
454
+ }
455
+
456
+ const partialSignature = selectedFiles.length > 0
457
+ ? crypto
458
+ .createHash('sha256')
459
+ .update([...selectedFiles].sort().join('\n'))
460
+ .digest('hex')
461
+ .slice(0, 8)
462
+ : null
463
+
464
+ // 可执行位依赖 VCS 元数据(Windows 上 fs.stat 不体现 exec 位)
465
+ /** @type {string[]} */
466
+ let executableFiles = []
467
+ if (deployFiles.length > 0) {
468
+ try {
469
+ executableFiles = vcs === 'git'
470
+ ? getGitExecutableFiles({ sourceDir, files: deployFiles, prefix: gitPrefix })
471
+ : getSvnExecutableFiles({ sourceDir, files: deployFiles })
472
+ } catch (error) {
473
+ warnings.push(`无法读取可执行权限(这些文件将按 0644 打包): ${error.message}`)
474
+ }
475
+ }
476
+ // 非 Windows 上补一层文件系统权限判断:--worktree-diff 的未纳入版本控制的
477
+ // 脚本没有 svn:executable 属性,只能依靠实际执行位
478
+ if (process.platform !== 'win32') {
479
+ const execSet = new Set(executableFiles)
480
+ for (const rel of deployFiles) {
481
+ try {
482
+ if ((fs.statSync(path.join(sourceDir, rel)).mode & 0o111) !== 0) {
483
+ execSet.add(rel)
484
+ }
485
+ } catch {
486
+ // 无法读取的文件保持默认权限
487
+ }
488
+ }
489
+ executableFiles = [...execSet].sort()
490
+ }
491
+
492
+ /** @type {Record<string, number>} */
493
+ const fileModes = {}
494
+ for (const rel of executableFiles) {
495
+ fileModes[rel] = 0o755
496
+ }
497
+
498
+ const { patchName, zipName } = buildArtifactNames(
499
+ vcs,
500
+ toLabel,
501
+ zipPrefix,
502
+ version,
503
+ partialSignature,
504
+ worktreeLabel
505
+ )
286
506
  const patchDir = path.join(outDir, patchName)
287
507
  const zipPath = path.join(outDir, zipName)
288
508
  const stagingDir = `${patchDir}.staging-${Date.now()}`
289
- const versionLabel = version ? sanitizeVersionLabel(version) : null
290
- const deployVersionLabel = buildDeployVersionLabel(vcs, toLabel, versionLabel)
509
+
510
+ // 同版本重复构建:目标版本不同则提示(产物、archive、versions.json 会被覆盖)
511
+ if (fs.existsSync(patchDir)) {
512
+ /** @type {string|null} */
513
+ let previousTo = null
514
+ try {
515
+ const prev = fs.readFileSync(path.join(patchDir, 'VERSION.txt'), 'utf8')
516
+ const match = prev.match(/^to_(?:revision|commit)=(.+)$/m)
517
+ previousTo = match ? match[1].trim() : null
518
+ } catch {
519
+ // 无 VERSION.txt 的旧产物
520
+ }
521
+ if (previousTo === null || previousTo !== toLabel) {
522
+ warnings.push(
523
+ `将覆盖已存在的同版本产物 ${patchName}` +
524
+ `(原目标 ${previousTo ?? '未知'},本次 ${toLabel});如需保留请先改 --version 或备份 deploy/out`
525
+ )
526
+ }
527
+ }
528
+ const {
529
+ versionLabel,
530
+ partialSuffix,
531
+ baseVersionLabel,
532
+ deployVersionLabel,
533
+ artifactVersionLabel
534
+ } = buildVersionLabels({
535
+ vcs,
536
+ toLabel,
537
+ version,
538
+ partialSignature,
539
+ worktreeLabel
540
+ })
291
541
 
292
542
  await fse.emptyDir(stagingDir)
293
543
 
@@ -306,7 +556,7 @@ async function buildPatch (config) {
306
556
  '# PHP 后端增量部署清单',
307
557
  `backend=${backend}`,
308
558
  `vcs=${vcs}`,
309
- ...(versionLabel ? [`version=${versionLabel}`] : []),
559
+ `version=${deployVersionLabel}`,
310
560
  vcs === 'svn'
311
561
  ? `from_revision=${fromLabel}`
312
562
  : `from_commit=${fromLabel}`,
@@ -315,6 +565,10 @@ async function buildPatch (config) {
315
565
  : `to_commit=${toLabel}`,
316
566
  `generated_at=${new Date().toISOString().replace('T', ' ').slice(0, 19)}`,
317
567
  `generated_by=${os.userInfo().username}`,
568
+ `content_source=${fromWorktree || diffWorktree ? 'working-copy' : 'target-revision'}`,
569
+ ...(worktreeLabel ? ['worktree=1'] : []),
570
+ ...(profile ? [`profile=${profile}`] : []),
571
+ ...(protectFiles && protectFiles.length > 0 ? [`protect_count=${protectFiles.length}`] : []),
318
572
  '',
319
573
  '# files',
320
574
  ...deployFiles,
@@ -336,11 +590,18 @@ async function buildPatch (config) {
336
590
  `version=${deployVersionLabel}`,
337
591
  `vcs=${vcs}`,
338
592
  vcs === 'svn' ? `to_revision=${toLabel}` : `to_commit=${toLabel}`,
593
+ ...(worktreeLabel ? ['content_source=working-copy'] : []),
594
+ ...(partialSignature
595
+ ? ['partial=1', ...(baseVersionLabel ? [`source_version=${baseVersionLabel}`] : [])]
596
+ : []),
339
597
  `file_count=${deployFiles.length}`
340
598
  ].join('\n'),
341
599
  'utf8'
342
600
  )
343
601
 
602
+ // 受保护文件清单:服务器端对“线上已存在”的文件跳过覆盖
603
+ writeProtectFile(stagingDir, protectFiles || [])
604
+
344
605
  if (deployDeleted.length > 0) {
345
606
  fs.writeFileSync(
346
607
  path.join(stagingDir, 'MANIFEST-deleted.txt'),
@@ -349,6 +610,9 @@ async function buildPatch (config) {
349
610
  )
350
611
  }
351
612
 
613
+ // 可执行文件清单:服务器端据此 chmod(zip 在 Windows 上无法携带 exec 位)
614
+ writeModesFile(stagingDir, executableFiles)
615
+
352
616
  // 构建成功后再替换目标 patch 目录,避免中途失败清空旧包
353
617
  if (fs.existsSync(patchDir)) {
354
618
  await fse.remove(patchDir)
@@ -356,7 +620,7 @@ async function buildPatch (config) {
356
620
  await fse.move(stagingDir, patchDir)
357
621
 
358
622
  if (!noZip) {
359
- const zipResult = await createZipFromDirectory(patchDir, zipPath)
623
+ const zipResult = await createZipFromDirectory(patchDir, zipPath, { modes: fileModes })
360
624
  zipSize = zipResult.size
361
625
  }
362
626
  } catch (error) {
@@ -365,14 +629,22 @@ async function buildPatch (config) {
365
629
  }
366
630
 
367
631
  if (markDeployed) {
368
- writeDeployMarker(deployDir, vcs, toLabel)
632
+ writeDeployMarker(deployDir, vcs, toLabel, profile || null)
369
633
  }
370
634
 
635
+ // 子集包使用独立标识,避免覆盖同版本的完整包;工作副本包带 wc 标识,
636
+ // 且不写入同版本号记录,避免覆盖真正的发布包
637
+ // (deployVersionLabel / artifactVersionLabel 已在上方统一计算)
638
+
371
639
  // 注册版本号到 deploy/versions.json,便于后续 patch-extract 按版本提取
372
640
  if (versionLabel) {
373
641
  registerVersion(deployDir, {
374
- version: String(version),
375
- versionLabel,
642
+ version: worktreeLabel
643
+ ? `${String(version)}-${worktreeLabel}${partialSuffix}`
644
+ : partialSignature
645
+ ? `${String(version)}-partial-${partialSignature}`
646
+ : String(version),
647
+ versionLabel: artifactVersionLabel,
376
648
  vcs,
377
649
  fromLabel,
378
650
  toLabel,
@@ -380,31 +652,53 @@ async function buildPatch (config) {
380
652
  deleted: deployDeleted,
381
653
  patchDir,
382
654
  zipPath: noZip ? undefined : zipPath,
383
- partial: selectedFiles.length > 0,
655
+ partial: Boolean(partialSignature),
656
+ worktree: Boolean(worktreeLabel),
657
+ sourceVersion: partialSignature ? String(version) : undefined,
384
658
  createdAt: new Date().toISOString()
385
659
  })
660
+ if (worktreeLabel) {
661
+ // 工作副本包可随时重建,注册表只保留最近若干个,避免无限增长
662
+ pruneWorktreeRecords(deployDir, 5)
663
+ }
386
664
  }
387
665
 
388
666
  /** @type {string|undefined} */
389
667
  let archiveDir
390
- if (!noArchive) {
668
+ if (worktreeLabel) {
669
+ // 工作副本包可从工作区随时重建;不写入 archive(提示已在 warnings 中)
670
+ } else if (!noArchive) {
391
671
  const archived = await archivePatchPackage({
392
672
  deployDir,
393
- archiveLabel: deployVersionLabel,
673
+ archiveLabel: artifactVersionLabel || deployVersionLabel,
394
674
  patchDir,
395
675
  zipPath: noZip ? undefined : zipPath,
396
676
  vcs,
397
677
  fromLabel,
398
678
  toLabel,
399
- keepVersions: keepVersions || 10
679
+ partial: Boolean(partialSignature),
680
+ keepVersions: keepVersions || 10,
681
+ link: Boolean(archiveLink)
400
682
  })
401
683
  archiveDir = archived.archiveDir
402
684
  }
403
685
 
686
+ /** @type {string[]} */
687
+ let prunedOut = []
688
+ if (pruneOut) {
689
+ prunedOut = await pruneOutDirectory(outDir, {
690
+ keep: keepOut || keepVersions || 10,
691
+ zipPrefix,
692
+ keepPaths: [patchDir, noZip ? null : zipPath]
693
+ })
694
+ }
695
+
404
696
  return {
405
697
  success: true,
406
698
  skipped: false,
407
- message: `打包完成:${versionLabel ? `v${versionLabel}` : (vcs === 'svn' ? `r${fromLabel} -> r${toLabel}` : `${fromLabel.slice(0, 8)}..${toLabel.slice(0, 8)}`)}`,
699
+ message: worktreeLabel
700
+ ? `打包完成:工作副本(基准 r${fromLabel})`
701
+ : `打包完成:${versionLabel ? `v${versionLabel}` : (vcs === 'svn' ? `r${fromLabel} -> r${toLabel}` : `${fromLabel.slice(0, 8)}..${toLabel.slice(0, 8)}`)}`,
408
702
  patchDir,
409
703
  zipPath: noZip ? undefined : zipPath,
410
704
  zipSize,
@@ -414,17 +708,24 @@ async function buildPatch (config) {
414
708
  vcs,
415
709
  fromLabel,
416
710
  toLabel,
711
+ profile: profile || undefined,
712
+ protectCount: (protectFiles || []).length,
713
+ executableCount: executableFiles.length,
417
714
  version: versionLabel || undefined,
418
715
  deployVersionLabel,
716
+ artifactVersionLabel: artifactVersionLabel || undefined,
419
717
  archiveDir,
420
- markDeployedHint: markDeployed
718
+ prunedOut: prunedOut.length > 0 ? prunedOut : undefined,
719
+ markDeployedHint: markDeployed || worktreeLabel
421
720
  ? undefined
422
- : `上线成功后执行:patch-build --vcs=${vcs} --mark-deployed --from=${toLabel} --to=${toLabel}`
721
+ : `上线成功后执行:patch-build --vcs=${vcs}${profile ? ` --profile=${profile}` : ''} --mark-deployed --from=${toLabel} --to=${toLabel}` +
722
+ (fromWorktree ? '\n注意: 本次用工作区内容打包,请确认该 commit/revision 与线上内容一致' : '')
423
723
  }
424
724
  }
425
725
 
426
726
  module.exports = {
427
727
  buildPatch,
728
+ buildVersionLabels,
428
729
  classifyDiffEntries,
429
730
  buildArtifactNames
430
731
  }