Last active
January 7, 2019 18:04
-
-
Save daxian-dbw/3df8f9236a73a1d9b8c5cc913476d618 to your computer and use it in GitHub Desktop.
Validate string replacement changes that affect large amount of files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## | |
## Get the diff content of a Github PR, and analyze if the diffs are only caused by the string replacement. | |
## [Interesting exercise] | |
## | |
param( | |
[Parameter(Mandatory=$true)] | |
[string] | |
$PRUrl, | |
[Parameter(Mandatory=$true)] | |
[string] | |
$OriginalString, | |
[Parameter(Mandatory=$true)] | |
[string] | |
$CurrentString, | |
[switch] | |
$PassThru | |
) | |
enum DiffState { | |
None | |
FileInfo | |
DiffContent | |
} | |
[DiffState]$state = [DiffState]::None | |
$Log = @{} | |
$diffFile = Join-Path -Path $PSScriptRoot -ChildPath "diff.txt" | |
if (Test-Path -Path $diffFile) { Remove-Item -Path $diffFile -Force -ErrorAction Stop } | |
## | |
## .diff and .patch can be append in the end to get some raw content | |
## | |
$PRUrl += ".diff" | |
Invoke-WebRequest -Uri $PRUrl -OutFile $diffFile | |
$Script:ScanResult = $null | |
switch -Wildcard -File $diffFile | |
{ | |
"diff --git *" | |
{ | |
$state = [DiffState]::FileInfo | |
$fileName = ($_ -split " ")[-1].Substring(2) | |
if ($Script:ScanResult -ne $null -and | |
$Script:ScanResult["OriginalStrs"].Count -ne $Script:ScanResult["NewStrs"].Count) | |
{ | |
throw "$($Script:ScanResult['FileName']) contains mismatched + and -" | |
} | |
if ($Log.ContainsKey($fileName)) | |
{ | |
throw "$fileName appeared twice in diff file. Something is wrong" | |
} | |
$Script:ScanResult = @{ FileName = $fileName; OriginalStrs = @(); NewStrs = @() } | |
$Log[$fileName] = $Script:ScanResult | |
} | |
"@@ * @@ *" | |
{ | |
$state = [DiffState]::DiffContent | |
} | |
"-*" | |
{ | |
if ($state -eq [DiffState]::DiffContent) | |
{ | |
$old = $_.Substring(1) | |
if (![string]::IsNullOrWhiteSpace($old)) | |
{ | |
$Script:ScanResult["OriginalStrs"] += $old | |
} | |
} | |
} | |
"+*" | |
{ | |
if ($state -eq [DiffState]::DiffContent) | |
{ | |
$new = $_.Substring(1) | |
if (![string]::IsNullOrWhiteSpace($new)) | |
{ | |
$Script:ScanResult["NewStrs"] += $new | |
} | |
} | |
} | |
} | |
Write-Host -Object "$($Log.Count) files involved in this PR`n" | |
foreach ($result in $Log.Values) | |
{ | |
$newStrs = $result["NewStrs"] | |
$originalStrs = $result["OriginalStrs"] | |
for ($i = 0; $i -lt $originalStrs.Length; $i++) | |
{ | |
$oldString = $originalStrs[$i] | |
$newString = $newStrs[$i] | |
$afterReplace = $oldString -replace $OriginalString, $CurrentString | |
if ($afterReplace -cne $newString) | |
{ | |
Write-Host @" | |
$($result['FileName']) - mismatch | |
Old: $oldString | |
New: $newString | |
"@ | |
} | |
} | |
} | |
if ($PassThru) { return $Log } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment