两个冲突块从第几行开始
找出带标记文件里每个 <<<<<<< 的行号(从 1 数起)。运行下面这段程序:
def classify(b, o, t):
if o == t:
return "same"
if o == b:
return "theirs"
if t == b:
return "ours"
return "conflict"
def merge3(base, ours, theirs):
out = []
for b, o, t in zip(base, ours, theirs):
k = classify(b, o, t)
if k == "conflict":
out.append("<<<<<<< ours")
out.append(o)
out.append("=======")
out.append(t)
out.append(">>>>>>> theirs")
elif k == "theirs":
out.append(t)
else:
out.append(o)
return out
base = ['HOST = "localhost"', 'PORT = 8000', 'DEBUG = False',
'TIMEOUT = 30', 'RETRY = 1', 'LOG = "app.log"']
ours = ['HOST = "localhost"', 'PORT = 8080', 'DEBUG = True',
'TIMEOUT = 60', 'RETRY = 1', 'LOG = "main.log"']
theirs = ['HOST = "0.0.0.0"', 'PORT = 9000', 'DEBUG = True',
'TIMEOUT = 30', 'RETRY = 3', 'LOG = "sys.log"']
m = merge3(base, ours, theirs)
print("/".join(str(i) for i, l in enumerate(m, 1)
if l.startswith("<<<")))
全部评论