Commits

edgimar committed 08a8e2e

- add diff3 wrapper to allow diffing text strings

Comments (0)

Files changed (1)

+"""
+Wrapper around diff3 to allow merging text strings.
+
+"""
+import tempfile
+import subprocess
+
+def merge3_text(my_text, orig_text, other_text):
+    "Perform diff3 merge on the text-string arguments."
+    
+    # create temp files for operating on with diff3 tool
+    my_file = tempfile.NamedTemporaryFile()
+    orig_file = tempfile.NamedTemporaryFile()
+    other_file = tempfile.NamedTemporaryFile()
+    
+    # write text strings to files
+    my_file.write(my_text)
+    my_file.flush()
+    
+    orig_file.write(orig_text)
+    orig_file.flush()
+    
+    other_file.write(other_text)
+    other_file.flush()
+
+    # call the diff3 executable, and collect results
+    p = subprocess.Popen([
+                             'diff3', 
+                             '-m',
+                             '-L', 'MINE',
+                             '-L', 'ORIGINAL',
+                             '-L', 'OTHER',
+                             my_file.name,
+                             orig_file.name,
+                             other_file.name
+                         ],
+                        stdout=subprocess.PIPE,
+                        stderr=subprocess.PIPE)
+    stdout_text, stderr_text = p.communicate()
+    retcode = p.wait()
+    
+    # evaluate results
+    if retcode == 0: # no conflicts; successful merge
+        was_conflict = False
+    elif retcode == 1: # there was a conflict
+        was_conflict = True
+    else: # merge failed
+        was_conflict = True
+
+    return stdout_text, was_conflict
+
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.