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