Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically merge text files with potential conflicts (ala git or svn, etc)?

As part of a larger project, I want the ability to take two bodies of text and hand them to a merge algorithm which returns either an auto-merged result (in cases where the changes are not conflicting) or throws an error and (potentially) produces a single text document with the conflicting changes highlighted.

Basically, I just want a programmatic way to do what every source control system on the planet does internally, but I'm having a hard time finding it. There are tons of visual GUIs for doing this sort of thing that dominate my search results, but none of them seem to make easily accessible the core merging algorithm. Does everyone rely on some common and well understood algorithm/library and I just don't know the name so I'm having a hard time searching for it? Is this some just minor tweak on diff and I should be looking for diff libraries instead of merge libraries?

Python libraries would be most helpful, but I can live with the overhead of interfacing with some other library (or command line solution) if I have to; this operation should be relatively infrequent.

like image 278
drewww Avatar asked Oct 31 '10 22:10

drewww


2 Answers

You're probably searching for merge algorithms like 3-way merging, which you can find in many open source projects, e.g. in the bazaar VCS (merge3.py source).

like image 95
AndiDog Avatar answered Oct 15 '22 05:10

AndiDog


Did you check out difflib

  • http://docs.python.org/library/difflib.html
like image 1
pyfunc Avatar answered Oct 15 '22 05:10

pyfunc