hgext3rd/evolve/cmdrewrite.py
author Anton Shestakov <av6@dwimlabs.net>
Fri, 07 Jun 2019 18:14:48 +0800
branchstable
changeset 4687 313565dd75e3
parent 4642 a7c4f163656b
child 4690 98f345bb9235
permissions -rw-r--r--
pick: remove transaction on the whole command (issue6037) At its core, pick is a pretty straightforward and well-behaving command, it uses functions already in core hg, it checks that wdir is clean and that changeset to pick is not public, it checks if there happen to be merge conflicts and can be --continue'd later, etc. It is very similar to graft in core (it also uses mergemod.graft function), but it obsoletes the original changeset. However, graft does not experience this incorrect behavior from issue 6037. What happens in the test case for this issue when we pick a revision that touches both "a" and "b": mergemod.graft() takes the original changeset and tries to apply it to the wdir, which results in "b" being marked as newly added and ready to be committed, "a" updated with the new content and being marked as modified, but "a" also has conflicts. Pick correctly notices this and saves its state before asking for user intervention. So far so good. However, when the command raises InterventionRequired to print a user-facing message and exit while being wrapped in repo.transaction() context manager, the latter partially undoes what mergemod.graft() did: it unmarks "b" as added. And when user continues pick, "b" is therefore not tracked and is not included in the resulting commit. The transaction is not useful here, because it doesn't touch wdir (it's still dirty), it doesn't remove pickstate (and other commands will refuse to work until pick --abort or --continue), it just makes "b" untracked. The solution is to use repo.transaction() only to wrap code that writes data to hg store in the final stages of the command after all checks have passed and is not expected to fail on trivial cases like merge conflicts. For example, committing the picked changeset. But since pick uses repo.commit() for that, and because that function already uses a transaction, wrapping it in another transaction doesn't make sense.
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
3670
1043e9c54355 compat: adapt `amend --patch` to the new `patch.extract` API
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3661
diff changeset
    14
import contextlib
2763
4a5b0c373e65 commands: move the touch to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2762
diff changeset
    15
import random
4a5b0c373e65 commands: move the touch to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2762
diff changeset
    16
2724
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    17
from mercurial import (
2762
610581a2fb74 commands: move split to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2761
diff changeset
    18
    bookmarks as bookmarksmod,
2724
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    19
    cmdutil,
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    20
    commands,
2725
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
    21
    context,
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
    22
    copies,
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
    23
    error,
2760
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
    24
    hg,
2725
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
    25
    lock as lockmod,
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,
3697
6aff4bb3970d compat: drop compatibility layer for successorssets
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3694
diff changeset
    29
    obsutil,
2941
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
    30
    patch,
2725
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
    31
    phases,
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
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    36
from mercurial.i18n import _
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    37
3675
1dccccde82bb compat: access datestr in a way compatible with verison prior to 4.6
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3670
diff changeset
    38
try:
1dccccde82bb compat: access datestr in a way compatible with verison prior to 4.6
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3670
diff changeset
    39
    from mercurial.utils.dateutil import datestr
1dccccde82bb compat: access datestr in a way compatible with verison prior to 4.6
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3670
diff changeset
    40
except ImportError: # hg <= 4.5
1dccccde82bb compat: access datestr in a way compatible with verison prior to 4.6
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3670
diff changeset
    41
    from mercurial.util import datestr
3660
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
    42
2724
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    43
from . import (
2763
4a5b0c373e65 commands: move the touch to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2762
diff changeset
    44
    compat,
3457
82e9f9603b1b evolvestate: rename the file to state.py and class name to cmdstate
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3456
diff changeset
    45
    state,
2724
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    46
    exthelper,
2756
f4dd6e6d4c73 rewriteutil: create a rewriteutil module to host utility function
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2752
diff changeset
    47
    rewriteutil,
2763
4a5b0c373e65 commands: move the touch to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2762
diff changeset
    48
    utility,
2724
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
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    51
eh = exthelper.exthelper()
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    52
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    53
walkopts = commands.walkopts
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    54
commitopts = commands.commitopts
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    55
commitopts2 = commands.commitopts2
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    56
mergetoolopts = commands.mergetoolopts
2941
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
    57
stringio = util.stringio
2724
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    58
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    59
# option added by evolve
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    60
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
    61
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
    62
    """ 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
    63
7bc587557e4f cmdrewrite: add a utility function to make sure note is of valid format
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2949
diff changeset
    64
    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
    65
    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
    66
        return
7bc587557e4f cmdrewrite: add a utility function to make sure note is of valid format
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2949
diff changeset
    67
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
    68
    if not compat.isobsnotesupported():
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
    69
        ui.warn(_("current hg version does not support storing"
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
    70
                  " note in obsmarker\n"))
3213
7bc587557e4f cmdrewrite: add a utility function to make sure note is of valid format
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2949
diff changeset
    71
    if len(note) > 255:
7bc587557e4f cmdrewrite: add a utility function to make sure note is of valid format
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2949
diff changeset
    72
        raise error.Abort(_("cannot store a note of more than 255 bytes"))
7bc587557e4f cmdrewrite: add a utility function to make sure note is of valid format
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2949
diff changeset
    73
    if '\n' in note:
7bc587557e4f cmdrewrite: add a utility function to make sure note is of valid format
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2949
diff changeset
    74
        raise error.Abort(_("note cannot contain a newline"))
7bc587557e4f cmdrewrite: add a utility function to make sure note is of valid format
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2949
diff changeset
    75
2724
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    76
def _resolveoptions(ui, opts):
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    77
    """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
    78
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    79
    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
    80
    -d was supplied.
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    81
    """
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    82
    # 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
    83
    if not opts.get('date') and opts.get('current_date'):
3514
498f782ccb4b compat: add compat layer for date related functions
Boris Feld <boris.feld@octobus.net>
parents: 3506
diff changeset
    84
        opts['date'] = '%d %d' % compat.makedate()
2724
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    85
    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
    86
        opts['user'] = ui.username()
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    87
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    88
commitopts3 = [
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    89
    ('D', 'current-date', None,
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    90
     _('record the current date as commit date')),
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    91
    ('U', 'current-user', None,
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    92
     _('record the current user as committer')),
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    93
]
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    94
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    95
interactiveopt = [['i', 'interactive', None, _('use interactive mode')]]
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    96
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    97
@eh.command(
4191
5c734be63c3e evolve: adapt for changed @command decorator
Martin von Zweigbergk <martinvonz@google.com>
parents: 4126
diff changeset
    98
    'amend|refresh',
2724
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    99
    [('A', 'addremove', None,
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   100
      _('mark new/missing files as added/removed before committing')),
2730
7fbb7a5d359f uncommit: expose the feature with a '--extract' to amend
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2729
diff changeset
   101
     ('a', 'all', False, _("match all files")),
2724
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   102
     ('e', 'edit', False, _('invoke editor on commit messages')),
2730
7fbb7a5d359f uncommit: expose the feature with a '--extract' to amend
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2729
diff changeset
   103
     ('', 'extract', False, _('extract changes from the commit to the working copy')),
3660
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   104
     ('', 'patch', False, _('make changes to wdir parent by editing patch')),
2724
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   105
     ('', 'close-branch', None,
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   106
      _('mark a branch as closed, hiding it from the branch list')),
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   107
     ('s', 'secret', None, _('use the secret phase for committing')),
4183
536c67823962 commands: adjust metavariables as appropriate
Anton Shestakov <av6@dwimlabs.net>
parents: 4126
diff changeset
   108
     ('n', 'note', '', _('store a note on amend'), _('TEXT')),
2724
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   109
    ] + walkopts + commitopts + commitopts2 + commitopts3 + interactiveopt,
4191
5c734be63c3e evolve: adapt for changed @command decorator
Martin von Zweigbergk <martinvonz@google.com>
parents: 4126
diff changeset
   110
    _('[OPTION]... [FILE]...'),
5c734be63c3e evolve: adapt for changed @command decorator
Martin von Zweigbergk <martinvonz@google.com>
parents: 4126
diff changeset
   111
    helpbasic=True)
2724
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   112
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
   113
    """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
   114
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   115
    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
   116
    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
   117
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   118
    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
   119
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   120
    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
   121
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
   122
    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
   123
    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
   124
    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
   125
2724
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   126
    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
   127
    """
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
   128
    _checknotesize(ui, opts)
2724
e6bc6eaa17c5 amend: extract into a 'evolve.evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   129
    opts = opts.copy()
3660
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   130
    if opts.get('patch'):
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   131
        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
   132
    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
   133
        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
   134
    else:
7fbb7a5d359f uncommit: expose the feature with a '--extract' to amend
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2729
diff changeset
   135
        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
   136
            # 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
   137
            include = list(opts.get('include'))
7fbb7a5d359f uncommit: expose the feature with a '--extract' to amend
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2729
diff changeset
   138
            include.append('re:.*')
7fbb7a5d359f uncommit: expose the feature with a '--extract' to amend
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2729
diff changeset
   139
        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
   140
        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
   141
        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
   142
        _resolveoptions(ui, opts)
7fbb7a5d359f uncommit: expose the feature with a '--extract' to amend
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2729
diff changeset
   143
        _alias, commitcmd = cmdutil.findcmd('commit', commands.table)
3744
4a70392f1723 amend: use context manager for locks (issue5887)
Martin von Zweigbergk <martinvonz@google.com>
parents: 3709
diff changeset
   144
        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
   145
            if not (edit or opts['message'] or log):
8275ef099135 amend: query the wdir parent after taking lock (issue5266)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3451
diff changeset
   146
                opts['message'] = repo['.'].description()
2787
ebca049e8ca9 amend: use precheck to validate revision
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2786
diff changeset
   147
            rewriteutil.precheck(repo, [repo['.'].rev()], action='amend')
ebca049e8ca9 amend: use precheck to validate revision
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2786
diff changeset
   148
            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
   149
3660
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   150
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
   151
    """logic for --patch flag of `hg amend` command."""
4642
a7c4f163656b cmdrewrite: use context manager for some locks and transactions
Martin von Zweigbergk <martinvonz@google.com>
parents: 4640
diff changeset
   152
    with repo.wlock(), repo.lock(), repo.transaction('amend') as tr:
3660
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   153
        cmdutil.bailifchanged(repo)
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   154
        # first get the patch
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   155
        old = repo['.']
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   156
        p1 = old.p1()
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   157
        rewriteutil.precheck(repo, [old.rev()], 'amend')
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   158
        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
   159
        diffopts.nodates = True
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   160
        diffopts.git = True
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   161
        fp = stringio()
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   162
        _writectxmetadata(repo, old, fp)
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   163
        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
   164
        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
   165
                                         match=matcher,
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   166
                                         opts=diffopts):
4463
7d54a538dd1e evolve: unindent some lines caught by flake8
Anton Shestakov <av6@dwimlabs.net>
parents: 4446
diff changeset
   167
            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
   168
        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
   169
        if newnode == old.node():
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   170
            raise error.Abort(_("nothing changed"))
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   171
        metadata = {}
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   172
        if opts.get('note'):
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   173
            metadata['note'] = opts['note']
4372
5345be014f2c amend: use scmutil.cleanupnodes() with --patch
Martin von Zweigbergk <martinvonz@google.com>
parents: 4371
diff changeset
   174
        replacements = {old.node(): [newnode]}
5345be014f2c amend: use scmutil.cleanupnodes() with --patch
Martin von Zweigbergk <martinvonz@google.com>
parents: 4371
diff changeset
   175
        scmutil.cleanupnodes(repo, replacements, operation='amend',
5345be014f2c amend: use scmutil.cleanupnodes() with --patch
Martin von Zweigbergk <martinvonz@google.com>
parents: 4371
diff changeset
   176
                             metadata=metadata)
3660
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   177
        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
   178
        hg.updaterepo(repo, newnode, True)
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   179
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
   180
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
   181
    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
   182
    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
   183
        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
   184
        previous_patch = fp.getvalue()
3709
aaa6adbbb47a compat: handle different `ui.edit` signature with Mercurial 4.3
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3699
diff changeset
   185
        if 5 <= len(ui.edit.im_func.func_defaults):
aaa6adbbb47a compat: handle different `ui.edit` signature with Mercurial 4.3
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3699
diff changeset
   186
            newpatch = ui.edit(fp.getvalue(), old.user(), action="diff")
aaa6adbbb47a compat: handle different `ui.edit` signature with Mercurial 4.3
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3699
diff changeset
   187
        else:
aaa6adbbb47a compat: handle different `ui.edit` signature with Mercurial 4.3
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3699
diff changeset
   188
            newpatch = ui.edit(fp.getvalue(), old.user())
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
   189
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
        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
   191
        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
   192
        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
   193
            # write rest of the files in the 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
   194
            restmatcher = scmutil.match(old, [], opts={'exclude': 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
   195
            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
   196
                                             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
   197
                                             opts=diffopts):
4463
7d54a538dd1e evolve: unindent some lines caught by flake8
Anton Shestakov <av6@dwimlabs.net>
parents: 4446
diff changeset
   198
                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
   199
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
        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
   201
        if not user_patch:
96bbea985b25 amend: allow aborting an `amend --patch` by saving an empty file (issue5925)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3751
diff changeset
   202
            raise error.Abort(_("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
   203
        if user_patch == previous_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
   204
            raise error.Abort(_("patch unchanged"))
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
   205
        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
   206
        # 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
   207
        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
   208
            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
   209
        except patch.PatchError as err:
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
   210
            ui.write_err(_("failed to apply edited patch: %s\n") % err)
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
   211
            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
   212
            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
   213
                defaultchoice = 1 # no
4108
ef3c9ecb8099 cmdrewrite: rename variable and move it closer to where it is used
Anton Shestakov <av6@dwimlabs.net>
parents: 4052
diff changeset
   214
            retrychoice = _('try to fix the patch (yn)?$$ &Yes $$ &No')
ef3c9ecb8099 cmdrewrite: rename variable and move it closer to where it is used
Anton Shestakov <av6@dwimlabs.net>
parents: 4052
diff changeset
   215
            if ui.promptchoice(retrychoice, default=defaultchoice):
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
   216
                raise error.Abort(_("Could not apply amended path"))
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
            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
   218
                # 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
   219
                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
   220
                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
   221
    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
   222
3660
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   223
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
   224
    """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
   225
    repository with metadata being extracted from the patch"""
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   226
    metadata = patch.extract(ui, fp)
3670
1043e9c54355 compat: adapt `amend --patch` to the new `patch.extract` API
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3661
diff changeset
   227
    if util.safehasattr(metadata, 'get'): # < hg-4.6
1043e9c54355 compat: adapt `amend --patch` to the new `patch.extract` API
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3661
diff changeset
   228
        @contextlib.contextmanager
1043e9c54355 compat: adapt `amend --patch` to the new `patch.extract` API
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3661
diff changeset
   229
        def patchcontext():
1043e9c54355 compat: adapt `amend --patch` to the new `patch.extract` API
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3661
diff changeset
   230
            yield metadata
1043e9c54355 compat: adapt `amend --patch` to the new `patch.extract` API
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3661
diff changeset
   231
        patchcontext = patchcontext()
1043e9c54355 compat: adapt `amend --patch` to the new `patch.extract` API
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3661
diff changeset
   232
    else:
1043e9c54355 compat: adapt `amend --patch` to the new `patch.extract` API
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3661
diff changeset
   233
        patchcontext = metadata
3660
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   234
    pold = old.p1()
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   235
3670
1043e9c54355 compat: adapt `amend --patch` to the new `patch.extract` API
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3661
diff changeset
   236
    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
   237
        # store the metadata from the patch to variables
1043e9c54355 compat: adapt `amend --patch` to the new `patch.extract` API
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3661
diff changeset
   238
        parents = (metadata.get('p1'), metadata.get('p2'))
1043e9c54355 compat: adapt `amend --patch` to the new `patch.extract` API
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3661
diff changeset
   239
        date = metadata.get('date') or old.date()
1043e9c54355 compat: adapt `amend --patch` to the new `patch.extract` API
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3661
diff changeset
   240
        branch = metadata.get('branch') or old.branch()
1043e9c54355 compat: adapt `amend --patch` to the new `patch.extract` API
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3661
diff changeset
   241
        user = metadata.get('user') or old.user()
1043e9c54355 compat: adapt `amend --patch` to the new `patch.extract` API
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3661
diff changeset
   242
        # 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
   243
        extra = old.extra()
1043e9c54355 compat: adapt `amend --patch` to the new `patch.extract` API
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3661
diff changeset
   244
        message = metadata.get('message') or old.description()
1043e9c54355 compat: adapt `amend --patch` to the new `patch.extract` API
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3661
diff changeset
   245
        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
   246
        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
   247
        try:
1043e9c54355 compat: adapt `amend --patch` to the new `patch.extract` API
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3661
diff changeset
   248
            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
   249
            # 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
   250
            # of this function
1043e9c54355 compat: adapt `amend --patch` to the new `patch.extract` API
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3661
diff changeset
   251
            patch.patchrepo(ui, repo, pold, store, fp, 1, '',
1043e9c54355 compat: adapt `amend --patch` to the new `patch.extract` API
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3661
diff changeset
   252
                            files=files, eolmode=None)
3660
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   253
3670
1043e9c54355 compat: adapt `amend --patch` to the new `patch.extract` API
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3661
diff changeset
   254
            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
   255
                                    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
   256
                                    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
   257
                                    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
   258
                                    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
   259
                                    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
   260
            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
   261
        finally:
1043e9c54355 compat: adapt `amend --patch` to the new `patch.extract` API
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3661
diff changeset
   262
            store.close()
3660
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   263
    return newcm
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   264
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   265
def _writectxmetadata(repo, ctx, fp):
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   266
    nodeval = scmutil.binnode(ctx)
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   267
    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
   268
    branch = ctx.branch()
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   269
    if parents:
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   270
        prev = parents[0]
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   271
    else:
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   272
        prev = node.nullid
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   273
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   274
    fp.write("# HG changeset patch\n")
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   275
    fp.write("# User %s\n" % ctx.user())
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   276
    fp.write("# Date %d %d\n" % ctx.date())
3675
1dccccde82bb compat: access datestr in a way compatible with verison prior to 4.6
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3670
diff changeset
   277
    fp.write("#      %s\n" % datestr(ctx.date()))
3660
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   278
    if branch and branch != 'default':
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   279
        fp.write("# Branch %s\n" % branch)
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   280
    fp.write("# Node ID %s\n" % node.hex(nodeval))
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   281
    fp.write("# Parent  %s\n" % node.hex(prev))
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   282
    if len(parents) > 1:
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   283
        fp.write("# Parent  %s\n" % node.hex(parents[1]))
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   284
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   285
    for headerid in cmdutil.extraexport:
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   286
        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
   287
        if header is not None:
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   288
            fp.write('# %s\n' % header)
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   289
    fp.write(ctx.description().rstrip())
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   290
    fp.write("\n\n")
f018656ca3bf amend: add a new flag `--patch` to `hg amend`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3616
diff changeset
   291
2725
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   292
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
   293
    touched = set()
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   294
    for files in repo.status(source, dest, match=match)[:3]:
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   295
        touched.update(files)
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   296
    return touched
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   297
2728
3c371aa16cb9 uncommit: add support for --user and --date
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2727
diff changeset
   298
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
   299
                    date=None):
2725
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   300
    """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
   301
    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
   302
    """
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   303
    base = ctx.p1()
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   304
    if target is None:
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   305
        target = base
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   306
    # ctx
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   307
    initialfiles = _touchedbetween(repo, base, ctx)
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   308
    if base == target:
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   309
        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
   310
        newcontent = set()
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   311
    else:
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   312
        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
   313
        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
   314
    # 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
   315
    # + 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
   316
    # - 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
   317
    files = (initialfiles - affected) | newcontent
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   318
    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
   319
        return None
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   320
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   321
    # Filter copies
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   322
    copied = copies.pathcopies(target, ctx)
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   323
    copied = dict((dst, src) for dst, src in copied.iteritems()
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   324
                  if dst in files)
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   325
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   326
    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
   327
        if path in redirect:
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   328
            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
   329
        if path not in contentctx:
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   330
            return None
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   331
        fctx = contentctx[path]
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   332
        flags = fctx.flags()
3298
f4b06f44d274 memfilectx: changectx argument is not mandatory
Boris Feld <boris.feld@octobus.net>
parents: 3296
diff changeset
   333
        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
   334
        return mctx
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   335
2727
f7d44441dfd3 uncommit: add support for --message and --logfile
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2726
diff changeset
   336
    if message is None:
f7d44441dfd3 uncommit: add support for --message and --logfile
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2726
diff changeset
   337
        message = ctx.description()
2728
3c371aa16cb9 uncommit: add support for --user and --date
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2727
diff changeset
   338
    if not user:
3c371aa16cb9 uncommit: add support for --user and --date
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2727
diff changeset
   339
        user = ctx.user()
3c371aa16cb9 uncommit: add support for --user and --date
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2727
diff changeset
   340
    if not date:
3c371aa16cb9 uncommit: add support for --user and --date
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2727
diff changeset
   341
        date = ctx.date()
2725
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   342
    new = context.memctx(repo,
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   343
                         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
   344
                         text=message,
2725
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   345
                         files=files,
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   346
                         filectxfn=filectxfn,
2728
3c371aa16cb9 uncommit: add support for --user and --date
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2727
diff changeset
   347
                         user=user,
3c371aa16cb9 uncommit: add support for --user and --date
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2727
diff changeset
   348
                         date=date,
2725
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   349
                         extra=ctx.extra())
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   350
    # 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
   351
    newid = repo.commitctx(new)
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   352
    return newid
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   353
2943
25576132cb30 uncommit: fix the dirstate handling in `uncommit -i`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2942
diff changeset
   354
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
   355
    """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
   356
    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
   357
    match.
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   358
    """
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   359
    ctx = repo['.']
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   360
    ds = repo.dirstate
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   361
    copies = dict(ds.copies())
2943
25576132cb30 uncommit: fix the dirstate handling in `uncommit -i`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2942
diff changeset
   362
    if interactive:
25576132cb30 uncommit: fix the dirstate handling in `uncommit -i`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2942
diff changeset
   363
        # 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
   364
        # 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
   365
        # 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
   366
        # us in defining the exact behavior
25576132cb30 uncommit: fix the dirstate handling in `uncommit -i`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2942
diff changeset
   367
        m, a, r = repo.status(oldctx, ctx, match=match)[:3]
25576132cb30 uncommit: fix the dirstate handling in `uncommit -i`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2942
diff changeset
   368
        for f in m:
25576132cb30 uncommit: fix the dirstate handling in `uncommit -i`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2942
diff changeset
   369
            # 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
   370
            # 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
   371
            # modifications are uncommitted
25576132cb30 uncommit: fix the dirstate handling in `uncommit -i`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2942
diff changeset
   372
            # 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
   373
            # 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
   374
            # 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
   375
            # 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
   376
            # 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
   377
            # resulting status can be modified or clean.
25576132cb30 uncommit: fix the dirstate handling in `uncommit -i`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2942
diff changeset
   378
            if ds[f] == 'r':
25576132cb30 uncommit: fix the dirstate handling in `uncommit -i`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2942
diff changeset
   379
                # 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
   380
                # as removed
25576132cb30 uncommit: fix the dirstate handling in `uncommit -i`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2942
diff changeset
   381
                continue
25576132cb30 uncommit: fix the dirstate handling in `uncommit -i`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2942
diff changeset
   382
            ds.normallookup(f)
25576132cb30 uncommit: fix the dirstate handling in `uncommit -i`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2942
diff changeset
   383
25576132cb30 uncommit: fix the dirstate handling in `uncommit -i`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2942
diff changeset
   384
        for f in a:
25576132cb30 uncommit: fix the dirstate handling in `uncommit -i`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2942
diff changeset
   385
            # 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
   386
            # 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
   387
            # 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
   388
            # 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
   389
            # 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
   390
            # 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
   391
            # removed in oldctx
25576132cb30 uncommit: fix the dirstate handling in `uncommit -i`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2942
diff changeset
   392
            if ds[f] == 'a':
25576132cb30 uncommit: fix the dirstate handling in `uncommit -i`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2942
diff changeset
   393
                ds.normallookup(f)
25576132cb30 uncommit: fix the dirstate handling in `uncommit -i`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2942
diff changeset
   394
                continue
25576132cb30 uncommit: fix the dirstate handling in `uncommit -i`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2942
diff changeset
   395
            ds.remove(f)
25576132cb30 uncommit: fix the dirstate handling in `uncommit -i`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2942
diff changeset
   396
25576132cb30 uncommit: fix the dirstate handling in `uncommit -i`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2942
diff changeset
   397
        for f in r:
25576132cb30 uncommit: fix the dirstate handling in `uncommit -i`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2942
diff changeset
   398
            # 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
   399
            # 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
   400
            # 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
   401
            # 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
   402
            # 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
   403
            # result in file being stated as unknown.
25576132cb30 uncommit: fix the dirstate handling in `uncommit -i`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2942
diff changeset
   404
            if ds[f] == 'r':
25576132cb30 uncommit: fix the dirstate handling in `uncommit -i`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2942
diff changeset
   405
                # 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
   406
                # unknown
25576132cb30 uncommit: fix the dirstate handling in `uncommit -i`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2942
diff changeset
   407
                ds.drop(f)
25576132cb30 uncommit: fix the dirstate handling in `uncommit -i`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2942
diff changeset
   408
                continue
25576132cb30 uncommit: fix the dirstate handling in `uncommit -i`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2942
diff changeset
   409
            ds.add(f)
25576132cb30 uncommit: fix the dirstate handling in `uncommit -i`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2942
diff changeset
   410
    else:
2942
10194206acc7 uncommit: pre-indent a block in if-True
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2941
diff changeset
   411
        m, a, r = repo.status(oldctx.p1(), oldctx, match=match)[:3]
10194206acc7 uncommit: pre-indent a block in if-True
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2941
diff changeset
   412
        for f in m:
10194206acc7 uncommit: pre-indent a block in if-True
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2941
diff changeset
   413
            if ds[f] == 'r':
10194206acc7 uncommit: pre-indent a block in if-True
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2941
diff changeset
   414
                # modified + removed -> removed
10194206acc7 uncommit: pre-indent a block in if-True
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2941
diff changeset
   415
                continue
10194206acc7 uncommit: pre-indent a block in if-True
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2941
diff changeset
   416
            ds.normallookup(f)
2725
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   417
2942
10194206acc7 uncommit: pre-indent a block in if-True
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2941
diff changeset
   418
        for f in a:
10194206acc7 uncommit: pre-indent a block in if-True
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2941
diff changeset
   419
            if ds[f] == 'r':
10194206acc7 uncommit: pre-indent a block in if-True
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2941
diff changeset
   420
                # added + removed -> unknown
10194206acc7 uncommit: pre-indent a block in if-True
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2941
diff changeset
   421
                ds.drop(f)
10194206acc7 uncommit: pre-indent a block in if-True
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2941
diff changeset
   422
            elif ds[f] != 'a':
10194206acc7 uncommit: pre-indent a block in if-True
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2941
diff changeset
   423
                ds.add(f)
2725
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   424
2942
10194206acc7 uncommit: pre-indent a block in if-True
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2941
diff changeset
   425
        for f in r:
10194206acc7 uncommit: pre-indent a block in if-True
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2941
diff changeset
   426
            if ds[f] == 'a':
10194206acc7 uncommit: pre-indent a block in if-True
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2941
diff changeset
   427
                # removed + added -> normal
10194206acc7 uncommit: pre-indent a block in if-True
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2941
diff changeset
   428
                ds.normallookup(f)
10194206acc7 uncommit: pre-indent a block in if-True
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2941
diff changeset
   429
            elif ds[f] != 'r':
10194206acc7 uncommit: pre-indent a block in if-True
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2941
diff changeset
   430
                ds.remove(f)
2725
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   431
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   432
    # 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
   433
    oldcopies = {}
2943
25576132cb30 uncommit: fix the dirstate handling in `uncommit -i`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2942
diff changeset
   434
    if interactive:
25576132cb30 uncommit: fix the dirstate handling in `uncommit -i`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2942
diff changeset
   435
        # 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
   436
        # original meaning to use them
25576132cb30 uncommit: fix the dirstate handling in `uncommit -i`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2942
diff changeset
   437
        m, a, r = repo.status(oldctx.p1(), oldctx, match=match)[:3]
2725
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   438
    for f in (m + a):
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   439
        src = oldctx[f].renamed()
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   440
        if src:
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   441
            oldcopies[f] = src[0]
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   442
    oldcopies.update(copies)
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   443
    copies = dict((dst, oldcopies.get(src, src))
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   444
                  for dst, src in oldcopies.iteritems())
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   445
    # Adjust the dirstate copies
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   446
    for dst, src in copies.iteritems():
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   447
        if (src not in ctx or dst in ctx or ds[dst] != 'a'):
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   448
            src = None
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   449
        ds.copy(src, dst)
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   450
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   451
@eh.command(
3229
63f6f9db9c3a help: remove a few commands from `hg` (no args) command list
Kyle Lippincott <spectral@google.com>
parents: 2949
diff changeset
   452
    'uncommit',
2725
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   453
    [('a', 'all', None, _('uncommit all changes when no arguments given')),
2941
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   454
     ('i', 'interactive', False, _('interactive mode to uncommit (EXPERIMENTAL)')),
4183
536c67823962 commands: adjust metavariables as appropriate
Anton Shestakov <av6@dwimlabs.net>
parents: 4126
diff changeset
   455
     ('r', 'rev', '', _('revert commit content to REV instead'), _('REV')),
3389
eacf6149b678 uncommit: add a new flag `--revert` to discard wdir changes after uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3298
diff changeset
   456
     ('', 'revert', False, _('discard working directory changes after uncommit')),
4183
536c67823962 commands: adjust metavariables as appropriate
Anton Shestakov <av6@dwimlabs.net>
parents: 4126
diff changeset
   457
     ('n', 'note', '', _('store a note on uncommit'), _('TEXT')),
2729
69fe16428b0f uncommit: add support for -U and -D
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2728
diff changeset
   458
     ] + commands.walkopts + commitopts + commitopts2 + commitopts3,
2725
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   459
    _('[OPTION]... [NAME]'))
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   460
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
   461
    """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
   462
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   463
    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
   464
    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
   465
    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
   466
    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
   467
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   468
    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
   469
    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
   470
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   471
    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
   472
    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
   473
    directory.
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   474
2941
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   475
    .. container:: verbose
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   476
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   477
       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
   478
       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
   479
2725
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   480
    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
   481
    """
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   482
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
   483
    _checknotesize(ui, opts)
2729
69fe16428b0f uncommit: add support for -U and -D
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2728
diff changeset
   484
    _resolveoptions(ui, opts) # process commitopts3
2941
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   485
    interactive = opts.get('interactive')
2725
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   486
    wlock = lock = tr = None
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   487
    try:
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   488
        wlock = repo.wlock()
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   489
        lock = repo.lock()
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   490
        wctx = repo[None]
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   491
        if len(wctx.parents()) <= 0:
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   492
            raise error.Abort(_("cannot uncommit null changeset"))
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   493
        if len(wctx.parents()) > 1:
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   494
            raise error.Abort(_("cannot uncommit while merging"))
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   495
        old = repo['.']
2788
554c069cdc85 uncommit: use precheck to validate revision
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2787
diff changeset
   496
        rewriteutil.precheck(repo, [repo['.'].rev()], action='uncommit')
2725
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   497
        if len(old.parents()) > 1:
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   498
            raise error.Abort(_("cannot uncommit merge changeset"))
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   499
        oldphase = old.phase()
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   500
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   501
        rev = None
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   502
        if opts.get('rev'):
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   503
            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
   504
            ctx = repo[None]
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   505
            if ctx.p1() == rev or ctx.p2() == rev:
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   506
                raise error.Abort(_("cannot uncommit to parent changeset"))
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   507
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   508
        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
   509
        disallowunstable = not obsolete.isenabled(repo,
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   510
                                                  obsolete.allowunstableopt)
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   511
        if disallowunstable and not onahead:
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   512
            raise error.Abort(_("cannot uncommit in the middle of a stack"))
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   513
4506
79de4e86d9a4 uncommit: abort if an explicitly given file cannot be uncommitted (BC)
Matt Harbison <matt_harbison@yahoo.com>
parents: 4499
diff changeset
   514
        match = scmutil.match(old, pats, opts)
79de4e86d9a4 uncommit: abort if an explicitly given file cannot be uncommitted (BC)
Matt Harbison <matt_harbison@yahoo.com>
parents: 4499
diff changeset
   515
79de4e86d9a4 uncommit: abort if an explicitly given file cannot be uncommitted (BC)
Matt Harbison <matt_harbison@yahoo.com>
parents: 4499
diff changeset
   516
        # 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
   517
        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
   518
            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
   519
            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
   520
79de4e86d9a4 uncommit: abort if an explicitly given file cannot be uncommitted (BC)
Matt Harbison <matt_harbison@yahoo.com>
parents: 4499
diff changeset
   521
            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
   522
79de4e86d9a4 uncommit: abort if an explicitly given file cannot be uncommitted (BC)
Matt Harbison <matt_harbison@yahoo.com>
parents: 4499
diff changeset
   523
            # 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
   524
            # 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
   525
            # uncommitted.
79de4e86d9a4 uncommit: abort if an explicitly given file cannot be uncommitted (BC)
Matt Harbison <matt_harbison@yahoo.com>
parents: 4499
diff changeset
   526
            if badfiles:
79de4e86d9a4 uncommit: abort if an explicitly given file cannot be uncommitted (BC)
Matt Harbison <matt_harbison@yahoo.com>
parents: 4499
diff changeset
   527
                badfiles -= set([f for f in util.dirs(eligible)])
79de4e86d9a4 uncommit: abort if an explicitly given file cannot be uncommitted (BC)
Matt Harbison <matt_harbison@yahoo.com>
parents: 4499
diff changeset
   528
79de4e86d9a4 uncommit: abort if an explicitly given file cannot be uncommitted (BC)
Matt Harbison <matt_harbison@yahoo.com>
parents: 4499
diff changeset
   529
            try:
79de4e86d9a4 uncommit: abort if an explicitly given file cannot be uncommitted (BC)
Matt Harbison <matt_harbison@yahoo.com>
parents: 4499
diff changeset
   530
                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
   531
            except AttributeError:
79de4e86d9a4 uncommit: abort if an explicitly given file cannot be uncommitted (BC)
Matt Harbison <matt_harbison@yahoo.com>
parents: 4499
diff changeset
   532
                uipathfn = match.rel   # <= 4.9
79de4e86d9a4 uncommit: abort if an explicitly given file cannot be uncommitted (BC)
Matt Harbison <matt_harbison@yahoo.com>
parents: 4499
diff changeset
   533
79de4e86d9a4 uncommit: abort if an explicitly given file cannot be uncommitted (BC)
Matt Harbison <matt_harbison@yahoo.com>
parents: 4499
diff changeset
   534
            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
   535
                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
   536
                    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
   537
                             b"parent")
79de4e86d9a4 uncommit: abort if an explicitly given file cannot be uncommitted (BC)
Matt Harbison <matt_harbison@yahoo.com>
parents: 4499
diff changeset
   538
                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
   539
                    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
   540
                else:
79de4e86d9a4 uncommit: abort if an explicitly given file cannot be uncommitted (BC)
Matt Harbison <matt_harbison@yahoo.com>
parents: 4499
diff changeset
   541
                    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
   542
79de4e86d9a4 uncommit: abort if an explicitly given file cannot be uncommitted (BC)
Matt Harbison <matt_harbison@yahoo.com>
parents: 4499
diff changeset
   543
                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
   544
                                  % 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
   545
2725
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   546
        # Recommit the filtered changeset
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   547
        tr = repo.transaction('uncommit')
2941
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   548
        if interactive:
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   549
            opts['all'] = True
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   550
            newid = _interactiveuncommit(ui, repo, old, match)
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   551
        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
   552
            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
   553
            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
   554
            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
   555
                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
   556
                    opts['message'] = old.description()
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
   557
                message = cmdutil.logmessage(ui, opts)
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
   558
                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
   559
                                        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
   560
                                        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
   561
            if newid is 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
   562
                raise error.Abort(_('nothing to uncommit'),
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
   563
                                  hint=_("use --all to uncommit all files"))
2941
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   564
3220
f0f4cc2febac uncommit: add support for storing a note in obsmarker
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3219
diff changeset
   565
        # 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
   566
        metadata = {}
f0f4cc2febac uncommit: add support for storing a note in obsmarker
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3219
diff changeset
   567
        if opts.get('note'):
f0f4cc2febac uncommit: add support for storing a note in obsmarker
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3219
diff changeset
   568
            metadata['note'] = opts['note']
f0f4cc2febac uncommit: add support for storing a note in obsmarker
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3219
diff changeset
   569
4373
11bbf4dc694f uncommit: use scmtutil.cleanupnodes()
Martin von Zweigbergk <martinvonz@google.com>
parents: 4372
diff changeset
   570
        replacements = {old.node(): [newid]}
11bbf4dc694f uncommit: use scmtutil.cleanupnodes()
Martin von Zweigbergk <martinvonz@google.com>
parents: 4372
diff changeset
   571
        scmutil.cleanupnodes(repo, replacements, operation="uncommit",
11bbf4dc694f uncommit: use scmtutil.cleanupnodes()
Martin von Zweigbergk <martinvonz@google.com>
parents: 4372
diff changeset
   572
                             metadata=metadata)
2725
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   573
        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
   574
        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
   575
            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
   576
        else:
eacf6149b678 uncommit: add a new flag `--revert` to discard wdir changes after uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3298
diff changeset
   577
            with repo.dirstate.parentchange():
eacf6149b678 uncommit: add a new flag `--revert` to discard wdir changes after uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3298
diff changeset
   578
                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
   579
                _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
   580
        if not repo[newid].files():
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   581
            ui.warn(_("new changeset is empty\n"))
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   582
            ui.status(_("(use 'hg prune .' to remove it)\n"))
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   583
        tr.close()
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   584
    finally:
4eb90eace7f9 uncommit: move to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2724
diff changeset
   585
        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
   586
2941
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   587
def _interactiveuncommit(ui, repo, old, match):
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   588
    """ 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
   589
    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
   590
    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
   591
    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
   592
    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
   593
    """
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   594
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   595
    # 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
   596
    tempnode = _createtempcommit(ui, repo, old, match)
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
    diffopts = patch.difffeatureopts(repo.ui, whitespace=True)
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   599
    diffopts.nodates = True
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   600
    diffopts.git = True
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   601
    fp = stringio()
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   602
    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
   603
                                     opts=diffopts):
4463
7d54a538dd1e evolve: unindent some lines caught by flake8
Anton Shestakov <av6@dwimlabs.net>
parents: 4446
diff changeset
   604
        fp.write(chunk)
2941
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   605
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   606
    fp.seek(0)
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   607
    newnode = _patchtocommit(ui, repo, old, fp)
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   608
    # creating obs marker temp -> ()
3694
c0d5e0929f8b compat: from compatibility layer for createmarkers
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3675
diff changeset
   609
    obsolete.createmarkers(repo, [(repo[tempnode], ())], operation="uncommit")
2941
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   610
    return newnode
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   611
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   612
def _createtempcommit(ui, repo, old, match):
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   613
    """ 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
   614
    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
   615
    """
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   616
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   617
    pold = old.p1()
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   618
    # 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
   619
    # cmdutil.revert()
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   620
    diffopts = patch.difffeatureopts(repo.ui, whitespace=True)
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   621
    diffopts.nodates = True
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   622
    diffopts.git = True
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   623
    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
   624
    originalchunks = patch.parsepatch(diff)
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   625
    # 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
   626
    # uncommit a removed file partially.
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   627
    # 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
   628
    # 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
   629
    try:
90f94231db5d evolve: compat patch for recordfilter change in mercurial
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4491
diff changeset
   630
        chunks, opts = cmdutil.recordfilter(repo.ui, originalchunks, match,
90f94231db5d evolve: compat patch for recordfilter change in mercurial
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4491
diff changeset
   631
                                            operation='discard')
90f94231db5d evolve: compat patch for recordfilter change in mercurial
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4491
diff changeset
   632
    except TypeError:
90f94231db5d evolve: compat patch for recordfilter change in mercurial
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4491
diff changeset
   633
        # hg <= 4.9 (db72f9f6580e)
90f94231db5d evolve: compat patch for recordfilter change in mercurial
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4491
diff changeset
   634
        chunks, opts = cmdutil.recordfilter(repo.ui, originalchunks,
90f94231db5d evolve: compat patch for recordfilter change in mercurial
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4491
diff changeset
   635
                                            operation='discard')
2941
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   636
    if not chunks:
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   637
        raise error.Abort(_("nothing selected to uncommit"))
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   638
    fp = stringio()
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   639
    for c in chunks:
4463
7d54a538dd1e evolve: unindent some lines caught by flake8
Anton Shestakov <av6@dwimlabs.net>
parents: 4446
diff changeset
   640
        c.write(fp)
2941
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   641
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   642
    fp.seek(0)
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   643
    oldnode = node.hex(old.node())[:12]
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   644
    message = 'temporary commit for uncommiting %s' % oldnode
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   645
    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
   646
    return tempnode
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   647
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   648
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
   649
    """ 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
   650
    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
   651
    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
   652
    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
   653
    """
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   654
    pold = old.p1()
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   655
    parents = (old.p1().node(), old.p2().node())
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   656
    date = old.date()
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   657
    branch = old.branch()
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   658
    user = old.user()
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   659
    extra = old.extra()
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   660
    if extras:
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   661
        extra['uncommit_source'] = extras
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   662
    if not message:
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   663
        message = old.description()
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   664
    store = patch.filestore()
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   665
    try:
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   666
        files = set()
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   667
        try:
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   668
            patch.patchrepo(ui, repo, pold, store, fp, 1, '',
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   669
                            files=files, eolmode=None)
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   670
        except patch.PatchError as err:
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   671
            raise error.Abort(str(err))
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   672
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   673
        finally:
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   674
            del fp
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   675
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   676
        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
   677
                                filectxfn=store,
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   678
                                user=user,
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   679
                                date=date,
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   680
                                branch=branch,
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   681
                                extra=extra)
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   682
        newcm = memctx.commit()
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   683
    finally:
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   684
        store.close()
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   685
    return newcm
b0458b9e1b47 uncommit: add an interactive option to uncommit
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2940
diff changeset
   686
2760
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   687
@eh.command(
4191
5c734be63c3e evolve: adapt for changed @command decorator
Martin von Zweigbergk <martinvonz@google.com>
parents: 4126
diff changeset
   688
    'fold|squash',
4183
536c67823962 commands: adjust metavariables as appropriate
Anton Shestakov <av6@dwimlabs.net>
parents: 4126
diff changeset
   689
    [('r', 'rev', [], _("revision to fold"), _('REV')),
2761
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   690
     ('', 'exact', None, _("only fold specified revisions")),
3219
b73bd280b21c fold: add support for storing a note in obsmarker
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3218
diff changeset
   691
     ('', 'from', None, _("fold revisions linearly to working copy parent")),
4183
536c67823962 commands: adjust metavariables as appropriate
Anton Shestakov <av6@dwimlabs.net>
parents: 4126
diff changeset
   692
     ('n', 'note', '', _('store a note on fold'), _('TEXT')),
2768
85e5a56db776 fold: add support for the -D and -U options
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2766
diff changeset
   693
    ] + commitopts + commitopts2 + commitopts3,
4191
5c734be63c3e evolve: adapt for changed @command decorator
Martin von Zweigbergk <martinvonz@google.com>
parents: 4126
diff changeset
   694
    _('hg fold [OPTION]... [-r] REV'),
5c734be63c3e evolve: adapt for changed @command decorator
Martin von Zweigbergk <martinvonz@google.com>
parents: 4126
diff changeset
   695
    helpbasic=True)
2761
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   696
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
   697
    """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
   698
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   699
    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
   700
    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
   701
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   702
    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
   703
    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
   704
    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
   705
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   706
    .. container:: verbose
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   707
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   708
     Some examples:
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   709
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   710
     - 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
   711
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   712
         hg fold --from .^
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 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
   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 'draft()'
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   717
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   718
       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
   719
       :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
   720
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   721
     - 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
   722
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   723
         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
   724
2923
8c2d3c474fc6 doc: make paragraphs before example code end with "::" for reST syntax
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 2790
diff changeset
   725
     - 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
   726
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   727
        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
   728
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   729
     - 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
   730
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   731
         hg fold foo::@ --exact
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   732
    """
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
   733
    _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
   734
    _resolveoptions(ui, opts)
2761
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   735
    revs = list(revs)
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   736
    revs.extend(opts['rev'])
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   737
    if not revs:
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   738
        raise error.Abort(_('no revisions specified'))
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   739
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   740
    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
   741
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   742
    if opts['from'] and opts['exact']:
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   743
        raise error.Abort(_('cannot use both --from and --exact'))
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   744
    elif opts['from']:
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   745
        # Try to extend given revision starting from the working directory
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   746
        extrevs = repo.revs('(%ld::.) or (.::%ld)', revs, revs)
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   747
        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
   748
        if discardedrevs:
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   749
            msg = _("cannot fold non-linear revisions")
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   750
            hint = _("given revisions are unrelated to parent of working"
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   751
                     " directory")
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   752
            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
   753
        revs = extrevs
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   754
    elif opts['exact']:
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   755
        # 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
   756
        pass
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   757
    else:
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   758
        raise error.Abort(_('must specify either --from or --exact'))
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   759
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   760
    if not revs:
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   761
        raise error.Abort(_('specified revisions evaluate to an empty set'),
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   762
                          hint=_('use different revision arguments'))
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   763
    elif len(revs) == 1:
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   764
        ui.write_err(_('single revision specified, nothing to fold\n'))
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   765
        return 1
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   766
4340
5300be965515 fold: sort concatenated commit messages in topological order
Martin von Zweigbergk <martinvonz@google.com>
parents: 4300
diff changeset
   767
    # 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
   768
    # in topological order.
5300be965515 fold: sort concatenated commit messages in topological order
Martin von Zweigbergk <martinvonz@google.com>
parents: 4300
diff changeset
   769
    revs.sort()
5300be965515 fold: sort concatenated commit messages in topological order
Martin von Zweigbergk <martinvonz@google.com>
parents: 4300
diff changeset
   770
2761
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   771
    wlock = lock = None
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
        wlock = repo.wlock()
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   774
        lock = repo.lock()
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   775
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   776
        root, head = rewriteutil.foldcheck(repo, revs)
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   777
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   778
        tr = repo.transaction('fold')
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   779
        try:
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   780
            commitopts = opts.copy()
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   781
            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
   782
            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
   783
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   784
            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
   785
                commitopts['edit'] = False
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   786
            else:
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   787
                msgs = ["HG: This is a fold of %d changesets." % len(allctx)]
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   788
                msgs += ["HG: Commit message of changeset %s.\n\n%s\n" %
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   789
                         (c.rev(), c.description()) for c in allctx]
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   790
                commitopts['message'] = "\n".join(msgs)
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   791
                commitopts['edit'] = True
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   792
3219
b73bd280b21c fold: add support for storing a note in obsmarker
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3218
diff changeset
   793
            metadata = {}
b73bd280b21c fold: add support for storing a note in obsmarker
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3218
diff changeset
   794
            if opts.get('note'):
b73bd280b21c fold: add support for storing a note in obsmarker
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3218
diff changeset
   795
                metadata['note'] = opts['note']
b73bd280b21c fold: add support for storing a note in obsmarker
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3218
diff changeset
   796
2761
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   797
            newid, unusedvariable = rewriteutil.rewrite(repo, root, allctx,
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   798
                                                        head,
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   799
                                                        [root.p1().node(),
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   800
                                                         root.p2().node()],
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   801
                                                        commitopts=commitopts)
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   802
            phases.retractboundary(repo, tr, targetphase, [newid])
4370
45c0415e45b9 fold: use scmutil.cleanupnodes()
Martin von Zweigbergk <martinvonz@google.com>
parents: 4340
diff changeset
   803
            replacements = {ctx.node(): [newid] for ctx in allctx}
45c0415e45b9 fold: use scmutil.cleanupnodes()
Martin von Zweigbergk <martinvonz@google.com>
parents: 4340
diff changeset
   804
            scmutil.cleanupnodes(repo, replacements, operation="fold",
45c0415e45b9 fold: use scmutil.cleanupnodes()
Martin von Zweigbergk <martinvonz@google.com>
parents: 4340
diff changeset
   805
                                 metadata=metadata)
2761
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   806
            tr.close()
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   807
        finally:
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   808
            tr.release()
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   809
        ui.status('%i changesets folded\n' % len(revs))
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   810
        if repo['.'].rev() in revs:
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   811
            hg.update(repo, newid)
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
        lockmod.release(lock, wlock)
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   814
7450e360c88c commands: move fold to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2760
diff changeset
   815
@eh.command(
3229
63f6f9db9c3a help: remove a few commands from `hg` (no args) command list
Kyle Lippincott <spectral@google.com>
parents: 2949
diff changeset
   816
    'metaedit',
4183
536c67823962 commands: adjust metavariables as appropriate
Anton Shestakov <av6@dwimlabs.net>
parents: 4126
diff changeset
   817
    [('r', 'rev', [], _("revision to edit"), _('REV')),
2760
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   818
     ('', 'fold', None, _("also fold specified revisions into one")),
4183
536c67823962 commands: adjust metavariables as appropriate
Anton Shestakov <av6@dwimlabs.net>
parents: 4126
diff changeset
   819
     ('n', 'note', '', _('store a note on metaedit'), _('TEXT')),
2769
b96349ae3e2a metaedit: add support for the -D and -U options
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2768
diff changeset
   820
    ] + commitopts + commitopts2 + commitopts3,
2760
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   821
    _('hg metaedit [OPTION]... [-r] [REV]'))
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   822
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
   823
    """edit commit information
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   824
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   825
    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
   826
    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
   827
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   828
    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
   829
    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
   830
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   831
    .. container:: verbose
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   832
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   833
     Some examples:
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   834
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   835
     - 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
   836
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   837
         hg metaedit
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
     - 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
   840
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   841
         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
   842
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   843
     - 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
   844
       one::
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   845
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   846
         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
   847
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   848
       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
   849
       :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
   850
    """
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
   851
    _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
   852
    _resolveoptions(ui, opts)
2760
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   853
    revs = list(revs)
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   854
    revs.extend(opts['rev'])
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   855
    if not revs:
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   856
        if opts['fold']:
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   857
            raise error.Abort(_('revisions must be specified with --fold'))
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   858
        revs = ['.']
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   859
4642
a7c4f163656b cmdrewrite: use context manager for some locks and transactions
Martin von Zweigbergk <martinvonz@google.com>
parents: 4640
diff changeset
   860
    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
   861
        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
   862
        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
   863
            # 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
   864
            # 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
   865
            #
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   866
            #   a ---- b ---- c
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   867
            #
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   868
            # 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
   869
            # 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
   870
            # revisions in topological order.
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   871
            raise error.Abort(_('editing multiple revisions without --fold is '
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   872
                                'not currently supported'))
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
        if opts['fold']:
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   875
            root, head = rewriteutil.foldcheck(repo, revs)
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   876
        else:
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   877
            if repo.revs("%ld and public()", revs):
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   878
                raise error.Abort(_('cannot edit commit information for public '
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   879
                                    'revisions'))
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   880
            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
   881
            if newunstable:
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   882
                msg = _('cannot edit commit information in the middle'
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   883
                        ' of a stack')
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   884
                hint = _('%s will become unstable and new unstable changes'
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   885
                         ' are not allowed')
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   886
                hint %= repo[newunstable.first()]
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   887
                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
   888
            root = head = repo[revs.first()]
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   889
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   890
        wctx = repo[None]
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   891
        p1 = wctx.p1()
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   892
        tr = repo.transaction('metaedit')
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   893
        newp1 = None
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   894
        try:
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   895
            commitopts = opts.copy()
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   896
            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
   897
            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
   898
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   899
            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
   900
                commitopts['edit'] = False
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   901
            else:
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   902
                if opts['fold']:
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   903
                    msgs = ["HG: This is a fold of %d changesets." % len(allctx)]
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   904
                    msgs += ["HG: Commit message of changeset %s.\n\n%s\n" %
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   905
                             (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
   906
                else:
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   907
                    msgs = [head.description()]
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   908
                commitopts['message'] = "\n".join(msgs)
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   909
                commitopts['edit'] = True
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   910
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   911
            # TODO: if the author and message are the same, don't create a new
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   912
            # hash. Right now we create a new hash because the date can be
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   913
            # different.
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   914
            newid, created = rewriteutil.rewrite(repo, root, allctx, head,
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   915
                                                 [root.p1().node(),
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   916
                                                  root.p2().node()],
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   917
                                                 commitopts=commitopts)
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   918
            if created:
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   919
                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
   920
                    newp1 = newid
3218
772cc931f085 metaedit: add support for storing a note in obsmarker
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3217
diff changeset
   921
                # 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
   922
                metadata = {}
772cc931f085 metaedit: add support for storing a note in obsmarker
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3217
diff changeset
   923
                if opts.get('note'):
772cc931f085 metaedit: add support for storing a note in obsmarker
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3217
diff changeset
   924
                    metadata['note'] = opts['note']
772cc931f085 metaedit: add support for storing a note in obsmarker
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3217
diff changeset
   925
2760
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   926
                phases.retractboundary(repo, tr, targetphase, [newid])
3694
c0d5e0929f8b compat: from compatibility layer for createmarkers
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3675
diff changeset
   927
                obsolete.createmarkers(repo, [(ctx, (repo[newid],))
c0d5e0929f8b compat: from compatibility layer for createmarkers
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3675
diff changeset
   928
                                              for ctx in allctx],
c0d5e0929f8b compat: from compatibility layer for createmarkers
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3675
diff changeset
   929
                                       metadata=metadata, operation="metaedit")
2760
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   930
            else:
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   931
                ui.status(_("nothing changed\n"))
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   932
            tr.close()
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   933
        finally:
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   934
            tr.release()
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   935
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   936
        if opts['fold']:
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   937
            ui.status('%i changesets folded\n' % len(revs))
ddff53ecc00b commands: move metaedit to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2756
diff changeset
   938
        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
   939
            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
   940
2766
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
   941
metadataopts = [
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
   942
    ('d', 'date', '',
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
   943
     _('record the specified date in metadata'), _('DATE')),
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
   944
    ('u', 'user', '',
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
   945
     _('record the specified user in metadata'), _('USER')),
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
   946
]
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
   947
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
   948
def _getmetadata(**opts):
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
   949
    metadata = {}
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
   950
    date = opts.get('date')
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
   951
    user = opts.get('user')
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
   952
    if date:
3514
498f782ccb4b compat: add compat layer for date related functions
Boris Feld <boris.feld@octobus.net>
parents: 3506
diff changeset
   953
        metadata['date'] = '%i %i' % compat.parsedate(date)
2766
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
   954
    if user:
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
   955
        metadata['user'] = user
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
   956
    return metadata
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
   957
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
   958
@eh.command(
4191
5c734be63c3e evolve: adapt for changed @command decorator
Martin von Zweigbergk <martinvonz@google.com>
parents: 4126
diff changeset
   959
    'prune|obsolete',
2766
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
   960
    [('n', 'new', [], _("successor changeset (DEPRECATED)")),
4183
536c67823962 commands: adjust metavariables as appropriate
Anton Shestakov <av6@dwimlabs.net>
parents: 4126
diff changeset
   961
     ('s', 'succ', [], _("successor changeset"), _('REV')),
536c67823962 commands: adjust metavariables as appropriate
Anton Shestakov <av6@dwimlabs.net>
parents: 4126
diff changeset
   962
     ('r', 'rev', [], _("revisions to prune"), _('REV')),
2766
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
   963
     ('k', 'keep', None, _("does not modify working copy during prune")),
4183
536c67823962 commands: adjust metavariables as appropriate
Anton Shestakov <av6@dwimlabs.net>
parents: 4126
diff changeset
   964
     ('n', 'note', '', _('store a note on prune'), _('TEXT')),
4045
821e4be40c6c prune: replace --biject with --pair
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3990
diff changeset
   965
     ('', 'pair', False, _("record a pairing, such as a rebase or divergence resolution "
821e4be40c6c prune: replace --biject with --pair
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3990
diff changeset
   966
                           "(pairing multiple precursors to multiple successors)")),
821e4be40c6c prune: replace --biject with --pair
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3990
diff changeset
   967
     ('', 'biject', False, _("alias to --pair (DEPRECATED)")),
2766
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
   968
     ('', 'fold', False,
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
   969
      _("record a fold (multiple precursors, one successors)")),
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
   970
     ('', 'split', False,
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
   971
      _("record a split (on precursor, multiple successors)")),
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
   972
     ('B', 'bookmark', [], _("remove revs only reachable from given"
4183
536c67823962 commands: adjust metavariables as appropriate
Anton Shestakov <av6@dwimlabs.net>
parents: 4126
diff changeset
   973
                             " bookmark"), _('BOOKMARK'))] + metadataopts,
4191
5c734be63c3e evolve: adapt for changed @command decorator
Martin von Zweigbergk <martinvonz@google.com>
parents: 4126
diff changeset
   974
    _('[OPTION] [-r] REV...'),
5c734be63c3e evolve: adapt for changed @command decorator
Martin von Zweigbergk <martinvonz@google.com>
parents: 4126
diff changeset
   975
    helpbasic=True)
2766
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
   976
# 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
   977
def cmdprune(ui, repo, *revs, **opts):
3989
b60113460856 prune: improve help first line
Dan Villiom Podlaski Christiansen <dan@magenta.dk>
parents: 3988
diff changeset
   978
    """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
   979
3990
566fe891f1ec prune: more human-friendly documentation
Dan Villiom Podlaski Christiansen <dan@magenta.dk>
parents: 3989
diff changeset
   980
    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
   981
    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
   982
    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
   983
    "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
   984
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
   985
    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
   986
    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
   987
3984
33f795a17622 prune: use ``-s/--succ`` in documentation
Dan Villiom Podlaski Christiansen <dan@magenta.dk>
parents: 3877
diff changeset
   988
    You can use ``-s/--succ`` to tell Mercurial that a newer version (successor) of the
2766
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
   989
    pruned changeset exists. Mercurial records successor revisions in obsolescence
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
   990
    markers.
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
   991
3988
3e4fbda6bdce prune: try to clarify the "multi" revisions case
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3987
diff changeset
   992
    If you prune a single revision and specify multiple revisions in
3e4fbda6bdce prune: try to clarify the "multi" revisions case
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3987
diff changeset
   993
    ``-s/--succ``, you are recording a "split" and must acknowledge it by
3e4fbda6bdce prune: try to clarify the "multi" revisions case
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3987
diff changeset
   994
    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
   995
    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
   996
3988
3e4fbda6bdce prune: try to clarify the "multi" revisions case
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3987
diff changeset
   997
    If you want to supersede multiple revisions at the same time, use
4045
821e4be40c6c prune: replace --biject with --pair
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3990
diff changeset
   998
    ``--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
   999
    This is commonly useful for resolving history divergence, or when someone
3e4fbda6bdce prune: try to clarify the "multi" revisions case
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3987
diff changeset
  1000
    else does edits history without obsolescence enabled.
2766
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1001
    """
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
  1002
    _checknotesize(ui, opts)
2766
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1003
    revs = scmutil.revrange(repo, list(revs) + opts.get('rev'))
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1004
    succs = opts['new'] + opts['succ']
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1005
    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
  1006
    metadata = _getmetadata(**opts)
4045
821e4be40c6c prune: replace --biject with --pair
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3990
diff changeset
  1007
    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
  1008
    fold = opts.get('fold')
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1009
    split = opts.get('split')
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1010
4045
821e4be40c6c prune: replace --biject with --pair
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3990
diff changeset
  1011
    options = [o for o in ('pair', 'fold', '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
  1012
    if 1 < len(options):
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1013
        raise error.Abort(_("can only specify one of %s") % ', '.join(options))
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1014
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1015
    if bookmarks:
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1016
        reachablefrombookmark = rewriteutil.reachablefrombookmark
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1017
        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
  1018
        if not revs:
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1019
            # 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
  1020
            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
  1021
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1022
    if not revs:
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1023
        raise error.Abort(_('nothing to prune'))
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1024
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1025
    wlock = lock = tr = None
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1026
    try:
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1027
        wlock = repo.wlock()
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1028
        lock = repo.lock()
4445
0f2305921e4d prune: use action "prune", not "touch", in precheck
Martin von Zweigbergk <martinvonz@google.com>
parents: 4423
diff changeset
  1029
        rewriteutil.precheck(repo, revs, 'prune')
2766
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1030
        tr = repo.transaction('prune')
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1031
        # defines pruned changesets
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1032
        precs = []
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1033
        revs.sort()
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1034
        for p in revs:
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1035
            cp = repo[p]
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1036
            precs.append(cp)
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1037
        if not precs:
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1038
            raise error.Abort('nothing to prune')
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1039
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1040
        # defines successors changesets
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1041
        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
  1042
        sucs.sort()
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1043
        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
  1044
        if not biject and len(sucs) > 1 and len(precs) > 1:
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1045
            msg = "Can't use multiple successors for multiple precursors"
4045
821e4be40c6c prune: replace --biject with --pair
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3990
diff changeset
  1046
            hint = _("use --pair to mark a series as a replacement"
2766
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1047
                     " for another")
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1048
            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
  1049
        elif biject and len(sucs) != len(precs):
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1050
            msg = "Can't use %d successors for %d precursors" \
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1051
                % (len(sucs), len(precs))
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1052
            raise error.Abort(msg)
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1053
        elif (len(precs) == 1 and len(sucs) > 1) and not split:
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1054
            msg = "please add --split if you want to do a split"
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1055
            raise error.Abort(msg)
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1056
        elif len(sucs) == 1 and len(precs) > 1 and not fold:
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1057
            msg = "please add --fold if you want to do a fold"
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1058
            raise error.Abort(msg)
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1059
        elif biject:
4374
86bf0b106949 prune: use scmutil.cleanupnodes()
Martin von Zweigbergk <martinvonz@google.com>
parents: 4373
diff changeset
  1060
            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
  1061
        else:
4374
86bf0b106949 prune: use scmutil.cleanupnodes()
Martin von Zweigbergk <martinvonz@google.com>
parents: 4373
diff changeset
  1062
            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
  1063
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1064
        wdp = repo['.']
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1065
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1066
        if len(sucs) == 1 and len(precs) == 1 and wdp in precs:
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1067
            # '.' killed, so update to the successor
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1068
            newnode = sucs[0]
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1069
        else:
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1070
            # update to an unkilled parent
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1071
            newnode = wdp
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1072
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1073
            while newnode in precs or newnode.obsolete():
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1074
                newnode = newnode.parents()[0]
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1075
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1076
        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
  1077
            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
  1078
                # 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
  1079
                # 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
  1080
                # common at some point.
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1081
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1082
                # 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
  1083
                # between the working context and uctx
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1084
                descendantrevs = repo.revs("%d::." % newnode.rev())
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1085
                changedfiles = []
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1086
                for rev in descendantrevs:
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1087
                    # 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
  1088
                    # changed
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1089
                    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
  1090
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1091
                # 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
  1092
                dirstate = repo.dirstate
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1093
                dirchanges = [f for f in dirstate if dirstate[f] != 'n']
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1094
                changedfiles.extend(dirchanges)
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1095
                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
  1096
                                      changedfiles)
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1097
                dirstate.write(tr)
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1098
            else:
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1099
                bookactive = repo._activebookmark
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1100
                # 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
  1101
                # 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
  1102
                # after
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1103
                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
  1104
                if movebookmark:
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1105
                    bookmarksmod.deactivate(repo)
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1106
                    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
  1107
                    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
  1108
                commands.update(ui, repo, newnode.hex())
4613
30a544904238 messages: standardize on "working copy is now at" including "is"
Martin von Zweigbergk <martinvonz@google.com>
parents: 4506
diff changeset
  1109
                ui.status(_('working directory is now at %s\n')
2766
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1110
                          % ui.label(str(newnode), 'evolve.node'))
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1111
                if movebookmark:
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1112
                    bookmarksmod.activate(repo, bookactive)
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
        # update bookmarks
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1115
        if bookmarks:
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1116
            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
  1117
3217
b6ba296532cb prune: add support for storing a note in obsmarker
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3216
diff changeset
  1118
        # store note in metadata
b6ba296532cb prune: add support for storing a note in obsmarker
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3216
diff changeset
  1119
        if opts.get('note'):
b6ba296532cb prune: add support for storing a note in obsmarker
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3216
diff changeset
  1120
            metadata['note'] = opts['note']
b6ba296532cb prune: add support for storing a note in obsmarker
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3216
diff changeset
  1121
3506
6b4272bbb65d evolve: update code for not implicitly converting ctx to revision
Boris Feld <boris.feld@octobus.net>
parents: 3484
diff changeset
  1122
        precrevs = (precursor.rev() for precursor in precs)
4374
86bf0b106949 prune: use scmutil.cleanupnodes()
Martin von Zweigbergk <martinvonz@google.com>
parents: 4373
diff changeset
  1123
        moves = {}
3506
6b4272bbb65d evolve: update code for not implicitly converting ctx to revision
Boris Feld <boris.feld@octobus.net>
parents: 3484
diff changeset
  1124
        for ctx in repo.unfiltered().set('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
  1125
            # used to be:
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1126
            #
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1127
            #   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
  1128
            #   if ldest:
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1129
            #      c = ldest[0]
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1130
            #
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1131
            # 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
  1132
            # 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
  1133
            for dest in ctx.ancestors():
4374
86bf0b106949 prune: use scmutil.cleanupnodes()
Martin von Zweigbergk <martinvonz@google.com>
parents: 4373
diff changeset
  1134
                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
  1135
                    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
  1136
                    break
4374
86bf0b106949 prune: use scmutil.cleanupnodes()
Martin von Zweigbergk <martinvonz@google.com>
parents: 4373
diff changeset
  1137
        scmutil.cleanupnodes(repo, replacements, operation="prune", moves=moves,
86bf0b106949 prune: use scmutil.cleanupnodes()
Martin von Zweigbergk <martinvonz@google.com>
parents: 4373
diff changeset
  1138
                             metadata=metadata)
86bf0b106949 prune: use scmutil.cleanupnodes()
Martin von Zweigbergk <martinvonz@google.com>
parents: 4373
diff changeset
  1139
86bf0b106949 prune: use scmutil.cleanupnodes()
Martin von Zweigbergk <martinvonz@google.com>
parents: 4373
diff changeset
  1140
        # informs that changeset have been pruned
86bf0b106949 prune: use scmutil.cleanupnodes()
Martin von Zweigbergk <martinvonz@google.com>
parents: 4373
diff changeset
  1141
        ui.status(_('%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
  1142
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1143
        tr.close()
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1144
    finally:
83ad13719e26 commands: move 'prune' to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2763
diff changeset
  1145
        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
  1146
2762
610581a2fb74 commands: move split to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2761
diff changeset
  1147
@eh.command(
4191
5c734be63c3e evolve: adapt for changed @command decorator
Martin von Zweigbergk <martinvonz@google.com>
parents: 4126
diff changeset
  1148
    'split',
4300
702f7e1d0b01 split: add a --interactive flag
Boris Feld <boris.feld@octobus.net>
parents: 4299
diff changeset
  1149
    [('i', 'interactive', True, _('use interactive mode')),
702f7e1d0b01 split: add a --interactive flag
Boris Feld <boris.feld@octobus.net>
parents: 4299
diff changeset
  1150
     ('r', 'rev', [], _("revision to split"), _('REV')),
4183
536c67823962 commands: adjust metavariables as appropriate
Anton Shestakov <av6@dwimlabs.net>
parents: 4126
diff changeset
  1151
     ('n', 'note', '', _("store a note on split"), _('TEXT')),
2771
6044bd16bfb7 split: add support for the -D and -U option
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2770
diff changeset
  1152
    ] + commitopts + commitopts2 + commitopts3,
4299
4af0235e7b0b split: take file patterns to limit selection on matching file patterns
Boris Feld <boris.feld@octobus.net>
parents: 4298
diff changeset
  1153
    _('hg split [OPTION] [-r REV] [FILES]'),
4191
5c734be63c3e evolve: adapt for changed @command decorator
Martin von Zweigbergk <martinvonz@google.com>
parents: 4126
diff changeset
  1154
    helpbasic=True)
4299
4af0235e7b0b split: take file patterns to limit selection on matching file patterns
Boris Feld <boris.feld@octobus.net>
parents: 4298
diff changeset
  1155
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
  1156
    """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
  1157
610581a2fb74 commands: move split to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2761
diff changeset
  1158
    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
  1159
    redistributed into new changesets.
610581a2fb74 commands: move split to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2761
diff changeset
  1160
610581a2fb74 commands: move split to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2761
diff changeset
  1161
    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
  1162
4af0235e7b0b split: take file patterns to limit selection on matching file patterns
Boris Feld <boris.feld@octobus.net>
parents: 4298
diff changeset
  1163
    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
  1164
    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
  1165
    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
  1166
    """
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
  1167
    _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
  1168
    _resolveoptions(ui, opts)
2762
610581a2fb74 commands: move split to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2761
diff changeset
  1169
    tr = wlock = lock = None
610581a2fb74 commands: move split to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2761
diff changeset
  1170
    newcommits = []
4300
702f7e1d0b01 split: add a --interactive flag
Boris Feld <boris.feld@octobus.net>
parents: 4299
diff changeset
  1171
    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
  1172
4298
a98fa6c0d4f3 split: only accept explicit revision through the `-r` option
Boris Feld <boris.feld@octobus.net>
parents: 4294
diff changeset
  1173
    revs = opts.get('rev') or '.'
a98fa6c0d4f3 split: only accept explicit revision through the `-r` option
Boris Feld <boris.feld@octobus.net>
parents: 4294
diff changeset
  1174
    if not revs:
a98fa6c0d4f3 split: only accept explicit revision through the `-r` option
Boris Feld <boris.feld@octobus.net>
parents: 4294
diff changeset
  1175
        revarg = '.'
a98fa6c0d4f3 split: only accept explicit revision through the `-r` option
Boris Feld <boris.feld@octobus.net>
parents: 4294
diff changeset
  1176
    elif len(revs) == 1:
a98fa6c0d4f3 split: only accept explicit revision through the `-r` option
Boris Feld <boris.feld@octobus.net>
parents: 4294
diff changeset
  1177
        revarg = revs[0]
a98fa6c0d4f3 split: only accept explicit revision through the `-r` option
Boris Feld <boris.feld@octobus.net>
parents: 4294
diff changeset
  1178
    else:
a98fa6c0d4f3 split: only accept explicit revision through the `-r` option
Boris Feld <boris.feld@octobus.net>
parents: 4294
diff changeset
  1179
        # 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
  1180
        # complains here instead of just taking the last value.
a98fa6c0d4f3 split: only accept explicit revision through the `-r` option
Boris Feld <boris.feld@octobus.net>
parents: 4294
diff changeset
  1181
        raise error.Abort(_('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
  1182
3270
e6150b9b88d9 split: force the branch to fix the split bug
Boris Feld <boris.feld@octobus.net>
parents: 3233
diff changeset
  1183
    # 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
  1184
    savedbranch = repo.dirstate.branch()
e6150b9b88d9 split: force the branch to fix the split bug
Boris Feld <boris.feld@octobus.net>
parents: 3233
diff changeset
  1185
2762
610581a2fb74 commands: move split to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2761
diff changeset
  1186
    try:
610581a2fb74 commands: move split to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2761
diff changeset
  1187
        wlock = repo.wlock()
610581a2fb74 commands: move split to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2761
diff changeset
  1188
        lock = repo.lock()
4298
a98fa6c0d4f3 split: only accept explicit revision through the `-r` option
Boris Feld <boris.feld@octobus.net>
parents: 4294
diff changeset
  1189
        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
  1190
        rev = ctx.rev()
2762
610581a2fb74 commands: move split to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2761
diff changeset
  1191
        cmdutil.bailifchanged(repo)
2786
ae690d39fc92 split: use precheck to validate revision
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2774
diff changeset
  1192
        rewriteutil.precheck(repo, [rev], action='split')
2762
610581a2fb74 commands: move split to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2761
diff changeset
  1193
        tr = repo.transaction('split')
4423
144cd06029de split: use ui.configoverride to preserve phase while commiting
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4410
diff changeset
  1194
        # make sure we respect the phase while splitting
144cd06029de split: use ui.configoverride to preserve phase while commiting
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4410
diff changeset
  1195
        overrides = {('phases', '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
  1196
610581a2fb74 commands: move split to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2761
diff changeset
  1197
        if len(ctx.parents()) > 1:
610581a2fb74 commands: move split to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2761
diff changeset
  1198
            raise error.Abort(_("cannot split merge commits"))
610581a2fb74 commands: move split to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2761
diff changeset
  1199
        prev = ctx.p1()
610581a2fb74 commands: move split to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2761
diff changeset
  1200
        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
  1201
        bookactive = repo._activebookmark
610581a2fb74 commands: move split to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2761
diff changeset
  1202
        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
  1203
            repo.ui.status(_("(leaving bookmark %s)\n") % repo._activebookmark)
610581a2fb74 commands: move split to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2761
diff changeset
  1204
        bookmarksmod.deactivate(repo)
610581a2fb74 commands: move split to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2761
diff changeset
  1205
610581a2fb74 commands: move split to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2761
diff changeset
  1206
        # Prepare the working directory
610581a2fb74 commands: move split to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2761
diff changeset
  1207
        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
  1208
4299
4af0235e7b0b split: take file patterns to limit selection on matching file patterns
Boris Feld <boris.feld@octobus.net>
parents: 4298
diff changeset
  1209
        def haschanges(matcher=None):
4af0235e7b0b split: take file patterns to limit selection on matching file patterns
Boris Feld <boris.feld@octobus.net>
parents: 4298
diff changeset
  1210
            modified, added, removed, deleted = repo.status(match=matcher)[:4]
2762
610581a2fb74 commands: move split to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2761
diff changeset
  1211
            return modified or added or removed or deleted
610581a2fb74 commands: move split to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2761
diff changeset
  1212
        msg = ("HG: This is the original pre-split commit message. "
610581a2fb74 commands: move split to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2761
diff changeset
  1213
               "Edit it as appropriate.\n\n")
610581a2fb74 commands: move split to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2761
diff changeset
  1214
        msg += ctx.description()
610581a2fb74 commands: move split to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2761
diff changeset
  1215
        opts['message'] = msg
610581a2fb74 commands: move split to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2761
diff changeset
  1216
        opts['edit'] = True
2770
a9ea16a1f4dc split: fix the --user option
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2769
diff changeset
  1217
        if not opts['user']:
a9ea16a1f4dc split: fix the --user option
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2769
diff changeset
  1218
            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
  1219
e6150b9b88d9 split: force the branch to fix the split bug
Boris Feld <boris.feld@octobus.net>
parents: 3233
diff changeset
  1220
        # Set the right branch
e6150b9b88d9 split: force the branch to fix the split bug
Boris Feld <boris.feld@octobus.net>
parents: 3233
diff changeset
  1221
        # 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
  1222
        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
  1223
4299
4af0235e7b0b split: take file patterns to limit selection on matching file patterns
Boris Feld <boris.feld@octobus.net>
parents: 4298
diff changeset
  1224
        if pats:
4af0235e7b0b split: take file patterns to limit selection on matching file patterns
Boris Feld <boris.feld@octobus.net>
parents: 4298
diff changeset
  1225
            # 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
  1226
            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
  1227
        else:
4af0235e7b0b split: take file patterns to limit selection on matching file patterns
Boris Feld <boris.feld@octobus.net>
parents: 4298
diff changeset
  1228
            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
  1229
2762
610581a2fb74 commands: move split to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2761
diff changeset
  1230
        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
  1231
4af0235e7b0b split: take file patterns to limit selection on matching file patterns
Boris Feld <boris.feld@octobus.net>
parents: 4298
diff changeset
  1232
            if haschanges(matcher):
4300
702f7e1d0b01 split: add a --interactive flag
Boris Feld <boris.feld@octobus.net>
parents: 4299
diff changeset
  1233
                if iselect:
4423
144cd06029de split: use ui.configoverride to preserve phase while commiting
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4410
diff changeset
  1234
                    with repo.ui.configoverride(overrides, 'split'):
144cd06029de split: use ui.configoverride to preserve phase while commiting
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4410
diff changeset
  1235
                        cmdutil.dorecord(ui, repo, commands.commit, 'commit',
144cd06029de split: use ui.configoverride to preserve phase while commiting
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4410
diff changeset
  1236
                                         False, cmdutil.recordfilter, *pats,
144cd06029de split: use ui.configoverride to preserve phase while commiting
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4410
diff changeset
  1237
                                         **opts)
4300
702f7e1d0b01 split: add a --interactive flag
Boris Feld <boris.feld@octobus.net>
parents: 4299
diff changeset
  1238
                    # 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
  1239
                    # We should make dorecord return the newly created commit
702f7e1d0b01 split: add a --interactive flag
Boris Feld <boris.feld@octobus.net>
parents: 4299
diff changeset
  1240
                    newcommits.append(repo['.'])
702f7e1d0b01 split: add a --interactive flag
Boris Feld <boris.feld@octobus.net>
parents: 4299
diff changeset
  1241
                elif not pats:
702f7e1d0b01 split: add a --interactive flag
Boris Feld <boris.feld@octobus.net>
parents: 4299
diff changeset
  1242
                    msg = _("no files of directories specified")
702f7e1d0b01 split: add a --interactive flag
Boris Feld <boris.feld@octobus.net>
parents: 4299
diff changeset
  1243
                    hint = _("do you want --interactive")
702f7e1d0b01 split: add a --interactive flag
Boris Feld <boris.feld@octobus.net>
parents: 4299
diff changeset
  1244
                    raise error.Abort(msg, hint=hint)
702f7e1d0b01 split: add a --interactive flag
Boris Feld <boris.feld@octobus.net>
parents: 4299
diff changeset
  1245
                else:
4423
144cd06029de split: use ui.configoverride to preserve phase while commiting
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4410
diff changeset
  1246
                    with repo.ui.configoverride(overrides, 'split'):
144cd06029de split: use ui.configoverride to preserve phase while commiting
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4410
diff changeset
  1247
                        commands.commit(ui, repo, *pats, **opts)
4300
702f7e1d0b01 split: add a --interactive flag
Boris Feld <boris.feld@octobus.net>
parents: 4299
diff changeset
  1248
                    newcommits.append(repo['.'])
4299
4af0235e7b0b split: take file patterns to limit selection on matching file patterns
Boris Feld <boris.feld@octobus.net>
parents: 4298
diff changeset
  1249
            if pats:
4af0235e7b0b split: take file patterns to limit selection on matching file patterns
Boris Feld <boris.feld@octobus.net>
parents: 4298
diff changeset
  1250
                # 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
  1251
                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
  1252
            else:
4af0235e7b0b split: take file patterns to limit selection on matching file patterns
Boris Feld <boris.feld@octobus.net>
parents: 4298
diff changeset
  1253
                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
  1254
4af0235e7b0b split: take file patterns to limit selection on matching file patterns
Boris Feld <boris.feld@octobus.net>
parents: 4298
diff changeset
  1255
            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
  1256
                nextaction = None
8f54ab5dd4e2 split: add a help entry to the final prompt
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4236
diff changeset
  1257
                while nextaction is None:
4294
8974a05a49fa split: rework the prompt to be more standard
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4293
diff changeset
  1258
                    nextaction = ui.prompt('continue splitting? [Ycdq?]', default='y')
8974a05a49fa split: rework the prompt to be more standard
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4293
diff changeset
  1259
                    if nextaction == 'c':
4423
144cd06029de split: use ui.configoverride to preserve phase while commiting
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4410
diff changeset
  1260
                        with repo.ui.configoverride(overrides, 'split'):
144cd06029de split: use ui.configoverride to preserve phase while commiting
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4410
diff changeset
  1261
                            commands.commit(ui, repo, **opts)
4291
8f54ab5dd4e2 split: add a help entry to the final prompt
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4236
diff changeset
  1262
                        newcommits.append(repo['.'])
8f54ab5dd4e2 split: add a help entry to the final prompt
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4236
diff changeset
  1263
                        break
4292
caaa89adf3eb split: add a way to abort a split from the prompt
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4291
diff changeset
  1264
                    elif nextaction == 'q':
caaa89adf3eb split: add a way to abort a split from the prompt
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4291
diff changeset
  1265
                        raise error.Abort(_('user quit'))
4293
d4902d48d095 split: add a option to discard remaining change during split
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4292
diff changeset
  1266
                    elif nextaction == 'd':
d4902d48d095 split: add a option to discard remaining change during split
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4292
diff changeset
  1267
                        # 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
  1268
                        # 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
  1269
                        # 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
  1270
                        # prompting for confirmation
d4902d48d095 split: add a option to discard remaining change during split
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4292
diff changeset
  1271
                        ui.status(_('discarding remaining changes\n'))
d4902d48d095 split: add a option to discard remaining change during split
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4292
diff changeset
  1272
                        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
  1273
                        if pats:
4af0235e7b0b split: take file patterns to limit selection on matching file patterns
Boris Feld <boris.feld@octobus.net>
parents: 4298
diff changeset
  1274
                            status = repo.status(match=matcher)[:4]
4af0235e7b0b split: take file patterns to limit selection on matching file patterns
Boris Feld <boris.feld@octobus.net>
parents: 4298
diff changeset
  1275
                            dirty = set()
4af0235e7b0b split: take file patterns to limit selection on matching file patterns
Boris Feld <boris.feld@octobus.net>
parents: 4298
diff changeset
  1276
                            for i in status:
4af0235e7b0b split: take file patterns to limit selection on matching file patterns
Boris Feld <boris.feld@octobus.net>
parents: 4298
diff changeset
  1277
                                dirty.update(i)
4af0235e7b0b split: take file patterns to limit selection on matching file patterns
Boris Feld <boris.feld@octobus.net>
parents: 4298
diff changeset
  1278
                            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
  1279
                            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
  1280
                                           (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
  1281
                        else:
4af0235e7b0b split: take file patterns to limit selection on matching file patterns
Boris Feld <boris.feld@octobus.net>
parents: 4298
diff changeset
  1282
                            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
  1283
                                           (target, node.nullid), all=True)
4291
8f54ab5dd4e2 split: add a help entry to the final prompt
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4236
diff changeset
  1284
                    elif nextaction == '?':
8f54ab5dd4e2 split: add a help entry to the final prompt
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4236
diff changeset
  1285
                        nextaction = None
4294
8974a05a49fa split: rework the prompt to be more standard
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4293
diff changeset
  1286
                        ui.write(_("y - yes, continue selection\n"))
8974a05a49fa split: rework the prompt to be more standard
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4293
diff changeset
  1287
                        ui.write(_("c - commit, select all 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
  1288
                        ui.write(_("d - discard, discard remaining changes\n"))
4292
caaa89adf3eb split: add a way to abort a split from the prompt
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4291
diff changeset
  1289
                        ui.write(_("q - quit, abort the split\n"))
4291
8f54ab5dd4e2 split: add a help entry to the final prompt
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4236
diff changeset
  1290
                        ui.write(_("? - ?, display help\n"))
8f54ab5dd4e2 split: add a help entry to the final prompt
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4236
diff changeset
  1291
                else:
8f54ab5dd4e2 split: add a help entry to the final prompt
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4236
diff changeset
  1292
                    continue
8f54ab5dd4e2 split: add a help entry to the final prompt
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4236
diff changeset
  1293
                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
  1294
            else:
610581a2fb74 commands: move split to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2761
diff changeset
  1295
                ui.status(_("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
  1296
                if haschanges():
4af0235e7b0b split: take file patterns to limit selection on matching file patterns
Boris Feld <boris.feld@octobus.net>
parents: 4298
diff changeset
  1297
                    # 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
  1298
                    # that we create another commit with remaining changes?
4423
144cd06029de split: use ui.configoverride to preserve phase while commiting
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4410
diff changeset
  1299
                    with repo.ui.configoverride(overrides, 'split'):
144cd06029de split: use ui.configoverride to preserve phase while commiting
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4410
diff changeset
  1300
                        commands.commit(ui, repo, **opts)
4299
4af0235e7b0b split: take file patterns to limit selection on matching file patterns
Boris Feld <boris.feld@octobus.net>
parents: 4298
diff changeset
  1301
                    newcommits.append(repo['.'])
2762
610581a2fb74 commands: move split to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2761
diff changeset
  1302
        if newcommits:
610581a2fb74 commands: move split to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2761
diff changeset
  1303
            tip = repo[newcommits[-1]]
610581a2fb74 commands: move split to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2761
diff changeset
  1304
            bmupdate(tip.node())
610581a2fb74 commands: move split to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2761
diff changeset
  1305
            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
  1306
                bookmarksmod.activate(repo, bookactive)
3216
13cb0810ce22 split: add support for storing a note in obsmarker
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3215
diff changeset
  1307
            metadata = {}
13cb0810ce22 split: add support for storing a note in obsmarker
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3215
diff changeset
  1308
            if opts.get('note'):
13cb0810ce22 split: add support for storing a note in obsmarker
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3215
diff changeset
  1309
                metadata['note'] = opts['note']
3694
c0d5e0929f8b compat: from compatibility layer for createmarkers
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3675
diff changeset
  1310
            obsolete.createmarkers(repo, [(repo[rev], newcommits)],
c0d5e0929f8b compat: from compatibility layer for createmarkers
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3675
diff changeset
  1311
                                   metadata=metadata, operation="split")
2762
610581a2fb74 commands: move split to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2761
diff changeset
  1312
        tr.close()
610581a2fb74 commands: move split to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2761
diff changeset
  1313
    finally:
3270
e6150b9b88d9 split: force the branch to fix the split bug
Boris Feld <boris.feld@octobus.net>
parents: 3233
diff changeset
  1314
        # Restore the old branch
e6150b9b88d9 split: force the branch to fix the split bug
Boris Feld <boris.feld@octobus.net>
parents: 3233
diff changeset
  1315
        repo.dirstate.setbranch(savedbranch)
e6150b9b88d9 split: force the branch to fix the split bug
Boris Feld <boris.feld@octobus.net>
parents: 3233
diff changeset
  1316
2762
610581a2fb74 commands: move split to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2761
diff changeset
  1317
        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
  1318
4a5b0c373e65 commands: move the touch to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2762
diff changeset
  1319
@eh.command(
3229
63f6f9db9c3a help: remove a few commands from `hg` (no args) command list
Kyle Lippincott <spectral@google.com>
parents: 2949
diff changeset
  1320
    'touch',
4183
536c67823962 commands: adjust metavariables as appropriate
Anton Shestakov <av6@dwimlabs.net>
parents: 4126
diff changeset
  1321
    [('r', 'rev', [], _('revision to update'), _('REV')),
536c67823962 commands: adjust metavariables as appropriate
Anton Shestakov <av6@dwimlabs.net>
parents: 4126
diff changeset
  1322
     ('n', 'note', '', _('store a note on touch'), _('TEXT')),
2763
4a5b0c373e65 commands: move the touch to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2762
diff changeset
  1323
     ('D', 'duplicate', False,
4a5b0c373e65 commands: move the touch to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2762
diff changeset
  1324
      'do not mark the new revision as successor of the old one'),
4a5b0c373e65 commands: move the touch to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2762
diff changeset
  1325
     ('A', 'allowdivergence', False,
4a5b0c373e65 commands: move the touch to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2762
diff changeset
  1326
      'mark the new revision as successor of the old one potentially creating '
4a5b0c373e65 commands: move the touch to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2762
diff changeset
  1327
      'divergence')],
4a5b0c373e65 commands: move the touch to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2762
diff changeset
  1328
    # allow to choose the seed ?
4a5b0c373e65 commands: move the touch to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2762
diff changeset
  1329
    _('[-r] revs'))
4a5b0c373e65 commands: move the touch to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2762
diff changeset
  1330
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
  1331
    """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
  1332
4a5b0c373e65 commands: move the touch to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2762
diff changeset
  1333
    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
  1334
    """
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
  1335
    _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
  1336
    revs = list(revs)
4a5b0c373e65 commands: move the touch to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2762
diff changeset
  1337
    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
  1338
    if not revs:
4a5b0c373e65 commands: move the touch to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2762
diff changeset
  1339
        revs = ['.']
4a5b0c373e65 commands: move the touch to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2762
diff changeset
  1340
    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
  1341
    if not revs:
4a5b0c373e65 commands: move the touch to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2762
diff changeset
  1342
        ui.write_err('no revision to touch\n')
4a5b0c373e65 commands: move the touch to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2762
diff changeset
  1343
        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
  1344
7978db1dda3e touch: extract the logic of touching rev's to its own function
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4613
diff changeset
  1345
    duplicate = opts['duplicate']
2790
1b7b9acda2a9 touch: use precheck to validate revision
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2789
diff changeset
  1346
    if not duplicate:
4446
8232bddf543a touch: use action "touch", not "<function touch at ...>", in precheck
Martin von Zweigbergk <martinvonz@google.com>
parents: 4445
diff changeset
  1347
        rewriteutil.precheck(repo, revs, 'touch')
2763
4a5b0c373e65 commands: move the touch to the 'evocommands' module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2762
diff changeset
  1348
    tmpl = utility.shorttemplate
3483
f03845bfd015 compat: add wrapper for logcmdutil functions
Yuya Nishihara <yuya@tcha.org>
parents: 3452
diff changeset
  1349
    displayer = compat.changesetdisplayer(ui, repo, {'template': tmpl})
4640
c720d09c2366 touch: use util.acceptintervention() for closing the transaction
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4639
diff changeset
  1350
    with repo.wlock(), repo.lock():
c720d09c2366 touch: use util.acceptintervention() for closing the transaction
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4639
diff changeset
  1351
        tr = repo.transaction('touch')
c720d09c2366 touch: use util.acceptintervention() for closing the transaction
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4639
diff changeset
  1352
        with util.acceptintervention(tr):
c720d09c2366 touch: use util.acceptintervention() for closing the transaction
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4639
diff changeset
  1353
            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
  1354
4638
7978db1dda3e touch: extract the logic of touching rev's to its own function
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4613
diff changeset
  1355
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
  1356
    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
  1357
    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
  1358
    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
  1359
    newmapping = {}
7978db1dda3e touch: extract the logic of touching rev's to its own function
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4613
diff changeset
  1360
    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
  1361
        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
  1362
        extra = ctx.extra().copy()
7978db1dda3e touch: extract the logic of touching rev's to its own function
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4613
diff changeset
  1363
        extra['__touch-noise__'] = random.randint(0, 0xffffffff)
7978db1dda3e touch: extract the logic of touching rev's to its own function
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4613
diff changeset
  1364
        # 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
  1365
        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
  1366
        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
  1367
        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
  1368
        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
  1369
7978db1dda3e touch: extract the logic of touching rev's to its own function
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4613
diff changeset
  1370
        if not (duplicate or allowdivergence):
7978db1dda3e touch: extract the logic of touching rev's to its own function
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4613
diff changeset
  1371
            # The user hasn't yet decided what to do with the revived
7978db1dda3e touch: extract the logic of touching rev's to its own function
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4613
diff changeset
  1372
            # cset, let's ask
7978db1dda3e touch: extract the logic of touching rev's to its own function
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4613
diff changeset
  1373
            sset = obsutil.successorssets(repo, ctx.node())
7978db1dda3e touch: extract the logic of touching rev's to its own function
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4613
diff changeset
  1374
            nodivergencerisk = (len(sset) == 0
7978db1dda3e touch: extract the logic of touching rev's to its own function
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4613
diff changeset
  1375
                                or (len(sset) == 1
7978db1dda3e touch: extract the logic of touching rev's to its own function
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4613
diff changeset
  1376
                                    and len(sset[0]) == 1
7978db1dda3e touch: extract the logic of touching rev's to its own function
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4613
diff changeset
  1377
                                    and repo[sset[0][0]].rev() == ctx.rev()
7978db1dda3e touch: extract the logic of touching rev's to its own function
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4613
diff changeset
  1378
                                ))
7978db1dda3e touch: extract the logic of touching rev's to its own function
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4613
diff changeset
  1379
            if nodivergencerisk:
7978db1dda3e touch: extract the logic of touching rev's to its own function
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4613
diff changeset
  1380
                duplicate = False
7978db1dda3e touch: extract the logic of touching rev's to its own function
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4613
diff changeset
  1381
            else:
7978db1dda3e touch: extract the logic of touching rev's to its own function
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4613
diff changeset
  1382
                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
  1383
                index = ui.promptchoice(
7978db1dda3e touch: extract the logic of touching rev's to its own function
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4613
diff changeset
  1384
                    _("reviving this changeset will create divergence"
7978db1dda3e touch: extract the logic of touching rev's to its own function
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4613
diff changeset
  1385
                      " unless you make a duplicate.\n(a)llow divergence or"
7978db1dda3e touch: extract the logic of touching rev's to its own function
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4613
diff changeset
  1386
                      " (d)uplicate the changeset? $$ &Allowdivergence $$ "
7978db1dda3e touch: extract the logic of touching rev's to its own function
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4613
diff changeset
  1387
                      "&Duplicate"), 0)
7978db1dda3e touch: extract the logic of touching rev's to its own function
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4613
diff changeset
  1388
                choice = ['allowdivergence', 'duplicate'][index]
7978db1dda3e touch: extract the logic of touching rev's to its own function
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4613
diff changeset
  1389
                if choice == 'allowdivergence':
7978db1dda3e touch: extract the logic of touching rev's to its own function
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4613
diff changeset
  1390
                    duplicate = False
7978db1dda3e touch: extract the logic of touching rev's to its own function
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4613
diff changeset
  1391
                else:
7978db1dda3e touch: extract the logic of touching rev's to its own function
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4613
diff changeset
  1392
                    duplicate = True
7978db1dda3e touch: extract the logic of touching rev's to its own function
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4613
diff changeset
  1393
7978db1dda3e touch: extract the logic of touching rev's to its own function
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4613
diff changeset
  1394
        extradict = {'extra': extra}
7978db1dda3e touch: extract the logic of touching rev's to its own function
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4613
diff changeset
  1395
        new, unusedvariable = rewriteutil.rewrite(repo, ctx, [], ctx,
7978db1dda3e touch: extract the logic of touching rev's to its own function
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4613
diff changeset
  1396
                                                  [p1, p2],
7978db1dda3e touch: extract the logic of touching rev's to its own function
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4613
diff changeset
  1397
                                                  commitopts=extradict)
7978db1dda3e touch: extract the logic of touching rev's to its own function
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4613
diff changeset
  1398
        # 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
  1399
        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
  1400
7978db1dda3e touch: extract the logic of touching rev's to its own function
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4613
diff changeset
  1401
        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
  1402
            metadata = {}
7978db1dda3e touch: extract the logic of touching rev's to its own function
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4613
diff changeset
  1403
            if opts.get('note'):
7978db1dda3e touch: extract the logic of touching rev's to its own function
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4613
diff changeset
  1404
                metadata['note'] = opts['note']
7978db1dda3e touch: extract the logic of touching rev's to its own function
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4613
diff changeset
  1405
            obsolete.createmarkers(repo, [(ctx, (repo[new],))],
7978db1dda3e touch: extract the logic of touching rev's to its own function
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4613
diff changeset
  1406
                                   metadata=metadata, operation="touch")
7978db1dda3e touch: extract the logic of touching rev's to its own function
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4613
diff changeset
  1407
        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
  1408
        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
  1409
        if ctx in repo[None].parents():
7978db1dda3e touch: extract the logic of touching rev's to its own function
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4613
diff changeset
  1410
            with repo.dirstate.parentchange():
7978db1dda3e touch: extract the logic of touching rev's to its own function
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 4613
diff changeset
  1411
                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
  1412
3453
32ed5b6fadd3 grab: add a command to grab a commit and update to it
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3389
diff changeset
  1413
@eh.command(
4048
d7034826c0a2 pick: rename the grab command to pick
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4047
diff changeset
  1414
    'pick|grab',
4183
536c67823962 commands: adjust metavariables as appropriate
Anton Shestakov <av6@dwimlabs.net>
parents: 4126
diff changeset
  1415
    [('r', 'rev', '', _('revision to pick'), _('REV')),
4048
d7034826c0a2 pick: rename the grab command to pick
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4047
diff changeset
  1416
     ('c', 'continue', False, 'continue interrupted pick'),
d7034826c0a2 pick: rename the grab command to pick
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4047
diff changeset
  1417
     ('a', 'abort', False, 'abort interrupted pick'),
4391
054ff759f2fd pick: add --tool for hg pick to specify mergetool
Sangeet Kumar Mishra <mail2sangeetmishra@gmail.com>
parents: 4374
diff changeset
  1418
    ] + mergetoolopts,
3453
32ed5b6fadd3 grab: add a command to grab a commit and update to it
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3389
diff changeset
  1419
    _('[-r] rev'))
4048
d7034826c0a2 pick: rename the grab command to pick
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4047
diff changeset
  1420
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
  1421
    """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
  1422
32ed5b6fadd3 grab: add a command to grab a commit and update to it
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3389
diff changeset
  1423
    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
  1424
    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
  1425
32ed5b6fadd3 grab: add a command to grab a commit and update to it
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3389
diff changeset
  1426
    if cont and abort:
32ed5b6fadd3 grab: add a command to grab a commit and update to it
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3389
diff changeset
  1427
        raise error.Abort(_("cannot specify both --continue and --abort"))
32ed5b6fadd3 grab: add a command to grab a commit and update to it
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3389
diff changeset
  1428
32ed5b6fadd3 grab: add a command to grab a commit and update to it
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3389
diff changeset
  1429
    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
  1430
    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
  1431
        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
  1432
4391
054ff759f2fd pick: add --tool for hg pick to specify mergetool
Sangeet Kumar Mishra <mail2sangeetmishra@gmail.com>
parents: 4374
diff changeset
  1433
    overrides = {('ui', 'forcemerge'): opts.get('tool', '')}
4687
313565dd75e3 pick: remove transaction on the whole command (issue6037)
Anton Shestakov <av6@dwimlabs.net>
parents: 4642
diff changeset
  1434
    with repo.wlock(), repo.lock(), ui.configoverride(overrides, 'pick'):
4052
73e73471d6c6 pick: replace "grabstate" with "pickstate"
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4051
diff changeset
  1435
        pickstate = state.cmdstate(repo, path='pickstate')
3526
df20ddc79064 grab: move the initialization of pctx variable outside of if-else
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3522
diff changeset
  1436
        pctx = repo['.']
3453
32ed5b6fadd3 grab: add a command to grab a commit and update to it
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3389
diff changeset
  1437
32ed5b6fadd3 grab: add a command to grab a commit and update to it
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3389
diff changeset
  1438
        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
  1439
            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
  1440
            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
  1441
            if len(revs) > 1:
32ed5b6fadd3 grab: add a command to grab a commit and update to it
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3389
diff changeset
  1442
                raise error.Abort(_("specify just one revision"))
32ed5b6fadd3 grab: add a command to grab a commit and update to it
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3389
diff changeset
  1443
            elif not revs:
32ed5b6fadd3 grab: add a command to grab a commit and update to it
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3389
diff changeset
  1444
                raise error.Abort(_("empty revision set"))
32ed5b6fadd3 grab: add a command to grab a commit and update to it
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3389
diff changeset
  1445
32ed5b6fadd3 grab: add a command to grab a commit and update to it
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3389
diff changeset
  1446
            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
  1447
3527
7b4d1bfb6b7d grab: gracefully handle the case when we try to grab parent of wdir
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3526
diff changeset
  1448
            if origctx in pctx.ancestors() or origctx.node() == pctx.node():
4049
25981fae92f9 pick: update command output to mention pick instead of grab
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4048
diff changeset
  1449
                raise error.Abort(_("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
  1450
4049
25981fae92f9 pick: update command output to mention pick instead of grab
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4048
diff changeset
  1451
            rewriteutil.precheck(repo, [origctx.rev()], 'pick')
3453
32ed5b6fadd3 grab: add a command to grab a commit and update to it
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3389
diff changeset
  1452
4049
25981fae92f9 pick: update command output to mention pick instead of grab
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4048
diff changeset
  1453
            ui.status(_('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
  1454
                      (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
  1455
                       origctx.description().split("\n", 1)[0]))
32ed5b6fadd3 grab: add a command to grab a commit and update to it
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3389
diff changeset
  1456
            stats = merge.graft(repo, origctx, origctx.p1(), ['local',
32ed5b6fadd3 grab: add a command to grab a commit and update to it
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3389
diff changeset
  1457
                                                              'destination'])
3616
f6d629514607 compat: use updateresult API if available
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3569
diff changeset
  1458
            if compat.hasconflict(stats):
4052
73e73471d6c6 pick: replace "grabstate" with "pickstate"
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4051
diff changeset
  1459
                pickstate.addopts({'orignode': origctx.node(),
3457
82e9f9603b1b evolvestate: rename the file to state.py and class name to cmdstate
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3456
diff changeset
  1460
                                   'oldpctx': pctx.node()})
4052
73e73471d6c6 pick: replace "grabstate" with "pickstate"
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4051
diff changeset
  1461
                pickstate.save()
3453
32ed5b6fadd3 grab: add a command to grab a commit and update to it
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3389
diff changeset
  1462
                raise error.InterventionRequired(_("unresolved merge conflicts"
32ed5b6fadd3 grab: add a command to grab a commit and update to it
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3389
diff changeset
  1463
                                                   " (see hg help resolve)"))
32ed5b6fadd3 grab: add a command to grab a commit and update to it
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3389
diff changeset
  1464
32ed5b6fadd3 grab: add a command to grab a commit and update to it
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3389
diff changeset
  1465
        elif abort:
4052
73e73471d6c6 pick: replace "grabstate" with "pickstate"
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4051
diff changeset
  1466
            if not pickstate:
4049
25981fae92f9 pick: update command output to mention pick instead of grab
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4048
diff changeset
  1467
                raise error.Abort(_("no interrupted pick state exists"))
4052
73e73471d6c6 pick: replace "grabstate" with "pickstate"
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4051
diff changeset
  1468
            pickstate.load()
73e73471d6c6 pick: replace "grabstate" with "pickstate"
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4051
diff changeset
  1469
            pctxnode = pickstate['oldpctx']
4049
25981fae92f9 pick: update command output to mention pick instead of grab
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4048
diff changeset
  1470
            ui.status(_("aborting pick, updating to %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
  1471
                      node.hex(pctxnode)[:12])
32ed5b6fadd3 grab: add a command to grab a commit and update to it
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3389
diff changeset
  1472
            hg.updaterepo(repo, pctxnode, True)
32ed5b6fadd3 grab: add a command to grab a commit and update to it
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3389
diff changeset
  1473
            return 0
32ed5b6fadd3 grab: add a command to grab a commit and update to it
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3389
diff changeset
  1474
32ed5b6fadd3 grab: add a command to grab a commit and update to it
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3389
diff changeset
  1475
        else:
32ed5b6fadd3 grab: add a command to grab a commit and update to it
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3389
diff changeset
  1476
            if revs:
32ed5b6fadd3 grab: add a command to grab a commit and update to it
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3389
diff changeset
  1477
                raise error.Abort(_("cannot specify both --continue and "
32ed5b6fadd3 grab: add a command to grab a commit and update to it
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3389
diff changeset
  1478
                                    "revision"))
4052
73e73471d6c6 pick: replace "grabstate" with "pickstate"
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4051
diff changeset
  1479
            if not pickstate:
4049
25981fae92f9 pick: update command output to mention pick instead of grab
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4048
diff changeset
  1480
                raise error.Abort(_("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
  1481
4052
73e73471d6c6 pick: replace "grabstate" with "pickstate"
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4051
diff changeset
  1482
            pickstate.load()
73e73471d6c6 pick: replace "grabstate" with "pickstate"
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4051
diff changeset
  1483
            orignode = pickstate['orignode']
3453
32ed5b6fadd3 grab: add a command to grab a commit and update to it
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3389
diff changeset
  1484
            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
  1485
3475
a03bb02dfaba grab: preserve phase of the grabbed changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3457
diff changeset
  1486
        overrides = {('phases', 'new-commit'): origctx.phase()}
4050
3dddb03f3559 pick: use "pick" instead of "grab" for various internal API
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4049
diff changeset
  1487
        with repo.ui.configoverride(overrides, 'pick'):
3475
a03bb02dfaba grab: preserve phase of the grabbed changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3457
diff changeset
  1488
            newnode = repo.commit(text=origctx.description(),
a03bb02dfaba grab: preserve phase of the grabbed changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3457
diff changeset
  1489
                                  user=origctx.user(),
a03bb02dfaba grab: preserve phase of the grabbed changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3457
diff changeset
  1490
                                  date=origctx.date(), extra=origctx.extra())
4485
e3785a8d0712 pick: update working dir branch (issue6089)
Manuel Jacob <me@manueljacob.de>
parents: 4463
diff changeset
  1491
        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
  1492
4052
73e73471d6c6 pick: replace "grabstate" with "pickstate"
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4051
diff changeset
  1493
        if pickstate:
73e73471d6c6 pick: replace "grabstate" with "pickstate"
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4051
diff changeset
  1494
            pickstate.delete()
3569
236d36b17aa4 grab: include "operation" metadata in obsmarkers
Martin von Zweigbergk <martinvonz@google.com>
parents: 3568
diff changeset
  1495
        newctx = repo[newnode] if newnode else pctx
4371
9a218768bac5 pick: use scmutil.cleanupnodes()
Martin von Zweigbergk <martinvonz@google.com>
parents: 4370
diff changeset
  1496
        replacements = {origctx.node(): [newctx.node()]}
9a218768bac5 pick: use scmutil.cleanupnodes()
Martin von Zweigbergk <martinvonz@google.com>
parents: 4370
diff changeset
  1497
        scmutil.cleanupnodes(repo, replacements, operation="pick")
3453
32ed5b6fadd3 grab: add a command to grab a commit and update to it
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3389
diff changeset
  1498
32ed5b6fadd3 grab: add a command to grab a commit and update to it
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3389
diff changeset
  1499
        if newnode is None:
4049
25981fae92f9 pick: update command output to mention pick instead of grab
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4048
diff changeset
  1500
            ui.warn(_("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
  1501
                    (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
  1502
            return 0
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
        return 0