histedit.py
1598 lines
| 55.9 KiB
| text/x-python
|
PythonLexer
/ hgext / histedit.py
Augie Fackler
|
r17064 | # histedit.py - interactive history editing for mercurial | ||
# | ||||
# Copyright 2009 Augie Fackler <raf@durin42.com> | ||||
# | ||||
# This software may be used and distributed according to the terms of the | ||||
# GNU General Public License version 2 or any later version. | ||||
Augie Fackler
|
r17131 | """interactive history editing | ||
With this extension installed, Mercurial gains one new command: histedit. Usage | ||||
is as follows, assuming the following history:: | ||||
@ 3[tip] 7c2fd3b9020c 2009-04-27 18:04 -0500 durin42 | ||||
| Add delta | ||||
| | ||||
o 2 030b686bedc4 2009-04-27 18:04 -0500 durin42 | ||||
| Add gamma | ||||
| | ||||
o 1 c561b4e977df 2009-04-27 18:04 -0500 durin42 | ||||
| Add beta | ||||
| | ||||
o 0 d8d2fcd0e319 2009-04-27 18:04 -0500 durin42 | ||||
Add alpha | ||||
If you were to run ``hg histedit c561b4e977df``, you would see the following | ||||
file open in your editor:: | ||||
pick c561b4e977df Add beta | ||||
pick 030b686bedc4 Add gamma | ||||
pick 7c2fd3b9020c Add delta | ||||
FUJIWARA Katsunori
|
r18322 | # Edit history between c561b4e977df and 7c2fd3b9020c | ||
Augie Fackler
|
r17131 | # | ||
Adrian Zgorzałek
|
r20503 | # Commits are listed from least to most recent | ||
# | ||||
Augie Fackler
|
r17131 | # Commands: | ||
# p, pick = use commit | ||||
# e, edit = use commit, but stop for amending | ||||
Matt Mackall
|
r20511 | # f, fold = use commit, but combine it with the one above | ||
Mike Edgar
|
r22152 | # r, roll = like fold, but discard this commit's description | ||
Augie Fackler
|
r17131 | # d, drop = remove commit from history | ||
timeless@mozdev.org
|
r26100 | # m, mess = edit commit message without changing commit content | ||
Augie Fackler
|
r17131 | # | ||
In this file, lines beginning with ``#`` are ignored. You must specify a rule | ||||
for each revision in your history. For example, if you had meant to add gamma | ||||
before beta, and then wanted to add delta in the same revision as beta, you | ||||
would reorganize the file to look like this:: | ||||
pick 030b686bedc4 Add gamma | ||||
pick c561b4e977df Add beta | ||||
fold 7c2fd3b9020c Add delta | ||||
FUJIWARA Katsunori
|
r18322 | # Edit history between c561b4e977df and 7c2fd3b9020c | ||
Augie Fackler
|
r17131 | # | ||
Adrian Zgorzałek
|
r20503 | # Commits are listed from least to most recent | ||
# | ||||
Augie Fackler
|
r17131 | # Commands: | ||
# p, pick = use commit | ||||
# e, edit = use commit, but stop for amending | ||||
Matt Mackall
|
r20511 | # f, fold = use commit, but combine it with the one above | ||
Mike Edgar
|
r22152 | # r, roll = like fold, but discard this commit's description | ||
Augie Fackler
|
r17131 | # d, drop = remove commit from history | ||
timeless@mozdev.org
|
r26100 | # m, mess = edit commit message without changing commit content | ||
Augie Fackler
|
r17131 | # | ||
At which point you close the editor and ``histedit`` starts working. When you | ||||
specify a ``fold`` operation, ``histedit`` will open an editor when it folds | ||||
those revisions together, offering you a chance to clean up the commit message:: | ||||
Add beta | ||||
*** | ||||
Add delta | ||||
Augie Fackler
|
r17064 | |||
Augie Fackler
|
r17131 | Edit the commit message to your liking, then close the editor. For | ||
this example, let's assume that the commit message was changed to | ||||
``Add beta and delta.`` After histedit has run and had a chance to | ||||
remove any old or temporary revisions it needed, the history looks | ||||
like this:: | ||||
@ 2[tip] 989b4d060121 2009-04-27 18:04 -0500 durin42 | ||||
| Add beta and delta. | ||||
| | ||||
o 1 081603921c3f 2009-04-27 18:04 -0500 durin42 | ||||
| Add gamma | ||||
| | ||||
o 0 d8d2fcd0e319 2009-04-27 18:04 -0500 durin42 | ||||
Add alpha | ||||
Note that ``histedit`` does *not* remove any revisions (even its own temporary | ||||
ones) until after it has completed all the editing operations, so it will | ||||
probably perform several strip operations when it's done. For the above example, | ||||
it had to run strip twice. Strip can be slow depending on a variety of factors, | ||||
so you might need to be a little patient. You can choose to keep the original | ||||
revisions by passing the ``--keep`` flag. | ||||
The ``edit`` operation will drop you back to a command prompt, | ||||
allowing you to edit files freely, or even use ``hg record`` to commit | ||||
some changes as a separate commit. When you're done, any remaining | ||||
uncommitted changes will be committed as well. When done, run ``hg | ||||
histedit --continue`` to finish this step. You'll be prompted for a | ||||
new commit message, but the default commit message will be the | ||||
original message for the ``edit`` ed revision. | ||||
The ``message`` operation will give you a chance to revise a commit | ||||
message without changing the contents. It's a shortcut for doing | ||||
``edit`` immediately followed by `hg histedit --continue``. | ||||
If ``histedit`` encounters a conflict when moving a revision (while | ||||
handling ``pick`` or ``fold``), it'll stop in a similar manner to | ||||
``edit`` with the difference that it won't prompt you for a commit | ||||
message when done. If you decide at this point that you don't like how | ||||
much work it will be to rearrange history, or that you made a mistake, | ||||
you can use ``hg histedit --abort`` to abandon the new changes you | ||||
have made and return to the state before you attempted to edit your | ||||
history. | ||||
FUJIWARA Katsunori
|
r18323 | If we clone the histedit-ed example repository above and add four more | ||
changes, such that we have the following history:: | ||||
Augie Fackler
|
r17131 | |||
@ 6[tip] 038383181893 2009-04-27 18:04 -0500 stefan | ||||
| Add theta | ||||
| | ||||
o 5 140988835471 2009-04-27 18:04 -0500 stefan | ||||
| Add eta | ||||
| | ||||
o 4 122930637314 2009-04-27 18:04 -0500 stefan | ||||
| Add zeta | ||||
| | ||||
o 3 836302820282 2009-04-27 18:04 -0500 stefan | ||||
| Add epsilon | ||||
| | ||||
o 2 989b4d060121 2009-04-27 18:04 -0500 durin42 | ||||
| Add beta and delta. | ||||
| | ||||
o 1 081603921c3f 2009-04-27 18:04 -0500 durin42 | ||||
| Add gamma | ||||
| | ||||
o 0 d8d2fcd0e319 2009-04-27 18:04 -0500 durin42 | ||||
Add alpha | ||||
If you run ``hg histedit --outgoing`` on the clone then it is the same | ||||
as running ``hg histedit 836302820282``. If you need plan to push to a | ||||
repository that Mercurial does not detect to be related to the source | ||||
repo, you can add a ``--force`` option. | ||||
Mateusz Kwapich
|
r24199 | |||
Mateusz Kwapich
|
r27414 | Config | ||
------ | ||||
Mateusz Kwapich
|
r24199 | Histedit rule lines are truncated to 80 characters by default. You | ||
timeless@mozdev.org
|
r26171 | can customize this behavior by setting a different length in your | ||
FUJIWARA Katsunori
|
r24869 | configuration file:: | ||
Mateusz Kwapich
|
r24199 | |||
FUJIWARA Katsunori
|
r24869 | [histedit] | ||
linelen = 120 # truncate rule lines at 120 characters | ||||
Gregory Szorc
|
r27262 | |||
``hg histedit`` attempts to automatically choose an appropriate base | ||||
revision to use. To change which base revision is used, define a | ||||
revset in your configuration file:: | ||||
[histedit] | ||||
defaultrev = only(.) & draft() | ||||
Mateusz Kwapich
|
r27414 | |||
By default each edited revision needs to be present in histedit commands. | ||||
To remove revision you need to use ``drop`` operation. You can configure | ||||
FUJIWARA Katsunori
|
r27957 | the drop to be implicit for missing commits by adding:: | ||
Mateusz Kwapich
|
r27414 | |||
[histedit] | ||||
dropmissing = True | ||||
Augie Fackler
|
r17064 | """ | ||
Augie Fackler
|
r17131 | |||
Bryan O'Sullivan
|
r27534 | import pickle | ||
Siddharth Agarwal
|
r22368 | import errno | ||
Augie Fackler
|
r17064 | import os | ||
Pierre-Yves David
|
r19018 | import sys | ||
Augie Fackler
|
r17064 | |||
Pierre-Yves David
|
r26798 | from mercurial import bundle2 | ||
Augie Fackler
|
r17064 | from mercurial import cmdutil | ||
from mercurial import discovery | ||||
from mercurial import error | ||||
Pierre-Yves David
|
r17644 | from mercurial import copies | ||
from mercurial import context | ||||
Gregory Szorc
|
r27262 | from mercurial import destutil | ||
Durham Goode
|
r24757 | from mercurial import exchange | ||
Mateusz Kwapich
|
r24111 | from mercurial import extensions | ||
Augie Fackler
|
r17064 | from mercurial import hg | ||
from mercurial import node | ||||
from mercurial import repair | ||||
Augie Fackler
|
r21950 | from mercurial import scmutil | ||
Augie Fackler
|
r17064 | from mercurial import util | ||
Pierre-Yves David
|
r17759 | from mercurial import obsolete | ||
Pierre-Yves David
|
r17647 | from mercurial import merge as mergemod | ||
Siddharth Agarwal
|
r20071 | from mercurial.lock import release | ||
Augie Fackler
|
r17064 | from mercurial.i18n import _ | ||
Adrian Buehlmann
|
r17147 | cmdtable = {} | ||
command = cmdutil.command(cmdtable) | ||||
Augie Fackler
|
r27086 | class _constraints(object): | ||
# aborts if there are multiple rules for one node | ||||
noduplicates = 'noduplicates' | ||||
# abort if the node does belong to edited stack | ||||
forceother = 'forceother' | ||||
# abort if the node doesn't belong to edited stack | ||||
noother = 'noother' | ||||
@classmethod | ||||
def known(cls): | ||||
return set([v for k, v in cls.__dict__.items() if k[0] != '_']) | ||||
Augie Fackler
|
r25186 | # Note for extension authors: ONLY specify testedwith = 'internal' for | ||
# extensions which SHIP WITH MERCURIAL. Non-mainline extensions should | ||||
# be specifying the version(s) of Mercurial they are tested with, or | ||||
# leave the attribute unspecified. | ||||
Augie Fackler
|
r17069 | testedwith = 'internal' | ||
Augie Fackler
|
r17064 | |||
timeless
|
r27675 | actiontable = {} | ||
primaryactions = set() | ||||
secondaryactions = set() | ||||
tertiaryactions = set() | ||||
internalactions = set() | ||||
Mateusz Kwapich
|
r28592 | def geteditcomment(ui, first, last): | ||
timeless
|
r27673 | """ construct the editor comment | ||
The comment includes:: | ||||
- an intro | ||||
timeless
|
r27674 | - sorted primary commands | ||
- sorted short commands | ||||
timeless
|
r27675 | - sorted long commands | ||
Mateusz Kwapich
|
r28592 | - additional hints | ||
timeless
|
r27673 | |||
Commands are only included once. | ||||
""" | ||||
intro = _("""Edit history between %s and %s | ||||
Commits are listed from least to most recent | ||||
liscju
|
r28396 | You can reorder changesets by reordering the lines | ||
timeless
|
r27675 | Commands: | ||
FUJIWARA Katsunori
|
r17315 | """) | ||
timeless
|
r27675 | actions = [] | ||
def addverb(v): | ||||
a = actiontable[v] | ||||
lines = a.message.split("\n") | ||||
if len(a.verbs): | ||||
v = ', '.join(sorted(a.verbs, key=lambda v: len(v))) | ||||
actions.append(" %s = %s" % (v, lines[0])) | ||||
actions.extend([' %s' for l in lines[1:]]) | ||||
for v in ( | ||||
sorted(primaryactions) + | ||||
sorted(secondaryactions) + | ||||
sorted(tertiaryactions) | ||||
): | ||||
addverb(v) | ||||
actions.append('') | ||||
Augie Fackler
|
r17064 | |||
Mateusz Kwapich
|
r28592 | hints = [] | ||
if ui.configbool('histedit', 'dropmissing'): | ||||
hints.append("Deleting a changeset from the list " | ||||
"will DISCARD it from the edited history!") | ||||
lines = (intro % (first, last)).split('\n') + actions + hints | ||||
return ''.join(['# %s\n' % l if l else '#\n' for l in lines]) | ||||
timeless
|
r27673 | |||
David Soria Parra
|
r22976 | class histeditstate(object): | ||
Mateusz Kwapich
|
r27208 | def __init__(self, repo, parentctxnode=None, actions=None, keep=None, | ||
David Soria Parra
|
r22984 | topmost=None, replacements=None, lock=None, wlock=None): | ||
David Soria Parra
|
r22976 | self.repo = repo | ||
Mateusz Kwapich
|
r27208 | self.actions = actions | ||
David Soria Parra
|
r22976 | self.keep = keep | ||
self.topmost = topmost | ||||
Mateusz Kwapich
|
r24112 | self.parentctxnode = parentctxnode | ||
David Soria Parra
|
r22984 | self.lock = lock | ||
self.wlock = wlock | ||||
Durham Goode
|
r24757 | self.backupfile = None | ||
David Soria Parra
|
r22976 | if replacements is None: | ||
self.replacements = [] | ||||
else: | ||||
self.replacements = replacements | ||||
David Soria Parra
|
r22983 | def read(self): | ||
Augie Fackler
|
r22986 | """Load histedit state from disk and set fields appropriately.""" | ||
David Soria Parra
|
r22983 | try: | ||
Bryan O'Sullivan
|
r27527 | state = self.repo.vfs.read('histedit-state') | ||
Gregory Szorc
|
r25660 | except IOError as err: | ||
David Soria Parra
|
r22983 | if err.errno != errno.ENOENT: | ||
raise | ||||
timeless
|
r28123 | cmdutil.wrongtooltocontinue(self.repo, _('histedit')) | ||
David Soria Parra
|
r22983 | |||
Bryan O'Sullivan
|
r27527 | if state.startswith('v1\n'): | ||
data = self._load() | ||||
parentctxnode, rules, keep, topmost, replacements, backupfile = data | ||||
else: | ||||
data = pickle.loads(state) | ||||
Durham Goode
|
r24756 | parentctxnode, rules, keep, topmost, replacements = data | ||
Durham Goode
|
r24757 | backupfile = None | ||
David Soria Parra
|
r22983 | |||
Mateusz Kwapich
|
r24112 | self.parentctxnode = parentctxnode | ||
Mateusz Kwapich
|
r27208 | rules = "\n".join(["%s %s" % (verb, rest) for [verb, rest] in rules]) | ||
actions = parserules(rules, self) | ||||
self.actions = actions | ||||
David Soria Parra
|
r22983 | self.keep = keep | ||
self.topmost = topmost | ||||
self.replacements = replacements | ||||
Durham Goode
|
r24757 | self.backupfile = backupfile | ||
David Soria Parra
|
r22983 | |||
David Soria Parra
|
r22976 | def write(self): | ||
fp = self.repo.vfs('histedit-state', 'w') | ||||
Durham Goode
|
r24756 | fp.write('v1\n') | ||
fp.write('%s\n' % node.hex(self.parentctxnode)) | ||||
fp.write('%s\n' % node.hex(self.topmost)) | ||||
fp.write('%s\n' % self.keep) | ||||
Mateusz Kwapich
|
r27208 | fp.write('%d\n' % len(self.actions)) | ||
for action in self.actions: | ||||
fp.write('%s\n' % action.tostate()) | ||||
Durham Goode
|
r24756 | fp.write('%d\n' % len(self.replacements)) | ||
for replacement in self.replacements: | ||||
fp.write('%s%s\n' % (node.hex(replacement[0]), ''.join(node.hex(r) | ||||
for r in replacement[1]))) | ||||
Durham Goode
|
r24958 | backupfile = self.backupfile | ||
if not backupfile: | ||||
backupfile = '' | ||||
fp.write('%s\n' % backupfile) | ||||
David Soria Parra
|
r22976 | fp.close() | ||
Durham Goode
|
r24756 | def _load(self): | ||
fp = self.repo.vfs('histedit-state', 'r') | ||||
lines = [l[:-1] for l in fp.readlines()] | ||||
index = 0 | ||||
lines[index] # version number | ||||
index += 1 | ||||
parentctxnode = node.bin(lines[index]) | ||||
index += 1 | ||||
topmost = node.bin(lines[index]) | ||||
index += 1 | ||||
keep = lines[index] == 'True' | ||||
index += 1 | ||||
# Rules | ||||
rules = [] | ||||
rulelen = int(lines[index]) | ||||
index += 1 | ||||
for i in xrange(rulelen): | ||||
Durham Goode
|
r24810 | ruleaction = lines[index] | ||
index += 1 | ||||
Durham Goode
|
r24756 | rule = lines[index] | ||
index += 1 | ||||
Durham Goode
|
r24810 | rules.append((ruleaction, rule)) | ||
Durham Goode
|
r24756 | |||
# Replacements | ||||
replacements = [] | ||||
replacementlen = int(lines[index]) | ||||
index += 1 | ||||
for i in xrange(replacementlen): | ||||
replacement = lines[index] | ||||
original = node.bin(replacement[:40]) | ||||
succ = [node.bin(replacement[i:i + 40]) for i in | ||||
range(40, len(replacement), 40)] | ||||
replacements.append((original, succ)) | ||||
index += 1 | ||||
Durham Goode
|
r24757 | backupfile = lines[index] | ||
index += 1 | ||||
Durham Goode
|
r24756 | fp.close() | ||
Durham Goode
|
r24757 | return parentctxnode, rules, keep, topmost, replacements, backupfile | ||
Durham Goode
|
r24756 | |||
David Soria Parra
|
r22978 | def clear(self): | ||
Christian Delahousse
|
r26583 | if self.inprogress(): | ||
self.repo.vfs.unlink('histedit-state') | ||||
David Soria Parra
|
r22978 | |||
Christian Delahousse
|
r26582 | def inprogress(self): | ||
return self.repo.vfs.exists('histedit-state') | ||||
Mateusz Kwapich
|
r27200 | |||
Durham Goode
|
r24765 | class histeditaction(object): | ||
def __init__(self, state, node): | ||||
self.state = state | ||||
self.repo = state.repo | ||||
self.node = node | ||||
@classmethod | ||||
def fromrule(cls, state, rule): | ||||
"""Parses the given rule, returning an instance of the histeditaction. | ||||
""" | ||||
rulehash = rule.strip().split(' ', 1)[0] | ||||
timeless
|
r27547 | try: | ||
rev = node.bin(rulehash) | ||||
except TypeError: | ||||
raise error.ParseError("invalid changeset %s" % rulehash) | ||||
return cls(state, rev) | ||||
Mateusz Kwapich
|
r27202 | |||
timeless
|
r27541 | def verify(self, prev): | ||
Mateusz Kwapich
|
r27202 | """ Verifies semantic correctness of the rule""" | ||
repo = self.repo | ||||
ha = node.hex(self.node) | ||||
Durham Goode
|
r24765 | try: | ||
Mateusz Kwapich
|
r27202 | self.node = repo[ha].node() | ||
Durham Goode
|
r24765 | except error.RepoError: | ||
timeless
|
r27545 | raise error.ParseError(_('unknown changeset %s listed') | ||
Mateusz Kwapich
|
r27202 | % ha[:12]) | ||
Durham Goode
|
r24765 | |||
Mateusz Kwapich
|
r27203 | def torule(self): | ||
"""build a histedit rule line for an action | ||||
by default lines are in the form: | ||||
<hash> <rev> <summary> | ||||
""" | ||||
ctx = self.repo[self.node] | ||||
summary = '' | ||||
if ctx.description(): | ||||
summary = ctx.description().splitlines()[0] | ||||
line = '%s %s %d %s' % (self.verb, ctx, ctx.rev(), summary) | ||||
# trim to 75 columns by default so it's not stupidly wide in my editor | ||||
# (the 5 more are left for verb) | ||||
maxlen = self.repo.ui.configint('histedit', 'linelen', default=80) | ||||
maxlen = max(maxlen, 22) # avoid truncating hash | ||||
return util.ellipsis(line, maxlen) | ||||
Mateusz Kwapich
|
r27206 | def tostate(self): | ||
"""Print an action in format used by histedit state files | ||||
(the first line is a verb, the remainder is the second) | ||||
""" | ||||
return "%s\n%s" % (self.verb, node.hex(self.node)) | ||||
Mateusz Kwapich
|
r27082 | def constraints(self): | ||
"""Return a set of constrains that this action should be verified for | ||||
""" | ||||
Augie Fackler
|
r27086 | return set([_constraints.noduplicates, _constraints.noother]) | ||
Mateusz Kwapich
|
r27082 | |||
def nodetoverify(self): | ||||
"""Returns a node associated with the action that will be used for | ||||
verification purposes. | ||||
If the action doesn't correspond to node it should return None | ||||
""" | ||||
return self.node | ||||
Durham Goode
|
r24765 | def run(self): | ||
"""Runs the action. The default behavior is simply apply the action's | ||||
rulectx onto the current parentctx.""" | ||||
self.applychange() | ||||
self.continuedirty() | ||||
return self.continueclean() | ||||
def applychange(self): | ||||
"""Applies the changes from this action's rulectx onto the current | ||||
parentctx, but does not commit them.""" | ||||
repo = self.repo | ||||
rulectx = repo[self.node] | ||||
timeless
|
r28004 | repo.ui.pushbuffer(error=True, labeled=True) | ||
timeless
|
r27405 | hg.update(repo, self.state.parentctxnode, quietempty=True) | ||
Durham Goode
|
r24765 | stats = applychanges(repo.ui, repo, rulectx, {}) | ||
if stats and stats[3] > 0: | ||||
timeless
|
r28004 | buf = repo.ui.popbuffer() | ||
repo.ui.write(*buf) | ||||
timeless
|
r27629 | raise error.InterventionRequired( | ||
_('Fix up the change (%s %s)') % | ||||
(self.verb, node.short(self.node)), | ||||
hint=_('hg histedit --continue to resume')) | ||||
timeless
|
r28004 | else: | ||
repo.ui.popbuffer() | ||||
Durham Goode
|
r24765 | |||
def continuedirty(self): | ||||
"""Continues the action when changes have been applied to the working | ||||
copy. The default behavior is to commit the dirty changes.""" | ||||
repo = self.repo | ||||
rulectx = repo[self.node] | ||||
editor = self.commiteditor() | ||||
commit = commitfuncfor(repo, rulectx) | ||||
commit(text=rulectx.description(), user=rulectx.user(), | ||||
date=rulectx.date(), extra=rulectx.extra(), editor=editor) | ||||
def commiteditor(self): | ||||
"""The editor to be used to edit the commit message.""" | ||||
return False | ||||
def continueclean(self): | ||||
"""Continues the action when the working copy is clean. The default | ||||
behavior is to accept the current commit as the new version of the | ||||
rulectx.""" | ||||
ctx = self.repo['.'] | ||||
if ctx.node() == self.state.parentctxnode: | ||||
timeless
|
r28340 | self.repo.ui.warn(_('%s: skipping changeset (no changes)\n') % | ||
Durham Goode
|
r24765 | node.short(self.node)) | ||
return ctx, [(self.node, tuple())] | ||||
if ctx.node() == self.node: | ||||
# Nothing changed | ||||
return ctx, [] | ||||
return ctx, [(self.node, (ctx.node(),))] | ||||
Pierre-Yves David
|
r18436 | def commitfuncfor(repo, src): | ||
"""Build a commit function for the replacement of <src> | ||||
Mads Kiilerich
|
r18644 | This function ensure we apply the same treatment to all changesets. | ||
Pierre-Yves David
|
r18436 | |||
Pierre-Yves David
|
r18437 | - Add a 'histedit_source' entry in extra. | ||
Pierre-Yves David
|
r18436 | |||
Augie Fackler
|
r25450 | Note that fold has its own separated logic because its handling is a bit | ||
Pierre-Yves David
|
r18436 | different and not easily factored out of the fold method. | ||
""" | ||||
Pierre-Yves David
|
r18440 | phasemin = src.phase() | ||
Pierre-Yves David
|
r18436 | def commitfunc(**kwargs): | ||
Pierre-Yves David
|
r18440 | phasebackup = repo.ui.backupconfig('phases', 'new-commit') | ||
try: | ||||
Mads Kiilerich
|
r20790 | repo.ui.setconfig('phases', 'new-commit', phasemin, | ||
'histedit') | ||||
Pierre-Yves David
|
r18440 | extra = kwargs.get('extra', {}).copy() | ||
extra['histedit_source'] = src.hex() | ||||
kwargs['extra'] = extra | ||||
return repo.commit(**kwargs) | ||||
finally: | ||||
repo.ui.restoreconfig(phasebackup) | ||||
Pierre-Yves David
|
r18436 | return commitfunc | ||
Pierre-Yves David
|
r17647 | def applychanges(ui, repo, ctx, opts): | ||
"""Merge changeset from ctx (only) in the current working directory""" | ||||
wcpar = repo.dirstate.parents()[0] | ||||
if ctx.p1().node() == wcpar: | ||||
timeless@mozdev.org
|
r26171 | # edits are "in place" we do not need to make any merge, | ||
timeless
|
r27603 | # just applies changes on parent for editing | ||
Pierre-Yves David
|
r17647 | cmdutil.revert(ui, repo, ctx, (wcpar, node.nullid), all=True) | ||
stats = None | ||||
else: | ||||
try: | ||||
# ui.forcemerge is an internal variable, do not document | ||||
Mads Kiilerich
|
r20790 | repo.ui.setconfig('ui', 'forcemerge', opts.get('tool', ''), | ||
'histedit') | ||||
Matt Mackall
|
r22904 | stats = mergemod.graft(repo, ctx, ctx.p1(), ['local', 'histedit']) | ||
Pierre-Yves David
|
r17647 | finally: | ||
Mads Kiilerich
|
r20790 | repo.ui.setconfig('ui', 'forcemerge', '', 'histedit') | ||
Pierre-Yves David
|
r17647 | return stats | ||
Leah Xue
|
r17407 | |||
Durham Goode
|
r24828 | def collapse(repo, first, last, commitopts, skipprompt=False): | ||
Pierre-Yves David
|
r17644 | """collapse the set of revisions from first to last as new one. | ||
Expected commit options are: | ||||
- message | ||||
- date | ||||
- username | ||||
Mads Kiilerich
|
r17738 | Commit message is edited in all cases. | ||
Pierre-Yves David
|
r17644 | |||
This function works in memory.""" | ||||
ctxs = list(repo.set('%d::%d', first, last)) | ||||
if not ctxs: | ||||
return None | ||||
Augie Fackler
|
r25452 | for c in ctxs: | ||
if not c.mutable(): | ||||
timeless
|
r27545 | raise error.ParseError( | ||
Augie Fackler
|
r25452 | _("cannot fold into public change %s") % node.short(c.node())) | ||
Pierre-Yves David
|
r17644 | base = first.parents()[0] | ||
# commit a new version of the old changeset, including the update | ||||
# collect all files which might be affected | ||||
files = set() | ||||
for ctx in ctxs: | ||||
files.update(ctx.files()) | ||||
# Recompute copies (avoid recording a -> b -> a) | ||||
Martin Geisler
|
r19392 | copied = copies.pathcopies(base, last) | ||
Pierre-Yves David
|
r17644 | |||
# prune files which were reverted by the updates | ||||
def samefile(f): | ||||
if f in last.manifest(): | ||||
a = last.filectx(f) | ||||
if f in base.manifest(): | ||||
b = base.filectx(f) | ||||
return (a.data() == b.data() | ||||
and a.flags() == b.flags()) | ||||
else: | ||||
return False | ||||
else: | ||||
return f not in base.manifest() | ||||
files = [f for f in files if not samefile(f)] | ||||
# commit version of these files as defined by head | ||||
headmf = last.manifest() | ||||
def filectxfn(repo, ctx, path): | ||||
if path in headmf: | ||||
fctx = last[path] | ||||
flags = fctx.flags() | ||||
Sean Farley
|
r21689 | mctx = context.memfilectx(repo, | ||
fctx.path(), fctx.data(), | ||||
Pierre-Yves David
|
r17644 | islink='l' in flags, | ||
isexec='x' in flags, | ||||
copied=copied.get(path)) | ||||
return mctx | ||||
Mads Kiilerich
|
r22296 | return None | ||
Pierre-Yves David
|
r17644 | |||
if commitopts.get('message'): | ||||
message = commitopts['message'] | ||||
else: | ||||
message = first.description() | ||||
user = commitopts.get('user') | ||||
date = commitopts.get('date') | ||||
Pierre-Yves David
|
r18437 | extra = commitopts.get('extra') | ||
Pierre-Yves David
|
r17644 | |||
parents = (first.p1().node(), first.p2().node()) | ||||
Mike Edgar
|
r22152 | editor = None | ||
Durham Goode
|
r24828 | if not skipprompt: | ||
Mike Edgar
|
r22152 | editor = cmdutil.getcommiteditor(edit=True, editform='histedit.fold') | ||
Pierre-Yves David
|
r17644 | new = context.memctx(repo, | ||
parents=parents, | ||||
text=message, | ||||
files=files, | ||||
filectxfn=filectxfn, | ||||
user=user, | ||||
date=date, | ||||
FUJIWARA Katsunori
|
r21239 | extra=extra, | ||
FUJIWARA Katsunori
|
r22002 | editor=editor) | ||
Pierre-Yves David
|
r17644 | return repo.commitctx(new) | ||
liscju
|
r26981 | def _isdirtywc(repo): | ||
return repo[None].dirty(missing=True) | ||||
Mateusz Kwapich
|
r27084 | def abortdirty(): | ||
raise error.Abort(_('working copy has pending changes'), | ||||
hint=_('amend, commit, or revert them and run histedit ' | ||||
'--continue, or abort with histedit --abort')) | ||||
timeless
|
r27675 | def action(verbs, message, priority=False, internal=False): | ||
def wrap(cls): | ||||
assert not priority or not internal | ||||
verb = verbs[0] | ||||
if priority: | ||||
primaryactions.add(verb) | ||||
elif internal: | ||||
internalactions.add(verb) | ||||
elif len(verbs) > 1: | ||||
secondaryactions.add(verb) | ||||
else: | ||||
tertiaryactions.add(verb) | ||||
Mateusz Kwapich
|
r27201 | |||
timeless
|
r27675 | cls.verb = verb | ||
cls.verbs = verbs | ||||
cls.message = message | ||||
Mateusz Kwapich
|
r27201 | for verb in verbs: | ||
actiontable[verb] = cls | ||||
return cls | ||||
return wrap | ||||
timeless
|
r27675 | @action(['pick', 'p'], | ||
_('use commit'), | ||||
priority=True) | ||||
Durham Goode
|
r24767 | class pick(histeditaction): | ||
def run(self): | ||||
rulectx = self.repo[self.node] | ||||
if rulectx.parents()[0].node() == self.state.parentctxnode: | ||||
self.repo.ui.debug('node %s unchanged\n' % node.short(self.node)) | ||||
return rulectx, [] | ||||
Augie Fackler
|
r17064 | |||
Durham Goode
|
r24767 | return super(pick, self).run() | ||
Augie Fackler
|
r17064 | |||
timeless
|
r27675 | @action(['edit', 'e'], | ||
_('use commit, but stop for amending'), | ||||
priority=True) | ||||
Durham Goode
|
r24770 | class edit(histeditaction): | ||
def run(self): | ||||
repo = self.repo | ||||
rulectx = repo[self.node] | ||||
timeless
|
r27407 | hg.update(repo, self.state.parentctxnode, quietempty=True) | ||
Durham Goode
|
r24770 | applychanges(repo.ui, repo, rulectx, {}) | ||
raise error.InterventionRequired( | ||||
timeless
|
r27629 | _('Editing (%s), you may commit or record as needed now.') | ||
% node.short(self.node), | ||||
hint=_('hg histedit --continue to resume')) | ||||
Durham Goode
|
r24770 | |||
def commiteditor(self): | ||||
return cmdutil.getcommiteditor(edit=True, editform='histedit.edit') | ||||
Augie Fackler
|
r17064 | |||
timeless
|
r27675 | @action(['fold', 'f'], | ||
_('use commit, but combine it with the one above')) | ||||
Durham Goode
|
r24771 | class fold(histeditaction): | ||
timeless
|
r27542 | def verify(self, prev): | ||
""" Verifies semantic correctness of the fold rule""" | ||||
super(fold, self).verify(prev) | ||||
repo = self.repo | ||||
if not prev: | ||||
c = repo[self.node].parents()[0] | ||||
elif not prev.verb in ('pick', 'base'): | ||||
return | ||||
else: | ||||
c = repo[prev.node] | ||||
if not c.mutable(): | ||||
timeless
|
r27545 | raise error.ParseError( | ||
timeless
|
r27542 | _("cannot fold into public change %s") % node.short(c.node())) | ||
Durham Goode
|
r24771 | def continuedirty(self): | ||
repo = self.repo | ||||
rulectx = repo[self.node] | ||||
commit = commitfuncfor(repo, rulectx) | ||||
commit(text='fold-temp-revision %s' % node.short(self.node), | ||||
user=rulectx.user(), date=rulectx.date(), | ||||
extra=rulectx.extra()) | ||||
def continueclean(self): | ||||
repo = self.repo | ||||
ctx = repo['.'] | ||||
rulectx = repo[self.node] | ||||
parentctxnode = self.state.parentctxnode | ||||
if ctx.node() == parentctxnode: | ||||
repo.ui.warn(_('%s: empty changeset\n') % | ||||
node.short(self.node)) | ||||
return ctx, [(self.node, (parentctxnode,))] | ||||
Mike Edgar
|
r22152 | |||
Durham Goode
|
r24771 | parentctx = repo[parentctxnode] | ||
newcommits = set(c.node() for c in repo.set('(%d::. - %d)', parentctx, | ||||
parentctx)) | ||||
if not newcommits: | ||||
repo.ui.warn(_('%s: cannot fold - working copy is not a ' | ||||
'descendant of previous commit %s\n') % | ||||
(node.short(self.node), node.short(parentctxnode))) | ||||
return ctx, [(self.node, (ctx.node(),))] | ||||
middlecommits = newcommits.copy() | ||||
middlecommits.discard(ctx.node()) | ||||
Durham Goode
|
r24773 | return self.finishfold(repo.ui, repo, parentctx, rulectx, ctx.node(), | ||
middlecommits) | ||||
Durham Goode
|
r24771 | |||
Durham Goode
|
r24773 | def skipprompt(self): | ||
Augie Fackler
|
r26246 | """Returns true if the rule should skip the message editor. | ||
For example, 'fold' wants to show an editor, but 'rollup' | ||||
doesn't want to. | ||||
""" | ||||
Durham Goode
|
r24773 | return False | ||
Durham Goode
|
r24772 | |||
Augie Fackler
|
r26246 | def mergedescs(self): | ||
"""Returns true if the rule should merge messages of multiple changes. | ||||
This exists mainly so that 'rollup' rules can be a subclass of | ||||
'fold'. | ||||
""" | ||||
return True | ||||
Durham Goode
|
r24773 | def finishfold(self, ui, repo, ctx, oldctx, newnode, internalchanges): | ||
Durham Goode
|
r24772 | parent = ctx.parents()[0].node() | ||
timeless
|
r28004 | repo.ui.pushbuffer() | ||
Durham Goode
|
r24772 | hg.update(repo, parent) | ||
timeless
|
r28004 | repo.ui.popbuffer() | ||
Durham Goode
|
r24772 | ### prepare new commit data | ||
Durham Goode
|
r24773 | commitopts = {} | ||
Durham Goode
|
r24772 | commitopts['user'] = ctx.user() | ||
# commit message | ||||
Augie Fackler
|
r26246 | if not self.mergedescs(): | ||
Durham Goode
|
r24772 | newmessage = ctx.description() | ||
else: | ||||
newmessage = '\n***\n'.join( | ||||
[ctx.description()] + | ||||
[repo[r].description() for r in internalchanges] + | ||||
[oldctx.description()]) + '\n' | ||||
commitopts['message'] = newmessage | ||||
# date | ||||
commitopts['date'] = max(ctx.date(), oldctx.date()) | ||||
extra = ctx.extra().copy() | ||||
# histedit_source | ||||
# note: ctx is likely a temporary commit but that the best we can do | ||||
# here. This is sufficient to solve issue3681 anyway. | ||||
extra['histedit_source'] = '%s,%s' % (ctx.hex(), oldctx.hex()) | ||||
commitopts['extra'] = extra | ||||
phasebackup = repo.ui.backupconfig('phases', 'new-commit') | ||||
try: | ||||
phasemin = max(ctx.phase(), oldctx.phase()) | ||||
repo.ui.setconfig('phases', 'new-commit', phasemin, 'histedit') | ||||
Durham Goode
|
r24828 | n = collapse(repo, ctx, repo[newnode], commitopts, | ||
skipprompt=self.skipprompt()) | ||||
Durham Goode
|
r24772 | finally: | ||
repo.ui.restoreconfig(phasebackup) | ||||
if n is None: | ||||
return ctx, [] | ||||
timeless
|
r28004 | repo.ui.pushbuffer() | ||
Durham Goode
|
r24772 | hg.update(repo, n) | ||
timeless
|
r28004 | repo.ui.popbuffer() | ||
Durham Goode
|
r24772 | replacements = [(oldctx.node(), (newnode,)), | ||
(ctx.node(), (n,)), | ||||
(newnode, (n,)), | ||||
] | ||||
for ich in internalchanges: | ||||
replacements.append((ich, (n,))) | ||||
return repo[n], replacements | ||||
Durham Goode
|
r24771 | |||
Mateusz Kwapich
|
r27085 | class base(histeditaction): | ||
def constraints(self): | ||||
Augie Fackler
|
r27086 | return set([_constraints.forceother]) | ||
Mateusz Kwapich
|
r27085 | |||
def run(self): | ||||
if self.repo['.'].node() != self.node: | ||||
Augie Fackler
|
r27344 | mergemod.update(self.repo, self.node, False, True) | ||
# branchmerge, force) | ||||
Mateusz Kwapich
|
r27085 | return self.continueclean() | ||
def continuedirty(self): | ||||
abortdirty() | ||||
def continueclean(self): | ||||
basectx = self.repo['.'] | ||||
return basectx, [] | ||||
timeless
|
r27675 | @action(['_multifold'], | ||
_( | ||||
Augie Fackler
|
r26246 | """fold subclass used for when multiple folds happen in a row | ||
We only want to fire the editor for the folded message once when | ||||
(say) four changes are folded down into a single change. This is | ||||
similar to rollup, but we should preserve both messages so that | ||||
when the last fold operation runs we can show the user all the | ||||
commit messages in their editor. | ||||
timeless
|
r27675 | """), | ||
internal=True) | ||||
class _multifold(fold): | ||||
Augie Fackler
|
r26246 | def skipprompt(self): | ||
return True | ||||
timeless
|
r27675 | @action(["roll", "r"], | ||
_("like fold, but discard this commit's description")) | ||||
Durham Goode
|
r24771 | class rollup(fold): | ||
Augie Fackler
|
r26246 | def mergedescs(self): | ||
return False | ||||
Durham Goode
|
r24773 | def skipprompt(self): | ||
return True | ||||
Augie Fackler
|
r17064 | |||
timeless
|
r27675 | @action(["drop", "d"], | ||
_('remove commit from history')) | ||||
Durham Goode
|
r24768 | class drop(histeditaction): | ||
def run(self): | ||||
parentctx = self.repo[self.state.parentctxnode] | ||||
return parentctx, [(self.node, tuple())] | ||||
Augie Fackler
|
r17064 | |||
timeless
|
r27675 | @action(["mess", "m"], | ||
_('edit commit message without changing commit content'), | ||||
priority=True) | ||||
Durham Goode
|
r24769 | class message(histeditaction): | ||
def commiteditor(self): | ||||
return cmdutil.getcommiteditor(edit=True, editform='histedit.mess') | ||||
Augie Fackler
|
r17064 | |||
Pierre-Yves David
|
r26335 | def findoutgoing(ui, repo, remote=None, force=False, opts=None): | ||
Pierre-Yves David
|
r19021 | """utility function to find the first outgoing changeset | ||
timeless@mozdev.org
|
r26171 | Used by initialization code""" | ||
Pierre-Yves David
|
r26335 | if opts is None: | ||
opts = {} | ||||
Pierre-Yves David
|
r19021 | dest = ui.expandpath(remote or 'default-push', remote or 'default') | ||
dest, revs = hg.parseurl(dest, None)[:2] | ||||
ui.status(_('comparing with %s\n') % util.hidepassword(dest)) | ||||
revs, checkout = hg.addbranchrevs(repo, repo, revs, None) | ||||
other = hg.peer(repo, opts, dest) | ||||
if revs: | ||||
revs = [repo.lookup(rev) for rev in revs] | ||||
outgoing = discovery.findcommonoutgoing(repo, other, revs, force=force) | ||||
if not outgoing.missing: | ||||
Pierre-Yves David
|
r26587 | raise error.Abort(_('no outgoing ancestors')) | ||
FUJIWARA Katsunori
|
r19841 | roots = list(repo.revs("roots(%ln)", outgoing.missing)) | ||
if 1 < len(roots): | ||||
msg = _('there are ambiguous outgoing revisions') | ||||
hint = _('see "hg help histedit" for more detail') | ||||
Pierre-Yves David
|
r26587 | raise error.Abort(msg, hint=hint) | ||
FUJIWARA Katsunori
|
r19841 | return repo.lookup(roots[0]) | ||
Pierre-Yves David
|
r19021 | |||
Adrian Buehlmann
|
r17147 | |||
@command('histedit', | ||||
[('', 'commands', '', | ||||
Anton Shestakov
|
r24232 | _('read history edits from the specified file'), _('FILE')), | ||
Adrian Buehlmann
|
r17147 | ('c', 'continue', False, _('continue an edit already in progress')), | ||
Mateusz Kwapich
|
r24142 | ('', 'edit-plan', False, _('edit remaining actions list')), | ||
Adrian Buehlmann
|
r17147 | ('k', 'keep', False, | ||
_("don't strip old nodes after edit is complete")), | ||||
('', 'abort', False, _('abort an edit in progress')), | ||||
('o', 'outgoing', False, _('changesets not found in destination')), | ||||
('f', 'force', False, | ||||
_('force outgoing even for unrelated repositories')), | ||||
Anton Shestakov
|
r24232 | ('r', 'rev', [], _('first revision to be edited'), _('REV'))], | ||
timeless
|
r27714 | _("[OPTIONS] ([ANCESTOR] | --outgoing [URL])")) | ||
Pierre-Yves David
|
r19020 | def histedit(ui, repo, *freeargs, **opts): | ||
Augie Fackler
|
r17131 | """interactively edit changeset history | ||
FUJIWARA Katsunori
|
r19621 | |||
timeless
|
r27713 | This command lets you edit a linear series of changesets (up to | ||
and including the working directory, which should be clean). | ||||
FUJIWARA Katsunori
|
r27956 | You can: | ||
timeless
|
r27713 | |||
- `pick` to [re]order a changeset | ||||
- `drop` to omit changeset | ||||
- `mess` to reword the changeset commit message | ||||
- `fold` to combine it with the preceding changeset | ||||
- `roll` like fold, but discarding this commit's description | ||||
- `edit` to edit this changeset | ||||
FUJIWARA Katsunori
|
r19622 | |||
Wagner Bruna
|
r27972 | There are a number of ways to select the root changeset: | ||
timeless
|
r27714 | |||
- Specify ANCESTOR directly | ||||
Gregory Szorc
|
r27262 | |||
timeless
|
r27714 | - Use --outgoing -- it will be the first linear changeset not | ||
FUJIWARA Katsunori
|
r28077 | included in destination. (See :hg:`help config.paths.default-push`) | ||
timeless
|
r27714 | |||
- Otherwise, the value from the "histedit.defaultrev" config option | ||||
is used as a revset to select the base revision when ANCESTOR is not | ||||
specified. The first revision returned by the revset is used. By | ||||
default, this selects the editable history that is unique to the | ||||
ancestry of the working directory. | ||||
FUJIWARA Katsunori
|
r19842 | |||
timeless
|
r27630 | .. container:: verbose | ||
FUJIWARA Katsunori
|
r19842 | |||
timeless
|
r27630 | If you use --outgoing, this command will abort if there are ambiguous | ||
outgoing revisions. For example, if there are multiple branches | ||||
containing outgoing revisions. | ||||
Use "min(outgoing() and ::.)" or similar revset specification | ||||
instead of --outgoing to specify edit target revision exactly in | ||||
such ambiguous situation. See :hg:`help revsets` for detail about | ||||
selecting revisions. | ||||
FUJIWARA Katsunori
|
r19972 | |||
Mathias De Maré
|
r27145 | .. container:: verbose | ||
Examples: | ||||
- A number of changes have been made. | ||||
Revision 3 is no longer needed. | ||||
Start history editing from revision 3:: | ||||
hg histedit -r 3 | ||||
An editor opens, containing the list of revisions, | ||||
with specific actions specified:: | ||||
pick 5339bf82f0ca 3 Zworgle the foobar | ||||
pick 8ef592ce7cc4 4 Bedazzle the zerlog | ||||
pick 0a9639fcda9d 5 Morgify the cromulancy | ||||
Additional information about the possible actions | ||||
to take appears below the list of revisions. | ||||
To remove revision 3 from the history, | ||||
its action (at the beginning of the relevant line) | ||||
is changed to 'drop':: | ||||
drop 5339bf82f0ca 3 Zworgle the foobar | ||||
pick 8ef592ce7cc4 4 Bedazzle the zerlog | ||||
pick 0a9639fcda9d 5 Morgify the cromulancy | ||||
- A number of changes have been made. | ||||
Revision 2 and 4 need to be swapped. | ||||
Start history editing from revision 2:: | ||||
hg histedit -r 2 | ||||
An editor opens, containing the list of revisions, | ||||
with specific actions specified:: | ||||
pick 252a1af424ad 2 Blorb a morgwazzle | ||||
pick 5339bf82f0ca 3 Zworgle the foobar | ||||
pick 8ef592ce7cc4 4 Bedazzle the zerlog | ||||
To swap revision 2 and 4, its lines are swapped | ||||
in the editor:: | ||||
pick 8ef592ce7cc4 4 Bedazzle the zerlog | ||||
pick 5339bf82f0ca 3 Zworgle the foobar | ||||
pick 252a1af424ad 2 Blorb a morgwazzle | ||||
FUJIWARA Katsunori
|
r19972 | Returns 0 on success, 1 if user intervention is required (not only | ||
for intentional "edit" command, but also for resolving unexpected | ||||
conflicts). | ||||
Augie Fackler
|
r17064 | """ | ||
David Soria Parra
|
r22984 | state = histeditstate(repo) | ||
Siddharth Agarwal
|
r20071 | try: | ||
David Soria Parra
|
r22984 | state.wlock = repo.wlock() | ||
state.lock = repo.lock() | ||||
_histedit(ui, repo, state, *freeargs, **opts) | ||||
Siddharth Agarwal
|
r20071 | finally: | ||
David Soria Parra
|
r22984 | release(state.lock, state.wlock) | ||
Siddharth Agarwal
|
r20071 | |||
Kostia Balytskyi
|
r28144 | goalcontinue = 'continue' | ||
goalabort = 'abort' | ||||
goaleditplan = 'edit-plan' | ||||
goalnew = 'new' | ||||
Kostia Balytskyi
|
r28134 | def _getgoal(opts): | ||
if opts.get('continue'): | ||||
Kostia Balytskyi
|
r28144 | return goalcontinue | ||
Kostia Balytskyi
|
r28134 | if opts.get('abort'): | ||
Kostia Balytskyi
|
r28144 | return goalabort | ||
Kostia Balytskyi
|
r28134 | if opts.get('edit_plan'): | ||
Kostia Balytskyi
|
r28144 | return goaleditplan | ||
return goalnew | ||||
Kostia Balytskyi
|
r28134 | |||
Jun Wu
|
r28550 | def _readfile(path): | ||
if path == '-': | ||||
return sys.stdin.read() | ||||
else: | ||||
with open(path, 'rb') as f: | ||||
return f.read() | ||||
Kostia Balytskyi
|
r28134 | def _validateargs(ui, repo, state, freeargs, opts, goal, rules, revs): | ||
timeless
|
r27169 | # TODO only abort if we try to histedit mq patches, not just | ||
Augie Fackler
|
r17064 | # blanket if mq patches are applied somewhere | ||
mq = getattr(repo, 'mq', None) | ||||
if mq and mq.applied: | ||||
Pierre-Yves David
|
r26587 | raise error.Abort(_('source has mq patches applied')) | ||
Augie Fackler
|
r17064 | |||
Pierre-Yves David
|
r19020 | # basic argument incompatibility processing | ||
outg = opts.get('outgoing') | ||||
Mateusz Kwapich
|
r24142 | editplan = opts.get('edit_plan') | ||
Pierre-Yves David
|
r19020 | abort = opts.get('abort') | ||
force = opts.get('force') | ||||
if force and not outg: | ||||
Pierre-Yves David
|
r26587 | raise error.Abort(_('--force only allowed with --outgoing')) | ||
Kostia Balytskyi
|
r28134 | if goal == 'continue': | ||
Augie Fackler
|
r25149 | if any((outg, abort, revs, freeargs, rules, editplan)): | ||
Pierre-Yves David
|
r26587 | raise error.Abort(_('no arguments allowed with --continue')) | ||
Kostia Balytskyi
|
r28134 | elif goal == 'abort': | ||
Augie Fackler
|
r25149 | if any((outg, revs, freeargs, rules, editplan)): | ||
Pierre-Yves David
|
r26587 | raise error.Abort(_('no arguments allowed with --abort')) | ||
Kostia Balytskyi
|
r28134 | elif goal == 'edit-plan': | ||
Augie Fackler
|
r25149 | if any((outg, revs, freeargs)): | ||
Pierre-Yves David
|
r26587 | raise error.Abort(_('only --commands argument allowed with ' | ||
Mateusz Kwapich
|
r24142 | '--edit-plan')) | ||
Pierre-Yves David
|
r19020 | else: | ||
if os.path.exists(os.path.join(repo.path, 'histedit-state')): | ||||
Pierre-Yves David
|
r26587 | raise error.Abort(_('history edit already in progress, try ' | ||
Pierre-Yves David
|
r19020 | '--continue or --abort')) | ||
if outg: | ||||
if revs: | ||||
Pierre-Yves David
|
r26587 | raise error.Abort(_('no revisions allowed with --outgoing')) | ||
Pierre-Yves David
|
r19020 | if len(freeargs) > 1: | ||
Pierre-Yves David
|
r26587 | raise error.Abort( | ||
Pierre-Yves David
|
r19020 | _('only one repo argument allowed with --outgoing')) | ||
else: | ||||
Pierre-Yves David
|
r19021 | revs.extend(freeargs) | ||
Durham Goode
|
r24009 | if len(revs) == 0: | ||
Gregory Szorc
|
r27262 | defaultrev = destutil.desthistedit(ui, repo) | ||
if defaultrev is not None: | ||||
revs.append(defaultrev) | ||||
Pierre-Yves David
|
r19021 | if len(revs) != 1: | ||
Pierre-Yves David
|
r26587 | raise error.Abort( | ||
FUJIWARA Katsunori
|
r19621 | _('histedit requires exactly one ancestor revision')) | ||
Pierre-Yves David
|
r19020 | |||
Kostia Balytskyi
|
r28134 | def _histedit(ui, repo, state, *freeargs, **opts): | ||
goal = _getgoal(opts) | ||||
revs = opts.get('rev', []) | ||||
rules = opts.get('commands', '') | ||||
state.keep = opts.get('keep', False) | ||||
Augie Fackler
|
r17064 | |||
Kostia Balytskyi
|
r28134 | _validateargs(ui, repo, state, freeargs, opts, goal, rules, revs) | ||
David Soria Parra
|
r22977 | |||
# rebuild state | ||||
Kostia Balytskyi
|
r28144 | if goal == goalcontinue: | ||
David Soria Parra
|
r22983 | state.read() | ||
David Soria Parra
|
r22980 | state = bootstrapcontinue(ui, state, opts) | ||
Kostia Balytskyi
|
r28144 | elif goal == goaleditplan: | ||
Kostia Balytskyi
|
r28154 | _edithisteditplan(ui, repo, state, rules) | ||
Mateusz Kwapich
|
r24142 | return | ||
Kostia Balytskyi
|
r28144 | elif goal == goalabort: | ||
Kostia Balytskyi
|
r28154 | _aborthistedit(ui, repo, state) | ||
Augie Fackler
|
r17064 | return | ||
else: | ||||
Kostia Balytskyi
|
r28144 | # goal == goalnew | ||
Kostia Balytskyi
|
r28154 | _newhistedit(ui, repo, state, revs, freeargs, opts) | ||
Durham Goode
|
r24757 | |||
Kostia Balytskyi
|
r28154 | _continuehistedit(ui, repo, state) | ||
_finishhistedit(ui, repo, state) | ||||
Kostia Balytskyi
|
r28133 | |||
Kostia Balytskyi
|
r28154 | def _continuehistedit(ui, repo, state): | ||
"""This function runs after either: | ||||
Kostia Balytskyi
|
r28133 | - bootstrapcontinue (if the goal is 'continue') | ||
Kostia Balytskyi
|
r28154 | - _newhistedit (if the goal is 'new') | ||
Kostia Balytskyi
|
r28133 | """ | ||
Augie Fackler
|
r26246 | # preprocess rules so that we can hide inner folds from the user | ||
# and only show one editor | ||||
Mateusz Kwapich
|
r27207 | actions = state.actions[:] | ||
for idx, (action, nextact) in enumerate( | ||||
zip(actions, actions[1:] + [None])): | ||||
if action.verb == 'fold' and nextact and nextact.verb == 'fold': | ||||
state.actions[idx].__class__ = _multifold | ||||
Augie Fackler
|
r26246 | |||
timeless
|
r27451 | total = len(state.actions) | ||
pos = 0 | ||||
Mateusz Kwapich
|
r27207 | while state.actions: | ||
David Soria Parra
|
r22977 | state.write() | ||
Mateusz Kwapich
|
r27207 | actobj = state.actions.pop(0) | ||
timeless
|
r27451 | pos += 1 | ||
ui.progress(_("editing"), pos, actobj.torule(), | ||||
_('changes'), total) | ||||
Mateusz Kwapich
|
r27207 | ui.debug('histedit: processing %s %s\n' % (actobj.verb,\ | ||
actobj.torule())) | ||||
Durham Goode
|
r24774 | parentctx, replacement_ = actobj.run() | ||
Mateusz Kwapich
|
r24112 | state.parentctxnode = parentctx.node() | ||
David Soria Parra
|
r22977 | state.replacements.extend(replacement_) | ||
Mateusz Kwapich
|
r24111 | state.write() | ||
timeless
|
r27451 | ui.progress(_("editing"), None) | ||
Augie Fackler
|
r17064 | |||
Kostia Balytskyi
|
r28154 | def _finishhistedit(ui, repo, state): | ||
Kostia Balytskyi
|
r28153 | """This action runs when histedit is finishing its session""" | ||
timeless
|
r28004 | repo.ui.pushbuffer() | ||
timeless
|
r27406 | hg.update(repo, state.parentctxnode, quietempty=True) | ||
timeless
|
r28004 | repo.ui.popbuffer() | ||
Augie Fackler
|
r17064 | |||
Augie Fackler
|
r22985 | mapping, tmpnodes, created, ntm = processreplacement(state) | ||
Pierre-Yves David
|
r17758 | if mapping: | ||
for prec, succs in mapping.iteritems(): | ||||
if not succs: | ||||
ui.debug('histedit: %s is dropped\n' % node.short(prec)) | ||||
else: | ||||
ui.debug('histedit: %s is replaced by %s\n' % ( | ||||
node.short(prec), node.short(succs[0]))) | ||||
if len(succs) > 1: | ||||
m = 'histedit: %s' | ||||
for n in succs[1:]: | ||||
ui.debug(m % node.short(n)) | ||||
Kostia Balytskyi
|
r28133 | supportsmarkers = obsolete.isenabled(repo, obsolete.createmarkersopt) | ||
Durham Goode
|
r26763 | if supportsmarkers: | ||
# Only create markers if the temp nodes weren't already removed. | ||||
obsolete.createmarkers(repo, ((repo[t],()) for t in sorted(tmpnodes) | ||||
if t in repo)) | ||||
else: | ||||
cleanupnode(ui, repo, 'temp', tmpnodes) | ||||
Durham Goode
|
r25330 | if not state.keep: | ||
Pierre-Yves David
|
r17758 | if mapping: | ||
David Soria Parra
|
r22977 | movebookmarks(ui, repo, mapping, state.topmost, ntm) | ||
Pierre-Yves David
|
r17663 | # TODO update mq state | ||
Laurent Charignon
|
r25808 | if supportsmarkers: | ||
Pierre-Yves David
|
r17759 | markers = [] | ||
Pierre-Yves David
|
r17771 | # sort by revision number because it sound "right" | ||
for prec in sorted(mapping, key=repo.changelog.rev): | ||||
succs = mapping[prec] | ||||
Pierre-Yves David
|
r17759 | markers.append((repo[prec], | ||
tuple(repo[s] for s in succs))) | ||||
if markers: | ||||
obsolete.createmarkers(repo, markers) | ||||
else: | ||||
cleanupnode(ui, repo, 'replaced', mapping) | ||||
Pierre-Yves David
|
r25894 | |||
David Soria Parra
|
r22978 | state.clear() | ||
Augie Fackler
|
r17064 | if os.path.exists(repo.sjoin('undo')): | ||
os.unlink(repo.sjoin('undo')) | ||||
timeless
|
r27546 | if repo.vfs.exists('histedit-last-edit.txt'): | ||
repo.vfs.unlink('histedit-last-edit.txt') | ||||
Augie Fackler
|
r17064 | |||
Kostia Balytskyi
|
r28154 | def _aborthistedit(ui, repo, state): | ||
Kostia Balytskyi
|
r28130 | try: | ||
state.read() | ||||
Kostia Balytskyi
|
r28179 | __, leafs, tmpnodes, __ = processreplacement(state) | ||
Kostia Balytskyi
|
r28130 | ui.debug('restore wc to old parent %s\n' | ||
% node.short(state.topmost)) | ||||
# Recover our old commits if necessary | ||||
if not state.topmost in repo and state.backupfile: | ||||
backupfile = repo.join(state.backupfile) | ||||
f = hg.openpath(ui, backupfile) | ||||
gen = exchange.readbundle(ui, f, backupfile) | ||||
with repo.transaction('histedit.abort') as tr: | ||||
if not isinstance(gen, bundle2.unbundle20): | ||||
gen.apply(repo, 'histedit', 'bundle:' + backupfile) | ||||
if isinstance(gen, bundle2.unbundle20): | ||||
bundle2.applybundle(repo, gen, tr, | ||||
source='histedit', | ||||
url='bundle:' + backupfile) | ||||
os.remove(backupfile) | ||||
# check whether we should update away | ||||
if repo.unfiltered().revs('parents() and (%n or %ln::)', | ||||
state.parentctxnode, leafs | tmpnodes): | ||||
hg.clean(repo, state.topmost, show_stats=True, quietempty=True) | ||||
cleanupnode(ui, repo, 'created', tmpnodes) | ||||
cleanupnode(ui, repo, 'temp', leafs) | ||||
except Exception: | ||||
if state.inprogress(): | ||||
ui.warn(_('warning: encountered an exception during histedit ' | ||||
'--abort; the repository may not have been completely ' | ||||
'cleaned up\n')) | ||||
raise | ||||
finally: | ||||
state.clear() | ||||
Kostia Balytskyi
|
r28154 | def _edithisteditplan(ui, repo, state, rules): | ||
Kostia Balytskyi
|
r28131 | state.read() | ||
if not rules: | ||||
Mateusz Kwapich
|
r28592 | comment = geteditcomment(ui, | ||
node.short(state.parentctxnode), | ||||
Kostia Balytskyi
|
r28131 | node.short(state.topmost)) | ||
rules = ruleeditor(repo, ui, state.actions, comment) | ||||
else: | ||||
Jun Wu
|
r28550 | rules = _readfile(rules) | ||
Kostia Balytskyi
|
r28131 | actions = parserules(rules, state) | ||
ctxs = [repo[act.nodetoverify()] \ | ||||
for act in state.actions if act.nodetoverify()] | ||||
warnverifyactions(ui, repo, actions, state, ctxs) | ||||
state.actions = actions | ||||
state.write() | ||||
Kostia Balytskyi
|
r28154 | def _newhistedit(ui, repo, state, revs, freeargs, opts): | ||
Kostia Balytskyi
|
r28132 | outg = opts.get('outgoing') | ||
rules = opts.get('commands', '') | ||||
force = opts.get('force') | ||||
cmdutil.checkunfinished(repo) | ||||
cmdutil.bailifchanged(repo) | ||||
topmost, empty = repo.dirstate.parents() | ||||
if outg: | ||||
if freeargs: | ||||
remote = freeargs[0] | ||||
else: | ||||
remote = None | ||||
root = findoutgoing(ui, repo, remote, force, opts) | ||||
else: | ||||
rr = list(repo.set('roots(%ld)', scmutil.revrange(repo, revs))) | ||||
if len(rr) != 1: | ||||
raise error.Abort(_('The specified revisions must have ' | ||||
'exactly one common root')) | ||||
root = rr[0].node() | ||||
revs = between(repo, root, topmost, state.keep) | ||||
if not revs: | ||||
raise error.Abort(_('%s is not an ancestor of working directory') % | ||||
node.short(root)) | ||||
ctxs = [repo[r] for r in revs] | ||||
if not rules: | ||||
Mateusz Kwapich
|
r28592 | comment = geteditcomment(ui, node.short(root), node.short(topmost)) | ||
Kostia Balytskyi
|
r28132 | actions = [pick(state, r) for r in revs] | ||
rules = ruleeditor(repo, ui, actions, comment) | ||||
else: | ||||
Jun Wu
|
r28550 | rules = _readfile(rules) | ||
Kostia Balytskyi
|
r28132 | actions = parserules(rules, state) | ||
warnverifyactions(ui, repo, actions, state, ctxs) | ||||
parentctxnode = repo[root].parents()[0].node() | ||||
state.parentctxnode = parentctxnode | ||||
state.actions = actions | ||||
state.topmost = topmost | ||||
state.replacements = [] | ||||
# Create a backup so we can always abort completely. | ||||
backupfile = None | ||||
if not obsolete.isenabled(repo, obsolete.createmarkersopt): | ||||
backupfile = repair._bundle(repo, [parentctxnode], [topmost], root, | ||||
'histedit') | ||||
state.backupfile = backupfile | ||||
Durham Goode
|
r24774 | def bootstrapcontinue(ui, state, opts): | ||
repo = state.repo | ||||
Mateusz Kwapich
|
r27207 | if state.actions: | ||
actobj = state.actions.pop(0) | ||||
Durham Goode
|
r24959 | |||
liscju
|
r26981 | if _isdirtywc(repo): | ||
Durham Goode
|
r24959 | actobj.continuedirty() | ||
liscju
|
r26981 | if _isdirtywc(repo): | ||
Mateusz Kwapich
|
r27084 | abortdirty() | ||
Durham Goode
|
r24766 | |||
Durham Goode
|
r24959 | parentctx, replacements = actobj.continueclean() | ||
Pierre-Yves David
|
r17666 | |||
Durham Goode
|
r24959 | state.parentctxnode = parentctx.node() | ||
state.replacements.extend(replacements) | ||||
David Soria Parra
|
r22980 | |||
return state | ||||
Pierre-Yves David
|
r17666 | |||
Pierre-Yves David
|
r17642 | def between(repo, old, new, keep): | ||
"""select and validate the set of revision to edit | ||||
When keep is false, the specified set can't have children.""" | ||||
Pierre-Yves David
|
r17765 | ctxs = list(repo.set('%n::%n', old, new)) | ||
Pierre-Yves David
|
r17766 | if ctxs and not keep: | ||
Durham Goode
|
r22952 | if (not obsolete.isenabled(repo, obsolete.allowunstableopt) and | ||
Pierre-Yves David
|
r18270 | repo.revs('(%ld::) - (%ld)', ctxs, ctxs)): | ||
liscju
|
r28294 | raise error.Abort(_('can only histedit a changeset together ' | ||
'with all its descendants')) | ||||
Augie Fackler
|
r19473 | if repo.revs('(%ld) and merge()', ctxs): | ||
Pierre-Yves David
|
r26587 | raise error.Abort(_('cannot edit history that contains merges')) | ||
Pierre-Yves David
|
r17767 | root = ctxs[0] # list is already sorted by repo.set | ||
Augie Fackler
|
r22416 | if not root.mutable(): | ||
Pierre-Yves David
|
r26587 | raise error.Abort(_('cannot edit public changeset: %s') % root, | ||
Jordi Gutiérrez Hermoso
|
r25412 | hint=_('see "hg help phases" for details')) | ||
Pierre-Yves David
|
r17765 | return [c.node() for c in ctxs] | ||
Pierre-Yves David
|
r17642 | |||
Mateusz Kwapich
|
r27204 | def ruleeditor(repo, ui, actions, editcomment=""): | ||
Mateusz Kwapich
|
r24140 | """open an editor to edit rules | ||
rules are in the format [ [act, ctx], ...] like in state.rules | ||||
""" | ||||
Mateusz Kwapich
|
r27204 | rules = '\n'.join([act.torule() for act in actions]) | ||
Mateusz Kwapich
|
r24140 | rules += '\n\n' | ||
rules += editcomment | ||||
Mykola Nikishov
|
r27154 | rules = ui.edit(rules, ui.username(), {'prefix': 'histedit'}) | ||
Mateusz Kwapich
|
r24140 | |||
# Save edit rules in .hg/histedit-last-edit.txt in case | ||||
# the user needs to ask for help after something | ||||
# surprising happens. | ||||
f = open(repo.join('histedit-last-edit.txt'), 'w') | ||||
f.write(rules) | ||||
f.close() | ||||
return rules | ||||
Mateusz Kwapich
|
r27208 | def parserules(rules, state): | ||
"""Read the histedit rules string and return list of action objects """ | ||||
rules = [l for l in (r.strip() for r in rules.splitlines()) | ||||
if l and not l.startswith('#')] | ||||
actions = [] | ||||
Augie Fackler
|
r17064 | for r in rules: | ||
if ' ' not in r: | ||||
timeless
|
r27545 | raise error.ParseError(_('malformed line "%s"') % r) | ||
Mateusz Kwapich
|
r27082 | verb, rest = r.split(' ', 1) | ||
Mateusz Kwapich
|
r27208 | if verb not in actiontable: | ||
timeless
|
r27545 | raise error.ParseError(_('unknown action "%s"') % verb) | ||
Mateusz Kwapich
|
r27208 | |||
Mateusz Kwapich
|
r27082 | action = actiontable[verb].fromrule(state, rest) | ||
Mateusz Kwapich
|
r27208 | actions.append(action) | ||
return actions | ||||
timeless
|
r27543 | def warnverifyactions(ui, repo, actions, state, ctxs): | ||
try: | ||||
verifyactions(actions, state, ctxs) | ||||
timeless
|
r27545 | except error.ParseError: | ||
timeless
|
r27543 | if repo.vfs.exists('histedit-last-edit.txt'): | ||
ui.warn(_('warning: histedit rules saved ' | ||||
'to: .hg/histedit-last-edit.txt\n')) | ||||
raise | ||||
Mateusz Kwapich
|
r27208 | def verifyactions(actions, state, ctxs): | ||
"""Verify that there exists exactly one action per given changeset and | ||||
other constraints. | ||||
Will abort if there are to many or too few rules, a malformed rule, | ||||
or a rule on a changeset outside of the user-given range. | ||||
""" | ||||
expected = set(c.hex() for c in ctxs) | ||||
seen = set() | ||||
timeless
|
r27541 | prev = None | ||
Mateusz Kwapich
|
r27208 | for action in actions: | ||
timeless
|
r27541 | action.verify(prev) | ||
prev = action | ||||
Mateusz Kwapich
|
r27082 | constraints = action.constraints() | ||
for constraint in constraints: | ||||
Augie Fackler
|
r27086 | if constraint not in _constraints.known(): | ||
timeless
|
r27545 | raise error.ParseError(_('unknown constraint "%s"') % | ||
constraint) | ||||
Mateusz Kwapich
|
r27082 | |||
nodetoverify = action.nodetoverify() | ||||
if nodetoverify is not None: | ||||
ha = node.hex(nodetoverify) | ||||
Augie Fackler
|
r27086 | if _constraints.noother in constraints and ha not in expected: | ||
timeless
|
r27545 | raise error.ParseError( | ||
timeless
|
r27712 | _('%s "%s" changeset was not a candidate') | ||
FUJIWARA Katsunori
|
r27955 | % (action.verb, ha[:12]), | ||
timeless
|
r27712 | hint=_('only use listed changesets')) | ||
Augie Fackler
|
r27086 | if _constraints.forceother in constraints and ha in expected: | ||
timeless
|
r27545 | raise error.ParseError( | ||
timeless
|
r27712 | _('%s "%s" changeset was not an edited list candidate') | ||
FUJIWARA Katsunori
|
r27955 | % (action.verb, ha[:12]), | ||
timeless
|
r27712 | hint=_('only use listed changesets')) | ||
Augie Fackler
|
r27086 | if _constraints.noduplicates in constraints and ha in seen: | ||
timeless
|
r27545 | raise error.ParseError(_( | ||
'duplicated command for changeset %s') % | ||||
Mateusz Kwapich
|
r27082 | ha[:12]) | ||
seen.add(ha) | ||||
Pierre-Yves David
|
r19048 | missing = sorted(expected - seen) # sort to stabilize output | ||
Mateusz Kwapich
|
r27414 | |||
if state.repo.ui.configbool('histedit', 'dropmissing'): | ||||
Mateusz Kwapich
|
r28519 | if len(actions) == 0: | ||
raise error.ParseError(_('no rules provided'), | ||||
hint=_('use strip extension to remove commits')) | ||||
Mateusz Kwapich
|
r27414 | drops = [drop(state, node.bin(n)) for n in missing] | ||
# put the in the beginning so they execute immediately and | ||||
# don't show in the edit-plan in the future | ||||
actions[:0] = drops | ||||
elif missing: | ||||
timeless
|
r27545 | raise error.ParseError(_('missing rules for changeset %s') % | ||
Mateusz Kwapich
|
r24002 | missing[0][:12], | ||
Mateusz Kwapich
|
r27414 | hint=_('use "drop %s" to discard, see also: ' | ||
'"hg help -e histedit.config"') % missing[0][:12]) | ||||
Pierre-Yves David
|
r17663 | |||
Kostia Balytskyi
|
r28216 | def adjustreplacementsfrommarkers(repo, oldreplacements): | ||
"""Adjust replacements from obsolescense markers | ||||
Replacements structure is originally generated based on | ||||
histedit's state and does not account for changes that are | ||||
not recorded there. This function fixes that by adding | ||||
data read from obsolescense markers""" | ||||
if not obsolete.isenabled(repo, obsolete.createmarkersopt): | ||||
return oldreplacements | ||||
unfi = repo.unfiltered() | ||||
Pierre-Yves David
|
r28224 | nm = unfi.changelog.nodemap | ||
obsstore = repo.obsstore | ||||
Kostia Balytskyi
|
r28216 | newreplacements = list(oldreplacements) | ||
oldsuccs = [r[1] for r in oldreplacements] | ||||
# successors that have already been added to succstocheck once | ||||
seensuccs = set().union(*oldsuccs) # create a set from an iterable of tuples | ||||
succstocheck = list(seensuccs) | ||||
while succstocheck: | ||||
n = succstocheck.pop() | ||||
Pierre-Yves David
|
r28224 | missing = nm.get(n) is None | ||
markers = obsstore.successors.get(n, ()) | ||||
if missing and not markers: | ||||
# dead end, mark it as such | ||||
Kostia Balytskyi
|
r28216 | newreplacements.append((n, ())) | ||
Pierre-Yves David
|
r28224 | for marker in markers: | ||
nsuccs = marker[1] | ||||
Kostia Balytskyi
|
r28216 | newreplacements.append((n, nsuccs)) | ||
for nsucc in nsuccs: | ||||
if nsucc not in seensuccs: | ||||
seensuccs.add(nsucc) | ||||
succstocheck.append(nsucc) | ||||
return newreplacements | ||||
Augie Fackler
|
r22985 | def processreplacement(state): | ||
Pierre-Yves David
|
r17758 | """process the list of replacements to return | ||
1) the final mapping between original and created nodes | ||||
2) the list of temporary node created by histedit | ||||
3) the list of new commit created by histedit""" | ||||
Kostia Balytskyi
|
r28216 | replacements = adjustreplacementsfrommarkers(state.repo, state.replacements) | ||
Pierre-Yves David
|
r17758 | allsuccs = set() | ||
replaced = set() | ||||
fullmapping = {} | ||||
Augie Fackler
|
r26039 | # initialize basic set | ||
# fullmapping records all operations recorded in replacement | ||||
Pierre-Yves David
|
r17758 | for rep in replacements: | ||
allsuccs.update(rep[1]) | ||||
replaced.add(rep[0]) | ||||
fullmapping.setdefault(rep[0], set()).update(rep[1]) | ||||
new = allsuccs - replaced | ||||
tmpnodes = allsuccs & replaced | ||||
Augie Fackler
|
r26039 | # Reduce content fullmapping into direct relation between original nodes | ||
Pierre-Yves David
|
r17758 | # and final node created during history edition | ||
# Dropped changeset are replaced by an empty list | ||||
toproceed = set(fullmapping) | ||||
final = {} | ||||
while toproceed: | ||||
for x in list(toproceed): | ||||
succs = fullmapping[x] | ||||
for s in list(succs): | ||||
if s in toproceed: | ||||
# non final node with unknown closure | ||||
# We can't process this now | ||||
break | ||||
elif s in final: | ||||
# non final node, replace with closure | ||||
succs.remove(s) | ||||
succs.update(final[s]) | ||||
else: | ||||
final[x] = succs | ||||
toproceed.remove(x) | ||||
# remove tmpnodes from final mapping | ||||
for n in tmpnodes: | ||||
del final[n] | ||||
# we expect all changes involved in final to exist in the repo | ||||
# turn `final` into list (topologically sorted) | ||||
Augie Fackler
|
r22985 | nm = state.repo.changelog.nodemap | ||
Pierre-Yves David
|
r17758 | for prec, succs in final.items(): | ||
final[prec] = sorted(succs, key=nm.get) | ||||
Pierre-Yves David
|
r17663 | |||
Pierre-Yves David
|
r17758 | # computed topmost element (necessary for bookmark) | ||
if new: | ||||
Augie Fackler
|
r22985 | newtopmost = sorted(new, key=state.repo.changelog.rev)[-1] | ||
Pierre-Yves David
|
r17758 | elif not final: | ||
# Nothing rewritten at all. we won't need `newtopmost` | ||||
# It is the same as `oldtopmost` and `processreplacement` know it | ||||
newtopmost = None | ||||
else: | ||||
# every body died. The newtopmost is the parent of the root. | ||||
Augie Fackler
|
r22985 | r = state.repo.changelog.rev | ||
newtopmost = state.repo[sorted(final, key=r)[0]].p1().node() | ||||
Pierre-Yves David
|
r17758 | |||
return final, tmpnodes, new, newtopmost | ||||
Pierre-Yves David
|
r17663 | |||
Pierre-Yves David
|
r17758 | def movebookmarks(ui, repo, mapping, oldtopmost, newtopmost): | ||
"""Move bookmark from old to newly created node""" | ||||
if not mapping: | ||||
# if nothing got rewritten there is not purpose for this function | ||||
return | ||||
moves = [] | ||||
Mads Kiilerich
|
r18370 | for bk, old in sorted(repo._bookmarks.iteritems()): | ||
Pierre-Yves David
|
r17758 | if old == oldtopmost: | ||
Pierre-Yves David
|
r18436 | # special case ensure bookmark stay on tip. | ||
Pierre-Yves David
|
r17758 | # | ||
# This is arguably a feature and we may only want that for the | ||||
# active bookmark. But the behavior is kept compatible with the old | ||||
# version for now. | ||||
moves.append((bk, newtopmost)) | ||||
continue | ||||
base = old | ||||
new = mapping.get(base, None) | ||||
if new is None: | ||||
continue | ||||
while not new: | ||||
# base is killed, trying with parent | ||||
base = repo[base].p1().node() | ||||
new = mapping.get(base, (base,)) | ||||
# nothing to move | ||||
moves.append((bk, new[-1])) | ||||
if moves: | ||||
Laurent Charignon
|
r27051 | lock = tr = None | ||
try: | ||||
lock = repo.lock() | ||||
tr = repo.transaction('histedit') | ||||
marks = repo._bookmarks | ||||
for mark, new in moves: | ||||
old = marks[mark] | ||||
ui.note(_('histedit: moving bookmarks %s from %s to %s\n') | ||||
% (mark, node.short(old), node.short(new))) | ||||
marks[mark] = new | ||||
marks.recordchange(tr) | ||||
tr.close() | ||||
finally: | ||||
release(tr, lock) | ||||
Pierre-Yves David
|
r17664 | |||
def cleanupnode(ui, repo, name, nodes): | ||||
"""strip a group of nodes from the repository | ||||
The set of node to strip may contains unknown nodes.""" | ||||
ui.debug('should strip %s nodes %s\n' % | ||||
(name, ', '.join([node.short(n) for n in nodes]))) | ||||
Bryan O'Sullivan
|
r27833 | with repo.lock(): | ||
Pierre-Yves David
|
r25906 | # do not let filtering get in the way of the cleanse | ||
timeless@mozdev.org
|
r26203 | # we should probably get rid of obsolescence marker created during the | ||
Pierre-Yves David
|
r25906 | # histedit, but we currently do not have such information. | ||
repo = repo.unfiltered() | ||||
timeless@mozdev.org
|
r26171 | # Find all nodes that need to be stripped | ||
# (we use %lr instead of %ln to silently ignore unknown items) | ||||
Pierre-Yves David
|
r17664 | nm = repo.changelog.nodemap | ||
Pierre-Yves David
|
r22873 | nodes = sorted(n for n in nodes if n in nm) | ||
Pierre-Yves David
|
r17664 | roots = [c.node() for c in repo.set("roots(%ln)", nodes)] | ||
for c in roots: | ||||
# We should process node in reverse order to strip tip most first. | ||||
# but this trigger a bug in changegroup hook. | ||||
# This would reduce bundle overhead | ||||
repair.strip(ui, repo, c) | ||||
Bryan O'Sullivan
|
r19215 | |||
Mateusz Kwapich
|
r24111 | def stripwrapper(orig, ui, repo, nodelist, *args, **kwargs): | ||
if isinstance(nodelist, str): | ||||
nodelist = [nodelist] | ||||
if os.path.exists(os.path.join(repo.path, 'histedit-state')): | ||||
state = histeditstate(repo) | ||||
state.read() | ||||
Mateusz Kwapich
|
r27207 | histedit_nodes = set([action.nodetoverify() for action | ||
in state.actions if action.nodetoverify()]) | ||||
Durham Goode
|
r24626 | strip_nodes = set([repo[n].node() for n in nodelist]) | ||
Mateusz Kwapich
|
r24111 | common_nodes = histedit_nodes & strip_nodes | ||
if common_nodes: | ||||
Pierre-Yves David
|
r26587 | raise error.Abort(_("histedit in progress, can't strip %s") | ||
Matt Mackall
|
r24196 | % ', '.join(node.short(x) for x in common_nodes)) | ||
Mateusz Kwapich
|
r24111 | return orig(ui, repo, nodelist, *args, **kwargs) | ||
extensions.wrapfunction(repair, 'strip', stripwrapper) | ||||
Bryan O'Sullivan
|
r19215 | def summaryhook(ui, repo): | ||
if not os.path.exists(repo.join('histedit-state')): | ||||
return | ||||
David Soria Parra
|
r22983 | state = histeditstate(repo) | ||
state.read() | ||||
Mateusz Kwapich
|
r27207 | if state.actions: | ||
Bryan O'Sullivan
|
r19215 | # i18n: column positioning for "hg summary" | ||
ui.write(_('hist: %s (histedit --continue)\n') % | ||||
(ui.label(_('%d remaining'), 'histedit.remaining') % | ||||
Mateusz Kwapich
|
r27207 | len(state.actions))) | ||
Bryan O'Sullivan
|
r19215 | |||
def extsetup(ui): | ||||
cmdutil.summaryhooks.add('histedit', summaryhook) | ||||
Matt Mackall
|
r19479 | cmdutil.unfinishedstates.append( | ||
Matt Mackall
|
r19496 | ['histedit-state', False, True, _('histedit in progress'), | ||
Matt Mackall
|
r19479 | _("use 'hg histedit --continue' or 'hg histedit --abort'")]) | ||
timeless
|
r27627 | cmdutil.afterresolvedstates.append( | ||
['histedit-state', _('hg histedit --continue')]) | ||||
Mateusz Kwapich
|
r27085 | if ui.configbool("experimental", "histeditng"): | ||
timeless
|
r27675 | globals()['base'] = action(['base', 'b'], | ||
_('checkout changeset and apply further changesets from there') | ||||
)(base) | ||||