-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Expand-Diff.ps1
152 lines (131 loc) · 3.97 KB
/
Expand-Diff.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<#PSScriptInfo
.VERSION 1.0.4
.AUTHOR Roman Kuzmin
.COPYRIGHT (c) Roman Kuzmin
.TAGS Git Diff Patch Compare Merge
.GUID 964fe648-2547-4062-a2ce-6a0756b50223
.PROJECTURI https://github.com/nightroman/PowerShelf
.LICENSEURI http://www.apache.org/licenses/LICENSE-2.0
#>
<#
.Synopsis
Expands git diff into directories "a" and "b".
.Description
The script is designed for diff and patch files created by git. It expands
the specified diff file into directories "a" and "b" (original and changes)
with pieces of files stored in the diff.
Then you can use your diff tool of choice in order to compare the
directories "a" and "b", i.e. to visualize the original diff file.
The following diff lines are recognized and processed:
--- a/... | "a/..." | /dev/null ~ file "a"
+++ b/... | "b/..." | /dev/null ~ file "b"
@@... ~ chunk header
... ~ common line
-... ~ "a" line
+... ~ "b" line
Other lines are ignored.
.Parameter Diff
Specifies the diff file path.
.Parameter Root
Specifies the output directory where the directories "a" and "b" will
be created. The script fails if "a" or "b" exists in this directory.
If this parameter is omitted then the current location is used.
.Link
https://github.com/nightroman/PowerShelf
#>
[CmdletBinding()]
param(
[Parameter(Mandatory=1)]
[string]$Diff,
[string]$Root = '.'
)
trap {$PSCmdlet.ThrowTerminatingError($_)}
$ErrorActionPreference = 1
$Diff = $PSCmdlet.GetUnresolvedProviderPathFromPSPath($Diff)
$Root = $PSCmdlet.GetUnresolvedProviderPathFromPSPath($Root)
$RootA = Join-Path $Root a
$RootB = Join-Path $Root b
if (Test-Path -LiteralPath $RootA) {throw "The item '$RootA' exists. Remove it or specify a different root."}
if (Test-Path -LiteralPath $RootB) {throw "The item '$RootB' exists. Remove it or specify a different root."}
$null = [System.IO.Directory]::CreateDirectory($RootA)
$null = [System.IO.Directory]::CreateDirectory($RootB)
$fileA = ''
$fileB = ''
$linesA = [Activator]::CreateInstance([System.Collections.Generic.List[string]])
$linesB = [Activator]::CreateInstance([System.Collections.Generic.List[string]])
function Write-A {
if ($fileA) {
[System.IO.File]::WriteAllLines("$Root/$fileA", $linesA)
$script:fileA = ''
$linesA.Clear()
}
}
function Write-B {
if ($fileB) {
[System.IO.File]::WriteAllLines("$Root/$fileB", $linesB)
$script:fileB = ''
$linesB.Clear()
}
}
$regexOctets = [regex]'(\\\d\d\d)+'
$decodeOctets = {
$x = $args[0].Value
$n = $x.Length / 4
$a = [Activator]::CreateInstance([byte[]], $n)
for($i = 0; $i -lt $n; ++$i) {
$a[$i] = [Convert]::ToByte($x.Substring($i * 4 + 1, 3), 8)
}
[System.Text.Encoding]::UTF8.GetString($a)
}
function Decode-Path($Path) {
$regexOctets.Replace($Path, $decodeOctets)
}
foreach($line in Get-Content -LiteralPath $Diff -Encoding UTF8) {
if ($line.StartsWith('@@')) {
if ($fileA -and $fileB) {
if ($linesA) {$linesA.Add('')}
if ($linesB) {$linesB.Add('')}
$linesA.Add($line)
$linesB.Add($line)
}
}
elseif ($line -cmatch '^--- ([ab]/.+?)(\t|$)') {
Write-A
$fileA = $matches[1]
$null = [System.IO.Directory]::CreateDirectory("$Root/$(Split-Path $fileA)")
}
elseif ($line -cmatch '^--- "([ab]/.+)"') {
Write-A
$fileA = Decode-Path ($matches[1])
$null = [System.IO.Directory]::CreateDirectory("$Root/$(Split-Path $fileA)")
}
elseif ($line.StartsWith('--- /dev/null')) {
Write-A
}
elseif ($line -cmatch '^\+\+\+ ([ab]/.+?)(\t|$)') {
Write-B
$fileB = $matches[1]
$null = [System.IO.Directory]::CreateDirectory("$Root/$(Split-Path $fileB)")
}
elseif ($line -cmatch '^\+\+\+ "([ab]/.+)"') {
Write-B
$fileB = Decode-Path ($matches[1])
$null = [System.IO.Directory]::CreateDirectory("$Root/$(Split-Path $fileB)")
}
elseif ($line.StartsWith('+++ /dev/null')) {
Write-B
}
elseif ($line.StartsWith(' ')) {
$text = $line.Substring(1)
$linesA.Add($text)
$linesB.Add($text)
}
elseif ($line.StartsWith('-')) {
$linesA.Add($line.Substring(1))
}
elseif ($line.StartsWith('+')) {
$linesB.Add($line.Substring(1))
}
}
Write-A
Write-B