hgext/evolution.py
author Pierre-Yves David <pierre-yves.david@ens-lyon.org>
Mon, 28 Nov 2011 00:57:49 +0100
changeset 105 6300dd1c2df7
parent 104 6c3c7a18f079
child 108 85271a8a7fb9
permissions -rw-r--r--
Add missing break line at the end of a debug message

# 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 command to make changeset evolve.'''

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 context
from mercurial import util
from mercurial.i18n import _
from mercurial.commands import walkopts, commitopts, commitopts2, logopts

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

### extension check
#############################

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

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

def rewrite(repo, old, updates, head, newbases, commitopts):
    if len(old.parents()) > 1: #XXX remove this unecessary limitation.
        raise error.Abort(_('cannot amend merge changesets'))
    base = old.p1()
    bm = bookmarks.readcurrent(repo)

    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())
        # 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()
                            and a.renamed() == b.renamed())
                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:
                return head.filectx(path)
            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()

        new = context.memctx(repo,
                             parents=newbases,
                             text=message,
                             files=files,
                             filectxfn=filectxfn,
                             user=commitopts.get('user') or None,
                             date=commitopts.get('date') or None,
                             extra=commitopts.get('extra') or None)
        newid = repo.commitctx(new)
        new = repo[newid]

        # update the bookmark
        if bm:
            repo._bookmarks[bm] = newid
            bookmarks.write(repo)

        # hide obsolete csets
        repo.changelog.hiddeninit = False

        # add evolution metadata
        repo.addobsolete(new.node(), old.node())
        for u in updates:
            repo.addobsolete(u.node(), old.node())
            repo.addobsolete(new.node(), u.node())

    finally:
        wlock.release()

    return newid

def relocate(repo, rev, dest):
    """rewrite <rev> on dest"""
    try:
        rebase = extensions.find('rebase')
        # dummy state to trick rebase node
        assert repo[rev].p2().rev() == node.nullrev, 'no support yet'
        rebase.updatedirstate(repo, rev, repo[dest].node(),
                                         repo[rev].p2().node())
        rebase.rebasenode(repo, rev, dest, {node.nullrev: node.nullrev})
        nodenew = rebase.concludenode(repo, rev, dest, node.nullid)
        nodesrc = repo.changelog.node(rev)
        repo.addobsolete(nodenew, nodesrc)
        oldbookmarks = repo.nodebookmarks(nodesrc)
        for book in oldbookmarks:
            repo._bookmarks[book] = nodenew
        if oldbookmarks:
            bookmarks.write(repo)
    except util.Abort:
        # Invalidate the previous setparents
        repo.dirstate.invalidate()
        raise



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

@command('^evolve',
    [],
    '')
def evolve(ui, repo):
    """suggest the next evolution step"""
    obsolete = extensions.find('obsolete')
    next = min(obsolete.unstables(repo))
    obs = repo[next].parents()[0]
    if not obs.obsolete():
        obs = next.parents()[1]
    assert obs.obsolete()
    newer = obsolete.newerversion(repo, obs.node())
    target = newer[-1]
    repo.ui.status('hg relocate --rev %s %s\n' % (repo[next], repo[target]))

@command('^relocate',
    [
    ('r', 'rev', '.',
     _('revision to relocate')),
    ],
    '')
def cmdrelocate(ui, repo, dest, rev='.'):
    """relocate a changeset"""
    wlock = repo.wlock()
    try:
        src = scmutil.revsingle(repo, rev, rev)
        dest = scmutil.revsingle(repo, dest, dest)
        if src == src.ancestor(dest):
            raise util.Abort(_('source is ancestor of destination'))
        relocate(repo, src.rev(), dest.rev())
        return 0
    finally:
        wlock.release()



@command('^kill',
    [],
    '<revs>')
def kill(ui, repo, *revs):
    """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:
        targetnodes = set(noderange(repo, revs))
        for n in targetnodes:
            repo.addobsolete(node.nullid, n)
        # 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:
        wlock.release()

@command('^amend',
    [('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 changeset to amend'), _('REV')),
    ('b', 'branch', '',
     _('specifies a branch for the new.'), _('REV')),
    ] + 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.
    This allows you to commit updates manually first. As a special shorthand you
    can say `--amend .` instead of '--amend p1(p1())', which subsumes your latest
    commit as an update of its parent.

    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
    change = opts.get('change')
    if change == '.':
        change = 'p1(p1())'
    old = scmutil.revsingle(repo, change)
    branch = opts.get('branch')
    if branch:
        opts.setdefault('extra', {})['branch'] = branch

    wlock = repo.wlock()
    try:
        if not old.state().mutable:
            raise util.Abort(_("can not rewrite immutable changeset %s") % old)

        # 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
        if ciopts.get('force_editor'):
            e = cmdutil.commitforceeditor
        def commitfunc(ui, repo, message, match, opts):
            return repo.commit(message, opts.get('user'), opts.get('date'), match,
                               editor=e)
        cmdutil.commit(ui, repo, commitfunc, pats, ciopts)

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

        # perform amend
        newid = rewrite(repo, old, updates, head,
                        [old.p1().node(), old.p2().node()], opts)

        # reroute the working copy parent to the new changeset
        repo.dirstate.setparents(newid, node.nullid)

    finally:
        wlock.release()