Mercurial > hg
annotate hgext/transplant.py @ 3725:ccc7a9eb0e5e
transplant: preserve filter changes in --continue log
author | Brendan Cully <brendan@kublai.com> |
---|---|
date | Thu, 30 Nov 2006 11:32:40 -0800 |
parents | ea523d6f5f1a |
children | 752884db5037 |
rev | line source |
---|---|
3714 | 1 # Patch transplanting extension for Mercurial |
2 # | |
3 # Copyright 2006 Brendan Cully <brendan@kublai.com> | |
4 # | |
5 # This software may be used and distributed according to the terms | |
6 # of the GNU General Public License, incorporated herein by reference. | |
7 | |
8 from mercurial.demandload import * | |
9 from mercurial.i18n import gettext as _ | |
10 demandload(globals(), 'os tempfile') | |
11 demandload(globals(), 'mercurial:bundlerepo,cmdutil,commands,hg,merge,patch') | |
12 demandload(globals(), 'mercurial:revlog,util') | |
13 | |
14 '''patch transplanting tool | |
15 | |
16 This extension allows you to transplant patches from another branch. | |
17 | |
18 Transplanted patches are recorded in .hg/transplant/transplants, as a map | |
19 from a changeset hash to its hash in the source repository. | |
20 ''' | |
21 | |
22 class transplantentry: | |
23 def __init__(self, lnode, rnode): | |
24 self.lnode = lnode | |
25 self.rnode = rnode | |
26 | |
27 class transplants: | |
28 def __init__(self, path=None, transplantfile=None, opener=None): | |
29 self.path = path | |
30 self.transplantfile = transplantfile | |
31 self.opener = opener | |
32 | |
33 if not opener: | |
34 self.opener = util.opener(self.path) | |
35 self.transplants = [] | |
36 self.dirty = False | |
37 self.read() | |
38 | |
39 def read(self): | |
40 abspath = os.path.join(self.path, self.transplantfile) | |
41 if self.transplantfile and os.path.exists(abspath): | |
42 for line in self.opener(self.transplantfile).read().splitlines(): | |
43 lnode, rnode = map(revlog.bin, line.split(':')) | |
44 self.transplants.append(transplantentry(lnode, rnode)) | |
45 | |
46 def write(self): | |
47 if self.dirty and self.transplantfile: | |
48 if not os.path.isdir(self.path): | |
49 os.mkdir(self.path) | |
50 fp = self.opener(self.transplantfile, 'w') | |
51 for c in self.transplants: | |
52 l, r = map(revlog.hex, (c.lnode, c.rnode)) | |
53 fp.write(l + ':' + r + '\n') | |
54 fp.close() | |
55 self.dirty = False | |
56 | |
57 def get(self, rnode): | |
58 return [t for t in self.transplants if t.rnode == rnode] | |
59 | |
60 def set(self, lnode, rnode): | |
61 self.transplants.append(transplantentry(lnode, rnode)) | |
62 self.dirty = True | |
63 | |
64 def remove(self, transplant): | |
65 del self.transplants[self.transplants.index(transplant)] | |
66 self.dirty = True | |
67 | |
68 class transplanter: | |
69 def __init__(self, ui, repo): | |
70 self.ui = ui | |
71 self.path = repo.join('transplant') | |
72 self.opener = util.opener(self.path) | |
73 self.transplants = transplants(self.path, 'transplants', opener=self.opener) | |
74 | |
75 def applied(self, repo, node, parent): | |
76 '''returns True if a node is already an ancestor of parent | |
77 or has already been transplanted''' | |
78 if hasnode(repo, node): | |
79 if node in repo.changelog.reachable(parent, stop=node): | |
80 return True | |
81 for t in self.transplants.get(node): | |
82 # it might have been stripped | |
83 if not hasnode(repo, t.lnode): | |
84 self.transplants.remove(t) | |
85 return False | |
86 if t.lnode in repo.changelog.reachable(parent, stop=t.lnode): | |
87 return True | |
88 return False | |
89 | |
90 def apply(self, repo, source, revmap, merges, opts={}): | |
91 '''apply the revisions in revmap one by one in revision order''' | |
92 revs = revmap.keys() | |
93 revs.sort() | |
94 | |
95 p1, p2 = repo.dirstate.parents() | |
96 pulls = [] | |
97 diffopts = patch.diffopts(self.ui, opts) | |
98 diffopts.git = True | |
99 | |
100 lock = repo.lock() | |
101 wlock = repo.wlock() | |
102 try: | |
103 for rev in revs: | |
104 node = revmap[rev] | |
105 revstr = '%s:%s' % (rev, revlog.short(node)) | |
106 | |
107 if self.applied(repo, node, p1): | |
108 self.ui.warn(_('skipping already applied revision %s\n') % | |
109 revstr) | |
110 continue | |
111 | |
112 parents = source.changelog.parents(node) | |
113 if not opts.get('filter'): | |
114 # If the changeset parent is the same as the wdir's parent, | |
115 # just pull it. | |
116 if parents[0] == p1: | |
117 pulls.append(node) | |
118 p1 = node | |
119 continue | |
120 if pulls: | |
121 if source != repo: | |
122 repo.pull(source, heads=pulls, lock=lock) | |
123 merge.update(repo, pulls[-1], wlock=wlock) | |
124 p1, p2 = repo.dirstate.parents() | |
125 pulls = [] | |
126 | |
127 domerge = False | |
128 if node in merges: | |
129 # pulling all the merge revs at once would mean we couldn't | |
130 # transplant after the latest even if transplants before them | |
131 # fail. | |
132 domerge = True | |
133 if not hasnode(repo, node): | |
134 repo.pull(source, heads=[node], lock=lock) | |
135 | |
136 if parents[1] != revlog.nullid: | |
137 self.ui.note(_('skipping merge changeset %s:%s\n') | |
138 % (rev, revlog.short(node))) | |
139 patchfile = None | |
140 else: | |
141 fd, patchfile = tempfile.mkstemp(prefix='hg-transplant-') | |
142 fp = os.fdopen(fd, 'w') | |
143 patch.export(source, [node], fp=fp, opts=diffopts) | |
144 fp.close() | |
145 | |
146 del revmap[rev] | |
147 if patchfile or domerge: | |
148 try: | |
149 n = self.applyone(repo, node, source.changelog.read(node), | |
150 patchfile, merge=domerge, | |
151 log=opts.get('log'), | |
152 filter=opts.get('filter'), | |
153 lock=lock, wlock=wlock) | |
154 if domerge: | |
155 self.ui.status(_('%s merged at %s\n') % (revstr, | |
156 revlog.short(n))) | |
157 else: | |
158 self.ui.status(_('%s transplanted to %s\n') % (revlog.short(node), | |
159 revlog.short(n))) | |
160 finally: | |
161 if patchfile: | |
162 os.unlink(patchfile) | |
163 if pulls: | |
164 repo.pull(source, heads=pulls, lock=lock) | |
165 merge.update(repo, pulls[-1], wlock=wlock) | |
166 finally: | |
167 self.saveseries(revmap, merges) | |
168 self.transplants.write() | |
169 | |
170 def filter(self, filter, changelog, patchfile): | |
171 '''arbitrarily rewrite changeset before applying it''' | |
172 | |
173 self.ui.status('filtering %s' % patchfile) | |
174 util.system('%s %s' % (filter, util.shellquote(patchfile)), | |
175 environ={'HGUSER': changelog[1]}, | |
176 onerr=util.Abort, errprefix=_('filter failed')) | |
177 | |
178 def applyone(self, repo, node, cl, patchfile, merge=False, log=False, | |
179 filter=None, lock=None, wlock=None): | |
180 '''apply the patch in patchfile to the repository as a transplant''' | |
181 (manifest, user, (time, timezone), files, message) = cl[:5] | |
182 date = "%d %d" % (time, timezone) | |
183 extra = {'transplant_source': node} | |
184 if filter: | |
185 self.filter(filter, cl, patchfile) | |
186 patchfile, message, user, date = patch.extract(self.ui, file(patchfile)) | |
187 | |
188 if log: | |
189 message += '\n(transplanted from %s)' % revlog.hex(node) | |
190 | |
191 self.ui.status(_('applying %s\n') % revlog.short(node)) | |
192 self.ui.note('%s %s\n%s\n' % (user, date, message)) | |
193 | |
194 if not patchfile and not merge: | |
195 raise util.Abort(_('can only omit patchfile if merging')) | |
196 if patchfile: | |
197 try: | |
198 files = {} | |
199 fuzz = patch.patch(patchfile, self.ui, cwd=repo.root, | |
200 files=files) | |
201 if not files: | |
202 self.ui.warn(_('%s: empty changeset') % revlog.hex(node)) | |
203 return | |
204 files = patch.updatedir(self.ui, repo, files, wlock=wlock) | |
205 if filter: | |
206 os.unlink(patchfile) | |
207 except Exception, inst: | |
208 if filter: | |
209 os.unlink(patchfile) | |
210 p1 = repo.dirstate.parents()[0] | |
211 p2 = node | |
3725
ccc7a9eb0e5e
transplant: preserve filter changes in --continue log
Brendan Cully <brendan@kublai.com>
parents:
3724
diff
changeset
|
212 self.log(user, date, message, p1, p2, merge=merge) |
3714 | 213 self.ui.write(str(inst) + '\n') |
214 raise util.Abort(_('Fix up the merge and run hg transplant --continue')) | |
215 else: | |
216 files = None | |
217 if merge: | |
218 p1, p2 = repo.dirstate.parents() | |
219 repo.dirstate.setparents(p1, node) | |
220 | |
221 n = repo.commit(files, message, user, date, lock=lock, wlock=wlock, | |
222 extra=extra) | |
223 if not merge: | |
224 self.transplants.set(n, node) | |
225 | |
226 return n | |
227 | |
228 def resume(self, repo, source, opts=None): | |
229 '''recover last transaction and apply remaining changesets''' | |
230 if os.path.exists(os.path.join(self.path, 'journal')): | |
231 n, node = self.recover(repo) | |
3724
ea523d6f5f1a
transplant: fix --continue; add --continue test
Brendan Cully <brendan@kublai.com>
parents:
3723
diff
changeset
|
232 self.ui.status(_('%s transplanted as %s\n') % (revlog.short(node), |
ea523d6f5f1a
transplant: fix --continue; add --continue test
Brendan Cully <brendan@kublai.com>
parents:
3723
diff
changeset
|
233 revlog.short(n))) |
3714 | 234 seriespath = os.path.join(self.path, 'series') |
235 if not os.path.exists(seriespath): | |
236 return | |
237 nodes, merges = self.readseries() | |
238 revmap = {} | |
239 for n in nodes: | |
240 revmap[source.changelog.rev(n)] = n | |
241 os.unlink(seriespath) | |
242 | |
243 self.apply(repo, source, revmap, merges, opts) | |
244 | |
245 def recover(self, repo): | |
246 '''commit working directory using journal metadata''' | |
247 node, user, date, message, parents = self.readlog() | |
248 merge = len(parents) == 2 | |
249 | |
250 if not user or not date or not message or not parents[0]: | |
251 raise util.Abort(_('transplant log file is corrupt')) | |
252 | |
253 wlock = repo.wlock() | |
254 p1, p2 = repo.dirstate.parents() | |
255 if p1 != parents[0]: | |
256 raise util.Abort(_('working dir not at transplant parent %s') % | |
257 revlog.hex(parents[0])) | |
258 if merge: | |
259 repo.dirstate.setparents(p1, parents[1]) | |
260 n = repo.commit(None, message, user, date, wlock=wlock) | |
261 if not n: | |
262 raise util.Abort(_('commit failed')) | |
263 if not merge: | |
264 self.transplants.set(n, node) | |
265 self.unlog() | |
266 | |
267 return n, node | |
268 | |
269 def readseries(self): | |
270 nodes = [] | |
271 merges = [] | |
272 cur = nodes | |
273 for line in self.opener('series').read().splitlines(): | |
274 if line.startswith('# Merges'): | |
275 cur = merges | |
276 continue | |
277 cur.append(revlog.bin(line)) | |
278 | |
279 return (nodes, merges) | |
280 | |
281 def saveseries(self, revmap, merges): | |
282 if not revmap: | |
283 return | |
284 | |
285 if not os.path.isdir(self.path): | |
286 os.mkdir(self.path) | |
287 series = self.opener('series', 'w') | |
288 revs = revmap.keys() | |
289 revs.sort() | |
290 for rev in revs: | |
291 series.write(revlog.hex(revmap[rev]) + '\n') | |
292 if merges: | |
293 series.write('# Merges\n') | |
294 for m in merges: | |
295 series.write(revlog.hex(m) + '\n') | |
296 series.close() | |
297 | |
3725
ccc7a9eb0e5e
transplant: preserve filter changes in --continue log
Brendan Cully <brendan@kublai.com>
parents:
3724
diff
changeset
|
298 def log(self, user, date, message, p1, p2, merge=False): |
3714 | 299 '''journal changelog metadata for later recover''' |
300 | |
301 if not os.path.isdir(self.path): | |
302 os.mkdir(self.path) | |
303 fp = self.opener('journal', 'w') | |
3725
ccc7a9eb0e5e
transplant: preserve filter changes in --continue log
Brendan Cully <brendan@kublai.com>
parents:
3724
diff
changeset
|
304 fp.write('# User %s\n' % user) |
ccc7a9eb0e5e
transplant: preserve filter changes in --continue log
Brendan Cully <brendan@kublai.com>
parents:
3724
diff
changeset
|
305 fp.write('# Date %s\n' % date) |
3714 | 306 fp.write('# Node ID %s\n' % revlog.hex(p2)) |
307 fp.write('# Parent ' + revlog.hex(p1) + '\n') | |
308 if merge: | |
309 fp.write('# Parent ' + revlog.hex(p2) + '\n') | |
3725
ccc7a9eb0e5e
transplant: preserve filter changes in --continue log
Brendan Cully <brendan@kublai.com>
parents:
3724
diff
changeset
|
310 fp.write(message.rstrip() + '\n') |
3714 | 311 fp.close() |
312 | |
313 def readlog(self): | |
314 parents = [] | |
315 message = [] | |
316 for line in self.opener('journal').read().splitlines(): | |
317 if line.startswith('# User '): | |
318 user = line[7:] | |
319 elif line.startswith('# Date '): | |
320 date = line[7:] | |
321 elif line.startswith('# Node ID '): | |
322 node = revlog.bin(line[10:]) | |
323 elif line.startswith('# Parent '): | |
324 parents.append(revlog.bin(line[9:])) | |
325 else: | |
326 message.append(line) | |
327 return (node, user, date, '\n'.join(message), parents) | |
328 | |
329 def unlog(self): | |
330 '''remove changelog journal''' | |
331 absdst = os.path.join(self.path, 'journal') | |
332 if os.path.exists(absdst): | |
333 os.unlink(absdst) | |
334 | |
335 def transplantfilter(self, repo, source, root): | |
336 def matchfn(node): | |
337 if self.applied(repo, node, root): | |
338 return False | |
339 if source.changelog.parents(node)[1] != revlog.nullid: | |
340 return False | |
341 extra = source.changelog.read(node)[5] | |
342 cnode = extra.get('transplant_source') | |
343 if cnode and self.applied(repo, cnode, root): | |
344 return False | |
345 return True | |
346 | |
347 return matchfn | |
348 | |
349 def hasnode(repo, node): | |
350 try: | |
351 return repo.changelog.rev(node) != None | |
352 except revlog.RevlogError: | |
353 return False | |
354 | |
355 def browserevs(ui, repo, nodes, opts): | |
356 '''interactively transplant changesets''' | |
357 def browsehelp(ui): | |
358 ui.write('y: transplant this changeset\n' | |
359 'n: skip this changeset\n' | |
360 'm: merge at this changeset\n' | |
361 'p: show patch\n' | |
362 'c: commit selected changesets\n' | |
363 'q: cancel transplant\n' | |
364 '?: show this help\n') | |
365 | |
3723
c828fca6f38a
transplant: show_changeset moved to cmdutil
Brendan Cully <brendan@kublai.com>
parents:
3714
diff
changeset
|
366 displayer = cmdutil.show_changeset(ui, repo, opts) |
3714 | 367 transplants = [] |
368 merges = [] | |
369 for node in nodes: | |
370 displayer.show(changenode=node) | |
371 action = None | |
372 while not action: | |
373 action = ui.prompt(_('apply changeset? [ynmpcq?]:')) | |
374 if action == '?': | |
375 browsehelp(ui) | |
376 action = None | |
377 elif action == 'p': | |
378 parent = repo.changelog.parents(node)[0] | |
379 patch.diff(repo, parent, node) | |
380 action = None | |
381 elif action not in ('y', 'n', 'm', 'c', 'q'): | |
382 ui.write('no such option\n') | |
383 action = None | |
384 if action == 'y': | |
385 transplants.append(node) | |
386 elif action == 'm': | |
387 merges.append(node) | |
388 elif action == 'c': | |
389 break | |
390 elif action == 'q': | |
391 transplants = () | |
392 merges = () | |
393 break | |
394 return (transplants, merges) | |
395 | |
396 def transplant(ui, repo, *revs, **opts): | |
397 '''transplant changesets from another branch | |
398 | |
399 Selected changesets will be applied on top of the current working | |
400 directory with the log of the original changeset. If --log is | |
401 specified, log messages will have a comment appended of the form: | |
402 | |
403 (transplanted from CHANGESETHASH) | |
404 | |
405 You can rewrite the changelog message with the --filter option. | |
406 Its argument will be invoked with the current changelog message | |
407 as $1 and the patch as $2. | |
408 | |
409 If --source is specified, selects changesets from the named | |
410 repository. If --branch is specified, selects changesets from the | |
411 branch holding the named revision, up to that revision. If --all | |
412 is specified, all changesets on the branch will be transplanted, | |
413 otherwise you will be prompted to select the changesets you want. | |
414 | |
415 hg transplant --branch REVISION --all will rebase the selected branch | |
416 (up to the named revision) onto your current working directory. | |
417 | |
418 You can optionally mark selected transplanted changesets as | |
419 merge changesets. You will not be prompted to transplant any | |
420 ancestors of a merged transplant, and you can merge descendants | |
421 of them normally instead of transplanting them. | |
422 | |
423 If no merges or revisions are provided, hg transplant will start | |
424 an interactive changeset browser. | |
425 | |
426 If a changeset application fails, you can fix the merge by hand and | |
427 then resume where you left off by calling hg transplant --continue. | |
428 ''' | |
429 def getoneitem(opts, item, errmsg): | |
430 val = opts.get(item) | |
431 if val: | |
432 if len(val) > 1: | |
433 raise util.Abort(errmsg) | |
434 else: | |
435 return val[0] | |
436 | |
437 def getremotechanges(repo, url): | |
438 sourcerepo = ui.expandpath(url) | |
439 source = hg.repository(ui, sourcerepo) | |
440 incoming = repo.findincoming(source, force=True) | |
441 if not incoming: | |
442 return (source, None, None) | |
443 | |
444 bundle = None | |
445 if not source.local(): | |
446 cg = source.changegroup(incoming, 'incoming') | |
447 bundle = commands.write_bundle(cg, compress=False) | |
448 source = bundlerepo.bundlerepository(ui, repo.root, bundle) | |
449 | |
450 return (source, incoming, bundle) | |
451 | |
452 def incwalk(repo, incoming, branches, match=util.always): | |
453 if not branches: | |
454 branches=None | |
455 for node in repo.changelog.nodesbetween(incoming, branches)[0]: | |
456 if match(node): | |
457 yield node | |
458 | |
459 def transplantwalk(repo, root, branches, match=util.always): | |
460 if not branches: | |
461 branches = repo.heads() | |
462 ancestors = [] | |
463 for branch in branches: | |
464 ancestors.append(repo.changelog.ancestor(root, branch)) | |
465 for node in repo.changelog.nodesbetween(ancestors, branches)[0]: | |
466 if match(node): | |
467 yield node | |
468 | |
469 def checkopts(opts, revs): | |
470 if opts.get('continue'): | |
471 if filter(lambda opt: opts.get(opt), ('branch', 'all', 'merge')): | |
472 raise util.Abort(_('--continue is incompatible with branch, all or merge')) | |
473 return | |
474 if not (opts.get('source') or revs or | |
475 opts.get('merge') or opts.get('branch')): | |
476 raise util.Abort(_('no source URL, branch tag or revision list provided')) | |
477 if opts.get('all'): | |
478 if not opts.get('branch'): | |
479 raise util.Abort(_('--all requires a branch revision')) | |
480 if revs: | |
481 raise util.Abort(_('--all is incompatible with a revision list')) | |
482 | |
483 checkopts(opts, revs) | |
484 | |
485 if not opts.get('log'): | |
486 opts['log'] = ui.config('transplant', 'log') | |
487 if not opts.get('filter'): | |
488 opts['filter'] = ui.config('transplant', 'filter') | |
489 | |
490 tp = transplanter(ui, repo) | |
491 | |
492 p1, p2 = repo.dirstate.parents() | |
493 if p1 == revlog.nullid: | |
494 raise util.Abort(_('no revision checked out')) | |
495 if not opts.get('continue'): | |
496 if p2 != revlog.nullid: | |
497 raise util.Abort(_('outstanding uncommitted merges')) | |
498 m, a, r, d = repo.status()[:4] | |
499 if m or a or r or d: | |
500 raise util.Abort(_('outstanding local changes')) | |
501 | |
502 bundle = None | |
503 source = opts.get('source') | |
504 if source: | |
505 (source, incoming, bundle) = getremotechanges(repo, source) | |
506 else: | |
507 source = repo | |
508 | |
509 try: | |
510 if opts.get('continue'): | |
3724
ea523d6f5f1a
transplant: fix --continue; add --continue test
Brendan Cully <brendan@kublai.com>
parents:
3723
diff
changeset
|
511 tp.resume(repo, source, opts) |
3714 | 512 return |
513 | |
514 tf=tp.transplantfilter(repo, source, p1) | |
515 if opts.get('prune'): | |
516 prune = [source.lookup(r) | |
517 for r in cmdutil.revrange(source, opts.get('prune'))] | |
518 matchfn = lambda x: tf(x) and x not in prune | |
519 else: | |
520 matchfn = tf | |
521 branches = map(source.lookup, opts.get('branch', ())) | |
522 merges = map(source.lookup, opts.get('merge', ())) | |
523 revmap = {} | |
524 if revs: | |
525 for r in cmdutil.revrange(source, revs): | |
526 revmap[int(r)] = source.lookup(r) | |
527 elif opts.get('all') or not merges: | |
528 if source != repo: | |
529 alltransplants = incwalk(source, incoming, branches, match=matchfn) | |
530 else: | |
531 alltransplants = transplantwalk(source, p1, branches, match=matchfn) | |
532 if opts.get('all'): | |
533 revs = alltransplants | |
534 else: | |
535 revs, newmerges = browserevs(ui, source, alltransplants, opts) | |
536 merges.extend(newmerges) | |
537 for r in revs: | |
538 revmap[source.changelog.rev(r)] = r | |
539 for r in merges: | |
540 revmap[source.changelog.rev(r)] = r | |
541 | |
542 revs = revmap.keys() | |
543 revs.sort() | |
544 pulls = [] | |
545 | |
546 tp.apply(repo, source, revmap, merges, opts) | |
547 finally: | |
548 if bundle: | |
549 os.unlink(bundle) | |
550 | |
551 cmdtable = { | |
552 "transplant": | |
553 (transplant, | |
554 [('s', 'source', '', _('pull patches from REPOSITORY')), | |
555 ('b', 'branch', [], _('pull patches from branch BRANCH')), | |
556 ('a', 'all', None, _('pull all changesets up to BRANCH')), | |
557 ('p', 'prune', [], _('skip over REV')), | |
558 ('m', 'merge', [], _('merge at REV')), | |
559 ('', 'log', None, _('append transplant info to log message')), | |
560 ('c', 'continue', None, _('continue last transplant session after repair')), | |
561 ('', 'filter', '', _('filter changesets through FILTER'))], | |
562 _('hg transplant [-s REPOSITORY] [-b BRANCH] [-p REV] [-m REV] [-n] REV...')) | |
563 } |