Andreas Gohr
Andreas Gohr

Reputation: 4945

3-way merge in JavaScript or PHP

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.

Upvotes: 8

Views: 2873

Answers (4)

Max Hudson
Max Hudson

Reputation: 10226

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();

Upvotes: 0

Krassmus
Krassmus

Reputation: 359

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.

Upvotes: 1

user207825
user207825

Reputation: 71

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.

Upvotes: 6

Beni Cherniavsky-Paskin
Beni Cherniavsky-Paskin

Reputation: 10039

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

Upvotes: 3

Related Questions