hgext3rd/evolve/cmdrewrite.py
author Pierre-Yves David <pierre-yves.david@octobus.net>
Fri, 03 Apr 2020 19:10:29 +0200
changeset 5262 678a9223aad3
parent 5193 a4d081923c81
parent 5213 d5544ebf5836
permissions -rw-r--r--
branching: merge stable in default
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2772
394b836e475b commands: rewrite the 'evocommands' module to 'cmdrewrite'
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2771
diff changeset
     1
# Module dedicated to host history rewriting commands
2724
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
     2
#
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
     3
# Copyright 2017 Octobus <contact@octobus.net>
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
     4
#
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
     5
# This software may be used and distributed according to the terms of the
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
     6
# GNU General Public License version 2 or any later version.
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
     7
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
     8
# Status: Stabilization of the API in progress
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
     9
#
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    10
#   The final set of command should go into core.
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    11
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    12
from __future__ import absolute_import
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    13
2763
4a5b0c373e65 commands: move the touch to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2762
diff changeset
    14
import random
4a5b0c373e65 commands: move the touch to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2762
diff changeset
    15
2724
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    16
from mercurial import (
2762
610581a2fb74 commands: move split to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2761
diff changeset
    17
    bookmarks as bookmarksmod,
2724
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    18
    cmdutil,
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    19
    commands,
2725
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
    20
    context,
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
    21
    copies,
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
    22
    error,
2760
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
    23
    hg,
2725
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
    24
    lock as lockmod,
5181
c979d64a2589 compat: drop 4.5 compatibility layer for log utility
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 5179
diff changeset
    25
    logcmdutil,
3453
32ed5b6fadd3 grab: add a command to grab a commit and update to it
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3389
diff changeset
    26
    merge,
2725
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
    27
    node,
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
    28
    obsolete,
2941
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
    29
    patch,
2725
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
    30
    phases,
4752
8a73a8df63b6 py3: convert opts keys to bytes before passing to core APIs
Martin von Zweigbergk <martinvonz@google.com>
parents: 4747
diff changeset
    31
    pycompat,
2725
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
    32
    scmutil,
2724
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    33
    util,
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    34
)
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    35
5178
61e49d2654cc compat: directly use function from dateutil
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 5136
diff changeset
    36
from mercurial.utils import dateutil
61e49d2654cc compat: directly use function from dateutil
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 5136
diff changeset
    37
2724
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    38
from mercurial.i18n import _
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    39
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    40
from . import (
2763
4a5b0c373e65 commands: move the touch to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2762
diff changeset
    41
    compat,
3457
82e9f9603b1b evolvestate: rename the file to state.py and class name to cmdstate
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3456
diff changeset
    42
    state,
2724
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    43
    exthelper,
2756
f4dd6e6d4c73 rewriteutil: create a rewriteutil module to host utility function
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2752
diff changeset
    44
    rewriteutil,
2763
4a5b0c373e65 commands: move the touch to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2762
diff changeset
    45
    utility,
2724
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    46
)
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    47
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    48
eh = exthelper.exthelper()
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    49
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    50
walkopts = commands.walkopts
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    51
commitopts = commands.commitopts
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    52
commitopts2 = commands.commitopts2
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    53
mergetoolopts = commands.mergetoolopts
2941
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
    54
stringio = util.stringio
5078
66f01dd79f5d evolve: rename a function to make it explicit
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 5073
diff changeset
    55
precheck_contentdiv = rewriteutil.precheck_contentdiv
2724
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    56
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    57
# option added by evolve
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    58
3283
039c4b8dc3ed obsnote: warn if user try to store a note in obsmarker on an older hg
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3271
diff changeset
    59
def _checknotesize(ui, opts):
3213
7bc587557e4f cmdrewrite: add a utility function to make sure note is of valid format
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2949
diff changeset
    60
    """ make sure note is of valid format """
7bc587557e4f cmdrewrite: add a utility function to make sure note is of valid format
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2949
diff changeset
    61
7bc587557e4f cmdrewrite: add a utility function to make sure note is of valid format
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2949
diff changeset
    62
    note = opts.get('note')
7bc587557e4f cmdrewrite: add a utility function to make sure note is of valid format
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2949
diff changeset
    63
    if not note:
7bc587557e4f cmdrewrite: add a utility function to make sure note is of valid format
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2949
diff changeset
    64
        return
7bc587557e4f cmdrewrite: add a utility function to make sure note is of valid format
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2949
diff changeset
    65
7bc587557e4f cmdrewrite: add a utility function to make sure note is of valid format
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2949
diff changeset
    66
    if len(note) > 255:
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
    67
        raise error.Abort(_(b"cannot store a note of more than 255 bytes"))
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
    68
    if b'\n' in note:
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
    69
        raise error.Abort(_(b"note cannot contain a newline"))
3213
7bc587557e4f cmdrewrite: add a utility function to make sure note is of valid format
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2949
diff changeset
    70
2724
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    71
def _resolveoptions(ui, opts):
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    72
    """modify commit options dict to handle related options
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    73
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    74
    For now, all it does is figure out the commit date: respect -D unless
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    75
    -d was supplied.
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    76
    """
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    77
    # N.B. this is extremely similar to setupheaderopts() in mq.py
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    78
    if not opts.get('date') and opts.get('current_date'):
5178
61e49d2654cc compat: directly use function from dateutil
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 5136
diff changeset
    79
        opts['date'] = b'%d %d' % dateutil.makedate()
2724
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    80
    if not opts.get('user') and opts.get('current_user'):
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    81
        opts['user'] = ui.username()
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    82
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    83
commitopts3 = [
4715
12c8b24757f4 py3: use byte strings for @command registrations
Martin von Zweigbergk <martinvonz@google.com>
parents: 4714
diff changeset
    84
    (b'D', b'current-date', None,
12c8b24757f4 py3: use byte strings for @command registrations
Martin von Zweigbergk <martinvonz@google.com>
parents: 4714
diff changeset
    85
     _(b'record the current date as commit date')),
12c8b24757f4 py3: use byte strings for @command registrations
Martin von Zweigbergk <martinvonz@google.com>
parents: 4714
diff changeset
    86
    (b'U', b'current-user', None,
12c8b24757f4 py3: use byte strings for @command registrations
Martin von Zweigbergk <martinvonz@google.com>
parents: 4714
diff changeset
    87
     _(b'record the current user as committer')),
2724
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    88
]
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    89
4715
12c8b24757f4 py3: use byte strings for @command registrations
Martin von Zweigbergk <martinvonz@google.com>
parents: 4714
diff changeset
    90
interactiveopt = [[b'i', b'interactive', None, _(b'use interactive mode')]]
2724
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    91
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    92
@eh.command(
4715
12c8b24757f4 py3: use byte strings for @command registrations
Martin von Zweigbergk <martinvonz@google.com>
parents: 4714
diff changeset
    93
    b'amend|refresh',
12c8b24757f4 py3: use byte strings for @command registrations
Martin von Zweigbergk <martinvonz@google.com>
parents: 4714
diff changeset
    94
    [(b'A', b'addremove', None,
12c8b24757f4 py3: use byte strings for @command registrations
Martin von Zweigbergk <martinvonz@google.com>
parents: 4714
diff changeset
    95
      _(b'mark new/missing files as added/removed before committing')),
12c8b24757f4 py3: use byte strings for @command registrations
Martin von Zweigbergk <martinvonz@google.com>
parents: 4714
diff changeset
    96
     (b'a', b'all', False, _(b"match all files")),
12c8b24757f4 py3: use byte strings for @command registrations
Martin von Zweigbergk <martinvonz@google.com>
parents: 4714
diff changeset
    97
     (b'e', b'edit', False, _(b'invoke editor on commit messages')),
12c8b24757f4 py3: use byte strings for @command registrations
Martin von Zweigbergk <martinvonz@google.com>
parents: 4714
diff changeset
    98
     (b'', b'extract', False, _(b'extract changes from the commit to the working copy')),
12c8b24757f4 py3: use byte strings for @command registrations
Martin von Zweigbergk <martinvonz@google.com>
parents: 4714
diff changeset
    99
     (b'', b'patch', False, _(b'make changes to wdir parent by editing patch')),
12c8b24757f4 py3: use byte strings for @command registrations
Martin von Zweigbergk <martinvonz@google.com>
parents: 4714
diff changeset
   100
     (b'', b'close-branch', None,
12c8b24757f4 py3: use byte strings for @command registrations
Martin von Zweigbergk <martinvonz@google.com>
parents: 4714
diff changeset
   101
      _(b'mark a branch as closed, hiding it from the branch list')),
12c8b24757f4 py3: use byte strings for @command registrations
Martin von Zweigbergk <martinvonz@google.com>
parents: 4714
diff changeset
   102
     (b's', b'secret', None, _(b'use the secret phase for committing')),
12c8b24757f4 py3: use byte strings for @command registrations
Martin von Zweigbergk <martinvonz@google.com>
parents: 4714
diff changeset
   103
     (b'n', b'note', b'', _(b'store a note on amend'), _(b'TEXT')),
12c8b24757f4 py3: use byte strings for @command registrations
Martin von Zweigbergk <martinvonz@google.com>
parents: 4714
diff changeset
   104
     ] + walkopts + commitopts + commitopts2 + commitopts3 + interactiveopt,
12c8b24757f4 py3: use byte strings for @command registrations
Martin von Zweigbergk <martinvonz@google.com>
parents: 4714
diff changeset
   105
    _(b'[OPTION]... [FILE]...'),
4894
f9743b13de6d help: categorizing evolve and topic commands
Rodrigo Damazio <rdamazio@google.com>
parents: 4891
diff changeset
   106
    helpbasic=True,
f9743b13de6d help: categorizing evolve and topic commands
Rodrigo Damazio <rdamazio@google.com>
parents: 4891
diff changeset
   107
    **compat.helpcategorykwargs('CATEGORY_COMMITTING'))
2724
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   108
def amend(ui, repo, *pats, **opts):
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   109
    """combine a changeset with updates and replace it with a new one
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   110
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   111
    Commits a new changeset incorporating both the changes to the given files
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   112
    and all the changes from the current parent changeset into the repository.
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   113
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   114
    See :hg:`commit` for details about committing changes.
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   115
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   116
    If you don't specify -m, the parent's message will be reused.
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   117
3296
b834cb64f779 amend: fix a typo in amend help text related to the extract option
Boris Feld <boris.feld@octobus.net>
parents: 3283
diff changeset
   118
    If --extract is specified, the behavior of `hg amend` is reversed: Changes
2730
7fbb7a5d359f uncommit: expose the feature with a '--extract' to amend
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2729
diff changeset
   119
    to selected files in the checked out revision appear again as uncommitted
7fbb7a5d359f uncommit: expose the feature with a '--extract' to amend
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2729
diff changeset
   120
    changed in the working directory.
7fbb7a5d359f uncommit: expose the feature with a '--extract' to amend
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2729
diff changeset
   121
2724
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   122
    Returns 0 on success, 1 if nothing changed.
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   123
    """
3283
039c4b8dc3ed obsnote: warn if user try to store a note in obsmarker on an older hg
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3271
diff changeset
   124
    _checknotesize(ui, opts)
2724
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   125
    opts = opts.copy()
4988
588b3484c131 amend: abort if both --patch and --extract are used
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4956
diff changeset
   126
    if opts.get('patch') and opts.get('extract'):
588b3484c131 amend: abort if both --patch and --extract are used
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4956
diff changeset
   127
        raise error.Abort(_(b'cannot use both --patch and --extract'))
3660
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   128
    if opts.get('patch'):
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   129
        return amendpatch(ui, repo, *pats, **opts)
2730
7fbb7a5d359f uncommit: expose the feature with a '--extract' to amend
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2729
diff changeset
   130
    if opts.get('extract'):
7fbb7a5d359f uncommit: expose the feature with a '--extract' to amend
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2729
diff changeset
   131
        return uncommit(ui, repo, *pats, **opts)
7fbb7a5d359f uncommit: expose the feature with a '--extract' to amend
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2729
diff changeset
   132
    else:
7fbb7a5d359f uncommit: expose the feature with a '--extract' to amend
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2729
diff changeset
   133
        if opts.pop('all', False):
7fbb7a5d359f uncommit: expose the feature with a '--extract' to amend
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2729
diff changeset
   134
            # add an include for all
7fbb7a5d359f uncommit: expose the feature with a '--extract' to amend
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2729
diff changeset
   135
            include = list(opts.get('include'))
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   136
            include.append(b're:.*')
2730
7fbb7a5d359f uncommit: expose the feature with a '--extract' to amend
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2729
diff changeset
   137
        edit = opts.pop('edit', False)
7fbb7a5d359f uncommit: expose the feature with a '--extract' to amend
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2729
diff changeset
   138
        log = opts.get('logfile')
7fbb7a5d359f uncommit: expose the feature with a '--extract' to amend
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2729
diff changeset
   139
        opts['amend'] = True
7fbb7a5d359f uncommit: expose the feature with a '--extract' to amend
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2729
diff changeset
   140
        _resolveoptions(ui, opts)
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   141
        _alias, commitcmd = cmdutil.findcmd(b'commit', commands.table)
3744
4a70392f1723 amend: use context manager for locks (issue5887)
Martin von Zweigbergk <martinvonz@google.com>
parents: 3709
diff changeset
   142
        with repo.wlock(), repo.lock():
3452
8275ef099135 amend: query the wdir parent after taking lock (issue5266)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3451
diff changeset
   143
            if not (edit or opts['message'] or log):
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   144
                opts['message'] = repo[b'.'].description()
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   145
            rewriteutil.precheck(repo, [repo[b'.'].rev()], action=b'amend')
2787
ebca049e8ca9 amend: use precheck to validate revision
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2786
diff changeset
   146
            return commitcmd[0](ui, repo, *pats, **opts)
2725
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   147
3660
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   148
def amendpatch(ui, repo, *pats, **opts):
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   149
    """logic for --patch flag of `hg amend` command."""
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   150
    with repo.wlock(), repo.lock(), repo.transaction(b'amend') as tr:
3660
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   151
        cmdutil.bailifchanged(repo)
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   152
        # first get the patch
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   153
        old = repo[b'.']
3660
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   154
        p1 = old.p1()
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   155
        rewriteutil.precheck(repo, [old.rev()], b'amend')
3660
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   156
        diffopts = patch.difffeatureopts(repo.ui, whitespace=True)
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   157
        diffopts.nodates = True
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   158
        diffopts.git = True
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   159
        fp = stringio()
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   160
        _writectxmetadata(repo, old, fp)
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   161
        matcher = scmutil.match(old, pats, opts)
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   162
        for chunk, label in patch.diffui(repo, p1.node(), old.node(),
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   163
                                         match=matcher,
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   164
                                         opts=diffopts):
4463
7d54a538dd1e evolve: unindent some lines caught by flake8
Anton Shestakov <av6@dwimlabs.net>
parents: 4446
diff changeset
   165
            fp.write(chunk)
3661
61fdd25542a6 patch: offer the user a chance to fix the patch if it fails to apply
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3660
diff changeset
   166
        newnode = _editandapply(ui, repo, pats, old, p1, fp, diffopts)
3660
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   167
        if newnode == old.node():
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   168
            raise error.Abort(_(b"nothing changed"))
3660
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   169
        metadata = {}
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   170
        if opts.get('note'):
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   171
            metadata[b'note'] = opts['note']
4372
5345be014f2c amend: use scmutil.cleanupnodes() with --patch
Martin von Zweigbergk <martinvonz@google.com>
parents: 4371
diff changeset
   172
        replacements = {old.node(): [newnode]}
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   173
        scmutil.cleanupnodes(repo, replacements, operation=b'amend',
4372
5345be014f2c amend: use scmutil.cleanupnodes() with --patch
Martin von Zweigbergk <martinvonz@google.com>
parents: 4371
diff changeset
   174
                             metadata=metadata)
3660
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   175
        phases.retractboundary(repo, tr, old.phase(), [newnode])
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   176
        hg.updaterepo(repo, newnode, True)
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   177
3661
61fdd25542a6 patch: offer the user a chance to fix the patch if it fails to apply
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3660
diff changeset
   178
def _editandapply(ui, repo, pats, old, p1, fp, diffopts):
61fdd25542a6 patch: offer the user a chance to fix the patch if it fails to apply
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3660
diff changeset
   179
    newnode = None
61fdd25542a6 patch: offer the user a chance to fix the patch if it fails to apply
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3660
diff changeset
   180
    while newnode is None:
61fdd25542a6 patch: offer the user a chance to fix the patch if it fails to apply
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3660
diff changeset
   181
        fp.seek(0)
61fdd25542a6 patch: offer the user a chance to fix the patch if it fails to apply
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3660
diff changeset
   182
        previous_patch = fp.getvalue()
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   183
        newpatch = ui.edit(fp.getvalue(), old.user(), action=b"diff")
3661
61fdd25542a6 patch: offer the user a chance to fix the patch if it fails to apply
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3660
diff changeset
   184
61fdd25542a6 patch: offer the user a chance to fix the patch if it fails to apply
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3660
diff changeset
   185
        afp = stringio()
61fdd25542a6 patch: offer the user a chance to fix the patch if it fails to apply
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3660
diff changeset
   186
        afp.write(newpatch)
61fdd25542a6 patch: offer the user a chance to fix the patch if it fails to apply
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3660
diff changeset
   187
        if pats:
61fdd25542a6 patch: offer the user a chance to fix the patch if it fails to apply
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3660
diff changeset
   188
            # write rest of the files in the patch
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   189
            restmatcher = scmutil.match(old, [], opts={b'exclude': pats})
3661
61fdd25542a6 patch: offer the user a chance to fix the patch if it fails to apply
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3660
diff changeset
   190
            for chunk, label in patch.diffui(repo, p1.node(), old.node(),
61fdd25542a6 patch: offer the user a chance to fix the patch if it fails to apply
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3660
diff changeset
   191
                                             match=restmatcher,
61fdd25542a6 patch: offer the user a chance to fix the patch if it fails to apply
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3660
diff changeset
   192
                                             opts=diffopts):
4463
7d54a538dd1e evolve: unindent some lines caught by flake8
Anton Shestakov <av6@dwimlabs.net>
parents: 4446
diff changeset
   193
                afp.write(chunk)
3661
61fdd25542a6 patch: offer the user a chance to fix the patch if it fails to apply
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3660
diff changeset
   194
61fdd25542a6 patch: offer the user a chance to fix the patch if it fails to apply
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3660
diff changeset
   195
        user_patch = afp.getvalue()
3877
96bbea985b25 amend: allow aborting an `amend --patch` by saving an empty file (issue5925)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3751
diff changeset
   196
        if not user_patch:
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   197
            raise error.Abort(_(b"empty patch file, amend aborted"))
3661
61fdd25542a6 patch: offer the user a chance to fix the patch if it fails to apply
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3660
diff changeset
   198
        if user_patch == previous_patch:
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   199
            raise error.Abort(_(b"patch unchanged"))
3661
61fdd25542a6 patch: offer the user a chance to fix the patch if it fails to apply
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3660
diff changeset
   200
        afp.seek(0)
61fdd25542a6 patch: offer the user a chance to fix the patch if it fails to apply
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3660
diff changeset
   201
        # write the patch to repo and get the newnode
61fdd25542a6 patch: offer the user a chance to fix the patch if it fails to apply
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3660
diff changeset
   202
        try:
61fdd25542a6 patch: offer the user a chance to fix the patch if it fails to apply
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3660
diff changeset
   203
            newnode = _writepatch(ui, repo, old, afp)
61fdd25542a6 patch: offer the user a chance to fix the patch if it fails to apply
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3660
diff changeset
   204
        except patch.PatchError as err:
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   205
            ui.write_err(_(b"failed to apply edited patch: %s\n") % err)
3661
61fdd25542a6 patch: offer the user a chance to fix the patch if it fails to apply
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3660
diff changeset
   206
            defaultchoice = 0 # yes
61fdd25542a6 patch: offer the user a chance to fix the patch if it fails to apply
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3660
diff changeset
   207
            if not ui.interactive:
61fdd25542a6 patch: offer the user a chance to fix the patch if it fails to apply
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3660
diff changeset
   208
                defaultchoice = 1 # no
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   209
            retrychoice = _(b'try to fix the patch (yn)?$$ &Yes $$ &No')
4108
ef3c9ecb8099 cmdrewrite: rename variable and move it closer to where it is used
Anton Shestakov <av6@dwimlabs.net>
parents: 4052
diff changeset
   210
            if ui.promptchoice(retrychoice, default=defaultchoice):
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   211
                raise error.Abort(_(b"Could not apply amended path"))
3661
61fdd25542a6 patch: offer the user a chance to fix the patch if it fails to apply
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3660
diff changeset
   212
            else:
61fdd25542a6 patch: offer the user a chance to fix the patch if it fails to apply
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3660
diff changeset
   213
                # consider a third choice where we restore the original patch
61fdd25542a6 patch: offer the user a chance to fix the patch if it fails to apply
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3660
diff changeset
   214
                fp = stringio()
61fdd25542a6 patch: offer the user a chance to fix the patch if it fails to apply
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3660
diff changeset
   215
                fp.write(user_patch)
61fdd25542a6 patch: offer the user a chance to fix the patch if it fails to apply
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3660
diff changeset
   216
    return newnode
61fdd25542a6 patch: offer the user a chance to fix the patch if it fails to apply
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3660
diff changeset
   217
3660
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   218
def _writepatch(ui, repo, old, fp):
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   219
    """utility function to use filestore and patchrepo to apply a patch to the
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   220
    repository with metadata being extracted from the patch"""
5185
c111d5db3713 evolve: drop 4.5 compatibility with `patch.extract` API
Anton Shestakov <av6@dwimlabs.net>
parents: 5181
diff changeset
   221
    patchcontext = patch.extract(ui, fp)
3660
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   222
    pold = old.p1()
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   223
3670
1043e9c54355 compat: adapt `amend --patch` to the new `patch.extract` API
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3661
diff changeset
   224
    with patchcontext as metadata:
1043e9c54355 compat: adapt `amend --patch` to the new `patch.extract` API
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3661
diff changeset
   225
        # store the metadata from the patch to variables
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   226
        parents = (metadata.get(b'p1'), metadata.get(b'p2'))
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   227
        date = metadata.get(b'date') or old.date()
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   228
        branch = metadata.get(b'branch') or old.branch()
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   229
        user = metadata.get(b'user') or old.user()
3670
1043e9c54355 compat: adapt `amend --patch` to the new `patch.extract` API
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3661
diff changeset
   230
        # XXX: we must extract extras from the patchfile too
1043e9c54355 compat: adapt `amend --patch` to the new `patch.extract` API
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3661
diff changeset
   231
        extra = old.extra()
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   232
        message = metadata.get(b'message') or old.description()
3670
1043e9c54355 compat: adapt `amend --patch` to the new `patch.extract` API
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3661
diff changeset
   233
        store = patch.filestore()
1043e9c54355 compat: adapt `amend --patch` to the new `patch.extract` API
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3661
diff changeset
   234
        fp.seek(0)
1043e9c54355 compat: adapt `amend --patch` to the new `patch.extract` API
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3661
diff changeset
   235
        try:
1043e9c54355 compat: adapt `amend --patch` to the new `patch.extract` API
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3661
diff changeset
   236
            files = set()
1043e9c54355 compat: adapt `amend --patch` to the new `patch.extract` API
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3661
diff changeset
   237
            # beware: next line may raise a PatchError to be handled by the caller
1043e9c54355 compat: adapt `amend --patch` to the new `patch.extract` API
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3661
diff changeset
   238
            # of this function
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   239
            patch.patchrepo(ui, repo, pold, store, fp, 1, b'',
3670
1043e9c54355 compat: adapt `amend --patch` to the new `patch.extract` API
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3661
diff changeset
   240
                            files=files, eolmode=None)
3660
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   241
3670
1043e9c54355 compat: adapt `amend --patch` to the new `patch.extract` API
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3661
diff changeset
   242
            memctx = context.memctx(repo, parents, message, files=files,
1043e9c54355 compat: adapt `amend --patch` to the new `patch.extract` API
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3661
diff changeset
   243
                                    filectxfn=store,
1043e9c54355 compat: adapt `amend --patch` to the new `patch.extract` API
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3661
diff changeset
   244
                                    user=user,
1043e9c54355 compat: adapt `amend --patch` to the new `patch.extract` API
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3661
diff changeset
   245
                                    date=date,
1043e9c54355 compat: adapt `amend --patch` to the new `patch.extract` API
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3661
diff changeset
   246
                                    branch=branch,
1043e9c54355 compat: adapt `amend --patch` to the new `patch.extract` API
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3661
diff changeset
   247
                                    extra=extra)
1043e9c54355 compat: adapt `amend --patch` to the new `patch.extract` API
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3661
diff changeset
   248
            newcm = memctx.commit()
1043e9c54355 compat: adapt `amend --patch` to the new `patch.extract` API
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3661
diff changeset
   249
        finally:
1043e9c54355 compat: adapt `amend --patch` to the new `patch.extract` API
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3661
diff changeset
   250
            store.close()
3660
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   251
    return newcm
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   252
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   253
def _writectxmetadata(repo, ctx, fp):
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   254
    nodeval = scmutil.binnode(ctx)
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   255
    parents = [p.node() for p in ctx.parents() if p]
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   256
    branch = ctx.branch()
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   257
    if parents:
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   258
        prev = parents[0]
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   259
    else:
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   260
        prev = node.nullid
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   261
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   262
    fp.write(b"# HG changeset patch\n")
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   263
    fp.write(b"# User %s\n" % ctx.user())
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   264
    fp.write(b"# Date %d %d\n" % ctx.date())
5178
61e49d2654cc compat: directly use function from dateutil
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 5136
diff changeset
   265
    fp.write(b"#      %s\n" % dateutil.datestr(ctx.date()))
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   266
    if branch and branch != b'default':
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   267
        fp.write(b"# Branch %s\n" % branch)
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   268
    fp.write(b"# Node ID %s\n" % node.hex(nodeval))
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   269
    fp.write(b"# Parent  %s\n" % node.hex(prev))
3660
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   270
    if len(parents) > 1:
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   271
        fp.write(b"# Parent  %s\n" % node.hex(parents[1]))
3660
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   272
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   273
    for headerid in cmdutil.extraexport:
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   274
        header = cmdutil.extraexportmap[headerid](1, ctx)
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   275
        if header is not None:
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   276
            fp.write(b'# %s\n' % header)
3660
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   277
    fp.write(ctx.description().rstrip())
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   278
    fp.write(b"\n\n")
3660
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   279
2725
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   280
def _touchedbetween(repo, source, dest, match=None):
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   281
    touched = set()
5093
fd1d12497fdb cmdrewrite: avoid accessing scmutil.status fields by index
Martin von Zweigbergk <martinvonz@google.com>
parents: 4988
diff changeset
   282
    st = repo.status(source, dest, match=match)
fd1d12497fdb cmdrewrite: avoid accessing scmutil.status fields by index
Martin von Zweigbergk <martinvonz@google.com>
parents: 4988
diff changeset
   283
    touched.update(st.modified)
fd1d12497fdb cmdrewrite: avoid accessing scmutil.status fields by index
Martin von Zweigbergk <martinvonz@google.com>
parents: 4988
diff changeset
   284
    touched.update(st.added)
fd1d12497fdb cmdrewrite: avoid accessing scmutil.status fields by index
Martin von Zweigbergk <martinvonz@google.com>
parents: 4988
diff changeset
   285
    touched.update(st.removed)
2725
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   286
    return touched
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   287
2728
3c371aa16cb9 uncommit: add support for --user and --date
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2727
diff changeset
   288
def _commitfiltered(repo, ctx, match, target=None, message=None, user=None,
3c371aa16cb9 uncommit: add support for --user and --date
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2727
diff changeset
   289
                    date=None):
2725
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   290
    """Recommit ctx with changed files not in match. Return the new
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   291
    node identifier, or None if nothing changed.
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   292
    """
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   293
    base = ctx.p1()
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   294
    if target is None:
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   295
        target = base
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   296
    # ctx
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   297
    initialfiles = _touchedbetween(repo, base, ctx)
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   298
    if base == target:
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   299
        affected = set(f for f in initialfiles if match(f))
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   300
        newcontent = set()
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   301
    else:
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   302
        affected = _touchedbetween(repo, target, ctx, match=match)
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   303
        newcontent = _touchedbetween(repo, target, base, match=match)
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   304
    # The commit touchs all existing files
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   305
    # + all file that needs a new content
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   306
    # - the file affected bny uncommit with the same content than base.
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   307
    files = (initialfiles - affected) | newcontent
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   308
    if not newcontent and files == initialfiles:
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   309
        return None
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   310
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   311
    # Filter copies
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   312
    copied = copies.pathcopies(target, ctx)
4714
c51fc0ae7a7e py3: switch from iteritems() to items()
Martin von Zweigbergk <martinvonz@google.com>
parents: 4707
diff changeset
   313
    copied = dict((dst, src) for dst, src in copied.items()
2725
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   314
                  if dst in files)
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   315
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   316
    def filectxfn(repo, memctx, path, contentctx=ctx, redirect=newcontent):
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   317
        if path in redirect:
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   318
            return filectxfn(repo, memctx, path, contentctx=target, redirect=())
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   319
        if path not in contentctx:
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   320
            return None
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   321
        fctx = contentctx[path]
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   322
        flags = fctx.flags()
3298
f4b06f44d274 memfilectx: changectx argument is not mandatory
Boris Feld <boris.feld@octobus.net>
parents: 3296
diff changeset
   323
        mctx = compat.memfilectx(repo, memctx, fctx, flags, copied, path)
2725
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   324
        return mctx
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   325
2727
f7d44441dfd3 uncommit: add support for --message and --logfile
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2726
diff changeset
   326
    if message is None:
f7d44441dfd3 uncommit: add support for --message and --logfile
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2726
diff changeset
   327
        message = ctx.description()
2728
3c371aa16cb9 uncommit: add support for --user and --date
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2727
diff changeset
   328
    if not user:
3c371aa16cb9 uncommit: add support for --user and --date
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2727
diff changeset
   329
        user = ctx.user()
3c371aa16cb9 uncommit: add support for --user and --date
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2727
diff changeset
   330
    if not date:
3c371aa16cb9 uncommit: add support for --user and --date
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2727
diff changeset
   331
        date = ctx.date()
2725
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   332
    new = context.memctx(repo,
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   333
                         parents=[base.node(), node.nullid],
2727
f7d44441dfd3 uncommit: add support for --message and --logfile
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2726
diff changeset
   334
                         text=message,
2725
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   335
                         files=files,
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   336
                         filectxfn=filectxfn,
2728
3c371aa16cb9 uncommit: add support for --user and --date
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2727
diff changeset
   337
                         user=user,
3c371aa16cb9 uncommit: add support for --user and --date
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2727
diff changeset
   338
                         date=date,
2725
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   339
                         extra=ctx.extra())
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   340
    # commitctx always create a new revision, no need to check
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   341
    newid = repo.commitctx(new)
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   342
    return newid
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   343
2943
25576132cb30 uncommit: fix the dirstate handling in `uncommit -i`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2942
diff changeset
   344
def _uncommitdirstate(repo, oldctx, match, interactive):
2725
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   345
    """Fix the dirstate after switching the working directory from
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   346
    oldctx to a copy of oldctx not containing changed files matched by
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   347
    match.
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   348
    """
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   349
    ctx = repo[b'.']
2725
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   350
    ds = repo.dirstate
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   351
    copies = dict(ds.copies())
2943
25576132cb30 uncommit: fix the dirstate handling in `uncommit -i`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2942
diff changeset
   352
    if interactive:
25576132cb30 uncommit: fix the dirstate handling in `uncommit -i`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2942
diff changeset
   353
        # In interactive cases, we will find the status between oldctx and ctx
25576132cb30 uncommit: fix the dirstate handling in `uncommit -i`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2942
diff changeset
   354
        # and considering only the files which are changed between oldctx and
25576132cb30 uncommit: fix the dirstate handling in `uncommit -i`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2942
diff changeset
   355
        # ctx, and the status of what changed between oldctx and ctx will help
25576132cb30 uncommit: fix the dirstate handling in `uncommit -i`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2942
diff changeset
   356
        # us in defining the exact behavior
5093
fd1d12497fdb cmdrewrite: avoid accessing scmutil.status fields by index
Martin von Zweigbergk <martinvonz@google.com>
parents: 4988
diff changeset
   357
        st = repo.status(oldctx, ctx, match=match)
fd1d12497fdb cmdrewrite: avoid accessing scmutil.status fields by index
Martin von Zweigbergk <martinvonz@google.com>
parents: 4988
diff changeset
   358
        for f in st.modified:
2943
25576132cb30 uncommit: fix the dirstate handling in `uncommit -i`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2942
diff changeset
   359
            # These are files which are modified between oldctx and ctx which
25576132cb30 uncommit: fix the dirstate handling in `uncommit -i`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2942
diff changeset
   360
            # contains two cases: 1) Were modified in oldctx and some
25576132cb30 uncommit: fix the dirstate handling in `uncommit -i`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2942
diff changeset
   361
            # modifications are uncommitted
25576132cb30 uncommit: fix the dirstate handling in `uncommit -i`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2942
diff changeset
   362
            # 2) Were added in oldctx but some part is uncommitted (this cannot
25576132cb30 uncommit: fix the dirstate handling in `uncommit -i`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2942
diff changeset
   363
            # contain the case when added files are uncommitted completely as
25576132cb30 uncommit: fix the dirstate handling in `uncommit -i`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2942
diff changeset
   364
            # that will result in status as removed not modified.)
25576132cb30 uncommit: fix the dirstate handling in `uncommit -i`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2942
diff changeset
   365
            # Also any modifications to a removed file will result the status as
25576132cb30 uncommit: fix the dirstate handling in `uncommit -i`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2942
diff changeset
   366
            # added, so we have only two cases. So in either of the cases, the
25576132cb30 uncommit: fix the dirstate handling in `uncommit -i`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2942
diff changeset
   367
            # resulting status can be modified or clean.
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   368
            if ds[f] == b'r':
2943
25576132cb30 uncommit: fix the dirstate handling in `uncommit -i`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2942
diff changeset
   369
                # But the file is removed in the working directory, leaving that
25576132cb30 uncommit: fix the dirstate handling in `uncommit -i`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2942
diff changeset
   370
                # as removed
25576132cb30 uncommit: fix the dirstate handling in `uncommit -i`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2942
diff changeset
   371
                continue
25576132cb30 uncommit: fix the dirstate handling in `uncommit -i`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2942
diff changeset
   372
            ds.normallookup(f)
25576132cb30 uncommit: fix the dirstate handling in `uncommit -i`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2942
diff changeset
   373
5093
fd1d12497fdb cmdrewrite: avoid accessing scmutil.status fields by index
Martin von Zweigbergk <martinvonz@google.com>
parents: 4988
diff changeset
   374
        for f in st.added:
2943
25576132cb30 uncommit: fix the dirstate handling in `uncommit -i`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2942
diff changeset
   375
            # These are the files which are added between oldctx and ctx(new
25576132cb30 uncommit: fix the dirstate handling in `uncommit -i`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2942
diff changeset
   376
            # one), which means the files which were removed in oldctx
25576132cb30 uncommit: fix the dirstate handling in `uncommit -i`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2942
diff changeset
   377
            # but uncommitted completely while making the ctx
25576132cb30 uncommit: fix the dirstate handling in `uncommit -i`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2942
diff changeset
   378
            # This file should be marked as removed if the working directory
25576132cb30 uncommit: fix the dirstate handling in `uncommit -i`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2942
diff changeset
   379
            # does not adds it back. If it's adds it back, we do a normallookup.
25576132cb30 uncommit: fix the dirstate handling in `uncommit -i`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2942
diff changeset
   380
            # The file can't be removed in working directory, because it was
25576132cb30 uncommit: fix the dirstate handling in `uncommit -i`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2942
diff changeset
   381
            # removed in oldctx
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   382
            if ds[f] == b'a':
2943
25576132cb30 uncommit: fix the dirstate handling in `uncommit -i`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2942
diff changeset
   383
                ds.normallookup(f)
25576132cb30 uncommit: fix the dirstate handling in `uncommit -i`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2942
diff changeset
   384
                continue
25576132cb30 uncommit: fix the dirstate handling in `uncommit -i`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2942
diff changeset
   385
            ds.remove(f)
25576132cb30 uncommit: fix the dirstate handling in `uncommit -i`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2942
diff changeset
   386
5093
fd1d12497fdb cmdrewrite: avoid accessing scmutil.status fields by index
Martin von Zweigbergk <martinvonz@google.com>
parents: 4988
diff changeset
   387
        for f in st.removed:
2943
25576132cb30 uncommit: fix the dirstate handling in `uncommit -i`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2942
diff changeset
   388
            # These are files which are removed between oldctx and ctx, which
25576132cb30 uncommit: fix the dirstate handling in `uncommit -i`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2942
diff changeset
   389
            # means the files which were added in oldctx and were completely
25576132cb30 uncommit: fix the dirstate handling in `uncommit -i`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2942
diff changeset
   390
            # uncommitted in ctx. If a added file is partially uncommitted, that
25576132cb30 uncommit: fix the dirstate handling in `uncommit -i`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2942
diff changeset
   391
            # would have resulted in modified status, not removed.
25576132cb30 uncommit: fix the dirstate handling in `uncommit -i`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2942
diff changeset
   392
            # So a file added in a commit, and uncommitting that addition must
25576132cb30 uncommit: fix the dirstate handling in `uncommit -i`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2942
diff changeset
   393
            # result in file being stated as unknown.
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   394
            if ds[f] == b'r':
2943
25576132cb30 uncommit: fix the dirstate handling in `uncommit -i`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2942
diff changeset
   395
                # The working directory say it's removed, so lets make the file
25576132cb30 uncommit: fix the dirstate handling in `uncommit -i`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2942
diff changeset
   396
                # unknown
25576132cb30 uncommit: fix the dirstate handling in `uncommit -i`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2942
diff changeset
   397
                ds.drop(f)
25576132cb30 uncommit: fix the dirstate handling in `uncommit -i`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2942
diff changeset
   398
                continue
25576132cb30 uncommit: fix the dirstate handling in `uncommit -i`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2942
diff changeset
   399
            ds.add(f)
25576132cb30 uncommit: fix the dirstate handling in `uncommit -i`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2942
diff changeset
   400
    else:
5093
fd1d12497fdb cmdrewrite: avoid accessing scmutil.status fields by index
Martin von Zweigbergk <martinvonz@google.com>
parents: 4988
diff changeset
   401
        st = repo.status(oldctx.p1(), oldctx, match=match)
fd1d12497fdb cmdrewrite: avoid accessing scmutil.status fields by index
Martin von Zweigbergk <martinvonz@google.com>
parents: 4988
diff changeset
   402
        for f in st.modified:
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   403
            if ds[f] == b'r':
2942
10194206acc7 uncommit: pre-indent a block in if-True
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2941
diff changeset
   404
                # modified + removed -> removed
10194206acc7 uncommit: pre-indent a block in if-True
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2941
diff changeset
   405
                continue
10194206acc7 uncommit: pre-indent a block in if-True
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2941
diff changeset
   406
            ds.normallookup(f)
2725
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   407
5093
fd1d12497fdb cmdrewrite: avoid accessing scmutil.status fields by index
Martin von Zweigbergk <martinvonz@google.com>
parents: 4988
diff changeset
   408
        for f in st.added:
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   409
            if ds[f] == b'r':
2942
10194206acc7 uncommit: pre-indent a block in if-True
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2941
diff changeset
   410
                # added + removed -> unknown
10194206acc7 uncommit: pre-indent a block in if-True
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2941
diff changeset
   411
                ds.drop(f)
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   412
            elif ds[f] != b'a':
2942
10194206acc7 uncommit: pre-indent a block in if-True
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2941
diff changeset
   413
                ds.add(f)
2725
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   414
5093
fd1d12497fdb cmdrewrite: avoid accessing scmutil.status fields by index
Martin von Zweigbergk <martinvonz@google.com>
parents: 4988
diff changeset
   415
        for f in st.removed:
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   416
            if ds[f] == b'a':
2942
10194206acc7 uncommit: pre-indent a block in if-True
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2941
diff changeset
   417
                # removed + added -> normal
10194206acc7 uncommit: pre-indent a block in if-True
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2941
diff changeset
   418
                ds.normallookup(f)
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   419
            elif ds[f] != b'r':
2942
10194206acc7 uncommit: pre-indent a block in if-True
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2941
diff changeset
   420
                ds.remove(f)
2725
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   421
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   422
    # Merge old parent and old working dir copies
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   423
    oldcopies = {}
2943
25576132cb30 uncommit: fix the dirstate handling in `uncommit -i`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2942
diff changeset
   424
    if interactive:
25576132cb30 uncommit: fix the dirstate handling in `uncommit -i`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2942
diff changeset
   425
        # Interactive had different meaning of the variables so restoring the
25576132cb30 uncommit: fix the dirstate handling in `uncommit -i`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2942
diff changeset
   426
        # original meaning to use them
5093
fd1d12497fdb cmdrewrite: avoid accessing scmutil.status fields by index
Martin von Zweigbergk <martinvonz@google.com>
parents: 4988
diff changeset
   427
        st = repo.status(oldctx.p1(), oldctx, match=match)
fd1d12497fdb cmdrewrite: avoid accessing scmutil.status fields by index
Martin von Zweigbergk <martinvonz@google.com>
parents: 4988
diff changeset
   428
    for f in (st.modified + st.added):
2725
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   429
        src = oldctx[f].renamed()
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   430
        if src:
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   431
            oldcopies[f] = src[0]
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   432
    oldcopies.update(copies)
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   433
    copies = dict((dst, oldcopies.get(src, src))
4714
c51fc0ae7a7e py3: switch from iteritems() to items()
Martin von Zweigbergk <martinvonz@google.com>
parents: 4707
diff changeset
   434
                  for dst, src in oldcopies.items())
2725
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   435
    # Adjust the dirstate copies
4714
c51fc0ae7a7e py3: switch from iteritems() to items()
Martin von Zweigbergk <martinvonz@google.com>
parents: 4707
diff changeset
   436
    for dst, src in copies.items():
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   437
        if (src not in ctx or dst in ctx or ds[dst] != b'a'):
2725
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   438
            src = None
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   439
        ds.copy(src, dst)
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   440
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   441
@eh.command(
4715
12c8b24757f4 py3: use byte strings for @command registrations
Martin von Zweigbergk <martinvonz@google.com>
parents: 4714
diff changeset
   442
    b'uncommit',
12c8b24757f4 py3: use byte strings for @command registrations
Martin von Zweigbergk <martinvonz@google.com>
parents: 4714
diff changeset
   443
    [(b'a', b'all', None, _(b'uncommit all changes when no arguments given')),
12c8b24757f4 py3: use byte strings for @command registrations
Martin von Zweigbergk <martinvonz@google.com>
parents: 4714
diff changeset
   444
     (b'i', b'interactive', False, _(b'interactive mode to uncommit (EXPERIMENTAL)')),
12c8b24757f4 py3: use byte strings for @command registrations
Martin von Zweigbergk <martinvonz@google.com>
parents: 4714
diff changeset
   445
     (b'r', b'rev', b'', _(b'revert commit content to REV instead'), _(b'REV')),
12c8b24757f4 py3: use byte strings for @command registrations
Martin von Zweigbergk <martinvonz@google.com>
parents: 4714
diff changeset
   446
     (b'', b'revert', False, _(b'discard working directory changes after uncommit')),
12c8b24757f4 py3: use byte strings for @command registrations
Martin von Zweigbergk <martinvonz@google.com>
parents: 4714
diff changeset
   447
     (b'n', b'note', b'', _(b'store a note on uncommit'), _(b'TEXT')),
2729
69fe16428b0f uncommit: add support for -U and -D
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2728
diff changeset
   448
     ] + commands.walkopts + commitopts + commitopts2 + commitopts3,
4921
a7c01a2a3974 branching: merge with stable
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4906 4915
diff changeset
   449
    _(b'[OPTION]... [FILE]...'),
4894
f9743b13de6d help: categorizing evolve and topic commands
Rodrigo Damazio <rdamazio@google.com>
parents: 4891
diff changeset
   450
    **compat.helpcategorykwargs('CATEGORY_CHANGE_MANAGEMENT'))
2725
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   451
def uncommit(ui, repo, *pats, **opts):
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   452
    """move changes from parent revision to working directory
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   453
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   454
    Changes to selected files in the checked out revision appear again as
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   455
    uncommitted changed in the working directory. A new revision
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   456
    without the selected changes is created, becomes the checked out
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   457
    revision, and obsoletes the previous one.
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   458
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   459
    The --include option specifies patterns to uncommit.
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   460
    The --exclude option specifies patterns to keep in the commit.
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   461
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   462
    The --rev argument let you change the commit file to a content of another
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   463
    revision. It still does not change the content of your file in the working
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   464
    directory.
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   465
2941
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   466
    .. container:: verbose
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   467
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   468
       The --interactive option lets you select hunks interactively to uncommit.
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   469
       You can uncommit parts of file using this option.
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   470
2725
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   471
    Return 0 if changed files are uncommitted.
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   472
    """
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   473
3283
039c4b8dc3ed obsnote: warn if user try to store a note in obsmarker on an older hg
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3271
diff changeset
   474
    _checknotesize(ui, opts)
2729
69fe16428b0f uncommit: add support for -U and -D
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2728
diff changeset
   475
    _resolveoptions(ui, opts) # process commitopts3
2941
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   476
    interactive = opts.get('interactive')
2725
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   477
    wlock = lock = tr = None
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   478
    try:
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   479
        wlock = repo.wlock()
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   480
        lock = repo.lock()
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   481
        wctx = repo[None]
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   482
        if len(wctx.parents()) <= 0:
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   483
            raise error.Abort(_(b"cannot uncommit null changeset"))
2725
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   484
        if len(wctx.parents()) > 1:
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   485
            raise error.Abort(_(b"cannot uncommit while merging"))
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   486
        old = repo[b'.']
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   487
        rewriteutil.precheck(repo, [repo[b'.'].rev()], action=b'uncommit')
2725
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   488
        if len(old.parents()) > 1:
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   489
            raise error.Abort(_(b"cannot uncommit merge changeset"))
2725
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   490
        oldphase = old.phase()
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   491
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   492
        rev = None
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   493
        if opts.get('rev'):
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   494
            rev = scmutil.revsingle(repo, opts.get('rev'))
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   495
            ctx = repo[None]
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   496
            if ctx.p1() == rev or ctx.p2() == rev:
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   497
                raise error.Abort(_(b"cannot uncommit to parent changeset"))
2725
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   498
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   499
        onahead = old.rev() in repo.changelog.headrevs()
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   500
        disallowunstable = not obsolete.isenabled(repo,
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   501
                                                  obsolete.allowunstableopt)
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   502
        if disallowunstable and not onahead:
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   503
            raise error.Abort(_(b"cannot uncommit in the middle of a stack"))
2725
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   504
4752
8a73a8df63b6 py3: convert opts keys to bytes before passing to core APIs
Martin von Zweigbergk <martinvonz@google.com>
parents: 4747
diff changeset
   505
        match = scmutil.match(old, pats, pycompat.byteskwargs(opts))
4506
79de4e86d9a4 uncommit: abort if an explicitly given file cannot be uncommitted (BC)
Matt Harbison <matt_harbison@yahoo.com>
parents: 4499
diff changeset
   506
79de4e86d9a4 uncommit: abort if an explicitly given file cannot be uncommitted (BC)
Matt Harbison <matt_harbison@yahoo.com>
parents: 4499
diff changeset
   507
        # Check all explicitly given files; abort if there's a problem.
79de4e86d9a4 uncommit: abort if an explicitly given file cannot be uncommitted (BC)
Matt Harbison <matt_harbison@yahoo.com>
parents: 4499
diff changeset
   508
        if match.files():
79de4e86d9a4 uncommit: abort if an explicitly given file cannot be uncommitted (BC)
Matt Harbison <matt_harbison@yahoo.com>
parents: 4499
diff changeset
   509
            s = old.status(old.p1(), match, listclean=True)
79de4e86d9a4 uncommit: abort if an explicitly given file cannot be uncommitted (BC)
Matt Harbison <matt_harbison@yahoo.com>
parents: 4499
diff changeset
   510
            eligible = set(s.added) | set(s.modified) | set(s.removed)
79de4e86d9a4 uncommit: abort if an explicitly given file cannot be uncommitted (BC)
Matt Harbison <matt_harbison@yahoo.com>
parents: 4499
diff changeset
   511
79de4e86d9a4 uncommit: abort if an explicitly given file cannot be uncommitted (BC)
Matt Harbison <matt_harbison@yahoo.com>
parents: 4499
diff changeset
   512
            badfiles = set(match.files()) - eligible
79de4e86d9a4 uncommit: abort if an explicitly given file cannot be uncommitted (BC)
Matt Harbison <matt_harbison@yahoo.com>
parents: 4499
diff changeset
   513
79de4e86d9a4 uncommit: abort if an explicitly given file cannot be uncommitted (BC)
Matt Harbison <matt_harbison@yahoo.com>
parents: 4499
diff changeset
   514
            # Naming a parent directory of an eligible file is OK, even
79de4e86d9a4 uncommit: abort if an explicitly given file cannot be uncommitted (BC)
Matt Harbison <matt_harbison@yahoo.com>
parents: 4499
diff changeset
   515
            # if not everything tracked in that directory can be
79de4e86d9a4 uncommit: abort if an explicitly given file cannot be uncommitted (BC)
Matt Harbison <matt_harbison@yahoo.com>
parents: 4499
diff changeset
   516
            # uncommitted.
79de4e86d9a4 uncommit: abort if an explicitly given file cannot be uncommitted (BC)
Matt Harbison <matt_harbison@yahoo.com>
parents: 4499
diff changeset
   517
            if badfiles:
4956
0fe5d74134d6 compat: compatibility for pathuril.dirs vs util.dirs
Anton Shestakov <av6@dwimlabs.net>
parents: 4915
diff changeset
   518
                badfiles -= set([f for f in compat.dirs(eligible)])
4506
79de4e86d9a4 uncommit: abort if an explicitly given file cannot be uncommitted (BC)
Matt Harbison <matt_harbison@yahoo.com>
parents: 4499
diff changeset
   519
79de4e86d9a4 uncommit: abort if an explicitly given file cannot be uncommitted (BC)
Matt Harbison <matt_harbison@yahoo.com>
parents: 4499
diff changeset
   520
            try:
79de4e86d9a4 uncommit: abort if an explicitly given file cannot be uncommitted (BC)
Matt Harbison <matt_harbison@yahoo.com>
parents: 4499
diff changeset
   521
                uipathfn = scmutil.getuipathfn(repo)
79de4e86d9a4 uncommit: abort if an explicitly given file cannot be uncommitted (BC)
Matt Harbison <matt_harbison@yahoo.com>
parents: 4499
diff changeset
   522
            except AttributeError:
5193
a4d081923c81 compat: update hg-X.Y compat comments and test them
Anton Shestakov <av6@dwimlabs.net>
parents: 5185
diff changeset
   523
                # hg <= 4.9 (e6ec0737b706)
a4d081923c81 compat: update hg-X.Y compat comments and test them
Anton Shestakov <av6@dwimlabs.net>
parents: 5185
diff changeset
   524
                uipathfn = match.rel
4506
79de4e86d9a4 uncommit: abort if an explicitly given file cannot be uncommitted (BC)
Matt Harbison <matt_harbison@yahoo.com>
parents: 4499
diff changeset
   525
79de4e86d9a4 uncommit: abort if an explicitly given file cannot be uncommitted (BC)
Matt Harbison <matt_harbison@yahoo.com>
parents: 4499
diff changeset
   526
            for f in sorted(badfiles):
79de4e86d9a4 uncommit: abort if an explicitly given file cannot be uncommitted (BC)
Matt Harbison <matt_harbison@yahoo.com>
parents: 4499
diff changeset
   527
                if f in s.clean:
79de4e86d9a4 uncommit: abort if an explicitly given file cannot be uncommitted (BC)
Matt Harbison <matt_harbison@yahoo.com>
parents: 4499
diff changeset
   528
                    hint = _(b"file was not changed in working directory "
79de4e86d9a4 uncommit: abort if an explicitly given file cannot be uncommitted (BC)
Matt Harbison <matt_harbison@yahoo.com>
parents: 4499
diff changeset
   529
                             b"parent")
79de4e86d9a4 uncommit: abort if an explicitly given file cannot be uncommitted (BC)
Matt Harbison <matt_harbison@yahoo.com>
parents: 4499
diff changeset
   530
                elif repo.wvfs.exists(f):
79de4e86d9a4 uncommit: abort if an explicitly given file cannot be uncommitted (BC)
Matt Harbison <matt_harbison@yahoo.com>
parents: 4499
diff changeset
   531
                    hint = _(b"file was untracked in working directory parent")
79de4e86d9a4 uncommit: abort if an explicitly given file cannot be uncommitted (BC)
Matt Harbison <matt_harbison@yahoo.com>
parents: 4499
diff changeset
   532
                else:
79de4e86d9a4 uncommit: abort if an explicitly given file cannot be uncommitted (BC)
Matt Harbison <matt_harbison@yahoo.com>
parents: 4499
diff changeset
   533
                    hint = _(b"file does not exist")
79de4e86d9a4 uncommit: abort if an explicitly given file cannot be uncommitted (BC)
Matt Harbison <matt_harbison@yahoo.com>
parents: 4499
diff changeset
   534
79de4e86d9a4 uncommit: abort if an explicitly given file cannot be uncommitted (BC)
Matt Harbison <matt_harbison@yahoo.com>
parents: 4499
diff changeset
   535
                raise error.Abort(_(b'cannot uncommit "%s"')
79de4e86d9a4 uncommit: abort if an explicitly given file cannot be uncommitted (BC)
Matt Harbison <matt_harbison@yahoo.com>
parents: 4499
diff changeset
   536
                                  % uipathfn(f), hint=hint)
79de4e86d9a4 uncommit: abort if an explicitly given file cannot be uncommitted (BC)
Matt Harbison <matt_harbison@yahoo.com>
parents: 4499
diff changeset
   537
2725
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   538
        # Recommit the filtered changeset
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   539
        tr = repo.transaction(b'uncommit')
2941
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   540
        if interactive:
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   541
            opts['all'] = True
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   542
            newid = _interactiveuncommit(ui, repo, old, match)
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   543
        else:
2940
89b205e5271e uncommit: pre-indent a block in if-True for the next patch
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2923
diff changeset
   544
            newid = None
89b205e5271e uncommit: pre-indent a block in if-True for the next patch
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2923
diff changeset
   545
            includeorexclude = opts.get('include') or opts.get('exclude')
89b205e5271e uncommit: pre-indent a block in if-True for the next patch
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2923
diff changeset
   546
            if (pats or includeorexclude or opts.get('all')):
89b205e5271e uncommit: pre-indent a block in if-True for the next patch
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2923
diff changeset
   547
                if not (opts['message'] or opts['logfile']):
89b205e5271e uncommit: pre-indent a block in if-True for the next patch
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2923
diff changeset
   548
                    opts['message'] = old.description()
4752
8a73a8df63b6 py3: convert opts keys to bytes before passing to core APIs
Martin von Zweigbergk <martinvonz@google.com>
parents: 4747
diff changeset
   549
                message = cmdutil.logmessage(ui, pycompat.byteskwargs(opts))
2940
89b205e5271e uncommit: pre-indent a block in if-True for the next patch
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2923
diff changeset
   550
                newid = _commitfiltered(repo, old, match, target=rev,
89b205e5271e uncommit: pre-indent a block in if-True for the next patch
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2923
diff changeset
   551
                                        message=message, user=opts.get('user'),
89b205e5271e uncommit: pre-indent a block in if-True for the next patch
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2923
diff changeset
   552
                                        date=opts.get('date'))
89b205e5271e uncommit: pre-indent a block in if-True for the next patch
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2923
diff changeset
   553
            if newid is None:
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   554
                raise error.Abort(_(b'nothing to uncommit'),
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   555
                                  hint=_(b"use --all to uncommit all files"))
2941
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   556
3220
f0f4cc2febac uncommit: add support for storing a note in obsmarker
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3219
diff changeset
   557
        # metadata to be stored in obsmarker
f0f4cc2febac uncommit: add support for storing a note in obsmarker
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3219
diff changeset
   558
        metadata = {}
f0f4cc2febac uncommit: add support for storing a note in obsmarker
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3219
diff changeset
   559
        if opts.get('note'):
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   560
            metadata[b'note'] = opts['note']
3220
f0f4cc2febac uncommit: add support for storing a note in obsmarker
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3219
diff changeset
   561
4373
11bbf4dc694f uncommit: use scmtutil.cleanupnodes()
Martin von Zweigbergk <martinvonz@google.com>
parents: 4372
diff changeset
   562
        replacements = {old.node(): [newid]}
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   563
        scmutil.cleanupnodes(repo, replacements, operation=b"uncommit",
4373
11bbf4dc694f uncommit: use scmtutil.cleanupnodes()
Martin von Zweigbergk <martinvonz@google.com>
parents: 4372
diff changeset
   564
                             metadata=metadata)
2725
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   565
        phases.retractboundary(repo, tr, oldphase, [newid])
3389
eacf6149b678 uncommit: add a new flag `--revert` to discard wdir changes after uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3298
diff changeset
   566
        if opts.get('revert'):
eacf6149b678 uncommit: add a new flag `--revert` to discard wdir changes after uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3298
diff changeset
   567
            hg.updaterepo(repo, newid, True)
eacf6149b678 uncommit: add a new flag `--revert` to discard wdir changes after uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3298
diff changeset
   568
        else:
5096
6742ce189373 compat: add a context manager that calls _quick_access_changeid_invalidate()
Anton Shestakov <av6@dwimlabs.net>
parents: 5093
diff changeset
   569
            with repo.dirstate.parentchange(), compat.parentchange(repo):
3389
eacf6149b678 uncommit: add a new flag `--revert` to discard wdir changes after uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3298
diff changeset
   570
                repo.dirstate.setparents(newid, node.nullid)
eacf6149b678 uncommit: add a new flag `--revert` to discard wdir changes after uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3298
diff changeset
   571
                _uncommitdirstate(repo, old, match, interactive)
2725
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   572
        if not repo[newid].files():
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   573
            ui.warn(_(b"new changeset is empty\n"))
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   574
            ui.status(_(b"(use 'hg prune .' to remove it)\n"))
2725
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   575
        tr.close()
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   576
    finally:
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   577
        lockmod.release(tr, lock, wlock)
2760
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   578
2941
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   579
def _interactiveuncommit(ui, repo, old, match):
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   580
    """ The function which contains all the logic for interactively uncommiting
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   581
    a commit. This function makes a temporary commit with the chunks which user
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   582
    selected to uncommit. After that the diff of the parent and that commit is
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   583
    applied to the working directory and committed again which results in the
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   584
    new commit which should be one after uncommitted.
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   585
    """
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   586
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   587
    # create a temporary commit with hunks user selected
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   588
    tempnode = _createtempcommit(ui, repo, old, match)
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   589
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   590
    diffopts = patch.difffeatureopts(repo.ui, whitespace=True)
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   591
    diffopts.nodates = True
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   592
    diffopts.git = True
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   593
    fp = stringio()
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   594
    for chunk, label in patch.diffui(repo, tempnode, old.node(), None,
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   595
                                     opts=diffopts):
4463
7d54a538dd1e evolve: unindent some lines caught by flake8
Anton Shestakov <av6@dwimlabs.net>
parents: 4446
diff changeset
   596
        fp.write(chunk)
2941
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   597
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   598
    fp.seek(0)
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   599
    newnode = _patchtocommit(ui, repo, old, fp)
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   600
    # creating obs marker temp -> ()
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   601
    obsolete.createmarkers(repo, [(repo[tempnode], ())], operation=b"uncommit")
2941
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   602
    return newnode
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   603
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   604
def _createtempcommit(ui, repo, old, match):
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   605
    """ Creates a temporary commit for `uncommit --interative` which contains
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   606
    the hunks which were selected by the user to uncommit.
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   607
    """
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   608
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   609
    pold = old.p1()
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   610
    # The logic to interactively selecting something copied from
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   611
    # cmdutil.revert()
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   612
    diffopts = patch.difffeatureopts(repo.ui, whitespace=True)
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   613
    diffopts.nodates = True
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   614
    diffopts.git = True
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   615
    diff = patch.diff(repo, pold.node(), old.node(), match, opts=diffopts)
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   616
    originalchunks = patch.parsepatch(diff)
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   617
    # XXX: The interactive selection is buggy and does not let you
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   618
    # uncommit a removed file partially.
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   619
    # TODO: wrap the operations in mercurial/patch.py and mercurial/crecord.py
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   620
    # to add uncommit as an operation taking care of BC.
4499
90f94231db5d evolve: compat patch for recordfilter change in mercurial
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4491
diff changeset
   621
    try:
90f94231db5d evolve: compat patch for recordfilter change in mercurial
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4491
diff changeset
   622
        chunks, opts = cmdutil.recordfilter(repo.ui, originalchunks, match,
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   623
                                            operation=b'discard')
4499
90f94231db5d evolve: compat patch for recordfilter change in mercurial
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4491
diff changeset
   624
    except TypeError:
90f94231db5d evolve: compat patch for recordfilter change in mercurial
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4491
diff changeset
   625
        # hg <= 4.9 (db72f9f6580e)
90f94231db5d evolve: compat patch for recordfilter change in mercurial
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4491
diff changeset
   626
        chunks, opts = cmdutil.recordfilter(repo.ui, originalchunks,
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   627
                                            operation=b'discard')
2941
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   628
    if not chunks:
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   629
        raise error.Abort(_(b"nothing selected to uncommit"))
2941
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   630
    fp = stringio()
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   631
    for c in chunks:
4463
7d54a538dd1e evolve: unindent some lines caught by flake8
Anton Shestakov <av6@dwimlabs.net>
parents: 4446
diff changeset
   632
        c.write(fp)
2941
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   633
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   634
    fp.seek(0)
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   635
    oldnode = node.hex(old.node())[:12]
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   636
    message = b'temporary commit for uncommiting %s' % oldnode
2941
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   637
    tempnode = _patchtocommit(ui, repo, old, fp, message, oldnode)
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   638
    return tempnode
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   639
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   640
def _patchtocommit(ui, repo, old, fp, message=None, extras=None):
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   641
    """ A function which will apply the patch to the working directory and
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   642
    make a commit whose parents are same as that of old argument. The message
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   643
    argument tells us whether to use the message of the old commit or a
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   644
    different message which is passed. Returns the node of new commit made.
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   645
    """
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   646
    pold = old.p1()
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   647
    parents = (old.p1().node(), old.p2().node())
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   648
    date = old.date()
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   649
    branch = old.branch()
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   650
    user = old.user()
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   651
    extra = old.extra()
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   652
    if extras:
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   653
        extra[b'uncommit_source'] = extras
2941
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   654
    if not message:
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   655
        message = old.description()
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   656
    store = patch.filestore()
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   657
    try:
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   658
        files = set()
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   659
        try:
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   660
            patch.patchrepo(ui, repo, pold, store, fp, 1, b'',
2941
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   661
                            files=files, eolmode=None)
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   662
        except patch.PatchError as err:
4759
f30c1fab7155 py3: convert exceptions to bytes using pycompat.bytestr()
Martin von Zweigbergk <martinvonz@google.com>
parents: 4758
diff changeset
   663
            raise error.Abort(pycompat.bytestr(err))
2941
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   664
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   665
        finally:
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   666
            del fp
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   667
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   668
        memctx = context.memctx(repo, parents, message, files=files,
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   669
                                filectxfn=store,
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   670
                                user=user,
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   671
                                date=date,
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   672
                                branch=branch,
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   673
                                extra=extra)
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   674
        newcm = memctx.commit()
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   675
    finally:
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   676
        store.close()
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   677
    return newcm
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   678
2760
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   679
@eh.command(
4715
12c8b24757f4 py3: use byte strings for @command registrations
Martin von Zweigbergk <martinvonz@google.com>
parents: 4714
diff changeset
   680
    b'fold|squash',
12c8b24757f4 py3: use byte strings for @command registrations
Martin von Zweigbergk <martinvonz@google.com>
parents: 4714
diff changeset
   681
    [(b'r', b'rev', [], _(b"revision to fold"), _(b'REV')),
12c8b24757f4 py3: use byte strings for @command registrations
Martin von Zweigbergk <martinvonz@google.com>
parents: 4714
diff changeset
   682
     (b'', b'exact', None, _(b"only fold specified revisions")),
12c8b24757f4 py3: use byte strings for @command registrations
Martin von Zweigbergk <martinvonz@google.com>
parents: 4714
diff changeset
   683
     (b'', b'from', None, _(b"fold revisions linearly to working copy parent")),
12c8b24757f4 py3: use byte strings for @command registrations
Martin von Zweigbergk <martinvonz@google.com>
parents: 4714
diff changeset
   684
     (b'n', b'note', b'', _(b'store a note on fold'), _(b'TEXT')),
12c8b24757f4 py3: use byte strings for @command registrations
Martin von Zweigbergk <martinvonz@google.com>
parents: 4714
diff changeset
   685
     ] + commitopts + commitopts2 + commitopts3,
4915
5113b38cb677 evolve: use more often seen metavariables in command synopsis strings
Anton Shestakov <av6@dwimlabs.net>
parents: 4898
diff changeset
   686
    _(b'hg fold [OPTION]... [-r] REV...'),
4894
f9743b13de6d help: categorizing evolve and topic commands
Rodrigo Damazio <rdamazio@google.com>
parents: 4891
diff changeset
   687
    helpbasic=True,
f9743b13de6d help: categorizing evolve and topic commands
Rodrigo Damazio <rdamazio@google.com>
parents: 4891
diff changeset
   688
    **compat.helpcategorykwargs('CATEGORY_CHANGE_MANAGEMENT'))
2761
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   689
def fold(ui, repo, *revs, **opts):
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   690
    """fold multiple revisions into a single one
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   691
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   692
    With --from, folds all the revisions linearly between the given revisions
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   693
    and the parent of the working directory.
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   694
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   695
    With --exact, folds only the specified revisions while ignoring the
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   696
    parent of the working directory. In this case, the given revisions must
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   697
    form a linear unbroken chain.
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   698
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   699
    .. container:: verbose
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   700
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   701
     Some examples:
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   702
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   703
     - Fold the current revision with its parent::
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   704
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   705
         hg fold --from .^
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   706
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   707
     - Fold all draft revisions with working directory parent::
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   708
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   709
         hg fold --from 'draft()'
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   710
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   711
       See :hg:`help phases` for more about draft revisions and
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   712
       :hg:`help revsets` for more about the `draft()` keyword
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   713
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   714
     - Fold revisions between 3 and 6 with the working directory parent::
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   715
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   716
         hg fold --from 3::6
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   717
2923
8c2d3c474fc6 doc: make paragraphs before example code end with "::" for reST syntax
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 2790
diff changeset
   718
     - Fold revisions 3 and 4::
2761
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   719
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   720
        hg fold "3 + 4" --exact
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   721
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   722
     - Only fold revisions linearly between foo and @::
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   723
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   724
         hg fold foo::@ --exact
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   725
    """
3283
039c4b8dc3ed obsnote: warn if user try to store a note in obsmarker on an older hg
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3271
diff changeset
   726
    _checknotesize(ui, opts)
2768
85e5a56db776 fold: add support for the -D and -U options
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2766
diff changeset
   727
    _resolveoptions(ui, opts)
2761
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   728
    revs = list(revs)
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   729
    revs.extend(opts['rev'])
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   730
    if not revs:
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   731
        raise error.Abort(_(b'no revisions specified'))
2761
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   732
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   733
    revs = scmutil.revrange(repo, revs)
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   734
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   735
    if opts['from'] and opts['exact']:
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   736
        raise error.Abort(_(b'cannot use both --from and --exact'))
2761
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   737
    elif opts['from']:
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   738
        # Try to extend given revision starting from the working directory
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   739
        extrevs = repo.revs(b'(%ld::.) or (.::%ld)', revs, revs)
2761
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   740
        discardedrevs = [r for r in revs if r not in extrevs]
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   741
        if discardedrevs:
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   742
            msg = _(b"cannot fold non-linear revisions")
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   743
            hint = _(b"given revisions are unrelated to parent of working"
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   744
                     b" directory")
2761
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   745
            raise error.Abort(msg, hint=hint)
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   746
        revs = extrevs
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   747
    elif opts['exact']:
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   748
        # Nothing to do; "revs" is already set correctly
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   749
        pass
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   750
    else:
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   751
        raise error.Abort(_(b'must specify either --from or --exact'))
2761
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   752
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   753
    if not revs:
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   754
        raise error.Abort(_(b'specified revisions evaluate to an empty set'),
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   755
                          hint=_(b'use different revision arguments'))
2761
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   756
    elif len(revs) == 1:
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   757
        ui.write_err(_(b'single revision specified, nothing to fold\n'))
2761
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   758
        return 1
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   759
4340
5300be965515 fold: sort concatenated commit messages in topological order
Martin von Zweigbergk <martinvonz@google.com>
parents: 4300
diff changeset
   760
    # Sort so combined commit message of `hg fold --exact -r . -r .^` is
5300be965515 fold: sort concatenated commit messages in topological order
Martin von Zweigbergk <martinvonz@google.com>
parents: 4300
diff changeset
   761
    # in topological order.
5300be965515 fold: sort concatenated commit messages in topological order
Martin von Zweigbergk <martinvonz@google.com>
parents: 4300
diff changeset
   762
    revs.sort()
5300be965515 fold: sort concatenated commit messages in topological order
Martin von Zweigbergk <martinvonz@google.com>
parents: 4300
diff changeset
   763
2761
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   764
    wlock = lock = None
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   765
    try:
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   766
        wlock = repo.wlock()
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   767
        lock = repo.lock()
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   768
4723
41885988921e fold: allow operations on merge commits with some conditions
Anton Shestakov <av6@dwimlabs.net>
parents: 4721
diff changeset
   769
        root, head, p2 = rewriteutil.foldcheck(repo, revs)
2761
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   770
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   771
        tr = repo.transaction(b'fold')
2761
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   772
        try:
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   773
            commitopts = opts.copy()
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   774
            allctx = [repo[r] for r in revs]
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   775
            targetphase = max(c.phase() for c in allctx)
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   776
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   777
            if commitopts.get('message') or commitopts.get('logfile'):
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   778
                commitopts['edit'] = False
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   779
            else:
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   780
                msgs = [b"HG: This is a fold of %d changesets." % len(allctx)]
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   781
                msgs += [b"HG: Commit message of changeset %d.\n\n%s\n" %
2761
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   782
                         (c.rev(), c.description()) for c in allctx]
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   783
                commitopts['message'] = b"\n".join(msgs)
2761
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   784
                commitopts['edit'] = True
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   785
3219
b73bd280b21c fold: add support for storing a note in obsmarker
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3218
diff changeset
   786
            metadata = {}
b73bd280b21c fold: add support for storing a note in obsmarker
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3218
diff changeset
   787
            if opts.get('note'):
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   788
                metadata[b'note'] = opts['note']
3219
b73bd280b21c fold: add support for storing a note in obsmarker
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3218
diff changeset
   789
4723
41885988921e fold: allow operations on merge commits with some conditions
Anton Shestakov <av6@dwimlabs.net>
parents: 4721
diff changeset
   790
            updates = allctx[:]
41885988921e fold: allow operations on merge commits with some conditions
Anton Shestakov <av6@dwimlabs.net>
parents: 4721
diff changeset
   791
            if p2 is not None and root.p2() != p2:
41885988921e fold: allow operations on merge commits with some conditions
Anton Shestakov <av6@dwimlabs.net>
parents: 4721
diff changeset
   792
                updates.append(p2)
4752
8a73a8df63b6 py3: convert opts keys to bytes before passing to core APIs
Martin von Zweigbergk <martinvonz@google.com>
parents: 4747
diff changeset
   793
            commitopts = pycompat.byteskwargs(commitopts)
4723
41885988921e fold: allow operations on merge commits with some conditions
Anton Shestakov <av6@dwimlabs.net>
parents: 4721
diff changeset
   794
            newid, unusedvariable = rewriteutil.rewrite(repo, root, updates,
2761
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   795
                                                        head,
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   796
                                                        [root.p1().node(),
4723
41885988921e fold: allow operations on merge commits with some conditions
Anton Shestakov <av6@dwimlabs.net>
parents: 4721
diff changeset
   797
                                                         p2.node()],
2761
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   798
                                                        commitopts=commitopts)
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   799
            phases.retractboundary(repo, tr, targetphase, [newid])
5126
d57f4326623e fold: replace a compat condition with a less specific proxy
Martin von Zweigbergk <martinvonz@google.com>
parents: 5114
diff changeset
   800
            # Use this condition as a proxy since the commit we care about
d57f4326623e fold: replace a compat condition with a less specific proxy
Martin von Zweigbergk <martinvonz@google.com>
parents: 5114
diff changeset
   801
            # (b99903534e06) didn't change any signatures.
d57f4326623e fold: replace a compat condition with a less specific proxy
Martin von Zweigbergk <martinvonz@google.com>
parents: 5114
diff changeset
   802
            if util.safehasattr(scmutil, 'nullrev'):
5113
a66c9fc9c503 fold: actually track folds using fold-* metadata in obsmarkers
Anton Shestakov <av6@dwimlabs.net>
parents: 5111
diff changeset
   803
                replacements = {tuple(ctx.node() for ctx in allctx): [newid]}
a66c9fc9c503 fold: actually track folds using fold-* metadata in obsmarkers
Anton Shestakov <av6@dwimlabs.net>
parents: 5111
diff changeset
   804
                scmutil.cleanupnodes(repo, replacements, operation=b"fold",
a66c9fc9c503 fold: actually track folds using fold-* metadata in obsmarkers
Anton Shestakov <av6@dwimlabs.net>
parents: 5111
diff changeset
   805
                                     metadata=metadata)
a66c9fc9c503 fold: actually track folds using fold-* metadata in obsmarkers
Anton Shestakov <av6@dwimlabs.net>
parents: 5111
diff changeset
   806
            else:
a66c9fc9c503 fold: actually track folds using fold-* metadata in obsmarkers
Anton Shestakov <av6@dwimlabs.net>
parents: 5111
diff changeset
   807
                # hg <= 4.7 (b99903534e06)
a66c9fc9c503 fold: actually track folds using fold-* metadata in obsmarkers
Anton Shestakov <av6@dwimlabs.net>
parents: 5111
diff changeset
   808
                replacements = {ctx.node(): [newid] for ctx in allctx}
a66c9fc9c503 fold: actually track folds using fold-* metadata in obsmarkers
Anton Shestakov <av6@dwimlabs.net>
parents: 5111
diff changeset
   809
                scmutil.cleanupnodes(repo, replacements, operation=b"fold",
a66c9fc9c503 fold: actually track folds using fold-* metadata in obsmarkers
Anton Shestakov <av6@dwimlabs.net>
parents: 5111
diff changeset
   810
                                     metadata=metadata)
2761
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   811
            tr.close()
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   812
        finally:
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   813
            tr.release()
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   814
        ui.status(b'%i changesets folded\n' % len(revs))
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   815
        if repo[b'.'].rev() in revs:
2761
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   816
            hg.update(repo, newid)
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   817
    finally:
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   818
        lockmod.release(lock, wlock)
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   819
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   820
@eh.command(
4715
12c8b24757f4 py3: use byte strings for @command registrations
Martin von Zweigbergk <martinvonz@google.com>
parents: 4714
diff changeset
   821
    b'metaedit',
12c8b24757f4 py3: use byte strings for @command registrations
Martin von Zweigbergk <martinvonz@google.com>
parents: 4714
diff changeset
   822
    [(b'r', b'rev', [], _(b"revision to edit"), _(b'REV')),
12c8b24757f4 py3: use byte strings for @command registrations
Martin von Zweigbergk <martinvonz@google.com>
parents: 4714
diff changeset
   823
     (b'', b'fold', None, _(b"also fold specified revisions into one")),
12c8b24757f4 py3: use byte strings for @command registrations
Martin von Zweigbergk <martinvonz@google.com>
parents: 4714
diff changeset
   824
     (b'n', b'note', b'', _(b'store a note on metaedit'), _(b'TEXT')),
12c8b24757f4 py3: use byte strings for @command registrations
Martin von Zweigbergk <martinvonz@google.com>
parents: 4714
diff changeset
   825
     ] + commitopts + commitopts2 + commitopts3,
4921
a7c01a2a3974 branching: merge with stable
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4906 4915
diff changeset
   826
    _(b'hg metaedit [OPTION]... [[-r] REV]...'),
4894
f9743b13de6d help: categorizing evolve and topic commands
Rodrigo Damazio <rdamazio@google.com>
parents: 4891
diff changeset
   827
    **compat.helpcategorykwargs('CATEGORY_CHANGE_MANAGEMENT'))
2760
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   828
def metaedit(ui, repo, *revs, **opts):
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   829
    """edit commit information
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   830
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   831
    Edits the commit information for the specified revisions. By default, edits
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   832
    commit information for the working directory parent.
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   833
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   834
    With --fold, also folds multiple revisions into one if necessary. In this
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   835
    case, the given revisions must form a linear unbroken chain.
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   836
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   837
    .. container:: verbose
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   838
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   839
     Some examples:
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   840
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   841
     - Edit the commit message for the working directory parent::
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   842
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   843
         hg metaedit
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   844
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   845
     - Change the username for the working directory parent::
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   846
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   847
         hg metaedit --user 'New User <new-email@example.com>'
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   848
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   849
     - Combine all draft revisions that are ancestors of foo but not of @ into
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   850
       one::
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   851
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   852
         hg metaedit --fold 'draft() and only(foo,@)'
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   853
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   854
       See :hg:`help phases` for more about draft revisions, and
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   855
       :hg:`help revsets` for more about the `draft()` and `only()` keywords.
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   856
    """
3283
039c4b8dc3ed obsnote: warn if user try to store a note in obsmarker on an older hg
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3271
diff changeset
   857
    _checknotesize(ui, opts)
2769
b96349ae3e2a metaedit: add support for the -D and -U options
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2768
diff changeset
   858
    _resolveoptions(ui, opts)
2760
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   859
    revs = list(revs)
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   860
    revs.extend(opts['rev'])
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   861
    if not revs:
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   862
        if opts['fold']:
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   863
            raise error.Abort(_(b'revisions must be specified with --fold'))
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   864
        revs = [b'.']
2760
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   865
4642
a7c4f163656b cmdrewrite: use context manager for some locks and transactions
Martin von Zweigbergk <martinvonz@google.com>
parents: 4640
diff changeset
   866
    with repo.wlock(), repo.lock():
2760
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   867
        revs = scmutil.revrange(repo, revs)
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   868
        if not opts['fold'] and len(revs) > 1:
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   869
            # TODO: handle multiple revisions. This is somewhat tricky because
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   870
            # if we want to edit a series of commits:
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   871
            #
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   872
            #   a ---- b ---- c
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   873
            #
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   874
            # we need to rewrite a first, then directly rewrite b on top of the
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   875
            # new a, then rewrite c on top of the new b. So we need to handle
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   876
            # revisions in topological order.
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   877
            raise error.Abort(_(b'editing multiple revisions without --fold is '
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   878
                                b'not currently supported'))
2760
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   879
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   880
        if opts['fold']:
4723
41885988921e fold: allow operations on merge commits with some conditions
Anton Shestakov <av6@dwimlabs.net>
parents: 4721
diff changeset
   881
            root, head, p2 = rewriteutil.foldcheck(repo, revs)
2760
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   882
        else:
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   883
            if repo.revs(b"%ld and public()", revs):
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   884
                raise error.Abort(_(b'cannot edit commit information for public '
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   885
                                    b'revisions'))
2760
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   886
            newunstable = rewriteutil.disallowednewunstable(repo, revs)
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   887
            if newunstable:
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   888
                msg = _(b'cannot edit commit information in the middle'
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   889
                        b' of a stack')
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   890
                hint = _(b'%s will become unstable and new unstable changes'
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   891
                         b' are not allowed')
2760
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   892
                hint %= repo[newunstable.first()]
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   893
                raise error.Abort(msg, hint=hint)
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   894
            root = head = repo[revs.first()]
4724
77bf84025dd5 metaedit: allow operations on merge commits with some conditions
Anton Shestakov <av6@dwimlabs.net>
parents: 4723
diff changeset
   895
            p2 = root.p2()
2760
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   896
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   897
        wctx = repo[None]
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   898
        p1 = wctx.p1()
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   899
        tr = repo.transaction(b'metaedit')
2760
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   900
        newp1 = None
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   901
        try:
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   902
            commitopts = opts.copy()
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   903
            allctx = [repo[r] for r in revs]
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   904
            targetphase = max(c.phase() for c in allctx)
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   905
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   906
            if commitopts.get('message') or commitopts.get('logfile'):
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   907
                commitopts['edit'] = False
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   908
            else:
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   909
                if opts['fold']:
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   910
                    msgs = [b"HG: This is a fold of %d changesets." % len(allctx)]
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   911
                    msgs += [b"HG: Commit message of changeset %d.\n\n%s\n" %
2760
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   912
                             (c.rev(), c.description()) for c in allctx]
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   913
                else:
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   914
                    msgs = [head.description()]
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   915
                commitopts['message'] = b"\n".join(msgs)
2760
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   916
                commitopts['edit'] = True
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   917
4724
77bf84025dd5 metaedit: allow operations on merge commits with some conditions
Anton Shestakov <av6@dwimlabs.net>
parents: 4723
diff changeset
   918
            updates = allctx[:]
77bf84025dd5 metaedit: allow operations on merge commits with some conditions
Anton Shestakov <av6@dwimlabs.net>
parents: 4723
diff changeset
   919
            if p2 is not None and (root.p2() != p2 or not opts['fold']):
77bf84025dd5 metaedit: allow operations on merge commits with some conditions
Anton Shestakov <av6@dwimlabs.net>
parents: 4723
diff changeset
   920
                updates.append(p2)
4898
9936aecc9ea3 metaedit: don't change commit date by default (issue5994)
Anton Shestakov <av6@dwimlabs.net>
parents: 4891
diff changeset
   921
            if not commitopts['fold'] and not commitopts['date']:
9936aecc9ea3 metaedit: don't change commit date by default (issue5994)
Anton Shestakov <av6@dwimlabs.net>
parents: 4891
diff changeset
   922
                commitopts['date'] = root.date()
4752
8a73a8df63b6 py3: convert opts keys to bytes before passing to core APIs
Martin von Zweigbergk <martinvonz@google.com>
parents: 4747
diff changeset
   923
            commitopts = pycompat.byteskwargs(commitopts)
4724
77bf84025dd5 metaedit: allow operations on merge commits with some conditions
Anton Shestakov <av6@dwimlabs.net>
parents: 4723
diff changeset
   924
            newid, created = rewriteutil.rewrite(repo, root, updates, head,
2760
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   925
                                                 [root.p1().node(),
4724
77bf84025dd5 metaedit: allow operations on merge commits with some conditions
Anton Shestakov <av6@dwimlabs.net>
parents: 4723
diff changeset
   926
                                                  p2.node()],
2760
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   927
                                                 commitopts=commitopts)
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   928
            if created:
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   929
                if p1.rev() in revs:
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   930
                    newp1 = newid
3218
772cc931f085 metaedit: add support for storing a note in obsmarker
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3217
diff changeset
   931
                # metadata to be stored on obsmarker
772cc931f085 metaedit: add support for storing a note in obsmarker
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3217
diff changeset
   932
                metadata = {}
772cc931f085 metaedit: add support for storing a note in obsmarker
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3217
diff changeset
   933
                if opts.get('note'):
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   934
                    metadata[b'note'] = opts['note']
3218
772cc931f085 metaedit: add support for storing a note in obsmarker
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3217
diff changeset
   935
2760
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   936
                phases.retractboundary(repo, tr, targetphase, [newid])
5127
f4fae6359593 metaedit: replace a compat condition with a less specific proxy
Martin von Zweigbergk <martinvonz@google.com>
parents: 5126
diff changeset
   937
                # Use this condition as a proxy since the commit we care about
f4fae6359593 metaedit: replace a compat condition with a less specific proxy
Martin von Zweigbergk <martinvonz@google.com>
parents: 5126
diff changeset
   938
                # (6335c0de80fa) didn't change any signatures.
f4fae6359593 metaedit: replace a compat condition with a less specific proxy
Martin von Zweigbergk <martinvonz@google.com>
parents: 5126
diff changeset
   939
                if not util.safehasattr(context, 'bin'):
5114
08112f100d70 metaedit: actually track folds using fold-* metadata in obsmarkers
Anton Shestakov <av6@dwimlabs.net>
parents: 5113
diff changeset
   940
                    obsolete.createmarkers(repo, [(tuple(allctx), (repo[newid],))],
08112f100d70 metaedit: actually track folds using fold-* metadata in obsmarkers
Anton Shestakov <av6@dwimlabs.net>
parents: 5113
diff changeset
   941
                                           metadata=metadata, operation=b"metaedit")
08112f100d70 metaedit: actually track folds using fold-* metadata in obsmarkers
Anton Shestakov <av6@dwimlabs.net>
parents: 5113
diff changeset
   942
                else:
08112f100d70 metaedit: actually track folds using fold-* metadata in obsmarkers
Anton Shestakov <av6@dwimlabs.net>
parents: 5113
diff changeset
   943
                    # hg <= 4.7 (6335c0de80fa)
08112f100d70 metaedit: actually track folds using fold-* metadata in obsmarkers
Anton Shestakov <av6@dwimlabs.net>
parents: 5113
diff changeset
   944
                    obsolete.createmarkers(repo, [(ctx, (repo[newid],)) for ctx in allctx],
08112f100d70 metaedit: actually track folds using fold-* metadata in obsmarkers
Anton Shestakov <av6@dwimlabs.net>
parents: 5113
diff changeset
   945
                                           metadata=metadata, operation=b"metaedit")
2760
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   946
            else:
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   947
                ui.status(_(b"nothing changed\n"))
2760
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   948
            tr.close()
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   949
        finally:
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   950
            tr.release()
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   951
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   952
        if opts['fold']:
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   953
            ui.status(b'%i changesets folded\n' % len(revs))
2760
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   954
        if newp1 is not None:
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   955
            hg.update(repo, newp1)
2762
610581a2fb74 commands: move split to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2761
diff changeset
   956
2766
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
   957
metadataopts = [
4715
12c8b24757f4 py3: use byte strings for @command registrations
Martin von Zweigbergk <martinvonz@google.com>
parents: 4714
diff changeset
   958
    (b'd', b'date', b'',
12c8b24757f4 py3: use byte strings for @command registrations
Martin von Zweigbergk <martinvonz@google.com>
parents: 4714
diff changeset
   959
     _(b'record the specified date in metadata'), _(b'DATE')),
12c8b24757f4 py3: use byte strings for @command registrations
Martin von Zweigbergk <martinvonz@google.com>
parents: 4714
diff changeset
   960
    (b'u', b'user', b'',
12c8b24757f4 py3: use byte strings for @command registrations
Martin von Zweigbergk <martinvonz@google.com>
parents: 4714
diff changeset
   961
     _(b'record the specified user in metadata'), _(b'USER')),
2766
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
   962
]
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
   963
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
   964
def _getmetadata(**opts):
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
   965
    metadata = {}
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
   966
    date = opts.get('date')
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
   967
    user = opts.get('user')
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
   968
    if date:
5178
61e49d2654cc compat: directly use function from dateutil
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 5136
diff changeset
   969
        metadata[b'date'] = b'%i %i' % dateutil.parsedate(date)
2766
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
   970
    if user:
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   971
        metadata[b'user'] = user
2766
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
   972
    return metadata
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
   973
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
   974
@eh.command(
4715
12c8b24757f4 py3: use byte strings for @command registrations
Martin von Zweigbergk <martinvonz@google.com>
parents: 4714
diff changeset
   975
    b'prune|obsolete',
12c8b24757f4 py3: use byte strings for @command registrations
Martin von Zweigbergk <martinvonz@google.com>
parents: 4714
diff changeset
   976
    [(b'n', b'new', [], _(b"successor changeset (DEPRECATED)")),
4774
12a88b988875 branching: merge stable into default
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4759 4765
diff changeset
   977
     (b's', b'successor', [], _(b"successor changeset"), _(b'REV')),
4715
12c8b24757f4 py3: use byte strings for @command registrations
Martin von Zweigbergk <martinvonz@google.com>
parents: 4714
diff changeset
   978
     (b'r', b'rev', [], _(b"revisions to prune"), _(b'REV')),
12c8b24757f4 py3: use byte strings for @command registrations
Martin von Zweigbergk <martinvonz@google.com>
parents: 4714
diff changeset
   979
     (b'k', b'keep', None, _(b"does not modify working copy during prune")),
12c8b24757f4 py3: use byte strings for @command registrations
Martin von Zweigbergk <martinvonz@google.com>
parents: 4714
diff changeset
   980
     (b'n', b'note', b'', _(b'store a note on prune'), _(b'TEXT')),
12c8b24757f4 py3: use byte strings for @command registrations
Martin von Zweigbergk <martinvonz@google.com>
parents: 4714
diff changeset
   981
     (b'', b'pair', False, _(b"record a pairing, such as a rebase or divergence resolution "
12c8b24757f4 py3: use byte strings for @command registrations
Martin von Zweigbergk <martinvonz@google.com>
parents: 4714
diff changeset
   982
                             b"(pairing multiple precursors to multiple successors)")),
12c8b24757f4 py3: use byte strings for @command registrations
Martin von Zweigbergk <martinvonz@google.com>
parents: 4714
diff changeset
   983
     (b'', b'biject', False, _(b"alias to --pair (DEPRECATED)")),
12c8b24757f4 py3: use byte strings for @command registrations
Martin von Zweigbergk <martinvonz@google.com>
parents: 4714
diff changeset
   984
     (b'', b'fold', False,
4774
12a88b988875 branching: merge stable into default
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4759 4765
diff changeset
   985
      _(b"record a fold (multiple precursors, one successor)")),
4715
12c8b24757f4 py3: use byte strings for @command registrations
Martin von Zweigbergk <martinvonz@google.com>
parents: 4714
diff changeset
   986
     (b'', b'split', False,
4774
12a88b988875 branching: merge stable into default
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4759 4765
diff changeset
   987
      _(b"record a split (one precursor, multiple successors)")),
4715
12c8b24757f4 py3: use byte strings for @command registrations
Martin von Zweigbergk <martinvonz@google.com>
parents: 4714
diff changeset
   988
     (b'B', b'bookmark', [], _(b"remove revs only reachable from given"
12c8b24757f4 py3: use byte strings for @command registrations
Martin von Zweigbergk <martinvonz@google.com>
parents: 4714
diff changeset
   989
                               b" bookmark"), _(b'BOOKMARK'))] + metadataopts,
4915
5113b38cb677 evolve: use more often seen metavariables in command synopsis strings
Anton Shestakov <av6@dwimlabs.net>
parents: 4898
diff changeset
   990
    _(b'[OPTION]... [-r] REV...'),
4894
f9743b13de6d help: categorizing evolve and topic commands
Rodrigo Damazio <rdamazio@google.com>
parents: 4891
diff changeset
   991
    helpbasic=True,
f9743b13de6d help: categorizing evolve and topic commands
Rodrigo Damazio <rdamazio@google.com>
parents: 4891
diff changeset
   992
    **compat.helpcategorykwargs('CATEGORY_CHANGE_MANAGEMENT'))
2766
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
   993
# XXX -U  --noupdate option to prevent wc update and or bookmarks update ?
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
   994
def cmdprune(ui, repo, *revs, **opts):
3989
b60113460856 prune: improve help first line
Dan Villiom Podlaski Christiansen <dan@magenta.dk>
parents: 3988
diff changeset
   995
    """mark changesets as obsolete or succeeded by another changeset
2766
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
   996
3990
566fe891f1ec prune: more human-friendly documentation
Dan Villiom Podlaski Christiansen <dan@magenta.dk>
parents: 3989
diff changeset
   997
    Pruning changesets marks them obsolete, hiding them from the
566fe891f1ec prune: more human-friendly documentation
Dan Villiom Podlaski Christiansen <dan@magenta.dk>
parents: 3989
diff changeset
   998
    history log, provided they have no descendants. Otherwise, all
566fe891f1ec prune: more human-friendly documentation
Dan Villiom Podlaski Christiansen <dan@magenta.dk>
parents: 3989
diff changeset
   999
    such descendants that aren't themselves obsolete become
566fe891f1ec prune: more human-friendly documentation
Dan Villiom Podlaski Christiansen <dan@magenta.dk>
parents: 3989
diff changeset
  1000
    "unstable". Use :hg:`evolve` to handle this situation.
2766
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1001
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1002
    When you prune the parent of your working copy, Mercurial updates the working
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1003
    copy to a non-obsolete parent.
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1004
4763
184c64c81528 prune: spell --successor flag without any unnecessary shortcuts
Anton Shestakov <av6@dwimlabs.net>
parents: 4728
diff changeset
  1005
    You can use ``-s/--successor`` to tell Mercurial that a newer version
184c64c81528 prune: spell --successor flag without any unnecessary shortcuts
Anton Shestakov <av6@dwimlabs.net>
parents: 4728
diff changeset
  1006
    (successor) of the pruned changeset exists. Mercurial records successor
184c64c81528 prune: spell --successor flag without any unnecessary shortcuts
Anton Shestakov <av6@dwimlabs.net>
parents: 4728
diff changeset
  1007
    revisions in obsolescence markers.
2766
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1008
3988
3e4fbda6bdce prune: try to clarify the "multi" revisions case
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3987
diff changeset
  1009
    If you prune a single revision and specify multiple revisions in
4763
184c64c81528 prune: spell --successor flag without any unnecessary shortcuts
Anton Shestakov <av6@dwimlabs.net>
parents: 4728
diff changeset
  1010
    ``-s/--successor``, you are recording a "split" and must acknowledge it by
3988
3e4fbda6bdce prune: try to clarify the "multi" revisions case
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3987
diff changeset
  1011
    passing ``--split``. Similarly, when you prune multiple changesets with a
3e4fbda6bdce prune: try to clarify the "multi" revisions case
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3987
diff changeset
  1012
    single successor, you must pass the ``--fold`` option.
3987
b802003ffb50 prune: move help about biject after the one about split and fold
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3986
diff changeset
  1013
4765
c1d0faaa5b92 prune: fix small grammatical issues in help text
Kyle Lippincott <spectral@google.com>
parents: 4763
diff changeset
  1014
    If you want to supersede multiple revisions at the same time, use the
4045
821e4be40c6c prune: replace --biject with --pair
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3990
diff changeset
  1015
    ``--pair`` option to pair the pruned precursor and successor changesets.
3988
3e4fbda6bdce prune: try to clarify the "multi" revisions case
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3987
diff changeset
  1016
    This is commonly useful for resolving history divergence, or when someone
4765
c1d0faaa5b92 prune: fix small grammatical issues in help text
Kyle Lippincott <spectral@google.com>
parents: 4763
diff changeset
  1017
    else edits history without obsolescence enabled.
4844
2488ec1bd22f docs: add example for the `prune` command
Raphaël Gomès <rgomes@octobus.net>
parents: 4841
diff changeset
  1018
2488ec1bd22f docs: add example for the `prune` command
Raphaël Gomès <rgomes@octobus.net>
parents: 4841
diff changeset
  1019
    .. container:: verbose
2488ec1bd22f docs: add example for the `prune` command
Raphaël Gomès <rgomes@octobus.net>
parents: 4841
diff changeset
  1020
2488ec1bd22f docs: add example for the `prune` command
Raphaël Gomès <rgomes@octobus.net>
parents: 4841
diff changeset
  1021
        ``hg prune A::B -s C::D --pair`` will mark all revisions in the A::B
2488ec1bd22f docs: add example for the `prune` command
Raphaël Gomès <rgomes@octobus.net>
parents: 4841
diff changeset
  1022
        range as superseded by the revisions in C::D. Both revsets need to have
2488ec1bd22f docs: add example for the `prune` command
Raphaël Gomès <rgomes@octobus.net>
parents: 4841
diff changeset
  1023
        the same number of changesets.
2766
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1024
    """
3283
039c4b8dc3ed obsnote: warn if user try to store a note in obsmarker on an older hg
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3271
diff changeset
  1025
    _checknotesize(ui, opts)
2766
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1026
    revs = scmutil.revrange(repo, list(revs) + opts.get('rev'))
4763
184c64c81528 prune: spell --successor flag without any unnecessary shortcuts
Anton Shestakov <av6@dwimlabs.net>
parents: 4728
diff changeset
  1027
    succs = opts['new'] + opts['successor']
2766
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1028
    bookmarks = set(opts.get('bookmark'))
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1029
    metadata = _getmetadata(**opts)
4045
821e4be40c6c prune: replace --biject with --pair
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3990
diff changeset
  1030
    biject = opts.get('pair') or opts.get('biject')
2766
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1031
    fold = opts.get('fold')
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1032
    split = opts.get('split')
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1033
4804
079dbf36e884 python3: add raw prefix in cases harder to analyze at the token level
Raphaël Gomès <rgomes@octobus.net>
parents: 4800
diff changeset
  1034
    options = [o for o in (r'pair', r'fold', r'split') if opts.get(o)]
2766
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1035
    if 1 < len(options):
4804
079dbf36e884 python3: add raw prefix in cases harder to analyze at the token level
Raphaël Gomès <rgomes@octobus.net>
parents: 4800
diff changeset
  1036
        _opts = pycompat.sysbytes(r', '.join(options))
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
  1037
        raise error.Abort(_(b"can only specify one of %s") % _opts)
2766
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1038
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1039
    if bookmarks:
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1040
        reachablefrombookmark = rewriteutil.reachablefrombookmark
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1041
        repomarks, revs = reachablefrombookmark(repo, revs, bookmarks)
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1042
        if not revs:
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1043
            # no revisions to prune - delete bookmark immediately
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1044
            rewriteutil.deletebookmark(repo, repomarks, bookmarks)
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1045
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1046
    if not revs:
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
  1047
        raise error.Abort(_(b'no revisions specified to prune'))
2766
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1048
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1049
    wlock = lock = tr = None
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1050
    try:
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1051
        wlock = repo.wlock()
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1052
        lock = repo.lock()
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
  1053
        rewriteutil.precheck(repo, revs, b'prune')
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
  1054
        tr = repo.transaction(b'prune')
2766
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1055
        # defines pruned changesets
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1056
        precs = []
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1057
        revs.sort()
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1058
        for p in revs:
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1059
            cp = repo[p]
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1060
            precs.append(cp)
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1061
        if not precs:
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
  1062
            raise error.Abort(b'nothing to prune')
2766
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1063
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1064
        # defines successors changesets
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1065
        sucs = scmutil.revrange(repo, succs)
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1066
        sucs.sort()
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1067
        sucs = tuple(repo[n] for n in sucs)
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1068
        if not biject and len(sucs) > 1 and len(precs) > 1:
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
  1069
            msg = b"Can't use multiple successors for multiple precursors"
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
  1070
            hint = _(b"use --pair to mark a series as a replacement"
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
  1071
                     b" for another")
2766
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1072
            raise error.Abort(msg, hint=hint)
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1073
        elif biject and len(sucs) != len(precs):
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
  1074
            msg = b"Can't use %d successors for %d precursors"\
2766
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1075
                % (len(sucs), len(precs))
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1076
            raise error.Abort(msg)
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1077
        elif (len(precs) == 1 and len(sucs) > 1) and not split:
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
  1078
            msg = b"please add --split if you want to do a split"
2766
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1079
            raise error.Abort(msg)
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1080
        elif len(sucs) == 1 and len(precs) > 1 and not fold:
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
  1081
            msg = b"please add --fold if you want to do a fold"
2766
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1082
            raise error.Abort(msg)
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1083
        elif biject:
4374
86bf0b106949 prune: use scmutil.cleanupnodes()
Martin von Zweigbergk <martinvonz@google.com>
parents: 4373
diff changeset
  1084
            replacements = {p.node(): [s.node()] for p, s in zip(precs, sucs)}
2766
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1085
        else:
4374
86bf0b106949 prune: use scmutil.cleanupnodes()
Martin von Zweigbergk <martinvonz@google.com>
parents: 4373
diff changeset
  1086
            replacements = {p.node(): [s.node() for s in sucs] for p in precs}
2766
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1087
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
  1088
        wdp = repo[b'.']
2766
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1089
4702
fcecbb1261f2 evolve: fix the inconsistent behaviour of prune (issue6137)
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4699
diff changeset
  1090
        if wdp in precs:
fcecbb1261f2 evolve: fix the inconsistent behaviour of prune (issue6137)
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4699
diff changeset
  1091
            if len(sucs) == 1 and len(precs) == 1:
fcecbb1261f2 evolve: fix the inconsistent behaviour of prune (issue6137)
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4699
diff changeset
  1092
                # '.' killed, so update to the successor
fcecbb1261f2 evolve: fix the inconsistent behaviour of prune (issue6137)
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4699
diff changeset
  1093
                newnode = sucs[0]
fcecbb1261f2 evolve: fix the inconsistent behaviour of prune (issue6137)
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4699
diff changeset
  1094
            elif biject:
fcecbb1261f2 evolve: fix the inconsistent behaviour of prune (issue6137)
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4699
diff changeset
  1095
                # find the exact successor of '.'
fcecbb1261f2 evolve: fix the inconsistent behaviour of prune (issue6137)
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4699
diff changeset
  1096
                newnode = sucs[precs.index(wdp)]
fcecbb1261f2 evolve: fix the inconsistent behaviour of prune (issue6137)
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4699
diff changeset
  1097
            else:
fcecbb1261f2 evolve: fix the inconsistent behaviour of prune (issue6137)
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4699
diff changeset
  1098
                # update to an unkilled parent
fcecbb1261f2 evolve: fix the inconsistent behaviour of prune (issue6137)
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4699
diff changeset
  1099
                newnode = wdp
fcecbb1261f2 evolve: fix the inconsistent behaviour of prune (issue6137)
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4699
diff changeset
  1100
fcecbb1261f2 evolve: fix the inconsistent behaviour of prune (issue6137)
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4699
diff changeset
  1101
                while newnode in precs or newnode.obsolete():
5019
967e9a87e82d cleanup: replace .parents()[0] by .p1()
Martin von Zweigbergk <martinvonz@google.com>
parents: 5000
diff changeset
  1102
                    newnode = newnode.p1()
2766
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1103
        else:
4702
fcecbb1261f2 evolve: fix the inconsistent behaviour of prune (issue6137)
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4699
diff changeset
  1104
            # no need to update anywhere as wdp is not related to revs
fcecbb1261f2 evolve: fix the inconsistent behaviour of prune (issue6137)
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4699
diff changeset
  1105
            # being pruned
2766
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1106
            newnode = wdp
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1107
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1108
        if newnode.node() != wdp.node():
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1109
            if opts.get('keep', False):
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1110
                # This is largely the same as the implementation in
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1111
                # strip.stripcmd(). We might want to refactor this somewhere
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1112
                # common at some point.
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1113
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1114
                # only reset the dirstate for files that would actually change
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1115
                # between the working context and uctx
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
  1116
                descendantrevs = repo.revs(b"%d::." % newnode.rev())
2766
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1117
                changedfiles = []
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1118
                for rev in descendantrevs:
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1119
                    # blindly reset the files, regardless of what actually
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1120
                    # changed
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1121
                    changedfiles.extend(repo[rev].files())
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1122
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1123
                # reset files that only changed in the dirstate too
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1124
                dirstate = repo.dirstate
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
  1125
                dirchanges = [f for f in dirstate if dirstate[f] != b'n']
2766
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1126
                changedfiles.extend(dirchanges)
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1127
                repo.dirstate.rebuild(newnode.node(), newnode.manifest(),
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1128
                                      changedfiles)
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1129
                dirstate.write(tr)
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1130
            else:
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1131
                bookactive = repo._activebookmark
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1132
                # Active bookmark that we don't want to delete (with -B option)
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1133
                # we deactivate and move it before the update and reactivate it
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1134
                # after
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1135
                movebookmark = bookactive and not bookmarks
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1136
                if movebookmark:
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1137
                    bookmarksmod.deactivate(repo)
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1138
                    bmchanges = [(bookactive, newnode.node())]
3699
67d6cc7e0979 compat: drop compatibility layer for bookmark.applychanges
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3697
diff changeset
  1139
                    repo._bookmarks.applychanges(repo, tr, bmchanges)
3751
a1a0cfbffd30 evolve: pass revisions as strings to commands.*()
Martin von Zweigbergk <martinvonz@google.com>
parents: 3744
diff changeset
  1140
                commands.update(ui, repo, newnode.hex())
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
  1141
                ui.status(_(b'working directory is now at %s\n')
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
  1142
                          % ui.label(bytes(newnode), b'evolve.node'))
2766
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1143
                if movebookmark:
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1144
                    bookmarksmod.activate(repo, bookactive)
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1145
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1146
        # update bookmarks
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1147
        if bookmarks:
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1148
            rewriteutil.deletebookmark(repo, repomarks, bookmarks)
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1149
3217
b6ba296532cb prune: add support for storing a note in obsmarker
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3216
diff changeset
  1150
        # store note in metadata
b6ba296532cb prune: add support for storing a note in obsmarker
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3216
diff changeset
  1151
        if opts.get('note'):
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
  1152
            metadata[b'note'] = opts['note']
3217
b6ba296532cb prune: add support for storing a note in obsmarker
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3216
diff changeset
  1153
3506
6b4272bbb65d evolve: update code for not implicitly converting ctx to revision
Boris Feld <boris.feld@octobus.net>
parents: 3484
diff changeset
  1154
        precrevs = (precursor.rev() for precursor in precs)
4374
86bf0b106949 prune: use scmutil.cleanupnodes()
Martin von Zweigbergk <martinvonz@google.com>
parents: 4373
diff changeset
  1155
        moves = {}
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
  1156
        for ctx in repo.unfiltered().set(b'bookmark() and %ld', precrevs):
2766
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1157
            # used to be:
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1158
            #
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1159
            #   ldest = list(repo.set('max((::%d) - obsolete())', ctx))
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1160
            #   if ldest:
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1161
            #      c = ldest[0]
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1162
            #
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1163
            # but then revset took a lazy arrow in the knee and became much
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1164
            # slower. The new forms makes as much sense and a much faster.
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1165
            for dest in ctx.ancestors():
4374
86bf0b106949 prune: use scmutil.cleanupnodes()
Martin von Zweigbergk <martinvonz@google.com>
parents: 4373
diff changeset
  1166
                if not dest.obsolete() and dest.node() not in replacements:
86bf0b106949 prune: use scmutil.cleanupnodes()
Martin von Zweigbergk <martinvonz@google.com>
parents: 4373
diff changeset
  1167
                    moves[ctx.node()] = dest.node()
2766
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1168
                    break
5131
53dd70f557aa prune: actually track folds using fold-* metadata in obsmarkers
Anton Shestakov <av6@dwimlabs.net>
parents: 5127
diff changeset
  1169
        if len(sucs) == 1 and len(precs) > 1 and fold:
53dd70f557aa prune: actually track folds using fold-* metadata in obsmarkers
Anton Shestakov <av6@dwimlabs.net>
parents: 5127
diff changeset
  1170
            # hg <= 4.7 (b99903534e06)
53dd70f557aa prune: actually track folds using fold-* metadata in obsmarkers
Anton Shestakov <av6@dwimlabs.net>
parents: 5127
diff changeset
  1171
            # Using a proxy condition to let people wrap cleanupnodes()
53dd70f557aa prune: actually track folds using fold-* metadata in obsmarkers
Anton Shestakov <av6@dwimlabs.net>
parents: 5127
diff changeset
  1172
            if util.safehasattr(scmutil, 'nullrev'):
53dd70f557aa prune: actually track folds using fold-* metadata in obsmarkers
Anton Shestakov <av6@dwimlabs.net>
parents: 5127
diff changeset
  1173
                replacements = {tuple(p.node() for p in precs): [s.node() for s in sucs]}
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
  1174
        scmutil.cleanupnodes(repo, replacements, operation=b"prune", moves=moves,
4374
86bf0b106949 prune: use scmutil.cleanupnodes()
Martin von Zweigbergk <martinvonz@google.com>
parents: 4373
diff changeset
  1175
                             metadata=metadata)
86bf0b106949 prune: use scmutil.cleanupnodes()
Martin von Zweigbergk <martinvonz@google.com>
parents: 4373
diff changeset
  1176
86bf0b106949 prune: use scmutil.cleanupnodes()
Martin von Zweigbergk <martinvonz@google.com>
parents: 4373
diff changeset
  1177
        # informs that changeset have been pruned
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
  1178
        ui.status(_(b'%i changesets pruned\n') % len(precs))
2766
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1179
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1180
        tr.close()
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1181
    finally:
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1182
        lockmod.release(tr, lock, wlock)
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1183
2762
610581a2fb74 commands: move split to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2761
diff changeset
  1184
@eh.command(
4715
12c8b24757f4 py3: use byte strings for @command registrations
Martin von Zweigbergk <martinvonz@google.com>
parents: 4714
diff changeset
  1185
    b'split',
12c8b24757f4 py3: use byte strings for @command registrations
Martin von Zweigbergk <martinvonz@google.com>
parents: 4714
diff changeset
  1186
    [(b'i', b'interactive', True, _(b'use interactive mode')),
12c8b24757f4 py3: use byte strings for @command registrations
Martin von Zweigbergk <martinvonz@google.com>
parents: 4714
diff changeset
  1187
     (b'r', b'rev', [], _(b"revision to split"), _(b'REV')),
12c8b24757f4 py3: use byte strings for @command registrations
Martin von Zweigbergk <martinvonz@google.com>
parents: 4714
diff changeset
  1188
     (b'n', b'note', b'', _(b"store a note on split"), _(b'TEXT')),
12c8b24757f4 py3: use byte strings for @command registrations
Martin von Zweigbergk <martinvonz@google.com>
parents: 4714
diff changeset
  1189
     ] + commitopts + commitopts2 + commitopts3,
4915
5113b38cb677 evolve: use more often seen metavariables in command synopsis strings
Anton Shestakov <av6@dwimlabs.net>
parents: 4898
diff changeset
  1190
    _(b'hg split [OPTION]... [-r REV] [FILE]...'),
4894
f9743b13de6d help: categorizing evolve and topic commands
Rodrigo Damazio <rdamazio@google.com>
parents: 4891
diff changeset
  1191
    helpbasic=True,
f9743b13de6d help: categorizing evolve and topic commands
Rodrigo Damazio <rdamazio@google.com>
parents: 4891
diff changeset
  1192
    **compat.helpcategorykwargs('CATEGORY_CHANGE_MANAGEMENT'))
4299
4af0235e7b0b split: take file patterns to limit selection on matching file patterns
Boris Feld <boris.feld@octobus.net>
parents: 4298
diff changeset
  1193
def cmdsplit(ui, repo, *pats, **opts):
2762
610581a2fb74 commands: move split to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2761
diff changeset
  1194
    """split a changeset into smaller changesets
610581a2fb74 commands: move split to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2761
diff changeset
  1195
610581a2fb74 commands: move split to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2761
diff changeset
  1196
    By default, split the current revision by prompting for all its hunks to be
610581a2fb74 commands: move split to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2761
diff changeset
  1197
    redistributed into new changesets.
610581a2fb74 commands: move split to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2761
diff changeset
  1198
610581a2fb74 commands: move split to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2761
diff changeset
  1199
    Use --rev to split a given changeset instead.
4299
4af0235e7b0b split: take file patterns to limit selection on matching file patterns
Boris Feld <boris.feld@octobus.net>
parents: 4298
diff changeset
  1200
4af0235e7b0b split: take file patterns to limit selection on matching file patterns
Boris Feld <boris.feld@octobus.net>
parents: 4298
diff changeset
  1201
    If file patterns are specified only files matching these patterns will be
4af0235e7b0b split: take file patterns to limit selection on matching file patterns
Boris Feld <boris.feld@octobus.net>
parents: 4298
diff changeset
  1202
    considered to be split in earlier changesets. The files that doesn't match
4af0235e7b0b split: take file patterns to limit selection on matching file patterns
Boris Feld <boris.feld@octobus.net>
parents: 4298
diff changeset
  1203
    will be gathered in the last changeset.
2762
610581a2fb74 commands: move split to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2761
diff changeset
  1204
    """
3283
039c4b8dc3ed obsnote: warn if user try to store a note in obsmarker on an older hg
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3271
diff changeset
  1205
    _checknotesize(ui, opts)
2771
6044bd16bfb7 split: add support for the -D and -U option
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2770
diff changeset
  1206
    _resolveoptions(ui, opts)
2762
610581a2fb74 commands: move split to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2761
diff changeset
  1207
    tr = wlock = lock = None
610581a2fb74 commands: move split to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2761
diff changeset
  1208
    newcommits = []
4300
702f7e1d0b01 split: add a --interactive flag
Boris Feld <boris.feld@octobus.net>
parents: 4299
diff changeset
  1209
    iselect = opts.pop('interactive')
2762
610581a2fb74 commands: move split to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2761
diff changeset
  1210
4733
85ae5dc0507d split: remove an unnecessary (and confusingly typed) fallback revision
Martin von Zweigbergk <martinvonz@google.com>
parents: 4729
diff changeset
  1211
    revs = opts.get('rev')
4298
a98fa6c0d4f3 split: only accept explicit revision through the `-r` option
Boris Feld <boris.feld@octobus.net>
parents: 4294
diff changeset
  1212
    if not revs:
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
  1213
        revarg = b'.'
4298
a98fa6c0d4f3 split: only accept explicit revision through the `-r` option
Boris Feld <boris.feld@octobus.net>
parents: 4294
diff changeset
  1214
    elif len(revs) == 1:
a98fa6c0d4f3 split: only accept explicit revision through the `-r` option
Boris Feld <boris.feld@octobus.net>
parents: 4294
diff changeset
  1215
        revarg = revs[0]
a98fa6c0d4f3 split: only accept explicit revision through the `-r` option
Boris Feld <boris.feld@octobus.net>
parents: 4294
diff changeset
  1216
    else:
a98fa6c0d4f3 split: only accept explicit revision through the `-r` option
Boris Feld <boris.feld@octobus.net>
parents: 4294
diff changeset
  1217
        # XXX --rev often accept multiple value, it seems safer to explicitly
a98fa6c0d4f3 split: only accept explicit revision through the `-r` option
Boris Feld <boris.feld@octobus.net>
parents: 4294
diff changeset
  1218
        # complains here instead of just taking the last value.
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
  1219
        raise error.Abort(_(b'more than one revset is given'))
2762
610581a2fb74 commands: move split to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2761
diff changeset
  1220
3270
e6150b9b88d9 split: force the branch to fix the split bug
Boris Feld <boris.feld@octobus.net>
parents: 3233
diff changeset
  1221
    # Save the current branch to restore it in the end
e6150b9b88d9 split: force the branch to fix the split bug
Boris Feld <boris.feld@octobus.net>
parents: 3233
diff changeset
  1222
    savedbranch = repo.dirstate.branch()
e6150b9b88d9 split: force the branch to fix the split bug
Boris Feld <boris.feld@octobus.net>
parents: 3233
diff changeset
  1223
2762
610581a2fb74 commands: move split to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2761
diff changeset
  1224
    try:
610581a2fb74 commands: move split to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2761
diff changeset
  1225
        wlock = repo.wlock()
610581a2fb74 commands: move split to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2761
diff changeset
  1226
        lock = repo.lock()
4298
a98fa6c0d4f3 split: only accept explicit revision through the `-r` option
Boris Feld <boris.feld@octobus.net>
parents: 4294
diff changeset
  1227
        ctx = scmutil.revsingle(repo, revarg)
3506
6b4272bbb65d evolve: update code for not implicitly converting ctx to revision
Boris Feld <boris.feld@octobus.net>
parents: 3484
diff changeset
  1228
        rev = ctx.rev()
2762
610581a2fb74 commands: move split to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2761
diff changeset
  1229
        cmdutil.bailifchanged(repo)
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
  1230
        rewriteutil.precheck(repo, [rev], action=b'split')
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
  1231
        tr = repo.transaction(b'split')
4423
144cd06029de split: use ui.configoverride to preserve phase while commiting
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4410
diff changeset
  1232
        # make sure we respect the phase while splitting
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
  1233
        overrides = {(b'phases', b'new-commit'): ctx.phase()}
2762
610581a2fb74 commands: move split to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2761
diff changeset
  1234
610581a2fb74 commands: move split to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2761
diff changeset
  1235
        if len(ctx.parents()) > 1:
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
  1236
            raise error.Abort(_(b"cannot split merge commits"))
2762
610581a2fb74 commands: move split to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2761
diff changeset
  1237
        prev = ctx.p1()
610581a2fb74 commands: move split to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2761
diff changeset
  1238
        bmupdate = rewriteutil.bookmarksupdater(repo, ctx.node(), tr)
610581a2fb74 commands: move split to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2761
diff changeset
  1239
        bookactive = repo._activebookmark
610581a2fb74 commands: move split to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2761
diff changeset
  1240
        if bookactive is not None:
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
  1241
            repo.ui.status(_(b"(leaving bookmark %s)\n") % repo._activebookmark)
2762
610581a2fb74 commands: move split to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2761
diff changeset
  1242
        bookmarksmod.deactivate(repo)
610581a2fb74 commands: move split to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2761
diff changeset
  1243
610581a2fb74 commands: move split to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2761
diff changeset
  1244
        # Prepare the working directory
610581a2fb74 commands: move split to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2761
diff changeset
  1245
        rewriteutil.presplitupdate(repo, ui, prev, ctx)
610581a2fb74 commands: move split to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2761
diff changeset
  1246
4299
4af0235e7b0b split: take file patterns to limit selection on matching file patterns
Boris Feld <boris.feld@octobus.net>
parents: 4298
diff changeset
  1247
        def haschanges(matcher=None):
5093
fd1d12497fdb cmdrewrite: avoid accessing scmutil.status fields by index
Martin von Zweigbergk <martinvonz@google.com>
parents: 4988
diff changeset
  1248
            st = repo.status(match=matcher)
fd1d12497fdb cmdrewrite: avoid accessing scmutil.status fields by index
Martin von Zweigbergk <martinvonz@google.com>
parents: 4988
diff changeset
  1249
            return st.modified or st.added or st.removed or st.deleted
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
  1250
        msg = (b"HG: This is the original pre-split commit message. "
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
  1251
               b"Edit it as appropriate.\n\n")
2762
610581a2fb74 commands: move split to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2761
diff changeset
  1252
        msg += ctx.description()
610581a2fb74 commands: move split to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2761
diff changeset
  1253
        opts['message'] = msg
610581a2fb74 commands: move split to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2761
diff changeset
  1254
        opts['edit'] = True
2770
a9ea16a1f4dc split: fix the --user option
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2769
diff changeset
  1255
        if not opts['user']:
a9ea16a1f4dc split: fix the --user option
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2769
diff changeset
  1256
            opts['user'] = ctx.user()
3270
e6150b9b88d9 split: force the branch to fix the split bug
Boris Feld <boris.feld@octobus.net>
parents: 3233
diff changeset
  1257
e6150b9b88d9 split: force the branch to fix the split bug
Boris Feld <boris.feld@octobus.net>
parents: 3233
diff changeset
  1258
        # Set the right branch
e6150b9b88d9 split: force the branch to fix the split bug
Boris Feld <boris.feld@octobus.net>
parents: 3233
diff changeset
  1259
        # XXX-TODO: Find a way to set the branch without altering the dirstate
e6150b9b88d9 split: force the branch to fix the split bug
Boris Feld <boris.feld@octobus.net>
parents: 3233
diff changeset
  1260
        repo.dirstate.setbranch(ctx.branch())
e6150b9b88d9 split: force the branch to fix the split bug
Boris Feld <boris.feld@octobus.net>
parents: 3233
diff changeset
  1261
4299
4af0235e7b0b split: take file patterns to limit selection on matching file patterns
Boris Feld <boris.feld@octobus.net>
parents: 4298
diff changeset
  1262
        if pats:
4af0235e7b0b split: take file patterns to limit selection on matching file patterns
Boris Feld <boris.feld@octobus.net>
parents: 4298
diff changeset
  1263
            # refresh the wctx used for the matcher
4af0235e7b0b split: take file patterns to limit selection on matching file patterns
Boris Feld <boris.feld@octobus.net>
parents: 4298
diff changeset
  1264
            matcher = scmutil.match(repo[None], pats)
4af0235e7b0b split: take file patterns to limit selection on matching file patterns
Boris Feld <boris.feld@octobus.net>
parents: 4298
diff changeset
  1265
        else:
4af0235e7b0b split: take file patterns to limit selection on matching file patterns
Boris Feld <boris.feld@octobus.net>
parents: 4298
diff changeset
  1266
            matcher = scmutil.matchall(repo)
4af0235e7b0b split: take file patterns to limit selection on matching file patterns
Boris Feld <boris.feld@octobus.net>
parents: 4298
diff changeset
  1267
2762
610581a2fb74 commands: move split to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2761
diff changeset
  1268
        while haschanges():
4299
4af0235e7b0b split: take file patterns to limit selection on matching file patterns
Boris Feld <boris.feld@octobus.net>
parents: 4298
diff changeset
  1269
4af0235e7b0b split: take file patterns to limit selection on matching file patterns
Boris Feld <boris.feld@octobus.net>
parents: 4298
diff changeset
  1270
            if haschanges(matcher):
4300
702f7e1d0b01 split: add a --interactive flag
Boris Feld <boris.feld@octobus.net>
parents: 4299
diff changeset
  1271
                if iselect:
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
  1272
                    with repo.ui.configoverride(overrides, b'split'):
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
  1273
                        cmdutil.dorecord(ui, repo, commands.commit, b'commit',
4423
144cd06029de split: use ui.configoverride to preserve phase while commiting
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4410
diff changeset
  1274
                                         False, cmdutil.recordfilter, *pats,
144cd06029de split: use ui.configoverride to preserve phase while commiting
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4410
diff changeset
  1275
                                         **opts)
4300
702f7e1d0b01 split: add a --interactive flag
Boris Feld <boris.feld@octobus.net>
parents: 4299
diff changeset
  1276
                    # TODO: Does no seem like the best way to do this
702f7e1d0b01 split: add a --interactive flag
Boris Feld <boris.feld@octobus.net>
parents: 4299
diff changeset
  1277
                    # We should make dorecord return the newly created commit
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
  1278
                    newcommits.append(repo[b'.'])
4300
702f7e1d0b01 split: add a --interactive flag
Boris Feld <boris.feld@octobus.net>
parents: 4299
diff changeset
  1279
                elif not pats:
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
  1280
                    msg = _(b"no files of directories specified")
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
  1281
                    hint = _(b"do you want --interactive")
4300
702f7e1d0b01 split: add a --interactive flag
Boris Feld <boris.feld@octobus.net>
parents: 4299
diff changeset
  1282
                    raise error.Abort(msg, hint=hint)
702f7e1d0b01 split: add a --interactive flag
Boris Feld <boris.feld@octobus.net>
parents: 4299
diff changeset
  1283
                else:
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
  1284
                    with repo.ui.configoverride(overrides, b'split'):
4423
144cd06029de split: use ui.configoverride to preserve phase while commiting
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4410
diff changeset
  1285
                        commands.commit(ui, repo, *pats, **opts)
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
  1286
                    newcommits.append(repo[b'.'])
4299
4af0235e7b0b split: take file patterns to limit selection on matching file patterns
Boris Feld <boris.feld@octobus.net>
parents: 4298
diff changeset
  1287
            if pats:
4af0235e7b0b split: take file patterns to limit selection on matching file patterns
Boris Feld <boris.feld@octobus.net>
parents: 4298
diff changeset
  1288
                # refresh the wctx used for the matcher
4af0235e7b0b split: take file patterns to limit selection on matching file patterns
Boris Feld <boris.feld@octobus.net>
parents: 4298
diff changeset
  1289
                matcher = scmutil.match(repo[None], pats)
4af0235e7b0b split: take file patterns to limit selection on matching file patterns
Boris Feld <boris.feld@octobus.net>
parents: 4298
diff changeset
  1290
            else:
4af0235e7b0b split: take file patterns to limit selection on matching file patterns
Boris Feld <boris.feld@octobus.net>
parents: 4298
diff changeset
  1291
                matcher = scmutil.matchall(repo)
4af0235e7b0b split: take file patterns to limit selection on matching file patterns
Boris Feld <boris.feld@octobus.net>
parents: 4298
diff changeset
  1292
4af0235e7b0b split: take file patterns to limit selection on matching file patterns
Boris Feld <boris.feld@octobus.net>
parents: 4298
diff changeset
  1293
            if haschanges(matcher):
4291
8f54ab5dd4e2 split: add a help entry to the final prompt
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4236
diff changeset
  1294
                nextaction = None
8f54ab5dd4e2 split: add a help entry to the final prompt
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4236
diff changeset
  1295
                while nextaction is None:
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
  1296
                    nextaction = ui.prompt(b'continue splitting? [Ycdq?]', default=b'y')
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
  1297
                    if nextaction == b'c':
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
  1298
                        with repo.ui.configoverride(overrides, b'split'):
4423
144cd06029de split: use ui.configoverride to preserve phase while commiting
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4410
diff changeset
  1299
                            commands.commit(ui, repo, **opts)
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
  1300
                        newcommits.append(repo[b'.'])
4291
8f54ab5dd4e2 split: add a help entry to the final prompt
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4236
diff changeset
  1301
                        break
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
  1302
                    elif nextaction == b'q':
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
  1303
                        raise error.Abort(_(b'user quit'))
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
  1304
                    elif nextaction == b'd':
4293
d4902d48d095 split: add a option to discard remaining change during split
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4292
diff changeset
  1305
                        # TODO: We should offer a way for the user to confirm
d4902d48d095 split: add a option to discard remaining change during split
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4292
diff changeset
  1306
                        # what is the remaining changes, either via a separate
d4902d48d095 split: add a option to discard remaining change during split
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4292
diff changeset
  1307
                        # diff action or by showing the remaining and
d4902d48d095 split: add a option to discard remaining change during split
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4292
diff changeset
  1308
                        # prompting for confirmation
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
  1309
                        ui.status(_(b'discarding remaining changes\n'))
4293
d4902d48d095 split: add a option to discard remaining change during split
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4292
diff changeset
  1310
                        target = newcommits[0]
4299
4af0235e7b0b split: take file patterns to limit selection on matching file patterns
Boris Feld <boris.feld@octobus.net>
parents: 4298
diff changeset
  1311
                        if pats:
5093
fd1d12497fdb cmdrewrite: avoid accessing scmutil.status fields by index
Martin von Zweigbergk <martinvonz@google.com>
parents: 4988
diff changeset
  1312
                            status = repo.status(match=matcher)
4299
4af0235e7b0b split: take file patterns to limit selection on matching file patterns
Boris Feld <boris.feld@octobus.net>
parents: 4298
diff changeset
  1313
                            dirty = set()
5093
fd1d12497fdb cmdrewrite: avoid accessing scmutil.status fields by index
Martin von Zweigbergk <martinvonz@google.com>
parents: 4988
diff changeset
  1314
                            dirty.update(status.modified)
fd1d12497fdb cmdrewrite: avoid accessing scmutil.status fields by index
Martin von Zweigbergk <martinvonz@google.com>
parents: 4988
diff changeset
  1315
                            dirty.update(status.added)
fd1d12497fdb cmdrewrite: avoid accessing scmutil.status fields by index
Martin von Zweigbergk <martinvonz@google.com>
parents: 4988
diff changeset
  1316
                            dirty.update(status.removed)
fd1d12497fdb cmdrewrite: avoid accessing scmutil.status fields by index
Martin von Zweigbergk <martinvonz@google.com>
parents: 4988
diff changeset
  1317
                            dirty.update(status.deleted)
4299
4af0235e7b0b split: take file patterns to limit selection on matching file patterns
Boris Feld <boris.feld@octobus.net>
parents: 4298
diff changeset
  1318
                            dirty = sorted(dirty)
4af0235e7b0b split: take file patterns to limit selection on matching file patterns
Boris Feld <boris.feld@octobus.net>
parents: 4298
diff changeset
  1319
                            cmdutil.revert(ui, repo, repo[target],
4af0235e7b0b split: take file patterns to limit selection on matching file patterns
Boris Feld <boris.feld@octobus.net>
parents: 4298
diff changeset
  1320
                                           (target, node.nullid), *dirty)
4af0235e7b0b split: take file patterns to limit selection on matching file patterns
Boris Feld <boris.feld@octobus.net>
parents: 4298
diff changeset
  1321
                        else:
4af0235e7b0b split: take file patterns to limit selection on matching file patterns
Boris Feld <boris.feld@octobus.net>
parents: 4298
diff changeset
  1322
                            cmdutil.revert(ui, repo, repo[target],
4af0235e7b0b split: take file patterns to limit selection on matching file patterns
Boris Feld <boris.feld@octobus.net>
parents: 4298
diff changeset
  1323
                                           (target, node.nullid), all=True)
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
  1324
                    elif nextaction == b'?':
4291
8f54ab5dd4e2 split: add a help entry to the final prompt
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4236
diff changeset
  1325
                        nextaction = None
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
  1326
                        ui.write(_(b"y - yes, continue selection\n"))
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
  1327
                        ui.write(_(b"c - commit, select all remaining changes\n"))
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
  1328
                        ui.write(_(b"d - discard, discard remaining changes\n"))
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
  1329
                        ui.write(_(b"q - quit, abort the split\n"))
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
  1330
                        ui.write(_(b"? - ?, display help\n"))
4291
8f54ab5dd4e2 split: add a help entry to the final prompt
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4236
diff changeset
  1331
                else:
8f54ab5dd4e2 split: add a help entry to the final prompt
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4236
diff changeset
  1332
                    continue
8f54ab5dd4e2 split: add a help entry to the final prompt
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4236
diff changeset
  1333
                break # propagate the previous break
2762
610581a2fb74 commands: move split to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2761
diff changeset
  1334
            else:
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
  1335
                ui.status(_(b"no more change to split\n"))
4299
4af0235e7b0b split: take file patterns to limit selection on matching file patterns
Boris Feld <boris.feld@octobus.net>
parents: 4298
diff changeset
  1336
                if haschanges():
4af0235e7b0b split: take file patterns to limit selection on matching file patterns
Boris Feld <boris.feld@octobus.net>
parents: 4298
diff changeset
  1337
                    # XXX: Should we show a message for informing the user
4af0235e7b0b split: take file patterns to limit selection on matching file patterns
Boris Feld <boris.feld@octobus.net>
parents: 4298
diff changeset
  1338
                    # that we create another commit with remaining changes?
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
  1339
                    with repo.ui.configoverride(overrides, b'split'):
4423
144cd06029de split: use ui.configoverride to preserve phase while commiting
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4410
diff changeset
  1340
                        commands.commit(ui, repo, **opts)
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
  1341
                    newcommits.append(repo[b'.'])
2762
610581a2fb74 commands: move split to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2761
diff changeset
  1342
        if newcommits:
610581a2fb74 commands: move split to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2761
diff changeset
  1343
            tip = repo[newcommits[-1]]
610581a2fb74 commands: move split to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2761
diff changeset
  1344
            bmupdate(tip.node())
610581a2fb74 commands: move split to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2761
diff changeset
  1345
            if bookactive is not None:
610581a2fb74 commands: move split to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2761
diff changeset
  1346
                bookmarksmod.activate(repo, bookactive)
3216
13cb0810ce22 split: add support for storing a note in obsmarker
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3215
diff changeset
  1347
            metadata = {}
13cb0810ce22 split: add support for storing a note in obsmarker
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3215
diff changeset
  1348
            if opts.get('note'):
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
  1349
                metadata[b'note'] = opts['note']
3694
c0d5e0929f8b compat: from compatibility layer for createmarkers
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3675
diff changeset
  1350
            obsolete.createmarkers(repo, [(repo[rev], newcommits)],
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
  1351
                                   metadata=metadata, operation=b"split")
2762
610581a2fb74 commands: move split to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2761
diff changeset
  1352
        tr.close()
610581a2fb74 commands: move split to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2761
diff changeset
  1353
    finally:
3270
e6150b9b88d9 split: force the branch to fix the split bug
Boris Feld <boris.feld@octobus.net>
parents: 3233
diff changeset
  1354
        # Restore the old branch
e6150b9b88d9 split: force the branch to fix the split bug
Boris Feld <boris.feld@octobus.net>
parents: 3233
diff changeset
  1355
        repo.dirstate.setbranch(savedbranch)
e6150b9b88d9 split: force the branch to fix the split bug
Boris Feld <boris.feld@octobus.net>
parents: 3233
diff changeset
  1356
2762
610581a2fb74 commands: move split to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2761
diff changeset
  1357
        lockmod.release(tr, lock, wlock)
2763
4a5b0c373e65 commands: move the touch to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2762
diff changeset
  1358
4a5b0c373e65 commands: move the touch to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2762
diff changeset
  1359
@eh.command(
4715
12c8b24757f4 py3: use byte strings for @command registrations
Martin von Zweigbergk <martinvonz@google.com>
parents: 4714
diff changeset
  1360
    b'touch',
12c8b24757f4 py3: use byte strings for @command registrations
Martin von Zweigbergk <martinvonz@google.com>
parents: 4714
diff changeset
  1361
    [(b'r', b'rev', [], _(b'revision to update'), _(b'REV')),
12c8b24757f4 py3: use byte strings for @command registrations
Martin von Zweigbergk <martinvonz@google.com>
parents: 4714
diff changeset
  1362
     (b'n', b'note', b'', _(b'store a note on touch'), _(b'TEXT')),
12c8b24757f4 py3: use byte strings for @command registrations
Martin von Zweigbergk <martinvonz@google.com>
parents: 4714
diff changeset
  1363
     (b'D', b'duplicate', False,
12c8b24757f4 py3: use byte strings for @command registrations
Martin von Zweigbergk <martinvonz@google.com>
parents: 4714
diff changeset
  1364
      b'do not mark the new revision as successor of the old one'),
12c8b24757f4 py3: use byte strings for @command registrations
Martin von Zweigbergk <martinvonz@google.com>
parents: 4714
diff changeset
  1365
     (b'A', b'allowdivergence', False,
12c8b24757f4 py3: use byte strings for @command registrations
Martin von Zweigbergk <martinvonz@google.com>
parents: 4714
diff changeset
  1366
      b'mark the new revision as successor of the old one potentially creating '
12c8b24757f4 py3: use byte strings for @command registrations
Martin von Zweigbergk <martinvonz@google.com>
parents: 4714
diff changeset
  1367
      b'divergence')],
2763
4a5b0c373e65 commands: move the touch to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2762
diff changeset
  1368
    # allow to choose the seed ?
4921
a7c01a2a3974 branching: merge with stable
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4906 4915
diff changeset
  1369
    _(b'[OPTION]... [-r] REV...'),
4894
f9743b13de6d help: categorizing evolve and topic commands
Rodrigo Damazio <rdamazio@google.com>
parents: 4891
diff changeset
  1370
    **compat.helpcategorykwargs('CATEGORY_CHANGE_MANAGEMENT'))
2763
4a5b0c373e65 commands: move the touch to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2762
diff changeset
  1371
def touch(ui, repo, *revs, **opts):
3233
bd01eb0108f4 touch: shorten touch's docstring to make it fit into a one line
Denis Laxalde <denis@laxalde.org>
parents: 3231
diff changeset
  1372
    """create successors identical to their predecessors but the changeset ID
2763
4a5b0c373e65 commands: move the touch to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2762
diff changeset
  1373
4a5b0c373e65 commands: move the touch to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2762
diff changeset
  1374
    This is used to "resurrect" changesets
4a5b0c373e65 commands: move the touch to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2762
diff changeset
  1375
    """
3283
039c4b8dc3ed obsnote: warn if user try to store a note in obsmarker on an older hg
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3271
diff changeset
  1376
    _checknotesize(ui, opts)
2763
4a5b0c373e65 commands: move the touch to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2762
diff changeset
  1377
    revs = list(revs)
4a5b0c373e65 commands: move the touch to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2762
diff changeset
  1378
    revs.extend(opts['rev'])
4a5b0c373e65 commands: move the touch to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2762
diff changeset
  1379
    if not revs:
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
  1380
        revs = [b'.']
2763
4a5b0c373e65 commands: move the touch to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2762
diff changeset
  1381
    revs = scmutil.revrange(repo, revs)
4a5b0c373e65 commands: move the touch to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2762
diff changeset
  1382
    if not revs:
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
  1383
        ui.write_err(b'no revision to touch\n')
2763
4a5b0c373e65 commands: move the touch to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2762
diff changeset
  1384
        return 1
4638
7978db1dda3e touch: extract the logic of touching rev's to its own function
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4613
diff changeset
  1385
7978db1dda3e touch: extract the logic of touching rev's to its own function
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4613
diff changeset
  1386
    duplicate = opts['duplicate']
2790
1b7b9acda2a9 touch: use precheck to validate revision
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2789
diff changeset
  1387
    if not duplicate:
5136
bbf33d5f32ef touch: allow divergence with -A even if allowdivergence=no
Martin von Zweigbergk <martinvonz@google.com>
parents: 5135
diff changeset
  1388
        # Override allowdivergence=true because we'll do our own checking later
bbf33d5f32ef touch: allow divergence with -A even if allowdivergence=no
Martin von Zweigbergk <martinvonz@google.com>
parents: 5135
diff changeset
  1389
        # instead
bbf33d5f32ef touch: allow divergence with -A even if allowdivergence=no
Martin von Zweigbergk <martinvonz@google.com>
parents: 5135
diff changeset
  1390
        overrides = {(b'experimental', b'evolution.allowdivergence'): b"true"}
bbf33d5f32ef touch: allow divergence with -A even if allowdivergence=no
Martin von Zweigbergk <martinvonz@google.com>
parents: 5135
diff changeset
  1391
        with ui.configoverride(overrides, b'touch'):
bbf33d5f32ef touch: allow divergence with -A even if allowdivergence=no
Martin von Zweigbergk <martinvonz@google.com>
parents: 5135
diff changeset
  1392
            rewriteutil.precheck(repo, revs, b'touch')
2763
4a5b0c373e65 commands: move the touch to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2762
diff changeset
  1393
    tmpl = utility.shorttemplate
5181
c979d64a2589 compat: drop 4.5 compatibility layer for log utility
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 5179
diff changeset
  1394
    displayer = logcmdutil.changesetdisplayer(ui, repo, {b'template': tmpl})
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
  1395
    with repo.wlock(), repo.lock(), repo.transaction(b'touch'):
4707
ea84a151fa62 touch: let's not use util.acceptintervention() as it's not required
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4706
diff changeset
  1396
        touchnodes(ui, repo, revs, displayer, **opts)
3453
32ed5b6fadd3 grab: add a command to grab a commit and update to it
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3389
diff changeset
  1397
4638
7978db1dda3e touch: extract the logic of touching rev's to its own function
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4613
diff changeset
  1398
def touchnodes(ui, repo, revs, displayer, **opts):
7978db1dda3e touch: extract the logic of touching rev's to its own function
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4613
diff changeset
  1399
    duplicate = opts['duplicate']
7978db1dda3e touch: extract the logic of touching rev's to its own function
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4613
diff changeset
  1400
    allowdivergence = opts['allowdivergence']
7978db1dda3e touch: extract the logic of touching rev's to its own function
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4613
diff changeset
  1401
    revs.sort() # ensure parent are run first
7978db1dda3e touch: extract the logic of touching rev's to its own function
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4613
diff changeset
  1402
    newmapping = {}
7978db1dda3e touch: extract the logic of touching rev's to its own function
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4613
diff changeset
  1403
    for r in revs:
7978db1dda3e touch: extract the logic of touching rev's to its own function
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4613
diff changeset
  1404
        ctx = repo[r]
7978db1dda3e touch: extract the logic of touching rev's to its own function
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4613
diff changeset
  1405
        extra = ctx.extra().copy()
5213
d5544ebf5836 touch: make __touch-noise__ extra field be bytes
Anton Shestakov <av6@dwimlabs.net>
parents: 5136
diff changeset
  1406
        extra[b'__touch-noise__'] = b'%d' % random.randint(0, 0xffffffff)
4638
7978db1dda3e touch: extract the logic of touching rev's to its own function
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4613
diff changeset
  1407
        # search for touched parent
7978db1dda3e touch: extract the logic of touching rev's to its own function
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4613
diff changeset
  1408
        p1 = ctx.p1().node()
7978db1dda3e touch: extract the logic of touching rev's to its own function
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4613
diff changeset
  1409
        p2 = ctx.p2().node()
7978db1dda3e touch: extract the logic of touching rev's to its own function
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4613
diff changeset
  1410
        p1 = newmapping.get(p1, p1)
7978db1dda3e touch: extract the logic of touching rev's to its own function
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4613
diff changeset
  1411
        p2 = newmapping.get(p2, p2)
7978db1dda3e touch: extract the logic of touching rev's to its own function
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4613
diff changeset
  1412
7978db1dda3e touch: extract the logic of touching rev's to its own function
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4613
diff changeset
  1413
        if not (duplicate or allowdivergence):
5078
66f01dd79f5d evolve: rename a function to make it explicit
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 5073
diff changeset
  1414
            if precheck_contentdiv(repo, ctx):
4638
7978db1dda3e touch: extract the logic of touching rev's to its own function
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4613
diff changeset
  1415
                displayer.show(ctx)
7978db1dda3e touch: extract the logic of touching rev's to its own function
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4613
diff changeset
  1416
                index = ui.promptchoice(
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
  1417
                    _(b"reviving this changeset will create divergence"
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
  1418
                      b" unless you make a duplicate.\n(a)llow divergence or"
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
  1419
                      b" (d)uplicate the changeset? $$ &Allowdivergence $$ "
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
  1420
                      b"&Duplicate"), 0)
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
  1421
                choice = [b'allowdivergence', b'duplicate'][index]
5038
f583d9eedbce evolve: extract logic of pre-checking divergence risk to its own function
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 5019
diff changeset
  1422
                if choice == b'duplicate':
4638
7978db1dda3e touch: extract the logic of touching rev's to its own function
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4613
diff changeset
  1423
                    duplicate = True
7978db1dda3e touch: extract the logic of touching rev's to its own function
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4613
diff changeset
  1424
4721
b69497b23d31 touch: make sure merge commits include files from p1 and p2
Anton Shestakov <av6@dwimlabs.net>
parents: 4715
diff changeset
  1425
        updates = []
b69497b23d31 touch: make sure merge commits include files from p1 and p2
Anton Shestakov <av6@dwimlabs.net>
parents: 4715
diff changeset
  1426
        if len(ctx.parents()) > 1:
b69497b23d31 touch: make sure merge commits include files from p1 and p2
Anton Shestakov <av6@dwimlabs.net>
parents: 4715
diff changeset
  1427
            updates = ctx.parents()
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
  1428
        extradict = {b'extra': extra}
4721
b69497b23d31 touch: make sure merge commits include files from p1 and p2
Anton Shestakov <av6@dwimlabs.net>
parents: 4715
diff changeset
  1429
        new, unusedvariable = rewriteutil.rewrite(repo, ctx, updates, ctx,
4638
7978db1dda3e touch: extract the logic of touching rev's to its own function
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4613
diff changeset
  1430
                                                  [p1, p2],
7978db1dda3e touch: extract the logic of touching rev's to its own function
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4613
diff changeset
  1431
                                                  commitopts=extradict)
7978db1dda3e touch: extract the logic of touching rev's to its own function
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4613
diff changeset
  1432
        # store touched version to help potential children
7978db1dda3e touch: extract the logic of touching rev's to its own function
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4613
diff changeset
  1433
        newmapping[ctx.node()] = new
7978db1dda3e touch: extract the logic of touching rev's to its own function
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4613
diff changeset
  1434
7978db1dda3e touch: extract the logic of touching rev's to its own function
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4613
diff changeset
  1435
        if not duplicate:
7978db1dda3e touch: extract the logic of touching rev's to its own function
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4613
diff changeset
  1436
            metadata = {}
7978db1dda3e touch: extract the logic of touching rev's to its own function
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4613
diff changeset
  1437
            if opts.get('note'):
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
  1438
                metadata[b'note'] = opts['note']
4638
7978db1dda3e touch: extract the logic of touching rev's to its own function
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4613
diff changeset
  1439
            obsolete.createmarkers(repo, [(ctx, (repo[new],))],
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
  1440
                                   metadata=metadata, operation=b"touch")
4638
7978db1dda3e touch: extract the logic of touching rev's to its own function
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4613
diff changeset
  1441
        tr = repo.currenttransaction()
7978db1dda3e touch: extract the logic of touching rev's to its own function
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4613
diff changeset
  1442
        phases.retractboundary(repo, tr, ctx.phase(), [new])
7978db1dda3e touch: extract the logic of touching rev's to its own function
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4613
diff changeset
  1443
        if ctx in repo[None].parents():
5096
6742ce189373 compat: add a context manager that calls _quick_access_changeid_invalidate()
Anton Shestakov <av6@dwimlabs.net>
parents: 5093
diff changeset
  1444
            with repo.dirstate.parentchange(), compat.parentchange(repo):
4638
7978db1dda3e touch: extract the logic of touching rev's to its own function
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4613
diff changeset
  1445
                repo.dirstate.setparents(new, node.nullid)
7978db1dda3e touch: extract the logic of touching rev's to its own function
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4613
diff changeset
  1446
3453
32ed5b6fadd3 grab: add a command to grab a commit and update to it
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3389
diff changeset
  1447
@eh.command(
4715
12c8b24757f4 py3: use byte strings for @command registrations
Martin von Zweigbergk <martinvonz@google.com>
parents: 4714
diff changeset
  1448
    b'pick|grab',
12c8b24757f4 py3: use byte strings for @command registrations
Martin von Zweigbergk <martinvonz@google.com>
parents: 4714
diff changeset
  1449
    [(b'r', b'rev', b'', _(b'revision to pick'), _(b'REV')),
12c8b24757f4 py3: use byte strings for @command registrations
Martin von Zweigbergk <martinvonz@google.com>
parents: 4714
diff changeset
  1450
     (b'c', b'continue', False, b'continue interrupted pick'),
12c8b24757f4 py3: use byte strings for @command registrations
Martin von Zweigbergk <martinvonz@google.com>
parents: 4714
diff changeset
  1451
     (b'a', b'abort', False, b'abort interrupted pick'),
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
  1452
     ] + mergetoolopts,
4921
a7c01a2a3974 branching: merge with stable
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4906 4915
diff changeset
  1453
    _(b'[OPTION]... [-r] REV'),
4894
f9743b13de6d help: categorizing evolve and topic commands
Rodrigo Damazio <rdamazio@google.com>
parents: 4891
diff changeset
  1454
    **compat.helpcategorykwargs('CATEGORY_CHANGE_MANAGEMENT'))
4048
d7034826c0a2 pick: rename the grab command to pick
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4047
diff changeset
  1455
def cmdpick(ui, repo, *revs, **opts):
4229
a8ed26f01c8d pick: `hg help` was not showing the full cmd desc of `pick`
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4191
diff changeset
  1456
    """move a commit on the top of working directory parent and updates to it."""
3453
32ed5b6fadd3 grab: add a command to grab a commit and update to it
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3389
diff changeset
  1457
32ed5b6fadd3 grab: add a command to grab a commit and update to it
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3389
diff changeset
  1458
    cont = opts.get('continue')
32ed5b6fadd3 grab: add a command to grab a commit and update to it
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3389
diff changeset
  1459
    abort = opts.get('abort')
32ed5b6fadd3 grab: add a command to grab a commit and update to it
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3389
diff changeset
  1460
32ed5b6fadd3 grab: add a command to grab a commit and update to it
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3389
diff changeset
  1461
    if cont and abort:
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
  1462
        raise error.Abort(_(b"cannot specify both --continue and --abort"))
3453
32ed5b6fadd3 grab: add a command to grab a commit and update to it
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3389
diff changeset
  1463
32ed5b6fadd3 grab: add a command to grab a commit and update to it
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3389
diff changeset
  1464
    revs = list(revs)
32ed5b6fadd3 grab: add a command to grab a commit and update to it
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3389
diff changeset
  1465
    if opts.get('rev'):
32ed5b6fadd3 grab: add a command to grab a commit and update to it
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3389
diff changeset
  1466
        revs.append(opts['rev'])
32ed5b6fadd3 grab: add a command to grab a commit and update to it
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3389
diff changeset
  1467
4706
de194ed973ba branching: merge with stable
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4679 4702
diff changeset
  1468
    with repo.wlock(), repo.lock():
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
  1469
        pickstate = state.cmdstate(repo, path=b'pickstate')
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
  1470
        pctx = repo[b'.']
3453
32ed5b6fadd3 grab: add a command to grab a commit and update to it
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3389
diff changeset
  1471
32ed5b6fadd3 grab: add a command to grab a commit and update to it
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3389
diff changeset
  1472
        if not cont and not abort:
32ed5b6fadd3 grab: add a command to grab a commit and update to it
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3389
diff changeset
  1473
            cmdutil.bailifchanged(repo)
32ed5b6fadd3 grab: add a command to grab a commit and update to it
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3389
diff changeset
  1474
            revs = scmutil.revrange(repo, revs)
32ed5b6fadd3 grab: add a command to grab a commit and update to it
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3389
diff changeset
  1475
            if len(revs) > 1:
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
  1476
                raise error.Abort(_(b"specify just one revision"))
3453
32ed5b6fadd3 grab: add a command to grab a commit and update to it
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3389
diff changeset
  1477
            elif not revs:
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
  1478
                raise error.Abort(_(b"empty revision set"))
3453
32ed5b6fadd3 grab: add a command to grab a commit and update to it
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3389
diff changeset
  1479
32ed5b6fadd3 grab: add a command to grab a commit and update to it
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3389
diff changeset
  1480
            origctx = repo[revs.first()]
32ed5b6fadd3 grab: add a command to grab a commit and update to it
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3389
diff changeset
  1481
3527
7b4d1bfb6b7d grab: gracefully handle the case when we try to grab parent of wdir
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3526
diff changeset
  1482
            if origctx in pctx.ancestors() or origctx.node() == pctx.node():
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
  1483
                raise error.Abort(_(b"cannot pick an ancestor revision"))
3453
32ed5b6fadd3 grab: add a command to grab a commit and update to it
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3389
diff changeset
  1484
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
  1485
            rewriteutil.precheck(repo, [origctx.rev()], b'pick')
3453
32ed5b6fadd3 grab: add a command to grab a commit and update to it
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3389
diff changeset
  1486
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
  1487
            ui.status(_(b'picking %d:%s "%s"\n') %
3453
32ed5b6fadd3 grab: add a command to grab a commit and update to it
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3389
diff changeset
  1488
                      (origctx.rev(), origctx,
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
  1489
                       origctx.description().split(b"\n", 1)[0]))
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
  1490
            overrides = {(b'ui', b'forcemerge'): opts.get('tool', b'')}
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
  1491
            with ui.configoverride(overrides, b'pick'):
4679
d9016946ec69 pick: reduce configoverride() scope
Anton Shestakov <av6@dwimlabs.net>
parents: 4642
diff changeset
  1492
                stats = merge.graft(repo, origctx, origctx.p1(),
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
  1493
                                    [b'local', b'destination'])
5179
7c41a1ebcf7f compat: directly use status attribute instead of the compat layer
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 5178
diff changeset
  1494
            if stats.unresolvedcount:
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
  1495
                pickstate.addopts({b'orignode': origctx.node(),
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
  1496
                                   b'oldpctx': pctx.node()})
4052
73e73471d6c6 pick: replace "grabstate" with "pickstate"
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4051
diff changeset
  1497
                pickstate.save()
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
  1498
                raise error.InterventionRequired(_(b"unresolved merge conflicts"
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
  1499
                                                   b" (see hg help resolve)"))
3453
32ed5b6fadd3 grab: add a command to grab a commit and update to it
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3389
diff changeset
  1500
32ed5b6fadd3 grab: add a command to grab a commit and update to it
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3389
diff changeset
  1501
        elif abort:
4799
6f9e66433ff8 pick: factor our the abort function
Taapas Agrawal <taapas2897@gmail.com>
parents: 4795
diff changeset
  1502
            return abortpick(ui, repo, pickstate)
3453
32ed5b6fadd3 grab: add a command to grab a commit and update to it
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3389
diff changeset
  1503
32ed5b6fadd3 grab: add a command to grab a commit and update to it
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3389
diff changeset
  1504
        else:
32ed5b6fadd3 grab: add a command to grab a commit and update to it
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3389
diff changeset
  1505
            if revs:
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
  1506
                raise error.Abort(_(b"cannot specify both --continue and "
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
  1507
                                    b"revision"))
4052
73e73471d6c6 pick: replace "grabstate" with "pickstate"
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4051
diff changeset
  1508
            if not pickstate:
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
  1509
                raise error.Abort(_(b"no interrupted pick state exists"))
3453
32ed5b6fadd3 grab: add a command to grab a commit and update to it
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3389
diff changeset
  1510
4052
73e73471d6c6 pick: replace "grabstate" with "pickstate"
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4051
diff changeset
  1511
            pickstate.load()
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
  1512
            orignode = pickstate[b'orignode']
3453
32ed5b6fadd3 grab: add a command to grab a commit and update to it
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3389
diff changeset
  1513
            origctx = repo[orignode]
32ed5b6fadd3 grab: add a command to grab a commit and update to it
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3389
diff changeset
  1514
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
  1515
        overrides = {(b'phases', b'new-commit'): origctx.phase()}
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
  1516
        with repo.ui.configoverride(overrides, b'pick'):
3475
a03bb02dfaba grab: preserve phase of the grabbed changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3457
diff changeset
  1517
            newnode = repo.commit(text=origctx.description(),
a03bb02dfaba grab: preserve phase of the grabbed changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3457
diff changeset
  1518
                                  user=origctx.user(),
a03bb02dfaba grab: preserve phase of the grabbed changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3457
diff changeset
  1519
                                  date=origctx.date(), extra=origctx.extra())
4485
e3785a8d0712 pick: update working dir branch (issue6089)
Manuel Jacob <me@manueljacob.de>
parents: 4463
diff changeset
  1520
        repo.dirstate.setbranch(origctx.branch())
3453
32ed5b6fadd3 grab: add a command to grab a commit and update to it
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3389
diff changeset
  1521
4052
73e73471d6c6 pick: replace "grabstate" with "pickstate"
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4051
diff changeset
  1522
        if pickstate:
73e73471d6c6 pick: replace "grabstate" with "pickstate"
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4051
diff changeset
  1523
            pickstate.delete()
4891
a9929dd36405 pick: don't create any successors when there were no changes (issue6093)
Anton Shestakov <av6@dwimlabs.net>
parents: 4845
diff changeset
  1524
        if newnode is None:
a9929dd36405 pick: don't create any successors when there were no changes (issue6093)
Anton Shestakov <av6@dwimlabs.net>
parents: 4845
diff changeset
  1525
            replacements = {origctx.node(): []}
a9929dd36405 pick: don't create any successors when there were no changes (issue6093)
Anton Shestakov <av6@dwimlabs.net>
parents: 4845
diff changeset
  1526
        else:
a9929dd36405 pick: don't create any successors when there were no changes (issue6093)
Anton Shestakov <av6@dwimlabs.net>
parents: 4845
diff changeset
  1527
            newctx = repo[newnode]
a9929dd36405 pick: don't create any successors when there were no changes (issue6093)
Anton Shestakov <av6@dwimlabs.net>
parents: 4845
diff changeset
  1528
            replacements = {origctx.node(): [newctx.node()]}
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
  1529
        scmutil.cleanupnodes(repo, replacements, operation=b"pick")
3453
32ed5b6fadd3 grab: add a command to grab a commit and update to it
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3389
diff changeset
  1530
32ed5b6fadd3 grab: add a command to grab a commit and update to it
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3389
diff changeset
  1531
        if newnode is None:
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
  1532
            ui.warn(_(b"note: picking %d:%s created no changes to commit\n") %
3453
32ed5b6fadd3 grab: add a command to grab a commit and update to it
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3389
diff changeset
  1533
                    (origctx.rev(), origctx))
32ed5b6fadd3 grab: add a command to grab a commit and update to it
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3389
diff changeset
  1534
            return 0
32ed5b6fadd3 grab: add a command to grab a commit and update to it
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3389
diff changeset
  1535
32ed5b6fadd3 grab: add a command to grab a commit and update to it
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3389
diff changeset
  1536
        return 0
4799
6f9e66433ff8 pick: factor our the abort function
Taapas Agrawal <taapas2897@gmail.com>
parents: 4795
diff changeset
  1537
6f9e66433ff8 pick: factor our the abort function
Taapas Agrawal <taapas2897@gmail.com>
parents: 4795
diff changeset
  1538
def abortpick(ui, repo, pickstate, abortcmd=False):
6f9e66433ff8 pick: factor our the abort function
Taapas Agrawal <taapas2897@gmail.com>
parents: 4795
diff changeset
  1539
    """logic to abort pick"""
6f9e66433ff8 pick: factor our the abort function
Taapas Agrawal <taapas2897@gmail.com>
parents: 4795
diff changeset
  1540
    if not pickstate and not abortcmd:
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
  1541
        raise error.Abort(_(b"no interrupted pick state exists"))
4799
6f9e66433ff8 pick: factor our the abort function
Taapas Agrawal <taapas2897@gmail.com>
parents: 4795
diff changeset
  1542
    pickstate.load()
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
  1543
    pctxnode = pickstate[b'oldpctx']
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
  1544
    ui.status(_(b"aborting pick, updating to %s\n") %
4799
6f9e66433ff8 pick: factor our the abort function
Taapas Agrawal <taapas2897@gmail.com>
parents: 4795
diff changeset
  1545
              node.hex(pctxnode)[:12])
6f9e66433ff8 pick: factor our the abort function
Taapas Agrawal <taapas2897@gmail.com>
parents: 4795
diff changeset
  1546
    hg.updaterepo(repo, pctxnode, True)
6f9e66433ff8 pick: factor our the abort function
Taapas Agrawal <taapas2897@gmail.com>
parents: 4795
diff changeset
  1547
    pickstate.delete()
6f9e66433ff8 pick: factor our the abort function
Taapas Agrawal <taapas2897@gmail.com>
parents: 4795
diff changeset
  1548
    return 0
4800
fa534a8c5863 pick: added support for hg abort
Taapas Agrawal <taapas2897@gmail.com>
parents: 4799
diff changeset
  1549
fa534a8c5863 pick: added support for hg abort
Taapas Agrawal <taapas2897@gmail.com>
parents: 4799
diff changeset
  1550
def hgabortpick(ui, repo):
fa534a8c5863 pick: added support for hg abort
Taapas Agrawal <taapas2897@gmail.com>
parents: 4799
diff changeset
  1551
    """logic to abort pick using 'hg abort'"""
fa534a8c5863 pick: added support for hg abort
Taapas Agrawal <taapas2897@gmail.com>
parents: 4799
diff changeset
  1552
    with repo.wlock(), repo.lock():
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
  1553
        pickstate = state.cmdstate(repo, path=b'pickstate')
4800
fa534a8c5863 pick: added support for hg abort
Taapas Agrawal <taapas2897@gmail.com>
parents: 4799
diff changeset
  1554
        return abortpick(ui, repo, pickstate, abortcmd=True)