This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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) |