mercurial/rewriteutil.py
author Pulkit Goyal <7895pulkit@gmail.com>
Sun, 02 Aug 2020 10:24:28 -0700
changeset 45293 4e6a2889dd1d
parent 45122 a391d0710f22
child 45424 0a57ef4b3bdb
permissions -rw-r--r--
merge: use the new action based mapping introduced in mergeresult obj Before this patch, we have good number of instances of following types: ``` for f, (m, args, msg) in mresult.actions: if m == ACTION_X: do_something ``` We iterate over the whole list and then filter for a certain action. Previous patch introduced a action based mapping in mergeresult object. The above code now looks like: ``` for f, args, msg in mresult.getactions([ACTION_X, ...]): do_something ``` Differential Revision: https://phab.mercurial-scm.org/D8831

# rewriteutil.py - utility functions for rewriting changesets
#
# Copyright 2017 Octobus <contact@octobus.net>
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2 or any later version.

from __future__ import absolute_import

from .i18n import _

from . import (
    error,
    node,
    obsolete,
    revset,
)


def precheck(repo, revs, action=b'rewrite'):
    """check if revs can be rewritten
    action is used to control the error message.

    Make sure this function is called after taking the lock.
    """
    if node.nullrev in revs:
        msg = _(b"cannot %s null changeset") % action
        hint = _(b"no changeset checked out")
        raise error.Abort(msg, hint=hint)

    if len(repo[None].parents()) > 1:
        raise error.Abort(_(b"cannot %s while merging") % action)

    publicrevs = repo.revs(b'%ld and public()', revs)
    if publicrevs:
        msg = _(b"cannot %s public changesets") % action
        hint = _(b"see 'hg help phases' for details")
        raise error.Abort(msg, hint=hint)

    newunstable = disallowednewunstable(repo, revs)
    if newunstable:
        raise error.Abort(_(b"cannot %s changeset with children") % action)


def disallowednewunstable(repo, revs):
    """Checks whether editing the revs will create new unstable changesets and
    are we allowed to create them.

    To allow new unstable changesets, set the config:
        `experimental.evolution.allowunstable=True`
    """
    allowunstable = obsolete.isenabled(repo, obsolete.allowunstableopt)
    if allowunstable:
        return revset.baseset()
    return repo.revs(b"(%ld::) - %ld", revs, revs)


def skip_empty_successor(ui, command):
    empty_successor = ui.config(b'rewrite', b'empty-successor')
    if empty_successor == b'skip':
        return True
    elif empty_successor == b'keep':
        return False
    else:
        raise error.ConfigError(
            _(
                b"%s doesn't know how to handle config "
                b"rewrite.empty-successor=%s (only 'skip' and 'keep' are "
                b"supported)"
            )
            % (command, empty_successor)
        )