mercurial/cmd_impls/graft.py
author Matt Harbison <matt_harbison@yahoo.com>
Fri, 29 Nov 2024 19:43:39 -0500
changeset 52351 9042ffea4edd
parent 52350 d4e30c15626d
child 52503 cef86c1d5dfd
permissions -rw-r--r--
typing: add minimal annotations to cmd_impls/graft.py to pytype with 3.10 I'm not sure why the same version of pytype passed in CI with Python 3.11. What's failing on 3.10 is related to `statedata`, which is keyed on bytes, but has various value types. It looks like these several types are treated as a union when run with 3.10, and then all of them need to have the same attributes. This will take awhile to untangle, because `TypedDict` requires str keys, so we'll either have to change the keys (and whoever calls this), or migrate to a class with typed fields (and change all of the callers). There are some changes to this module currently in-flight, so I'm opting for the minimal changes here to minimally affect that, while keeping my ability to run pytype locally and track the changes. It's worth pointing out that I'm starting to use py3.9 type hints here, i.e. `Foo | None` instead of `Optional[Foo]`. That's fine even with py3.8 support because of the `from __future__ import annotations`, which delays evaluation. We already don't support pytype checking with all of the runtime supported versions of Python since at least 0851d94bfdaa, with the `ByteString` usage. The errors at the start of this series were: File "/mnt/c/Users/Matt/hg/mercurial/cmd_impls/graft.py", line 238, in _graft_revisions: No attribute 'get' on bool [attribute-error] In Union[Any, Callable, Dict[bytes, Optional[Any]], bool, bytes, dict] Called from (traceback): line 21, in cmd_graft File "/mnt/c/Users/Matt/hg/mercurial/cmd_impls/graft.py", line 238, in _graft_revisions: No attribute 'get' on bytes [attribute-error] In Union[Any, Callable, Dict[bytes, Optional[Any]], bool, bytes, dict] Called from (traceback): line 21, in cmd_graft File "/mnt/c/Users/Matt/hg/mercurial/cmd_impls/graft.py", line 239, in _graft_revisions: No attribute 'get' on bool [attribute-error] In Union[Any, Callable, Dict[bytes, Optional[Any]], bool, bytes, dict] Called from (traceback): line 21, in cmd_graft File "/mnt/c/Users/Matt/hg/mercurial/cmd_impls/graft.py", line 239, in _graft_revisions: No attribute 'get' on bytes [attribute-error] In Union[Any, Callable, Dict[bytes, Optional[Any]], bool, bytes, dict] Called from (traceback): line 21, in cmd_graft File "/mnt/c/Users/Matt/hg/mercurial/cmd_impls/graft.py", line 241, in _graft_revisions: No attribute 'get' on bool [attribute-error] In Union[Any, Callable, Dict[bytes, Optional[Any]], bool, bytes, dict] Called from (traceback): line 21, in cmd_graft File "/mnt/c/Users/Matt/hg/mercurial/cmd_impls/graft.py", line 241, in _graft_revisions: No attribute 'get' on bytes [attribute-error] In Union[Any, Callable, Dict[bytes, Optional[Any]], bool, bytes, dict] Called from (traceback): line 21, in cmd_graft File "/mnt/c/Users/Matt/hg/mercurial/cmd_impls/graft.py", line 260, in _graft_revisions: unsupported operand type(s) for item assignment: bool [unsupported-operands] No attribute '__setitem__' on bool Called from (traceback): line 21, in cmd_graft File "/mnt/c/Users/Matt/hg/mercurial/cmd_impls/graft.py", line 260, in _graft_revisions: unsupported operand type(s) for item assignment: bytes [unsupported-operands] No attribute '__setitem__' on bytes Called from (traceback): line 21, in cmd_graft File "/mnt/c/Users/Matt/hg/mercurial/cmd_impls/graft.py", line 270, in _graft_revisions: No attribute 'get' on bool [attribute-error] In Union[Any, Callable, Dict[bytes, Optional[Any]], bool, bytes, dict] Called from (traceback): line 21, in cmd_graft File "/mnt/c/Users/Matt/hg/mercurial/cmd_impls/graft.py", line 270, in _graft_revisions: No attribute 'get' on bytes [attribute-error] In Union[Any, Callable, Dict[bytes, Optional[Any]], bool, bytes, dict] Called from (traceback): line 21, in cmd_graft File "/mnt/c/Users/Matt/hg/mercurial/cmd_impls/graft.py", line 280, in _graft_revisions: No attribute 'get' on bool [attribute-error] In Union[Any, Callable, Dict[bytes, Optional[Any]], bool, bytes, dict] Called from (traceback): line 21, in cmd_graft File "/mnt/c/Users/Matt/hg/mercurial/cmd_impls/graft.py", line 280, in _graft_revisions: No attribute 'get' on bytes [attribute-error] In Union[Any, Callable, Dict[bytes, Optional[Any]], bool, bytes, dict] Called from (traceback): line 21, in cmd_graft
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
52328
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
     1
# graft.py - implementation of the graft command
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
     2
52350
d4e30c15626d typing: add missing `from __future__ import annotations` to core modules
Matt Harbison <matt_harbison@yahoo.com>
parents: 52348
diff changeset
     3
from __future__ import annotations
d4e30c15626d typing: add missing `from __future__ import annotations` to core modules
Matt Harbison <matt_harbison@yahoo.com>
parents: 52348
diff changeset
     4
52351
9042ffea4edd typing: add minimal annotations to cmd_impls/graft.py to pytype with 3.10
Matt Harbison <matt_harbison@yahoo.com>
parents: 52350
diff changeset
     5
import typing
9042ffea4edd typing: add minimal annotations to cmd_impls/graft.py to pytype with 3.10
Matt Harbison <matt_harbison@yahoo.com>
parents: 52350
diff changeset
     6
9042ffea4edd typing: add minimal annotations to cmd_impls/graft.py to pytype with 3.10
Matt Harbison <matt_harbison@yahoo.com>
parents: 52350
diff changeset
     7
from typing import (
9042ffea4edd typing: add minimal annotations to cmd_impls/graft.py to pytype with 3.10
Matt Harbison <matt_harbison@yahoo.com>
parents: 52350
diff changeset
     8
    Any,
9042ffea4edd typing: add minimal annotations to cmd_impls/graft.py to pytype with 3.10
Matt Harbison <matt_harbison@yahoo.com>
parents: 52350
diff changeset
     9
    Tuple,
9042ffea4edd typing: add minimal annotations to cmd_impls/graft.py to pytype with 3.10
Matt Harbison <matt_harbison@yahoo.com>
parents: 52350
diff changeset
    10
)
9042ffea4edd typing: add minimal annotations to cmd_impls/graft.py to pytype with 3.10
Matt Harbison <matt_harbison@yahoo.com>
parents: 52350
diff changeset
    11
52328
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    12
from ..i18n import _
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    13
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    14
from .. import cmdutil, error, logcmdutil, merge as mergemod, state as statemod
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    15
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    16
52351
9042ffea4edd typing: add minimal annotations to cmd_impls/graft.py to pytype with 3.10
Matt Harbison <matt_harbison@yahoo.com>
parents: 52350
diff changeset
    17
if typing.TYPE_CHECKING:
9042ffea4edd typing: add minimal annotations to cmd_impls/graft.py to pytype with 3.10
Matt Harbison <matt_harbison@yahoo.com>
parents: 52350
diff changeset
    18
    _ActionT = str
9042ffea4edd typing: add minimal annotations to cmd_impls/graft.py to pytype with 3.10
Matt Harbison <matt_harbison@yahoo.com>
parents: 52350
diff changeset
    19
    _CmdArgsT = Any  # TODO: (statedata, revs, editor, cont, dry_run, tool)
9042ffea4edd typing: add minimal annotations to cmd_impls/graft.py to pytype with 3.10
Matt Harbison <matt_harbison@yahoo.com>
parents: 52350
diff changeset
    20
9042ffea4edd typing: add minimal annotations to cmd_impls/graft.py to pytype with 3.10
Matt Harbison <matt_harbison@yahoo.com>
parents: 52350
diff changeset
    21
9042ffea4edd typing: add minimal annotations to cmd_impls/graft.py to pytype with 3.10
Matt Harbison <matt_harbison@yahoo.com>
parents: 52350
diff changeset
    22
def cmd_graft(ui, repo, *revs, **opts) -> int:
52347
0facc743b92f graft: fix a few typos in doc comments
Matt Harbison <matt_harbison@yahoo.com>
parents: 52346
diff changeset
    23
    """implement the graft command as defined in mercurial/commands.py"""
52329
5ab77b93567c graft: split the argument processing from the grafting
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52328
diff changeset
    24
    ret = _process_args(ui, repo, *revs, **opts)
52334
77cb5d8643b3 graft: clarify the args passing depending of variants
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52333
diff changeset
    25
    action, graftstate, args = ret
77cb5d8643b3 graft: clarify the args passing depending of variants
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52333
diff changeset
    26
    if action == "ERROR":
52329
5ab77b93567c graft: split the argument processing from the grafting
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52328
diff changeset
    27
        return -1
52334
77cb5d8643b3 graft: clarify the args passing depending of variants
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52333
diff changeset
    28
    elif action == "ABORT":
77cb5d8643b3 graft: clarify the args passing depending of variants
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52333
diff changeset
    29
        assert args is None
77cb5d8643b3 graft: clarify the args passing depending of variants
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52333
diff changeset
    30
        return cmdutil.abortgraft(ui, repo, graftstate)
52329
5ab77b93567c graft: split the argument processing from the grafting
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52328
diff changeset
    31
    elif action == "STOP":
52334
77cb5d8643b3 graft: clarify the args passing depending of variants
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52333
diff changeset
    32
        assert args is None
77cb5d8643b3 graft: clarify the args passing depending of variants
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52333
diff changeset
    33
        return _stopgraft(ui, repo, graftstate)
52329
5ab77b93567c graft: split the argument processing from the grafting
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52328
diff changeset
    34
    elif action == "GRAFT":
52334
77cb5d8643b3 graft: clarify the args passing depending of variants
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52333
diff changeset
    35
        return _graft_revisions(ui, repo, graftstate, *args)
52329
5ab77b93567c graft: split the argument processing from the grafting
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52328
diff changeset
    36
    else:
52346
1802d9afddf7 graft: fix interpolation in a ProgrammingError case
Matt Harbison <matt_harbison@yahoo.com>
parents: 52337
diff changeset
    37
        raise error.ProgrammingError('unknown action: %s' % action)
52329
5ab77b93567c graft: split the argument processing from the grafting
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52328
diff changeset
    38
5ab77b93567c graft: split the argument processing from the grafting
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52328
diff changeset
    39
52351
9042ffea4edd typing: add minimal annotations to cmd_impls/graft.py to pytype with 3.10
Matt Harbison <matt_harbison@yahoo.com>
parents: 52350
diff changeset
    40
def _process_args(
9042ffea4edd typing: add minimal annotations to cmd_impls/graft.py to pytype with 3.10
Matt Harbison <matt_harbison@yahoo.com>
parents: 52350
diff changeset
    41
    ui, repo, *revs, **opts
9042ffea4edd typing: add minimal annotations to cmd_impls/graft.py to pytype with 3.10
Matt Harbison <matt_harbison@yahoo.com>
parents: 52350
diff changeset
    42
) -> Tuple[_ActionT, statemod.cmdstate | None, _CmdArgsT | None]:
52329
5ab77b93567c graft: split the argument processing from the grafting
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52328
diff changeset
    43
    """process the graft command argument to figure out what to do
5ab77b93567c graft: split the argument processing from the grafting
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52328
diff changeset
    44
5ab77b93567c graft: split the argument processing from the grafting
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52328
diff changeset
    45
    This also filter the selected revision to skip the one that cannot be graft
52347
0facc743b92f graft: fix a few typos in doc comments
Matt Harbison <matt_harbison@yahoo.com>
parents: 52346
diff changeset
    46
    or were already grafted.
52329
5ab77b93567c graft: split the argument processing from the grafting
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52328
diff changeset
    47
    """
52328
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    48
    if revs and opts.get('rev'):
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    49
        ui.warn(
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    50
            _(
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    51
                b'warning: inconsistent use of --rev might give unexpected '
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    52
                b'revision ordering!\n'
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    53
            )
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    54
        )
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    55
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    56
    revs = list(revs)
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    57
    revs.extend(opts.get('rev'))
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    58
    # a dict of data to be stored in state file
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    59
    statedata = {}
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    60
    # list of new nodes created by ongoing graft
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    61
    statedata[b'newnodes'] = []
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    62
52336
8faabe8abf66 graft: gather arg compatibility code
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52335
diff changeset
    63
    # argument incompatible with followup from an interrupted operation
8faabe8abf66 graft: gather arg compatibility code
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52335
diff changeset
    64
    commit_args = ['edit', 'log', 'user', 'date', 'currentdate', 'currentuser']
52337
58827d815646 graft: also forbid "--base" with "--stop" and the like
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52336
diff changeset
    65
    nofollow_args = commit_args + ['base', 'rev']
52328
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    66
52336
8faabe8abf66 graft: gather arg compatibility code
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52335
diff changeset
    67
    arg_compatibilities = [
8faabe8abf66 graft: gather arg compatibility code
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52335
diff changeset
    68
        ('no_commit', commit_args),
8faabe8abf66 graft: gather arg compatibility code
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52335
diff changeset
    69
        ('stop', nofollow_args),
8faabe8abf66 graft: gather arg compatibility code
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52335
diff changeset
    70
        ('abort', nofollow_args),
8faabe8abf66 graft: gather arg compatibility code
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52335
diff changeset
    71
    ]
52328
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    72
    cmdutil.check_at_most_one_arg(opts, 'abort', 'stop', 'continue')
52336
8faabe8abf66 graft: gather arg compatibility code
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52335
diff changeset
    73
    for arg, incompatible in arg_compatibilities:
8faabe8abf66 graft: gather arg compatibility code
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52335
diff changeset
    74
        cmdutil.check_incompatible_arguments(opts, arg, incompatible)
8faabe8abf66 graft: gather arg compatibility code
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52335
diff changeset
    75
8faabe8abf66 graft: gather arg compatibility code
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52335
diff changeset
    76
    cmdutil.resolve_commit_options(ui, opts)
52328
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    77
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    78
    cont = False
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    79
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    80
    graftstate = statemod.cmdstate(repo, b'graftstate')
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    81
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    82
    if opts.get('stop'):
52334
77cb5d8643b3 graft: clarify the args passing depending of variants
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52333
diff changeset
    83
        return "STOP", graftstate, None
52328
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    84
    elif opts.get('abort'):
52334
77cb5d8643b3 graft: clarify the args passing depending of variants
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52333
diff changeset
    85
        return "ABORT", graftstate, None
52328
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    86
    elif opts.get('continue'):
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    87
        cont = True
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    88
        if revs:
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    89
            raise error.InputError(_(b"can't specify --continue and revisions"))
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    90
        # read in unfinished revisions
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    91
        if graftstate.exists():
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    92
            statedata = cmdutil.readgraftstate(repo, graftstate)
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    93
            if statedata.get(b'no_commit'):
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    94
                opts['no_commit'] = statedata.get(b'no_commit')
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    95
            if statedata.get(b'base'):
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    96
                opts['base'] = statedata.get(b'base')
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    97
            nodes = statedata[b'nodes']
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    98
            revs = [repo[node].rev() for node in nodes]
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    99
        else:
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   100
            cmdutil.wrongtooltocontinue(repo, _(b'graft'))
52329
5ab77b93567c graft: split the argument processing from the grafting
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52328
diff changeset
   101
    elif not revs:
5ab77b93567c graft: split the argument processing from the grafting
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52328
diff changeset
   102
        raise error.InputError(_(b'no revisions specified'))
52328
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   103
    else:
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   104
        cmdutil.checkunfinished(repo)
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   105
        cmdutil.bailifchanged(repo)
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   106
        revs = logcmdutil.revrange(repo, revs)
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   107
52330
8572e80f978c graft: assign computed configuration value to statedata instead of opts
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52329
diff changeset
   108
    for o in (
8572e80f978c graft: assign computed configuration value to statedata instead of opts
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52329
diff changeset
   109
        b'date',
8572e80f978c graft: assign computed configuration value to statedata instead of opts
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52329
diff changeset
   110
        b'user',
8572e80f978c graft: assign computed configuration value to statedata instead of opts
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52329
diff changeset
   111
        b'log',
52331
11fb7f737456 graft: move no_commit into "statedata" too
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52330
diff changeset
   112
        b'no_commit',
52332
8d7029218a61 graft: move "dry_run" and "base" in statedate
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52331
diff changeset
   113
        b'dry_run',
52330
8572e80f978c graft: assign computed configuration value to statedata instead of opts
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52329
diff changeset
   114
    ):
8572e80f978c graft: assign computed configuration value to statedata instead of opts
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52329
diff changeset
   115
        v = opts.get(o.decode('ascii'))
8572e80f978c graft: assign computed configuration value to statedata instead of opts
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52329
diff changeset
   116
        # if statedata is already set, it comes from --continue and test says
8572e80f978c graft: assign computed configuration value to statedata instead of opts
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52329
diff changeset
   117
        # we should honor them above the options (which seems weird).
8572e80f978c graft: assign computed configuration value to statedata instead of opts
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52329
diff changeset
   118
        if v and o not in statedata:
8572e80f978c graft: assign computed configuration value to statedata instead of opts
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52329
diff changeset
   119
            statedata[o] = v
8572e80f978c graft: assign computed configuration value to statedata instead of opts
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52329
diff changeset
   120
52328
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   121
    skipped = set()
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   122
    basectx = None
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   123
    if opts.get('base'):
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   124
        basectx = logcmdutil.revsingle(repo, opts['base'], None)
52332
8d7029218a61 graft: move "dry_run" and "base" in statedate
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52331
diff changeset
   125
        statedata[b'base'] = basectx.hex()
52328
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   126
    if basectx is None:
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   127
        # check for merges
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   128
        for rev in repo.revs(b'%ld and merge()', revs):
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   129
            ui.warn(_(b'skipping ungraftable merge revision %d\n') % rev)
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   130
            skipped.add(rev)
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   131
    revs = [r for r in revs if r not in skipped]
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   132
    if not revs:
52334
77cb5d8643b3 graft: clarify the args passing depending of variants
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52333
diff changeset
   133
        return "ERROR", None, None
52328
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   134
    if basectx is not None and len(revs) != 1:
52348
99615755fb8e graft: trim an InputError message
Matt Harbison <matt_harbison@yahoo.com>
parents: 52347
diff changeset
   135
        raise error.InputError(_(b'only one revision allowed with --base'))
52328
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   136
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   137
    # Don't check in the --continue case, in effect retaining --force across
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   138
    # --continues. That's because without --force, any revisions we decided to
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   139
    # skip would have been filtered out here, so they wouldn't have made their
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   140
    # way to the graftstate. With --force, any revisions we would have otherwise
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   141
    # skipped would not have been filtered out, and if they hadn't been applied
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   142
    # already, they'd have been in the graftstate.
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   143
    if not (cont or opts.get('force')) and basectx is None:
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   144
        # check for ancestors of dest branch
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   145
        ancestors = repo.revs(b'%ld & (::.)', revs)
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   146
        for rev in ancestors:
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   147
            ui.warn(_(b'skipping ancestor revision %d:%s\n') % (rev, repo[rev]))
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   148
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   149
        revs = [r for r in revs if r not in ancestors]
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   150
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   151
        if not revs:
52334
77cb5d8643b3 graft: clarify the args passing depending of variants
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52333
diff changeset
   152
            return "ERROR", None, None
52328
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   153
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   154
        # analyze revs for earlier grafts
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   155
        ids = {}
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   156
        for ctx in repo.set(b"%ld", revs):
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   157
            ids[ctx.hex()] = ctx.rev()
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   158
            n = ctx.extra().get(b'source')
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   159
            if n:
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   160
                ids[n] = ctx.rev()
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   161
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   162
        # check ancestors for earlier grafts
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   163
        ui.debug(b'scanning for duplicate grafts\n')
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   164
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   165
        # The only changesets we can be sure doesn't contain grafts of any
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   166
        # revs, are the ones that are common ancestors of *all* revs:
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   167
        for rev in repo.revs(b'only(%d,ancestor(%ld))', repo[b'.'].rev(), revs):
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   168
            ctx = repo[rev]
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   169
            n = ctx.extra().get(b'source')
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   170
            if n in ids:
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   171
                try:
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   172
                    r = repo[n].rev()
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   173
                except error.RepoLookupError:
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   174
                    r = None
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   175
                if r in revs:
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   176
                    ui.warn(
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   177
                        _(
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   178
                            b'skipping revision %d:%s '
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   179
                            b'(already grafted to %d:%s)\n'
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   180
                        )
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   181
                        % (r, repo[r], rev, ctx)
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   182
                    )
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   183
                    revs.remove(r)
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   184
                elif ids[n] in revs:
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   185
                    if r is None:
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   186
                        ui.warn(
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   187
                            _(
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   188
                                b'skipping already grafted revision %d:%s '
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   189
                                b'(%d:%s also has unknown origin %s)\n'
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   190
                            )
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   191
                            % (ids[n], repo[ids[n]], rev, ctx, n[:12])
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   192
                        )
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   193
                    else:
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   194
                        ui.warn(
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   195
                            _(
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   196
                                b'skipping already grafted revision %d:%s '
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   197
                                b'(%d:%s also has origin %d:%s)\n'
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   198
                            )
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   199
                            % (ids[n], repo[ids[n]], rev, ctx, r, n[:12])
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   200
                        )
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   201
                    revs.remove(ids[n])
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   202
            elif ctx.hex() in ids:
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   203
                r = ids[ctx.hex()]
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   204
                if r in revs:
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   205
                    ui.warn(
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   206
                        _(
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   207
                            b'skipping already grafted revision %d:%s '
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   208
                            b'(was grafted from %d:%s)\n'
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   209
                        )
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   210
                        % (r, repo[r], rev, ctx)
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   211
                    )
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   212
                    revs.remove(r)
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   213
        if not revs:
52334
77cb5d8643b3 graft: clarify the args passing depending of variants
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52333
diff changeset
   214
            return "ERROR", None, None
52328
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   215
52335
0b52283d50bb graft: get the editor later
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52334
diff changeset
   216
    editor = cmdutil.getcommiteditor(editform=b'graft', **opts)
52332
8d7029218a61 graft: move "dry_run" and "base" in statedate
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52331
diff changeset
   217
    dry_run = bool(opts.get("dry_run"))
52333
da216ed31c3d graft: explicitly pass the "tool" argument
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52332
diff changeset
   218
    tool = opts.get('tool', b'')
52334
77cb5d8643b3 graft: clarify the args passing depending of variants
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52333
diff changeset
   219
    return "GRAFT", graftstate, (statedata, revs, editor, cont, dry_run, tool)
52329
5ab77b93567c graft: split the argument processing from the grafting
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52328
diff changeset
   220
5ab77b93567c graft: split the argument processing from the grafting
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52328
diff changeset
   221
5ab77b93567c graft: split the argument processing from the grafting
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52328
diff changeset
   222
def _graft_revisions(
5ab77b93567c graft: split the argument processing from the grafting
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52328
diff changeset
   223
    ui,
5ab77b93567c graft: split the argument processing from the grafting
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52328
diff changeset
   224
    repo,
5ab77b93567c graft: split the argument processing from the grafting
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52328
diff changeset
   225
    graftstate,
5ab77b93567c graft: split the argument processing from the grafting
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52328
diff changeset
   226
    statedata,
5ab77b93567c graft: split the argument processing from the grafting
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52328
diff changeset
   227
    revs,
5ab77b93567c graft: split the argument processing from the grafting
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52328
diff changeset
   228
    editor,
5ab77b93567c graft: split the argument processing from the grafting
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52328
diff changeset
   229
    cont=False,
52332
8d7029218a61 graft: move "dry_run" and "base" in statedate
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52331
diff changeset
   230
    dry_run=False,
52333
da216ed31c3d graft: explicitly pass the "tool" argument
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52332
diff changeset
   231
    tool=b'',
52329
5ab77b93567c graft: split the argument processing from the grafting
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52328
diff changeset
   232
):
5ab77b93567c graft: split the argument processing from the grafting
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52328
diff changeset
   233
    """actually graft some revisions"""
52328
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   234
    for pos, ctx in enumerate(repo.set(b"%ld", revs)):
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   235
        desc = b'%d:%s "%s"' % (
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   236
            ctx.rev(),
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   237
            ctx,
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   238
            ctx.description().split(b'\n', 1)[0],
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   239
        )
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   240
        names = repo.nodetags(ctx.node()) + repo.nodebookmarks(ctx.node())
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   241
        if names:
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   242
            desc += b' (%s)' % b' '.join(names)
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   243
        ui.status(_(b'grafting %s\n') % desc)
52332
8d7029218a61 graft: move "dry_run" and "base" in statedate
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52331
diff changeset
   244
        if dry_run:
52328
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   245
            continue
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   246
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   247
        source = ctx.extra().get(b'source')
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   248
        extra = {}
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   249
        if source:
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   250
            extra[b'source'] = source
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   251
            extra[b'intermediate-source'] = ctx.hex()
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   252
        else:
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   253
            extra[b'source'] = ctx.hex()
52330
8572e80f978c graft: assign computed configuration value to statedata instead of opts
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52329
diff changeset
   254
        user = statedata.get(b'user', ctx.user())
8572e80f978c graft: assign computed configuration value to statedata instead of opts
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52329
diff changeset
   255
        date = statedata.get(b'date', ctx.date())
52328
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   256
        message = ctx.description()
52330
8572e80f978c graft: assign computed configuration value to statedata instead of opts
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52329
diff changeset
   257
        if statedata.get(b'log'):
52328
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   258
            message += b'\n(grafted from %s)' % ctx.hex()
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   259
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   260
        # we don't merge the first commit when continuing
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   261
        if not cont:
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   262
            # perform the graft merge with p1(rev) as 'ancestor'
52333
da216ed31c3d graft: explicitly pass the "tool" argument
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52332
diff changeset
   263
            overrides = {(b'ui', b'forcemerge'): tool}
52332
8d7029218a61 graft: move "dry_run" and "base" in statedate
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52331
diff changeset
   264
            if b'base' in statedata:
8d7029218a61 graft: move "dry_run" and "base" in statedate
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52331
diff changeset
   265
                base = repo[statedata[b'base']]
8d7029218a61 graft: move "dry_run" and "base" in statedate
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52331
diff changeset
   266
            else:
8d7029218a61 graft: move "dry_run" and "base" in statedate
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52331
diff changeset
   267
                base = ctx.p1()
52328
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   268
            with ui.configoverride(overrides, b'graft'):
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   269
                stats = mergemod.graft(
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   270
                    repo, ctx, base, [b'local', b'graft', b'parent of graft']
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   271
                )
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   272
            # report any conflicts
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   273
            if stats.unresolvedcount > 0:
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   274
                # write out state for --continue
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   275
                nodes = [repo[rev].hex() for rev in revs[pos:]]
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   276
                statedata[b'nodes'] = nodes
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   277
                stateversion = 1
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   278
                graftstate.save(stateversion, statedata)
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   279
                ui.error(_(b"abort: unresolved conflicts, can't continue\n"))
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   280
                ui.error(_(b"(use 'hg resolve' and 'hg graft --continue')\n"))
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   281
                return 1
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   282
        else:
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   283
            cont = False
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   284
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   285
        # commit if --no-commit is false
52331
11fb7f737456 graft: move no_commit into "statedata" too
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52330
diff changeset
   286
        if not statedata.get(b'no_commit'):
52328
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   287
            node = repo.commit(
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   288
                text=message, user=user, date=date, extra=extra, editor=editor
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   289
            )
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   290
            if node is None:
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   291
                ui.warn(
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   292
                    _(b'note: graft of %d:%s created no changes to commit\n')
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   293
                    % (ctx.rev(), ctx)
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   294
                )
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   295
            # checking that newnodes exist because old state files won't have it
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   296
            elif statedata.get(b'newnodes') is not None:
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   297
                nn = statedata[b'newnodes']
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   298
                assert isinstance(nn, list)  # list of bytes
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   299
                nn.append(node)
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   300
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   301
    # remove state when we complete successfully
52332
8d7029218a61 graft: move "dry_run" and "base" in statedate
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 52331
diff changeset
   302
    if not dry_run:
52328
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   303
        graftstate.delete()
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   304
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   305
    return 0
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   306
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   307
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   308
def _stopgraft(ui, repo, graftstate):
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   309
    """stop the interrupted graft"""
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   310
    if not graftstate.exists():
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   311
        raise error.StateError(_(b"no interrupted graft found"))
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   312
    pctx = repo[b'.']
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   313
    mergemod.clean_update(pctx)
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   314
    graftstate.delete()
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   315
    ui.status(_(b"stopped the interrupted graft\n"))
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   316
    ui.status(_(b"working directory is now at %s\n") % pctx.hex()[:12])
f2fc0a91faca commands: create a "mercurial.cmd_impls" module to host graft
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   317
    return 0