histedit.py
1219 lines
| 43.4 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 | |||
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 | ||||
Augie Fackler
|
r17064 | """ | ||
Augie Fackler
|
r17131 | |||
Augie Fackler
|
r17064 | try: | ||
import cPickle as pickle | ||||
Simon Heimberg
|
r19284 | pickle.dump # import now | ||
Augie Fackler
|
r17064 | except ImportError: | ||
import pickle | ||||
Siddharth Agarwal
|
r22368 | import errno | ||
Augie Fackler
|
r17064 | import os | ||
Pierre-Yves David
|
r19018 | import sys | ||
Augie Fackler
|
r17064 | |||
from mercurial import cmdutil | ||||
from mercurial import discovery | ||||
from mercurial import error | ||||
Durham Goode
|
r24757 | from mercurial import changegroup | ||
Pierre-Yves David
|
r17644 | from mercurial import copies | ||
from mercurial import context | ||||
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
|
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 | |||
Wagner Bruna
|
r17337 | # i18n: command names and abbreviations must remain untranslated | ||
FUJIWARA Katsunori
|
r17315 | editcomment = _("""# Edit history between %s and %s | ||
Augie Fackler
|
r17064 | # | ||
Adrian Zgorzałek
|
r20503 | # Commits are listed from least to most recent | ||
# | ||||
Augie Fackler
|
r17064 | # 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
|
r17064 | # d, drop = remove commit from history | ||
timeless@mozdev.org
|
r26100 | # m, mess = edit commit message without changing commit content | ||
Augie Fackler
|
r17064 | # | ||
FUJIWARA Katsunori
|
r17315 | """) | ||
Augie Fackler
|
r17064 | |||
David Soria Parra
|
r22976 | class histeditstate(object): | ||
Mateusz Kwapich
|
r24112 | def __init__(self, repo, parentctxnode=None, rules=None, keep=None, | ||
David Soria Parra
|
r22984 | topmost=None, replacements=None, lock=None, wlock=None): | ||
David Soria Parra
|
r22976 | self.repo = repo | ||
self.rules = rules | ||||
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: | ||
fp = self.repo.vfs('histedit-state', 'r') | ||||
Gregory Szorc
|
r25660 | except IOError as err: | ||
David Soria Parra
|
r22983 | if err.errno != errno.ENOENT: | ||
raise | ||||
raise util.Abort(_('no histedit in progress')) | ||||
Durham Goode
|
r24756 | try: | ||
data = pickle.load(fp) | ||||
parentctxnode, rules, keep, topmost, replacements = data | ||||
Durham Goode
|
r24757 | backupfile = None | ||
Durham Goode
|
r24756 | except pickle.UnpicklingError: | ||
data = self._load() | ||||
Durham Goode
|
r24757 | parentctxnode, rules, keep, topmost, replacements, backupfile = data | ||
David Soria Parra
|
r22983 | |||
Mateusz Kwapich
|
r24112 | self.parentctxnode = parentctxnode | ||
David Soria Parra
|
r22983 | self.rules = rules | ||
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) | ||||
fp.write('%d\n' % len(self.rules)) | ||||
for rule in self.rules: | ||||
Durham Goode
|
r24810 | fp.write('%s\n' % rule[0]) # action | ||
fp.write('%s\n' % rule[1]) # remainder | ||||
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): | ||
self.repo.vfs.unlink('histedit-state') | ||||
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. | ||||
""" | ||||
repo = state.repo | ||||
rulehash = rule.strip().split(' ', 1)[0] | ||||
try: | ||||
node = repo[rulehash].node() | ||||
except error.RepoError: | ||||
raise util.Abort(_('unknown changeset %s listed') % rulehash[:12]) | ||||
return cls(state, node) | ||||
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] | ||||
hg.update(repo, self.state.parentctxnode) | ||||
stats = applychanges(repo.ui, repo, rulectx, {}) | ||||
if stats and stats[3] > 0: | ||||
raise error.InterventionRequired(_('Fix up the change and run ' | ||||
'hg histedit --continue')) | ||||
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: | ||||
self.repo.ui.warn(_('%s: empty changeset\n') % | ||||
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, | ||
Pierre-Yves David
|
r17647 | # just applies changes on parent for edition | ||
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(): | ||||
raise util.Abort( | ||||
_("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) | ||
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 | |||
Durham Goode
|
r24770 | class edit(histeditaction): | ||
def run(self): | ||||
repo = self.repo | ||||
rulectx = repo[self.node] | ||||
hg.update(repo, self.state.parentctxnode) | ||||
applychanges(repo.ui, repo, rulectx, {}) | ||||
raise error.InterventionRequired( | ||||
_('Make changes as needed, you may commit or record as needed ' | ||||
'now.\nWhen you are finished, run hg histedit --continue to ' | ||||
'resume.')) | ||||
def commiteditor(self): | ||||
return cmdutil.getcommiteditor(edit=True, editform='histedit.edit') | ||||
Augie Fackler
|
r17064 | |||
Durham Goode
|
r24771 | class fold(histeditaction): | ||
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() | ||
hg.update(repo, parent) | ||||
### 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, [] | ||||
hg.update(repo, n) | ||||
replacements = [(oldctx.node(), (newnode,)), | ||||
(ctx.node(), (n,)), | ||||
(newnode, (n,)), | ||||
] | ||||
for ich in internalchanges: | ||||
replacements.append((ich, (n,))) | ||||
return repo[n], replacements | ||||
Durham Goode
|
r24771 | |||
Augie Fackler
|
r26246 | class _multifold(fold): | ||
"""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. | ||||
""" | ||||
def skipprompt(self): | ||||
return True | ||||
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 | |||
Durham Goode
|
r24768 | class drop(histeditaction): | ||
def run(self): | ||||
parentctx = self.repo[self.state.parentctxnode] | ||||
return parentctx, [(self.node, tuple())] | ||||
Augie Fackler
|
r17064 | |||
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: | ||||
raise util.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') | ||||
raise util.Abort(msg, hint=hint) | ||||
return repo.lookup(roots[0]) | ||||
Pierre-Yves David
|
r19021 | |||
Augie Fackler
|
r17064 | actiontable = {'p': pick, | ||
'pick': pick, | ||||
'e': edit, | ||||
'edit': edit, | ||||
'f': fold, | ||||
'fold': fold, | ||||
Augie Fackler
|
r26246 | '_multifold': _multifold, | ||
Mike Edgar
|
r22152 | 'r': rollup, | ||
'roll': rollup, | ||||
Augie Fackler
|
r17064 | 'd': drop, | ||
'drop': drop, | ||||
'm': message, | ||||
'mess': message, | ||||
} | ||||
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'))], | ||
FUJIWARA Katsunori
|
r19622 | _("ANCESTOR | --outgoing [URL]")) | ||
Pierre-Yves David
|
r19020 | def histedit(ui, repo, *freeargs, **opts): | ||
Augie Fackler
|
r17131 | """interactively edit changeset history | ||
FUJIWARA Katsunori
|
r19621 | |||
This command edits changesets between ANCESTOR and the parent of | ||||
the working directory. | ||||
FUJIWARA Katsunori
|
r19622 | |||
With --outgoing, this edits changesets not found in the | ||||
destination repository. If URL of the destination is omitted, the | ||||
'default-push' (or 'default') path will be used. | ||||
FUJIWARA Katsunori
|
r19842 | |||
timeless@mozdev.org
|
r26096 | For safety, this command is also aborted if there are ambiguous | ||
outgoing revisions which may confuse users: for example, if there | ||||
are multiple branches containing outgoing revisions. | ||||
FUJIWARA Katsunori
|
r19842 | |||
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 | |||
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 | |||
David Soria Parra
|
r22984 | def _histedit(ui, repo, state, *freeargs, **opts): | ||
Augie Fackler
|
r17064 | # TODO only abort if we try and histedit mq patches, not just | ||
# blanket if mq patches are applied somewhere | ||||
mq = getattr(repo, 'mq', None) | ||||
if mq and mq.applied: | ||||
raise util.Abort(_('source has mq patches applied')) | ||||
Pierre-Yves David
|
r19020 | # basic argument incompatibility processing | ||
outg = opts.get('outgoing') | ||||
cont = opts.get('continue') | ||||
Mateusz Kwapich
|
r24142 | editplan = opts.get('edit_plan') | ||
Pierre-Yves David
|
r19020 | abort = opts.get('abort') | ||
force = opts.get('force') | ||||
rules = opts.get('commands', '') | ||||
revs = opts.get('rev', []) | ||||
goal = 'new' # This invocation goal, in new, continue, abort | ||||
if force and not outg: | ||||
raise util.Abort(_('--force only allowed with --outgoing')) | ||||
if cont: | ||||
Augie Fackler
|
r25149 | if any((outg, abort, revs, freeargs, rules, editplan)): | ||
Pierre-Yves David
|
r19020 | raise util.Abort(_('no arguments allowed with --continue')) | ||
goal = 'continue' | ||||
elif abort: | ||||
Augie Fackler
|
r25149 | if any((outg, revs, freeargs, rules, editplan)): | ||
Pierre-Yves David
|
r19020 | raise util.Abort(_('no arguments allowed with --abort')) | ||
goal = 'abort' | ||||
Mateusz Kwapich
|
r24142 | elif editplan: | ||
Augie Fackler
|
r25149 | if any((outg, revs, freeargs)): | ||
Wagner Bruna
|
r24831 | raise util.Abort(_('only --commands argument allowed with ' | ||
Mateusz Kwapich
|
r24142 | '--edit-plan')) | ||
goal = 'edit-plan' | ||||
Pierre-Yves David
|
r19020 | else: | ||
if os.path.exists(os.path.join(repo.path, 'histedit-state')): | ||||
raise util.Abort(_('history edit already in progress, try ' | ||||
'--continue or --abort')) | ||||
if outg: | ||||
if revs: | ||||
raise util.Abort(_('no revisions allowed with --outgoing')) | ||||
if len(freeargs) > 1: | ||||
raise util.Abort( | ||||
_('only one repo argument allowed with --outgoing')) | ||||
else: | ||||
Pierre-Yves David
|
r19021 | revs.extend(freeargs) | ||
Durham Goode
|
r24009 | if len(revs) == 0: | ||
Matt Mackall
|
r25824 | # experimental config: histedit.defaultrev | ||
Durham Goode
|
r24009 | histeditdefault = ui.config('histedit', 'defaultrev') | ||
if histeditdefault: | ||||
revs.append(histeditdefault) | ||||
Pierre-Yves David
|
r19021 | if len(revs) != 1: | ||
Pierre-Yves David
|
r19020 | raise util.Abort( | ||
FUJIWARA Katsunori
|
r19621 | _('histedit requires exactly one ancestor revision')) | ||
Pierre-Yves David
|
r19020 | |||
Augie Fackler
|
r17064 | |||
David Soria Parra
|
r22977 | replacements = [] | ||
Durham Goode
|
r25330 | state.keep = opts.get('keep', False) | ||
Laurent Charignon
|
r25808 | supportsmarkers = obsolete.isenabled(repo, obsolete.createmarkersopt) | ||
David Soria Parra
|
r22977 | |||
# rebuild state | ||||
Pierre-Yves David
|
r19020 | if goal == 'continue': | ||
David Soria Parra
|
r22983 | state.read() | ||
David Soria Parra
|
r22980 | state = bootstrapcontinue(ui, state, opts) | ||
Mateusz Kwapich
|
r24142 | elif goal == 'edit-plan': | ||
state.read() | ||||
if not rules: | ||||
Durham Goode
|
r24920 | comment = editcomment % (node.short(state.parentctxnode), | ||
node.short(state.topmost)) | ||||
Mateusz Kwapich
|
r24142 | rules = ruleeditor(repo, ui, state.rules, comment) | ||
else: | ||||
if rules == '-': | ||||
f = sys.stdin | ||||
else: | ||||
f = open(rules) | ||||
rules = f.read() | ||||
f.close() | ||||
rules = [l for l in (r.strip() for r in rules.splitlines()) | ||||
if l and not l.startswith('#')] | ||||
rules = verifyrules(rules, repo, [repo[c] for [_a, c] in state.rules]) | ||||
state.rules = rules | ||||
state.write() | ||||
return | ||||
Pierre-Yves David
|
r19020 | elif goal == 'abort': | ||
David Soria Parra
|
r22983 | state.read() | ||
Pierre-Yves David
|
r25898 | tmpnodes, leafs = newnodestoabort(state) | ||
David Soria Parra
|
r22977 | ui.debug('restore wc to old parent %s\n' % node.short(state.topmost)) | ||
Durham Goode
|
r24757 | |||
# 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) | ||||
changegroup.addchangegroup(repo, gen, 'histedit', | ||||
'bundle:' + backupfile) | ||||
os.remove(backupfile) | ||||
Matt Mackall
|
r19519 | # check whether we should update away | ||
Pierre-Yves David
|
r25910 | if repo.unfiltered().revs('parents() and (%n or %ln::)', | ||
Pierre-Yves David
|
r25909 | state.parentctxnode, leafs | tmpnodes): | ||
Pierre-Yves David
|
r25908 | hg.clean(repo, state.topmost) | ||
Pierre-Yves David
|
r25894 | cleanupnode(ui, repo, 'created', tmpnodes) | ||
cleanupnode(ui, repo, 'temp', leafs) | ||||
David Soria Parra
|
r22978 | state.clear() | ||
Augie Fackler
|
r17064 | return | ||
else: | ||||
Matt Mackall
|
r19479 | cmdutil.checkunfinished(repo) | ||
Augie Fackler
|
r17064 | cmdutil.bailifchanged(repo) | ||
Pierre-Yves David
|
r17665 | topmost, empty = repo.dirstate.parents() | ||
Pierre-Yves David
|
r19021 | if outg: | ||
if freeargs: | ||||
remote = freeargs[0] | ||||
else: | ||||
remote = None | ||||
root = findoutgoing(ui, repo, remote, force, opts) | ||||
else: | ||||
Augie Fackler
|
r21950 | rr = list(repo.set('roots(%ld)', scmutil.revrange(repo, revs))) | ||
if len(rr) != 1: | ||||
Wagner Bruna
|
r21175 | raise util.Abort(_('The specified revisions must have ' | ||
David Soria Parra
|
r20806 | 'exactly one common root')) | ||
Augie Fackler
|
r21950 | root = rr[0].node() | ||
Augie Fackler
|
r17064 | |||
Durham Goode
|
r25330 | revs = between(repo, root, topmost, state.keep) | ||
Pierre-Yves David
|
r17766 | if not revs: | ||
Simon Heimberg
|
r18608 | raise util.Abort(_('%s is not an ancestor of working directory') % | ||
Pierre-Yves David
|
r19021 | node.short(root)) | ||
Augie Fackler
|
r17064 | |||
ctxs = [repo[r] for r in revs] | ||||
if not rules: | ||||
Mateusz Kwapich
|
r24142 | comment = editcomment % (node.short(root), node.short(topmost)) | ||
rules = ruleeditor(repo, ui, [['pick', c] for c in ctxs], comment) | ||||
Augie Fackler
|
r17064 | else: | ||
Pierre-Yves David
|
r19018 | if rules == '-': | ||
f = sys.stdin | ||||
else: | ||||
f = open(rules) | ||||
Augie Fackler
|
r17064 | rules = f.read() | ||
f.close() | ||||
rules = [l for l in (r.strip() for r in rules.splitlines()) | ||||
Mike Edgar
|
r22280 | if l and not l.startswith('#')] | ||
Augie Fackler
|
r17064 | rules = verifyrules(rules, repo, ctxs) | ||
Mateusz Kwapich
|
r24112 | parentctxnode = repo[root].parents()[0].node() | ||
Augie Fackler
|
r17064 | |||
Mateusz Kwapich
|
r24112 | state.parentctxnode = parentctxnode | ||
David Soria Parra
|
r22984 | state.rules = rules | ||
state.topmost = topmost | ||||
state.replacements = replacements | ||||
Augie Fackler
|
r17064 | |||
Durham Goode
|
r24757 | # 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 | ||||
Augie Fackler
|
r26246 | # preprocess rules so that we can hide inner folds from the user | ||
# and only show one editor | ||||
rules = state.rules[:] | ||||
for idx, ((action, ha), (nextact, unused)) in enumerate( | ||||
zip(rules, rules[1:] + [(None, None)])): | ||||
if action == 'fold' and nextact == 'fold': | ||||
state.rules[idx] = '_multifold', ha | ||||
David Soria Parra
|
r22977 | while state.rules: | ||
state.write() | ||||
action, ha = state.rules.pop(0) | ||||
Mateusz Kwapich
|
r24002 | ui.debug('histedit: processing %s %s\n' % (action, ha[:12])) | ||
Durham Goode
|
r24774 | actobj = actiontable[action].fromrule(state, ha) | ||
parentctx, replacement_ = actobj.run() | ||||
Mateusz Kwapich
|
r24112 | state.parentctxnode = parentctx.node() | ||
David Soria Parra
|
r22977 | state.replacements.extend(replacement_) | ||
Mateusz Kwapich
|
r24111 | state.write() | ||
Augie Fackler
|
r17064 | |||
Mateusz Kwapich
|
r24112 | hg.update(repo, state.parentctxnode) | ||
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)) | ||||
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 | |||
cleanupnode(ui, repo, 'temp', tmpnodes) | ||||
David Soria Parra
|
r22978 | state.clear() | ||
Augie Fackler
|
r17064 | if os.path.exists(repo.sjoin('undo')): | ||
os.unlink(repo.sjoin('undo')) | ||||
Durham Goode
|
r24774 | def bootstrapcontinue(ui, state, opts): | ||
repo = state.repo | ||||
Durham Goode
|
r24959 | if state.rules: | ||
action, currentnode = state.rules.pop(0) | ||||
Olle Lundberg
|
r20648 | |||
Durham Goode
|
r24959 | actobj = actiontable[action].fromrule(state, currentnode) | ||
Durham Goode
|
r24774 | s = repo.status() | ||
Durham Goode
|
r24766 | if s.modified or s.added or s.removed or s.deleted: | ||
Durham Goode
|
r24959 | actobj.continuedirty() | ||
s = repo.status() | ||||
if s.modified or s.added or s.removed or s.deleted: | ||||
raise util.Abort(_("working copy still dirty")) | ||||
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)): | ||
Pierre-Yves David
|
r17762 | raise util.Abort(_('cannot edit history that would orphan nodes')) | ||
Augie Fackler
|
r19473 | if repo.revs('(%ld) and merge()', ctxs): | ||
raise util.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(): | ||
Jordi Gutiérrez Hermoso
|
r25412 | raise util.Abort(_('cannot edit public changeset: %s') % root, | ||
hint=_('see "hg help phases" for details')) | ||||
Pierre-Yves David
|
r17765 | return [c.node() for c in ctxs] | ||
Pierre-Yves David
|
r17642 | |||
Mateusz Kwapich
|
r24141 | def makedesc(repo, action, rev): | ||
"""build a initial action line for a ctx | ||||
Pierre-Yves David
|
r17643 | |||
line are in the form: | ||||
Mateusz Kwapich
|
r24141 | <action> <hash> <rev> <summary> | ||
Pierre-Yves David
|
r17643 | """ | ||
Mateusz Kwapich
|
r24141 | ctx = repo[rev] | ||
Pierre-Yves David
|
r17643 | summary = '' | ||
Mateusz Kwapich
|
r24141 | if ctx.description(): | ||
summary = ctx.description().splitlines()[0] | ||||
line = '%s %s %d %s' % (action, ctx, ctx.rev(), summary) | ||||
FUJIWARA Katsunori
|
r21858 | # trim to 80 columns so it's not stupidly wide in my editor | ||
Mateusz Kwapich
|
r24199 | maxlen = repo.ui.configint('histedit', 'linelen', default=80) | ||
maxlen = max(maxlen, 22) # avoid truncating hash | ||||
return util.ellipsis(line, maxlen) | ||||
Pierre-Yves David
|
r17643 | |||
Mateusz Kwapich
|
r24140 | def ruleeditor(repo, ui, rules, editcomment=""): | ||
"""open an editor to edit rules | ||||
rules are in the format [ [act, ctx], ...] like in state.rules | ||||
""" | ||||
Mateusz Kwapich
|
r24141 | rules = '\n'.join([makedesc(repo, act, rev) for [act, rev] in rules]) | ||
Mateusz Kwapich
|
r24140 | rules += '\n\n' | ||
rules += editcomment | ||||
rules = ui.edit(rules, ui.username()) | ||||
# 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 | ||||
Augie Fackler
|
r17064 | def verifyrules(rules, repo, ctxs): | ||
"""Verify that there exists exactly one edit rule per given changeset. | ||||
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. | ||||
""" | ||||
parsed = [] | ||||
Mateusz Kwapich
|
r24002 | expected = set(c.hex() for c in ctxs) | ||
Pierre-Yves David
|
r19047 | seen = set() | ||
Augie Fackler
|
r17064 | for r in rules: | ||
if ' ' not in r: | ||||
raise util.Abort(_('malformed line "%s"') % r) | ||||
action, rest = r.split(' ', 1) | ||||
Pierre-Yves David
|
r19039 | ha = rest.strip().split(' ', 1)[0] | ||
Augie Fackler
|
r17064 | try: | ||
Mateusz Kwapich
|
r24002 | ha = repo[ha].hex() | ||
Augie Fackler
|
r17064 | except error.RepoError: | ||
Mateusz Kwapich
|
r24002 | raise util.Abort(_('unknown changeset %s listed') % ha[:12]) | ||
Pierre-Yves David
|
r19046 | if ha not in expected: | ||
Pierre-Yves David
|
r19045 | raise util.Abort( | ||
_('may not use changesets other than the ones listed')) | ||||
Pierre-Yves David
|
r19047 | if ha in seen: | ||
Mateusz Kwapich
|
r24002 | raise util.Abort(_('duplicated command for changeset %s') % | ||
ha[:12]) | ||||
Pierre-Yves David
|
r19047 | seen.add(ha) | ||
Augie Fackler
|
r26246 | if action not in actiontable or action.startswith('_'): | ||
Augie Fackler
|
r17064 | raise util.Abort(_('unknown action "%s"') % action) | ||
parsed.append([action, ha]) | ||||
Pierre-Yves David
|
r19048 | missing = sorted(expected - seen) # sort to stabilize output | ||
if missing: | ||||
Mateusz Kwapich
|
r24002 | raise util.Abort(_('missing rules for changeset %s') % | ||
missing[0][:12], | ||||
hint=_('do you want to use the drop action?')) | ||||
Augie Fackler
|
r17064 | return parsed | ||
Pierre-Yves David
|
r17663 | |||
Pierre-Yves David
|
r25898 | def newnodestoabort(state): | ||
"""process the list of replacements to return | ||||
1) the list of final node | ||||
2) the list of temporary node | ||||
This meant to be used on abort as less data are required in this case. | ||||
""" | ||||
replacements = state.replacements | ||||
allsuccs = set() | ||||
replaced = set() | ||||
for rep in replacements: | ||||
allsuccs.update(rep[1]) | ||||
replaced.add(rep[0]) | ||||
newnodes = allsuccs - replaced | ||||
tmpnodes = allsuccs & replaced | ||||
return newnodes, tmpnodes | ||||
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""" | ||||
David Soria Parra
|
r22981 | replacements = 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: | ||||
Augie Fackler
|
r17922 | marks = repo._bookmarks | ||
Pierre-Yves David
|
r17758 | for mark, new in moves: | ||
Augie Fackler
|
r17922 | old = marks[mark] | ||
Pierre-Yves David
|
r17758 | ui.note(_('histedit: moving bookmarks %s from %s to %s\n') | ||
% (mark, node.short(old), node.short(new))) | ||||
Augie Fackler
|
r17922 | marks[mark] = new | ||
marks.write() | ||||
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]))) | ||||
lock = None | ||||
try: | ||||
lock = 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) | ||||
finally: | ||||
Olle Lundberg
|
r20647 | release(lock) | ||
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() | ||||
Durham Goode
|
r24626 | histedit_nodes = set([repo[rulehash].node() for (action, rulehash) | ||
in state.rules if rulehash in repo]) | ||||
strip_nodes = set([repo[n].node() for n in nodelist]) | ||||
Mateusz Kwapich
|
r24111 | common_nodes = histedit_nodes & strip_nodes | ||
if common_nodes: | ||||
Matt Mackall
|
r24196 | raise util.Abort(_("histedit in progress, can't strip %s") | ||
% ', '.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() | ||||
David Soria Parra
|
r22977 | if state.rules: | ||
Bryan O'Sullivan
|
r19215 | # i18n: column positioning for "hg summary" | ||
ui.write(_('hist: %s (histedit --continue)\n') % | ||||
(ui.label(_('%d remaining'), 'histedit.remaining') % | ||||
David Soria Parra
|
r22977 | len(state.rules))) | ||
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'")]) | ||