]> scripts.mit.edu Git - wizard.git/blob - wizard/resolve.py
Make pull.sh call aklog.
[wizard.git] / wizard / resolve.py
1 """
2 .. highlight:: diff
3
4 This module contains algorithms for performing conflict
5 resolution after Git performs its recursive merge.  It
6 defines a simple domain specific language (that, at
7 its simplest form, merely involves copying conflict markers
8 and writing in the form that they should be resolved as) for
9 specifying how to resolve conflicts.  These are mostly relevant
10 for resolving conflicts in configuration files.
11
12 The conflict resolution DSL is described here:
13
14 Resolutions are specified as input-output pairs.  An input
15 is a string with the conflict resolution markers ("<" * 7,
16 "=" * 7 and ">" * 7), with the HEAD content above the equals
17 divider, and the upstream content below the equals divider.
18 Lines can also be marked as "***N***" where N is a natural
19 number greater than 0 (i.e. 1 or more), which means that
20 an arbitrary number of lines may be matched and available for output.
21
22 Output is a list of integers and strings.  Integers expand
23 to lines that were specified earlier; -1 and 0 are special integers
24 that correspond to the entire HEAD text, and the entire upstream
25 text, respectively.  Strings can be used to insert custom lines.
26
27 The DSL does not currently claim to support character level granularity.
28 It also does not claim to support contiguous conflicts.
29 Our hope is that this simple syntax will be sufficient to cover
30 most common merge failures.
31
32 Here are some examples::
33
34     <<<<<<<
35     downstream
36     =======
37     upstream
38     >>>>>>>
39
40 With ``[-1]`` would discard all upstream changes, whereas with ``[0]``
41 would discard downstream changes (you would probably want to be
42 careful about wildcarding in the upstream string).
43
44 Pattern matching in action::
45
46     <<<<<<<
47     ***1***
48     old upstream
49     ***2***
50     old upstream
51     ***3***
52     =======
53     new upstream
54     >>>>>>>
55
56 With ``[0, 1, 2, 3]`` would resolve with the new upstream text, and
57 then the user matched globs.
58 """
59
60 import re
61 import itertools
62 import logging
63
64 re_var = re.compile("^\*\*\*(\d+)\*\*\*\\\n", re.MULTILINE)
65
66 def spec_to_regex(spec):
67     """
68     Translates a specification string into a regular expression tuple.
69     Note that pattern matches are out of order, so the second element
70     of the tuple is a dict specified strings to subpattern numbers.
71     Requires re.DOTALL for correct operation.
72     """
73     ours, _, theirs = "".join(spec.strip().splitlines(True)[1:-1]).partition("=======\n")
74     def regexify(text, fullmatch, matchno):
75         text_split = re.split(re_var, text)
76         ret = ""
77         mappings = {fullmatch: matchno}
78         for is_var, line in zip(itertools.cycle([False, True]), text_split):
79             if is_var:
80                 ret += "(.*\\\n)"
81                 matchno += 1
82                 mappings[int(line)] = matchno
83             else:
84                 ret += re.escape(line)
85         return ("(" + ret + ")", mappings)
86     ours_regex, ours_mappings = regexify(ours, -1, 1)
87     theirs_regex, theirs_mappings = regexify(theirs, 0, len(ours_mappings) + 1)
88     ours_mappings.update(theirs_mappings)
89     return ("<<<<<<<[^\n]*\\\n" + ours_regex + "=======\\\n" + theirs_regex + ">>>>>>>[^\n]*(\\\n|$)", ours_mappings)
90
91 def result_to_repl(result, mappings):
92     def ritem_to_string(r):
93         if type(r) is int:
94             return "\\%d" % mappings[r]
95         else:
96             return r + "\n"
97     return "".join(map(ritem_to_string, result))
98
99 def resolve(contents, spec, result):
100     rstring, mappings = spec_to_regex(spec)
101     regex = re.compile(rstring, re.DOTALL)
102     repl = result_to_repl(result, mappings)
103     ret = ""
104     conflict = ""
105     status = 0
106     for line in contents.splitlines(True):
107         if status == 0 and line.startswith("<<<<<<<"):
108             status = 1
109         elif status == 1 and line.startswith("======="):
110             status = 2
111         # ok, now process
112         if status == 2 and line.startswith(">>>>>>>"):
113             status = 0
114             conflict += line
115             ret += regex.sub(repl, conflict)
116             conflict = ""
117         elif status:
118             conflict += line
119         else:
120             ret += line
121     return ret
122
123 def is_conflict(contents):
124     # Really really simple heuristic
125     return "<<<<<<<" in contents
126
127 def fix_newlines(file, log=True):
128     """
129     Normalizes newlines in a file into UNIX file endings.  If
130     ``log`` is ``True`` an info log mesage is printed if
131     any normalization occurs.  Return value is ``True`` if
132     normalization occurred.
133     """
134     old_contents = open(file, "r").read()
135     contents = old_contents
136     while "\r\n" in contents:
137         contents = contents.replace("\r\n", "\n")
138     contents = contents.replace("\r", "\n")
139     if contents != old_contents:
140         logging.info("Converted %s to UNIX file endings" % file)
141         open(file, "w").write(contents)
142         return True
143     return False