Skip to content

Commit

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

{ #category : #operations }
UnifiedDiffChangeChunk >> applyTo: input [
| lineNumber newContentsLines |
lineNumber := 0.
newContentsLines := self newContentsLines.
^ String streamContents: [ :out |
input linesDo: [ :line |
lineNumber := lineNumber + 1.
(lineNumber between: self oldStart and: self oldEnd)
ifTrue: [
| diffLine |
diffLine := newContentsLines at: lineNumber - self oldStart + 1.
out next: diffLine size - 1 putAll: diffLine startingAt: 2; cr ]
ifFalse: [
out nextPutAll: line; cr ] ] ]
]

{ #category : #accessing }
UnifiedDiffChangeChunk >> children [
^ #()
Expand Down Expand Up @@ -66,9 +83,9 @@ UnifiedDiffChangeChunk >> newContentsLineAt: lineNumber [
{ #category : #accessing }
UnifiedDiffChangeChunk >> newContentsLineAt: lineNumber ifAbsent: absentBlock [
| newPosition |
(lineNumber between: newStart and: newStart + newCount - 1)
(lineNumber between: self newStart and: self newEnd)
ifFalse: [ ^ absentBlock value ].
newPosition := newStart.
newPosition := self newStart.
diffLines do: [ :line |
(line first = $+ and: [ newPosition = lineNumber ])
ifTrue: [ ^ line allButFirst ].
Expand All @@ -82,6 +99,21 @@ UnifiedDiffChangeChunk >> newContentsLines [
^ diffLines reject: [ : line | line first = $- ]
]

{ #category : #accessing }
UnifiedDiffChangeChunk >> newCount [
^ newCount
]

{ #category : #accessing }
UnifiedDiffChangeChunk >> newEnd [
^ newStart + newCount - 1
]

{ #category : #accessing }
UnifiedDiffChangeChunk >> newStart [
^ newStart
]

{ #category : #accessing }
UnifiedDiffChangeChunk >> numberOfLinesAdded [
^ diffLines count: [ :each | each first = $+ ]
Expand Down Expand Up @@ -130,9 +162,9 @@ UnifiedDiffChangeChunk >> oldContentsLineAt: lineNumber [
{ #category : #accessing }
UnifiedDiffChangeChunk >> oldContentsLineAt: lineNumber ifAbsent: absentBlock [
| oldPosition |
(lineNumber between: oldStart and: oldStart + oldCount - 1)
(lineNumber between: self oldStart and: self oldEnd)
ifFalse: [ ^ absentBlock value ].
oldPosition := oldStart.
oldPosition := self oldStart.
diffLines do: [ :line |
(line first = $- and: [ oldPosition = lineNumber ])
ifTrue: [ ^ line allButFirst ].
Expand All @@ -146,6 +178,21 @@ UnifiedDiffChangeChunk >> oldContentsLines [
^ diffLines reject: [ : line | line first = $+ ]
]

{ #category : #accessing }
UnifiedDiffChangeChunk >> oldCount [
^ oldCount
]

{ #category : #accessing }
UnifiedDiffChangeChunk >> oldEnd [
^ oldStart + oldCount - 1
]

{ #category : #accessing }
UnifiedDiffChangeChunk >> oldStart [
^ oldStart
]

{ #category : #printing }
UnifiedDiffChangeChunk >> printOn: stream [
super printOn: stream.
Expand Down
88 changes: 88 additions & 0 deletions src/Iceberg-Git-CLI/UnifiedDiffExamples.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,94 @@ Class {
#category : #'Iceberg-Git-CLI-Pure'
}

{ #category : #accessing }
UnifiedDiffExamples >> exampleBarContents [
<gtExample>
^ 'A
B
C
D
e
F
x
G
I
J
K
L
'
]

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

| unifiedDiff changeChunk output |

unifiedDiff := UnifiedDiff new
readFrom: self exampleFooBarGitUnifiedDiffOutput readStream.

changeChunk := unifiedDiff changeChunks first.

self
assert: changeChunk oldContents
equals: ((self exampleFooContents lines copyFrom: changeChunk oldStart to: changeChunk oldEnd)
flatCollect: [ :each | each , String cr ] as: String).
self
assert: changeChunk newContents
equals: ((self exampleBarContents lines copyFrom: changeChunk newStart to: changeChunk newEnd)
flatCollect: [ :each | each , String cr ] as: String).

output := changeChunk applyTo: self exampleFooContents.

self assert: output equals: self exampleBarContents.

^ unifiedDiff
]

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

"git diff foo.txt bar.txt"

^ 'diff --git a/foo.txt b/bar.txt
index 475f87b..d7f6539 100644
--- a/foo.txt
+++ b/bar.txt
@@ -2,10 +2,10 @@ A
B
C
D
-E
+e
F
+x
G
-H
I
J
K'
]

{ #category : #accessing }
UnifiedDiffExamples >> exampleFooContents [
<gtExample>
^ 'A
B
C
D
E
F
G
H
I
J
K
L
'
]

{ #category : #accessing }
UnifiedDiffExamples >> exampleGitUnifiedDiff [
<gtExample>
Expand Down

0 comments on commit a8b5011

Please sign in to comment.