Private/Get-CCTextDiff.ps1
|
function Get-CCTextDiff { <# .SYNOPSIS Returns a unified diff between two strings via `git diff --no-index` (Compare-Object fallback). #> [CmdletBinding()] [OutputType([string])] param( [Parameter(Mandatory)][AllowEmptyString()][string]$Current, [Parameter(Mandatory)][AllowEmptyString()][string]$Proposed ) $tmp = Join-Path ([System.IO.Path]::GetTempPath()) ("cc-diff-" + [guid]::NewGuid().ToString('N')) New-Item -ItemType Directory -Path $tmp | Out-Null try { $a = Join-Path $tmp 'current' $b = Join-Path $tmp 'proposed' Set-Content -LiteralPath $a -Value $Current -NoNewline Set-Content -LiteralPath $b -Value $Proposed -NoNewline if (Get-Command git -ErrorAction SilentlyContinue) { # git diff --no-index exits 1 when files differ — expected, not an error. $out = & git diff --no-index --no-color -- $a $b 2>$null # Strip the header lines that reference the throwaway temp paths. return ($out | Where-Object { $_ -notmatch '^(diff --git|index |--- |\+\+\+ )' }) -join "`n" } $comparison = Compare-Object ($Current -split "`n") ($Proposed -split "`n") return ($comparison | ForEach-Object { $sign = if ($_.SideIndicator -eq '=>') { '+' } else { '-' } "$sign $($_.InputObject)" }) -join "`n" } finally { Remove-Item $tmp -Recurse -Force -ErrorAction SilentlyContinue } } |