dsh-profile-config-sync 0.4.1 → 0.6.40
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/LICENSE +2 -1
- package/README.en.md +167 -738
- package/README.md +238 -571
- package/client.js +721 -269
- package/cordis.patch.yml +1 -1
- package/docs/CHANGELOG.en.md +28 -0
- package/docs/CHANGELOG.md +80 -0
- package/docs/HISTORY.en.md +221 -0
- package/docs/HISTORY.md +252 -0
- package/docs/MULTI-PLATFORM.md +165 -0
- package/docs/REFERENCE.md +172 -0
- package/index.js +1247 -190
- package/package.json +11 -5
- package/runtime/apply.ps1 +79 -33
- package/runtime/atomic-write.mjs +57 -0
- package/runtime/capture.ps1 +188 -37
- package/runtime/contract.mjs +59 -7
- package/runtime/dsh-sync-common.ps1 +361 -22
- package/runtime/install-plugin.ps1 +94 -3
- package/runtime/live-log.mjs +75 -0
- package/runtime/platform.mjs +35 -0
- package/runtime/post-exit.ps1 +201 -28
- package/runtime/restore.ps1 +76 -8
- package/runtime/sync-engine.mjs +1324 -857
- package/runtime/sync.ps1 +192 -82
- package/runtime/tools/fix-session-cwd.mjs +12 -3
- package/runtime/tools/workspace-view.mjs +7 -3
- package/runtime/webdav.mjs +269 -23
package/runtime/sync.ps1
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
<#
|
|
1
|
+
<#
|
|
2
2
|
DSH 配置同步 —— 一条命令入口(把原来的「capture + git add + commit + push」和「git pull + apply」各收成一次调用)
|
|
3
3
|
|
|
4
4
|
用法
|
|
@@ -48,9 +48,8 @@ if (-not $RepoRoot) { $RepoRoot = $PSScriptRoot }
|
|
|
48
48
|
Reset-SyncNotices $DshHome $Action
|
|
49
49
|
|
|
50
50
|
function Get-SyncSettings {
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
try { return (Get-Content -LiteralPath $p -Raw -Encoding utf8 | ConvertFrom-Json) } catch { return $null }
|
|
51
|
+
# 面板保存设置走 rename,读者可能撞上极短的占用窗口;重试逻辑放在 common 里,可被夹具直接测。
|
|
52
|
+
return (Read-JsonFileWithRetry (Join-Path $DshHome 'dsh-config-sync.json'))
|
|
54
53
|
}
|
|
55
54
|
|
|
56
55
|
function Test-WebdavActive($s) {
|
|
@@ -61,6 +60,12 @@ function Test-WebdavActive($s) {
|
|
|
61
60
|
}
|
|
62
61
|
|
|
63
62
|
$SyncSettings = Get-SyncSettings
|
|
63
|
+
# 文件在但读不出来(占用/损坏):默认明确报错,绝不能让「读失败」伪装成「没配 WebDAV,走 git」。
|
|
64
|
+
# 但 status 是诊断命令:它本身就应该能回答「现在什么状态」,所以降级成警告、继续往下走。
|
|
65
|
+
if (-not $SyncSettings -and (Test-Path -LiteralPath (Join-Path $DshHome 'dsh-config-sync.json'))) {
|
|
66
|
+
$settingsUnreadable = "读不出同步设置(文件存在但此刻读不到或解析失败):{0}。稍后重试;反复出现请在面板里重新保存一次设置。" -f (Join-Path $DshHome 'dsh-config-sync.json')
|
|
67
|
+
if ($Action -eq 'status') { Warn $settingsUnreadable } else { Die $settingsUnreadable }
|
|
68
|
+
}
|
|
64
69
|
$UseWebdav = Test-WebdavActive $SyncSettings
|
|
65
70
|
|
|
66
71
|
# git 位置探测统一在 dsh-sync-common.ps1 里(Get-GitExe);WebDAV 模式不依赖 git
|
|
@@ -85,9 +90,12 @@ Say "== DSH 配置同步 / $Action =="
|
|
|
85
90
|
Say "仓库:$RepoRoot"
|
|
86
91
|
|
|
87
92
|
function Get-RepoState {
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
93
|
+
# 不能直接 `& $git ... 2>$null`:$ErrorActionPreference='Stop' 下 native 命令往 stderr 写东西
|
|
94
|
+
# 会被包成终止错误(脚本 94-98 行自己记的就是这条规律)。而空仓库上 `log -1` 必然写 fatal,
|
|
95
|
+
# 于是新 init、还没第一个 commit 时 `sync.ps1 status` 直接崩。统一走 Get-GitText(cmd 转发看退出码)。
|
|
96
|
+
$branch = (Get-GitText 'rev-parse --abbrev-ref HEAD').Text
|
|
97
|
+
$head = (Get-GitText 'log -1 --oneline').Text
|
|
98
|
+
$dirty = @((Get-GitText 'status --porcelain').Text -split "`r?`n" | Where-Object { $_ -and $_.Trim() })
|
|
91
99
|
return [pscustomobject]@{ Branch = $branch; Head = $head; Dirty = $dirty.Count }
|
|
92
100
|
}
|
|
93
101
|
|
|
@@ -130,7 +138,7 @@ function Write-SyncState([string]$Mode) {
|
|
|
130
138
|
head = $head
|
|
131
139
|
}
|
|
132
140
|
$text = ((($state | ConvertTo-Json -Depth 4) -replace "`r`n", "`n") + "`n")
|
|
133
|
-
|
|
141
|
+
$null = Write-FileAtomic (Join-Path $DshHome 'config-sync-state.json') $text (New-Object Text.UTF8Encoding($false, $false))
|
|
134
142
|
} catch {
|
|
135
143
|
Warn ('写同步状态失败(不影响同步本身):{0}' -f $_.Exception.Message)
|
|
136
144
|
}
|
|
@@ -175,7 +183,7 @@ function Read-ConflictFile {
|
|
|
175
183
|
|
|
176
184
|
function Write-ConflictFile($obj) {
|
|
177
185
|
$text = ((($obj | ConvertTo-Json -Depth 8) -replace "`r`n", "`n") + "`n")
|
|
178
|
-
|
|
186
|
+
Write-FileAtomic (Get-ConflictPath) $text (New-Object Text.UTF8Encoding($false, $false))
|
|
179
187
|
}
|
|
180
188
|
|
|
181
189
|
<#
|
|
@@ -425,6 +433,82 @@ function Restore-LocalKept($Report) {
|
|
|
425
433
|
return $n
|
|
426
434
|
}
|
|
427
435
|
|
|
436
|
+
function Get-NodeExeForWebdav {
|
|
437
|
+
foreach ($c in @(
|
|
438
|
+
(Join-Path $AppRoot 'resources\runtime\primary-runtime\dependencies\node\bin\node.exe'),
|
|
439
|
+
(Join-Path $AppRoot 'resources\runtime\bin\node.exe'),
|
|
440
|
+
(Join-Path $AppRoot 'resources\runtime\node.exe')
|
|
441
|
+
)) {
|
|
442
|
+
if ($c -and (Test-Path -LiteralPath $c)) { return $c }
|
|
443
|
+
}
|
|
444
|
+
$cmd = Get-Command node.exe -ErrorAction SilentlyContinue
|
|
445
|
+
if ($cmd) { return $cmd.Source }
|
|
446
|
+
return $null
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
function Find-WebdavScript {
|
|
450
|
+
foreach ($c in @(
|
|
451
|
+
(Join-Path $PSScriptRoot 'webdav.mjs'),
|
|
452
|
+
(Join-Path $RepoRoot 'webdav.mjs')
|
|
453
|
+
)) {
|
|
454
|
+
if ($c -and (Test-Path -LiteralPath $c)) { return $c }
|
|
455
|
+
}
|
|
456
|
+
return $null
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
# 往 .git\info\exclude 追加一条排除规则(幂等)。
|
|
460
|
+
# 坑:Add-Content -Encoding utf8 在文件不存在时会写 UTF-8 BOM(PS 5.1 实测首字节 239,187,191),
|
|
461
|
+
# BOM 紧贴第一条规则,git 就认不出 `/路径` —— 症状是「只有第一条排除永远不生效」,
|
|
462
|
+
# 含令牌/超限文件每轮被 git add -A 重新收进索引,只能靠门禁每轮再 reset 兜一次。
|
|
463
|
+
# 这里统一用不带 BOM 的 UTF8 追加,并把旧版本留下的 BOM 去掉。
|
|
464
|
+
function Add-GitExcludeEntry([string]$RelPath) {
|
|
465
|
+
$excludeFile = Join-Path $RepoRoot '.git\info\exclude'
|
|
466
|
+
$excludeDir = Split-Path -Parent $excludeFile
|
|
467
|
+
if (-not (Test-Path -LiteralPath $excludeDir)) { New-Item -ItemType Directory -Path $excludeDir -Force | Out-Null }
|
|
468
|
+
$exLine = '/' + ([string]$RelPath).Replace([string][char]92, '/')
|
|
469
|
+
$enc = New-Object Text.UTF8Encoding($false)
|
|
470
|
+
# 注意:不能用 [IO.File]::ReadAllText —— StreamReader 会自动吃掉 UTF-8 BOM,
|
|
471
|
+
# 于是"去掉历史 BOM"那段永远不生效。必须按字节判断首三字节。
|
|
472
|
+
$bytes = if (Test-Path -LiteralPath $excludeFile) { [IO.File]::ReadAllBytes($excludeFile) } else { [byte[]]@() }
|
|
473
|
+
$hasBom = ($bytes.Length -ge 3 -and $bytes[0] -eq 0xEF -and $bytes[1] -eq 0xBB -and $bytes[2] -eq 0xBF)
|
|
474
|
+
$cur = [Text.Encoding]::UTF8.GetString($bytes)
|
|
475
|
+
if ($cur.Length -gt 0 -and $cur[0] -eq [char]0xFEFF) { $cur = $cur.Substring(1) }
|
|
476
|
+
if ($hasBom) { [IO.File]::WriteAllText($excludeFile, $cur, $enc) }
|
|
477
|
+
if ($cur -notmatch [regex]::Escape($exLine)) {
|
|
478
|
+
[IO.File]::AppendAllText($excludeFile, $exLine + "`r`n", $enc)
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
function Invoke-WebdavAction([string]$SubCmd) {
|
|
483
|
+
$nodeExe = Get-NodeExeForWebdav
|
|
484
|
+
if (-not $nodeExe) { Die "找不到 node.exe,无法执行 WebDAV 同步(请确认客户端完整或已安装 Node.js)。" }
|
|
485
|
+
$script = Find-WebdavScript
|
|
486
|
+
if (-not $script) { Die "找不到 webdav.mjs,无法执行 WebDAV 同步。" }
|
|
487
|
+
$eap = $ErrorActionPreference
|
|
488
|
+
$eapRestored = $false
|
|
489
|
+
try {
|
|
490
|
+
$ErrorActionPreference = 'Continue'
|
|
491
|
+
if ($SubCmd -eq 'probe') {
|
|
492
|
+
$out = (& $nodeExe $script $SubCmd -DshHome $DshHome -RepoRoot $RepoRoot 2>&1 | Out-String)
|
|
493
|
+
$ErrorActionPreference = $eap
|
|
494
|
+
$eapRestored = $true
|
|
495
|
+
$line = ($out -split "`r?`n" | Where-Object { $_.Trim().StartsWith('{') } | Select-Object -Last 1)
|
|
496
|
+
if (-not $line) { return [pscustomobject]@{ ok = $false; error = ($out.Trim()) } }
|
|
497
|
+
try { return ($line | ConvertFrom-Json) } catch { return [pscustomobject]@{ ok = $false; error = ($out.Trim()) } }
|
|
498
|
+
}
|
|
499
|
+
& $nodeExe $script $SubCmd -DshHome $DshHome -RepoRoot $RepoRoot 2>&1 | ForEach-Object {
|
|
500
|
+
$l = [string]$_
|
|
501
|
+
if ($l -and $l.Trim()) { Say (' ' + $l.TrimEnd()) }
|
|
502
|
+
}
|
|
503
|
+
$code = $LASTEXITCODE
|
|
504
|
+
} finally {
|
|
505
|
+
# 必须 finally 恢复:以前只在线性路径上恢复,一旦 & $nodeExe 抛错或脚本被删,
|
|
506
|
+
# 整个进程后续都停在 Continue —— 后面的 Die 之外的真错误不再终止,同步会带着错误继续跑。
|
|
507
|
+
if (-not $eapRestored) { $ErrorActionPreference = $eap }
|
|
508
|
+
}
|
|
509
|
+
if ($code -ne 0) { Die ("WebDAV $SubCmd 失败(退出码 $code)。") }
|
|
510
|
+
}
|
|
511
|
+
|
|
428
512
|
# ---------- status ----------
|
|
429
513
|
if ($Action -eq 'status') {
|
|
430
514
|
if ($UseWebdav) {
|
|
@@ -443,15 +527,25 @@ if ($Action -eq 'status') {
|
|
|
443
527
|
}
|
|
444
528
|
$app = Get-HostAppVersion $AppRoot
|
|
445
529
|
$contract = $null
|
|
530
|
+
$contractDoc = $null
|
|
446
531
|
$cp = Join-Path $RepoRoot 'profile\host-contract.json'
|
|
447
532
|
if (Test-Path -LiteralPath $cp) {
|
|
448
|
-
try { $
|
|
533
|
+
try { $contractDoc = Get-Content -LiteralPath $cp -Raw -Encoding utf8 | ConvertFrom-Json } catch { $contractDoc = $null }
|
|
534
|
+
}
|
|
535
|
+
if ($contractDoc) {
|
|
536
|
+
# 老格式契约(缺 electronVersion / platforms)不当基准:上游时期它可能把 Electron 版本写进 appVersion,
|
|
537
|
+
# 拿它比会一直报「版本不一致」。这种契约按「待重写」处理,不吓用户。
|
|
538
|
+
if (Test-ContractStale $contractDoc $app) {
|
|
539
|
+
Warn "仓库契约需要按本机版本重写(下一次推送会重写)"
|
|
540
|
+
} else {
|
|
541
|
+
$contract = [string]$contractDoc.appVersion
|
|
542
|
+
}
|
|
449
543
|
}
|
|
450
544
|
if ($app -and $contract) {
|
|
451
545
|
if ($app -eq $contract) { Ok "版本一致:app = $app" }
|
|
452
546
|
else { Warn "版本不一致:本机 app = $app,仓库契约 = $contract(pull 会被拒绝,除非加 -AllowVersionDrift)" }
|
|
453
|
-
}
|
|
454
|
-
Warn ("
|
|
547
|
+
} elseif (-not $app) {
|
|
548
|
+
Warn ("读不到本机 app 版本({0}):无法做版本校验" -f 'resources\app\package.json / app.asar')
|
|
455
549
|
}
|
|
456
550
|
$pkg = Join-Path $RepoRoot 'profile\package.json'
|
|
457
551
|
if (Test-Path -LiteralPath $pkg) {
|
|
@@ -571,24 +665,19 @@ function Initialize-RepoSkeleton {
|
|
|
571
665
|
|
|
572
666
|
$contractPath = Join-Path $RepoRoot 'profile\host-contract.json'
|
|
573
667
|
if (-not (Test-Path -LiteralPath $contractPath)) {
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
$vf = Join-Path $AppRoot 'version'
|
|
581
|
-
if (Test-Path -LiteralPath $vf) { $ver = (Get-Content -LiteralPath $vf -Raw -Encoding utf8).Trim() }
|
|
582
|
-
}
|
|
668
|
+
# 版本必须取「客户端 app 版本」——Get-HostAppVersion 会读 resources\app\package.json,
|
|
669
|
+
# 读不到再从 app.asar 里解 dsh/package.json。
|
|
670
|
+
# 旧写法自己找一套回退:先 resources\app\package.json,再 <AppRoot>\version。
|
|
671
|
+
# 但这台安装根本没有 resources\app\package.json,<AppRoot>\version 是 **Electron** 版本(实测 44.7.0),
|
|
672
|
+
# 于是「首次推送」把契约写成 Electron 版本,另一台按 app 版本比对永远不一致,pull 被永久拒绝。
|
|
673
|
+
$ver = Get-HostAppVersion $AppRoot
|
|
583
674
|
if ($ver) {
|
|
584
675
|
$dir = Join-Path $RepoRoot 'profile'
|
|
585
676
|
if (-not (Test-Path -LiteralPath $dir)) { New-Item -ItemType Directory -Path $dir -Force | Out-Null }
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
}
|
|
591
|
-
[IO.File]::WriteAllText($contractPath, ($obj | ConvertTo-Json -Depth 4), (New-Object Text.UTF8Encoding $false))
|
|
677
|
+
# 写现代形状(带 electronVersion):apply.ps1 / contract.mjs 把「缺 electronVersion 的契约」判为老格式并忽略,
|
|
678
|
+
# 只写 appVersion 会让两端都以为「仓库没有契约」。
|
|
679
|
+
$text = ("{`n `"appVersion`": `"$ver`",`n `"electronVersion`": `"$(Get-HostElectronVersion $AppRoot)`"`n}`n" -replace "`r`n", "`n")
|
|
680
|
+
Write-FileAtomic $contractPath $text (New-Object Text.UTF8Encoding($false, $false))
|
|
592
681
|
Ok "已写入版本契约:$ver"
|
|
593
682
|
} else {
|
|
594
683
|
Warn "读不到本机客户端版本,跳过 profile\host-contract.json(另一台 pull 时会跳过版本校验)"
|
|
@@ -596,50 +685,6 @@ function Initialize-RepoSkeleton {
|
|
|
596
685
|
}
|
|
597
686
|
}
|
|
598
687
|
|
|
599
|
-
function Get-NodeExeForWebdav {
|
|
600
|
-
foreach ($c in @(
|
|
601
|
-
(Join-Path $AppRoot 'resources\runtime\primary-runtime\dependencies\node\bin\node.exe'),
|
|
602
|
-
(Join-Path $AppRoot 'resources\runtime\bin\node.exe'),
|
|
603
|
-
(Join-Path $AppRoot 'resources\runtime\node.exe')
|
|
604
|
-
)) {
|
|
605
|
-
if ($c -and (Test-Path -LiteralPath $c)) { return $c }
|
|
606
|
-
}
|
|
607
|
-
$cmd = Get-Command node.exe -ErrorAction SilentlyContinue
|
|
608
|
-
if ($cmd) { return $cmd.Source }
|
|
609
|
-
return $null
|
|
610
|
-
}
|
|
611
|
-
|
|
612
|
-
function Find-WebdavScript {
|
|
613
|
-
foreach ($c in @(
|
|
614
|
-
(Join-Path $PSScriptRoot 'webdav.mjs'),
|
|
615
|
-
(Join-Path $RepoRoot 'webdav.mjs')
|
|
616
|
-
)) {
|
|
617
|
-
if ($c -and (Test-Path -LiteralPath $c)) { return $c }
|
|
618
|
-
}
|
|
619
|
-
return $null
|
|
620
|
-
}
|
|
621
|
-
|
|
622
|
-
function Invoke-WebdavAction([string]$SubCmd) {
|
|
623
|
-
$nodeExe = Get-NodeExeForWebdav
|
|
624
|
-
if (-not $nodeExe) { Die "找不到 node.exe,无法执行 WebDAV 同步(请确认客户端完整或已安装 Node.js)。" }
|
|
625
|
-
$script = Find-WebdavScript
|
|
626
|
-
if (-not $script) { Die "找不到 webdav.mjs,无法执行 WebDAV 同步。" }
|
|
627
|
-
$eap = $ErrorActionPreference
|
|
628
|
-
$ErrorActionPreference = 'Continue'
|
|
629
|
-
$out = (& $nodeExe $script $SubCmd -DshHome $DshHome -RepoRoot $RepoRoot 2>&1 | Out-String)
|
|
630
|
-
$code = $LASTEXITCODE
|
|
631
|
-
$ErrorActionPreference = $eap
|
|
632
|
-
if ($SubCmd -eq 'probe') {
|
|
633
|
-
$line = ($out -split "`r?`n" | Where-Object { $_.Trim().StartsWith('{') } | Select-Object -Last 1)
|
|
634
|
-
if (-not $line) { return [pscustomobject]@{ ok = $false; error = ($out.Trim()) } }
|
|
635
|
-
try { return ($line | ConvertFrom-Json) } catch { return [pscustomobject]@{ ok = $false; error = ($out.Trim()) } }
|
|
636
|
-
}
|
|
637
|
-
foreach ($l in ($out -split "`r?`n")) {
|
|
638
|
-
if ($l -and $l.Trim()) { Say (' ' + $l.TrimEnd()) }
|
|
639
|
-
}
|
|
640
|
-
if ($code -ne 0) { Die ("WebDAV $SubCmd 失败(退出码 $code)。") }
|
|
641
|
-
}
|
|
642
|
-
|
|
643
688
|
function Initialize-EmptyRepo([string]$RemoteUrl) {
|
|
644
689
|
if (-not $RemoteUrl) {
|
|
645
690
|
Die ("本机仓库还是空的($RepoRoot),也没读到云端仓库地址:" +
|
|
@@ -758,9 +803,31 @@ if ($Action -eq 'push') {
|
|
|
758
803
|
if ($SkipWorkspace) { $captureArgs['SkipWorkspace'] = $true }
|
|
759
804
|
if ($DeepVerify) { $captureArgs['DeepVerify'] = $true }
|
|
760
805
|
& (Join-Path $PSScriptRoot 'capture.ps1') @captureArgs
|
|
806
|
+
# 3 = 另一个同步动作正在进行(capture 的互斥):整条链要原样往上传,不能压成 1,
|
|
807
|
+
# 否则 post-exit 会当失败、pull 分支还会触发自动回滚。
|
|
808
|
+
if ($LASTEXITCODE -eq 3) { Warn "另一个同步动作正在进行,本次推送跳过。"; exit 3 }
|
|
761
809
|
if ($LASTEXITCODE -ne 0) { Die "capture.ps1 未正常结束,已停止(未上传)。" }
|
|
810
|
+
# 上传前防泄漏门禁:清掉任何误落入仓库的本机私有凭据文件或含 GitHub 令牌的文件
|
|
811
|
+
$runtimeSelf = @('apply.ps1', 'capture.ps1', 'dsh-sync-common.ps1', 'post-exit.ps1', 'restore.ps1', 'sync.ps1', 'webdav.mjs', 'sync-engine.mjs')
|
|
812
|
+
Get-ChildItem -LiteralPath $RepoRoot -Recurse -File -Force -ErrorAction SilentlyContinue | ForEach-Object {
|
|
813
|
+
$rel = $_.FullName.Substring($RepoRoot.Length).TrimStart('\', '/')
|
|
814
|
+
if (-not $rel -or $rel -like '.git\*' -or $rel -like '.git/*' -or ($runtimeSelf -contains $rel)) { return }
|
|
815
|
+
if (Test-BlockedProfilePath $rel) {
|
|
816
|
+
# 浏览器剖面 / 凭据库:与凭据不是一回事,措辞必须分开(否则用户会去查令牌)
|
|
817
|
+
Warn ("安全拦截:发现浏览器剖面 / 凭据库文件(含登录态),已从仓库剔除(绝不上传):$rel")
|
|
818
|
+
Remove-Item -LiteralPath $_.FullName -Force -ErrorAction SilentlyContinue
|
|
819
|
+
} elseif (Test-SyncExtraSecret $_.FullName $DshHome) {
|
|
820
|
+
Warn ("安全拦截:发现含令牌或本机私有凭据的文件,已从仓库剔除(绝不上传):$rel")
|
|
821
|
+
Remove-Item -LiteralPath $_.FullName -Force -ErrorAction SilentlyContinue
|
|
822
|
+
}
|
|
823
|
+
}
|
|
762
824
|
Say ""
|
|
763
825
|
Say "== 上传到 WebDAV =="
|
|
826
|
+
$delQueue = Join-Path $DshHome 'config-sync-delete-cloud.json'
|
|
827
|
+
if (Test-Path -LiteralPath $delQueue) {
|
|
828
|
+
Invoke-WebdavAction 'delete-paths'
|
|
829
|
+
try { Remove-Item -LiteralPath $delQueue -Force } catch { }
|
|
830
|
+
}
|
|
764
831
|
Invoke-WebdavAction 'upload'
|
|
765
832
|
Ok "已通过 WebDAV 推送到云端"
|
|
766
833
|
Write-SyncState 'push'
|
|
@@ -840,7 +907,10 @@ if ($Action -eq 'push') {
|
|
|
840
907
|
if ($SkipWorkspace) { $captureArgs['SkipWorkspace'] = $true }
|
|
841
908
|
if ($DeepVerify) { $captureArgs['DeepVerify'] = $true }
|
|
842
909
|
& (Join-Path $PSScriptRoot 'capture.ps1') @captureArgs
|
|
910
|
+
if ($LASTEXITCODE -eq 3) { Warn "另一个同步动作正在进行,本次推送跳过。"; exit 3 }
|
|
843
911
|
if ($LASTEXITCODE -ne 0) { Die "capture.ps1 未正常结束,已停止(未提交)。" }
|
|
912
|
+
$delQueue = Join-Path $DshHome 'config-sync-delete-cloud.json'
|
|
913
|
+
if (Test-Path -LiteralPath $delQueue) { try { Remove-Item -LiteralPath $delQueue -Force } catch { } }
|
|
844
914
|
|
|
845
915
|
Say ""
|
|
846
916
|
Say "== 提交 =="
|
|
@@ -850,6 +920,50 @@ if ($Action -eq 'push') {
|
|
|
850
920
|
# Test-Path -LiteralPath 就是「路径中具有非法字符」,整次同步会在这里挂掉
|
|
851
921
|
$pending = @((Read-GitLines 'diff --cached --name-only').Lines)
|
|
852
922
|
|
|
923
|
+
# ---------- 令牌 / 私密凭据防泄漏门禁(提交前) ----------
|
|
924
|
+
if ($pending.Count -gt 0) {
|
|
925
|
+
$runtimeSelf = @('apply.ps1', 'capture.ps1', 'dsh-sync-common.ps1', 'post-exit.ps1', 'restore.ps1', 'sync.ps1', 'webdav.mjs', 'sync-engine.mjs')
|
|
926
|
+
$secretHits = @()
|
|
927
|
+
foreach ($rel in $pending) {
|
|
928
|
+
if ($runtimeSelf -contains ([string]$rel).Replace('\', '/')) { continue }
|
|
929
|
+
try {
|
|
930
|
+
$full = Join-Path $RepoRoot $rel
|
|
931
|
+
if (-not (Test-Path -LiteralPath $full)) { continue }
|
|
932
|
+
if (Test-SyncExtraSecret $full $DshHome) { $secretHits += [string]$rel }
|
|
933
|
+
} catch { }
|
|
934
|
+
}
|
|
935
|
+
if ($secretHits.Count -gt 0) {
|
|
936
|
+
foreach ($relPath in $secretHits) {
|
|
937
|
+
# reset 也可能因为索引被占用(.git\index.lock)往 stderr 写错;EAP=Stop 下会打断整次推送,
|
|
938
|
+
# 留下一批「全部已 staged」的半提交态。这里只告警,不终止。
|
|
939
|
+
try { & $git -C $RepoRoot reset -q -- $relPath 2>$null } catch { Warn ("从索引撤回失败(下次提交前会再撤一次):" + $relPath) }
|
|
940
|
+
try { Add-GitExcludeEntry $relPath } catch { }
|
|
941
|
+
Warn ("安全拦截:检测到本机私有凭据或 GitHub 令牌,已阻止提交并排除:$relPath")
|
|
942
|
+
}
|
|
943
|
+
$pending = @((Read-GitLines 'diff --cached --name-only').Lines)
|
|
944
|
+
}
|
|
945
|
+
}
|
|
946
|
+
|
|
947
|
+
# ---------- 浏览器剖面 / 凭据库门禁(提交前,0.6.14) ----------
|
|
948
|
+
# 与 WebDAV 门禁共用同一份段名名单。为什么必须在这里挡:`git add -A` 会把仓库目录里
|
|
949
|
+
# 一切新文件收进索引,而**历史里的东西删不干净** —— 一旦 login data / cookies 进了提交,
|
|
950
|
+
# 后面就算删文件也只是再加一个删除提交,远端 clone 依旧拿得到。
|
|
951
|
+
if ($pending.Count -gt 0) {
|
|
952
|
+
$profileHits = @()
|
|
953
|
+
foreach ($rel in $pending) {
|
|
954
|
+
try { if (Test-BlockedProfilePath ([string]$rel)) { $profileHits += [string]$rel } } catch { }
|
|
955
|
+
}
|
|
956
|
+
if ($profileHits.Count -gt 0) {
|
|
957
|
+
foreach ($relPath in $profileHits) {
|
|
958
|
+
try { & $git -C $RepoRoot reset -q -- $relPath 2>$null } catch { Warn ("从索引撤回失败(下次提交前会再撤一次):" + $relPath) }
|
|
959
|
+
try { Add-GitExcludeEntry $relPath } catch { }
|
|
960
|
+
Warn ("安全拦截:浏览器剖面 / 凭据库文件(含登录态),已阻止提交并排除:$relPath")
|
|
961
|
+
}
|
|
962
|
+
Warn '这些文件仍留在磁盘;它们不该离开本机。要同步某个插件的小配置,把它挪出这些目录名即可。'
|
|
963
|
+
$pending = @((Read-GitLines 'diff --cached --name-only').Lines)
|
|
964
|
+
}
|
|
965
|
+
}
|
|
966
|
+
|
|
853
967
|
# ---------- 单文件上限预检(提交前) ----------
|
|
854
968
|
# capture 已在源头挡掉 workspace 里 >100MB 的资料,但仓库目录里仍可能有别处来的大文件
|
|
855
969
|
# (手工放的、旧版本留下的、其它插件写的),`git add -A` 会照样收进索引,然后 push 被
|
|
@@ -872,19 +986,10 @@ if ($Action -eq 'push') {
|
|
|
872
986
|
}
|
|
873
987
|
}
|
|
874
988
|
if ($tooLarge.Count -gt 0) {
|
|
875
|
-
$excludeFile = Join-Path $RepoRoot '.git\info\exclude'
|
|
876
|
-
$excludeDir = Split-Path -Parent $excludeFile
|
|
877
|
-
if (-not (Test-Path -LiteralPath $excludeDir)) { New-Item -ItemType Directory -Path $excludeDir -Force | Out-Null }
|
|
878
989
|
foreach ($o in $tooLarge) {
|
|
879
990
|
$relPath = [string]$o.Rel
|
|
880
|
-
& $git -C $RepoRoot reset -q -- $relPath 2>$null
|
|
881
|
-
|
|
882
|
-
try {
|
|
883
|
-
$cur = if (Test-Path -LiteralPath $excludeFile) { Get-Content -LiteralPath $excludeFile -Raw -Encoding utf8 } else { '' }
|
|
884
|
-
if ($cur -notmatch [regex]::Escape($exLine)) {
|
|
885
|
-
Add-Content -LiteralPath $excludeFile -Value $exLine -Encoding utf8
|
|
886
|
-
}
|
|
887
|
-
} catch { }
|
|
991
|
+
try { & $git -C $RepoRoot reset -q -- $relPath 2>$null } catch { Warn ("从索引撤回失败(下次提交前会再撤一次):" + $relPath) }
|
|
992
|
+
try { Add-GitExcludeEntry $relPath } catch { }
|
|
888
993
|
Warn ('超过单文件上限({0:N1} MB ≥ {1:N0} MB),已从本次提交剔除:{2}' -f ($o.Bytes / 1MB), ($pushHardLimit / 1MB), $relPath)
|
|
889
994
|
Add-SyncNotice -Kind 'too-large' -Path $relPath -Bytes $o.Bytes -Reason ('{0:N1} MB ≥ 单文件上限 {1:N0} MB,未提交也未推送' -f ($o.Bytes / 1MB), ($pushHardLimit / 1MB))
|
|
890
995
|
}
|
|
@@ -1052,6 +1157,11 @@ if ($Action -eq 'push') {
|
|
|
1052
1157
|
}
|
|
1053
1158
|
|
|
1054
1159
|
# ---------- pull ----------
|
|
1160
|
+
# 动手前先问一句:别的同步动作在跑就整段跳过(退出码 3),别把仓库先改了再说。
|
|
1161
|
+
if (Test-SyncRunBusy $DshHome) {
|
|
1162
|
+
Warn "另一个同步动作正在进行(同一个 DSH 目录),本次拉取没有执行。等它结束后再试一次。"
|
|
1163
|
+
exit 3
|
|
1164
|
+
}
|
|
1055
1165
|
if ($UseWebdav) {
|
|
1056
1166
|
if ((Test-DshRunning) -and (-not $Force)) {
|
|
1057
1167
|
Die "DeepSeek Harness 仍在运行。请完全退出(含托盘)后重跑,或加 -Force 强制继续。"
|
|
@@ -43,7 +43,11 @@ if (typeof zlib.zstdDecompressSync !== 'function' || typeof zlib.zstdCompressSyn
|
|
|
43
43
|
}
|
|
44
44
|
|
|
45
45
|
const spec = JSON.parse(fs.readFileSync(specPath, 'utf8'));
|
|
46
|
-
|
|
46
|
+
// 只在 Windows 上把 / 归一成 \:在 macOS/Linux 上这么干会把 /home/u/.dsh 变成
|
|
47
|
+
// \home\u\.dsh,路径直接失效(这个脚本是要跨平台跑的)。
|
|
48
|
+
const isWin = process.platform === 'win32';
|
|
49
|
+
const homeRaw = String(spec.home || '');
|
|
50
|
+
const home = (isWin ? homeRaw.replace(/\//g, '\\') : homeRaw).replace(/[\\/]+$/, '');
|
|
47
51
|
const dirs = spec.dirs || {};
|
|
48
52
|
const sessionsRoot = path.join(home, 'sessions');
|
|
49
53
|
|
|
@@ -103,8 +107,13 @@ for (const [dirName, cwd] of Object.entries(dirs)) {
|
|
|
103
107
|
if (!fs.existsSync(dirPath)) { result.missing++; continue; }
|
|
104
108
|
for (const entry of fs.readdirSync(dirPath, { withFileTypes: true })) {
|
|
105
109
|
if (!entry.isDirectory()) continue;
|
|
106
|
-
|
|
107
|
-
|
|
110
|
+
// 会话文件名带版本号(session.v4.jsonl.zstd):写死版本号会在宿主升版(v5/v6…)后
|
|
111
|
+
// 静默跳过所有会话,报告里却是「已修正 0 个」,看起来像没事。
|
|
112
|
+
const sessionFiles = fs.readdirSync(path.join(dirPath, entry.name))
|
|
113
|
+
.filter((n) => /^session\.v\d+\.jsonl\.zstd$/.test(n))
|
|
114
|
+
.sort()
|
|
115
|
+
if (!sessionFiles.length) continue;
|
|
116
|
+
const file = path.join(dirPath, entry.name, sessionFiles[sessionFiles.length - 1]);
|
|
108
117
|
try {
|
|
109
118
|
const buf = fs.readFileSync(file);
|
|
110
119
|
const first = decodeFirstFrame(buf);
|
|
@@ -56,9 +56,13 @@ function writeJsonAtomic(path, obj) {
|
|
|
56
56
|
try {
|
|
57
57
|
fs.writeFileSync(tmp, text, 'utf8')
|
|
58
58
|
try {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
59
|
+
// 同盘 rename 才是原子替换:copyFileSync 是「截断目标再写」,
|
|
60
|
+
// 宿主在这一瞬间读到的是半截 JSON —— workspace.json 会被当成空表重建(工作区列表瞬间清空)。
|
|
61
|
+
fs.renameSync(tmp, path)
|
|
62
|
+
} catch {
|
|
63
|
+
// Windows 上目标被别的进程打开时 rename 会 EPERM/EBUSY:退回拷贝,
|
|
64
|
+
// 只是失去原子性,比直接写错强。
|
|
65
|
+
try { fs.copyFileSync(tmp, path) } finally { try { fs.unlinkSync(tmp) } catch { } }
|
|
62
66
|
}
|
|
63
67
|
} catch (error) {
|
|
64
68
|
try { fs.unlinkSync(tmp) } catch { }
|