3-way merge in JavaScript or PHP [closed]
Asked Answered
F

4

8

Does anyone know of an Open Source three-way merge implementation in JavaScript or PHP? I want to merge plain text files without the need to rely on any server side binaries.

I found a few solutions for creating diffs, but no merge implementations.

Fascia answered 20/11, 2009 at 9:23 Comment(0)
I
3

Synchrotron looks good. E.g. see the demo of three-way merge and conflict-handling.

Illaudable answered 11/5, 2010 at 10:37 Comment(1)
Synchrotron seemed to have some bugs when merging for me, but it may work for you. I'd be interested if there are any other JavaScript librariesEstrous
K
6

Not exactly three-way merge, but Google's "Diff Match and Patch libraries offer robust algorithms to perform the operations required for synchronizing plain text". And the implementation is available in Java, JavaScript, C++, C#, Lua and Python.

Kirwin answered 17/9, 2010 at 8:28 Comment(1)
This is the best answer. While I understand links are not desired on SO, the authors of diff-match-patch provide an example of a 3-way merge that may be helpful for others using this library: neil.fraser.name/software/diff_match_patch/demos/patch.htmlHalsy
I
3

Synchrotron looks good. E.g. see the demo of three-way merge and conflict-handling.

Illaudable answered 11/5, 2010 at 10:37 Comment(1)
Synchrotron seemed to have some bugs when merging for me, but it may work for you. I'd be interested if there are any other JavaScript librariesEstrous
B
1

Just finished my work on such a js- and php-tool. Have a look and enjoy:

https://github.com/Krassmus/Textmerger

You'd just need to write

var merged = Textmerger.get().merge(original, mytext, theirtext);

or in PHP

$merged = Textmerger::get()->merge($original, $mytext, $theirtext);

and you're done.

Bethink answered 12/9, 2016 at 17:54 Comment(0)
S
0

After finding that the npm packages 3-way-merge and three-way-merge (the main ones that show up in search results for "three way merge npm") didn't work well, and are not maintained, I stumbled upon this much larger more helpful

https://www.npmjs.com/package/diff3

We ended up writing our solution like so:

var diff3 = require('diff3');
var mergeData = diff3(sourceArray, originalArray, targetArray);

var result = mergeData.map(({ok, conflict}) => {
  if (ok) {
    return ok;
  }
  else if (conflict) {
    return [
      '<<<<<<< target',
      ...conflict.b,
      '>>>>>>> target',
      '<<<<<<< source',
      ...conflict.a,
      '>>>>>>> source'
    ];
  }
}).flat();
Spirited answered 11/10, 2023 at 19:26 Comment(1)
We are meant to close (and not answer) questions which seek software recommendations.Simmie

© 2022 - 2024 — McMap. All rights reserved.