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