五项一起对得上吗
把这条路线算过的东西一次验完。运行下面这段程序:
def classify(b, o, t):
if o == t:
return "same"
if o == b:
return "theirs"
if t == b:
return "ours"
return "conflict"
def kinds(base, ours, theirs):
return [classify(b, o, t)
for b, o, t in zip(base, ours, theirs)]
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
def is_marker(line):
return line.startswith(("<<<<<<<", "=======", ">>>>>>>"))
def resolve(merged, take):
"""take: "ours" 取上半段,"theirs" 取下半段,"both" 两段都留"""
out = []
i = 0
while i < len(merged):
if merged[i].startswith("<<<<<<<"):
sep = merged.index("=======", i)
end = merged.index(">>>>>>>" + " theirs", i)
mine = merged[i + 1:sep]
yours = merged[sep + 1:end]
if take == "ours":
out += mine
elif take == "theirs":
out += yours
else:
out += mine + yours
i = end + 1
else:
out.append(merged[i])
i += 1
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"']
k = kinds(base, ours, theirs)
m = merge3(base, ours, theirs)
r = resolve(m, "theirs")
ok = (k.count("conflict") == 2
and len(m) == 14
and sum(1 for l in m if is_marker(l)) == 6
and len(r) == 6
and not any(is_marker(l) for l in r))
print(ok)
全部评论