hgext/evolve.py
author Pierre-Yves David <pierre-yves.david@ens-lyon.org>
Thu, 23 Aug 2012 12:51:06 +0200
changeset 482 d2531a747ff5
parent 481 4e0f1aae8d24
child 484 20e2f2dd71f1
permissions -rw-r--r--
evolve: fold command First crude version of a fold command

# states.py - introduce the state concept for mercurial changeset
#
# Copyright 2011 Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
#                Logilab SA        <contact@logilab.fr>
#                Pierre-Yves David <pierre-yves.david@ens-lyon.org>
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2 or any later version.

'''a set of commands to handle changeset mutation'''

import random

from mercurial import cmdutil
from mercurial import scmutil
from mercurial import node
from mercurial import error
from mercurial import extensions
from mercurial import commands
from mercurial import bookmarks
from mercurial import phases
from mercurial import context
from mercurial import copies
from mercurial import util
from mercurial.i18n import _
from mercurial.commands import walkopts, commitopts, commitopts2
from mercurial import hg

### util function
#############################

def noderange(repo, revsets):
    """The same as revrange but return node"""
    return map(repo.changelog.node,
               scmutil.revrange(repo, revsets))

### changeset rewriting logic
#############################

def rewrite(repo, old, updates, head, newbases, commitopts):
    """Return (nodeid, created) where nodeid is the identifier of the
    changeset generated by the rewrite process, and created is True if
    nodeid was actually created. If created is False, nodeid
    references a changeset existing before the rewrite call.
    """
    if len(old.parents()) > 1: #XXX remove this unecessary limitation.
        raise error.Abort(_('cannot amend merge changesets'))
    base = old.p1()
    updatebookmarks = _bookmarksupdater(repo, old.node())

    wlock = repo.wlock()
    try:

        # commit a new version of the old changeset, including the update
        # collect all files which might be affected
        files = set(old.files())
        for u in updates:
            files.update(u.files())

        # Recompute copies (avoid recording a -> b -> a)
        copied = copies.pathcopies(base, head)


        # prune files which were reverted by the updates
        def samefile(f):
            if f in head.manifest():
                a = head.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 = head.manifest()
        def filectxfn(repo, ctx, path):
            if path in headmf:
                fctx = head[path]
                flags = fctx.flags()
                mctx = context.memfilectx(fctx.path(), fctx.data(),
                                          islink='l' in flags,
                                          isexec='x' in flags,
                                          copied=copied.get(path))
                return mctx
            raise IOError()
        if commitopts.get('message') and commitopts.get('logfile'):
            raise util.Abort(_('options --message and --logfile are mutually'
                               ' exclusive'))
        if commitopts.get('logfile'):
            message= open(commitopts['logfile']).read()
        elif commitopts.get('message'):
            message = commitopts['message']
        else:
            message = old.description()

        user = commitopts.get('user') or old.user()
        date = commitopts.get('date') or None # old.date()
        extra = dict(commitopts.get('extra', {}))
        extra['branch'] = head.branch()

        new = context.memctx(repo,
                             parents=newbases,
                             text=message,
                             files=files,
                             filectxfn=filectxfn,
                             user=user,
                             date=date,
                             extra=extra)

        if commitopts.get('edit'):
            new._text = cmdutil.commitforceeditor(repo, new, [])
        revcount = len(repo)
        newid = repo.commitctx(new)
        new = repo[newid]
        created = len(repo) != revcount
        if created:
            updatebookmarks(newid)
            # add evolution metadata
            markers = [(u, (new,)) for u in updates]
            markers.append((old, (new,)))
            obsolete = extensions.find('obsolete')
            obsolete.createmarkers(repo, markers)
        else:
            # newid is an existing revision. It could make sense to
            # replace revisions with existing ones but probably not by
            # default.
            pass
    finally:
        wlock.release()

    return newid, created

class MergeFailure(util.Abort):
    pass

def relocate(repo, orig, dest):
    """rewrite <rev> on dest"""
    try:
        rebase = extensions.find('rebase')
        # dummy state to trick rebase node
        assert orig.p2().rev() == node.nullrev, 'no support yet'
        destbookmarks = repo.nodebookmarks(dest.node())
        cmdutil.duplicatecopies(repo, orig.node(), dest.node())
        nodesrc = orig.node()
        destphase = repo[nodesrc].phase()
        if rebase.rebasenode.func_code.co_argcount == 5:
            # rebasenode collapse argument was introduced by
            # d1afbf03e69a (2.3)
            rebase.rebasenode(repo, orig.node(), dest.node(),
                              {node.nullrev: node.nullrev}, False)
        else:
            rebase.rebasenode(repo, orig.node(), dest.node(),
                              {node.nullrev: node.nullrev})
        try:
            nodenew = rebase.concludenode(repo, orig.node(), dest.node(),
                                          node.nullid)
        except util.Abort, exc:
            class LocalMergeFailure(MergeFailure, exc.__class__):
                pass
            exc.__class__ = LocalMergeFailure
            raise
        oldbookmarks = repo.nodebookmarks(nodesrc)
        obsolete = extensions.find('obsolete')
        if nodenew is not None:
            phases.retractboundary(repo, destphase, [nodenew])
            obsolete.createmarkers(repo, [(repo[nodesrc], (repo[nodenew],))])
            for book in oldbookmarks:
                repo._bookmarks[book] = nodenew
        else:
            obsolete.createmarkers(repo, [(repo[nodesrc], ())])
            # Behave like rebase, move bookmarks to dest
            for book in oldbookmarks:
                repo._bookmarks[book] = dest.node()
        for book in destbookmarks: # restore bookmark that rebase move
            repo._bookmarks[book] = dest.node()
        if oldbookmarks or destbookmarks:
            bookmarks.write(repo)
        return nodenew
    except util.Abort:
        # Invalidate the previous setparents
        repo.dirstate.invalidate()
        raise

def _stabilizableunstable(repo, pctx):
    """Return a changectx for an unstable changeset which can be
    stabilized on top of pctx or one of its descendants. None if none
    can be found.
    """
    def selfanddescendants(repo, pctx):
        yield pctx
        for ctx in pctx.descendants():
            yield ctx

    # Look for an unstable which can be stabilized as a child of
    # node. The unstable must be a child of one of node predecessors.
    for ctx in selfanddescendants(repo, pctx):
        unstables = list(repo.set('unstable() and children(allprecursors(%d))',
                                  ctx.rev()))
        if unstables:
            return unstables[0]
    return None

def _bookmarksupdater(repo, oldid):
    """Return a callable update(newid) updating the current bookmark
    and bookmarks bound to oldid to newid.
    """
    bm = bookmarks.readcurrent(repo)
    def updatebookmarks(newid):
        dirty = False
        if bm:
            repo._bookmarks[bm] = newid
            dirty = True
        oldbookmarks = repo.nodebookmarks(oldid)
        if oldbookmarks:
            for b in oldbookmarks:
                repo._bookmarks[b] = newid
            dirty = True
        if dirty:
            bookmarks.write(repo)
    return updatebookmarks

### new command
#############################
cmdtable = {}
command = cmdutil.command(cmdtable)

@command('^stabilize|evolve|solve',
    [('n', 'dry-run', False, 'do not perform actions, print what to be done'),
    ('A', 'any', False, 'stabilize any unstable changeset'),
    ('c', 'continue', False, 'continue an interrupted stabilized'), ],
    _('[OPTIONS]...'))
def stabilize(ui, repo, **opts):
    """rebase an unstable changeset to make it stable again

    By default, take the first unstable changeset which could be
    rebased as child of the working directory parent revision or one
    of its descendants and rebase it.

    With --any, stabilize any unstable changeset.

    The working directory is updated to the rebased revision.
    """

    contopt = opts['continue']
    anyopt = opts['any']

    if contopt:
        if anyopt:
            raise util.Abort('can not specify both "--any" and "--continue"')
        graftcmd = commands.table['graft'][0]
        return graftcmd(ui, repo, old_obsolete=True, **{'continue': True})

    troubled = list(repo.revs('troubled()'))
    tr = _picknexttroubled(ui, repo, anyopt)
    if tr is None:
        if troubled:
            ui.write_err(_('nothing to stabilize here\n'))
            ui.status(_('(%i troubled changesets, do you want --any ?)\n')
                      % len(troubled))
            return 2
        else:
            ui.write_err(_('no troubled changeset\n'))
            return 1
    troubles = tr.troubles()
    if 'unstable' in troubles:
        return _solveunstable(ui, repo, tr, opts['dry_run'])
    elif 'latecomer' in troubles:
        return _solvelatecomer(ui, repo, tr, opts['dry_run'])
    elif 'conflicting' in troubles:
        ui.write_err(_('conflicting not handled yet\n'))
        return 4
    else:
        assert False  # WHAT? unknown troubles

def _picknexttroubled(ui, repo, any=False):
    """Pick a the next trouble changeset to solve"""
    tr = _stabilizableunstable(repo, repo['.'])
    if tr is None and any:
        troubled = list(repo.set('unstable()'))
        if not troubled:
            troubled = list(repo.set('latecomer()'))
        if not troubled:
            troubled = list(repo.set('conflicting()'))
        if troubled:
            tr = troubled[0]
    return tr


def _solveunstable(ui, repo, orig, dryrun=False):
    """Stabilize a unstable changeset"""
    obsolete = extensions.find('obsolete')
    obs = orig.parents()[0]
    if not obs.obsolete():
        obs = orig.parents()[1]
    assert obs.obsolete()
    newer = obsolete.newerversion(repo, obs.node())
    # search of a parent which is not killed
    while newer == [()]:
        ui.debug("stabilize target %s is plain dead,"
                 " trying to stabilize on it's parent")
        obs = obs.parents()[0]
        newer = obsolete.newerversion(repo, obs.node())
    if len(newer) > 1:
        ui.write_err(_("conflict rewriting. can't choose destination\n"))
        return 2
    targets = newer[0]
    assert targets
    if len(targets) > 1:
        ui.write_err(_("does not handle splitted parent yet\n"))
        return 2
    target = targets[0]
    displayer = cmdutil.show_changeset(ui, repo, {'template': shorttemplate})
    target = repo[target]
    repo.ui.status(_('move:'))
    if not ui.quiet:
        displayer.show(orig)
    repo.ui.status(_('atop:'))
    if not ui.quiet:
        displayer.show(target)
    todo = 'hg rebase -Dr %s -d %s\n' % (orig, target)
    if dryrun:
        repo.ui.write(todo)
    else:
        repo.ui.note(todo)
        lock = repo.lock()
        try:
            relocate(repo, orig, target)
        except MergeFailure:
            repo.opener.write('graftstate', orig.hex() + '\n')
            repo.ui.write_err(_('stabilize failed!\n'))
            repo.ui.write_err(_('fix conflict and run "hg stabilize --continue"\n'))
            raise
        finally:
            lock.release()

def _solvelatecomer(ui, repo, latecomer, dryrun=False):
    """Stabilize a latecomer changeset"""
    # For now we deny latecomer merge
    if len(latecomer.parents()) > 1:
        raise util.Abort('late comer stabilization is confused by latecomer'
                         ' %s being a merge' % latecomer)
    prec = repo.set('last(allprecursors(%d) and public())', latecomer).next()
    # For now we deny target merge
    if len(prec.parents()) > 1:
        raise util.Abort('late comer stabilization is confused by precursors'
                         ' %s being a merge' % prec)

    displayer = cmdutil.show_changeset(ui, repo, {'template': shorttemplate})
    repo.ui.status(_('recreate:'))
    if not ui.quiet:
        displayer.show(latecomer)
    repo.ui.status(_('atop:'))
    if not ui.quiet:
        displayer.show(prec)
    obsolete = extensions.find('obsolete')
    if dryrun:
        todo = 'hg rebase --rev %s --detach %s;\n' % (latecomer, prec.p1())
        repo.ui.write(todo)
        repo.ui.write('hg update %s;\n' % prec)
        repo.ui.write('hg revert --all --rev %s;\n' % latecomer)
        repo.ui.write('hg commit --msg "latecomer update to %s"')
        return 0
    wlock = repo.wlock()
    try:
        newid = tmpctx = None
        tmpctx = latecomer
        lock = repo.lock()
        try:
            bmupdate = _bookmarksupdater(repo, latecomer.node())
            # Basic check for common parent. Far too complicated and fragile
            tr = repo.transaction('latecomer-stabilize')
            try:
                if not list(repo.set('parents(%d) and parents(%d)', latecomer, prec)):
                    # Need to rebase the changeset at the right place
                    repo.ui.status(_('rebasing to destination parent: %s\n') % prec.p1())
                    try:
                        tmpid = relocate(repo, latecomer, prec.p1())
                        if tmpid is not None:
                            tmpctx = repo[tmpid]
                            obsolete.createmarkers(repo, [(latecomer, (tmpctx,))])
                    except MergeFailure:
                        repo.opener.write('graftstate', latecomer.hex() + '\n')
                        repo.ui.write_err(_('stabilize failed!\n'))
                        repo.ui.write_err(_('fix conflict and run "hg stabilize --continue"\n'))
                        raise
                # Create the new commit context
                repo.ui.status(_('computing new diff\n'))
                files = set()
                copied = copies.pathcopies(prec, latecomer)
                precmanifest = prec.manifest()
                for key, val in latecomer.manifest().iteritems():
                    if precmanifest.pop(key, None) != val:
                        files.add(key)
                files.update(precmanifest)  # add missing files
                # commit it
                if files: # something to commit!
                    def filectxfn(repo, ctx, path):
                        if path in latecomer:
                            fctx = latecomer[path]
                            flags = fctx.flags()
                            mctx = context.memfilectx(fctx.path(), fctx.data(),
                                                      islink='l' in flags,
                                                      isexec='x' in flags,
                                                      copied=copied.get(path))
                            return mctx
                        raise IOError()
                    text = 'latecomer update to %s:\n\n' % prec
                    text += latecomer.description()

                    new = context.memctx(repo,
                                         parents=[prec.node(), node.nullid],
                                         text=text,
                                         files=files,
                                         filectxfn=filectxfn,
                                         user=latecomer.user(),
                                         date=latecomer.date(),
                                         extra=latecomer.extra())

                    newid = repo.commitctx(new)
                if newid is None:
                    obsolete.createmarkers(repo, [(tmpctx, ())])
                    newid = prec.node()
                else:
                    phases.retractboundary(repo, latecomer.phase(), [newid])
                    obsolete.createmarkers(repo, [(tmpctx, (repo[newid],))],
                                           flag=obsolete.latediff)
                bmupdate(newid)
                tr.close()
                repo.ui.status(_('commited as %s\n') % node.short(newid))
            finally:
                tr.release()
        finally:
            lock.release()
        # reroute the working copy parent to the new changeset
        repo.dirstate.setparents(newid, node.nullid)
    finally:
        wlock.release()


shorttemplate = '[{rev}] {desc|firstline}\n'

@command('^gdown',
         [],
         '')
def cmdgdown(ui, repo):
    """update to parent an display summary lines"""
    wkctx = repo[None]
    wparents = wkctx.parents()
    if len(wparents) != 1:
        raise util.Abort('merge in progress')

    parents = wparents[0].parents()
    displayer = cmdutil.show_changeset(ui, repo, {'template': shorttemplate})
    if len(parents) == 1:
        p = parents[0]
        hg.update(repo, p.rev())
        displayer.show(p)
        return 0
    else:
        for p in parents:
            displayer.show(p)
        ui.warn(_('multiple parents, explicitly update to one\n'))
        return 1

@command('^gup',
         [],
         '')
def cmdup(ui, repo):
    """update to child an display summary lines"""
    wkctx = repo[None]
    wparents = wkctx.parents()
    if len(wparents) != 1:
        raise util.Abort('merge in progress')

    children = [ctx for ctx in wparents[0].children() if not ctx.obsolete()]
    displayer = cmdutil.show_changeset(ui, repo, {'template': shorttemplate})
    if not children:
        ui.warn(_('No non-obsolete children\n'))
        return 1
    if len(children) == 1:
        c = children[0]
        hg.update(repo, c.rev())
        displayer.show(c)
        return 0
    else:
        for c in children:
            displayer.show(c)
        ui.warn(_('Multiple non-obsolete children, explicitly update to one\n'))
        return 1

@command('^prune|obsolete|kill',
    [('n', 'new', [], _("successor changeset"))],
    _('[OPTION] REV...'))
def kill(ui, repo, *revs, **opts):
    """mark a changeset as obsolete

    This update the parent directory to a not-killed parent if the current
    working directory parent are killed.

    XXX bookmark support
    XXX handle merge
    XXX check immutable first
    """
    wlock = repo.wlock()
    try:
        lock = repo.lock()
        try:
            new = set(noderange(repo, opts['new']))
            targetnodes = set(noderange(repo, revs))
            if new:
                sucs = tuple(repo[n] for n in new)
            else:
                sucs = ()
            markers = []
            for n in targetnodes:
                markers.append((repo[n], sucs))
            obsolete = extensions.find('obsolete')
            obsolete.createmarkers(repo, markers)

            # update to an unkilled parent
            wdp = repo['.']
            newnode = wdp
            while newnode.obsolete():
                newnode = newnode.parents()[0]
            if newnode.node() != wdp.node():
                commands.update(ui, repo, newnode.rev())
                ui.status(_('working directory now at %s\n') % newnode)
        finally:
            lock.release()
    finally:
        wlock.release()

@command('^amend|refresh',
    [('A', 'addremove', None,
     _('mark new/missing files as added/removed before committing')),
    ('n', 'note', '', _('use text as commit message for this update')),
    ('c', 'change', '', _('specifies the changesets to amend'), _('REV')),
    ('e', 'edit', False, _('invoke editor on commit messages')),
    ] + walkopts + commitopts + commitopts2,
    _('[OPTION]... [FILE]...'))
def amend(ui, repo, *pats, **opts):
    """combine a changeset with updates and replace it with a new one

    Commits a new changeset incorporating both the changes to the given files
    and all the changes from the current parent changeset into the repository.

    See :hg:`commit` for details about committing changes.

    If you don't specify -m, the parent's message will be reused.

    If you specify --change, amend additionally considers all
    changesets between the indicated changeset and the working copy
    parent as updates to be subsumed.

    Behind the scenes, Mercurial first commits the update as a regular child
    of the current parent. Then it creates a new commit on the parent's parents
    with the updated contents. Then it changes the working copy parent to this
    new combined changeset. Finally, the old changeset and its update are hidden
    from :hg:`log` (unless you use --hidden with log).

    Returns 0 on success, 1 if nothing changed.
    """

    # determine updates to subsume
    old = scmutil.revsingle(repo, opts.get('change') or '.')

    lock = repo.lock()
    try:
        wlock = repo.wlock()
        try:
            if old.phase() == phases.public:
                raise util.Abort(_("can not rewrite immutable changeset %s")
                                 % old)
            if not repo.revs('%d and (::.)', old):
                raise error.Abort(_('cannot amend non ancestor changeset'))
            tr = repo.transaction('amend')
            try:
                oldphase = old.phase()
                # commit current changes as update
                # code copied from commands.commit to avoid noisy messages
                ciopts = dict(opts)
                ciopts.pop('message', None)
                ciopts.pop('logfile', None)
                ciopts['message'] = opts.get('note') or ('amends %s' % old.hex())
                e = cmdutil.commiteditor
                def commitfunc(ui, repo, message, match, opts):
                    return repo.commit(message, opts.get('user'), opts.get('date'),
                                       match, editor=e)
                revcount = len(repo)
                tempid = cmdutil.commit(ui, repo, commitfunc, pats, ciopts)
                if len(repo) == revcount:
                    # No revision created
                    tempid = None

                # find all changesets to be considered updates
                head = repo['.']
                updatenodes = set(repo.changelog.nodesbetween(
                        roots=[old.node()], heads=[head.node()])[0])
                updatenodes.remove(old.node())
                okoptions = ['message', 'logfile', 'edit', 'user']
                if not updatenodes:
                    for o in okoptions:
                        if opts.get(o):
                            break
                    else:
                        raise error.Abort(_('no updates found'))
                updates = [repo[n] for n in updatenodes]

                # perform amend
                if opts.get('edit'):
                    opts['force_editor'] = True
                newid, created = rewrite(repo, old, updates, head,
                                         [old.p1().node(), old.p2().node()], opts)
                if created:
                    # reroute the working copy parent to the new changeset
                    phases.retractboundary(repo, oldphase, [newid])
                    repo.dirstate.setparents(newid, node.nullid)
                else:
                    # rewrite() recreated an existing revision, discard
                    # the intermediate revision if any. No need to update
                    # phases or parents.
                    if tempid is not None:
                        obsolete = extensions.find('obsolete')
                        obsolete.createmarkers(repo, [(repo[tempid], ())])
                    # XXX: need another message in collapse case.
                    tr.close()
                    raise error.Abort(_('no updates found'))
                tr.close()
            finally:
                tr.release()
        finally:
            wlock.release()
    finally:
        lock.release()

def _commitfiltered(repo, ctx, match):
    """Recommit ctx with changed files not in match. Return the new
    node identifier, or None if nothing changed.
    """
    base = ctx.p1()
    m, a, r = repo.status(base, ctx)[:3]
    allfiles = set(m + a + r)
    files = set(f for f in allfiles if not match(f))
    if files == allfiles:
        return None

    # Filter copies
    copied = copies.pathcopies(base, ctx)
    copied = dict((src, dst) for src, dst in copied.iteritems()
                  if dst in files)
    def filectxfn(repo, memctx, path):
        if path not in ctx:
            raise IOError()
        fctx = ctx[path]
        flags = fctx.flags()
        mctx = context.memfilectx(fctx.path(), fctx.data(),
                                  islink='l' in flags,
                                  isexec='x' in flags,
                                  copied=copied.get(path))
        return mctx

    new = context.memctx(repo,
                         parents=[base.node(), node.nullid],
                         text=ctx.description(),
                         files=files,
                         filectxfn=filectxfn,
                         user=ctx.user(),
                         date=ctx.date(),
                         extra=ctx.extra())
    # commitctx always create a new revision, no need to check
    newid = repo.commitctx(new)
    return newid

def _uncommitdirstate(repo, oldctx, match):
    """Fix the dirstate after switching the working directory from
    oldctx to a copy of oldctx not containing changed files matched by
    match.
    """
    ctx = repo['.']
    ds = repo.dirstate
    copies = dict(ds.copies())
    m, a, r = repo.status(oldctx.p1(), oldctx, match=match)[:3]
    for f in m:
        if ds[f] == 'r':
            # modified + removed -> removed
            continue
        ds.normallookup(f)

    for f in a:
        if ds[f] == 'r':
            # added + removed -> unknown
            ds.drop(f)
        elif ds[f] != 'a':
            ds.add(f)

    for f in r:
        if ds[f] == 'a':
            # removed + added -> normal
            ds.normallookup(f)
        elif ds[f] != 'r':
            ds.remove(f)

    # Merge old parent and old working dir copies
    oldcopies = {}
    for f in (m + a):
        src = oldctx[f].renamed()
        if src:
            oldcopies[f] = src[0]
    oldcopies.update(copies)
    copies = dict((dst, oldcopies.get(src, src))
                  for dst, src in oldcopies.iteritems())
    # Adjust the dirstate copies
    for dst, src in copies.iteritems():
        if (src not in ctx or dst in ctx or ds[dst] != 'a'):
            src = None
        ds.copy(src, dst)

@command('^uncommit',
    [('a', 'all', None, _('uncommit all changes when no arguments given')),
     ] + commands.walkopts,
    _('[OPTION]... [NAME]'))
def uncommit(ui, repo, *pats, **opts):
    """move changes from parent revision to working directory

    Changes to selected files in parent revision appear again as
    uncommitted changed in the working directory. A new revision
    without selected changes is created, becomes the new parent and
    obsoletes the previous one.

    The --include option specify pattern to uncommit
    The --exclude option specify pattern to keep in the commit

    Return 0 if changed files are uncommitted.
    """
    lock = repo.lock()
    try:
        wlock = repo.wlock()
        try:
            wctx = repo[None]
            if len(wctx.parents()) <= 0:
                raise util.Abort(_("cannot uncommit null changeset"))
            if len(wctx.parents()) > 1:
                raise util.Abort(_("cannot uncommit while merging"))
            old = repo['.']
            if old.phase() == phases.public:
                raise util.Abort(_("cannot rewrite immutable changeset"))
            if len(old.parents()) > 1:
                raise util.Abort(_("cannot uncommit merge changeset"))
            oldphase = old.phase()
            updatebookmarks = _bookmarksupdater(repo, old.node())
            # Recommit the filtered changeset
            newid = None
            if (pats or opts.get('include') or opts.get('exclude')
                or opts.get('all')):
                match = scmutil.match(old, pats, opts)
                newid = _commitfiltered(repo, old, match)
            if newid is None:
                raise util.Abort(_('nothing to uncommit'))
            # Move local changes on filtered changeset
            obsolete = extensions.find('obsolete')
            obsolete.createmarkers(repo, [(old, (repo[newid],))])
            phases.retractboundary(repo, oldphase, [newid])
            repo.dirstate.setparents(newid, node.nullid)
            _uncommitdirstate(repo, old, match)
            updatebookmarks(newid)
            if not repo[newid].files():
                ui.warn(_("new changeset is empty\n"))
                ui.status(_('(use "hg kill ." to remove it)\n'))
        finally:
            wlock.release()
    finally:
        lock.release()

def commitwrapper(orig, ui, repo, *arg, **kwargs):
    lock = repo.lock()
    try:
        obsoleted = kwargs.get('obsolete', [])
        if obsoleted:
            obsoleted = repo.set('%lr', obsoleted)
        result = orig(ui, repo, *arg, **kwargs)
        if not result: # commit successed
            new = repo['-1']
            oldbookmarks = []
            obsolete = extensions.find('obsolete')
            markers = []
            for old in obsoleted:
                oldbookmarks.extend(repo.nodebookmarks(old.node()))
                markers.append((old, (new,)))
            if markers:
                obsolete.createmarkers(repo, markers)
            for book in oldbookmarks:
                repo._bookmarks[book] = new.node()
            if oldbookmarks:
                bookmarks.write(repo)
        return result
    finally:
        lock.release()

@command('^touch',
    [('r', 'rev', [], 'revision to update'),],
    # allow to choose the seed ?
    _('[-r] revs'))
def touch(ui, repo, *revs, **opts):
    """Create successors with exact same property but hash
    
    This is used to "resurect" changeset"""
    revs = list(revs)
    revs.extend(opts['rev'])
    if not revs:
        revs = ['.']
    revs = scmutil.revrange(repo, revs)
    if not revs:
        ui.write_err('no revision to touch\n')
        return 1
    if repo.revs('public() and %ld', revs):
        raise util.Abort("can't touch public revision")
    obsolete = extensions.find('obsolete')
    wlock = repo.wlock()
    try:
        lock = repo.lock()
        try:
            tr = repo.transaction('touch')
            try:
                for r in revs:
                    ctx = repo[r]
                    extra = ctx.extra().copy()
                    extra['__touch-noise__'] = random.randint(0, 0xffffffff)
                    new, _ = rewrite(repo, ctx, [], ctx,
                                     [ctx.p1().node(), ctx.p2().node()],
                                     commitopts={'extra': extra})
                    obsolete.createmarkers(repo, [(ctx, (repo[new],))])
                    phases.retractboundary(repo, ctx.phase(), [new])
                    if ctx in repo[None].parents():
                        repo.dirstate.setparents(new, node.nullid)
                tr.close()
            finally:
                tr.release()
        finally:
            lock.release()
    finally:
        wlock.release()

@command('^fold',
    [('r', 'rev', [], 'revision to fold'),],
    # allow to choose the seed ?
    _('[-r] revs'))
def fold(ui, repo, *revs, **opts):
    """Fold multiple revision into a single one"""
    revs = list(revs)
    revs.extend(opts['rev'])
    if not revs:
        revs = ['.']
    revs = scmutil.revrange(repo, revs)
    if not revs:
        ui.write_err('no revision to fold\n')
        return 1
    roots = repo.revs('roots(%ld)', revs)
    if len(roots) > 1:
        raise util.Abort("set have multiple roots")
    root = repo[roots[0]]
    if root.phase() <= phases.public:
        raise util.Abort("can't touch public revision")
    heads = repo.revs('heads(%ld)', revs)
    if len(heads) > 1:
        raise util.Abort("set have multiple heads")
    head = repo[heads[0]]
    obsolete = extensions.find('obsolete')
    wlock = repo.wlock()
    try:
        lock = repo.lock()
        try:
            tr = repo.transaction('touch')
            try:
                allctx = [repo[r] for r in revs]
                targetphase = max(c.phase() for c in allctx)
                msg = '\n\n***\n\n'.join(c.description() for c in allctx)
                newid, _ = rewrite(repo, root, allctx, head,
                                 [root.p1().node(), root.p2().node()],
                                 commitopts={'message': msg})
                phases.retractboundary(repo, targetphase, [newid])
                obsolete.createmarkers(repo, [(ctx, (repo[newid],))
                                               for ctx in allctx])
                tr.close()
            finally:
                tr.release()
        finally:
            lock.release()
        ui.status('%i changesets folded\n' % len(revs))
        if repo.revs('. and %ld', revs):
            repo.dirstate.setparents(newid, node.nullid)
    finally:
        wlock.release()


def graftwrapper(orig, ui, repo, *revs, **kwargs):
    kwargs = dict(kwargs)
    revs = list(revs) + kwargs.get('rev', [])
    kwargs['rev'] = []
    obsoleted = kwargs.setdefault('obsolete', [])

    lock = repo.lock()
    try:
        if kwargs.get('old_obsolete'):
            if kwargs.get('continue'):
                obsoleted.extend(repo.opener.read('graftstate').splitlines())
            else:
                obsoleted.extend(revs)
        # convert obsolete target into revs to avoid alias joke
        obsoleted[:] = [str(i) for i in repo.revs('%lr', obsoleted)]
        if obsoleted and len(revs) > 1:

            raise error.Abort(_('Can not graft multiple revision while '
                                'obsoleting (for now).'))

        return commitwrapper(orig, ui, repo,*revs, **kwargs)
    finally:
        lock.release()

def extsetup(ui):
    try:
        obsolete = extensions.find('obsolete')
    except KeyError:
        raise error.Abort(_('evolution extension requires obsolete extension.'))
    try:
        rebase = extensions.find('rebase')
    except KeyError:
        rebase = None
        raise error.Abort(_('evolution extension requires rebase extension.'))

    for cmd in ['amend', 'kill', 'uncommit']:
        entry = extensions.wrapcommand(cmdtable, cmd,
                                       obsolete.warnobserrors)

    entry = extensions.wrapcommand(commands.table, 'commit', commitwrapper)
    entry[1].append(('o', 'obsolete', [],
                     _("make commit obsolete this revision")))
    entry = extensions.wrapcommand(commands.table, 'graft', graftwrapper)
    entry[1].append(('o', 'obsolete', [],
                     _("make graft obsoletes this revision")))
    entry[1].append(('O', 'old-obsolete', False,
                     _("make graft obsoletes its source")))