Skip to content

Commit

Permalink
First version of UnifiedDiff>>#applyTo: with example
Browse files Browse the repository at this point in the history
  • Loading branch information
svenvc committed Oct 25, 2024
1 parent a8b5011 commit 1174372
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/Iceberg-Git-CLI/UnifiedDiff.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,26 @@ Class {
#category : #'Iceberg-Git-CLI-Pure'
}

{ #category : #operations }
UnifiedDiff >> applyTo: input [
| lines lineNumber|
lines := input lines.
lineNumber := 1.
^ String streamContents: [ :out |
self changeChunks do: [ :changeChunk |
[ lineNumber < changeChunk oldStart ]
whileTrue: [
out nextPutAll: (lines at: lineNumber); cr.
lineNumber := lineNumber + 1 ].
changeChunk newContentsLines do: [ :line |
out next: line size - 1 putAll: line startingAt: 2; cr ].
lineNumber := lineNumber + changeChunk oldCount ].
[ lineNumber <= lines size ]
whileTrue: [
out nextPutAll: (lines at: lineNumber); cr.
lineNumber := lineNumber + 1 ] ]
]

{ #category : #accessing }
UnifiedDiff >> changeChunks [
^ changeChunks
Expand Down
73 changes: 73 additions & 0 deletions src/Iceberg-Git-CLI/UnifiedDiffExamples.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,65 @@ to this document.
'
]

{ #category : #accessing }
UnifiedDiffExamples >> exampleOneContents [
<gtExample>
^ ($A to: $Z) flatCollect: [ :each | String with: each with: Character cr ] as: String
]

{ #category : #accessing }
UnifiedDiffExamples >> exampleOneTwoGitUnifiedDiff [
<gtExample>

| unifiedDiff output |
unifiedDiff := UnifiedDiff new
readFromLines: self exampleOneTwoGitUnifiedDiffOutput lines readStream.

output := unifiedDiff applyTo: self exampleOneContents.

self assert: output equals: self exampleTwoContents.

^ unifiedDiff
]

{ #category : #accessing }
UnifiedDiffExamples >> exampleOneTwoGitUnifiedDiffOutput [
<gtExample>

"git diff foo.txt bar.txt"

^ 'diff --git a/foo.txt b/bar.txt
index a6f1d23..c5f54f4 100644
--- a/foo.txt
+++ b/bar.txt
@@ -2,7 +2,7 @@ A
B
C
D
-E
+e
F
G
H
@@ -10,6 +10,7 @@ I
J
K
L
+x
M
N
O
@@ -18,7 +19,6 @@ Q
R
S
T
-U
V
W
X
'
]

{ #category : #accessing }
UnifiedDiffExamples >> exampleOriginalContents [
<gtExample>
Expand Down Expand Up @@ -312,3 +371,17 @@ UnifiedDiffExamples >> exampleStandardUnifiedDiffOutput [
+to this document.
'
]

{ #category : #accessing }
UnifiedDiffExamples >> exampleTwoContents [
<gtExample>
| alphabet |
alphabet := ($A to: $Z) asOrderedCollection.
"modify: $E -> $e"
alphabet at: 5 put: $e.
"delete: $U"
alphabet removeAt: 21.
"add: $x after $L"
alphabet add: $x afterIndex: 12.
^ alphabet flatCollect: [ :each | String with: each with: Character cr ] as: String
]

0 comments on commit 1174372

Please sign in to comment.