diff --git a/yokadi/tests/conflictutilstestcase.py b/yokadi/tests/conflictutilstestcase.py index 33148419..82086513 100644 --- a/yokadi/tests/conflictutilstestcase.py +++ b/yokadi/tests/conflictutilstestcase.py @@ -6,14 +6,24 @@ """ import textwrap -from yokadi.ycli.conflictutils import prepareConflictText +from yokadi.ycli.conflictutils import prepareConflictText, CONFLICT_BEGIN, CONFLICT_MIDDLE, CONFLICT_END from yokadi.tests.yokaditestcase import YokadiTestCase class ConflictUtilsTestCase(YokadiTestCase): def testPrepareConflictText(self): data = ( - ("foo", "bar", "L> foo\nR> bar\n"), + ( + "foo", + "bar", + textwrap.dedent("""\ + {begin} + foo + {mid} + bar + {end} + """.format(begin=CONFLICT_BEGIN, mid=CONFLICT_MIDDLE, end=CONFLICT_END)), + ), ( textwrap.dedent("""\ Common @@ -30,15 +40,24 @@ def testPrepareConflictText(self): Even more common"""), textwrap.dedent("""\ Common - L> Local1 - R> Remote1 + {begin} + Local1 + {mid} + Remote1 + {end} More common - R> Remote2 - L> Local2 - L> Local3 + {begin} + {mid} + Remote2 + {end} + {begin} + Local2 + Local3 + {mid} + {end} Even more common - """), - ) + """.format(begin=CONFLICT_BEGIN, mid=CONFLICT_MIDDLE, end=CONFLICT_END)), + ), ) for local, remote, expected in data: diff --git a/yokadi/ycli/conflictutils.py b/yokadi/ycli/conflictutils.py index 423bfcce..b9cc09af 100644 --- a/yokadi/ycli/conflictutils.py +++ b/yokadi/ycli/conflictutils.py @@ -7,26 +7,42 @@ from difflib import Differ -LOCAL_PREFIX = "L> " -REMOTE_PREFIX = "R> " +CONFLICT_BEGIN = "<<< LOCAL" +CONFLICT_MIDDLE = "===" +CONFLICT_END = ">>> REMOTE" + +_TRANSITIONS = { + (" ", "-"): [CONFLICT_BEGIN], + (" ", "+"): [CONFLICT_BEGIN, CONFLICT_MIDDLE], + ("-", " "): [CONFLICT_MIDDLE, CONFLICT_END], + ("-", "+"): [CONFLICT_MIDDLE], + ("+", " "): [CONFLICT_END], + ("+", "-"): [CONFLICT_END, CONFLICT_BEGIN], +} + + +def _switchToState(state, newState): + if state == newState: + return [] + return [x + "\n" for x in _TRANSITIONS[(state, newState)]] def prepareConflictText(local, remote): differ = Differ() diff = differ.compare(local.splitlines(keepends=True), remote.splitlines(keepends=True)) + state = " " lines = [] for line in diff: - code = line[0] + newState = line[0] rest = line[2:] if rest[-1] != "\n": rest += "\n" - if code == "?": + if newState == "?": continue - if code == "-": - lines.append(LOCAL_PREFIX + rest) - elif code == "+": - lines.append(REMOTE_PREFIX + rest) else: + lines.extend(_switchToState(state, newState)) + state = newState lines.append(rest) + lines.extend(_switchToState(state, " ")) return "".join(lines)