#! /usr/bin/python # ## Copyright (C) 2007 Mark Wooding ## Do with this as you will; attribution would be nice but is not ## essential. There is no warranty, not even of any kind. import sys as SYS import os as OS import subprocess as PROC import errno as E left, base, right = SYS.argv[1:] if left != OS.path.abspath('.hgtags'): next = OS.environ.get('HGMERGE_TAGS_NEXT', 'hgmerge') OS.execvp(next, [next] + SYS.argv[1:]) tagmap = {} def read(f, s): try: for line in file(f): if line.endswith('\n'): line = line[:-1] rev, name = line.split(None, 1) tagmap.setdefault(name, [None, None, None])[s] = rev except OSError, err: if err.errno != E.ENOENT: raise read(left, 0) read(base, 1) read(right, 2) seq = 1 while True: outname = left + '.' + str(seq) if not OS.path.exists(outname): break seq += 1 out = file(outname, 'w') confl = [] for name, val in tagmap.iteritems(): if val[0] == val[2]: merge = val[0] elif val[0] == val[1]: merge = val[2] elif val[2] == val[1]: merge = val[0] else: confl.append((name, val)) merge = None if merge is not None: out.write('%s %s\n' % (merge, name)) if not confl: out.close() else: out.write('*** CONFLICTS ***\n') for sep, f, i in ('<', left, 0), ('=', base, 1), ('=', right, 2): out.write(sep * 6 + ' ' + f + '\n') for name, val in confl: out.write('%s %s\n' % (name, val[i])) out.write('>' * 6 + ' ' + '\n') out.close() qn = "'" + ''.join((ch == "'" and "'\\''" or ch for ch in outname)) + "'" ed = OS.environ.get('VISUAL', OS.environ.get('EDITOR', 'vi')) editor = PROC.Popen(['sh', '-c', '%s %s' % (ed, qn)]) stat = editor.wait() if stat != 0: SYS.stderr.write('hgmerge-tags: editor failed (status = %d)' % stat) OS.unlink(outname) SYS.exit(1) for i in file(outname): if len(i) and i[0] in '*<=>': SYS.stderr.write('hgmerge-tags: user couldn\'t fix the merge') OS.unlink(outname) SYS.exit(1) OS.rename(outname, left) SYS.exit(0)