author | Brendan Cully <brendan@kublai.com> |
Fri, 03 Aug 2007 18:05:20 -0700 | |
changeset 5109 | 86327d13d916 |
parent 5054 | ec70fd08e16c |
child 5110 | 420e1166a876 |
permissions | -rw-r--r-- |
5044 | 1 |
# Copyright (C) 2007 Brendan Cully <brendan@kublai.com> |
2 |
# Published under the GNU GPL |
|
3 |
||
4 |
''' |
|
5 |
imerge - interactive merge |
|
6 |
''' |
|
7 |
||
8 |
from mercurial.i18n import _ |
|
9 |
from mercurial.node import * |
|
10 |
from mercurial import commands, cmdutil, hg, merge, util |
|
11 |
import os, tarfile |
|
12 |
||
13 |
class InvalidStateFileException(Exception): pass |
|
14 |
||
15 |
class ImergeStateFile(object): |
|
16 |
def __init__(self, im): |
|
17 |
self.im = im |
|
18 |
||
19 |
def save(self, dest): |
|
20 |
tf = tarfile.open(dest, 'w:gz') |
|
21 |
||
22 |
st = os.path.join(self.im.path, 'status') |
|
23 |
tf.add(st, os.path.join('.hg', 'imerge', 'status')) |
|
24 |
||
25 |
for f in self.im.resolved: |
|
5109 | 26 |
(fd, fo) = self.im.conflicts[f] |
27 |
abssrc = self.im.repo.wjoin(fd) |
|
28 |
tf.add(abssrc, fd) |
|
5044 | 29 |
|
30 |
tf.close() |
|
31 |
||
32 |
def load(self, source): |
|
33 |
wlock = self.im.repo.wlock() |
|
34 |
lock = self.im.repo.lock() |
|
35 |
||
36 |
tf = tarfile.open(source, 'r') |
|
37 |
contents = tf.getnames() |
|
38 |
statusfile = os.path.join('.hg', 'imerge', 'status') |
|
39 |
if statusfile not in contents: |
|
40 |
raise InvalidStateFileException('no status file') |
|
41 |
||
42 |
tf.extract(statusfile, self.im.repo.root) |
|
5109 | 43 |
p1, p2 = self.im.load() |
44 |
if self.im.repo.dirstate.parents()[0] != p1.node(): |
|
45 |
hg.clean(self.im.repo, p1.node()) |
|
46 |
self.im.start(p2.node()) |
|
5044 | 47 |
tf.extractall(self.im.repo.root) |
48 |
self.im.load() |
|
49 |
||
50 |
class Imerge(object): |
|
51 |
def __init__(self, ui, repo): |
|
52 |
self.ui = ui |
|
53 |
self.repo = repo |
|
54 |
||
55 |
self.path = repo.join('imerge') |
|
56 |
self.opener = util.opener(self.path) |
|
57 |
||
5109 | 58 |
self.wctx = self.repo.workingctx() |
5044 | 59 |
self.conflicts = {} |
60 |
self.resolved = [] |
|
61 |
||
62 |
def merging(self): |
|
5109 | 63 |
return len(self.wctx.parents()) > 1 |
5044 | 64 |
|
65 |
def load(self): |
|
66 |
# status format. \0-delimited file, fields are |
|
67 |
# p1, p2, conflict count, conflict filenames, resolved filenames |
|
5054
ec70fd08e16c
Update imerge for new filemerge interface
Brendan Cully <brendan@kublai.com>
parents:
5044
diff
changeset
|
68 |
# conflict filenames are tuples of localname, remoteorig, remotenew |
5044 | 69 |
|
70 |
statusfile = self.opener('status') |
|
71 |
||
72 |
status = statusfile.read().split('\0') |
|
73 |
if len(status) < 3: |
|
74 |
raise util.Abort('invalid imerge status file') |
|
75 |
||
76 |
try: |
|
5109 | 77 |
parents = [self.repo.changectx(n) for n in status[:2]] |
5044 | 78 |
except LookupError: |
79 |
raise util.Abort('merge parent %s not in repository' % short(p)) |
|
80 |
||
81 |
status = status[2:] |
|
5054
ec70fd08e16c
Update imerge for new filemerge interface
Brendan Cully <brendan@kublai.com>
parents:
5044
diff
changeset
|
82 |
conflicts = int(status.pop(0)) * 3 |
5044 | 83 |
self.resolved = status[conflicts:] |
5054
ec70fd08e16c
Update imerge for new filemerge interface
Brendan Cully <brendan@kublai.com>
parents:
5044
diff
changeset
|
84 |
for i in xrange(0, conflicts, 3): |
ec70fd08e16c
Update imerge for new filemerge interface
Brendan Cully <brendan@kublai.com>
parents:
5044
diff
changeset
|
85 |
self.conflicts[status[i]] = (status[i+1], status[i+2]) |
5044 | 86 |
|
5109 | 87 |
return parents |
88 |
||
5044 | 89 |
def save(self): |
90 |
lock = self.repo.lock() |
|
91 |
||
92 |
if not os.path.isdir(self.path): |
|
93 |
os.mkdir(self.path) |
|
94 |
fd = self.opener('status', 'wb') |
|
95 |
||
5109 | 96 |
out = [hex(n.node()) for n in self.wctx.parents()] |
5044 | 97 |
out.append(str(len(self.conflicts))) |
98 |
for f in sorted(self.conflicts): |
|
99 |
out.append(f) |
|
5054
ec70fd08e16c
Update imerge for new filemerge interface
Brendan Cully <brendan@kublai.com>
parents:
5044
diff
changeset
|
100 |
out.extend(self.conflicts[f]) |
5044 | 101 |
out.extend(self.resolved) |
102 |
||
103 |
fd.write('\0'.join(out)) |
|
104 |
||
105 |
def remaining(self): |
|
106 |
return [f for f in self.conflicts if f not in self.resolved] |
|
107 |
||
108 |
def filemerge(self, fn): |
|
109 |
wlock = self.repo.wlock() |
|
110 |
||
5054
ec70fd08e16c
Update imerge for new filemerge interface
Brendan Cully <brendan@kublai.com>
parents:
5044
diff
changeset
|
111 |
(fd, fo) = self.conflicts[fn] |
5109 | 112 |
p2 = self.wctx.parents()[1] |
113 |
return merge.filemerge(self.repo, fn, fd, fo, self.wctx, p2) |
|
5044 | 114 |
|
115 |
def start(self, rev=None): |
|
116 |
_filemerge = merge.filemerge |
|
5054
ec70fd08e16c
Update imerge for new filemerge interface
Brendan Cully <brendan@kublai.com>
parents:
5044
diff
changeset
|
117 |
def filemerge(repo, fw, fd, fo, wctx, mctx): |
ec70fd08e16c
Update imerge for new filemerge interface
Brendan Cully <brendan@kublai.com>
parents:
5044
diff
changeset
|
118 |
self.conflicts[fw] = (fd, fo) |
5044 | 119 |
|
120 |
merge.filemerge = filemerge |
|
121 |
commands.merge(self.ui, self.repo, rev=rev) |
|
122 |
merge.filemerge = _filemerge |
|
123 |
||
5109 | 124 |
self.wctx = self.repo.workingctx() |
5044 | 125 |
self.save() |
126 |
||
127 |
def resume(self): |
|
128 |
self.load() |
|
129 |
||
130 |
dp = self.repo.dirstate.parents() |
|
5109 | 131 |
p1, p2 = self.wctx.parents() |
132 |
if p1.node() != dp[0] or p2.node() != dp[1]: |
|
5044 | 133 |
raise util.Abort('imerge state does not match working directory') |
134 |
||
135 |
def status(self): |
|
5109 | 136 |
p1, p2 = self.wctx.parents() |
5044 | 137 |
self.ui.write('merging %s and %s\n' % \ |
5109 | 138 |
(short(p1.node()), short(p2.node()))) |
5044 | 139 |
|
140 |
if self.resolved: |
|
141 |
self.ui.write('resolved:\n') |
|
142 |
for fn in self.resolved: |
|
143 |
self.ui.write(' %s\n' % fn) |
|
144 |
remaining = [f for f in self.conflicts if f not in self.resolved] |
|
145 |
if remaining: |
|
146 |
self.ui.write('remaining:\n') |
|
147 |
for fn in remaining: |
|
5054
ec70fd08e16c
Update imerge for new filemerge interface
Brendan Cully <brendan@kublai.com>
parents:
5044
diff
changeset
|
148 |
(fd, fo) = self.conflicts[fn] |
5044 | 149 |
if fn == fo: |
150 |
self.ui.write(' %s\n' % (fn,)) |
|
151 |
else: |
|
5054
ec70fd08e16c
Update imerge for new filemerge interface
Brendan Cully <brendan@kublai.com>
parents:
5044
diff
changeset
|
152 |
self.ui.write(' %s (%s)\n' % (fn, fd)) |
5044 | 153 |
else: |
154 |
self.ui.write('all conflicts resolved\n') |
|
155 |
||
156 |
def next(self): |
|
157 |
remaining = self.remaining() |
|
158 |
return remaining and remaining[0] |
|
159 |
||
160 |
def resolve(self, files): |
|
161 |
resolved = dict.fromkeys(self.resolved) |
|
162 |
for fn in files: |
|
163 |
if fn not in self.conflicts: |
|
164 |
raise util.Abort('%s is not in the merge set' % fn) |
|
165 |
resolved[fn] = True |
|
166 |
self.resolved = sorted(resolved) |
|
167 |
self.save() |
|
168 |
return 0 |
|
169 |
||
170 |
def unresolve(self, files): |
|
171 |
resolved = dict.fromkeys(self.resolved) |
|
172 |
for fn in files: |
|
173 |
if fn not in resolved: |
|
174 |
raise util.Abort('%s is not resolved' % fn) |
|
175 |
del resolved[fn] |
|
176 |
self.resolved = sorted(resolved) |
|
177 |
self.save() |
|
178 |
return 0 |
|
179 |
||
180 |
def pickle(self, dest): |
|
181 |
'''write current merge state to file to be resumed elsewhere''' |
|
182 |
state = ImergeStateFile(self) |
|
183 |
return state.save(dest) |
|
184 |
||
185 |
def unpickle(self, source): |
|
186 |
'''read merge state from file''' |
|
187 |
state = ImergeStateFile(self) |
|
188 |
return state.load(source) |
|
189 |
||
190 |
def load(im, source): |
|
191 |
if im.merging(): |
|
192 |
raise util.Abort('there is already a merge in progress ' |
|
193 |
'(update -C <rev> to abort it)' ) |
|
194 |
m, a, r, d = im.repo.status()[:4] |
|
195 |
if m or a or r or d: |
|
196 |
raise util.Abort('working directory has uncommitted changes') |
|
197 |
||
198 |
rc = im.unpickle(source) |
|
199 |
if not rc: |
|
200 |
im.status() |
|
201 |
return rc |
|
202 |
||
203 |
def merge_(im, filename=None): |
|
204 |
if not filename: |
|
205 |
filename = im.next() |
|
206 |
if not filename: |
|
207 |
im.ui.write('all conflicts resolved\n') |
|
208 |
return 0 |
|
209 |
||
210 |
rc = im.filemerge(filename) |
|
211 |
if not rc: |
|
212 |
im.resolve([filename]) |
|
213 |
if not im.next(): |
|
214 |
im.ui.write('all conflicts resolved\n') |
|
215 |
return 0 |
|
216 |
return rc |
|
217 |
||
218 |
def next(im): |
|
219 |
n = im.next() |
|
220 |
if n: |
|
221 |
im.ui.write('%s\n' % n) |
|
222 |
else: |
|
223 |
im.ui.write('all conflicts resolved\n') |
|
224 |
return 0 |
|
225 |
||
226 |
def resolve(im, *files): |
|
227 |
if not files: |
|
228 |
raise util.Abort('resolve requires at least one filename') |
|
229 |
return im.resolve(files) |
|
230 |
||
231 |
def save(im, dest): |
|
232 |
return im.pickle(dest) |
|
233 |
||
234 |
def status(im): |
|
235 |
im.status() |
|
236 |
return 0 |
|
237 |
||
238 |
def unresolve(im, *files): |
|
239 |
if not files: |
|
240 |
raise util.Abort('unresolve requires at least one filename') |
|
241 |
return im.unresolve(files) |
|
242 |
||
243 |
subcmdtable = { |
|
244 |
'load': load, |
|
245 |
'merge': merge_, |
|
246 |
'next': next, |
|
247 |
'resolve': resolve, |
|
248 |
'save': save, |
|
249 |
'status': status, |
|
250 |
'unresolve': unresolve |
|
251 |
} |
|
252 |
||
253 |
def dispatch(im, args, opts): |
|
254 |
def complete(s, choices): |
|
255 |
candidates = [] |
|
256 |
for choice in choices: |
|
257 |
if choice.startswith(s): |
|
258 |
candidates.append(choice) |
|
259 |
return candidates |
|
260 |
||
261 |
c, args = args[0], args[1:] |
|
262 |
cmd = complete(c, subcmdtable.keys()) |
|
263 |
if not cmd: |
|
264 |
raise cmdutil.UnknownCommand('imerge ' + c) |
|
265 |
if len(cmd) > 1: |
|
266 |
raise cmdutil.AmbiguousCommand('imerge ' + c, sorted(cmd)) |
|
267 |
cmd = cmd[0] |
|
268 |
||
269 |
func = subcmdtable[cmd] |
|
270 |
try: |
|
271 |
return func(im, *args) |
|
272 |
except TypeError: |
|
273 |
raise cmdutil.ParseError('imerge', '%s: invalid arguments' % cmd) |
|
274 |
||
275 |
def imerge(ui, repo, *args, **opts): |
|
276 |
'''interactive merge |
|
277 |
||
278 |
imerge lets you split a merge into pieces. When you start a merge |
|
279 |
with imerge, the names of all files with conflicts are recorded. |
|
280 |
You can then merge any of these files, and if the merge is |
|
281 |
successful, they will be marked as resolved. When all files are |
|
282 |
resolved, the merge is complete. |
|
283 |
||
284 |
If no merge is in progress, hg imerge [rev] will merge the working |
|
285 |
directory with rev (defaulting to the other head if the repository |
|
286 |
only has two heads). You may also resume a saved merge with |
|
287 |
hg imerge load <file>. |
|
288 |
||
289 |
If a merge is in progress, hg imerge will default to merging the |
|
290 |
next unresolved file. |
|
291 |
||
292 |
The following subcommands are available: |
|
293 |
||
294 |
status: |
|
295 |
show the current state of the merge |
|
296 |
next: |
|
297 |
show the next unresolved file merge |
|
298 |
merge [<file>]: |
|
299 |
merge <file>. If the file merge is successful, the file will be |
|
300 |
recorded as resolved. If no file is given, the next unresolved |
|
301 |
file will be merged. |
|
302 |
resolve <file>...: |
|
303 |
mark files as successfully merged |
|
304 |
unresolve <file>...: |
|
305 |
mark files as requiring merging. |
|
306 |
save <file>: |
|
307 |
save the state of the merge to a file to be resumed elsewhere |
|
308 |
load <file>: |
|
309 |
load the state of the merge from a file created by save |
|
310 |
''' |
|
311 |
||
312 |
im = Imerge(ui, repo) |
|
313 |
||
314 |
if im.merging(): |
|
315 |
im.resume() |
|
316 |
else: |
|
317 |
rev = opts.get('rev') |
|
318 |
if rev and args: |
|
319 |
raise util.Abort('please specify just one revision') |
|
320 |
||
321 |
if len(args) == 2 and args[0] == 'load': |
|
322 |
pass |
|
323 |
else: |
|
324 |
if args: |
|
325 |
rev = args[0] |
|
326 |
im.start(rev=rev) |
|
327 |
args = ['status'] |
|
328 |
||
329 |
if not args: |
|
330 |
args = ['merge'] |
|
331 |
||
332 |
return dispatch(im, args, opts) |
|
333 |
||
334 |
cmdtable = { |
|
335 |
'^imerge': |
|
336 |
(imerge, |
|
337 |
[('r', 'rev', '', _('revision to merge'))], 'hg imerge [command]') |
|
338 |
} |