Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replaced static exception message with key indicating message #5

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
32 changes: 22 additions & 10 deletions src/ThreeWayMerge.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down Expand Up @@ -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) {
Expand All @@ -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,
Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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;
}
}
}
Expand Down Expand Up @@ -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;
}
}

Expand All @@ -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;
Expand Down Expand Up @@ -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;
}
}

Expand All @@ -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
Expand All @@ -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
Expand All @@ -310,7 +322,7 @@ protected function linesAddedRemovedAndModified(
unset($merged[$key]);
}
} else {
throw new ConflictException("A conflict has occured");
return -1;
}
}
}
Expand Down