Skip to content

Commit 5f723e6

Browse files
committed
Expand-Diff - support PS v2, add tests
1 parent 9ed9a9d commit 5f723e6

File tree

7 files changed

+132
-16
lines changed

7 files changed

+132
-16
lines changed

Demo/Expand-Diff/.test.ps1

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
2+
task Add {
3+
remove a, b
4+
5+
Expand-Diff.ps1 Test-Add.diff
6+
7+
$r = @(Get-ChildItem a -Recurse -Force)
8+
equals $r.Count 0
9+
10+
$r = @(Get-ChildItem b -Recurse -Force)
11+
equals $r.Count 1
12+
equals $r[0].FullName "$BuildRoot\b\1.txt"
13+
equals (Get-Content b\1.txt) '1'
14+
15+
remove a, b
16+
}
17+
18+
task Delete {
19+
remove a, b
20+
21+
Expand-Diff.ps1 Test-Delete.diff
22+
23+
$r = @(Get-ChildItem a -Recurse -Force)
24+
equals $r.Count 1
25+
equals $r[0].FullName "$BuildRoot\a\1.txt"
26+
equals (Get-Content a\1.txt) '1'
27+
28+
$r = @(Get-ChildItem b -Recurse -Force)
29+
equals $r.Count 0
30+
31+
remove a, b
32+
}
33+
34+
task Rename {
35+
remove a, b
36+
37+
Expand-Diff.ps1 Test-Rename.diff
38+
39+
$r = @(Get-ChildItem a -Recurse -Force)
40+
equals $r.Count 1
41+
equals $r[0].FullName "$BuildRoot\a\1.txt"
42+
equals (Get-Content a\1.txt) '2'
43+
44+
$r = @(Get-ChildItem b -Recurse -Force)
45+
equals $r.Count 1
46+
equals $r[0].FullName "$BuildRoot\b\2.txt"
47+
equals (Get-Content b\2.txt) '2'
48+
49+
remove a, b
50+
}
51+
52+
task Change {
53+
remove a, b
54+
55+
Expand-Diff.ps1 Test-Change.diff
56+
57+
$r = @(Get-ChildItem a -Recurse -Force)
58+
equals $r.Count 1
59+
equals $r[0].FullName "$BuildRoot\a\файл.txt"
60+
equals ((Get-Content a\файл.txt -Encoding UTF8) -join '|') '@@ -1,5 +1,5 @@|мой|дядя|самый|честных|правил'
61+
62+
$r = @(Get-ChildItem b -Recurse -Force)
63+
equals $r.Count 1
64+
equals $r[0].FullName "$BuildRoot\b\файл.txt"
65+
equals ((Get-Content b\файл.txt -Encoding UTF8) -join '|') '@@ -1,5 +1,5 @@|мой|дядя|самых|честных|правил'
66+
67+
remove a, b
68+
}

Demo/Expand-Diff/Test-Add.diff

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
diff --git a/1.txt b/1.txt
3+
new file mode 100644
4+
index 0000000..d00491f
5+
--- /dev/null
6+
+++ b/1.txt
7+
@@ -0,0 +1 @@
8+
+1

Demo/Expand-Diff/Test-Change.diff

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
diff --git "a/\321\204\320\260\320\271\320\273.txt" "b/\321\204\320\260\320\271\320\273.txt"
3+
index c6ab37f..33e126b 100644
4+
--- "a/\321\204\320\260\320\271\320\273.txt"
5+
+++ "b/\321\204\320\260\320\271\320\273.txt"
6+
@@ -1,5 +1,5 @@
7+
мой
8+
дядя
9+
-самый
10+
+самых
11+
честных
12+
правил

Demo/Expand-Diff/Test-Delete.diff

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
diff --git a/1.txt b/1.txt
3+
deleted file mode 100644
4+
index d00491f..0000000
5+
--- a/1.txt
6+
+++ /dev/null
7+
@@ -1 +0,0 @@
8+
-1

Demo/Expand-Diff/Test-Rename.diff

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
diff --git a/1.txt b/1.txt
3+
deleted file mode 100644
4+
index 0cfbf08..0000000
5+
--- a/1.txt
6+
+++ /dev/null
7+
@@ -1 +0,0 @@
8+
-2
9+
diff --git a/2.txt b/2.txt
10+
new file mode 100644
11+
index 0000000..0cfbf08
12+
--- /dev/null
13+
+++ b/2.txt
14+
@@ -0,0 +1 @@
15+
+2

Expand-Diff.ps1

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<#PSScriptInfo
2-
.VERSION 1.0.1
2+
.VERSION 1.0.2
33
.AUTHOR Roman Kuzmin
44
.COPYRIGHT (c) Roman Kuzmin
5-
.TAGS Git Diff Compare Merge
5+
.TAGS Git Diff Patch Compare Merge
66
.GUID 964fe648-2547-4062-a2ce-6a0756b50223
77
.PROJECTURI https://github.com/nightroman/PowerShelf
88
.LICENSEURI http://www.apache.org/licenses/LICENSE-2.0
@@ -15,19 +15,19 @@
1515
.Description
1616
The script is designed for diff and patch files created by git. It expands
1717
the specified diff file into directories "a" and "b" (original and changes)
18-
with pieces of files provided by the diff.
18+
with pieces of files stored in the diff.
1919
2020
Then you can use your diff tool of choice in order to compare the
2121
directories "a" and "b", i.e. to visualize the original diff file.
2222
2323
The following diff lines are recognized and processed:
2424
25-
|--- a/... | "a/..." | /dev/null ~ file "a"
26-
|+++ b/... | "b/..." | /dev/null ~ file "b"
27-
|@@... ~ chunk header
28-
| ... ~ common line
29-
|-... ~ "a" line
30-
|+... ~ "b" line
25+
--- a/... | "a/..." | /dev/null ~ file "a"
26+
+++ b/... | "b/..." | /dev/null ~ file "b"
27+
@@... ~ chunk header
28+
... ~ common line
29+
-... ~ "a" line
30+
+... ~ "b" line
3131
3232
Other lines are ignored.
3333
@@ -58,15 +58,15 @@ $Root = $PSCmdlet.GetUnresolvedProviderPathFromPSPath($Root)
5858

5959
$RootA = Join-Path $Root a
6060
$RootB = Join-Path $Root b
61-
if (Test-Path -LiteralPath $RootA) {throw "The path '$RootA' exists. Remove it or specify a different root."}
62-
if (Test-Path -LiteralPath $RootB) {throw "The path '$RootB' exists. Remove it or specify a different root."}
61+
if (Test-Path -LiteralPath $RootA) {throw "The item '$RootA' exists. Remove it or specify a different root."}
62+
if (Test-Path -LiteralPath $RootB) {throw "The item '$RootB' exists. Remove it or specify a different root."}
6363
$null = [System.IO.Directory]::CreateDirectory($RootA)
6464
$null = [System.IO.Directory]::CreateDirectory($RootB)
6565

6666
$fileA = ''
6767
$fileB = ''
68-
$linesA = [System.Collections.Generic.List[string]]@()
69-
$linesB = [System.Collections.Generic.List[string]]@()
68+
$linesA = [Activator]::CreateInstance([System.Collections.Generic.List[string]])
69+
$linesB = [Activator]::CreateInstance([System.Collections.Generic.List[string]])
7070

7171
function Write-A {
7272
if ($fileA) {
@@ -88,9 +88,10 @@ $regexOctets = [regex]'(\\\d\d\d)+'
8888

8989
$decodeOctets = {
9090
$x = $args[0].Value
91-
$a = [System.Collections.Generic.List[byte]]@()
92-
for($i = 1; $i -lt $x.Length; $i += 4) {
93-
$a.Add([byte][Convert]::ToInt32($x.Substring($i, 3), 8))
91+
$n = $x.Length / 4
92+
$a = [Activator]::CreateInstance([byte[]], $n)
93+
for($i = 0; $i -lt $n; ++$i) {
94+
$a[$i] = [Convert]::ToByte($x.Substring($i * 4 + 1, 3), 8)
9495
}
9596
[System.Text.Encoding]::UTF8.GetString($a)
9697
}

Pack/Release-Notes.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# PowerShelf Release Notes
22

3+
## v1.11.2
4+
5+
*Expand-Diff.ps1* - PowerShell v2 support, tests.
6+
37
## v1.11.1
48

59
*Expand-Diff.ps1* - deal with encoded file names.

0 commit comments

Comments
 (0)