From ebab524e5dc1b778916aada279db771f3a32ffc3 Mon Sep 17 00:00:00 2001 From: Andre D Date: Fri, 24 Aug 2012 11:42:02 -0500 Subject: [PATCH] Diff3: Added diff3 wrapper --- r2/example.ini | 4 ++++ r2/r2/lib/merge.py | 56 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 r2/r2/lib/merge.py diff --git a/r2/example.ini b/r2/example.ini index 9802e384e..4d167514f 100644 --- a/r2/example.ini +++ b/r2/example.ini @@ -477,6 +477,10 @@ feedback_email = reddit@gmail.com # Special case sensitive domains case_sensitive_domains = i.imgur.com, youtube.com +# Location (directory) for temp files for diff3 merging +# Empty will use python default for temp files +diff3_temp_location = /dev/shm + [server:main] use = egg:Paste#http host = 0.0.0.0 diff --git a/r2/r2/lib/merge.py b/r2/r2/lib/merge.py new file mode 100644 index 000000000..4d5d44f04 --- /dev/null +++ b/r2/r2/lib/merge.py @@ -0,0 +1,56 @@ +import subprocess +import tempfile +import difflib +from pylons.i18n import _ +from pylons import g + +class ConflictException(Exception): + def __init__(self, new, your, original): + self.your = your + self.new = new + self.original = original + self.htmldiff = make_htmldiff(new, your, _("current edit"), _("your edit")) + Exception.__init__(self) + + +def make_htmldiff(a, b, adesc, bdesc): + diffcontent = difflib.HtmlDiff(wrapcolumn=60) + return diffcontent.make_table(a.splitlines(), + b.splitlines(), + fromdesc=adesc, + todesc=bdesc) + +def threeWayMerge(original, a, b): + try: + temp_dir = g.diff3_temp_location if g.diff3_temp_location else None + data = [a, original, b] + files = [] + for d in data: + f = tempfile.NamedTemporaryFile(dir=temp_dir) + f.write(d.encode('utf-8')) + f.flush() + files.append(f) + try: + final = subprocess.check_output(["diff3", "-a", "--merge"] + [f.name for f in files]) + except subprocess.CalledProcessError: + raise ConflictException(b, a, original) + finally: + for f in files: + f.close() + return final.decode('utf-8') + +if __name__ == "__main__": + class test_globals: + diff3_temp_location = None + + g = test_globals() + + original = "Hello people of the human rance\n\nHow are you tday" + a = "Hello people of the human rance\n\nHow are you today" + b = "Hello people of the human race\n\nHow are you tday" + + print threeWayMerge(original, a, b) + + g.diff3_temp_location = '/dev/shm' + + print threeWayMerge(original, a, b)