diff --git a/scripts/deploy-from-mac.sh b/scripts/deploy-from-mac.sh index 4e00a2d3..07dbb5e4 100755 --- a/scripts/deploy-from-mac.sh +++ b/scripts/deploy-from-mac.sh @@ -45,7 +45,13 @@ release_includes_commit() { local unmerged_commits unique_merges merge conflict_resolution unmerged_commits="$(git cherry "$2" "$1" 2>/dev/null)" || return 1 if printf '%s\n' "$unmerged_commits" | grep -q '^+ '; then - return 1 + # 一次 squash 可能合并多条线上提交,单条 patch-id 无法匹配。 + # 仅当合并整个线上版本没有冲突,且合并结果与候选版本完全相同,才接受。 + local merged_tree release_tree + merged_tree="$(git merge-tree --write-tree "$2" "$1" 2>/dev/null)" || return 1 + release_tree="$(git rev-parse "${2}^{tree}" 2>/dev/null)" || return 1 + [ "$merged_tree" = "$release_tree" ] + return $? fi unique_merges="$(git rev-list --merges "$1" "^$2" 2>/dev/null)" || return 1 while IFS= read -r merge; do diff --git a/scripts/deploy-from-workstation.ps1 b/scripts/deploy-from-workstation.ps1 index 5306a185..afe08561 100644 --- a/scripts/deploy-from-workstation.ps1 +++ b/scripts/deploy-from-workstation.ps1 @@ -97,7 +97,15 @@ function Test-CaiwuReleaseIncludesCommit { # 才允许通过;任何真实新增或丢失的改动仍会被拦截。 $unmergedCommits = @(git cherry $ReleaseSha $PreviousSha 2>$null) if ($LASTEXITCODE -ne 0) { return $false } - if ($unmergedCommits | Where-Object { $_ -match '^\+ ' }) { return $false } + if ($unmergedCommits | Where-Object { $_ -match '^\+ ' }) { + # 一次 squash 可能合并多条线上提交,单条 patch-id 无法匹配。 + # 仅当合并整个线上版本没有冲突,且合并结果与候选版本完全相同,才接受。 + $mergedTree = (git merge-tree --write-tree $ReleaseSha $PreviousSha 2>$null) -join '' + if ($LASTEXITCODE -ne 0) { return $false } + $releaseTree = (git rev-parse "${ReleaseSha}^{tree}" 2>$null) -join '' + if ($LASTEXITCODE -ne 0) { return $false } + return $mergedTree -eq $releaseTree + } $uniqueMerges = @(git rev-list --merges $PreviousSha "^$ReleaseSha" 2>$null) if ($LASTEXITCODE -ne 0) { return $false } diff --git a/scripts/test-release-inclusion.ps1 b/scripts/test-release-inclusion.ps1 index 93629121..b5dbb610 100644 --- a/scripts/test-release-inclusion.ps1 +++ b/scripts/test-release-inclusion.ps1 @@ -46,12 +46,31 @@ try { $equivalentPatch=New-FixtureCommit 'hotfix content' 'equivalent squash on newer base' Invoke-FixtureGit switch -c unrelated $base | Out-Null $unrelated=New-FixtureCommit 'different hotfix content' 'hotfix' + Invoke-FixtureGit switch -c combined-hotfix $base | Out-Null + New-FixtureCommit 'first hotfix content' 'first hotfix' | Out-Null + [IO.File]::WriteAllText((Join-Path $fixture 'repair.txt'),'second hotfix content') + Invoke-FixtureGit add repair.txt | Out-Null + Invoke-FixtureGit commit -m 'second hotfix' | Out-Null + $combinedHotfix=(Invoke-FixtureGit rev-parse HEAD) -join '' + Invoke-FixtureGit switch -c combined-release $base | Out-Null + [IO.File]::WriteAllText((Join-Path $fixture 'new-feature.txt'),'new release content') + Invoke-FixtureGit add new-feature.txt | Out-Null + Invoke-FixtureGit commit -m 'new release context' | Out-Null + $incompleteRelease=New-FixtureCommit 'first hotfix content' 'only one hotfix' + Invoke-FixtureGit switch -c combined-squash ($incompleteRelease+'^') | Out-Null + [IO.File]::WriteAllText((Join-Path $fixture 'value.txt'),'first hotfix content') + [IO.File]::WriteAllText((Join-Path $fixture 'repair.txt'),'second hotfix content') + Invoke-FixtureGit add value.txt repair.txt | Out-Null + Invoke-FixtureGit commit -m 'both hotfixes in one squash' | Out-Null + $combinedSquash=(Invoke-FixtureGit rev-parse HEAD) -join '' $cases=@( @{name='direct ancestor';previous=$base;release=$release;expected=$true}, @{name='same commit';previous=$hotfix;release=$hotfix;expected=$true}, @{name='identical squash tree at head';previous=$hotfix;release=$squashed;expected=$true}, @{name='identical squash tree in release ancestry';previous=$hotfix;release=$release;expected=$true}, @{name='equivalent patch on different parent tree';previous=$hotfix;release=$equivalentPatch;expected=$true}, + @{name='multiple hotfix commits included by one squash';previous=$combinedHotfix;release=$combinedSquash;expected=$true}, + @{name='one of multiple hotfix commits is missing';previous=$combinedHotfix;release=$incompleteRelease;expected=$false}, @{name='real unmerged change';previous=$unrelated;release=$release;expected=$false}, @{name='rollback to before hotfix';previous=$hotfix;release=$base;expected=$false}, @{name='missing deployed commit';previous=('0'*40);release=$release;expected=$false},