From 0a13c4969c649ee702459d5c7a79f65faf0cc188 Mon Sep 17 00:00:00 2001 From: Rakesh Verma Date: Sat, 18 Jun 2016 05:46:48 +0530 Subject: [PATCH 01/13] Updated name space in all files --- src/ConflictException.php | 2 +- src/ThreeWayMerge.php | 2 +- tests/ThreeWayMergeTest.php | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/ConflictException.php b/src/ConflictException.php index 77375b9..7b5d70b 100644 --- a/src/ConflictException.php +++ b/src/ConflictException.php @@ -1,6 +1,6 @@ Date: Sat, 18 Jun 2016 05:50:08 +0530 Subject: [PATCH 02/13] Update ThreeWayMerge.php --- src/ThreeWayMerge.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ThreeWayMerge.php b/src/ThreeWayMerge.php index 79efe0c..f0d5500 100755 --- a/src/ThreeWayMerge.php +++ b/src/ThreeWayMerge.php @@ -1,6 +1,6 @@ Date: Sat, 18 Jun 2016 15:28:14 +0530 Subject: [PATCH 03/13] Fixed Key removal errors as well :) --- src/ThreeWayMerge.php | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/src/ThreeWayMerge.php b/src/ThreeWayMerge.php index 79efe0c..6127dc6 100755 --- a/src/ThreeWayMerge.php +++ b/src/ThreeWayMerge.php @@ -1,6 +1,6 @@ $value) { // Checks if the value contains an array itself. - if (is_array($value)) { + if (is_array($value) && array_key_exists($key, $local) && array_key_exists($key, $remote)) { $merged[$key] = $this->performMerge( $value, $local[$key], $remote[$key] ); } else { - $merged[$key] = $this->merge( - $ancestor[$key], - $local[$key], - $remote[$key], - $key - ); - //If a key doesn't have any value, unset the key. - if ($merged[$key] == null) { + if (array_key_exists($key, $local) && array_key_exists($key, $remote)) { + $merged[$key] = $this->merge( + $ancestor[$key], + $local[$key], + $remote[$key], + $key + ); + } elseif (array_key_exists($key, $local)) { + if ($ancestor[$key] != $local[$key]){ + throw new ConflictException("A conflict has occured"); + } + } elseif (array_key_exists($key, $remote)) { + if ($ancestor[$key] != $remote[$key]){ + throw new ConflictException("A conflict has occured"); + } + } else { unset($merged[$key]); } } + //If a key doesn't have any value, unset the key. +// if ($merged[$key] == null) { +// unset($merged[$key]); +// } } return $merged; } @@ -259,7 +271,7 @@ protected function linesAddedRemovedAndModified( sort($count_array); $mincount = min($count_local, $count_ancestor, $count_remote); $maxcount = max($count_local, $count_ancestor, $count_remote); - + // First for loop compares all 3 nodes and returns updated node. for ($key = 0; $key < $mincount; $key++) { if ($ancestor[$key] == $local[$key]) { @@ -271,7 +283,7 @@ protected function linesAddedRemovedAndModified( throw new ConflictException("A conflict has occured"); } } - + // Second for loop compares 2 nodes and if they are identical // add the changes to new array otherwise throw conflict exception. for ($key = $mincount; $key < $count_array[1]; $key++) { From 5eba53229d85ff2b0d2d151c777dbe87d16d514c Mon Sep 17 00:00:00 2001 From: Rakesh Verma Date: Sat, 18 Jun 2016 15:59:49 +0530 Subject: [PATCH 04/13] Fixing travis issues --- src/ThreeWayMerge.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ThreeWayMerge.php b/src/ThreeWayMerge.php index 6127dc6..190d592 100755 --- a/src/ThreeWayMerge.php +++ b/src/ThreeWayMerge.php @@ -36,11 +36,11 @@ public function performMerge(array $ancestor, array $local, array $remote) $key ); } elseif (array_key_exists($key, $local)) { - if ($ancestor[$key] != $local[$key]){ + if ($ancestor[$key] != $local[$key]) { throw new ConflictException("A conflict has occured"); } } elseif (array_key_exists($key, $remote)) { - if ($ancestor[$key] != $remote[$key]){ + if ($ancestor[$key] != $remote[$key]) { throw new ConflictException("A conflict has occured"); } } else { From cebaea1baf229cebac0a8be4df446f6da69ce197 Mon Sep 17 00:00:00 2001 From: Rakesh Verma Date: Sun, 19 Jun 2016 15:05:44 +0530 Subject: [PATCH 05/13] Update README.md --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index c980d85..df0fe2e 100644 --- a/README.md +++ b/README.md @@ -1 +1,4 @@ + +[![Build Status](https://travis-ci.org/relaxedws/merge.svg?branch=master)](https://travis-ci.org/relaxedws/merge) + PHP Library to perform 3 way merge on associative arrays. From 2ffab05e7bcdc8e3c8bd216b382e785ad2d19c20 Mon Sep 17 00:00:00 2001 From: Rakesh Verma Date: Sun, 19 Jun 2016 15:59:58 +0530 Subject: [PATCH 06/13] Update README.md --- README.md | 106 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 104 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index df0fe2e..50b550d 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,106 @@ +# Relaxedws/merge [![Build Status](https://travis-ci.org/relaxedws/merge.svg?branch=master)](https://travis-ci.org/relaxedws/merge) -[![Build Status](https://travis-ci.org/relaxedws/merge.svg?branch=master)](https://travis-ci.org/relaxedws/merge) +A Library to perform recursive 3 way merge algorithm +on associative arrays, written in PHP. + +## Quickstart Example + +After [installation](#install), we can perform a merge the following way: + +````php + [ + 0 => [ + 'keyB' => 'This is honey + like this', + 'keyC' => 'This is however, not apple', + ], + 1 => [ + 'keyB' => 'This is milk', + 'keyC' => 'This is mango', + ], + 2 => 'a little sugar', + ] +]; +$local = [ + 'keyA' => [ + 0 => [ + 'keyB' => 'This is honeybb + like ti', + 'keyC' => 'This is however, not apple', + ], + 1 => [ + 'keyB' => 'This is milky milky', + 'keyC' => 'This is mango', + ], + 2 => 'a little coffee' + ] +]; +$remote = [ + 'keyA' => [ + 0 => [ + 'keyB' => 'This is honey + like this', + 'keyC' => 'This is however, not apple', + ], + 1 => [ + 'keyB' => 'This is milk', + 'keyC' => 'This is changed because of remote', + ], + 2 => 'a little sugar', + ] +]; + +$multiline = new ThreeWayMerge(); +$new_arr = $multiline->performMerge($original, $local, $remote); +```` +## Features + +This library is built to perform a recursive 3 way merge algorithm. It takes 3 parameters which are arrays representing base entity, local entity and remote entity. It compares each of these entities with other entities line by line. +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 entity(Either remote or local). If more than one entity is updated on the same line, it'd throw a `ConflictException`. + +## Install + +The library can be installed via [composer](http://getcomposer.org). + +````JSON +{ + "name": "relaxedws/merge", + "description": "Library used to perform merges between normalized array structures.", + "require-dev": { + "phpunit/phpunit": "~4.0" + }, + "autoload": { + "psr-4": { + "Relaxed\\Merge\\": "src/" + } + }, + "autoload-dev": { + "psr-4": { + "Relaxed\\Merge\\Test\\": "tests/" + } + } +} +```` +## Contributing + +We welcome all developers to come forward for use/test of this library. +Though it's working well for almost all possible scenarios but we all know there are bugs +in every code. Although we keep updating and testing the library regulary, but still if +you encounter any bugs, please feel free to drop us a comment with as much explaination as +possible to reproduce the errors or you can even patch that. + +Other than directly working with the code, you can also help us with better documentation, fixing typo errors +or adding more content to the `readme` file. + +We are open to feedback, suggestions and questions. +We are always present at #drupal8-ports on irc.freenode.net . -PHP Library to perform 3 way merge on associative arrays. From 6c22beb914d9278351931e07a6298c9c194b9678 Mon Sep 17 00:00:00 2001 From: Rakesh Verma Date: Sun, 19 Jun 2016 16:03:02 +0530 Subject: [PATCH 07/13] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 50b550d..c7353b1 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ A Library to perform recursive 3 way merge algorithm on associative arrays, written in PHP. -## Quickstart Example +## Example After [installation](#install), we can perform a merge the following way: From 9d52778d0c6adfa8ed6db32e88b78a151a2c618e Mon Sep 17 00:00:00 2001 From: Rakesh Verma Date: Sun, 19 Jun 2016 16:06:09 +0530 Subject: [PATCH 08/13] Update README.md --- README.md | 57 +++++++++++++++++++++++++++++-------------------------- 1 file changed, 30 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index c7353b1..c618c46 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,36 @@ A Library to perform recursive 3 way merge algorithm on associative arrays, written in PHP. +## Insight + +This library is built to perform a recursive 3 way merge algorithm. It takes 3 parameters which are arrays representing base entity, local entity and remote entity. It compares each of these entities with other entities line by line. +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 entity(Either remote or local). If more than one entity is updated on the same line, it'd throw a `ConflictException`. + + +## Install + +The library can be installed via [composer](http://getcomposer.org). + +````JSON +{ + "name": "relaxedws/merge", + "description": "Library used to perform merges between normalized array structures.", + "require-dev": { + "phpunit/phpunit": "~4.0" + }, + "autoload": { + "psr-4": { + "Relaxed\\Merge\\": "src/" + } + }, + "autoload-dev": { + "psr-4": { + "Relaxed\\Merge\\Test\\": "tests/" + } + } +} +```` + ## Example After [installation](#install), we can perform a merge the following way: @@ -62,34 +92,7 @@ $remote = [ $multiline = new ThreeWayMerge(); $new_arr = $multiline->performMerge($original, $local, $remote); ```` -## Features - -This library is built to perform a recursive 3 way merge algorithm. It takes 3 parameters which are arrays representing base entity, local entity and remote entity. It compares each of these entities with other entities line by line. -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 entity(Either remote or local). If more than one entity is updated on the same line, it'd throw a `ConflictException`. - -## Install - -The library can be installed via [composer](http://getcomposer.org). -````JSON -{ - "name": "relaxedws/merge", - "description": "Library used to perform merges between normalized array structures.", - "require-dev": { - "phpunit/phpunit": "~4.0" - }, - "autoload": { - "psr-4": { - "Relaxed\\Merge\\": "src/" - } - }, - "autoload-dev": { - "psr-4": { - "Relaxed\\Merge\\Test\\": "tests/" - } - } -} -```` ## Contributing We welcome all developers to come forward for use/test of this library. From a5718f77642aa72183615422080e20386dd5f535 Mon Sep 17 00:00:00 2001 From: Rakesh Verma Date: Wed, 22 Jun 2016 14:30:28 +0530 Subject: [PATCH 09/13] Added dynamic exception message --- src/ThreeWayMerge.php | 71 +++++++++++++++++++++++-------------------- 1 file changed, 38 insertions(+), 33 deletions(-) diff --git a/src/ThreeWayMerge.php b/src/ThreeWayMerge.php index 190d592..083b92b 100755 --- a/src/ThreeWayMerge.php +++ b/src/ThreeWayMerge.php @@ -35,22 +35,21 @@ 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]); } } - //If a key doesn't have any value, unset the key. -// if ($merged[$key] == null) { -// unset($merged[$key]); -// } } return $merged; } @@ -69,9 +68,9 @@ public function performMerge(array $ancestor, array $local, array $remote) protected function merge($x, $y, $z, $key) { // Convert the value into array. - $ancestor = (strpos($x, "\n") !== false ? explode("\n", $x) : array($x)); - $local = (strpos($y, "\n") !== false ? explode("\n", $y) : array($y)); - $remote = (strpos($z, "\n") !== false ? explode("\n", $z) : array($z)); + $ancestor = (strpos($x, PHP_EOL) !== false ? explode(PHP_EOL, $x) : array($x)); + $local = (strpos($y, PHP_EOL) !== false ? explode(PHP_EOL, $y) : array($y)); + $remote = (strpos($z, PHP_EOL) !== false ? explode(PHP_EOL, $z) : array($z)); // Count number of lines in value or elements in new formed array. $count_ancestor = count($ancestor); @@ -107,24 +106,30 @@ protected function merge($x, $y, $z, $key) // If $count > $count_ancestor, that means lines have been added. // Otherwise, lines has been removed or modified. if ($count > $count_ancestor) { - $merged = $this->linesAddedOrModified( - $ancestor, - $local, - $remote, - $count, - $count_remote, - $count_ancestor, - $count_local - ); + $merged = $this->linesAddedOrModified( + $ancestor, + $local, + $remote, + $count, + $count_remote, + $count_ancestor, + $count_local + ); + if ($merged == -1) { + return -1; + } } else { - $merged = $this->linesRemovedOrModified( - $ancestor, - $local, - $remote, - $count_ancestor, - $count_local, - $count_remote - ); + $merged = $this->linesRemovedOrModified( + $ancestor, + $local, + $remote, + $count_ancestor, + $count_local, + $count_remote + ); + if ($merged == -1) { + return -1; + } } } // Convert returned array back to string. @@ -168,7 +173,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 @@ -184,7 +189,7 @@ protected function linesAddedOrModified( if ($local[$i] == $remote[$i]) { $merged[$i] = $local[$i]; } else { - throw new ConflictException("A conflict has occured"); + return -1; } } } @@ -227,7 +232,7 @@ protected function linesRemovedOrModified( || $local[$key] == $remote[$key]) { $merged[$key] = $local[$key]; } else { - throw new ConflictException("A conflict has occured"); + return -1; } } @@ -236,10 +241,10 @@ 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; + return $merged; } /** @@ -280,7 +285,7 @@ protected function linesAddedRemovedAndModified( || $local[$key] == $remote[$key]) { $merged[$key] = $local[$key]; } else { - throw new ConflictException("A conflict has occured"); + return -1; } } @@ -314,7 +319,7 @@ protected function linesAddedRemovedAndModified( unset($merged[$key]); } } else { - throw new ConflictException("A conflict has occured"); + return -1; } } } From f885dc2e8d9152ab7b52f624793dae9826fced83 Mon Sep 17 00:00:00 2001 From: Rakesh Verma Date: Wed, 22 Jun 2016 14:49:00 +0530 Subject: [PATCH 10/13] Removed conflicts from file aroused from last pull --- README.md | 46 ---------------------------------------------- 1 file changed, 46 deletions(-) diff --git a/README.md b/README.md index c24f20f..8c5d963 100644 --- a/README.md +++ b/README.md @@ -1,21 +1,12 @@ # Relaxedws/merge [![Build Status](https://travis-ci.org/relaxedws/merge.svg?branch=master)](https://travis-ci.org/relaxedws/merge) -<<<<<<< HEAD A Library to perform recursive 3 way merge algorithm -======= -A Library to perform recursive 3-way merge algorithm ->>>>>>> 014615c9b8cd6a0a289dc80e03b131e1784893e1 on associative arrays, written in PHP. ## Insight -<<<<<<< HEAD This library is built to perform a recursive 3 way merge algorithm. It takes 3 parameters which are arrays representing base entity, local entity and remote entity. It compares each of these entities with other entities line by line. 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 entity(Either remote or local). If more than one entity is updated on the same line, it'd throw a `ConflictException`. -======= -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`. ->>>>>>> 014615c9b8cd6a0a289dc80e03b131e1784893e1 ## Install @@ -24,27 +15,10 @@ The library can be installed via [composer](http://getcomposer.org). ````JSON { -<<<<<<< HEAD - "name": "relaxedws/merge", - "description": "Library used to perform merges between normalized array structures.", - "require-dev": { - "phpunit/phpunit": "~4.0" - }, - "autoload": { - "psr-4": { - "Relaxed\\Merge\\": "src/" - } - }, - "autoload-dev": { - "psr-4": { - "Relaxed\\Merge\\Test\\": "tests/" - } -======= "name": "myorg/mylib", "description": "A library depending on 3-way merge", "require": { "relaxedws/merge": "dev-master", ->>>>>>> 014615c9b8cd6a0a289dc80e03b131e1784893e1 } } ```` @@ -105,32 +79,12 @@ $remote = [ ] ]; -<<<<<<< HEAD -$multiline = new ThreeWayMerge(); -$new_arr = $multiline->performMerge($original, $local, $remote); -======= $merge = new ThreeWayMerge(); $updated_revision = $merge->performMerge($original, $local, $remote); ->>>>>>> 014615c9b8cd6a0a289dc80e03b131e1784893e1 ```` ## Contributing -<<<<<<< HEAD -We welcome all developers to come forward for use/test of this library. -Though it's working well for almost all possible scenarios but we all know there are bugs -in every code. Although we keep updating and testing the library regulary, but still if -you encounter any bugs, please feel free to drop us a comment with as much explaination as -possible to reproduce the errors or you can even patch that. - -Other than directly working with the code, you can also help us with better documentation, fixing typo errors -or adding more content to the `readme` file. - -We are open to feedback, suggestions and questions. -We are always present at #drupal8-ports on irc.freenode.net . - -======= We welcome anyone to use, test, or contribute back to this project. We have extensive test coverage, but as we all know there's always bugs in software. Please file issues or pull requests with your comments or suggestions. ->>>>>>> 014615c9b8cd6a0a289dc80e03b131e1784893e1 From 78d3559bf5ed7dfc7b0fffc31c0d5f76520ff19e Mon Sep 17 00:00:00 2001 From: Rakesh Verma Date: Wed, 22 Jun 2016 14:51:35 +0530 Subject: [PATCH 11/13] Removed conflicts from file aroused from last pull --- README.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 8c5d963..07b973c 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,13 @@ # Relaxedws/merge [![Build Status](https://travis-ci.org/relaxedws/merge.svg?branch=master)](https://travis-ci.org/relaxedws/merge) -A Library to perform recursive 3 way merge algorithm +A Library to perform recursive 3-way merge algorithm on associative arrays, written in PHP. ## Insight -This library is built to perform a recursive 3 way merge algorithm. It takes 3 parameters which are arrays representing base entity, local entity and remote entity. It compares each of these entities with other entities line by line. -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 entity(Either remote or local). If more than one entity is updated on the same line, it'd throw a `ConflictException`. +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 From 8de628da0d27d36a7cc645b7fc74a78467545ca8 Mon Sep 17 00:00:00 2001 From: Rakesh Verma Date: Wed, 22 Jun 2016 14:52:30 +0530 Subject: [PATCH 12/13] Removed new line character from readme file --- README.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/README.md b/README.md index 07b973c..7a4098a 100644 --- a/README.md +++ b/README.md @@ -8,8 +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). From 42dedd2cce342de6f8a59f3d27a516f507eaa0f2 Mon Sep 17 00:00:00 2001 From: Rakesh Verma Date: Wed, 22 Jun 2016 14:58:24 +0530 Subject: [PATCH 13/13] Fixing Travis error --- src/ThreeWayMerge.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/ThreeWayMerge.php b/src/ThreeWayMerge.php index 7f3f2ff..5f4bd81 100755 --- a/src/ThreeWayMerge.php +++ b/src/ThreeWayMerge.php @@ -44,7 +44,8 @@ public function performMerge(array $ancestor, array $local, array $remote) } } elseif (array_key_exists($key, $remote)) { if ($ancestor[$key] != $remote[$key]) { - throw new ConflictException("$key not same in Ancestor and Remote"); } + throw new ConflictException("$key not same in Ancestor and Remote"); + } } else { unset($merged[$key]); }