Skip to content

Commit

Permalink
Use line-based conflict markers
Browse files Browse the repository at this point in the history
  • Loading branch information
agateau committed Jan 19, 2017
1 parent 899fe48 commit d3cbb27
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 17 deletions.
37 changes: 28 additions & 9 deletions yokadi/tests/conflictutilstestcase.py
Expand Up @@ -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
Expand All @@ -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:
Expand Down
32 changes: 24 additions & 8 deletions yokadi/ycli/conflictutils.py
Expand Up @@ -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)

0 comments on commit d3cbb27

Please sign in to comment.