diff --git a/README.md b/README.md index 64b393d..7a4098a 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,6 @@ on associative arrays, written in PHP. This library is built to perform a recursive 3-way merge algorithm. It takes 3 parameters which are arrays representing base array, local array and remote array. It compares each of these entities with other arrays line-wise. If only one out of remote or local is updated out of these 3, the final revision will have all the unchanged data in it along with the update data from the update array (Either remote or local). If more than one array is updated on the same line, it'd throw a `ConflictException`. - ## Install The library can be installed via [composer](http://getcomposer.org). diff --git a/src/ThreeWayMerge.php b/src/ThreeWayMerge.php index ce7ff0b..5f4bd81 100755 --- a/src/ThreeWayMerge.php +++ b/src/ThreeWayMerge.php @@ -35,13 +35,16 @@ public function performMerge(array $ancestor, array $local, array $remote) $remote[$key], $key ); + if ($merged[$key] == -1) { + throw new ConflictException("Conflict in Key $key"); + } } elseif (array_key_exists($key, $local)) { if ($ancestor[$key] != $local[$key]) { - throw new ConflictException("A conflict has occured"); + throw new ConflictException("$key not same in Ancestor and Local"); } } elseif (array_key_exists($key, $remote)) { if ($ancestor[$key] != $remote[$key]) { - throw new ConflictException("A conflict has occured"); + throw new ConflictException("$key not same in Ancestor and Remote"); } } else { unset($merged[$key]); @@ -93,6 +96,9 @@ protected function merge($x, $y, $z, $key) $count_local, $count_remote ); + if ($merged == -1) { + return -1; + } } else { // Store the updated count value in a variable $count. if ($count_ancestor == $count_local) { @@ -112,6 +118,9 @@ protected function merge($x, $y, $z, $key) $count_ancestor, $count_local ); + if ($merged == -1) { + return -1; + } } else { $merged = $this->linesRemovedOrModified( $ancestor, @@ -121,6 +130,9 @@ protected function merge($x, $y, $z, $key) $count_local, $count_remote ); + if ($merged == -1) { + return -1; + } } } // Convert returned array back to string. @@ -164,7 +176,7 @@ protected function linesAddedOrModified( || $local[$key] == $remote[$key]) { $merged[$key] = $local[$key]; } else { - throw new ConflictException("A conflict has occured"); + return -1; } } // Once done with ancestor lines, we have hunk of @@ -180,7 +192,7 @@ protected function linesAddedOrModified( if ($local[$i] == $remote[$i]) { $merged[$i] = $local[$i]; } else { - throw new ConflictException("A conflict has occured"); + return -1; } } } @@ -223,7 +235,7 @@ protected function linesRemovedOrModified( || $local[$key] == $remote[$key]) { $merged[$key] = $local[$key]; } else { - throw new ConflictException("A conflict has occured"); + return -1; } } @@ -232,7 +244,7 @@ protected function linesRemovedOrModified( throw new ConflictException("A whole new conflict arised"); } elseif ($mincount == $count_remote && $ancestor[$key] != $local[$key]) { - throw new ConflictException("A whole new conflict arised"); + return -1; } } return $merged; @@ -276,7 +288,7 @@ protected function linesAddedRemovedAndModified( || $local[$key] == $remote[$key]) { $merged[$key] = $local[$key]; } else { - throw new ConflictException("A conflict has occured"); + return -1; } } @@ -289,7 +301,7 @@ protected function linesAddedRemovedAndModified( if ($local[$key] == $remote[$key]) { $merged[$key] = $local[$key]; } else { - throw new ConflictException("A conflict has occured"); + return -1; } } elseif ($count_local == $mincount && ($count_ancestor == $maxcount @@ -300,7 +312,7 @@ protected function linesAddedRemovedAndModified( unset($merged[$key]); } } else { - throw new ConflictException("A conflict has occured"); + return -1; } } elseif ($count_remote == $mincount && ($count_ancestor == $maxcount @@ -310,7 +322,7 @@ protected function linesAddedRemovedAndModified( unset($merged[$key]); } } else { - throw new ConflictException("A conflict has occured"); + return -1; } } }