diff --git a/src/Iceberg-Git-CLI/UnifiedDiff.class.st b/src/Iceberg-Git-CLI/UnifiedDiff.class.st index fde7b89..37e322f 100644 --- a/src/Iceberg-Git-CLI/UnifiedDiff.class.st +++ b/src/Iceberg-Git-CLI/UnifiedDiff.class.st @@ -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 diff --git a/src/Iceberg-Git-CLI/UnifiedDiffExamples.class.st b/src/Iceberg-Git-CLI/UnifiedDiffExamples.class.st index 1d48d12..ead6f67 100644 --- a/src/Iceberg-Git-CLI/UnifiedDiffExamples.class.st +++ b/src/Iceberg-Git-CLI/UnifiedDiffExamples.class.st @@ -235,6 +235,65 @@ to this document. ' ] +{ #category : #accessing } +UnifiedDiffExamples >> exampleOneContents [ + + ^ ($A to: $Z) flatCollect: [ :each | String with: each with: Character cr ] as: String +] + +{ #category : #accessing } +UnifiedDiffExamples >> exampleOneTwoGitUnifiedDiff [ + + + | 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 [ + + + "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 [ @@ -312,3 +371,17 @@ UnifiedDiffExamples >> exampleStandardUnifiedDiffOutput [ +to this document. ' ] + +{ #category : #accessing } +UnifiedDiffExamples >> exampleTwoContents [ + + | 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 +]