mercurial/rewriteutil.py
author Joerg Sonnenberger <joerg@bec.de>
Fri, 30 Apr 2021 02:11:58 +0200
changeset 47043 12450fbea288
parent 47020 ba6881c6a178
child 47069 5b6dd0d9171b
permissions -rw-r--r--
manifests: push down expected node length into the parser This strictly enforces the node length in the manifest lines according to what the repository expects. One test case moves large hash testing into the non-treemanifest part as treemanifests don't provide an interface for overriding just the node length for now. Differential Revision: https://phab.mercurial-scm.org/D10533
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
35242
27d5c2d2db2b rewriteutil: add utility function to check if we can create new unstable cset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
     1
# rewriteutil.py - utility functions for rewriting changesets
27d5c2d2db2b rewriteutil: add utility function to check if we can create new unstable cset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
     2
#
27d5c2d2db2b rewriteutil: add utility function to check if we can create new unstable cset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
     3
# Copyright 2017 Octobus <contact@octobus.net>
27d5c2d2db2b rewriteutil: add utility function to check if we can create new unstable cset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
     4
#
27d5c2d2db2b rewriteutil: add utility function to check if we can create new unstable cset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
     5
# This software may be used and distributed according to the terms of the
27d5c2d2db2b rewriteutil: add utility function to check if we can create new unstable cset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
     6
# GNU General Public License version 2 or any later version.
27d5c2d2db2b rewriteutil: add utility function to check if we can create new unstable cset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
     7
27d5c2d2db2b rewriteutil: add utility function to check if we can create new unstable cset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
     8
from __future__ import absolute_import
27d5c2d2db2b rewriteutil: add utility function to check if we can create new unstable cset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
     9
45424
0a57ef4b3bdb rewriteutil: extract evolve code used to replace obsolete hashes in commits
Matt Harbison <matt_harbison@yahoo.com>
parents: 45122
diff changeset
    10
import re
0a57ef4b3bdb rewriteutil: extract evolve code used to replace obsolete hashes in commits
Matt Harbison <matt_harbison@yahoo.com>
parents: 45122
diff changeset
    11
35243
490df753894d rewriteutil: add a precheck function to check if revs can be rewritten
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35242
diff changeset
    12
from .i18n import _
46113
59fa3890d40a node: import symbols explicitly
Joerg Sonnenberger <joerg@bec.de>
parents: 45853
diff changeset
    13
from .node import (
59fa3890d40a node: import symbols explicitly
Joerg Sonnenberger <joerg@bec.de>
parents: 45853
diff changeset
    14
    hex,
59fa3890d40a node: import symbols explicitly
Joerg Sonnenberger <joerg@bec.de>
parents: 45853
diff changeset
    15
    nullrev,
59fa3890d40a node: import symbols explicitly
Joerg Sonnenberger <joerg@bec.de>
parents: 45853
diff changeset
    16
)
35243
490df753894d rewriteutil: add a precheck function to check if revs can be rewritten
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35242
diff changeset
    17
35242
27d5c2d2db2b rewriteutil: add utility function to check if we can create new unstable cset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    18
from . import (
35243
490df753894d rewriteutil: add a precheck function to check if revs can be rewritten
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35242
diff changeset
    19
    error,
35242
27d5c2d2db2b rewriteutil: add utility function to check if we can create new unstable cset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    20
    obsolete,
45424
0a57ef4b3bdb rewriteutil: extract evolve code used to replace obsolete hashes in commits
Matt Harbison <matt_harbison@yahoo.com>
parents: 45122
diff changeset
    21
    obsutil,
35242
27d5c2d2db2b rewriteutil: add utility function to check if we can create new unstable cset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    22
    revset,
45424
0a57ef4b3bdb rewriteutil: extract evolve code used to replace obsolete hashes in commits
Matt Harbison <matt_harbison@yahoo.com>
parents: 45122
diff changeset
    23
    scmutil,
47016
8ad2f43fe37b rewriteutil: add devel warning if precheck is called with contexts
Martin von Zweigbergk <martinvonz@google.com>
parents: 47015
diff changeset
    24
    util,
35242
27d5c2d2db2b rewriteutil: add utility function to check if we can create new unstable cset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    25
)
27d5c2d2db2b rewriteutil: add utility function to check if we can create new unstable cset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    26
43075
57875cf423c9 style: run a patched black on a subset of mercurial
Augie Fackler <augie@google.com>
parents: 40636
diff changeset
    27
45427
78861610ded8 rewriteutil: relax the sha1 hash references to handle future hash types
Matt Harbison <matt_harbison@yahoo.com>
parents: 45425
diff changeset
    28
NODE_RE = re.compile(br'\b[0-9a-f]{6,64}\b')
45424
0a57ef4b3bdb rewriteutil: extract evolve code used to replace obsolete hashes in commits
Matt Harbison <matt_harbison@yahoo.com>
parents: 45122
diff changeset
    29
0a57ef4b3bdb rewriteutil: extract evolve code used to replace obsolete hashes in commits
Matt Harbison <matt_harbison@yahoo.com>
parents: 45122
diff changeset
    30
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43075
diff changeset
    31
def precheck(repo, revs, action=b'rewrite'):
35243
490df753894d rewriteutil: add a precheck function to check if revs can be rewritten
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35242
diff changeset
    32
    """check if revs can be rewritten
490df753894d rewriteutil: add a precheck function to check if revs can be rewritten
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35242
diff changeset
    33
    action is used to control the error message.
490df753894d rewriteutil: add a precheck function to check if revs can be rewritten
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35242
diff changeset
    34
490df753894d rewriteutil: add a precheck function to check if revs can be rewritten
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35242
diff changeset
    35
    Make sure this function is called after taking the lock.
490df753894d rewriteutil: add a precheck function to check if revs can be rewritten
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35242
diff changeset
    36
    """
46113
59fa3890d40a node: import symbols explicitly
Joerg Sonnenberger <joerg@bec.de>
parents: 45853
diff changeset
    37
    if nullrev in revs:
47015
7001f92e0ee9 rewriteutil: replace "null changeset" by "the null revision" in error message
Martin von Zweigbergk <martinvonz@google.com>
parents: 46113
diff changeset
    38
        msg = _(b"cannot %s the null revision") % action
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43075
diff changeset
    39
        hint = _(b"no changeset checked out")
45853
b4694ef45db5 errors: raise more specific errors from rewriteutil
Martin von Zweigbergk <martinvonz@google.com>
parents: 45711
diff changeset
    40
        raise error.InputError(msg, hint=hint)
35243
490df753894d rewriteutil: add a precheck function to check if revs can be rewritten
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35242
diff changeset
    41
47016
8ad2f43fe37b rewriteutil: add devel warning if precheck is called with contexts
Martin von Zweigbergk <martinvonz@google.com>
parents: 47015
diff changeset
    42
    if any(util.safehasattr(r, 'rev') for r in revs):
8ad2f43fe37b rewriteutil: add devel warning if precheck is called with contexts
Martin von Zweigbergk <martinvonz@google.com>
parents: 47015
diff changeset
    43
        repo.ui.develwarn(b"rewriteutil.precheck called with ctx not revs")
8ad2f43fe37b rewriteutil: add devel warning if precheck is called with contexts
Martin von Zweigbergk <martinvonz@google.com>
parents: 47015
diff changeset
    44
        revs = (r.rev() for r in revs)
8ad2f43fe37b rewriteutil: add devel warning if precheck is called with contexts
Martin von Zweigbergk <martinvonz@google.com>
parents: 47015
diff changeset
    45
35243
490df753894d rewriteutil: add a precheck function to check if revs can be rewritten
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35242
diff changeset
    46
    if len(repo[None].parents()) > 1:
47020
ba6881c6a178 rewriteutil: check for divergence
Martin von Zweigbergk <martinvonz@google.com>
parents: 47019
diff changeset
    47
        raise error.StateError(
ba6881c6a178 rewriteutil: check for divergence
Martin von Zweigbergk <martinvonz@google.com>
parents: 47019
diff changeset
    48
            _(b"cannot %s changesets while merging") % action
ba6881c6a178 rewriteutil: check for divergence
Martin von Zweigbergk <martinvonz@google.com>
parents: 47019
diff changeset
    49
        )
35243
490df753894d rewriteutil: add a precheck function to check if revs can be rewritten
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35242
diff changeset
    50
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43075
diff changeset
    51
    publicrevs = repo.revs(b'%ld and public()', revs)
35243
490df753894d rewriteutil: add a precheck function to check if revs can be rewritten
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35242
diff changeset
    52
    if publicrevs:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43075
diff changeset
    53
        msg = _(b"cannot %s public changesets") % action
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43075
diff changeset
    54
        hint = _(b"see 'hg help phases' for details")
45853
b4694ef45db5 errors: raise more specific errors from rewriteutil
Martin von Zweigbergk <martinvonz@google.com>
parents: 45711
diff changeset
    55
        raise error.InputError(msg, hint=hint)
35243
490df753894d rewriteutil: add a precheck function to check if revs can be rewritten
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35242
diff changeset
    56
490df753894d rewriteutil: add a precheck function to check if revs can be rewritten
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35242
diff changeset
    57
    newunstable = disallowednewunstable(repo, revs)
490df753894d rewriteutil: add a precheck function to check if revs can be rewritten
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35242
diff changeset
    58
    if newunstable:
47018
7a90fddb13b0 rewriteutil: point to help about instability when rewriting creates orphan
Martin von Zweigbergk <martinvonz@google.com>
parents: 47016
diff changeset
    59
        hint = _(b"see 'hg help evolution.instability'")
7a90fddb13b0 rewriteutil: point to help about instability when rewriting creates orphan
Martin von Zweigbergk <martinvonz@google.com>
parents: 47016
diff changeset
    60
        raise error.InputError(
7a90fddb13b0 rewriteutil: point to help about instability when rewriting creates orphan
Martin von Zweigbergk <martinvonz@google.com>
parents: 47016
diff changeset
    61
            _(b"cannot %s changeset with children") % action, hint=hint
7a90fddb13b0 rewriteutil: point to help about instability when rewriting creates orphan
Martin von Zweigbergk <martinvonz@google.com>
parents: 47016
diff changeset
    62
        )
35243
490df753894d rewriteutil: add a precheck function to check if revs can be rewritten
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35242
diff changeset
    63
47020
ba6881c6a178 rewriteutil: check for divergence
Martin von Zweigbergk <martinvonz@google.com>
parents: 47019
diff changeset
    64
    if not obsolete.isenabled(repo, obsolete.allowdivergenceopt):
ba6881c6a178 rewriteutil: check for divergence
Martin von Zweigbergk <martinvonz@google.com>
parents: 47019
diff changeset
    65
        new_divergence = _find_new_divergence(repo, revs)
ba6881c6a178 rewriteutil: check for divergence
Martin von Zweigbergk <martinvonz@google.com>
parents: 47019
diff changeset
    66
        if new_divergence:
ba6881c6a178 rewriteutil: check for divergence
Martin von Zweigbergk <martinvonz@google.com>
parents: 47019
diff changeset
    67
            local_ctx, other_ctx, base_ctx = new_divergence
ba6881c6a178 rewriteutil: check for divergence
Martin von Zweigbergk <martinvonz@google.com>
parents: 47019
diff changeset
    68
            msg = _(
ba6881c6a178 rewriteutil: check for divergence
Martin von Zweigbergk <martinvonz@google.com>
parents: 47019
diff changeset
    69
                b'cannot %s %s, as that creates content-divergence with %s'
ba6881c6a178 rewriteutil: check for divergence
Martin von Zweigbergk <martinvonz@google.com>
parents: 47019
diff changeset
    70
            ) % (
ba6881c6a178 rewriteutil: check for divergence
Martin von Zweigbergk <martinvonz@google.com>
parents: 47019
diff changeset
    71
                action,
ba6881c6a178 rewriteutil: check for divergence
Martin von Zweigbergk <martinvonz@google.com>
parents: 47019
diff changeset
    72
                local_ctx,
ba6881c6a178 rewriteutil: check for divergence
Martin von Zweigbergk <martinvonz@google.com>
parents: 47019
diff changeset
    73
                other_ctx,
ba6881c6a178 rewriteutil: check for divergence
Martin von Zweigbergk <martinvonz@google.com>
parents: 47019
diff changeset
    74
            )
ba6881c6a178 rewriteutil: check for divergence
Martin von Zweigbergk <martinvonz@google.com>
parents: 47019
diff changeset
    75
            if local_ctx.rev() != base_ctx.rev():
ba6881c6a178 rewriteutil: check for divergence
Martin von Zweigbergk <martinvonz@google.com>
parents: 47019
diff changeset
    76
                msg += _(b', from %s') % base_ctx
ba6881c6a178 rewriteutil: check for divergence
Martin von Zweigbergk <martinvonz@google.com>
parents: 47019
diff changeset
    77
            if repo.ui.verbose:
ba6881c6a178 rewriteutil: check for divergence
Martin von Zweigbergk <martinvonz@google.com>
parents: 47019
diff changeset
    78
                if local_ctx.rev() != base_ctx.rev():
ba6881c6a178 rewriteutil: check for divergence
Martin von Zweigbergk <martinvonz@google.com>
parents: 47019
diff changeset
    79
                    msg += _(
ba6881c6a178 rewriteutil: check for divergence
Martin von Zweigbergk <martinvonz@google.com>
parents: 47019
diff changeset
    80
                        b'\n    changeset %s is a successor of ' b'changeset %s'
ba6881c6a178 rewriteutil: check for divergence
Martin von Zweigbergk <martinvonz@google.com>
parents: 47019
diff changeset
    81
                    ) % (local_ctx, base_ctx)
ba6881c6a178 rewriteutil: check for divergence
Martin von Zweigbergk <martinvonz@google.com>
parents: 47019
diff changeset
    82
                msg += _(
ba6881c6a178 rewriteutil: check for divergence
Martin von Zweigbergk <martinvonz@google.com>
parents: 47019
diff changeset
    83
                    b'\n    changeset %s already has a successor in '
ba6881c6a178 rewriteutil: check for divergence
Martin von Zweigbergk <martinvonz@google.com>
parents: 47019
diff changeset
    84
                    b'changeset %s\n'
ba6881c6a178 rewriteutil: check for divergence
Martin von Zweigbergk <martinvonz@google.com>
parents: 47019
diff changeset
    85
                    b'    rewriting changeset %s would create '
ba6881c6a178 rewriteutil: check for divergence
Martin von Zweigbergk <martinvonz@google.com>
parents: 47019
diff changeset
    86
                    b'"content-divergence"\n'
ba6881c6a178 rewriteutil: check for divergence
Martin von Zweigbergk <martinvonz@google.com>
parents: 47019
diff changeset
    87
                    b'    set experimental.evolution.allowdivergence=True to '
ba6881c6a178 rewriteutil: check for divergence
Martin von Zweigbergk <martinvonz@google.com>
parents: 47019
diff changeset
    88
                    b'skip this check'
ba6881c6a178 rewriteutil: check for divergence
Martin von Zweigbergk <martinvonz@google.com>
parents: 47019
diff changeset
    89
                ) % (base_ctx, other_ctx, local_ctx)
ba6881c6a178 rewriteutil: check for divergence
Martin von Zweigbergk <martinvonz@google.com>
parents: 47019
diff changeset
    90
                raise error.InputError(msg)
ba6881c6a178 rewriteutil: check for divergence
Martin von Zweigbergk <martinvonz@google.com>
parents: 47019
diff changeset
    91
            else:
ba6881c6a178 rewriteutil: check for divergence
Martin von Zweigbergk <martinvonz@google.com>
parents: 47019
diff changeset
    92
                raise error.InputError(
ba6881c6a178 rewriteutil: check for divergence
Martin von Zweigbergk <martinvonz@google.com>
parents: 47019
diff changeset
    93
                    msg, hint=_(b"add --verbose for details")
ba6881c6a178 rewriteutil: check for divergence
Martin von Zweigbergk <martinvonz@google.com>
parents: 47019
diff changeset
    94
                )
ba6881c6a178 rewriteutil: check for divergence
Martin von Zweigbergk <martinvonz@google.com>
parents: 47019
diff changeset
    95
43075
57875cf423c9 style: run a patched black on a subset of mercurial
Augie Fackler <augie@google.com>
parents: 40636
diff changeset
    96
35242
27d5c2d2db2b rewriteutil: add utility function to check if we can create new unstable cset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    97
def disallowednewunstable(repo, revs):
27d5c2d2db2b rewriteutil: add utility function to check if we can create new unstable cset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    98
    """Checks whether editing the revs will create new unstable changesets and
27d5c2d2db2b rewriteutil: add utility function to check if we can create new unstable cset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    99
    are we allowed to create them.
27d5c2d2db2b rewriteutil: add utility function to check if we can create new unstable cset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   100
27d5c2d2db2b rewriteutil: add utility function to check if we can create new unstable cset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   101
    To allow new unstable changesets, set the config:
27d5c2d2db2b rewriteutil: add utility function to check if we can create new unstable cset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   102
        `experimental.evolution.allowunstable=True`
27d5c2d2db2b rewriteutil: add utility function to check if we can create new unstable cset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   103
    """
27d5c2d2db2b rewriteutil: add utility function to check if we can create new unstable cset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   104
    allowunstable = obsolete.isenabled(repo, obsolete.allowunstableopt)
27d5c2d2db2b rewriteutil: add utility function to check if we can create new unstable cset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   105
    if allowunstable:
27d5c2d2db2b rewriteutil: add utility function to check if we can create new unstable cset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   106
        return revset.baseset()
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43075
diff changeset
   107
    return repo.revs(b"(%ld::) - %ld", revs, revs)
45122
a391d0710f22 rewriteutil: add utility to check whether empty successors should be skipped
Manuel Jacob <me@manueljacob.de>
parents: 43077
diff changeset
   108
a391d0710f22 rewriteutil: add utility to check whether empty successors should be skipped
Manuel Jacob <me@manueljacob.de>
parents: 43077
diff changeset
   109
47020
ba6881c6a178 rewriteutil: check for divergence
Martin von Zweigbergk <martinvonz@google.com>
parents: 47019
diff changeset
   110
def _find_new_divergence(repo, revs):
ba6881c6a178 rewriteutil: check for divergence
Martin von Zweigbergk <martinvonz@google.com>
parents: 47019
diff changeset
   111
    obsrevs = repo.revs(b'%ld and obsolete()', revs)
ba6881c6a178 rewriteutil: check for divergence
Martin von Zweigbergk <martinvonz@google.com>
parents: 47019
diff changeset
   112
    for r in obsrevs:
ba6881c6a178 rewriteutil: check for divergence
Martin von Zweigbergk <martinvonz@google.com>
parents: 47019
diff changeset
   113
        div = find_new_divergence_from(repo, repo[r])
ba6881c6a178 rewriteutil: check for divergence
Martin von Zweigbergk <martinvonz@google.com>
parents: 47019
diff changeset
   114
        if div:
ba6881c6a178 rewriteutil: check for divergence
Martin von Zweigbergk <martinvonz@google.com>
parents: 47019
diff changeset
   115
            return (repo[r], repo[div[0]], repo[div[1]])
ba6881c6a178 rewriteutil: check for divergence
Martin von Zweigbergk <martinvonz@google.com>
parents: 47019
diff changeset
   116
    return None
ba6881c6a178 rewriteutil: check for divergence
Martin von Zweigbergk <martinvonz@google.com>
parents: 47019
diff changeset
   117
ba6881c6a178 rewriteutil: check for divergence
Martin von Zweigbergk <martinvonz@google.com>
parents: 47019
diff changeset
   118
ba6881c6a178 rewriteutil: check for divergence
Martin von Zweigbergk <martinvonz@google.com>
parents: 47019
diff changeset
   119
def find_new_divergence_from(repo, ctx):
ba6881c6a178 rewriteutil: check for divergence
Martin von Zweigbergk <martinvonz@google.com>
parents: 47019
diff changeset
   120
    """return divergent revision if rewriting an obsolete cset (ctx) will
ba6881c6a178 rewriteutil: check for divergence
Martin von Zweigbergk <martinvonz@google.com>
parents: 47019
diff changeset
   121
    create divergence
ba6881c6a178 rewriteutil: check for divergence
Martin von Zweigbergk <martinvonz@google.com>
parents: 47019
diff changeset
   122
ba6881c6a178 rewriteutil: check for divergence
Martin von Zweigbergk <martinvonz@google.com>
parents: 47019
diff changeset
   123
    Returns (<other node>, <common ancestor node>) or None
ba6881c6a178 rewriteutil: check for divergence
Martin von Zweigbergk <martinvonz@google.com>
parents: 47019
diff changeset
   124
    """
ba6881c6a178 rewriteutil: check for divergence
Martin von Zweigbergk <martinvonz@google.com>
parents: 47019
diff changeset
   125
    if not ctx.obsolete():
ba6881c6a178 rewriteutil: check for divergence
Martin von Zweigbergk <martinvonz@google.com>
parents: 47019
diff changeset
   126
        return None
ba6881c6a178 rewriteutil: check for divergence
Martin von Zweigbergk <martinvonz@google.com>
parents: 47019
diff changeset
   127
    # We need to check two cases that can cause divergence:
ba6881c6a178 rewriteutil: check for divergence
Martin von Zweigbergk <martinvonz@google.com>
parents: 47019
diff changeset
   128
    # case 1: the rev being rewritten has a non-obsolete successor (easily
ba6881c6a178 rewriteutil: check for divergence
Martin von Zweigbergk <martinvonz@google.com>
parents: 47019
diff changeset
   129
    #     detected by successorssets)
ba6881c6a178 rewriteutil: check for divergence
Martin von Zweigbergk <martinvonz@google.com>
parents: 47019
diff changeset
   130
    sset = obsutil.successorssets(repo, ctx.node())
ba6881c6a178 rewriteutil: check for divergence
Martin von Zweigbergk <martinvonz@google.com>
parents: 47019
diff changeset
   131
    if sset:
ba6881c6a178 rewriteutil: check for divergence
Martin von Zweigbergk <martinvonz@google.com>
parents: 47019
diff changeset
   132
        return (sset[0][0], ctx.node())
ba6881c6a178 rewriteutil: check for divergence
Martin von Zweigbergk <martinvonz@google.com>
parents: 47019
diff changeset
   133
    else:
ba6881c6a178 rewriteutil: check for divergence
Martin von Zweigbergk <martinvonz@google.com>
parents: 47019
diff changeset
   134
        # case 2: one of the precursors of the rev being revived has a
ba6881c6a178 rewriteutil: check for divergence
Martin von Zweigbergk <martinvonz@google.com>
parents: 47019
diff changeset
   135
        #     non-obsolete successor (we need divergentsets for this)
ba6881c6a178 rewriteutil: check for divergence
Martin von Zweigbergk <martinvonz@google.com>
parents: 47019
diff changeset
   136
        divsets = obsutil.divergentsets(repo, ctx)
ba6881c6a178 rewriteutil: check for divergence
Martin von Zweigbergk <martinvonz@google.com>
parents: 47019
diff changeset
   137
        if divsets:
ba6881c6a178 rewriteutil: check for divergence
Martin von Zweigbergk <martinvonz@google.com>
parents: 47019
diff changeset
   138
            nsuccset = divsets[0][b'divergentnodes']
ba6881c6a178 rewriteutil: check for divergence
Martin von Zweigbergk <martinvonz@google.com>
parents: 47019
diff changeset
   139
            prec = divsets[0][b'commonpredecessor']
ba6881c6a178 rewriteutil: check for divergence
Martin von Zweigbergk <martinvonz@google.com>
parents: 47019
diff changeset
   140
            return (nsuccset[0], prec)
ba6881c6a178 rewriteutil: check for divergence
Martin von Zweigbergk <martinvonz@google.com>
parents: 47019
diff changeset
   141
        return None
ba6881c6a178 rewriteutil: check for divergence
Martin von Zweigbergk <martinvonz@google.com>
parents: 47019
diff changeset
   142
ba6881c6a178 rewriteutil: check for divergence
Martin von Zweigbergk <martinvonz@google.com>
parents: 47019
diff changeset
   143
45122
a391d0710f22 rewriteutil: add utility to check whether empty successors should be skipped
Manuel Jacob <me@manueljacob.de>
parents: 43077
diff changeset
   144
def skip_empty_successor(ui, command):
a391d0710f22 rewriteutil: add utility to check whether empty successors should be skipped
Manuel Jacob <me@manueljacob.de>
parents: 43077
diff changeset
   145
    empty_successor = ui.config(b'rewrite', b'empty-successor')
a391d0710f22 rewriteutil: add utility to check whether empty successors should be skipped
Manuel Jacob <me@manueljacob.de>
parents: 43077
diff changeset
   146
    if empty_successor == b'skip':
a391d0710f22 rewriteutil: add utility to check whether empty successors should be skipped
Manuel Jacob <me@manueljacob.de>
parents: 43077
diff changeset
   147
        return True
a391d0710f22 rewriteutil: add utility to check whether empty successors should be skipped
Manuel Jacob <me@manueljacob.de>
parents: 43077
diff changeset
   148
    elif empty_successor == b'keep':
a391d0710f22 rewriteutil: add utility to check whether empty successors should be skipped
Manuel Jacob <me@manueljacob.de>
parents: 43077
diff changeset
   149
        return False
a391d0710f22 rewriteutil: add utility to check whether empty successors should be skipped
Manuel Jacob <me@manueljacob.de>
parents: 43077
diff changeset
   150
    else:
a391d0710f22 rewriteutil: add utility to check whether empty successors should be skipped
Manuel Jacob <me@manueljacob.de>
parents: 43077
diff changeset
   151
        raise error.ConfigError(
a391d0710f22 rewriteutil: add utility to check whether empty successors should be skipped
Manuel Jacob <me@manueljacob.de>
parents: 43077
diff changeset
   152
            _(
a391d0710f22 rewriteutil: add utility to check whether empty successors should be skipped
Manuel Jacob <me@manueljacob.de>
parents: 43077
diff changeset
   153
                b"%s doesn't know how to handle config "
a391d0710f22 rewriteutil: add utility to check whether empty successors should be skipped
Manuel Jacob <me@manueljacob.de>
parents: 43077
diff changeset
   154
                b"rewrite.empty-successor=%s (only 'skip' and 'keep' are "
a391d0710f22 rewriteutil: add utility to check whether empty successors should be skipped
Manuel Jacob <me@manueljacob.de>
parents: 43077
diff changeset
   155
                b"supported)"
a391d0710f22 rewriteutil: add utility to check whether empty successors should be skipped
Manuel Jacob <me@manueljacob.de>
parents: 43077
diff changeset
   156
            )
a391d0710f22 rewriteutil: add utility to check whether empty successors should be skipped
Manuel Jacob <me@manueljacob.de>
parents: 43077
diff changeset
   157
            % (command, empty_successor)
a391d0710f22 rewriteutil: add utility to check whether empty successors should be skipped
Manuel Jacob <me@manueljacob.de>
parents: 43077
diff changeset
   158
        )
45424
0a57ef4b3bdb rewriteutil: extract evolve code used to replace obsolete hashes in commits
Matt Harbison <matt_harbison@yahoo.com>
parents: 45122
diff changeset
   159
0a57ef4b3bdb rewriteutil: extract evolve code used to replace obsolete hashes in commits
Matt Harbison <matt_harbison@yahoo.com>
parents: 45122
diff changeset
   160
45425
f7e293e0475f rewriteutil: also consider pending obsoletes when updating hashes in messages
Matt Harbison <matt_harbison@yahoo.com>
parents: 45424
diff changeset
   161
def update_hash_refs(repo, commitmsg, pending=None):
45424
0a57ef4b3bdb rewriteutil: extract evolve code used to replace obsolete hashes in commits
Matt Harbison <matt_harbison@yahoo.com>
parents: 45122
diff changeset
   162
    """Replace all obsolete commit hashes in the message with the current hash.
0a57ef4b3bdb rewriteutil: extract evolve code used to replace obsolete hashes in commits
Matt Harbison <matt_harbison@yahoo.com>
parents: 45122
diff changeset
   163
0a57ef4b3bdb rewriteutil: extract evolve code used to replace obsolete hashes in commits
Matt Harbison <matt_harbison@yahoo.com>
parents: 45122
diff changeset
   164
    If the obsolete commit was split or is divergent, the hash is not replaced
0a57ef4b3bdb rewriteutil: extract evolve code used to replace obsolete hashes in commits
Matt Harbison <matt_harbison@yahoo.com>
parents: 45122
diff changeset
   165
    as there's no way to know which successor to choose.
45425
f7e293e0475f rewriteutil: also consider pending obsoletes when updating hashes in messages
Matt Harbison <matt_harbison@yahoo.com>
parents: 45424
diff changeset
   166
f7e293e0475f rewriteutil: also consider pending obsoletes when updating hashes in messages
Matt Harbison <matt_harbison@yahoo.com>
parents: 45424
diff changeset
   167
    For commands that update a series of commits in the current transaction, the
f7e293e0475f rewriteutil: also consider pending obsoletes when updating hashes in messages
Matt Harbison <matt_harbison@yahoo.com>
parents: 45424
diff changeset
   168
    new obsolete markers can be considered by setting ``pending`` to a mapping
f7e293e0475f rewriteutil: also consider pending obsoletes when updating hashes in messages
Matt Harbison <matt_harbison@yahoo.com>
parents: 45424
diff changeset
   169
    of ``pending[oldnode] = [successor_node1, successor_node2,..]``.
45424
0a57ef4b3bdb rewriteutil: extract evolve code used to replace obsolete hashes in commits
Matt Harbison <matt_harbison@yahoo.com>
parents: 45122
diff changeset
   170
    """
45425
f7e293e0475f rewriteutil: also consider pending obsoletes when updating hashes in messages
Matt Harbison <matt_harbison@yahoo.com>
parents: 45424
diff changeset
   171
    if not pending:
f7e293e0475f rewriteutil: also consider pending obsoletes when updating hashes in messages
Matt Harbison <matt_harbison@yahoo.com>
parents: 45424
diff changeset
   172
        pending = {}
45424
0a57ef4b3bdb rewriteutil: extract evolve code used to replace obsolete hashes in commits
Matt Harbison <matt_harbison@yahoo.com>
parents: 45122
diff changeset
   173
    cache = {}
45427
78861610ded8 rewriteutil: relax the sha1 hash references to handle future hash types
Matt Harbison <matt_harbison@yahoo.com>
parents: 45425
diff changeset
   174
    hashes = re.findall(NODE_RE, commitmsg)
45424
0a57ef4b3bdb rewriteutil: extract evolve code used to replace obsolete hashes in commits
Matt Harbison <matt_harbison@yahoo.com>
parents: 45122
diff changeset
   175
    unfi = repo.unfiltered()
45427
78861610ded8 rewriteutil: relax the sha1 hash references to handle future hash types
Matt Harbison <matt_harbison@yahoo.com>
parents: 45425
diff changeset
   176
    for h in hashes:
78861610ded8 rewriteutil: relax the sha1 hash references to handle future hash types
Matt Harbison <matt_harbison@yahoo.com>
parents: 45425
diff changeset
   177
        fullnode = scmutil.resolvehexnodeidprefix(unfi, h)
45424
0a57ef4b3bdb rewriteutil: extract evolve code used to replace obsolete hashes in commits
Matt Harbison <matt_harbison@yahoo.com>
parents: 45122
diff changeset
   178
        if fullnode is None:
0a57ef4b3bdb rewriteutil: extract evolve code used to replace obsolete hashes in commits
Matt Harbison <matt_harbison@yahoo.com>
parents: 45122
diff changeset
   179
            continue
0a57ef4b3bdb rewriteutil: extract evolve code used to replace obsolete hashes in commits
Matt Harbison <matt_harbison@yahoo.com>
parents: 45122
diff changeset
   180
        ctx = unfi[fullnode]
0a57ef4b3bdb rewriteutil: extract evolve code used to replace obsolete hashes in commits
Matt Harbison <matt_harbison@yahoo.com>
parents: 45122
diff changeset
   181
        if not ctx.obsolete():
45425
f7e293e0475f rewriteutil: also consider pending obsoletes when updating hashes in messages
Matt Harbison <matt_harbison@yahoo.com>
parents: 45424
diff changeset
   182
            successors = pending.get(fullnode)
f7e293e0475f rewriteutil: also consider pending obsoletes when updating hashes in messages
Matt Harbison <matt_harbison@yahoo.com>
parents: 45424
diff changeset
   183
            if successors is None:
f7e293e0475f rewriteutil: also consider pending obsoletes when updating hashes in messages
Matt Harbison <matt_harbison@yahoo.com>
parents: 45424
diff changeset
   184
                continue
f7e293e0475f rewriteutil: also consider pending obsoletes when updating hashes in messages
Matt Harbison <matt_harbison@yahoo.com>
parents: 45424
diff changeset
   185
            # obsutil.successorssets() returns a list of list of nodes
f7e293e0475f rewriteutil: also consider pending obsoletes when updating hashes in messages
Matt Harbison <matt_harbison@yahoo.com>
parents: 45424
diff changeset
   186
            successors = [successors]
f7e293e0475f rewriteutil: also consider pending obsoletes when updating hashes in messages
Matt Harbison <matt_harbison@yahoo.com>
parents: 45424
diff changeset
   187
        else:
f7e293e0475f rewriteutil: also consider pending obsoletes when updating hashes in messages
Matt Harbison <matt_harbison@yahoo.com>
parents: 45424
diff changeset
   188
            successors = obsutil.successorssets(repo, ctx.node(), cache=cache)
45424
0a57ef4b3bdb rewriteutil: extract evolve code used to replace obsolete hashes in commits
Matt Harbison <matt_harbison@yahoo.com>
parents: 45122
diff changeset
   189
0a57ef4b3bdb rewriteutil: extract evolve code used to replace obsolete hashes in commits
Matt Harbison <matt_harbison@yahoo.com>
parents: 45122
diff changeset
   190
        # We can't make any assumptions about how to update the hash if the
0a57ef4b3bdb rewriteutil: extract evolve code used to replace obsolete hashes in commits
Matt Harbison <matt_harbison@yahoo.com>
parents: 45122
diff changeset
   191
        # cset in question was split or diverged.
0a57ef4b3bdb rewriteutil: extract evolve code used to replace obsolete hashes in commits
Matt Harbison <matt_harbison@yahoo.com>
parents: 45122
diff changeset
   192
        if len(successors) == 1 and len(successors[0]) == 1:
45711
3d68b47e461b rewriteutil: handle dropped commits when updating description hashes
Matt Harbison <matt_harbison@yahoo.com>
parents: 45427
diff changeset
   193
            successor = successors[0][0]
3d68b47e461b rewriteutil: handle dropped commits when updating description hashes
Matt Harbison <matt_harbison@yahoo.com>
parents: 45427
diff changeset
   194
            if successor is not None:
46113
59fa3890d40a node: import symbols explicitly
Joerg Sonnenberger <joerg@bec.de>
parents: 45853
diff changeset
   195
                newhash = hex(successor)
45711
3d68b47e461b rewriteutil: handle dropped commits when updating description hashes
Matt Harbison <matt_harbison@yahoo.com>
parents: 45427
diff changeset
   196
                commitmsg = commitmsg.replace(h, newhash[: len(h)])
3d68b47e461b rewriteutil: handle dropped commits when updating description hashes
Matt Harbison <matt_harbison@yahoo.com>
parents: 45427
diff changeset
   197
            else:
3d68b47e461b rewriteutil: handle dropped commits when updating description hashes
Matt Harbison <matt_harbison@yahoo.com>
parents: 45427
diff changeset
   198
                repo.ui.note(
3d68b47e461b rewriteutil: handle dropped commits when updating description hashes
Matt Harbison <matt_harbison@yahoo.com>
parents: 45427
diff changeset
   199
                    _(
3d68b47e461b rewriteutil: handle dropped commits when updating description hashes
Matt Harbison <matt_harbison@yahoo.com>
parents: 45427
diff changeset
   200
                        b'The stale commit message reference to %s could '
3d68b47e461b rewriteutil: handle dropped commits when updating description hashes
Matt Harbison <matt_harbison@yahoo.com>
parents: 45427
diff changeset
   201
                        b'not be updated\n(The referenced commit was dropped)\n'
3d68b47e461b rewriteutil: handle dropped commits when updating description hashes
Matt Harbison <matt_harbison@yahoo.com>
parents: 45427
diff changeset
   202
                    )
3d68b47e461b rewriteutil: handle dropped commits when updating description hashes
Matt Harbison <matt_harbison@yahoo.com>
parents: 45427
diff changeset
   203
                    % h
3d68b47e461b rewriteutil: handle dropped commits when updating description hashes
Matt Harbison <matt_harbison@yahoo.com>
parents: 45427
diff changeset
   204
                )
45424
0a57ef4b3bdb rewriteutil: extract evolve code used to replace obsolete hashes in commits
Matt Harbison <matt_harbison@yahoo.com>
parents: 45122
diff changeset
   205
        else:
0a57ef4b3bdb rewriteutil: extract evolve code used to replace obsolete hashes in commits
Matt Harbison <matt_harbison@yahoo.com>
parents: 45122
diff changeset
   206
            repo.ui.note(
0a57ef4b3bdb rewriteutil: extract evolve code used to replace obsolete hashes in commits
Matt Harbison <matt_harbison@yahoo.com>
parents: 45122
diff changeset
   207
                _(
0a57ef4b3bdb rewriteutil: extract evolve code used to replace obsolete hashes in commits
Matt Harbison <matt_harbison@yahoo.com>
parents: 45122
diff changeset
   208
                    b'The stale commit message reference to %s could '
0a57ef4b3bdb rewriteutil: extract evolve code used to replace obsolete hashes in commits
Matt Harbison <matt_harbison@yahoo.com>
parents: 45122
diff changeset
   209
                    b'not be updated\n'
0a57ef4b3bdb rewriteutil: extract evolve code used to replace obsolete hashes in commits
Matt Harbison <matt_harbison@yahoo.com>
parents: 45122
diff changeset
   210
                )
45427
78861610ded8 rewriteutil: relax the sha1 hash references to handle future hash types
Matt Harbison <matt_harbison@yahoo.com>
parents: 45425
diff changeset
   211
                % h
45424
0a57ef4b3bdb rewriteutil: extract evolve code used to replace obsolete hashes in commits
Matt Harbison <matt_harbison@yahoo.com>
parents: 45122
diff changeset
   212
            )
0a57ef4b3bdb rewriteutil: extract evolve code used to replace obsolete hashes in commits
Matt Harbison <matt_harbison@yahoo.com>
parents: 45122
diff changeset
   213
0a57ef4b3bdb rewriteutil: extract evolve code used to replace obsolete hashes in commits
Matt Harbison <matt_harbison@yahoo.com>
parents: 45122
diff changeset
   214
    return commitmsg