Skip to content

Commit

Permalink
Fix merging of empty strings
Browse files Browse the repository at this point in the history
Fixes #45.
  • Loading branch information
MariusDoe committed Nov 17, 2024
1 parent 9dd6e56 commit 745f02a
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
merging
stringMergeLeft: leftString right: rightString base: baseString ifUnchanged: unchangedBlock ifMerged: mergedBlock ifConflict: conflictBlock
| leftLines diffResult |
| leftLines chunks |
leftLines := leftString lines.
diffResult := Diff3 new
chunks := (Diff3 new
file0: baseString lines;
file1: leftLines;
file2: rightString lines;
diffClass: GSTextUtilities diffClass;
merge: true.
^ (diffResult size = 1 and: [diffResult first key = #ok])
yourself) merge: true.
^ (chunks allSatisfy: [:each | each key = #ok])
ifFalse: [conflictBlock value: (GSMergeConflict left: leftString right: rightString base: baseString)]
ifTrue: [ | mergedLines |
mergedLines := diffResult first value.
mergedLines := chunks gather: #value.
mergedLines = leftLines
ifTrue: [unchangedBlock value]
ifFalse: [mergedBlock value: (mergedLines joinSeparatedBy: Character cr)]]
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"atomicMergeLeft:right:base:ifUnchanged:ifMerged:ifConflict:" : "mad 10/9/2024 16:01",
"hexHashMergeFor:left:right:base:ifUnchanged:" : "mad 11/12/2024 15:02",
"stringMergeLeft:right:base:ifConflict:" : "mad 10/2/2024 20:57",
"stringMergeLeft:right:base:ifUnchanged:ifMerged:ifConflict:" : "mad 10/9/2024 16:01",
"stringMergeLeft:right:base:ifUnchanged:ifMerged:ifConflict:" : "mad 11/17/2024 23:00",
"stringOrNilMergeLeft:right:base:ifUnchanged:ifMerged:ifConflict:" : "mad 10/2/2024 20:56" },
"instance" : {
} }
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
diffing
diffChunksFrom: fromCollection to: toCollection
(fromCollection size = 0 and: [toCollection size = 0]) ifTrue: [
"MyersUkkonenDiff doesn't properly handle this case"
^ {#common -> fromCollection}].
^ self diffClass new
file1: fromCollection;
file2: toCollection;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"asRemovalText:" : "mad 9/10/2023 18:09",
"bold:" : "mad 9/27/2023 16:03",
"colorText:with:" : "mad 9/10/2023 18:09",
"diffChunksFrom:to:" : "mad 10/4/2024 16:01",
"diffChunksFrom:to:" : "mad 11/17/2024 22:57",
"diffClass" : "mad 10/2/2024 18:29",
"diffFrom:to:" : "mad 9/20/2023 13:05",
"diffFromLines:toLines:" : "mad 9/20/2023 13:05",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
*GitS-Core-private-override
calculateLcs
"I find one of the longest common subsequences of my the arguments. I assume that none of my arguments are empty. I return nil or an Array which represents a list. The first two elements are the matching line numbers, the last is the next node in the list or nil if there are no more elements. The list containts the longest common subsequence. I'm a modified version of the greedy lcs algorithm from the 6th page of 'An O(ND) Difference Algorithm and Its Variations (1986)' by Eugene W. Myers"

| n m v lcss max |
n := file1 size.
m := file2 size.
max := m + n.
max = 0 ifTrue: [^ nil].
v := Array new: 2 * max + 1.
v at: max + 2 put: 0.
lcss := Array new: 2 * max + 1.
0 to: max do: [ :d |
d negated to: d by: 2 do: [ :k |
| index chain x y |
(k + d = 0 or: [ k ~= d and: [ (v at: max + k ) < (v at: max + k + 2) ] ])
ifTrue: [
index := max + k + 2.
x := v at: index ]
ifFalse: [
index := max + k.
x := (v at: index) + 1 ].
chain := lcss at: index.
y := x - k.
[ x < n and: [ y < m and: [ (file1 at: x + 1) = (file2 at: y + 1) ] ] ]
whileTrue: [
chain := { x := x + 1. y := y + 1. chain } ].
(x >= n and: [ y >= m ]) ifTrue: [
^chain ].
v at: max + k + 1 put: x.
lcss at: max + k + 1 put: chain ] ].
self error
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"class" : {
},
"instance" : {
"calculateLcs" : "mad 11/16/2024 13:52" } }
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
"name" : "MyersUkkonenDiff" }

0 comments on commit 745f02a

Please sign in to comment.