Created
January 30, 2017 17:27
-
-
Save elek/7844e7b086fe7f9339768e484819149b to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env python | |
import sys, difflib | |
# The version of the file from the common ancestor of the two branches. | |
# This constitutes the 'base' version of the file. | |
ancestor = open(sys.argv[1],'r').readlines() | |
# The version of the file at the HEAD of the current branch. | |
# The result of the merge should be left in this file by overwriting it. | |
current = open(sys.argv[2],'r').readlines() | |
# The version of the file at the HEAD of the other branch. | |
other = open(sys.argv[3],'r').readlines() | |
def get_difference(one,two): | |
d = difflib.Differ() | |
diff = d.compare(one,two) | |
added_lines = [] | |
for line in diff: | |
if line.startswith('+ '): | |
if not line.strip() == '+ <<<<<<< HEAD': | |
added_lines.append(line[2:]) | |
elif line.startswith('- '): | |
raise DiffError("remove is not supported") | |
return added_lines | |
lines = get_difference(ancestor,other) | |
print(lines) | |
while not current[-1].strip(): | |
current = current[:-1] | |
current.append("\n") | |
while not lines[0].strip(): | |
lines = lines[1:] | |
current.extend(lines) | |
f = open(sys.argv[2],'w') | |
f.writelines(current) | |
f.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment