##// END OF EJS Templates
histedit: remove "name" parameter from cleanupnode functions...
Jun Wu -
r33348:72c25a91 default
parent child Browse files
Show More
@@ -1,1677 +1,1677 b''
1 1 # histedit.py - interactive history editing for mercurial
2 2 #
3 3 # Copyright 2009 Augie Fackler <raf@durin42.com>
4 4 #
5 5 # This software may be used and distributed according to the terms of the
6 6 # GNU General Public License version 2 or any later version.
7 7 """interactive history editing
8 8
9 9 With this extension installed, Mercurial gains one new command: histedit. Usage
10 10 is as follows, assuming the following history::
11 11
12 12 @ 3[tip] 7c2fd3b9020c 2009-04-27 18:04 -0500 durin42
13 13 | Add delta
14 14 |
15 15 o 2 030b686bedc4 2009-04-27 18:04 -0500 durin42
16 16 | Add gamma
17 17 |
18 18 o 1 c561b4e977df 2009-04-27 18:04 -0500 durin42
19 19 | Add beta
20 20 |
21 21 o 0 d8d2fcd0e319 2009-04-27 18:04 -0500 durin42
22 22 Add alpha
23 23
24 24 If you were to run ``hg histedit c561b4e977df``, you would see the following
25 25 file open in your editor::
26 26
27 27 pick c561b4e977df Add beta
28 28 pick 030b686bedc4 Add gamma
29 29 pick 7c2fd3b9020c Add delta
30 30
31 31 # Edit history between c561b4e977df and 7c2fd3b9020c
32 32 #
33 33 # Commits are listed from least to most recent
34 34 #
35 35 # Commands:
36 36 # p, pick = use commit
37 37 # e, edit = use commit, but stop for amending
38 38 # f, fold = use commit, but combine it with the one above
39 39 # r, roll = like fold, but discard this commit's description and date
40 40 # d, drop = remove commit from history
41 41 # m, mess = edit commit message without changing commit content
42 42 #
43 43
44 44 In this file, lines beginning with ``#`` are ignored. You must specify a rule
45 45 for each revision in your history. For example, if you had meant to add gamma
46 46 before beta, and then wanted to add delta in the same revision as beta, you
47 47 would reorganize the file to look like this::
48 48
49 49 pick 030b686bedc4 Add gamma
50 50 pick c561b4e977df Add beta
51 51 fold 7c2fd3b9020c Add delta
52 52
53 53 # Edit history between c561b4e977df and 7c2fd3b9020c
54 54 #
55 55 # Commits are listed from least to most recent
56 56 #
57 57 # Commands:
58 58 # p, pick = use commit
59 59 # e, edit = use commit, but stop for amending
60 60 # f, fold = use commit, but combine it with the one above
61 61 # r, roll = like fold, but discard this commit's description and date
62 62 # d, drop = remove commit from history
63 63 # m, mess = edit commit message without changing commit content
64 64 #
65 65
66 66 At which point you close the editor and ``histedit`` starts working. When you
67 67 specify a ``fold`` operation, ``histedit`` will open an editor when it folds
68 68 those revisions together, offering you a chance to clean up the commit message::
69 69
70 70 Add beta
71 71 ***
72 72 Add delta
73 73
74 74 Edit the commit message to your liking, then close the editor. The date used
75 75 for the commit will be the later of the two commits' dates. For this example,
76 76 let's assume that the commit message was changed to ``Add beta and delta.``
77 77 After histedit has run and had a chance to remove any old or temporary
78 78 revisions it needed, the history looks like this::
79 79
80 80 @ 2[tip] 989b4d060121 2009-04-27 18:04 -0500 durin42
81 81 | Add beta and delta.
82 82 |
83 83 o 1 081603921c3f 2009-04-27 18:04 -0500 durin42
84 84 | Add gamma
85 85 |
86 86 o 0 d8d2fcd0e319 2009-04-27 18:04 -0500 durin42
87 87 Add alpha
88 88
89 89 Note that ``histedit`` does *not* remove any revisions (even its own temporary
90 90 ones) until after it has completed all the editing operations, so it will
91 91 probably perform several strip operations when it's done. For the above example,
92 92 it had to run strip twice. Strip can be slow depending on a variety of factors,
93 93 so you might need to be a little patient. You can choose to keep the original
94 94 revisions by passing the ``--keep`` flag.
95 95
96 96 The ``edit`` operation will drop you back to a command prompt,
97 97 allowing you to edit files freely, or even use ``hg record`` to commit
98 98 some changes as a separate commit. When you're done, any remaining
99 99 uncommitted changes will be committed as well. When done, run ``hg
100 100 histedit --continue`` to finish this step. If there are uncommitted
101 101 changes, you'll be prompted for a new commit message, but the default
102 102 commit message will be the original message for the ``edit`` ed
103 103 revision, and the date of the original commit will be preserved.
104 104
105 105 The ``message`` operation will give you a chance to revise a commit
106 106 message without changing the contents. It's a shortcut for doing
107 107 ``edit`` immediately followed by `hg histedit --continue``.
108 108
109 109 If ``histedit`` encounters a conflict when moving a revision (while
110 110 handling ``pick`` or ``fold``), it'll stop in a similar manner to
111 111 ``edit`` with the difference that it won't prompt you for a commit
112 112 message when done. If you decide at this point that you don't like how
113 113 much work it will be to rearrange history, or that you made a mistake,
114 114 you can use ``hg histedit --abort`` to abandon the new changes you
115 115 have made and return to the state before you attempted to edit your
116 116 history.
117 117
118 118 If we clone the histedit-ed example repository above and add four more
119 119 changes, such that we have the following history::
120 120
121 121 @ 6[tip] 038383181893 2009-04-27 18:04 -0500 stefan
122 122 | Add theta
123 123 |
124 124 o 5 140988835471 2009-04-27 18:04 -0500 stefan
125 125 | Add eta
126 126 |
127 127 o 4 122930637314 2009-04-27 18:04 -0500 stefan
128 128 | Add zeta
129 129 |
130 130 o 3 836302820282 2009-04-27 18:04 -0500 stefan
131 131 | Add epsilon
132 132 |
133 133 o 2 989b4d060121 2009-04-27 18:04 -0500 durin42
134 134 | Add beta and delta.
135 135 |
136 136 o 1 081603921c3f 2009-04-27 18:04 -0500 durin42
137 137 | Add gamma
138 138 |
139 139 o 0 d8d2fcd0e319 2009-04-27 18:04 -0500 durin42
140 140 Add alpha
141 141
142 142 If you run ``hg histedit --outgoing`` on the clone then it is the same
143 143 as running ``hg histedit 836302820282``. If you need plan to push to a
144 144 repository that Mercurial does not detect to be related to the source
145 145 repo, you can add a ``--force`` option.
146 146
147 147 Config
148 148 ------
149 149
150 150 Histedit rule lines are truncated to 80 characters by default. You
151 151 can customize this behavior by setting a different length in your
152 152 configuration file::
153 153
154 154 [histedit]
155 155 linelen = 120 # truncate rule lines at 120 characters
156 156
157 157 ``hg histedit`` attempts to automatically choose an appropriate base
158 158 revision to use. To change which base revision is used, define a
159 159 revset in your configuration file::
160 160
161 161 [histedit]
162 162 defaultrev = only(.) & draft()
163 163
164 164 By default each edited revision needs to be present in histedit commands.
165 165 To remove revision you need to use ``drop`` operation. You can configure
166 166 the drop to be implicit for missing commits by adding::
167 167
168 168 [histedit]
169 169 dropmissing = True
170 170
171 171 By default, histedit will close the transaction after each action. For
172 172 performance purposes, you can configure histedit to use a single transaction
173 173 across the entire histedit. WARNING: This setting introduces a significant risk
174 174 of losing the work you've done in a histedit if the histedit aborts
175 175 unexpectedly::
176 176
177 177 [histedit]
178 178 singletransaction = True
179 179
180 180 """
181 181
182 182 from __future__ import absolute_import
183 183
184 184 import errno
185 185 import os
186 186
187 187 from mercurial.i18n import _
188 188 from mercurial import (
189 189 bundle2,
190 190 cmdutil,
191 191 context,
192 192 copies,
193 193 destutil,
194 194 discovery,
195 195 error,
196 196 exchange,
197 197 extensions,
198 198 hg,
199 199 lock,
200 200 merge as mergemod,
201 201 mergeutil,
202 202 node,
203 203 obsolete,
204 204 registrar,
205 205 repair,
206 206 scmutil,
207 207 util,
208 208 )
209 209
210 210 pickle = util.pickle
211 211 release = lock.release
212 212 cmdtable = {}
213 213 command = registrar.command(cmdtable)
214 214
215 215 # Note for extension authors: ONLY specify testedwith = 'ships-with-hg-core' for
216 216 # extensions which SHIP WITH MERCURIAL. Non-mainline extensions should
217 217 # be specifying the version(s) of Mercurial they are tested with, or
218 218 # leave the attribute unspecified.
219 219 testedwith = 'ships-with-hg-core'
220 220
221 221 actiontable = {}
222 222 primaryactions = set()
223 223 secondaryactions = set()
224 224 tertiaryactions = set()
225 225 internalactions = set()
226 226
227 227 def geteditcomment(ui, first, last):
228 228 """ construct the editor comment
229 229 The comment includes::
230 230 - an intro
231 231 - sorted primary commands
232 232 - sorted short commands
233 233 - sorted long commands
234 234 - additional hints
235 235
236 236 Commands are only included once.
237 237 """
238 238 intro = _("""Edit history between %s and %s
239 239
240 240 Commits are listed from least to most recent
241 241
242 242 You can reorder changesets by reordering the lines
243 243
244 244 Commands:
245 245 """)
246 246 actions = []
247 247 def addverb(v):
248 248 a = actiontable[v]
249 249 lines = a.message.split("\n")
250 250 if len(a.verbs):
251 251 v = ', '.join(sorted(a.verbs, key=lambda v: len(v)))
252 252 actions.append(" %s = %s" % (v, lines[0]))
253 253 actions.extend([' %s' for l in lines[1:]])
254 254
255 255 for v in (
256 256 sorted(primaryactions) +
257 257 sorted(secondaryactions) +
258 258 sorted(tertiaryactions)
259 259 ):
260 260 addverb(v)
261 261 actions.append('')
262 262
263 263 hints = []
264 264 if ui.configbool('histedit', 'dropmissing'):
265 265 hints.append("Deleting a changeset from the list "
266 266 "will DISCARD it from the edited history!")
267 267
268 268 lines = (intro % (first, last)).split('\n') + actions + hints
269 269
270 270 return ''.join(['# %s\n' % l if l else '#\n' for l in lines])
271 271
272 272 class histeditstate(object):
273 273 def __init__(self, repo, parentctxnode=None, actions=None, keep=None,
274 274 topmost=None, replacements=None, lock=None, wlock=None):
275 275 self.repo = repo
276 276 self.actions = actions
277 277 self.keep = keep
278 278 self.topmost = topmost
279 279 self.parentctxnode = parentctxnode
280 280 self.lock = lock
281 281 self.wlock = wlock
282 282 self.backupfile = None
283 283 self.tr = None
284 284 if replacements is None:
285 285 self.replacements = []
286 286 else:
287 287 self.replacements = replacements
288 288
289 289 def read(self):
290 290 """Load histedit state from disk and set fields appropriately."""
291 291 try:
292 292 state = self.repo.vfs.read('histedit-state')
293 293 except IOError as err:
294 294 if err.errno != errno.ENOENT:
295 295 raise
296 296 cmdutil.wrongtooltocontinue(self.repo, _('histedit'))
297 297
298 298 if state.startswith('v1\n'):
299 299 data = self._load()
300 300 parentctxnode, rules, keep, topmost, replacements, backupfile = data
301 301 else:
302 302 data = pickle.loads(state)
303 303 parentctxnode, rules, keep, topmost, replacements = data
304 304 backupfile = None
305 305
306 306 self.parentctxnode = parentctxnode
307 307 rules = "\n".join(["%s %s" % (verb, rest) for [verb, rest] in rules])
308 308 actions = parserules(rules, self)
309 309 self.actions = actions
310 310 self.keep = keep
311 311 self.topmost = topmost
312 312 self.replacements = replacements
313 313 self.backupfile = backupfile
314 314
315 315 def write(self, tr=None):
316 316 if tr:
317 317 tr.addfilegenerator('histedit-state', ('histedit-state',),
318 318 self._write, location='plain')
319 319 else:
320 320 with self.repo.vfs("histedit-state", "w") as f:
321 321 self._write(f)
322 322
323 323 def _write(self, fp):
324 324 fp.write('v1\n')
325 325 fp.write('%s\n' % node.hex(self.parentctxnode))
326 326 fp.write('%s\n' % node.hex(self.topmost))
327 327 fp.write('%s\n' % self.keep)
328 328 fp.write('%d\n' % len(self.actions))
329 329 for action in self.actions:
330 330 fp.write('%s\n' % action.tostate())
331 331 fp.write('%d\n' % len(self.replacements))
332 332 for replacement in self.replacements:
333 333 fp.write('%s%s\n' % (node.hex(replacement[0]), ''.join(node.hex(r)
334 334 for r in replacement[1])))
335 335 backupfile = self.backupfile
336 336 if not backupfile:
337 337 backupfile = ''
338 338 fp.write('%s\n' % backupfile)
339 339
340 340 def _load(self):
341 341 fp = self.repo.vfs('histedit-state', 'r')
342 342 lines = [l[:-1] for l in fp.readlines()]
343 343
344 344 index = 0
345 345 lines[index] # version number
346 346 index += 1
347 347
348 348 parentctxnode = node.bin(lines[index])
349 349 index += 1
350 350
351 351 topmost = node.bin(lines[index])
352 352 index += 1
353 353
354 354 keep = lines[index] == 'True'
355 355 index += 1
356 356
357 357 # Rules
358 358 rules = []
359 359 rulelen = int(lines[index])
360 360 index += 1
361 361 for i in xrange(rulelen):
362 362 ruleaction = lines[index]
363 363 index += 1
364 364 rule = lines[index]
365 365 index += 1
366 366 rules.append((ruleaction, rule))
367 367
368 368 # Replacements
369 369 replacements = []
370 370 replacementlen = int(lines[index])
371 371 index += 1
372 372 for i in xrange(replacementlen):
373 373 replacement = lines[index]
374 374 original = node.bin(replacement[:40])
375 375 succ = [node.bin(replacement[i:i + 40]) for i in
376 376 range(40, len(replacement), 40)]
377 377 replacements.append((original, succ))
378 378 index += 1
379 379
380 380 backupfile = lines[index]
381 381 index += 1
382 382
383 383 fp.close()
384 384
385 385 return parentctxnode, rules, keep, topmost, replacements, backupfile
386 386
387 387 def clear(self):
388 388 if self.inprogress():
389 389 self.repo.vfs.unlink('histedit-state')
390 390
391 391 def inprogress(self):
392 392 return self.repo.vfs.exists('histedit-state')
393 393
394 394
395 395 class histeditaction(object):
396 396 def __init__(self, state, node):
397 397 self.state = state
398 398 self.repo = state.repo
399 399 self.node = node
400 400
401 401 @classmethod
402 402 def fromrule(cls, state, rule):
403 403 """Parses the given rule, returning an instance of the histeditaction.
404 404 """
405 405 rulehash = rule.strip().split(' ', 1)[0]
406 406 try:
407 407 rev = node.bin(rulehash)
408 408 except TypeError:
409 409 raise error.ParseError("invalid changeset %s" % rulehash)
410 410 return cls(state, rev)
411 411
412 412 def verify(self, prev, expected, seen):
413 413 """ Verifies semantic correctness of the rule"""
414 414 repo = self.repo
415 415 ha = node.hex(self.node)
416 416 try:
417 417 self.node = repo[ha].node()
418 418 except error.RepoError:
419 419 raise error.ParseError(_('unknown changeset %s listed')
420 420 % ha[:12])
421 421 if self.node is not None:
422 422 self._verifynodeconstraints(prev, expected, seen)
423 423
424 424 def _verifynodeconstraints(self, prev, expected, seen):
425 425 # by default command need a node in the edited list
426 426 if self.node not in expected:
427 427 raise error.ParseError(_('%s "%s" changeset was not a candidate')
428 428 % (self.verb, node.short(self.node)),
429 429 hint=_('only use listed changesets'))
430 430 # and only one command per node
431 431 if self.node in seen:
432 432 raise error.ParseError(_('duplicated command for changeset %s') %
433 433 node.short(self.node))
434 434
435 435 def torule(self):
436 436 """build a histedit rule line for an action
437 437
438 438 by default lines are in the form:
439 439 <hash> <rev> <summary>
440 440 """
441 441 ctx = self.repo[self.node]
442 442 summary = _getsummary(ctx)
443 443 line = '%s %s %d %s' % (self.verb, ctx, ctx.rev(), summary)
444 444 # trim to 75 columns by default so it's not stupidly wide in my editor
445 445 # (the 5 more are left for verb)
446 446 maxlen = self.repo.ui.configint('histedit', 'linelen', default=80)
447 447 maxlen = max(maxlen, 22) # avoid truncating hash
448 448 return util.ellipsis(line, maxlen)
449 449
450 450 def tostate(self):
451 451 """Print an action in format used by histedit state files
452 452 (the first line is a verb, the remainder is the second)
453 453 """
454 454 return "%s\n%s" % (self.verb, node.hex(self.node))
455 455
456 456 def run(self):
457 457 """Runs the action. The default behavior is simply apply the action's
458 458 rulectx onto the current parentctx."""
459 459 self.applychange()
460 460 self.continuedirty()
461 461 return self.continueclean()
462 462
463 463 def applychange(self):
464 464 """Applies the changes from this action's rulectx onto the current
465 465 parentctx, but does not commit them."""
466 466 repo = self.repo
467 467 rulectx = repo[self.node]
468 468 repo.ui.pushbuffer(error=True, labeled=True)
469 469 hg.update(repo, self.state.parentctxnode, quietempty=True)
470 470 stats = applychanges(repo.ui, repo, rulectx, {})
471 471 if stats and stats[3] > 0:
472 472 buf = repo.ui.popbuffer()
473 473 repo.ui.write(*buf)
474 474 raise error.InterventionRequired(
475 475 _('Fix up the change (%s %s)') %
476 476 (self.verb, node.short(self.node)),
477 477 hint=_('hg histedit --continue to resume'))
478 478 else:
479 479 repo.ui.popbuffer()
480 480
481 481 def continuedirty(self):
482 482 """Continues the action when changes have been applied to the working
483 483 copy. The default behavior is to commit the dirty changes."""
484 484 repo = self.repo
485 485 rulectx = repo[self.node]
486 486
487 487 editor = self.commiteditor()
488 488 commit = commitfuncfor(repo, rulectx)
489 489
490 490 commit(text=rulectx.description(), user=rulectx.user(),
491 491 date=rulectx.date(), extra=rulectx.extra(), editor=editor)
492 492
493 493 def commiteditor(self):
494 494 """The editor to be used to edit the commit message."""
495 495 return False
496 496
497 497 def continueclean(self):
498 498 """Continues the action when the working copy is clean. The default
499 499 behavior is to accept the current commit as the new version of the
500 500 rulectx."""
501 501 ctx = self.repo['.']
502 502 if ctx.node() == self.state.parentctxnode:
503 503 self.repo.ui.warn(_('%s: skipping changeset (no changes)\n') %
504 504 node.short(self.node))
505 505 return ctx, [(self.node, tuple())]
506 506 if ctx.node() == self.node:
507 507 # Nothing changed
508 508 return ctx, []
509 509 return ctx, [(self.node, (ctx.node(),))]
510 510
511 511 def commitfuncfor(repo, src):
512 512 """Build a commit function for the replacement of <src>
513 513
514 514 This function ensure we apply the same treatment to all changesets.
515 515
516 516 - Add a 'histedit_source' entry in extra.
517 517
518 518 Note that fold has its own separated logic because its handling is a bit
519 519 different and not easily factored out of the fold method.
520 520 """
521 521 phasemin = src.phase()
522 522 def commitfunc(**kwargs):
523 523 overrides = {('phases', 'new-commit'): phasemin}
524 524 with repo.ui.configoverride(overrides, 'histedit'):
525 525 extra = kwargs.get('extra', {}).copy()
526 526 extra['histedit_source'] = src.hex()
527 527 kwargs['extra'] = extra
528 528 return repo.commit(**kwargs)
529 529 return commitfunc
530 530
531 531 def applychanges(ui, repo, ctx, opts):
532 532 """Merge changeset from ctx (only) in the current working directory"""
533 533 wcpar = repo.dirstate.parents()[0]
534 534 if ctx.p1().node() == wcpar:
535 535 # edits are "in place" we do not need to make any merge,
536 536 # just applies changes on parent for editing
537 537 cmdutil.revert(ui, repo, ctx, (wcpar, node.nullid), all=True)
538 538 stats = None
539 539 else:
540 540 try:
541 541 # ui.forcemerge is an internal variable, do not document
542 542 repo.ui.setconfig('ui', 'forcemerge', opts.get('tool', ''),
543 543 'histedit')
544 544 stats = mergemod.graft(repo, ctx, ctx.p1(), ['local', 'histedit'])
545 545 finally:
546 546 repo.ui.setconfig('ui', 'forcemerge', '', 'histedit')
547 547 return stats
548 548
549 549 def collapse(repo, first, last, commitopts, skipprompt=False):
550 550 """collapse the set of revisions from first to last as new one.
551 551
552 552 Expected commit options are:
553 553 - message
554 554 - date
555 555 - username
556 556 Commit message is edited in all cases.
557 557
558 558 This function works in memory."""
559 559 ctxs = list(repo.set('%d::%d', first, last))
560 560 if not ctxs:
561 561 return None
562 562 for c in ctxs:
563 563 if not c.mutable():
564 564 raise error.ParseError(
565 565 _("cannot fold into public change %s") % node.short(c.node()))
566 566 base = first.parents()[0]
567 567
568 568 # commit a new version of the old changeset, including the update
569 569 # collect all files which might be affected
570 570 files = set()
571 571 for ctx in ctxs:
572 572 files.update(ctx.files())
573 573
574 574 # Recompute copies (avoid recording a -> b -> a)
575 575 copied = copies.pathcopies(base, last)
576 576
577 577 # prune files which were reverted by the updates
578 578 files = [f for f in files if not cmdutil.samefile(f, last, base)]
579 579 # commit version of these files as defined by head
580 580 headmf = last.manifest()
581 581 def filectxfn(repo, ctx, path):
582 582 if path in headmf:
583 583 fctx = last[path]
584 584 flags = fctx.flags()
585 585 mctx = context.memfilectx(repo,
586 586 fctx.path(), fctx.data(),
587 587 islink='l' in flags,
588 588 isexec='x' in flags,
589 589 copied=copied.get(path))
590 590 return mctx
591 591 return None
592 592
593 593 if commitopts.get('message'):
594 594 message = commitopts['message']
595 595 else:
596 596 message = first.description()
597 597 user = commitopts.get('user')
598 598 date = commitopts.get('date')
599 599 extra = commitopts.get('extra')
600 600
601 601 parents = (first.p1().node(), first.p2().node())
602 602 editor = None
603 603 if not skipprompt:
604 604 editor = cmdutil.getcommiteditor(edit=True, editform='histedit.fold')
605 605 new = context.memctx(repo,
606 606 parents=parents,
607 607 text=message,
608 608 files=files,
609 609 filectxfn=filectxfn,
610 610 user=user,
611 611 date=date,
612 612 extra=extra,
613 613 editor=editor)
614 614 return repo.commitctx(new)
615 615
616 616 def _isdirtywc(repo):
617 617 return repo[None].dirty(missing=True)
618 618
619 619 def abortdirty():
620 620 raise error.Abort(_('working copy has pending changes'),
621 621 hint=_('amend, commit, or revert them and run histedit '
622 622 '--continue, or abort with histedit --abort'))
623 623
624 624 def action(verbs, message, priority=False, internal=False):
625 625 def wrap(cls):
626 626 assert not priority or not internal
627 627 verb = verbs[0]
628 628 if priority:
629 629 primaryactions.add(verb)
630 630 elif internal:
631 631 internalactions.add(verb)
632 632 elif len(verbs) > 1:
633 633 secondaryactions.add(verb)
634 634 else:
635 635 tertiaryactions.add(verb)
636 636
637 637 cls.verb = verb
638 638 cls.verbs = verbs
639 639 cls.message = message
640 640 for verb in verbs:
641 641 actiontable[verb] = cls
642 642 return cls
643 643 return wrap
644 644
645 645 @action(['pick', 'p'],
646 646 _('use commit'),
647 647 priority=True)
648 648 class pick(histeditaction):
649 649 def run(self):
650 650 rulectx = self.repo[self.node]
651 651 if rulectx.parents()[0].node() == self.state.parentctxnode:
652 652 self.repo.ui.debug('node %s unchanged\n' % node.short(self.node))
653 653 return rulectx, []
654 654
655 655 return super(pick, self).run()
656 656
657 657 @action(['edit', 'e'],
658 658 _('use commit, but stop for amending'),
659 659 priority=True)
660 660 class edit(histeditaction):
661 661 def run(self):
662 662 repo = self.repo
663 663 rulectx = repo[self.node]
664 664 hg.update(repo, self.state.parentctxnode, quietempty=True)
665 665 applychanges(repo.ui, repo, rulectx, {})
666 666 raise error.InterventionRequired(
667 667 _('Editing (%s), you may commit or record as needed now.')
668 668 % node.short(self.node),
669 669 hint=_('hg histedit --continue to resume'))
670 670
671 671 def commiteditor(self):
672 672 return cmdutil.getcommiteditor(edit=True, editform='histedit.edit')
673 673
674 674 @action(['fold', 'f'],
675 675 _('use commit, but combine it with the one above'))
676 676 class fold(histeditaction):
677 677 def verify(self, prev, expected, seen):
678 678 """ Verifies semantic correctness of the fold rule"""
679 679 super(fold, self).verify(prev, expected, seen)
680 680 repo = self.repo
681 681 if not prev:
682 682 c = repo[self.node].parents()[0]
683 683 elif not prev.verb in ('pick', 'base'):
684 684 return
685 685 else:
686 686 c = repo[prev.node]
687 687 if not c.mutable():
688 688 raise error.ParseError(
689 689 _("cannot fold into public change %s") % node.short(c.node()))
690 690
691 691
692 692 def continuedirty(self):
693 693 repo = self.repo
694 694 rulectx = repo[self.node]
695 695
696 696 commit = commitfuncfor(repo, rulectx)
697 697 commit(text='fold-temp-revision %s' % node.short(self.node),
698 698 user=rulectx.user(), date=rulectx.date(),
699 699 extra=rulectx.extra())
700 700
701 701 def continueclean(self):
702 702 repo = self.repo
703 703 ctx = repo['.']
704 704 rulectx = repo[self.node]
705 705 parentctxnode = self.state.parentctxnode
706 706 if ctx.node() == parentctxnode:
707 707 repo.ui.warn(_('%s: empty changeset\n') %
708 708 node.short(self.node))
709 709 return ctx, [(self.node, (parentctxnode,))]
710 710
711 711 parentctx = repo[parentctxnode]
712 712 newcommits = set(c.node() for c in repo.set('(%d::. - %d)', parentctx,
713 713 parentctx))
714 714 if not newcommits:
715 715 repo.ui.warn(_('%s: cannot fold - working copy is not a '
716 716 'descendant of previous commit %s\n') %
717 717 (node.short(self.node), node.short(parentctxnode)))
718 718 return ctx, [(self.node, (ctx.node(),))]
719 719
720 720 middlecommits = newcommits.copy()
721 721 middlecommits.discard(ctx.node())
722 722
723 723 return self.finishfold(repo.ui, repo, parentctx, rulectx, ctx.node(),
724 724 middlecommits)
725 725
726 726 def skipprompt(self):
727 727 """Returns true if the rule should skip the message editor.
728 728
729 729 For example, 'fold' wants to show an editor, but 'rollup'
730 730 doesn't want to.
731 731 """
732 732 return False
733 733
734 734 def mergedescs(self):
735 735 """Returns true if the rule should merge messages of multiple changes.
736 736
737 737 This exists mainly so that 'rollup' rules can be a subclass of
738 738 'fold'.
739 739 """
740 740 return True
741 741
742 742 def firstdate(self):
743 743 """Returns true if the rule should preserve the date of the first
744 744 change.
745 745
746 746 This exists mainly so that 'rollup' rules can be a subclass of
747 747 'fold'.
748 748 """
749 749 return False
750 750
751 751 def finishfold(self, ui, repo, ctx, oldctx, newnode, internalchanges):
752 752 parent = ctx.parents()[0].node()
753 753 repo.ui.pushbuffer()
754 754 hg.update(repo, parent)
755 755 repo.ui.popbuffer()
756 756 ### prepare new commit data
757 757 commitopts = {}
758 758 commitopts['user'] = ctx.user()
759 759 # commit message
760 760 if not self.mergedescs():
761 761 newmessage = ctx.description()
762 762 else:
763 763 newmessage = '\n***\n'.join(
764 764 [ctx.description()] +
765 765 [repo[r].description() for r in internalchanges] +
766 766 [oldctx.description()]) + '\n'
767 767 commitopts['message'] = newmessage
768 768 # date
769 769 if self.firstdate():
770 770 commitopts['date'] = ctx.date()
771 771 else:
772 772 commitopts['date'] = max(ctx.date(), oldctx.date())
773 773 extra = ctx.extra().copy()
774 774 # histedit_source
775 775 # note: ctx is likely a temporary commit but that the best we can do
776 776 # here. This is sufficient to solve issue3681 anyway.
777 777 extra['histedit_source'] = '%s,%s' % (ctx.hex(), oldctx.hex())
778 778 commitopts['extra'] = extra
779 779 phasemin = max(ctx.phase(), oldctx.phase())
780 780 overrides = {('phases', 'new-commit'): phasemin}
781 781 with repo.ui.configoverride(overrides, 'histedit'):
782 782 n = collapse(repo, ctx, repo[newnode], commitopts,
783 783 skipprompt=self.skipprompt())
784 784 if n is None:
785 785 return ctx, []
786 786 repo.ui.pushbuffer()
787 787 hg.update(repo, n)
788 788 repo.ui.popbuffer()
789 789 replacements = [(oldctx.node(), (newnode,)),
790 790 (ctx.node(), (n,)),
791 791 (newnode, (n,)),
792 792 ]
793 793 for ich in internalchanges:
794 794 replacements.append((ich, (n,)))
795 795 return repo[n], replacements
796 796
797 797 class base(histeditaction):
798 798
799 799 def run(self):
800 800 if self.repo['.'].node() != self.node:
801 801 mergemod.update(self.repo, self.node, False, True)
802 802 # branchmerge, force)
803 803 return self.continueclean()
804 804
805 805 def continuedirty(self):
806 806 abortdirty()
807 807
808 808 def continueclean(self):
809 809 basectx = self.repo['.']
810 810 return basectx, []
811 811
812 812 def _verifynodeconstraints(self, prev, expected, seen):
813 813 # base can only be use with a node not in the edited set
814 814 if self.node in expected:
815 815 msg = _('%s "%s" changeset was an edited list candidate')
816 816 raise error.ParseError(
817 817 msg % (self.verb, node.short(self.node)),
818 818 hint=_('base must only use unlisted changesets'))
819 819
820 820 @action(['_multifold'],
821 821 _(
822 822 """fold subclass used for when multiple folds happen in a row
823 823
824 824 We only want to fire the editor for the folded message once when
825 825 (say) four changes are folded down into a single change. This is
826 826 similar to rollup, but we should preserve both messages so that
827 827 when the last fold operation runs we can show the user all the
828 828 commit messages in their editor.
829 829 """),
830 830 internal=True)
831 831 class _multifold(fold):
832 832 def skipprompt(self):
833 833 return True
834 834
835 835 @action(["roll", "r"],
836 836 _("like fold, but discard this commit's description and date"))
837 837 class rollup(fold):
838 838 def mergedescs(self):
839 839 return False
840 840
841 841 def skipprompt(self):
842 842 return True
843 843
844 844 def firstdate(self):
845 845 return True
846 846
847 847 @action(["drop", "d"],
848 848 _('remove commit from history'))
849 849 class drop(histeditaction):
850 850 def run(self):
851 851 parentctx = self.repo[self.state.parentctxnode]
852 852 return parentctx, [(self.node, tuple())]
853 853
854 854 @action(["mess", "m"],
855 855 _('edit commit message without changing commit content'),
856 856 priority=True)
857 857 class message(histeditaction):
858 858 def commiteditor(self):
859 859 return cmdutil.getcommiteditor(edit=True, editform='histedit.mess')
860 860
861 861 def findoutgoing(ui, repo, remote=None, force=False, opts=None):
862 862 """utility function to find the first outgoing changeset
863 863
864 864 Used by initialization code"""
865 865 if opts is None:
866 866 opts = {}
867 867 dest = ui.expandpath(remote or 'default-push', remote or 'default')
868 868 dest, revs = hg.parseurl(dest, None)[:2]
869 869 ui.status(_('comparing with %s\n') % util.hidepassword(dest))
870 870
871 871 revs, checkout = hg.addbranchrevs(repo, repo, revs, None)
872 872 other = hg.peer(repo, opts, dest)
873 873
874 874 if revs:
875 875 revs = [repo.lookup(rev) for rev in revs]
876 876
877 877 outgoing = discovery.findcommonoutgoing(repo, other, revs, force=force)
878 878 if not outgoing.missing:
879 879 raise error.Abort(_('no outgoing ancestors'))
880 880 roots = list(repo.revs("roots(%ln)", outgoing.missing))
881 881 if 1 < len(roots):
882 882 msg = _('there are ambiguous outgoing revisions')
883 883 hint = _("see 'hg help histedit' for more detail")
884 884 raise error.Abort(msg, hint=hint)
885 885 return repo.lookup(roots[0])
886 886
887 887
888 888 @command('histedit',
889 889 [('', 'commands', '',
890 890 _('read history edits from the specified file'), _('FILE')),
891 891 ('c', 'continue', False, _('continue an edit already in progress')),
892 892 ('', 'edit-plan', False, _('edit remaining actions list')),
893 893 ('k', 'keep', False,
894 894 _("don't strip old nodes after edit is complete")),
895 895 ('', 'abort', False, _('abort an edit in progress')),
896 896 ('o', 'outgoing', False, _('changesets not found in destination')),
897 897 ('f', 'force', False,
898 898 _('force outgoing even for unrelated repositories')),
899 899 ('r', 'rev', [], _('first revision to be edited'), _('REV'))],
900 900 _("[OPTIONS] ([ANCESTOR] | --outgoing [URL])"))
901 901 def histedit(ui, repo, *freeargs, **opts):
902 902 """interactively edit changeset history
903 903
904 904 This command lets you edit a linear series of changesets (up to
905 905 and including the working directory, which should be clean).
906 906 You can:
907 907
908 908 - `pick` to [re]order a changeset
909 909
910 910 - `drop` to omit changeset
911 911
912 912 - `mess` to reword the changeset commit message
913 913
914 914 - `fold` to combine it with the preceding changeset (using the later date)
915 915
916 916 - `roll` like fold, but discarding this commit's description and date
917 917
918 918 - `edit` to edit this changeset (preserving date)
919 919
920 920 There are a number of ways to select the root changeset:
921 921
922 922 - Specify ANCESTOR directly
923 923
924 924 - Use --outgoing -- it will be the first linear changeset not
925 925 included in destination. (See :hg:`help config.paths.default-push`)
926 926
927 927 - Otherwise, the value from the "histedit.defaultrev" config option
928 928 is used as a revset to select the base revision when ANCESTOR is not
929 929 specified. The first revision returned by the revset is used. By
930 930 default, this selects the editable history that is unique to the
931 931 ancestry of the working directory.
932 932
933 933 .. container:: verbose
934 934
935 935 If you use --outgoing, this command will abort if there are ambiguous
936 936 outgoing revisions. For example, if there are multiple branches
937 937 containing outgoing revisions.
938 938
939 939 Use "min(outgoing() and ::.)" or similar revset specification
940 940 instead of --outgoing to specify edit target revision exactly in
941 941 such ambiguous situation. See :hg:`help revsets` for detail about
942 942 selecting revisions.
943 943
944 944 .. container:: verbose
945 945
946 946 Examples:
947 947
948 948 - A number of changes have been made.
949 949 Revision 3 is no longer needed.
950 950
951 951 Start history editing from revision 3::
952 952
953 953 hg histedit -r 3
954 954
955 955 An editor opens, containing the list of revisions,
956 956 with specific actions specified::
957 957
958 958 pick 5339bf82f0ca 3 Zworgle the foobar
959 959 pick 8ef592ce7cc4 4 Bedazzle the zerlog
960 960 pick 0a9639fcda9d 5 Morgify the cromulancy
961 961
962 962 Additional information about the possible actions
963 963 to take appears below the list of revisions.
964 964
965 965 To remove revision 3 from the history,
966 966 its action (at the beginning of the relevant line)
967 967 is changed to 'drop'::
968 968
969 969 drop 5339bf82f0ca 3 Zworgle the foobar
970 970 pick 8ef592ce7cc4 4 Bedazzle the zerlog
971 971 pick 0a9639fcda9d 5 Morgify the cromulancy
972 972
973 973 - A number of changes have been made.
974 974 Revision 2 and 4 need to be swapped.
975 975
976 976 Start history editing from revision 2::
977 977
978 978 hg histedit -r 2
979 979
980 980 An editor opens, containing the list of revisions,
981 981 with specific actions specified::
982 982
983 983 pick 252a1af424ad 2 Blorb a morgwazzle
984 984 pick 5339bf82f0ca 3 Zworgle the foobar
985 985 pick 8ef592ce7cc4 4 Bedazzle the zerlog
986 986
987 987 To swap revision 2 and 4, its lines are swapped
988 988 in the editor::
989 989
990 990 pick 8ef592ce7cc4 4 Bedazzle the zerlog
991 991 pick 5339bf82f0ca 3 Zworgle the foobar
992 992 pick 252a1af424ad 2 Blorb a morgwazzle
993 993
994 994 Returns 0 on success, 1 if user intervention is required (not only
995 995 for intentional "edit" command, but also for resolving unexpected
996 996 conflicts).
997 997 """
998 998 state = histeditstate(repo)
999 999 try:
1000 1000 state.wlock = repo.wlock()
1001 1001 state.lock = repo.lock()
1002 1002 _histedit(ui, repo, state, *freeargs, **opts)
1003 1003 finally:
1004 1004 release(state.lock, state.wlock)
1005 1005
1006 1006 goalcontinue = 'continue'
1007 1007 goalabort = 'abort'
1008 1008 goaleditplan = 'edit-plan'
1009 1009 goalnew = 'new'
1010 1010
1011 1011 def _getgoal(opts):
1012 1012 if opts.get('continue'):
1013 1013 return goalcontinue
1014 1014 if opts.get('abort'):
1015 1015 return goalabort
1016 1016 if opts.get('edit_plan'):
1017 1017 return goaleditplan
1018 1018 return goalnew
1019 1019
1020 1020 def _readfile(ui, path):
1021 1021 if path == '-':
1022 1022 with ui.timeblockedsection('histedit'):
1023 1023 return ui.fin.read()
1024 1024 else:
1025 1025 with open(path, 'rb') as f:
1026 1026 return f.read()
1027 1027
1028 1028 def _validateargs(ui, repo, state, freeargs, opts, goal, rules, revs):
1029 1029 # TODO only abort if we try to histedit mq patches, not just
1030 1030 # blanket if mq patches are applied somewhere
1031 1031 mq = getattr(repo, 'mq', None)
1032 1032 if mq and mq.applied:
1033 1033 raise error.Abort(_('source has mq patches applied'))
1034 1034
1035 1035 # basic argument incompatibility processing
1036 1036 outg = opts.get('outgoing')
1037 1037 editplan = opts.get('edit_plan')
1038 1038 abort = opts.get('abort')
1039 1039 force = opts.get('force')
1040 1040 if force and not outg:
1041 1041 raise error.Abort(_('--force only allowed with --outgoing'))
1042 1042 if goal == 'continue':
1043 1043 if any((outg, abort, revs, freeargs, rules, editplan)):
1044 1044 raise error.Abort(_('no arguments allowed with --continue'))
1045 1045 elif goal == 'abort':
1046 1046 if any((outg, revs, freeargs, rules, editplan)):
1047 1047 raise error.Abort(_('no arguments allowed with --abort'))
1048 1048 elif goal == 'edit-plan':
1049 1049 if any((outg, revs, freeargs)):
1050 1050 raise error.Abort(_('only --commands argument allowed with '
1051 1051 '--edit-plan'))
1052 1052 else:
1053 1053 if os.path.exists(os.path.join(repo.path, 'histedit-state')):
1054 1054 raise error.Abort(_('history edit already in progress, try '
1055 1055 '--continue or --abort'))
1056 1056 if outg:
1057 1057 if revs:
1058 1058 raise error.Abort(_('no revisions allowed with --outgoing'))
1059 1059 if len(freeargs) > 1:
1060 1060 raise error.Abort(
1061 1061 _('only one repo argument allowed with --outgoing'))
1062 1062 else:
1063 1063 revs.extend(freeargs)
1064 1064 if len(revs) == 0:
1065 1065 defaultrev = destutil.desthistedit(ui, repo)
1066 1066 if defaultrev is not None:
1067 1067 revs.append(defaultrev)
1068 1068
1069 1069 if len(revs) != 1:
1070 1070 raise error.Abort(
1071 1071 _('histedit requires exactly one ancestor revision'))
1072 1072
1073 1073 def _histedit(ui, repo, state, *freeargs, **opts):
1074 1074 goal = _getgoal(opts)
1075 1075 revs = opts.get('rev', [])
1076 1076 rules = opts.get('commands', '')
1077 1077 state.keep = opts.get('keep', False)
1078 1078
1079 1079 _validateargs(ui, repo, state, freeargs, opts, goal, rules, revs)
1080 1080
1081 1081 # rebuild state
1082 1082 if goal == goalcontinue:
1083 1083 state.read()
1084 1084 state = bootstrapcontinue(ui, state, opts)
1085 1085 elif goal == goaleditplan:
1086 1086 _edithisteditplan(ui, repo, state, rules)
1087 1087 return
1088 1088 elif goal == goalabort:
1089 1089 _aborthistedit(ui, repo, state)
1090 1090 return
1091 1091 else:
1092 1092 # goal == goalnew
1093 1093 _newhistedit(ui, repo, state, revs, freeargs, opts)
1094 1094
1095 1095 _continuehistedit(ui, repo, state)
1096 1096 _finishhistedit(ui, repo, state)
1097 1097
1098 1098 def _continuehistedit(ui, repo, state):
1099 1099 """This function runs after either:
1100 1100 - bootstrapcontinue (if the goal is 'continue')
1101 1101 - _newhistedit (if the goal is 'new')
1102 1102 """
1103 1103 # preprocess rules so that we can hide inner folds from the user
1104 1104 # and only show one editor
1105 1105 actions = state.actions[:]
1106 1106 for idx, (action, nextact) in enumerate(
1107 1107 zip(actions, actions[1:] + [None])):
1108 1108 if action.verb == 'fold' and nextact and nextact.verb == 'fold':
1109 1109 state.actions[idx].__class__ = _multifold
1110 1110
1111 1111 total = len(state.actions)
1112 1112 pos = 0
1113 1113 state.tr = None
1114 1114
1115 1115 # Force an initial state file write, so the user can run --abort/continue
1116 1116 # even if there's an exception before the first transaction serialize.
1117 1117 state.write()
1118 1118 try:
1119 1119 # Don't use singletransaction by default since it rolls the entire
1120 1120 # transaction back if an unexpected exception happens (like a
1121 1121 # pretxncommit hook throws, or the user aborts the commit msg editor).
1122 1122 if ui.configbool("histedit", "singletransaction", False):
1123 1123 # Don't use a 'with' for the transaction, since actions may close
1124 1124 # and reopen a transaction. For example, if the action executes an
1125 1125 # external process it may choose to commit the transaction first.
1126 1126 state.tr = repo.transaction('histedit')
1127 1127
1128 1128 while state.actions:
1129 1129 state.write(tr=state.tr)
1130 1130 actobj = state.actions[0]
1131 1131 pos += 1
1132 1132 ui.progress(_("editing"), pos, actobj.torule(),
1133 1133 _('changes'), total)
1134 1134 ui.debug('histedit: processing %s %s\n' % (actobj.verb,\
1135 1135 actobj.torule()))
1136 1136 parentctx, replacement_ = actobj.run()
1137 1137 state.parentctxnode = parentctx.node()
1138 1138 state.replacements.extend(replacement_)
1139 1139 state.actions.pop(0)
1140 1140
1141 1141 if state.tr is not None:
1142 1142 state.tr.close()
1143 1143 except error.InterventionRequired:
1144 1144 if state.tr is not None:
1145 1145 state.tr.close()
1146 1146 raise
1147 1147 except Exception:
1148 1148 if state.tr is not None:
1149 1149 state.tr.abort()
1150 1150 raise
1151 1151
1152 1152 state.write()
1153 1153 ui.progress(_("editing"), None)
1154 1154
1155 1155 def _finishhistedit(ui, repo, state):
1156 1156 """This action runs when histedit is finishing its session"""
1157 1157 repo.ui.pushbuffer()
1158 1158 hg.update(repo, state.parentctxnode, quietempty=True)
1159 1159 repo.ui.popbuffer()
1160 1160
1161 1161 mapping, tmpnodes, created, ntm = processreplacement(state)
1162 1162 if mapping:
1163 1163 for prec, succs in mapping.iteritems():
1164 1164 if not succs:
1165 1165 ui.debug('histedit: %s is dropped\n' % node.short(prec))
1166 1166 else:
1167 1167 ui.debug('histedit: %s is replaced by %s\n' % (
1168 1168 node.short(prec), node.short(succs[0])))
1169 1169 if len(succs) > 1:
1170 1170 m = 'histedit: %s'
1171 1171 for n in succs[1:]:
1172 1172 ui.debug(m % node.short(n))
1173 1173
1174 safecleanupnode(ui, repo, 'temp', tmpnodes)
1174 safecleanupnode(ui, repo, tmpnodes)
1175 1175
1176 1176 if not state.keep:
1177 1177 if mapping:
1178 1178 movebookmarks(ui, repo, mapping, state.topmost, ntm)
1179 1179 # TODO update mq state
1180 safecleanupnode(ui, repo, 'replaced', mapping)
1180 safecleanupnode(ui, repo, mapping)
1181 1181
1182 1182 state.clear()
1183 1183 if os.path.exists(repo.sjoin('undo')):
1184 1184 os.unlink(repo.sjoin('undo'))
1185 1185 if repo.vfs.exists('histedit-last-edit.txt'):
1186 1186 repo.vfs.unlink('histedit-last-edit.txt')
1187 1187
1188 1188 def _aborthistedit(ui, repo, state):
1189 1189 try:
1190 1190 state.read()
1191 1191 __, leafs, tmpnodes, __ = processreplacement(state)
1192 1192 ui.debug('restore wc to old parent %s\n'
1193 1193 % node.short(state.topmost))
1194 1194
1195 1195 # Recover our old commits if necessary
1196 1196 if not state.topmost in repo and state.backupfile:
1197 1197 backupfile = repo.vfs.join(state.backupfile)
1198 1198 f = hg.openpath(ui, backupfile)
1199 1199 gen = exchange.readbundle(ui, f, backupfile)
1200 1200 with repo.transaction('histedit.abort') as tr:
1201 1201 bundle2.applybundle(repo, gen, tr, source='histedit',
1202 1202 url='bundle:' + backupfile)
1203 1203
1204 1204 os.remove(backupfile)
1205 1205
1206 1206 # check whether we should update away
1207 1207 if repo.unfiltered().revs('parents() and (%n or %ln::)',
1208 1208 state.parentctxnode, leafs | tmpnodes):
1209 1209 hg.clean(repo, state.topmost, show_stats=True, quietempty=True)
1210 cleanupnode(ui, repo, 'created', tmpnodes)
1211 cleanupnode(ui, repo, 'temp', leafs)
1210 cleanupnode(ui, repo, tmpnodes)
1211 cleanupnode(ui, repo, leafs)
1212 1212 except Exception:
1213 1213 if state.inprogress():
1214 1214 ui.warn(_('warning: encountered an exception during histedit '
1215 1215 '--abort; the repository may not have been completely '
1216 1216 'cleaned up\n'))
1217 1217 raise
1218 1218 finally:
1219 1219 state.clear()
1220 1220
1221 1221 def _edithisteditplan(ui, repo, state, rules):
1222 1222 state.read()
1223 1223 if not rules:
1224 1224 comment = geteditcomment(ui,
1225 1225 node.short(state.parentctxnode),
1226 1226 node.short(state.topmost))
1227 1227 rules = ruleeditor(repo, ui, state.actions, comment)
1228 1228 else:
1229 1229 rules = _readfile(ui, rules)
1230 1230 actions = parserules(rules, state)
1231 1231 ctxs = [repo[act.node] \
1232 1232 for act in state.actions if act.node]
1233 1233 warnverifyactions(ui, repo, actions, state, ctxs)
1234 1234 state.actions = actions
1235 1235 state.write()
1236 1236
1237 1237 def _newhistedit(ui, repo, state, revs, freeargs, opts):
1238 1238 outg = opts.get('outgoing')
1239 1239 rules = opts.get('commands', '')
1240 1240 force = opts.get('force')
1241 1241
1242 1242 cmdutil.checkunfinished(repo)
1243 1243 cmdutil.bailifchanged(repo)
1244 1244
1245 1245 topmost, empty = repo.dirstate.parents()
1246 1246 if outg:
1247 1247 if freeargs:
1248 1248 remote = freeargs[0]
1249 1249 else:
1250 1250 remote = None
1251 1251 root = findoutgoing(ui, repo, remote, force, opts)
1252 1252 else:
1253 1253 rr = list(repo.set('roots(%ld)', scmutil.revrange(repo, revs)))
1254 1254 if len(rr) != 1:
1255 1255 raise error.Abort(_('The specified revisions must have '
1256 1256 'exactly one common root'))
1257 1257 root = rr[0].node()
1258 1258
1259 1259 revs = between(repo, root, topmost, state.keep)
1260 1260 if not revs:
1261 1261 raise error.Abort(_('%s is not an ancestor of working directory') %
1262 1262 node.short(root))
1263 1263
1264 1264 ctxs = [repo[r] for r in revs]
1265 1265 if not rules:
1266 1266 comment = geteditcomment(ui, node.short(root), node.short(topmost))
1267 1267 actions = [pick(state, r) for r in revs]
1268 1268 rules = ruleeditor(repo, ui, actions, comment)
1269 1269 else:
1270 1270 rules = _readfile(ui, rules)
1271 1271 actions = parserules(rules, state)
1272 1272 warnverifyactions(ui, repo, actions, state, ctxs)
1273 1273
1274 1274 parentctxnode = repo[root].parents()[0].node()
1275 1275
1276 1276 state.parentctxnode = parentctxnode
1277 1277 state.actions = actions
1278 1278 state.topmost = topmost
1279 1279 state.replacements = []
1280 1280
1281 1281 # Create a backup so we can always abort completely.
1282 1282 backupfile = None
1283 1283 if not obsolete.isenabled(repo, obsolete.createmarkersopt):
1284 1284 backupfile = repair._bundle(repo, [parentctxnode], [topmost], root,
1285 1285 'histedit')
1286 1286 state.backupfile = backupfile
1287 1287
1288 1288 def _getsummary(ctx):
1289 1289 # a common pattern is to extract the summary but default to the empty
1290 1290 # string
1291 1291 summary = ctx.description() or ''
1292 1292 if summary:
1293 1293 summary = summary.splitlines()[0]
1294 1294 return summary
1295 1295
1296 1296 def bootstrapcontinue(ui, state, opts):
1297 1297 repo = state.repo
1298 1298
1299 1299 ms = mergemod.mergestate.read(repo)
1300 1300 mergeutil.checkunresolved(ms)
1301 1301
1302 1302 if state.actions:
1303 1303 actobj = state.actions.pop(0)
1304 1304
1305 1305 if _isdirtywc(repo):
1306 1306 actobj.continuedirty()
1307 1307 if _isdirtywc(repo):
1308 1308 abortdirty()
1309 1309
1310 1310 parentctx, replacements = actobj.continueclean()
1311 1311
1312 1312 state.parentctxnode = parentctx.node()
1313 1313 state.replacements.extend(replacements)
1314 1314
1315 1315 return state
1316 1316
1317 1317 def between(repo, old, new, keep):
1318 1318 """select and validate the set of revision to edit
1319 1319
1320 1320 When keep is false, the specified set can't have children."""
1321 1321 ctxs = list(repo.set('%n::%n', old, new))
1322 1322 if ctxs and not keep:
1323 1323 if (not obsolete.isenabled(repo, obsolete.allowunstableopt) and
1324 1324 repo.revs('(%ld::) - (%ld)', ctxs, ctxs)):
1325 1325 raise error.Abort(_('can only histedit a changeset together '
1326 1326 'with all its descendants'))
1327 1327 if repo.revs('(%ld) and merge()', ctxs):
1328 1328 raise error.Abort(_('cannot edit history that contains merges'))
1329 1329 root = ctxs[0] # list is already sorted by repo.set
1330 1330 if not root.mutable():
1331 1331 raise error.Abort(_('cannot edit public changeset: %s') % root,
1332 1332 hint=_("see 'hg help phases' for details"))
1333 1333 return [c.node() for c in ctxs]
1334 1334
1335 1335 def ruleeditor(repo, ui, actions, editcomment=""):
1336 1336 """open an editor to edit rules
1337 1337
1338 1338 rules are in the format [ [act, ctx], ...] like in state.rules
1339 1339 """
1340 1340 if repo.ui.configbool("experimental", "histedit.autoverb"):
1341 1341 newact = util.sortdict()
1342 1342 for act in actions:
1343 1343 ctx = repo[act.node]
1344 1344 summary = _getsummary(ctx)
1345 1345 fword = summary.split(' ', 1)[0].lower()
1346 1346 added = False
1347 1347
1348 1348 # if it doesn't end with the special character '!' just skip this
1349 1349 if fword.endswith('!'):
1350 1350 fword = fword[:-1]
1351 1351 if fword in primaryactions | secondaryactions | tertiaryactions:
1352 1352 act.verb = fword
1353 1353 # get the target summary
1354 1354 tsum = summary[len(fword) + 1:].lstrip()
1355 1355 # safe but slow: reverse iterate over the actions so we
1356 1356 # don't clash on two commits having the same summary
1357 1357 for na, l in reversed(list(newact.iteritems())):
1358 1358 actx = repo[na.node]
1359 1359 asum = _getsummary(actx)
1360 1360 if asum == tsum:
1361 1361 added = True
1362 1362 l.append(act)
1363 1363 break
1364 1364
1365 1365 if not added:
1366 1366 newact[act] = []
1367 1367
1368 1368 # copy over and flatten the new list
1369 1369 actions = []
1370 1370 for na, l in newact.iteritems():
1371 1371 actions.append(na)
1372 1372 actions += l
1373 1373
1374 1374 rules = '\n'.join([act.torule() for act in actions])
1375 1375 rules += '\n\n'
1376 1376 rules += editcomment
1377 1377 rules = ui.edit(rules, ui.username(), {'prefix': 'histedit'},
1378 1378 repopath=repo.path)
1379 1379
1380 1380 # Save edit rules in .hg/histedit-last-edit.txt in case
1381 1381 # the user needs to ask for help after something
1382 1382 # surprising happens.
1383 1383 f = open(repo.vfs.join('histedit-last-edit.txt'), 'w')
1384 1384 f.write(rules)
1385 1385 f.close()
1386 1386
1387 1387 return rules
1388 1388
1389 1389 def parserules(rules, state):
1390 1390 """Read the histedit rules string and return list of action objects """
1391 1391 rules = [l for l in (r.strip() for r in rules.splitlines())
1392 1392 if l and not l.startswith('#')]
1393 1393 actions = []
1394 1394 for r in rules:
1395 1395 if ' ' not in r:
1396 1396 raise error.ParseError(_('malformed line "%s"') % r)
1397 1397 verb, rest = r.split(' ', 1)
1398 1398
1399 1399 if verb not in actiontable:
1400 1400 raise error.ParseError(_('unknown action "%s"') % verb)
1401 1401
1402 1402 action = actiontable[verb].fromrule(state, rest)
1403 1403 actions.append(action)
1404 1404 return actions
1405 1405
1406 1406 def warnverifyactions(ui, repo, actions, state, ctxs):
1407 1407 try:
1408 1408 verifyactions(actions, state, ctxs)
1409 1409 except error.ParseError:
1410 1410 if repo.vfs.exists('histedit-last-edit.txt'):
1411 1411 ui.warn(_('warning: histedit rules saved '
1412 1412 'to: .hg/histedit-last-edit.txt\n'))
1413 1413 raise
1414 1414
1415 1415 def verifyactions(actions, state, ctxs):
1416 1416 """Verify that there exists exactly one action per given changeset and
1417 1417 other constraints.
1418 1418
1419 1419 Will abort if there are to many or too few rules, a malformed rule,
1420 1420 or a rule on a changeset outside of the user-given range.
1421 1421 """
1422 1422 expected = set(c.node() for c in ctxs)
1423 1423 seen = set()
1424 1424 prev = None
1425 1425 for action in actions:
1426 1426 action.verify(prev, expected, seen)
1427 1427 prev = action
1428 1428 if action.node is not None:
1429 1429 seen.add(action.node)
1430 1430 missing = sorted(expected - seen) # sort to stabilize output
1431 1431
1432 1432 if state.repo.ui.configbool('histedit', 'dropmissing'):
1433 1433 if len(actions) == 0:
1434 1434 raise error.ParseError(_('no rules provided'),
1435 1435 hint=_('use strip extension to remove commits'))
1436 1436
1437 1437 drops = [drop(state, n) for n in missing]
1438 1438 # put the in the beginning so they execute immediately and
1439 1439 # don't show in the edit-plan in the future
1440 1440 actions[:0] = drops
1441 1441 elif missing:
1442 1442 raise error.ParseError(_('missing rules for changeset %s') %
1443 1443 node.short(missing[0]),
1444 1444 hint=_('use "drop %s" to discard, see also: '
1445 1445 "'hg help -e histedit.config'")
1446 1446 % node.short(missing[0]))
1447 1447
1448 1448 def adjustreplacementsfrommarkers(repo, oldreplacements):
1449 1449 """Adjust replacements from obsolescence markers
1450 1450
1451 1451 Replacements structure is originally generated based on
1452 1452 histedit's state and does not account for changes that are
1453 1453 not recorded there. This function fixes that by adding
1454 1454 data read from obsolescence markers"""
1455 1455 if not obsolete.isenabled(repo, obsolete.createmarkersopt):
1456 1456 return oldreplacements
1457 1457
1458 1458 unfi = repo.unfiltered()
1459 1459 nm = unfi.changelog.nodemap
1460 1460 obsstore = repo.obsstore
1461 1461 newreplacements = list(oldreplacements)
1462 1462 oldsuccs = [r[1] for r in oldreplacements]
1463 1463 # successors that have already been added to succstocheck once
1464 1464 seensuccs = set().union(*oldsuccs) # create a set from an iterable of tuples
1465 1465 succstocheck = list(seensuccs)
1466 1466 while succstocheck:
1467 1467 n = succstocheck.pop()
1468 1468 missing = nm.get(n) is None
1469 1469 markers = obsstore.successors.get(n, ())
1470 1470 if missing and not markers:
1471 1471 # dead end, mark it as such
1472 1472 newreplacements.append((n, ()))
1473 1473 for marker in markers:
1474 1474 nsuccs = marker[1]
1475 1475 newreplacements.append((n, nsuccs))
1476 1476 for nsucc in nsuccs:
1477 1477 if nsucc not in seensuccs:
1478 1478 seensuccs.add(nsucc)
1479 1479 succstocheck.append(nsucc)
1480 1480
1481 1481 return newreplacements
1482 1482
1483 1483 def processreplacement(state):
1484 1484 """process the list of replacements to return
1485 1485
1486 1486 1) the final mapping between original and created nodes
1487 1487 2) the list of temporary node created by histedit
1488 1488 3) the list of new commit created by histedit"""
1489 1489 replacements = adjustreplacementsfrommarkers(state.repo, state.replacements)
1490 1490 allsuccs = set()
1491 1491 replaced = set()
1492 1492 fullmapping = {}
1493 1493 # initialize basic set
1494 1494 # fullmapping records all operations recorded in replacement
1495 1495 for rep in replacements:
1496 1496 allsuccs.update(rep[1])
1497 1497 replaced.add(rep[0])
1498 1498 fullmapping.setdefault(rep[0], set()).update(rep[1])
1499 1499 new = allsuccs - replaced
1500 1500 tmpnodes = allsuccs & replaced
1501 1501 # Reduce content fullmapping into direct relation between original nodes
1502 1502 # and final node created during history edition
1503 1503 # Dropped changeset are replaced by an empty list
1504 1504 toproceed = set(fullmapping)
1505 1505 final = {}
1506 1506 while toproceed:
1507 1507 for x in list(toproceed):
1508 1508 succs = fullmapping[x]
1509 1509 for s in list(succs):
1510 1510 if s in toproceed:
1511 1511 # non final node with unknown closure
1512 1512 # We can't process this now
1513 1513 break
1514 1514 elif s in final:
1515 1515 # non final node, replace with closure
1516 1516 succs.remove(s)
1517 1517 succs.update(final[s])
1518 1518 else:
1519 1519 final[x] = succs
1520 1520 toproceed.remove(x)
1521 1521 # remove tmpnodes from final mapping
1522 1522 for n in tmpnodes:
1523 1523 del final[n]
1524 1524 # we expect all changes involved in final to exist in the repo
1525 1525 # turn `final` into list (topologically sorted)
1526 1526 nm = state.repo.changelog.nodemap
1527 1527 for prec, succs in final.items():
1528 1528 final[prec] = sorted(succs, key=nm.get)
1529 1529
1530 1530 # computed topmost element (necessary for bookmark)
1531 1531 if new:
1532 1532 newtopmost = sorted(new, key=state.repo.changelog.rev)[-1]
1533 1533 elif not final:
1534 1534 # Nothing rewritten at all. we won't need `newtopmost`
1535 1535 # It is the same as `oldtopmost` and `processreplacement` know it
1536 1536 newtopmost = None
1537 1537 else:
1538 1538 # every body died. The newtopmost is the parent of the root.
1539 1539 r = state.repo.changelog.rev
1540 1540 newtopmost = state.repo[sorted(final, key=r)[0]].p1().node()
1541 1541
1542 1542 return final, tmpnodes, new, newtopmost
1543 1543
1544 1544 def movetopmostbookmarks(repo, oldtopmost, newtopmost):
1545 1545 """Move bookmark from oldtopmost to newly created topmost
1546 1546
1547 1547 This is arguably a feature and we may only want that for the active
1548 1548 bookmark. But the behavior is kept compatible with the old version for now.
1549 1549 """
1550 1550 if not oldtopmost or not newtopmost:
1551 1551 return
1552 1552 oldbmarks = repo.nodebookmarks(oldtopmost)
1553 1553 if oldbmarks:
1554 1554 with repo.lock(), repo.transaction('histedit') as tr:
1555 1555 marks = repo._bookmarks
1556 1556 for name in oldbmarks:
1557 1557 marks[name] = newtopmost
1558 1558 marks.recordchange(tr)
1559 1559
1560 1560 def movebookmarks(ui, repo, mapping, oldtopmost, newtopmost):
1561 1561 """Move bookmark from old to newly created node"""
1562 1562 if not mapping:
1563 1563 # if nothing got rewritten there is not purpose for this function
1564 1564 return
1565 1565 movetopmostbookmarks(repo, oldtopmost, newtopmost)
1566 1566 moves = []
1567 1567 for bk, old in sorted(repo._bookmarks.iteritems()):
1568 1568 base = old
1569 1569 new = mapping.get(base, None)
1570 1570 if new is None:
1571 1571 continue
1572 1572 while not new:
1573 1573 # base is killed, trying with parent
1574 1574 base = repo[base].p1().node()
1575 1575 new = mapping.get(base, (base,))
1576 1576 # nothing to move
1577 1577 moves.append((bk, new[-1]))
1578 1578 if moves:
1579 1579 lock = tr = None
1580 1580 try:
1581 1581 lock = repo.lock()
1582 1582 tr = repo.transaction('histedit')
1583 1583 marks = repo._bookmarks
1584 1584 for mark, new in moves:
1585 1585 old = marks[mark]
1586 1586 marks[mark] = new
1587 1587 marks.recordchange(tr)
1588 1588 tr.close()
1589 1589 finally:
1590 1590 release(tr, lock)
1591 1591
1592 def cleanupnode(ui, repo, name, nodes):
1592 def cleanupnode(ui, repo, nodes):
1593 1593 """strip a group of nodes from the repository
1594 1594
1595 1595 The set of node to strip may contains unknown nodes."""
1596 1596 with repo.lock():
1597 1597 # do not let filtering get in the way of the cleanse
1598 1598 # we should probably get rid of obsolescence marker created during the
1599 1599 # histedit, but we currently do not have such information.
1600 1600 repo = repo.unfiltered()
1601 1601 # Find all nodes that need to be stripped
1602 1602 # (we use %lr instead of %ln to silently ignore unknown items)
1603 1603 nm = repo.changelog.nodemap
1604 1604 nodes = sorted(n for n in nodes if n in nm)
1605 1605 roots = [c.node() for c in repo.set("roots(%ln)", nodes)]
1606 1606 for c in roots:
1607 1607 # We should process node in reverse order to strip tip most first.
1608 1608 # but this trigger a bug in changegroup hook.
1609 1609 # This would reduce bundle overhead
1610 1610 repair.strip(ui, repo, c)
1611 1611
1612 def safecleanupnode(ui, repo, name, nodes):
1612 def safecleanupnode(ui, repo, nodes):
1613 1613 """strip or obsolete nodes
1614 1614
1615 1615 nodes could be either a set or dict which maps to replacements.
1616 1616 nodes could be unknown (outside the repo).
1617 1617 """
1618 1618 supportsmarkers = obsolete.isenabled(repo, obsolete.createmarkersopt)
1619 1619 if supportsmarkers:
1620 1620 if util.safehasattr(nodes, 'get'):
1621 1621 # nodes is a dict-like mapping
1622 1622 # use unfiltered repo for successors in case they are hidden
1623 1623 urepo = repo.unfiltered()
1624 1624 def getmarker(prec):
1625 1625 succs = tuple(urepo[n] for n in nodes.get(prec, ()))
1626 1626 return (repo[prec], succs)
1627 1627 else:
1628 1628 # nodes is a set-like
1629 1629 def getmarker(prec):
1630 1630 return (repo[prec], ())
1631 1631 # sort by revision number because it sound "right"
1632 1632 sortednodes = sorted([n for n in nodes if n in repo],
1633 1633 key=repo.changelog.rev)
1634 1634 markers = [getmarker(t) for t in sortednodes]
1635 1635 if markers:
1636 1636 obsolete.createmarkers(repo, markers, operation='histedit')
1637 1637 else:
1638 return cleanupnode(ui, repo, name, nodes)
1638 return cleanupnode(ui, repo, nodes)
1639 1639
1640 1640 def stripwrapper(orig, ui, repo, nodelist, *args, **kwargs):
1641 1641 if isinstance(nodelist, str):
1642 1642 nodelist = [nodelist]
1643 1643 if os.path.exists(os.path.join(repo.path, 'histedit-state')):
1644 1644 state = histeditstate(repo)
1645 1645 state.read()
1646 1646 histedit_nodes = {action.node for action
1647 1647 in state.actions if action.node}
1648 1648 common_nodes = histedit_nodes & set(nodelist)
1649 1649 if common_nodes:
1650 1650 raise error.Abort(_("histedit in progress, can't strip %s")
1651 1651 % ', '.join(node.short(x) for x in common_nodes))
1652 1652 return orig(ui, repo, nodelist, *args, **kwargs)
1653 1653
1654 1654 extensions.wrapfunction(repair, 'strip', stripwrapper)
1655 1655
1656 1656 def summaryhook(ui, repo):
1657 1657 if not os.path.exists(repo.vfs.join('histedit-state')):
1658 1658 return
1659 1659 state = histeditstate(repo)
1660 1660 state.read()
1661 1661 if state.actions:
1662 1662 # i18n: column positioning for "hg summary"
1663 1663 ui.write(_('hist: %s (histedit --continue)\n') %
1664 1664 (ui.label(_('%d remaining'), 'histedit.remaining') %
1665 1665 len(state.actions)))
1666 1666
1667 1667 def extsetup(ui):
1668 1668 cmdutil.summaryhooks.add('histedit', summaryhook)
1669 1669 cmdutil.unfinishedstates.append(
1670 1670 ['histedit-state', False, True, _('histedit in progress'),
1671 1671 _("use 'hg histedit --continue' or 'hg histedit --abort'")])
1672 1672 cmdutil.afterresolvedstates.append(
1673 1673 ['histedit-state', _('hg histedit --continue')])
1674 1674 if ui.configbool("experimental", "histeditng"):
1675 1675 globals()['base'] = action(['base', 'b'],
1676 1676 _('checkout changeset and apply further changesets from there')
1677 1677 )(base)
General Comments 0
You need to be logged in to leave comments. Login now