vcs-patch-deploy 1.1.1 → 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.
- package/CHANGELOG.md +57 -2
- package/README.md +181 -8
- package/assets/php/apply-update.php +1207 -67
- package/bin/patch-build.js +74 -13
- package/bin/patch-extract.js +24 -8
- package/env.example +30 -0
- package/lib/backends/php.js +5 -0
- package/lib/build-patch.js +382 -58
- package/lib/config.js +101 -14
- package/lib/extract-patch.js +100 -17
- package/lib/file-filter.js +20 -0
- package/lib/git.js +161 -22
- package/lib/index.js +33 -4
- package/lib/manifest.js +65 -2
- package/lib/marker.js +79 -14
- package/lib/modes.js +72 -0
- package/lib/out-prune.js +121 -0
- package/lib/protect.js +124 -0
- package/lib/svn.js +409 -12
- package/lib/version-archive.js +238 -0
- package/lib/version-registry.js +87 -8
- package/lib/zip.js +126 -8
- package/package.json +5 -2
package/lib/build-patch.js
CHANGED
|
@@ -5,16 +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')
|
|
19
|
+
const { buildDeployVersionLabel, archivePatchPackage } = require('./version-archive')
|
|
20
|
+
const { pruneOutDirectory } = require('./out-prune')
|
|
21
|
+
const { writeProtectFile } = require('./protect')
|
|
22
|
+
const { writeModesFile } = require('./modes')
|
|
18
23
|
|
|
19
24
|
/**
|
|
20
25
|
* @typedef {object} BuildResult
|
|
@@ -23,9 +28,12 @@ const { createZipFromDirectory } = require('./zip')
|
|
|
23
28
|
* @property {string} message
|
|
24
29
|
* @property {string} [patchDir]
|
|
25
30
|
* @property {string} [zipPath]
|
|
31
|
+
* @property {string} [archiveDir]
|
|
32
|
+
* @property {string} [deployVersionLabel]
|
|
26
33
|
* @property {number} [fileCount]
|
|
27
34
|
* @property {number} [deletedCount]
|
|
28
35
|
* @property {string[]} [warnings]
|
|
36
|
+
* @property {string[]} [prunedOut] 被清理的 deploy/out 旧产物
|
|
29
37
|
* @property {'svn'|'git'} vcs
|
|
30
38
|
* @property {string} fromLabel
|
|
31
39
|
* @property {string} toLabel
|
|
@@ -71,6 +79,26 @@ function classifyDiffEntries (entries, sourceDir, backendId = 'php') {
|
|
|
71
79
|
continue
|
|
72
80
|
}
|
|
73
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
|
+
|
|
74
102
|
addedOrModified.push(rel)
|
|
75
103
|
}
|
|
76
104
|
|
|
@@ -81,33 +109,77 @@ function classifyDiffEntries (entries, sourceDir, backendId = 'php') {
|
|
|
81
109
|
}
|
|
82
110
|
}
|
|
83
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
|
+
|
|
84
154
|
/**
|
|
85
155
|
* 生成 patch 目录名与 zip 名后缀
|
|
86
156
|
* @param {'svn'|'git'} vcs
|
|
87
157
|
* @param {string} toLabel
|
|
88
|
-
* @param {string}
|
|
158
|
+
* @param {string} zipPrefix
|
|
159
|
+
* @param {string|null} [version]
|
|
160
|
+
* @param {string|null} [partialSuffix] 子集包唯一后缀(--only / --file-list)
|
|
161
|
+
* @param {string|null} [worktreeLabel] 工作副本包标识(如 wc-20261118093000)
|
|
89
162
|
*/
|
|
90
|
-
function buildArtifactNames (vcs, toLabel, zipPrefix, version = null) {
|
|
163
|
+
function buildArtifactNames (vcs, toLabel, zipPrefix, version = null, partialSuffix = null, worktreeLabel = null) {
|
|
91
164
|
const versionLabel = version ? sanitizeVersionLabel(version) : null
|
|
92
165
|
|
|
166
|
+
/** @type {string} */
|
|
167
|
+
let base
|
|
93
168
|
if (versionLabel) {
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
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)
|
|
98
176
|
}
|
|
99
177
|
|
|
100
|
-
|
|
101
|
-
return {
|
|
102
|
-
patchName: `patch-r${toLabel}`,
|
|
103
|
-
zipName: `${zipPrefix}-r${toLabel}.zip`
|
|
104
|
-
}
|
|
105
|
-
}
|
|
178
|
+
const suffix = partialSuffix ? `-partial-${partialSuffix}` : ''
|
|
106
179
|
|
|
107
|
-
const short = toLabel.slice(0, 12)
|
|
108
180
|
return {
|
|
109
|
-
patchName: `patch-${
|
|
110
|
-
zipName: `${zipPrefix}-${
|
|
181
|
+
patchName: `patch-${base}${suffix}`,
|
|
182
|
+
zipName: `${zipPrefix}-${base}${suffix}.zip`
|
|
111
183
|
}
|
|
112
184
|
}
|
|
113
185
|
|
|
@@ -129,7 +201,16 @@ async function buildPatch (config) {
|
|
|
129
201
|
zipPrefix,
|
|
130
202
|
version,
|
|
131
203
|
onlyFiles,
|
|
132
|
-
fileList
|
|
204
|
+
fileList,
|
|
205
|
+
noArchive,
|
|
206
|
+
keepVersions,
|
|
207
|
+
pruneOut,
|
|
208
|
+
keepOut,
|
|
209
|
+
archiveLink,
|
|
210
|
+
fromWorktree,
|
|
211
|
+
profile,
|
|
212
|
+
protectFiles,
|
|
213
|
+
diffWorktree
|
|
133
214
|
} = config
|
|
134
215
|
|
|
135
216
|
const backendModule = getBackend(backend)
|
|
@@ -151,10 +232,51 @@ async function buildPatch (config) {
|
|
|
151
232
|
let toLabel
|
|
152
233
|
/** @type {Array<{ action: string, relativePath: string, parseError?: string }>} */
|
|
153
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
|
+
}
|
|
154
258
|
|
|
155
|
-
|
|
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
|
+
}
|
|
156
267
|
|
|
157
|
-
|
|
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') {
|
|
158
280
|
const svnInfo = getSvnInfo(sourceDir)
|
|
159
281
|
const toRev = config.toRef != null ? Number(config.toRef) : svnInfo.revision
|
|
160
282
|
let fromRev = config.fromRef != null ? Number(config.fromRef) : null
|
|
@@ -164,7 +286,7 @@ async function buildPatch (config) {
|
|
|
164
286
|
fromRev = parseInt(marker.value, 10)
|
|
165
287
|
} else {
|
|
166
288
|
throw new Error(
|
|
167
|
-
`未找到 deploy
|
|
289
|
+
`未找到 deploy/${markerHint} 或 .last-deploy-rev,请指定 --from=<revision>`
|
|
168
290
|
)
|
|
169
291
|
}
|
|
170
292
|
}
|
|
@@ -175,7 +297,7 @@ async function buildPatch (config) {
|
|
|
175
297
|
|
|
176
298
|
if (fromRev >= toRev) {
|
|
177
299
|
if (markDeployed && fromRev === toRev) {
|
|
178
|
-
writeDeployMarker(deployDir, 'svn', String(toRev))
|
|
300
|
+
writeDeployMarker(deployDir, 'svn', String(toRev), profile || null)
|
|
179
301
|
return {
|
|
180
302
|
success: true,
|
|
181
303
|
skipped: true,
|
|
@@ -199,6 +321,12 @@ async function buildPatch (config) {
|
|
|
199
321
|
toLabel = String(toRev)
|
|
200
322
|
diffEntries = getSvnDiffSummary({ sourceDir, fromRev, toRev })
|
|
201
323
|
} else {
|
|
324
|
+
if (diffWorktree) {
|
|
325
|
+
throw new Error(
|
|
326
|
+
'--worktree-diff 目前仅支持 SVN 工作副本;Git 请先提交改动,再按 commit 打包(或用 --from-worktree 放宽一致性校验)'
|
|
327
|
+
)
|
|
328
|
+
}
|
|
329
|
+
|
|
202
330
|
const gitInfo = getGitInfo(sourceDir)
|
|
203
331
|
const toCommit = config.toRef != null
|
|
204
332
|
? resolveGitRef(sourceDir, String(config.toRef))
|
|
@@ -213,14 +341,14 @@ async function buildPatch (config) {
|
|
|
213
341
|
fromCommit = resolveGitRef(sourceDir, marker.value)
|
|
214
342
|
} else {
|
|
215
343
|
throw new Error(
|
|
216
|
-
`未找到 deploy
|
|
344
|
+
`未找到 deploy/${markerHint}(git:...),请指定 --from=<commit|tag>`
|
|
217
345
|
)
|
|
218
346
|
}
|
|
219
347
|
}
|
|
220
348
|
|
|
221
349
|
if (fromCommit === toCommit) {
|
|
222
350
|
if (markDeployed) {
|
|
223
|
-
writeDeployMarker(deployDir, 'git', toCommit)
|
|
351
|
+
writeDeployMarker(deployDir, 'git', toCommit, profile || null)
|
|
224
352
|
return {
|
|
225
353
|
success: true,
|
|
226
354
|
skipped: true,
|
|
@@ -242,26 +370,44 @@ async function buildPatch (config) {
|
|
|
242
370
|
|
|
243
371
|
fromLabel = fromCommit
|
|
244
372
|
toLabel = toCommit
|
|
245
|
-
|
|
373
|
+
gitPrefix = getGitPrefix(gitInfo.root, sourceDir)
|
|
374
|
+
diffEntries = getGitDiffSummary({
|
|
375
|
+
sourceDir,
|
|
376
|
+
fromRef: fromCommit,
|
|
377
|
+
toRef: toCommit,
|
|
378
|
+
prefix: gitPrefix
|
|
379
|
+
})
|
|
246
380
|
}
|
|
247
381
|
|
|
248
382
|
const { addedOrModified, deleted, warnings } = classifyDiffEntries(diffEntries, sourceDir, backend)
|
|
249
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
|
+
|
|
250
394
|
// 按 --only / --file-list 筛选本次打包文件(用于单独提取部署)
|
|
251
395
|
const selectedFiles = resolveSelectedFiles(onlyFiles || [], fileList || null)
|
|
252
396
|
let deployFiles = addedOrModified
|
|
253
397
|
let deployDeleted = deleted
|
|
254
398
|
if (selectedFiles.length > 0) {
|
|
255
399
|
const filePick = filterFilesBySelection(addedOrModified, selectedFiles)
|
|
256
|
-
if (filePick.missing.length > 0) {
|
|
257
|
-
warnings.push(`--only 中以下文件不在本次增量内: ${filePick.missing.join(', ')}`)
|
|
258
|
-
}
|
|
259
|
-
deployFiles = filePick.matched
|
|
260
|
-
|
|
261
400
|
const deletedPick = filterFilesBySelection(deleted, selectedFiles)
|
|
401
|
+
deployFiles = filePick.matched
|
|
262
402
|
deployDeleted = deletedPick.matched
|
|
263
|
-
|
|
264
|
-
|
|
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(', ')}`)
|
|
265
411
|
}
|
|
266
412
|
}
|
|
267
413
|
|
|
@@ -277,11 +423,121 @@ async function buildPatch (config) {
|
|
|
277
423
|
}
|
|
278
424
|
}
|
|
279
425
|
|
|
280
|
-
|
|
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
|
+
)
|
|
281
506
|
const patchDir = path.join(outDir, patchName)
|
|
282
507
|
const zipPath = path.join(outDir, zipName)
|
|
283
508
|
const stagingDir = `${patchDir}.staging-${Date.now()}`
|
|
284
|
-
|
|
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
|
+
})
|
|
285
541
|
|
|
286
542
|
await fse.emptyDir(stagingDir)
|
|
287
543
|
|
|
@@ -300,7 +556,7 @@ async function buildPatch (config) {
|
|
|
300
556
|
'# PHP 后端增量部署清单',
|
|
301
557
|
`backend=${backend}`,
|
|
302
558
|
`vcs=${vcs}`,
|
|
303
|
-
|
|
559
|
+
`version=${deployVersionLabel}`,
|
|
304
560
|
vcs === 'svn'
|
|
305
561
|
? `from_revision=${fromLabel}`
|
|
306
562
|
: `from_commit=${fromLabel}`,
|
|
@@ -309,6 +565,10 @@ async function buildPatch (config) {
|
|
|
309
565
|
: `to_commit=${toLabel}`,
|
|
310
566
|
`generated_at=${new Date().toISOString().replace('T', ' ').slice(0, 19)}`,
|
|
311
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}`] : []),
|
|
312
572
|
'',
|
|
313
573
|
'# files',
|
|
314
574
|
...deployFiles,
|
|
@@ -323,18 +583,24 @@ async function buildPatch (config) {
|
|
|
323
583
|
|
|
324
584
|
fs.writeFileSync(path.join(stagingDir, 'MANIFEST.txt'), manifestLines.join('\n'), 'utf8')
|
|
325
585
|
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
586
|
+
// 始终写入 VERSION.txt,供服务器按版本号备份(含 r467 等 revision 包)
|
|
587
|
+
fs.writeFileSync(
|
|
588
|
+
path.join(stagingDir, 'VERSION.txt'),
|
|
589
|
+
[
|
|
590
|
+
`version=${deployVersionLabel}`,
|
|
591
|
+
`vcs=${vcs}`,
|
|
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
|
+
: []),
|
|
597
|
+
`file_count=${deployFiles.length}`
|
|
598
|
+
].join('\n'),
|
|
599
|
+
'utf8'
|
|
600
|
+
)
|
|
601
|
+
|
|
602
|
+
// 受保护文件清单:服务器端对“线上已存在”的文件跳过覆盖
|
|
603
|
+
writeProtectFile(stagingDir, protectFiles || [])
|
|
338
604
|
|
|
339
605
|
if (deployDeleted.length > 0) {
|
|
340
606
|
fs.writeFileSync(
|
|
@@ -344,6 +610,9 @@ async function buildPatch (config) {
|
|
|
344
610
|
)
|
|
345
611
|
}
|
|
346
612
|
|
|
613
|
+
// 可执行文件清单:服务器端据此 chmod(zip 在 Windows 上无法携带 exec 位)
|
|
614
|
+
writeModesFile(stagingDir, executableFiles)
|
|
615
|
+
|
|
347
616
|
// 构建成功后再替换目标 patch 目录,避免中途失败清空旧包
|
|
348
617
|
if (fs.existsSync(patchDir)) {
|
|
349
618
|
await fse.remove(patchDir)
|
|
@@ -351,23 +620,31 @@ async function buildPatch (config) {
|
|
|
351
620
|
await fse.move(stagingDir, patchDir)
|
|
352
621
|
|
|
353
622
|
if (!noZip) {
|
|
354
|
-
const zipResult = await createZipFromDirectory(patchDir, zipPath)
|
|
623
|
+
const zipResult = await createZipFromDirectory(patchDir, zipPath, { modes: fileModes })
|
|
355
624
|
zipSize = zipResult.size
|
|
356
625
|
}
|
|
357
626
|
} catch (error) {
|
|
358
|
-
await fse.remove(stagingDir).catch(() => {})
|
|
627
|
+
await fse.remove(stagingDir).catch(() => { })
|
|
359
628
|
throw error
|
|
360
629
|
}
|
|
361
630
|
|
|
362
631
|
if (markDeployed) {
|
|
363
|
-
writeDeployMarker(deployDir, vcs, toLabel)
|
|
632
|
+
writeDeployMarker(deployDir, vcs, toLabel, profile || null)
|
|
364
633
|
}
|
|
365
634
|
|
|
635
|
+
// 子集包使用独立标识,避免覆盖同版本的完整包;工作副本包带 wc 标识,
|
|
636
|
+
// 且不写入同版本号记录,避免覆盖真正的发布包
|
|
637
|
+
// (deployVersionLabel / artifactVersionLabel 已在上方统一计算)
|
|
638
|
+
|
|
366
639
|
// 注册版本号到 deploy/versions.json,便于后续 patch-extract 按版本提取
|
|
367
640
|
if (versionLabel) {
|
|
368
641
|
registerVersion(deployDir, {
|
|
369
|
-
version:
|
|
370
|
-
|
|
642
|
+
version: worktreeLabel
|
|
643
|
+
? `${String(version)}-${worktreeLabel}${partialSuffix}`
|
|
644
|
+
: partialSignature
|
|
645
|
+
? `${String(version)}-partial-${partialSignature}`
|
|
646
|
+
: String(version),
|
|
647
|
+
versionLabel: artifactVersionLabel,
|
|
371
648
|
vcs,
|
|
372
649
|
fromLabel,
|
|
373
650
|
toLabel,
|
|
@@ -375,15 +652,53 @@ async function buildPatch (config) {
|
|
|
375
652
|
deleted: deployDeleted,
|
|
376
653
|
patchDir,
|
|
377
654
|
zipPath: noZip ? undefined : zipPath,
|
|
378
|
-
partial:
|
|
655
|
+
partial: Boolean(partialSignature),
|
|
656
|
+
worktree: Boolean(worktreeLabel),
|
|
657
|
+
sourceVersion: partialSignature ? String(version) : undefined,
|
|
379
658
|
createdAt: new Date().toISOString()
|
|
380
659
|
})
|
|
660
|
+
if (worktreeLabel) {
|
|
661
|
+
// 工作副本包可随时重建,注册表只保留最近若干个,避免无限增长
|
|
662
|
+
pruneWorktreeRecords(deployDir, 5)
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
/** @type {string|undefined} */
|
|
667
|
+
let archiveDir
|
|
668
|
+
if (worktreeLabel) {
|
|
669
|
+
// 工作副本包可从工作区随时重建;不写入 archive(提示已在 warnings 中)
|
|
670
|
+
} else if (!noArchive) {
|
|
671
|
+
const archived = await archivePatchPackage({
|
|
672
|
+
deployDir,
|
|
673
|
+
archiveLabel: artifactVersionLabel || deployVersionLabel,
|
|
674
|
+
patchDir,
|
|
675
|
+
zipPath: noZip ? undefined : zipPath,
|
|
676
|
+
vcs,
|
|
677
|
+
fromLabel,
|
|
678
|
+
toLabel,
|
|
679
|
+
partial: Boolean(partialSignature),
|
|
680
|
+
keepVersions: keepVersions || 10,
|
|
681
|
+
link: Boolean(archiveLink)
|
|
682
|
+
})
|
|
683
|
+
archiveDir = archived.archiveDir
|
|
684
|
+
}
|
|
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
|
+
})
|
|
381
694
|
}
|
|
382
695
|
|
|
383
696
|
return {
|
|
384
697
|
success: true,
|
|
385
698
|
skipped: false,
|
|
386
|
-
message:
|
|
699
|
+
message: worktreeLabel
|
|
700
|
+
? `打包完成:工作副本(基准 r${fromLabel})`
|
|
701
|
+
: `打包完成:${versionLabel ? `v${versionLabel}` : (vcs === 'svn' ? `r${fromLabel} -> r${toLabel}` : `${fromLabel.slice(0, 8)}..${toLabel.slice(0, 8)}`)}`,
|
|
387
702
|
patchDir,
|
|
388
703
|
zipPath: noZip ? undefined : zipPath,
|
|
389
704
|
zipSize,
|
|
@@ -393,15 +708,24 @@ async function buildPatch (config) {
|
|
|
393
708
|
vcs,
|
|
394
709
|
fromLabel,
|
|
395
710
|
toLabel,
|
|
711
|
+
profile: profile || undefined,
|
|
712
|
+
protectCount: (protectFiles || []).length,
|
|
713
|
+
executableCount: executableFiles.length,
|
|
396
714
|
version: versionLabel || undefined,
|
|
397
|
-
|
|
715
|
+
deployVersionLabel,
|
|
716
|
+
artifactVersionLabel: artifactVersionLabel || undefined,
|
|
717
|
+
archiveDir,
|
|
718
|
+
prunedOut: prunedOut.length > 0 ? prunedOut : undefined,
|
|
719
|
+
markDeployedHint: markDeployed || worktreeLabel
|
|
398
720
|
? undefined
|
|
399
|
-
: `上线成功后执行: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 与线上内容一致' : '')
|
|
400
723
|
}
|
|
401
724
|
}
|
|
402
725
|
|
|
403
726
|
module.exports = {
|
|
404
727
|
buildPatch,
|
|
728
|
+
buildVersionLabels,
|
|
405
729
|
classifyDiffEntries,
|
|
406
730
|
buildArtifactNames
|
|
407
731
|
}
|