X-Git-Url: https://scripts.mit.edu/gitweb/wizard.git/blobdiff_plain/220b036751ee12ff5dce564d93330bdb6f9ce903..ee8e5d33462267936f1b03e1d22c075c65d36854:/wizard/resolve.py diff --git a/wizard/resolve.py b/wizard/resolve.py index e2de1f2..9259c24 100644 --- a/wizard/resolve.py +++ b/wizard/resolve.py @@ -59,6 +59,7 @@ then the user matched globs. import re import itertools +import logging re_var = re.compile("^\*\*\*(\d+)\*\*\*\\\n", re.MULTILINE) @@ -97,7 +98,46 @@ def result_to_repl(result, mappings): def resolve(contents, spec, result): rstring, mappings = spec_to_regex(spec) - print rstring regex = re.compile(rstring, re.DOTALL) repl = result_to_repl(result, mappings) - return regex.sub(repl, contents) + ret = "" + conflict = "" + status = 0 + for line in contents.splitlines(True): + if status == 0 and line.startswith("<<<<<<<"): + status = 1 + elif status == 1 and line.startswith("======="): + status = 2 + # ok, now process + if status == 2 and line.startswith(">>>>>>>"): + status = 0 + conflict += line + ret += regex.sub(repl, conflict) + conflict = "" + elif status: + conflict += line + else: + ret += line + return ret + +def is_conflict(contents): + # Really really simple heuristic + return "<<<<<<<" in contents + +def fix_newlines(file, log=True): + """ + Normalizes newlines in a file into UNIX file endings. If + ``log`` is ``True`` an info log mesage is printed if + any normalization occurs. Return value is ``True`` if + normalization occurred. + """ + old_contents = open(file, "r").read() + contents = old_contents + while "\r\n" in contents: + contents = contents.replace("\r\n", "\n") + contents = contents.replace("\r", "\n") + if contents != old_contents: + logging.info("Converted %s to UNIX file endings" % file) + open(file, "w").write(contents) + return True + return False