mercurial/cmdutil.py
author Matt Harbison <matt_harbison@yahoo.com>
Sat, 02 Jun 2018 13:44:44 -0400
branchstable
changeset 38182 79c54e7c0c52
parent 37815 32a75a8a5b0f
child 38239 ead71b15efd5
permissions -rw-r--r--
rebase: prioritize indicating an interrupted rebase over update (issue5838) This should also cover the transplant extension, and any other non clearable states.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2957
6e062d9b188f fix comment.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2885
diff changeset
     1
# cmdutil.py - help for command processing in mercurial
2874
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
     2
#
4635
63b9d2deed48 Updated copyright notices and add "and others" to "hg version"
Thomas Arendsen Hein <thomas@intevation.de>
parents: 4633
diff changeset
     3
# Copyright 2005-2007 Matt Mackall <mpm@selenic.com>
2874
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
     4
#
8225
46293a0c7e9f updated license to be explicit about GPL version 2
Martin Geisler <mg@lazybytes.net>
parents: 8210
diff changeset
     5
# This software may be used and distributed according to the terms of the
10263
25e572394f5c Update license to GPLv2+
Matt Mackall <mpm@selenic.com>
parents: 10249
diff changeset
     6
# GNU General Public License version 2 or any later version.
2874
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
     7
28322
ebd0e86bdf89 cmdutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 28313
diff changeset
     8
from __future__ import absolute_import
ebd0e86bdf89 cmdutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 28313
diff changeset
     9
ebd0e86bdf89 cmdutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 28313
diff changeset
    10
import errno
ebd0e86bdf89 cmdutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 28313
diff changeset
    11
import os
ebd0e86bdf89 cmdutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 28313
diff changeset
    12
import re
ebd0e86bdf89 cmdutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 28313
diff changeset
    13
import tempfile
ebd0e86bdf89 cmdutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 28313
diff changeset
    14
ebd0e86bdf89 cmdutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 28313
diff changeset
    15
from .i18n import _
ebd0e86bdf89 cmdutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 28313
diff changeset
    16
from .node import (
ebd0e86bdf89 cmdutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 28313
diff changeset
    17
    hex,
ebd0e86bdf89 cmdutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 28313
diff changeset
    18
    nullid,
ebd0e86bdf89 cmdutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 28313
diff changeset
    19
    nullrev,
ebd0e86bdf89 cmdutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 28313
diff changeset
    20
    short,
ebd0e86bdf89 cmdutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 28313
diff changeset
    21
)
ebd0e86bdf89 cmdutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 28313
diff changeset
    22
ebd0e86bdf89 cmdutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 28313
diff changeset
    23
from . import (
ebd0e86bdf89 cmdutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 28313
diff changeset
    24
    bookmarks,
ebd0e86bdf89 cmdutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 28313
diff changeset
    25
    changelog,
ebd0e86bdf89 cmdutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 28313
diff changeset
    26
    copies,
ebd0e86bdf89 cmdutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 28313
diff changeset
    27
    crecord as crecordmod,
33617
5ac845ca059a commit: don't let failed commit with --addremove update dirstate (issue5645)
Martin von Zweigbergk <martinvonz@google.com>
parents: 33548
diff changeset
    28
    dirstateguard,
28322
ebd0e86bdf89 cmdutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 28313
diff changeset
    29
    encoding,
ebd0e86bdf89 cmdutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 28313
diff changeset
    30
    error,
ebd0e86bdf89 cmdutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 28313
diff changeset
    31
    formatter,
35885
7625b4f7db70 cmdutil: split functions of log-like commands to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents: 35874
diff changeset
    32
    logcmdutil,
28322
ebd0e86bdf89 cmdutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 28313
diff changeset
    33
    match as matchmod,
36010
46a54de96a54 merge: cut import cycle at merge -> extensions
Yuya Nishihara <yuya@tcha.org>
parents: 36009
diff changeset
    34
    merge as mergemod,
36844
eeb87b24aea7 amend: abort if unresolved merge conflicts found (issue5805)
Yuya Nishihara <yuya@tcha.org>
parents: 35746
diff changeset
    35
    mergeutil,
28322
ebd0e86bdf89 cmdutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 28313
diff changeset
    36
    obsolete,
ebd0e86bdf89 cmdutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 28313
diff changeset
    37
    patch,
ebd0e86bdf89 cmdutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 28313
diff changeset
    38
    pathutil,
30519
20a42325fdef py3: use pycompat.getcwd() instead of os.getcwd()
Pulkit Goyal <7895pulkit@gmail.com>
parents: 30506
diff changeset
    39
    pycompat,
32337
46ba2cdda476 registrar: move cmdutil.command to registrar module (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32327
diff changeset
    40
    registrar,
28322
ebd0e86bdf89 cmdutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 28313
diff changeset
    41
    revlog,
35745
3bd8ab4c80a5 branch: add a --rev flag to change branch name of given revisions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35743
diff changeset
    42
    rewriteutil,
28322
ebd0e86bdf89 cmdutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 28313
diff changeset
    43
    scmutil,
31023
aea06029919e revset: import set classes directly from smartset module
Yuya Nishihara <yuya@tcha.org>
parents: 30877
diff changeset
    44
    smartset,
36009
55e8efa2451a subrepo: split non-core functions to new module
Yuya Nishihara <yuya@tcha.org>
parents: 35954
diff changeset
    45
    subrepoutil,
28322
ebd0e86bdf89 cmdutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 28313
diff changeset
    46
    templatekw,
ebd0e86bdf89 cmdutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 28313
diff changeset
    47
    templater,
ebd0e86bdf89 cmdutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 28313
diff changeset
    48
    util,
31237
1b08aca7870a vfs: use 'vfs' module directly in 'mercurial.cmdutil'
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31216
diff changeset
    49
    vfs as vfsmod,
28322
ebd0e86bdf89 cmdutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 28313
diff changeset
    50
)
37084
f0b6fbea00cf stringutil: bulk-replace call sites to point to new module
Yuya Nishihara <yuya@tcha.org>
parents: 36988
diff changeset
    51
f0b6fbea00cf stringutil: bulk-replace call sites to point to new module
Yuya Nishihara <yuya@tcha.org>
parents: 36988
diff changeset
    52
from .utils import (
f0b6fbea00cf stringutil: bulk-replace call sites to point to new module
Yuya Nishihara <yuya@tcha.org>
parents: 36988
diff changeset
    53
    dateutil,
f0b6fbea00cf stringutil: bulk-replace call sites to point to new module
Yuya Nishihara <yuya@tcha.org>
parents: 36988
diff changeset
    54
    stringutil,
f0b6fbea00cf stringutil: bulk-replace call sites to point to new module
Yuya Nishihara <yuya@tcha.org>
parents: 36988
diff changeset
    55
)
f0b6fbea00cf stringutil: bulk-replace call sites to point to new module
Yuya Nishihara <yuya@tcha.org>
parents: 36988
diff changeset
    56
28861
86db5cb55d46 pycompat: switch to util.stringio for py3 compat
timeless <timeless@mozdev.org>
parents: 28837
diff changeset
    57
stringio = util.stringio
2874
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    58
32375
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
    59
# templates of common command options
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
    60
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
    61
dryrunopts = [
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
    62
    ('n', 'dry-run', None,
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
    63
     _('do not perform actions, just print output')),
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
    64
]
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
    65
37756
e7bf5a73e4e1 forget: add --confirm option
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 37621
diff changeset
    66
confirmopts = [
e7bf5a73e4e1 forget: add --confirm option
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 37621
diff changeset
    67
    ('', 'confirm', None,
e7bf5a73e4e1 forget: add --confirm option
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 37621
diff changeset
    68
     _('ask before applying actions')),
e7bf5a73e4e1 forget: add --confirm option
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 37621
diff changeset
    69
]
e7bf5a73e4e1 forget: add --confirm option
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 37621
diff changeset
    70
32375
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
    71
remoteopts = [
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
    72
    ('e', 'ssh', '',
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
    73
     _('specify ssh command to use'), _('CMD')),
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
    74
    ('', 'remotecmd', '',
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
    75
     _('specify hg command to run on the remote side'), _('CMD')),
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
    76
    ('', 'insecure', None,
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
    77
     _('do not verify server certificate (ignoring web.cacerts config)')),
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
    78
]
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
    79
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
    80
walkopts = [
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
    81
    ('I', 'include', [],
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
    82
     _('include names matching the given patterns'), _('PATTERN')),
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
    83
    ('X', 'exclude', [],
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
    84
     _('exclude names matching the given patterns'), _('PATTERN')),
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
    85
]
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
    86
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
    87
commitopts = [
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
    88
    ('m', 'message', '',
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
    89
     _('use text as commit message'), _('TEXT')),
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
    90
    ('l', 'logfile', '',
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
    91
     _('read commit message from file'), _('FILE')),
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
    92
]
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
    93
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
    94
commitopts2 = [
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
    95
    ('d', 'date', '',
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
    96
     _('record the specified date as commit date'), _('DATE')),
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
    97
    ('u', 'user', '',
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
    98
     _('record the specified user as committer'), _('USER')),
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
    99
]
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
   100
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
   101
# hidden for now
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
   102
formatteropts = [
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
   103
    ('T', 'template', '',
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
   104
     _('display with template (EXPERIMENTAL)'), _('TEMPLATE')),
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
   105
]
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
   106
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
   107
templateopts = [
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
   108
    ('', 'style', '',
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
   109
     _('display using template map file (DEPRECATED)'), _('STYLE')),
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
   110
    ('T', 'template', '',
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
   111
     _('display with template'), _('TEMPLATE')),
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
   112
]
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
   113
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
   114
logopts = [
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
   115
    ('p', 'patch', None, _('show patch')),
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
   116
    ('g', 'git', None, _('use git extended diff format')),
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
   117
    ('l', 'limit', '',
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
   118
     _('limit number of changes displayed'), _('NUM')),
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
   119
    ('M', 'no-merges', None, _('do not show merges')),
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
   120
    ('', 'stat', None, _('output diffstat-style summary of changes')),
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
   121
    ('G', 'graph', None, _("show the revision DAG")),
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
   122
] + templateopts
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
   123
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
   124
diffopts = [
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
   125
    ('a', 'text', None, _('treat all files as text')),
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
   126
    ('g', 'git', None, _('use git extended diff format')),
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
   127
    ('', 'binary', None, _('generate binary diffs in git mode (default)')),
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
   128
    ('', 'nodates', None, _('omit dates from diff headers'))
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
   129
]
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
   130
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
   131
diffwsopts = [
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
   132
    ('w', 'ignore-all-space', None,
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
   133
     _('ignore white space when comparing lines')),
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
   134
    ('b', 'ignore-space-change', None,
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
   135
     _('ignore changes in the amount of white space')),
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
   136
    ('B', 'ignore-blank-lines', None,
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
   137
     _('ignore changes whose lines are all blank')),
34013
da07367d683b mdiff: add a --ignore-space-at-eol option
David Soria Parra <davidsp@fb.com>
parents: 34001
diff changeset
   138
    ('Z', 'ignore-space-at-eol', None,
da07367d683b mdiff: add a --ignore-space-at-eol option
David Soria Parra <davidsp@fb.com>
parents: 34001
diff changeset
   139
     _('ignore changes in whitespace at EOL')),
32375
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
   140
]
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
   141
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
   142
diffopts2 = [
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
   143
    ('', 'noprefix', None, _('omit a/ and b/ prefixes from filenames')),
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
   144
    ('p', 'show-function', None, _('show which function each change is in')),
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
   145
    ('', 'reverse', None, _('produce a diff that undoes the changes')),
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
   146
] + diffwsopts + [
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
   147
    ('U', 'unified', '',
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
   148
     _('number of lines of context to show'), _('NUM')),
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
   149
    ('', 'stat', None, _('output diffstat-style summary of changes')),
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
   150
    ('', 'root', '', _('produce diffs relative to subdirectory'), _('DIR')),
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
   151
]
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
   152
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
   153
mergetoolopts = [
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
   154
    ('t', 'tool', '', _('specify merge tool')),
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
   155
]
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
   156
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
   157
similarityopts = [
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
   158
    ('s', 'similarity', '',
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
   159
     _('guess renamed files by similarity (0<=s<=100)'), _('SIMILARITY'))
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
   160
]
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
   161
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
   162
subrepoopts = [
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
   163
    ('S', 'subrepos', None,
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
   164
     _('recurse into subrepositories'))
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
   165
]
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
   166
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
   167
debugrevlogopts = [
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
   168
    ('c', 'changelog', False, _('open changelog')),
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
   169
    ('m', 'manifest', False, _('open manifest')),
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
   170
    ('', 'dir', '', _('open directory manifest')),
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
   171
]
04baab18d60a commands: move templates of common command options to cmdutil (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32362
diff changeset
   172
30703
5c85c93cdd61 cmdutil: add special string that ignores rest of text
Sean Farley <sean@farley.io>
parents: 30695
diff changeset
   173
# special string such that everything below this line will be ingored in the
5c85c93cdd61 cmdutil: add special string that ignores rest of text
Sean Farley <sean@farley.io>
parents: 30695
diff changeset
   174
# editor text
5c85c93cdd61 cmdutil: add special string that ignores rest of text
Sean Farley <sean@farley.io>
parents: 30695
diff changeset
   175
_linebelow = "^HG: ------------------------ >8 ------------------------$"
5c85c93cdd61 cmdutil: add special string that ignores rest of text
Sean Farley <sean@farley.io>
parents: 30695
diff changeset
   176
25256
5a8398b085ed record: extract ishunk to a function
Laurent Charignon <lcharignon@fb.com>
parents: 25228
diff changeset
   177
def ishunk(x):
5a8398b085ed record: extract ishunk to a function
Laurent Charignon <lcharignon@fb.com>
parents: 25228
diff changeset
   178
    hunkclasses = (crecordmod.uihunk, patch.recordhunk)
5a8398b085ed record: extract ishunk to a function
Laurent Charignon <lcharignon@fb.com>
parents: 25228
diff changeset
   179
    return isinstance(x, hunkclasses)
5a8398b085ed record: extract ishunk to a function
Laurent Charignon <lcharignon@fb.com>
parents: 25228
diff changeset
   180
25257
07326d76f19d record: extract code to compute newly added and modified files
Laurent Charignon <lcharignon@fb.com>
parents: 25256
diff changeset
   181
def newandmodified(chunks, originalchunks):
07326d76f19d record: extract code to compute newly added and modified files
Laurent Charignon <lcharignon@fb.com>
parents: 25256
diff changeset
   182
    newlyaddedandmodifiedfiles = set()
07326d76f19d record: extract code to compute newly added and modified files
Laurent Charignon <lcharignon@fb.com>
parents: 25256
diff changeset
   183
    for chunk in chunks:
07326d76f19d record: extract code to compute newly added and modified files
Laurent Charignon <lcharignon@fb.com>
parents: 25256
diff changeset
   184
        if ishunk(chunk) and chunk.header.isnewfile() and chunk not in \
07326d76f19d record: extract code to compute newly added and modified files
Laurent Charignon <lcharignon@fb.com>
parents: 25256
diff changeset
   185
            originalchunks:
07326d76f19d record: extract code to compute newly added and modified files
Laurent Charignon <lcharignon@fb.com>
parents: 25256
diff changeset
   186
            newlyaddedandmodifiedfiles.add(chunk.header.filename())
07326d76f19d record: extract code to compute newly added and modified files
Laurent Charignon <lcharignon@fb.com>
parents: 25256
diff changeset
   187
    return newlyaddedandmodifiedfiles
07326d76f19d record: extract code to compute newly added and modified files
Laurent Charignon <lcharignon@fb.com>
parents: 25256
diff changeset
   188
10401
6252852b4332 mq: add -Q option to all commands not in norepo
Brendan Cully <brendan@kublai.com>
parents: 10344
diff changeset
   189
def parsealiases(cmd):
6252852b4332 mq: add -Q option to all commands not in norepo
Brendan Cully <brendan@kublai.com>
parents: 10344
diff changeset
   190
    return cmd.lstrip("^").split("|")
6252852b4332 mq: add -Q option to all commands not in norepo
Brendan Cully <brendan@kublai.com>
parents: 10344
diff changeset
   191
24356
a38f384f2a57 record: consolidate ui.write wrapping in a function
Laurent Charignon <lcharignon@fb.com>
parents: 24345
diff changeset
   192
def setupwrapcolorwrite(ui):
a38f384f2a57 record: consolidate ui.write wrapping in a function
Laurent Charignon <lcharignon@fb.com>
parents: 24345
diff changeset
   193
    # wrap ui.write so diff output can be labeled/colorized
a38f384f2a57 record: consolidate ui.write wrapping in a function
Laurent Charignon <lcharignon@fb.com>
parents: 24345
diff changeset
   194
    def wrapwrite(orig, *args, **kw):
35350
82ee401135dd py3: handle keyword arguments correctly in cmdutil.py
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35277
diff changeset
   195
        label = kw.pop(r'label', '')
24356
a38f384f2a57 record: consolidate ui.write wrapping in a function
Laurent Charignon <lcharignon@fb.com>
parents: 24345
diff changeset
   196
        for chunk, l in patch.difflabel(lambda: args):
a38f384f2a57 record: consolidate ui.write wrapping in a function
Laurent Charignon <lcharignon@fb.com>
parents: 24345
diff changeset
   197
            orig(chunk, label=label + l)
a38f384f2a57 record: consolidate ui.write wrapping in a function
Laurent Charignon <lcharignon@fb.com>
parents: 24345
diff changeset
   198
a38f384f2a57 record: consolidate ui.write wrapping in a function
Laurent Charignon <lcharignon@fb.com>
parents: 24345
diff changeset
   199
    oldwrite = ui.write
a38f384f2a57 record: consolidate ui.write wrapping in a function
Laurent Charignon <lcharignon@fb.com>
parents: 24345
diff changeset
   200
    def wrap(*args, **kwargs):
a38f384f2a57 record: consolidate ui.write wrapping in a function
Laurent Charignon <lcharignon@fb.com>
parents: 24345
diff changeset
   201
        return wrapwrite(oldwrite, *args, **kwargs)
a38f384f2a57 record: consolidate ui.write wrapping in a function
Laurent Charignon <lcharignon@fb.com>
parents: 24345
diff changeset
   202
    setattr(ui, 'write', wrap)
a38f384f2a57 record: consolidate ui.write wrapping in a function
Laurent Charignon <lcharignon@fb.com>
parents: 24345
diff changeset
   203
    return oldwrite
a38f384f2a57 record: consolidate ui.write wrapping in a function
Laurent Charignon <lcharignon@fb.com>
parents: 24345
diff changeset
   204
25310
c1f5ef76d1c2 record: add an operation arguments to customize recording ui
Laurent Charignon <lcharignon@fb.com>
parents: 25273
diff changeset
   205
def filterchunks(ui, originalhunks, usecurses, testfile, operation=None):
24343
69538481ea9f record: enable curses recording logic with experimental flag
Laurent Charignon <lcharignon@fb.com>
parents: 24341
diff changeset
   206
    if usecurses:
69538481ea9f record: enable curses recording logic with experimental flag
Laurent Charignon <lcharignon@fb.com>
parents: 24341
diff changeset
   207
        if testfile:
69538481ea9f record: enable curses recording logic with experimental flag
Laurent Charignon <lcharignon@fb.com>
parents: 24341
diff changeset
   208
            recordfn = crecordmod.testdecorator(testfile,
69538481ea9f record: enable curses recording logic with experimental flag
Laurent Charignon <lcharignon@fb.com>
parents: 24341
diff changeset
   209
                                                crecordmod.testchunkselector)
69538481ea9f record: enable curses recording logic with experimental flag
Laurent Charignon <lcharignon@fb.com>
parents: 24341
diff changeset
   210
        else:
69538481ea9f record: enable curses recording logic with experimental flag
Laurent Charignon <lcharignon@fb.com>
parents: 24341
diff changeset
   211
            recordfn = crecordmod.chunkselector
69538481ea9f record: enable curses recording logic with experimental flag
Laurent Charignon <lcharignon@fb.com>
parents: 24341
diff changeset
   212
30534
c01033fb9864 revert: pass operation to crecord
Jun Wu <quark@fb.com>
parents: 30532
diff changeset
   213
        return crecordmod.filterpatch(ui, originalhunks, recordfn, operation)
24343
69538481ea9f record: enable curses recording logic with experimental flag
Laurent Charignon <lcharignon@fb.com>
parents: 24341
diff changeset
   214
69538481ea9f record: enable curses recording logic with experimental flag
Laurent Charignon <lcharignon@fb.com>
parents: 24341
diff changeset
   215
    else:
25310
c1f5ef76d1c2 record: add an operation arguments to customize recording ui
Laurent Charignon <lcharignon@fb.com>
parents: 25273
diff changeset
   216
        return patch.filterpatch(ui, originalhunks, operation)
c1f5ef76d1c2 record: add an operation arguments to customize recording ui
Laurent Charignon <lcharignon@fb.com>
parents: 25273
diff changeset
   217
c1f5ef76d1c2 record: add an operation arguments to customize recording ui
Laurent Charignon <lcharignon@fb.com>
parents: 25273
diff changeset
   218
def recordfilter(ui, originalhunks, operation=None):
c1f5ef76d1c2 record: add an operation arguments to customize recording ui
Laurent Charignon <lcharignon@fb.com>
parents: 25273
diff changeset
   219
    """ Prompts the user to filter the originalhunks and return a list of
c1f5ef76d1c2 record: add an operation arguments to customize recording ui
Laurent Charignon <lcharignon@fb.com>
parents: 25273
diff changeset
   220
    selected hunks.
29326
d48fc6f318a3 patch: define full messages for interactive record/revert
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 29283
diff changeset
   221
    *operation* is used for to build ui messages to indicate the user what
d48fc6f318a3 patch: define full messages for interactive record/revert
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 29283
diff changeset
   222
    kind of filtering they are doing: reverting, committing, shelving, etc.
d48fc6f318a3 patch: define full messages for interactive record/revert
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 29283
diff changeset
   223
    (see patch.filterpatch).
25310
c1f5ef76d1c2 record: add an operation arguments to customize recording ui
Laurent Charignon <lcharignon@fb.com>
parents: 25273
diff changeset
   224
    """
27531
84d686cb62c4 cmdutil: use crecordmod.checkcurses
Sean Farley <sean@farley.io>
parents: 27370
diff changeset
   225
    usecurses = crecordmod.checkcurses(ui)
33499
0407a51b9d8c codemod: register core configitems using a script
Jun Wu <quark@fb.com>
parents: 33438
diff changeset
   226
    testfile = ui.config('experimental', 'crecordtest')
24358
8d9e9063b040 record: move ui.write wrapping where it should be
Laurent Charignon <lcharignon@fb.com>
parents: 24357
diff changeset
   227
    oldwrite = setupwrapcolorwrite(ui)
8d9e9063b040 record: move ui.write wrapping where it should be
Laurent Charignon <lcharignon@fb.com>
parents: 24357
diff changeset
   228
    try:
27155
8d3c5797a175 commit: add a way to return more information from the chunkselector
Laurent Charignon <lcharignon@fb.com>
parents: 27148
diff changeset
   229
        newchunks, newopts = filterchunks(ui, originalhunks, usecurses,
8d3c5797a175 commit: add a way to return more information from the chunkselector
Laurent Charignon <lcharignon@fb.com>
parents: 27148
diff changeset
   230
                                          testfile, operation)
24358
8d9e9063b040 record: move ui.write wrapping where it should be
Laurent Charignon <lcharignon@fb.com>
parents: 24357
diff changeset
   231
    finally:
8d9e9063b040 record: move ui.write wrapping where it should be
Laurent Charignon <lcharignon@fb.com>
parents: 24357
diff changeset
   232
        ui.write = oldwrite
27155
8d3c5797a175 commit: add a way to return more information from the chunkselector
Laurent Charignon <lcharignon@fb.com>
parents: 27148
diff changeset
   233
    return newchunks, newopts
24357
2da601ab3125 record: refactor the filtering code
Laurent Charignon <lcharignon@fb.com>
parents: 24356
diff changeset
   234
24309
fefcafda10b8 record: change interface of dorecord to accept new filters
Laurent Charignon <lcharignon@fb.com>
parents: 24306
diff changeset
   235
def dorecord(ui, repo, commitfunc, cmdsuggest, backupall,
fefcafda10b8 record: change interface of dorecord to accept new filters
Laurent Charignon <lcharignon@fb.com>
parents: 24306
diff changeset
   236
            filterfn, *pats, **opts):
32144
93155367a2a6 py3: convert opts to bytes in cmdutil.dorecord()
Pulkit Goyal <7895pulkit@gmail.com>
parents: 32046
diff changeset
   237
    opts = pycompat.byteskwargs(opts)
24272
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   238
    if not ui.interactive():
25795
69145daacdfa cmdutil: allow callers of cmdutil.dorecord to omit suggestion
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 25763
diff changeset
   239
        if cmdsuggest:
69145daacdfa cmdutil: allow callers of cmdutil.dorecord to omit suggestion
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 25763
diff changeset
   240
            msg = _('running non-interactively, use %s instead') % cmdsuggest
69145daacdfa cmdutil: allow callers of cmdutil.dorecord to omit suggestion
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 25763
diff changeset
   241
        else:
69145daacdfa cmdutil: allow callers of cmdutil.dorecord to omit suggestion
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 25763
diff changeset
   242
            msg = _('running non-interactively')
26587
56b2bcea2529 error: get Abort from 'error' instead of 'util'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26579
diff changeset
   243
        raise error.Abort(msg)
24272
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   244
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   245
    # make sure username is set before going interactive
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   246
    if not opts.get('user'):
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   247
        ui.username() # raise exception, username not provided
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   248
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   249
    def recordfunc(ui, repo, message, match, opts):
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   250
        """This is generic record driver.
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   251
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   252
        Its job is to interactively filter local changes, and
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   253
        accordingly prepare working directory into a state in which the
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   254
        job can be delegated to a non-interactive commit command such as
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   255
        'commit' or 'qrefresh'.
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   256
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   257
        After the actual job is done by non-interactive command, the
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   258
        working directory is restored to its original state.
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   259
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   260
        In the end we'll record interesting changes, and everything else
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   261
        will be left in place, so the user can continue working.
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   262
        """
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   263
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   264
        checkunfinished(repo, commit=True)
28815
44611ad4fbd9 crecord: check for untracked arguments
timeless <timeless@mozdev.org>
parents: 28638
diff changeset
   265
        wctx = repo[None]
44611ad4fbd9 crecord: check for untracked arguments
timeless <timeless@mozdev.org>
parents: 28638
diff changeset
   266
        merge = len(wctx.parents()) > 1
24272
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   267
        if merge:
26587
56b2bcea2529 error: get Abort from 'error' instead of 'util'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26579
diff changeset
   268
            raise error.Abort(_('cannot partially commit a merge '
24272
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   269
                               '(use "hg commit" instead)'))
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   270
28815
44611ad4fbd9 crecord: check for untracked arguments
timeless <timeless@mozdev.org>
parents: 28638
diff changeset
   271
        def fail(f, msg):
44611ad4fbd9 crecord: check for untracked arguments
timeless <timeless@mozdev.org>
parents: 28638
diff changeset
   272
            raise error.Abort('%s: %s' % (f, msg))
44611ad4fbd9 crecord: check for untracked arguments
timeless <timeless@mozdev.org>
parents: 28638
diff changeset
   273
44611ad4fbd9 crecord: check for untracked arguments
timeless <timeless@mozdev.org>
parents: 28638
diff changeset
   274
        force = opts.get('force')
44611ad4fbd9 crecord: check for untracked arguments
timeless <timeless@mozdev.org>
parents: 28638
diff changeset
   275
        if not force:
44611ad4fbd9 crecord: check for untracked arguments
timeless <timeless@mozdev.org>
parents: 28638
diff changeset
   276
            vdirs = []
44611ad4fbd9 crecord: check for untracked arguments
timeless <timeless@mozdev.org>
parents: 28638
diff changeset
   277
            match.explicitdir = vdirs.append
44611ad4fbd9 crecord: check for untracked arguments
timeless <timeless@mozdev.org>
parents: 28638
diff changeset
   278
            match.bad = fail
44611ad4fbd9 crecord: check for untracked arguments
timeless <timeless@mozdev.org>
parents: 28638
diff changeset
   279
24272
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   280
        status = repo.status(match=match)
28815
44611ad4fbd9 crecord: check for untracked arguments
timeless <timeless@mozdev.org>
parents: 28638
diff changeset
   281
        if not force:
44611ad4fbd9 crecord: check for untracked arguments
timeless <timeless@mozdev.org>
parents: 28638
diff changeset
   282
            repo.checkcommitpatterns(wctx, vdirs, match, status, fail)
24272
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   283
        diffopts = patch.difffeatureopts(ui, opts=opts, whitespace=True)
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   284
        diffopts.nodates = True
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   285
        diffopts.git = True
27411
c84a07530040 record: turn on showfunc
timeless <timeless@mozdev.org>
parents: 27391
diff changeset
   286
        diffopts.showfunc = True
27637
b502138f5faa cleanup: remove superfluous space after space after equals (python)
timeless <timeless@mozdev.org>
parents: 27625
diff changeset
   287
        originaldiff = patch.diff(repo, changes=status, opts=diffopts)
24341
616c01b69898 record: change interface of the filtering function
Laurent Charignon <lcharignon@fb.com>
parents: 24311
diff changeset
   288
        originalchunks = patch.parsepatch(originaldiff)
24272
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   289
28570
5508cf9a52fe crecord: rewrite a comment about filtering patches
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 28452
diff changeset
   290
        # 1. filter patch, since we are intending to apply subset of it
24272
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   291
        try:
27155
8d3c5797a175 commit: add a way to return more information from the chunkselector
Laurent Charignon <lcharignon@fb.com>
parents: 27148
diff changeset
   292
            chunks, newopts = filterfn(ui, originalchunks)
34251
61714510220d error: move patch.PatchError so it can easily implement __bytes__ (API)
Yuya Nishihara <yuya@tcha.org>
parents: 34189
diff changeset
   293
        except error.PatchError as err:
26587
56b2bcea2529 error: get Abort from 'error' instead of 'util'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26579
diff changeset
   294
            raise error.Abort(_('error parsing patch: %s') % err)
27155
8d3c5797a175 commit: add a way to return more information from the chunkselector
Laurent Charignon <lcharignon@fb.com>
parents: 27148
diff changeset
   295
        opts.update(newopts)
24272
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   296
24845
8133494accf1 record: edit patch of newly added files (issue4304)
Laurent Charignon <lcharignon@fb.com>
parents: 24843
diff changeset
   297
        # We need to keep a backup of files that have been newly added and
8133494accf1 record: edit patch of newly added files (issue4304)
Laurent Charignon <lcharignon@fb.com>
parents: 24843
diff changeset
   298
        # modified during the recording process because there is a previous
8133494accf1 record: edit patch of newly added files (issue4304)
Laurent Charignon <lcharignon@fb.com>
parents: 24843
diff changeset
   299
        # version without the edit in the workdir
25257
07326d76f19d record: extract code to compute newly added and modified files
Laurent Charignon <lcharignon@fb.com>
parents: 25256
diff changeset
   300
        newlyaddedandmodifiedfiles = newandmodified(chunks, originalchunks)
24272
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   301
        contenders = set()
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   302
        for h in chunks:
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   303
            try:
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   304
                contenders.update(set(h.files()))
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   305
            except AttributeError:
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   306
                pass
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   307
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   308
        changed = status.modified + status.added + status.removed
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   309
        newfiles = [f for f in changed if f in contenders]
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   310
        if not newfiles:
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   311
            ui.status(_('no changes to record\n'))
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   312
            return 0
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   313
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   314
        modified = set(status.modified)
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   315
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   316
        # 2. backup changed files, so we can restore them in the end
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   317
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   318
        if backupall:
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   319
            tobackup = changed
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   320
        else:
24845
8133494accf1 record: edit patch of newly added files (issue4304)
Laurent Charignon <lcharignon@fb.com>
parents: 24843
diff changeset
   321
            tobackup = [f for f in newfiles if f in modified or f in \
8133494accf1 record: edit patch of newly added files (issue4304)
Laurent Charignon <lcharignon@fb.com>
parents: 24843
diff changeset
   322
                    newlyaddedandmodifiedfiles]
24272
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   323
        backups = {}
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   324
        if tobackup:
31320
1b0db28dadf1 cmdutil: directly use repo.vfs.join
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31309
diff changeset
   325
            backupdir = repo.vfs.join('record-backups')
24272
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   326
            try:
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   327
                os.mkdir(backupdir)
25660
328739ea70c3 global: mass rewrite to use modern exception syntax
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25657
diff changeset
   328
            except OSError as err:
24272
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   329
                if err.errno != errno.EEXIST:
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   330
                    raise
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   331
        try:
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   332
            # backup continues
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   333
            for f in tobackup:
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   334
                fd, tmpname = tempfile.mkstemp(prefix=f.replace('/', '_')+'.',
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   335
                                               dir=backupdir)
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   336
                os.close(fd)
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   337
                ui.debug('backup %r as %r\n' % (f, tmpname))
27370
d9e3ebe56970 record: don't dereference symlinks while copying over stat data
Siddharth Agarwal <sid0@fb.com>
parents: 26781
diff changeset
   338
                util.copyfile(repo.wjoin(f), tmpname, copystat=True)
24272
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   339
                backups[f] = tmpname
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   340
28861
86db5cb55d46 pycompat: switch to util.stringio for py3 compat
timeless <timeless@mozdev.org>
parents: 28837
diff changeset
   341
            fp = stringio()
24272
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   342
            for c in chunks:
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   343
                fname = c.filename()
24837
edf907bd8144 record: fix record with change on moved file crashes (issue4619)
Laurent Charignon <lcharignon@fb.com>
parents: 24720
diff changeset
   344
                if fname in backups:
24272
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   345
                    c.write(fp)
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   346
            dopatch = fp.tell()
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   347
            fp.seek(0)
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   348
28638
44319097e7b9 crecord: re-enable reviewing a patch before comitting it
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 28630
diff changeset
   349
            # 2.5 optionally review / modify patch in text editor
44319097e7b9 crecord: re-enable reviewing a patch before comitting it
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 28630
diff changeset
   350
            if opts.get('review', False):
44319097e7b9 crecord: re-enable reviewing a patch before comitting it
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 28630
diff changeset
   351
                patchtext = (crecordmod.diffhelptext
44319097e7b9 crecord: re-enable reviewing a patch before comitting it
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 28630
diff changeset
   352
                             + crecordmod.patchhelptext
44319097e7b9 crecord: re-enable reviewing a patch before comitting it
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 28630
diff changeset
   353
                             + fp.read())
44319097e7b9 crecord: re-enable reviewing a patch before comitting it
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 28630
diff changeset
   354
                reviewedpatch = ui.edit(patchtext, "",
34029
6e6452bc441d editor: use an unambiguous path suffix for editor files
Michael Bolin <mbolin@fb.com>
parents: 34022
diff changeset
   355
                                        action="diff",
30848
7080652af6e6 ui: rename tmpdir parameter to more specific repopath
Sean Farley <sean@farley.io>
parents: 30836
diff changeset
   356
                                        repopath=repo.path)
28638
44319097e7b9 crecord: re-enable reviewing a patch before comitting it
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 28630
diff changeset
   357
                fp.truncate(0)
44319097e7b9 crecord: re-enable reviewing a patch before comitting it
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 28630
diff changeset
   358
                fp.write(reviewedpatch)
44319097e7b9 crecord: re-enable reviewing a patch before comitting it
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 28630
diff changeset
   359
                fp.seek(0)
44319097e7b9 crecord: re-enable reviewing a patch before comitting it
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 28630
diff changeset
   360
24866
e1ec3d075c67 record: fix adding new file with record from within a subdir (issue4626)
Laurent Charignon <lcharignon@fb.com>
parents: 24864
diff changeset
   361
            [os.unlink(repo.wjoin(c)) for c in newlyaddedandmodifiedfiles]
24272
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   362
            # 3a. apply filtered patch to clean repo  (clean)
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   363
            if backups:
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   364
                # Equivalent to hg.revert
27344
43c00ca887d1 merge: have merge.update use a matcher instead of partial fn
Augie Fackler <augie@google.com>
parents: 27216
diff changeset
   365
                m = scmutil.matchfiles(repo, backups.keys())
24272
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   366
                mergemod.update(repo, repo.dirstate.p1(),
27344
43c00ca887d1 merge: have merge.update use a matcher instead of partial fn
Augie Fackler <augie@google.com>
parents: 27216
diff changeset
   367
                        False, True, matcher=m)
24272
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   368
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   369
            # 3b. (apply)
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   370
            if dopatch:
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   371
                try:
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   372
                    ui.debug('applying patch\n')
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   373
                    ui.debug(fp.getvalue())
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   374
                    patch.internalpatch(ui, repo, fp, 1, eolmode=None)
34251
61714510220d error: move patch.PatchError so it can easily implement __bytes__ (API)
Yuya Nishihara <yuya@tcha.org>
parents: 34189
diff changeset
   375
                except error.PatchError as err:
36719
390d16ea7c76 py3: use pycompat.bytestr instead of str
Pulkit Goyal <7895pulkit@gmail.com>
parents: 36663
diff changeset
   376
                    raise error.Abort(pycompat.bytestr(err))
24272
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   377
            del fp
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   378
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   379
            # 4. We prepared working directory according to filtered
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   380
            #    patch. Now is the time to delegate the job to
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   381
            #    commit/qrefresh or the like!
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   382
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   383
            # Make all of the pathnames absolute.
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   384
            newfiles = [repo.wjoin(nf) for nf in newfiles]
35350
82ee401135dd py3: handle keyword arguments correctly in cmdutil.py
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35277
diff changeset
   385
            return commitfunc(ui, repo, *newfiles, **pycompat.strkwargs(opts))
24272
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   386
        finally:
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   387
            # 5. finally restore backed-up files
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   388
            try:
25759
ff11c1565c04 cmdutil: apply dirstate.normallookup on (maybe partially) committed files
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 25758
diff changeset
   389
                dirstate = repo.dirstate
24272
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   390
                for realname, tmpname in backups.iteritems():
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   391
                    ui.debug('restoring %r to %r\n' % (tmpname, realname))
25759
ff11c1565c04 cmdutil: apply dirstate.normallookup on (maybe partially) committed files
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 25758
diff changeset
   392
ff11c1565c04 cmdutil: apply dirstate.normallookup on (maybe partially) committed files
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 25758
diff changeset
   393
                    if dirstate[realname] == 'n':
ff11c1565c04 cmdutil: apply dirstate.normallookup on (maybe partially) committed files
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 25758
diff changeset
   394
                        # without normallookup, restoring timestamp
ff11c1565c04 cmdutil: apply dirstate.normallookup on (maybe partially) committed files
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 25758
diff changeset
   395
                        # may cause partially committed files
ff11c1565c04 cmdutil: apply dirstate.normallookup on (maybe partially) committed files
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 25758
diff changeset
   396
                        # to be treated as unmodified
ff11c1565c04 cmdutil: apply dirstate.normallookup on (maybe partially) committed files
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 25758
diff changeset
   397
                        dirstate.normallookup(realname)
ff11c1565c04 cmdutil: apply dirstate.normallookup on (maybe partially) committed files
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 25758
diff changeset
   398
27370
d9e3ebe56970 record: don't dereference symlinks while copying over stat data
Siddharth Agarwal <sid0@fb.com>
parents: 26781
diff changeset
   399
                    # copystat=True here and above are a hack to trick any
d9e3ebe56970 record: don't dereference symlinks while copying over stat data
Siddharth Agarwal <sid0@fb.com>
parents: 26781
diff changeset
   400
                    # editors that have f open that we haven't modified them.
24272
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   401
                    #
27370
d9e3ebe56970 record: don't dereference symlinks while copying over stat data
Siddharth Agarwal <sid0@fb.com>
parents: 26781
diff changeset
   402
                    # Also note that this racy as an editor could notice the
d9e3ebe56970 record: don't dereference symlinks while copying over stat data
Siddharth Agarwal <sid0@fb.com>
parents: 26781
diff changeset
   403
                    # file's mtime before we've finished writing it.
d9e3ebe56970 record: don't dereference symlinks while copying over stat data
Siddharth Agarwal <sid0@fb.com>
parents: 26781
diff changeset
   404
                    util.copyfile(tmpname, repo.wjoin(realname), copystat=True)
24272
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   405
                    os.unlink(tmpname)
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   406
                if tobackup:
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   407
                    os.rmdir(backupdir)
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   408
            except OSError:
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   409
                pass
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   410
25758
c5dfa47ad7ee cmdutil: put recordfunc invocation into wlock scope for consistency
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 25755
diff changeset
   411
    def recordinwlock(ui, repo, message, match, opts):
27801
7be6371c732e with: use context manager for wlock in recordinwlock
Bryan O'Sullivan <bryano@fb.com>
parents: 27698
diff changeset
   412
        with repo.wlock():
25758
c5dfa47ad7ee cmdutil: put recordfunc invocation into wlock scope for consistency
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 25755
diff changeset
   413
            return recordfunc(ui, repo, message, match, opts)
c5dfa47ad7ee cmdutil: put recordfunc invocation into wlock scope for consistency
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 25755
diff changeset
   414
c5dfa47ad7ee cmdutil: put recordfunc invocation into wlock scope for consistency
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 25755
diff changeset
   415
    return commit(ui, repo, recordinwlock, pats, opts)
24272
26a1c617e047 record: move dorecord from record to cmdutil
Laurent Charignon <lcharignon@fb.com>
parents: 24260
diff changeset
   416
34682
7e3001b74ab3 tersestatus: re-implement the functionality to terse the status
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34372
diff changeset
   417
class dirnode(object):
33548
4cd4344a53c4 status: add a flag to terse the output (issue4119)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33509
diff changeset
   418
    """
34698
23eb03f46929 tersestatus: rework dirnode and tersedir docstrings
Denis Laxalde <denis@laxalde.org>
parents: 34684
diff changeset
   419
    Represent a directory in user working copy with information required for
23eb03f46929 tersestatus: rework dirnode and tersedir docstrings
Denis Laxalde <denis@laxalde.org>
parents: 34684
diff changeset
   420
    the purpose of tersing its status.
34682
7e3001b74ab3 tersestatus: re-implement the functionality to terse the status
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34372
diff changeset
   421
7e3001b74ab3 tersestatus: re-implement the functionality to terse the status
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34372
diff changeset
   422
    path is the path to the directory
7e3001b74ab3 tersestatus: re-implement the functionality to terse the status
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34372
diff changeset
   423
7e3001b74ab3 tersestatus: re-implement the functionality to terse the status
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34372
diff changeset
   424
    statuses is a set of statuses of all files in this directory (this includes
7e3001b74ab3 tersestatus: re-implement the functionality to terse the status
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34372
diff changeset
   425
    all the files in all the subdirectories too)
7e3001b74ab3 tersestatus: re-implement the functionality to terse the status
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34372
diff changeset
   426
7e3001b74ab3 tersestatus: re-implement the functionality to terse the status
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34372
diff changeset
   427
    files is a list of files which are direct child of this directory
7e3001b74ab3 tersestatus: re-implement the functionality to terse the status
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34372
diff changeset
   428
7e3001b74ab3 tersestatus: re-implement the functionality to terse the status
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34372
diff changeset
   429
    subdirs is a dictionary of sub-directory name as the key and it's own
7e3001b74ab3 tersestatus: re-implement the functionality to terse the status
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34372
diff changeset
   430
    dirnode object as the value
7e3001b74ab3 tersestatus: re-implement the functionality to terse the status
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34372
diff changeset
   431
    """
7e3001b74ab3 tersestatus: re-implement the functionality to terse the status
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34372
diff changeset
   432
7e3001b74ab3 tersestatus: re-implement the functionality to terse the status
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34372
diff changeset
   433
    def __init__(self, dirpath):
7e3001b74ab3 tersestatus: re-implement the functionality to terse the status
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34372
diff changeset
   434
        self.path = dirpath
7e3001b74ab3 tersestatus: re-implement the functionality to terse the status
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34372
diff changeset
   435
        self.statuses = set([])
7e3001b74ab3 tersestatus: re-implement the functionality to terse the status
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34372
diff changeset
   436
        self.files = []
7e3001b74ab3 tersestatus: re-implement the functionality to terse the status
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34372
diff changeset
   437
        self.subdirs = {}
7e3001b74ab3 tersestatus: re-implement the functionality to terse the status
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34372
diff changeset
   438
7e3001b74ab3 tersestatus: re-implement the functionality to terse the status
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34372
diff changeset
   439
    def _addfileindir(self, filename, status):
34698
23eb03f46929 tersestatus: rework dirnode and tersedir docstrings
Denis Laxalde <denis@laxalde.org>
parents: 34684
diff changeset
   440
        """Add a file in this directory as a direct child."""
34682
7e3001b74ab3 tersestatus: re-implement the functionality to terse the status
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34372
diff changeset
   441
        self.files.append((filename, status))
7e3001b74ab3 tersestatus: re-implement the functionality to terse the status
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34372
diff changeset
   442
7e3001b74ab3 tersestatus: re-implement the functionality to terse the status
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34372
diff changeset
   443
    def addfile(self, filename, status):
33548
4cd4344a53c4 status: add a flag to terse the output (issue4119)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33509
diff changeset
   444
        """
34698
23eb03f46929 tersestatus: rework dirnode and tersedir docstrings
Denis Laxalde <denis@laxalde.org>
parents: 34684
diff changeset
   445
        Add a file to this directory or to its direct parent directory.
23eb03f46929 tersestatus: rework dirnode and tersedir docstrings
Denis Laxalde <denis@laxalde.org>
parents: 34684
diff changeset
   446
23eb03f46929 tersestatus: rework dirnode and tersedir docstrings
Denis Laxalde <denis@laxalde.org>
parents: 34684
diff changeset
   447
        If the file is not direct child of this directory, we traverse to the
23eb03f46929 tersestatus: rework dirnode and tersedir docstrings
Denis Laxalde <denis@laxalde.org>
parents: 34684
diff changeset
   448
        directory of which this file is a direct child of and add the file
23eb03f46929 tersestatus: rework dirnode and tersedir docstrings
Denis Laxalde <denis@laxalde.org>
parents: 34684
diff changeset
   449
        there.
33548
4cd4344a53c4 status: add a flag to terse the output (issue4119)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33509
diff changeset
   450
        """
34682
7e3001b74ab3 tersestatus: re-implement the functionality to terse the status
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34372
diff changeset
   451
7e3001b74ab3 tersestatus: re-implement the functionality to terse the status
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34372
diff changeset
   452
        # the filename contains a path separator, it means it's not the direct
7e3001b74ab3 tersestatus: re-implement the functionality to terse the status
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34372
diff changeset
   453
        # child of this directory
34928
362096cfdb1f terse: split on repo separator instead of os.sep (issue5715)
Matt Harbison <matt_harbison@yahoo.com>
parents: 34907
diff changeset
   454
        if '/' in filename:
362096cfdb1f terse: split on repo separator instead of os.sep (issue5715)
Matt Harbison <matt_harbison@yahoo.com>
parents: 34907
diff changeset
   455
            subdir, filep = filename.split('/', 1)
34682
7e3001b74ab3 tersestatus: re-implement the functionality to terse the status
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34372
diff changeset
   456
7e3001b74ab3 tersestatus: re-implement the functionality to terse the status
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34372
diff changeset
   457
            # does the dirnode object for subdir exists
7e3001b74ab3 tersestatus: re-implement the functionality to terse the status
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34372
diff changeset
   458
            if subdir not in self.subdirs:
7e3001b74ab3 tersestatus: re-implement the functionality to terse the status
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34372
diff changeset
   459
                subdirpath = os.path.join(self.path, subdir)
7e3001b74ab3 tersestatus: re-implement the functionality to terse the status
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34372
diff changeset
   460
                self.subdirs[subdir] = dirnode(subdirpath)
7e3001b74ab3 tersestatus: re-implement the functionality to terse the status
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34372
diff changeset
   461
7e3001b74ab3 tersestatus: re-implement the functionality to terse the status
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34372
diff changeset
   462
            # try adding the file in subdir
7e3001b74ab3 tersestatus: re-implement the functionality to terse the status
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34372
diff changeset
   463
            self.subdirs[subdir].addfile(filep, status)
7e3001b74ab3 tersestatus: re-implement the functionality to terse the status
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34372
diff changeset
   464
7e3001b74ab3 tersestatus: re-implement the functionality to terse the status
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34372
diff changeset
   465
        else:
7e3001b74ab3 tersestatus: re-implement the functionality to terse the status
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34372
diff changeset
   466
            self._addfileindir(filename, status)
7e3001b74ab3 tersestatus: re-implement the functionality to terse the status
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34372
diff changeset
   467
7e3001b74ab3 tersestatus: re-implement the functionality to terse the status
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34372
diff changeset
   468
        if status not in self.statuses:
7e3001b74ab3 tersestatus: re-implement the functionality to terse the status
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34372
diff changeset
   469
            self.statuses.add(status)
7e3001b74ab3 tersestatus: re-implement the functionality to terse the status
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34372
diff changeset
   470
34684
5d98674df18a tersestatus: avoid modifying tersedict
Denis Laxalde <denis@laxalde.org>
parents: 34683
diff changeset
   471
    def iterfilepaths(self):
34698
23eb03f46929 tersestatus: rework dirnode and tersedir docstrings
Denis Laxalde <denis@laxalde.org>
parents: 34684
diff changeset
   472
        """Yield (status, path) for files directly under this directory."""
34683
3d6d4b12128e tersestatus: make methods part of the dirnode class
Denis Laxalde <denis@laxalde.org>
parents: 34682
diff changeset
   473
        for f, st in self.files:
34684
5d98674df18a tersestatus: avoid modifying tersedict
Denis Laxalde <denis@laxalde.org>
parents: 34683
diff changeset
   474
            yield st, os.path.join(self.path, f)
5d98674df18a tersestatus: avoid modifying tersedict
Denis Laxalde <denis@laxalde.org>
parents: 34683
diff changeset
   475
5d98674df18a tersestatus: avoid modifying tersedict
Denis Laxalde <denis@laxalde.org>
parents: 34683
diff changeset
   476
    def tersewalk(self, terseargs):
34683
3d6d4b12128e tersestatus: make methods part of the dirnode class
Denis Laxalde <denis@laxalde.org>
parents: 34682
diff changeset
   477
        """
34698
23eb03f46929 tersestatus: rework dirnode and tersedir docstrings
Denis Laxalde <denis@laxalde.org>
parents: 34684
diff changeset
   478
        Yield (status, path) obtained by processing the status of this
23eb03f46929 tersestatus: rework dirnode and tersedir docstrings
Denis Laxalde <denis@laxalde.org>
parents: 34684
diff changeset
   479
        dirnode.
34683
3d6d4b12128e tersestatus: make methods part of the dirnode class
Denis Laxalde <denis@laxalde.org>
parents: 34682
diff changeset
   480
3d6d4b12128e tersestatus: make methods part of the dirnode class
Denis Laxalde <denis@laxalde.org>
parents: 34682
diff changeset
   481
        terseargs is the string of arguments passed by the user with `--terse`
3d6d4b12128e tersestatus: make methods part of the dirnode class
Denis Laxalde <denis@laxalde.org>
parents: 34682
diff changeset
   482
        flag.
3d6d4b12128e tersestatus: make methods part of the dirnode class
Denis Laxalde <denis@laxalde.org>
parents: 34682
diff changeset
   483
3d6d4b12128e tersestatus: make methods part of the dirnode class
Denis Laxalde <denis@laxalde.org>
parents: 34682
diff changeset
   484
        Following are the cases which can happen:
3d6d4b12128e tersestatus: make methods part of the dirnode class
Denis Laxalde <denis@laxalde.org>
parents: 34682
diff changeset
   485
3d6d4b12128e tersestatus: make methods part of the dirnode class
Denis Laxalde <denis@laxalde.org>
parents: 34682
diff changeset
   486
        1) All the files in the directory (including all the files in its
3d6d4b12128e tersestatus: make methods part of the dirnode class
Denis Laxalde <denis@laxalde.org>
parents: 34682
diff changeset
   487
        subdirectories) share the same status and the user has asked us to terse
34684
5d98674df18a tersestatus: avoid modifying tersedict
Denis Laxalde <denis@laxalde.org>
parents: 34683
diff changeset
   488
        that status. -> yield (status, dirpath)
34683
3d6d4b12128e tersestatus: make methods part of the dirnode class
Denis Laxalde <denis@laxalde.org>
parents: 34682
diff changeset
   489
34698
23eb03f46929 tersestatus: rework dirnode and tersedir docstrings
Denis Laxalde <denis@laxalde.org>
parents: 34684
diff changeset
   490
        2) Otherwise, we do following:
34683
3d6d4b12128e tersestatus: make methods part of the dirnode class
Denis Laxalde <denis@laxalde.org>
parents: 34682
diff changeset
   491
34684
5d98674df18a tersestatus: avoid modifying tersedict
Denis Laxalde <denis@laxalde.org>
parents: 34683
diff changeset
   492
                a) Yield (status, filepath)  for all the files which are in this
5d98674df18a tersestatus: avoid modifying tersedict
Denis Laxalde <denis@laxalde.org>
parents: 34683
diff changeset
   493
                    directory (only the ones in this directory, not the subdirs)
34683
3d6d4b12128e tersestatus: make methods part of the dirnode class
Denis Laxalde <denis@laxalde.org>
parents: 34682
diff changeset
   494
3d6d4b12128e tersestatus: make methods part of the dirnode class
Denis Laxalde <denis@laxalde.org>
parents: 34682
diff changeset
   495
                b) Recurse the function on all the subdirectories of this
3d6d4b12128e tersestatus: make methods part of the dirnode class
Denis Laxalde <denis@laxalde.org>
parents: 34682
diff changeset
   496
                   directory
3d6d4b12128e tersestatus: make methods part of the dirnode class
Denis Laxalde <denis@laxalde.org>
parents: 34682
diff changeset
   497
        """
3d6d4b12128e tersestatus: make methods part of the dirnode class
Denis Laxalde <denis@laxalde.org>
parents: 34682
diff changeset
   498
3d6d4b12128e tersestatus: make methods part of the dirnode class
Denis Laxalde <denis@laxalde.org>
parents: 34682
diff changeset
   499
        if len(self.statuses) == 1:
3d6d4b12128e tersestatus: make methods part of the dirnode class
Denis Laxalde <denis@laxalde.org>
parents: 34682
diff changeset
   500
            onlyst = self.statuses.pop()
3d6d4b12128e tersestatus: make methods part of the dirnode class
Denis Laxalde <denis@laxalde.org>
parents: 34682
diff changeset
   501
3d6d4b12128e tersestatus: make methods part of the dirnode class
Denis Laxalde <denis@laxalde.org>
parents: 34682
diff changeset
   502
            # Making sure we terse only when the status abbreviation is
3d6d4b12128e tersestatus: make methods part of the dirnode class
Denis Laxalde <denis@laxalde.org>
parents: 34682
diff changeset
   503
            # passed as terse argument
3d6d4b12128e tersestatus: make methods part of the dirnode class
Denis Laxalde <denis@laxalde.org>
parents: 34682
diff changeset
   504
            if onlyst in terseargs:
34684
5d98674df18a tersestatus: avoid modifying tersedict
Denis Laxalde <denis@laxalde.org>
parents: 34683
diff changeset
   505
                yield onlyst, self.path + pycompat.ossep
34683
3d6d4b12128e tersestatus: make methods part of the dirnode class
Denis Laxalde <denis@laxalde.org>
parents: 34682
diff changeset
   506
                return
3d6d4b12128e tersestatus: make methods part of the dirnode class
Denis Laxalde <denis@laxalde.org>
parents: 34682
diff changeset
   507
3d6d4b12128e tersestatus: make methods part of the dirnode class
Denis Laxalde <denis@laxalde.org>
parents: 34682
diff changeset
   508
        # add the files to status list
34684
5d98674df18a tersestatus: avoid modifying tersedict
Denis Laxalde <denis@laxalde.org>
parents: 34683
diff changeset
   509
        for st, fpath in self.iterfilepaths():
5d98674df18a tersestatus: avoid modifying tersedict
Denis Laxalde <denis@laxalde.org>
parents: 34683
diff changeset
   510
            yield st, fpath
34683
3d6d4b12128e tersestatus: make methods part of the dirnode class
Denis Laxalde <denis@laxalde.org>
parents: 34682
diff changeset
   511
3d6d4b12128e tersestatus: make methods part of the dirnode class
Denis Laxalde <denis@laxalde.org>
parents: 34682
diff changeset
   512
        #recurse on the subdirs
3d6d4b12128e tersestatus: make methods part of the dirnode class
Denis Laxalde <denis@laxalde.org>
parents: 34682
diff changeset
   513
        for dirobj in self.subdirs.values():
34684
5d98674df18a tersestatus: avoid modifying tersedict
Denis Laxalde <denis@laxalde.org>
parents: 34683
diff changeset
   514
            for st, fpath in dirobj.tersewalk(terseargs):
5d98674df18a tersestatus: avoid modifying tersedict
Denis Laxalde <denis@laxalde.org>
parents: 34683
diff changeset
   515
                yield st, fpath
34682
7e3001b74ab3 tersestatus: re-implement the functionality to terse the status
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34372
diff changeset
   516
7e3001b74ab3 tersestatus: re-implement the functionality to terse the status
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34372
diff changeset
   517
def tersedir(statuslist, terseargs):
7e3001b74ab3 tersestatus: re-implement the functionality to terse the status
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34372
diff changeset
   518
    """
34698
23eb03f46929 tersestatus: rework dirnode and tersedir docstrings
Denis Laxalde <denis@laxalde.org>
parents: 34684
diff changeset
   519
    Terse the status if all the files in a directory shares the same status.
34682
7e3001b74ab3 tersestatus: re-implement the functionality to terse the status
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34372
diff changeset
   520
7e3001b74ab3 tersestatus: re-implement the functionality to terse the status
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34372
diff changeset
   521
    statuslist is scmutil.status() object which contains a list of files for
7e3001b74ab3 tersestatus: re-implement the functionality to terse the status
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34372
diff changeset
   522
    each status.
7e3001b74ab3 tersestatus: re-implement the functionality to terse the status
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34372
diff changeset
   523
    terseargs is string which is passed by the user as the argument to `--terse`
7e3001b74ab3 tersestatus: re-implement the functionality to terse the status
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34372
diff changeset
   524
    flag.
7e3001b74ab3 tersestatus: re-implement the functionality to terse the status
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34372
diff changeset
   525
7e3001b74ab3 tersestatus: re-implement the functionality to terse the status
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34372
diff changeset
   526
    The function makes a tree of objects of dirnode class, and at each node it
7e3001b74ab3 tersestatus: re-implement the functionality to terse the status
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34372
diff changeset
   527
    stores the information required to know whether we can terse a certain
7e3001b74ab3 tersestatus: re-implement the functionality to terse the status
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34372
diff changeset
   528
    directory or not.
7e3001b74ab3 tersestatus: re-implement the functionality to terse the status
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34372
diff changeset
   529
    """
7e3001b74ab3 tersestatus: re-implement the functionality to terse the status
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34372
diff changeset
   530
    # the order matters here as that is used to produce final list
7e3001b74ab3 tersestatus: re-implement the functionality to terse the status
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34372
diff changeset
   531
    allst = ('m', 'a', 'r', 'd', 'u', 'i', 'c')
7e3001b74ab3 tersestatus: re-implement the functionality to terse the status
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34372
diff changeset
   532
7e3001b74ab3 tersestatus: re-implement the functionality to terse the status
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34372
diff changeset
   533
    # checking the argument validity
34893
068e0e531584 cmdutil: fix status tersing on Python 3
Augie Fackler <augie@google.com>
parents: 34857
diff changeset
   534
    for s in pycompat.bytestr(terseargs):
34682
7e3001b74ab3 tersestatus: re-implement the functionality to terse the status
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34372
diff changeset
   535
        if s not in allst:
7e3001b74ab3 tersestatus: re-implement the functionality to terse the status
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34372
diff changeset
   536
            raise error.Abort(_("'%s' not recognized") % s)
7e3001b74ab3 tersestatus: re-implement the functionality to terse the status
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34372
diff changeset
   537
7e3001b74ab3 tersestatus: re-implement the functionality to terse the status
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34372
diff changeset
   538
    # creating a dirnode object for the root of the repo
7e3001b74ab3 tersestatus: re-implement the functionality to terse the status
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34372
diff changeset
   539
    rootobj = dirnode('')
7e3001b74ab3 tersestatus: re-implement the functionality to terse the status
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34372
diff changeset
   540
    pstatus = ('modified', 'added', 'deleted', 'clean', 'unknown',
7e3001b74ab3 tersestatus: re-implement the functionality to terse the status
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34372
diff changeset
   541
               'ignored', 'removed')
7e3001b74ab3 tersestatus: re-implement the functionality to terse the status
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34372
diff changeset
   542
7e3001b74ab3 tersestatus: re-implement the functionality to terse the status
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34372
diff changeset
   543
    tersedict = {}
7e3001b74ab3 tersestatus: re-implement the functionality to terse the status
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34372
diff changeset
   544
    for attrname in pstatus:
34893
068e0e531584 cmdutil: fix status tersing on Python 3
Augie Fackler <augie@google.com>
parents: 34857
diff changeset
   545
        statuschar = attrname[0:1]
34682
7e3001b74ab3 tersestatus: re-implement the functionality to terse the status
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34372
diff changeset
   546
        for f in getattr(statuslist, attrname):
34893
068e0e531584 cmdutil: fix status tersing on Python 3
Augie Fackler <augie@google.com>
parents: 34857
diff changeset
   547
            rootobj.addfile(f, statuschar)
068e0e531584 cmdutil: fix status tersing on Python 3
Augie Fackler <augie@google.com>
parents: 34857
diff changeset
   548
        tersedict[statuschar] = []
34682
7e3001b74ab3 tersestatus: re-implement the functionality to terse the status
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34372
diff changeset
   549
7e3001b74ab3 tersestatus: re-implement the functionality to terse the status
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34372
diff changeset
   550
    # we won't be tersing the root dir, so add files in it
34684
5d98674df18a tersestatus: avoid modifying tersedict
Denis Laxalde <denis@laxalde.org>
parents: 34683
diff changeset
   551
    for st, fpath in rootobj.iterfilepaths():
5d98674df18a tersestatus: avoid modifying tersedict
Denis Laxalde <denis@laxalde.org>
parents: 34683
diff changeset
   552
        tersedict[st].append(fpath)
34682
7e3001b74ab3 tersestatus: re-implement the functionality to terse the status
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34372
diff changeset
   553
7e3001b74ab3 tersestatus: re-implement the functionality to terse the status
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34372
diff changeset
   554
    # process each sub-directory and build tersedict
7e3001b74ab3 tersestatus: re-implement the functionality to terse the status
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34372
diff changeset
   555
    for subdir in rootobj.subdirs.values():
34684
5d98674df18a tersestatus: avoid modifying tersedict
Denis Laxalde <denis@laxalde.org>
parents: 34683
diff changeset
   556
        for st, f in subdir.tersewalk(terseargs):
5d98674df18a tersestatus: avoid modifying tersedict
Denis Laxalde <denis@laxalde.org>
parents: 34683
diff changeset
   557
            tersedict[st].append(f)
34682
7e3001b74ab3 tersestatus: re-implement the functionality to terse the status
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34372
diff changeset
   558
7e3001b74ab3 tersestatus: re-implement the functionality to terse the status
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34372
diff changeset
   559
    tersedlist = []
7e3001b74ab3 tersestatus: re-implement the functionality to terse the status
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34372
diff changeset
   560
    for st in allst:
7e3001b74ab3 tersestatus: re-implement the functionality to terse the status
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34372
diff changeset
   561
        tersedict[st].sort()
7e3001b74ab3 tersestatus: re-implement the functionality to terse the status
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34372
diff changeset
   562
        tersedlist.append(tersedict[st])
7e3001b74ab3 tersestatus: re-implement the functionality to terse the status
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34372
diff changeset
   563
7e3001b74ab3 tersestatus: re-implement the functionality to terse the status
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34372
diff changeset
   564
    return tersedlist
33548
4cd4344a53c4 status: add a flag to terse the output (issue4119)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33509
diff changeset
   565
33771
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   566
def _commentlines(raw):
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   567
    '''Surround lineswith a comment char and a new line'''
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   568
    lines = raw.splitlines()
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   569
    commentedlines = ['# %s' % line for line in lines]
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   570
    return '\n'.join(commentedlines) + '\n'
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   571
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   572
def _conflictsmsg(repo):
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   573
    mergestate = mergemod.mergestate.read(repo)
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   574
    if not mergestate.active():
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   575
        return
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   576
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   577
    m = scmutil.match(repo[None])
34001
3340efe80803 morestatus: simplify check for unresolved merge conflicts
Martin von Zweigbergk <martinvonz@google.com>
parents: 33883
diff changeset
   578
    unresolvedlist = [f for f in mergestate.unresolved() if m(f)]
33771
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   579
    if unresolvedlist:
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   580
        mergeliststr = '\n'.join(
34981
527655a049ee morestatus: don't crash with different drive letters for repo.root and CWD
Matt Harbison <matt_harbison@yahoo.com>
parents: 34928
diff changeset
   581
            ['    %s' % util.pathto(repo.root, pycompat.getcwd(), path)
527655a049ee morestatus: don't crash with different drive letters for repo.root and CWD
Matt Harbison <matt_harbison@yahoo.com>
parents: 34928
diff changeset
   582
             for path in unresolvedlist])
33771
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   583
        msg = _('''Unresolved merge conflicts:
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   584
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   585
%s
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   586
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   587
To mark files as resolved:  hg resolve --mark FILE''') % mergeliststr
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   588
    else:
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   589
        msg = _('No unresolved merge conflicts.')
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   590
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   591
    return _commentlines(msg)
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   592
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   593
def _helpmessage(continuecmd, abortcmd):
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   594
    msg = _('To continue:                %s\n'
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   595
            'To abort:                   %s') % (continuecmd, abortcmd)
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   596
    return _commentlines(msg)
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   597
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   598
def _rebasemsg():
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   599
    return _helpmessage('hg rebase --continue', 'hg rebase --abort')
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   600
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   601
def _histeditmsg():
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   602
    return _helpmessage('hg histedit --continue', 'hg histedit --abort')
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   603
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   604
def _unshelvemsg():
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   605
    return _helpmessage('hg unshelve --continue', 'hg unshelve --abort')
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   606
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   607
def _updatecleanmsg(dest=None):
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   608
    warning = _('warning: this will discard uncommitted changes')
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   609
    return 'hg update --clean %s    (%s)' % (dest or '.', warning)
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   610
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   611
def _graftmsg():
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   612
    # tweakdefaults requires `update` to have a rev hence the `.`
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   613
    return _helpmessage('hg graft --continue', _updatecleanmsg())
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   614
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   615
def _mergemsg():
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   616
    # tweakdefaults requires `update` to have a rev hence the `.`
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   617
     return _helpmessage('hg commit', _updatecleanmsg())
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   618
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   619
def _bisectmsg():
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   620
    msg = _('To mark the changeset good:    hg bisect --good\n'
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   621
            'To mark the changeset bad:     hg bisect --bad\n'
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   622
            'To abort:                      hg bisect --reset\n')
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   623
    return _commentlines(msg)
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   624
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   625
def fileexistspredicate(filename):
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   626
    return lambda repo: repo.vfs.exists(filename)
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   627
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   628
def _mergepredicate(repo):
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   629
    return len(repo[None].parents()) > 1
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   630
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   631
STATES = (
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   632
    # (state, predicate to detect states, helpful message function)
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   633
    ('histedit', fileexistspredicate('histedit-state'), _histeditmsg),
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   634
    ('bisect', fileexistspredicate('bisect.state'), _bisectmsg),
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   635
    ('graft', fileexistspredicate('graftstate'), _graftmsg),
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   636
    ('unshelve', fileexistspredicate('unshelverebasestate'), _unshelvemsg),
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   637
    ('rebase', fileexistspredicate('rebasestate'), _rebasemsg),
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   638
    # The merge state is part of a list that will be iterated over.
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   639
    # They need to be last because some of the other unfinished states may also
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   640
    # be in a merge or update state (eg. rebase, histedit, graft, etc).
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   641
    # We want those to have priority.
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   642
    ('merge', _mergepredicate, _mergemsg),
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   643
)
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   644
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   645
def _getrepostate(repo):
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   646
    # experimental config: commands.status.skipstates
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   647
    skip = set(repo.ui.configlist('commands', 'status.skipstates'))
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   648
    for state, statedetectionpredicate, msgfn in STATES:
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   649
        if state in skip:
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   650
            continue
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   651
        if statedetectionpredicate(repo):
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   652
            return (state, statedetectionpredicate, msgfn)
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   653
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   654
def morestatus(repo, fm):
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   655
    statetuple = _getrepostate(repo)
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   656
    label = 'status.morestatus'
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   657
    if statetuple:
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   658
        fm.startitem()
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   659
        state, statedetectionpredicate, helpfulmsg = statetuple
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   660
        statemsg = _('The repository is in an unfinished *%s* state.') % state
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   661
        fm.write('statemsg', '%s\n',  _commentlines(statemsg), label=label)
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   662
        conmsg = _conflictsmsg(repo)
33883
e5d104c35e51 morestatus: check whether the conflict message is None before printing
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33858
diff changeset
   663
        if conmsg:
e5d104c35e51 morestatus: check whether the conflict message is None before printing
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33858
diff changeset
   664
            fm.write('conflictsmsg', '%s\n', conmsg, label=label)
33771
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   665
        if helpfulmsg:
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   666
            helpmsg = helpfulmsg()
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   667
            fm.write('helpmsg', '%s\n', helpmsg, label=label)
96f43981c1c4 morestatus: move fb extension to core by plugging to `hg status --verbose`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33737
diff changeset
   668
7213
b4c035057d34 findcmd: have dispatch look up strict flag
Matt Mackall <mpm@selenic.com>
parents: 7121
diff changeset
   669
def findpossible(cmd, table, strict=False):
4549
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
   670
    """
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
   671
    Return cmd -> (aliases, command table entry)
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
   672
    for each matching command.
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
   673
    Return debug commands (or their aliases) only if no normal command matches.
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
   674
    """
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
   675
    choice = {}
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
   676
    debugchoice = {}
15600
195dbd1cef0c alias: shortcut command matching show shadowing works properly (issue3104)
Matt Mackall <mpm@selenic.com>
parents: 15231
diff changeset
   677
195dbd1cef0c alias: shortcut command matching show shadowing works properly (issue3104)
Matt Mackall <mpm@selenic.com>
parents: 15231
diff changeset
   678
    if cmd in table:
195dbd1cef0c alias: shortcut command matching show shadowing works properly (issue3104)
Matt Mackall <mpm@selenic.com>
parents: 15231
diff changeset
   679
        # short-circuit exact matches, "log" alias beats "^log|history"
195dbd1cef0c alias: shortcut command matching show shadowing works properly (issue3104)
Matt Mackall <mpm@selenic.com>
parents: 15231
diff changeset
   680
        keys = [cmd]
195dbd1cef0c alias: shortcut command matching show shadowing works properly (issue3104)
Matt Mackall <mpm@selenic.com>
parents: 15231
diff changeset
   681
    else:
195dbd1cef0c alias: shortcut command matching show shadowing works properly (issue3104)
Matt Mackall <mpm@selenic.com>
parents: 15231
diff changeset
   682
        keys = table.keys()
195dbd1cef0c alias: shortcut command matching show shadowing works properly (issue3104)
Matt Mackall <mpm@selenic.com>
parents: 15231
diff changeset
   683
24222
02d7b5cd373b dispatch: offer suggestions of similar-named commands
Augie Fackler <augie@google.com>
parents: 24216
diff changeset
   684
    allcmds = []
15600
195dbd1cef0c alias: shortcut command matching show shadowing works properly (issue3104)
Matt Mackall <mpm@selenic.com>
parents: 15231
diff changeset
   685
    for e in keys:
10401
6252852b4332 mq: add -Q option to all commands not in norepo
Brendan Cully <brendan@kublai.com>
parents: 10344
diff changeset
   686
        aliases = parsealiases(e)
24222
02d7b5cd373b dispatch: offer suggestions of similar-named commands
Augie Fackler <augie@google.com>
parents: 24216
diff changeset
   687
        allcmds.extend(aliases)
4549
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
   688
        found = None
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
   689
        if cmd in aliases:
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
   690
            found = cmd
7213
b4c035057d34 findcmd: have dispatch look up strict flag
Matt Mackall <mpm@selenic.com>
parents: 7121
diff changeset
   691
        elif not strict:
4549
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
   692
            for a in aliases:
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
   693
                if a.startswith(cmd):
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
   694
                    found = a
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
   695
                    break
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
   696
        if found is not None:
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
   697
            if aliases[0].startswith("debug") or found.startswith("debug"):
5178
18a9fbb5cd78 dispatch: move command dispatching into its own module
Matt Mackall <mpm@selenic.com>
parents: 5177
diff changeset
   698
                debugchoice[found] = (aliases, table[e])
4549
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
   699
            else:
5178
18a9fbb5cd78 dispatch: move command dispatching into its own module
Matt Mackall <mpm@selenic.com>
parents: 5177
diff changeset
   700
                choice[found] = (aliases, table[e])
4549
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
   701
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
   702
    if not choice and debugchoice:
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
   703
        choice = debugchoice
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
   704
24222
02d7b5cd373b dispatch: offer suggestions of similar-named commands
Augie Fackler <augie@google.com>
parents: 24216
diff changeset
   705
    return choice, allcmds
4549
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
   706
7213
b4c035057d34 findcmd: have dispatch look up strict flag
Matt Mackall <mpm@selenic.com>
parents: 7121
diff changeset
   707
def findcmd(cmd, table, strict=True):
4549
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
   708
    """Return (aliases, command table entry) for command string."""
24222
02d7b5cd373b dispatch: offer suggestions of similar-named commands
Augie Fackler <augie@google.com>
parents: 24216
diff changeset
   709
    choice, allcmds = findpossible(cmd, table, strict)
4549
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
   710
5915
d0576d065993 Prefer i in d over d.has_key(i)
Christian Ebert <blacktrash@gmx.net>
parents: 5843
diff changeset
   711
    if cmd in choice:
4549
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
   712
        return choice[cmd]
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
   713
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
   714
    if len(choice) > 1:
32528
9f56d462634c cmdutil: use sorted(dict) instead of x = dict.keys(); x.sort()
Augie Fackler <raf@durin42.com>
parents: 32434
diff changeset
   715
        clist = sorted(choice)
7643
9a1ea6587557 error: move UnknownCommand and AmbiguousCommand
Matt Mackall <mpm@selenic.com>
parents: 7404
diff changeset
   716
        raise error.AmbiguousCommand(cmd, clist)
4549
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
   717
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
   718
    if choice:
32862
e4a43b810528 py3: explicitly convert dict.values() to a list on py3
Pulkit Goyal <7895pulkit@gmail.com>
parents: 32842
diff changeset
   719
        return list(choice.values())[0]
4549
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
   720
24222
02d7b5cd373b dispatch: offer suggestions of similar-named commands
Augie Fackler <augie@google.com>
parents: 24216
diff changeset
   721
    raise error.UnknownCommand(cmd, allcmds)
4549
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
   722
35745
3bd8ab4c80a5 branch: add a --rev flag to change branch name of given revisions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35743
diff changeset
   723
def changebranch(ui, repo, revs, label):
3bd8ab4c80a5 branch: add a --rev flag to change branch name of given revisions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35743
diff changeset
   724
    """ Change the branch name of given revs to label """
3bd8ab4c80a5 branch: add a --rev flag to change branch name of given revisions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35743
diff changeset
   725
3bd8ab4c80a5 branch: add a --rev flag to change branch name of given revisions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35743
diff changeset
   726
    with repo.wlock(), repo.lock(), repo.transaction('branches'):
3bd8ab4c80a5 branch: add a --rev flag to change branch name of given revisions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35743
diff changeset
   727
        # abort in case of uncommitted merge or dirty wdir
3bd8ab4c80a5 branch: add a --rev flag to change branch name of given revisions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35743
diff changeset
   728
        bailifchanged(repo)
3bd8ab4c80a5 branch: add a --rev flag to change branch name of given revisions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35743
diff changeset
   729
        revs = scmutil.revrange(repo, revs)
3bd8ab4c80a5 branch: add a --rev flag to change branch name of given revisions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35743
diff changeset
   730
        if not revs:
3bd8ab4c80a5 branch: add a --rev flag to change branch name of given revisions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35743
diff changeset
   731
            raise error.Abort("empty revision set")
3bd8ab4c80a5 branch: add a --rev flag to change branch name of given revisions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35743
diff changeset
   732
        roots = repo.revs('roots(%ld)', revs)
3bd8ab4c80a5 branch: add a --rev flag to change branch name of given revisions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35743
diff changeset
   733
        if len(roots) > 1:
3bd8ab4c80a5 branch: add a --rev flag to change branch name of given revisions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35743
diff changeset
   734
            raise error.Abort(_("cannot change branch of non-linear revisions"))
3bd8ab4c80a5 branch: add a --rev flag to change branch name of given revisions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35743
diff changeset
   735
        rewriteutil.precheck(repo, revs, 'change branch of')
35746
e5b6ba786d83 branch: allow changing branch name to existing name if possible
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35745
diff changeset
   736
e5b6ba786d83 branch: allow changing branch name to existing name if possible
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35745
diff changeset
   737
        root = repo[roots.first()]
e5b6ba786d83 branch: allow changing branch name to existing name if possible
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35745
diff changeset
   738
        if not root.p1().branch() == label and label in repo.branchmap():
e5b6ba786d83 branch: allow changing branch name to existing name if possible
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35745
diff changeset
   739
            raise error.Abort(_("a branch of the same name already exists"))
e5b6ba786d83 branch: allow changing branch name to existing name if possible
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35745
diff changeset
   740
35745
3bd8ab4c80a5 branch: add a --rev flag to change branch name of given revisions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35743
diff changeset
   741
        if repo.revs('merge() and %ld', revs):
3bd8ab4c80a5 branch: add a --rev flag to change branch name of given revisions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35743
diff changeset
   742
            raise error.Abort(_("cannot change branch of a merge commit"))
3bd8ab4c80a5 branch: add a --rev flag to change branch name of given revisions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35743
diff changeset
   743
        if repo.revs('obsolete() and %ld', revs):
3bd8ab4c80a5 branch: add a --rev flag to change branch name of given revisions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35743
diff changeset
   744
            raise error.Abort(_("cannot change branch of a obsolete changeset"))
3bd8ab4c80a5 branch: add a --rev flag to change branch name of given revisions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35743
diff changeset
   745
3bd8ab4c80a5 branch: add a --rev flag to change branch name of given revisions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35743
diff changeset
   746
        # make sure only topological heads
3bd8ab4c80a5 branch: add a --rev flag to change branch name of given revisions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35743
diff changeset
   747
        if repo.revs('heads(%ld) - head()', revs):
3bd8ab4c80a5 branch: add a --rev flag to change branch name of given revisions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35743
diff changeset
   748
            raise error.Abort(_("cannot change branch in middle of a stack"))
3bd8ab4c80a5 branch: add a --rev flag to change branch name of given revisions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35743
diff changeset
   749
3bd8ab4c80a5 branch: add a --rev flag to change branch name of given revisions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35743
diff changeset
   750
        replacements = {}
3bd8ab4c80a5 branch: add a --rev flag to change branch name of given revisions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35743
diff changeset
   751
        # avoid import cycle mercurial.cmdutil -> mercurial.context ->
3bd8ab4c80a5 branch: add a --rev flag to change branch name of given revisions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35743
diff changeset
   752
        # mercurial.subrepo -> mercurial.cmdutil
3bd8ab4c80a5 branch: add a --rev flag to change branch name of given revisions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35743
diff changeset
   753
        from . import context
3bd8ab4c80a5 branch: add a --rev flag to change branch name of given revisions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35743
diff changeset
   754
        for rev in revs:
3bd8ab4c80a5 branch: add a --rev flag to change branch name of given revisions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35743
diff changeset
   755
            ctx = repo[rev]
3bd8ab4c80a5 branch: add a --rev flag to change branch name of given revisions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35743
diff changeset
   756
            oldbranch = ctx.branch()
3bd8ab4c80a5 branch: add a --rev flag to change branch name of given revisions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35743
diff changeset
   757
            # check if ctx has same branch
3bd8ab4c80a5 branch: add a --rev flag to change branch name of given revisions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35743
diff changeset
   758
            if oldbranch == label:
3bd8ab4c80a5 branch: add a --rev flag to change branch name of given revisions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35743
diff changeset
   759
                continue
3bd8ab4c80a5 branch: add a --rev flag to change branch name of given revisions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35743
diff changeset
   760
3bd8ab4c80a5 branch: add a --rev flag to change branch name of given revisions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35743
diff changeset
   761
            def filectxfn(repo, newctx, path):
3bd8ab4c80a5 branch: add a --rev flag to change branch name of given revisions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35743
diff changeset
   762
                try:
3bd8ab4c80a5 branch: add a --rev flag to change branch name of given revisions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35743
diff changeset
   763
                    return ctx[path]
3bd8ab4c80a5 branch: add a --rev flag to change branch name of given revisions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35743
diff changeset
   764
                except error.ManifestLookupError:
3bd8ab4c80a5 branch: add a --rev flag to change branch name of given revisions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35743
diff changeset
   765
                    return None
3bd8ab4c80a5 branch: add a --rev flag to change branch name of given revisions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35743
diff changeset
   766
3bd8ab4c80a5 branch: add a --rev flag to change branch name of given revisions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35743
diff changeset
   767
            ui.debug("changing branch of '%s' from '%s' to '%s'\n"
3bd8ab4c80a5 branch: add a --rev flag to change branch name of given revisions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35743
diff changeset
   768
                     % (hex(ctx.node()), oldbranch, label))
3bd8ab4c80a5 branch: add a --rev flag to change branch name of given revisions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35743
diff changeset
   769
            extra = ctx.extra()
3bd8ab4c80a5 branch: add a --rev flag to change branch name of given revisions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35743
diff changeset
   770
            extra['branch_change'] = hex(ctx.node())
3bd8ab4c80a5 branch: add a --rev flag to change branch name of given revisions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35743
diff changeset
   771
            # While changing branch of set of linear commits, make sure that
3bd8ab4c80a5 branch: add a --rev flag to change branch name of given revisions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35743
diff changeset
   772
            # we base our commits on new parent rather than old parent which
3bd8ab4c80a5 branch: add a --rev flag to change branch name of given revisions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35743
diff changeset
   773
            # was obsoleted while changing the branch
3bd8ab4c80a5 branch: add a --rev flag to change branch name of given revisions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35743
diff changeset
   774
            p1 = ctx.p1().node()
3bd8ab4c80a5 branch: add a --rev flag to change branch name of given revisions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35743
diff changeset
   775
            p2 = ctx.p2().node()
3bd8ab4c80a5 branch: add a --rev flag to change branch name of given revisions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35743
diff changeset
   776
            if p1 in replacements:
3bd8ab4c80a5 branch: add a --rev flag to change branch name of given revisions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35743
diff changeset
   777
                p1 = replacements[p1][0]
3bd8ab4c80a5 branch: add a --rev flag to change branch name of given revisions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35743
diff changeset
   778
            if p2 in replacements:
3bd8ab4c80a5 branch: add a --rev flag to change branch name of given revisions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35743
diff changeset
   779
                p2 = replacements[p2][0]
3bd8ab4c80a5 branch: add a --rev flag to change branch name of given revisions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35743
diff changeset
   780
3bd8ab4c80a5 branch: add a --rev flag to change branch name of given revisions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35743
diff changeset
   781
            mc = context.memctx(repo, (p1, p2),
3bd8ab4c80a5 branch: add a --rev flag to change branch name of given revisions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35743
diff changeset
   782
                                ctx.description(),
3bd8ab4c80a5 branch: add a --rev flag to change branch name of given revisions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35743
diff changeset
   783
                                ctx.files(),
3bd8ab4c80a5 branch: add a --rev flag to change branch name of given revisions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35743
diff changeset
   784
                                filectxfn,
3bd8ab4c80a5 branch: add a --rev flag to change branch name of given revisions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35743
diff changeset
   785
                                user=ctx.user(),
3bd8ab4c80a5 branch: add a --rev flag to change branch name of given revisions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35743
diff changeset
   786
                                date=ctx.date(),
3bd8ab4c80a5 branch: add a --rev flag to change branch name of given revisions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35743
diff changeset
   787
                                extra=extra,
3bd8ab4c80a5 branch: add a --rev flag to change branch name of given revisions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35743
diff changeset
   788
                                branch=label)
3bd8ab4c80a5 branch: add a --rev flag to change branch name of given revisions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35743
diff changeset
   789
3bd8ab4c80a5 branch: add a --rev flag to change branch name of given revisions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35743
diff changeset
   790
            commitphase = ctx.phase()
3bd8ab4c80a5 branch: add a --rev flag to change branch name of given revisions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35743
diff changeset
   791
            overrides = {('phases', 'new-commit'): commitphase}
3bd8ab4c80a5 branch: add a --rev flag to change branch name of given revisions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35743
diff changeset
   792
            with repo.ui.configoverride(overrides, 'branch-change'):
3bd8ab4c80a5 branch: add a --rev flag to change branch name of given revisions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35743
diff changeset
   793
                newnode = repo.commitctx(mc)
3bd8ab4c80a5 branch: add a --rev flag to change branch name of given revisions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35743
diff changeset
   794
3bd8ab4c80a5 branch: add a --rev flag to change branch name of given revisions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35743
diff changeset
   795
            replacements[ctx.node()] = (newnode,)
3bd8ab4c80a5 branch: add a --rev flag to change branch name of given revisions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35743
diff changeset
   796
            ui.debug('new node id is %s\n' % hex(newnode))
3bd8ab4c80a5 branch: add a --rev flag to change branch name of given revisions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35743
diff changeset
   797
3bd8ab4c80a5 branch: add a --rev flag to change branch name of given revisions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35743
diff changeset
   798
        # create obsmarkers and move bookmarks
3bd8ab4c80a5 branch: add a --rev flag to change branch name of given revisions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35743
diff changeset
   799
        scmutil.cleanupnodes(repo, replacements, 'branch-change')
3bd8ab4c80a5 branch: add a --rev flag to change branch name of given revisions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35743
diff changeset
   800
3bd8ab4c80a5 branch: add a --rev flag to change branch name of given revisions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35743
diff changeset
   801
        # move the working copy too
3bd8ab4c80a5 branch: add a --rev flag to change branch name of given revisions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35743
diff changeset
   802
        wctx = repo[None]
3bd8ab4c80a5 branch: add a --rev flag to change branch name of given revisions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35743
diff changeset
   803
        # in-progress merge is a bit too complex for now.
3bd8ab4c80a5 branch: add a --rev flag to change branch name of given revisions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35743
diff changeset
   804
        if len(wctx.parents()) == 1:
3bd8ab4c80a5 branch: add a --rev flag to change branch name of given revisions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35743
diff changeset
   805
            newid = replacements.get(wctx.p1().node())
3bd8ab4c80a5 branch: add a --rev flag to change branch name of given revisions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35743
diff changeset
   806
            if newid is not None:
3bd8ab4c80a5 branch: add a --rev flag to change branch name of given revisions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35743
diff changeset
   807
                # avoid import cycle mercurial.cmdutil -> mercurial.hg ->
3bd8ab4c80a5 branch: add a --rev flag to change branch name of given revisions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35743
diff changeset
   808
                # mercurial.cmdutil
3bd8ab4c80a5 branch: add a --rev flag to change branch name of given revisions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35743
diff changeset
   809
                from . import hg
3bd8ab4c80a5 branch: add a --rev flag to change branch name of given revisions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35743
diff changeset
   810
                hg.update(repo, newid[0], quietempty=True)
3bd8ab4c80a5 branch: add a --rev flag to change branch name of given revisions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35743
diff changeset
   811
3bd8ab4c80a5 branch: add a --rev flag to change branch name of given revisions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35743
diff changeset
   812
        ui.status(_("changed branch on %d changesets\n") % len(replacements))
3bd8ab4c80a5 branch: add a --rev flag to change branch name of given revisions
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35743
diff changeset
   813
10402
d216fa04e48a mq: make init -Q do what qinit -c did
Brendan Cully <brendan@kublai.com>
parents: 10401
diff changeset
   814
def findrepo(p):
d216fa04e48a mq: make init -Q do what qinit -c did
Brendan Cully <brendan@kublai.com>
parents: 10401
diff changeset
   815
    while not os.path.isdir(os.path.join(p, ".hg")):
d216fa04e48a mq: make init -Q do what qinit -c did
Brendan Cully <brendan@kublai.com>
parents: 10401
diff changeset
   816
        oldp, p = p, os.path.dirname(p)
d216fa04e48a mq: make init -Q do what qinit -c did
Brendan Cully <brendan@kublai.com>
parents: 10401
diff changeset
   817
        if p == oldp:
d216fa04e48a mq: make init -Q do what qinit -c did
Brendan Cully <brendan@kublai.com>
parents: 10401
diff changeset
   818
            return None
d216fa04e48a mq: make init -Q do what qinit -c did
Brendan Cully <brendan@kublai.com>
parents: 10401
diff changeset
   819
d216fa04e48a mq: make init -Q do what qinit -c did
Brendan Cully <brendan@kublai.com>
parents: 10401
diff changeset
   820
    return p
d216fa04e48a mq: make init -Q do what qinit -c did
Brendan Cully <brendan@kublai.com>
parents: 10401
diff changeset
   821
30755
0fbb3a5c188e rebase: provide detailed hint to abort message if working dir is not clean
Valters Vingolds <valters@vingolds.ch>
parents: 30724
diff changeset
   822
def bailifchanged(repo, merge=True, hint=None):
0fbb3a5c188e rebase: provide detailed hint to abort message if working dir is not clean
Valters Vingolds <valters@vingolds.ch>
parents: 30724
diff changeset
   823
    """ enforce the precondition that working directory must be clean.
0fbb3a5c188e rebase: provide detailed hint to abort message if working dir is not clean
Valters Vingolds <valters@vingolds.ch>
parents: 30724
diff changeset
   824
0fbb3a5c188e rebase: provide detailed hint to abort message if working dir is not clean
Valters Vingolds <valters@vingolds.ch>
parents: 30724
diff changeset
   825
    'merge' can be set to false if a pending uncommitted merge should be
0fbb3a5c188e rebase: provide detailed hint to abort message if working dir is not clean
Valters Vingolds <valters@vingolds.ch>
parents: 30724
diff changeset
   826
    ignored (such as when 'update --check' runs).
0fbb3a5c188e rebase: provide detailed hint to abort message if working dir is not clean
Valters Vingolds <valters@vingolds.ch>
parents: 30724
diff changeset
   827
0fbb3a5c188e rebase: provide detailed hint to abort message if working dir is not clean
Valters Vingolds <valters@vingolds.ch>
parents: 30724
diff changeset
   828
    'hint' is the usual hint given to Abort exception.
0fbb3a5c188e rebase: provide detailed hint to abort message if working dir is not clean
Valters Vingolds <valters@vingolds.ch>
parents: 30724
diff changeset
   829
    """
0fbb3a5c188e rebase: provide detailed hint to abort message if working dir is not clean
Valters Vingolds <valters@vingolds.ch>
parents: 30724
diff changeset
   830
24472
1bf71faf042e cmdutil: allow bailifchanged to ignore merging in progress
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24471
diff changeset
   831
    if merge and repo.dirstate.p2() != nullid:
30755
0fbb3a5c188e rebase: provide detailed hint to abort message if working dir is not clean
Valters Vingolds <valters@vingolds.ch>
parents: 30724
diff changeset
   832
        raise error.Abort(_('outstanding uncommitted merge'), hint=hint)
4549
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
   833
    modified, added, removed, deleted = repo.status()[:4]
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
   834
    if modified or added or removed or deleted:
30755
0fbb3a5c188e rebase: provide detailed hint to abort message if working dir is not clean
Valters Vingolds <valters@vingolds.ch>
parents: 30724
diff changeset
   835
        raise error.Abort(_('uncommitted changes'), hint=hint)
15231
cd6f10dccf16 cmdutil.bailifchanged: abort for dirty subrepos
Eric Roshan Eisner <ede@alum.mit.edu>
parents: 15214
diff changeset
   836
    ctx = repo[None]
18364
6252b4f1c4b4 subrepos: process subrepos in sorted order
Mads Kiilerich <mads@kiilerich.com>
parents: 18340
diff changeset
   837
    for s in sorted(ctx.substate):
30755
0fbb3a5c188e rebase: provide detailed hint to abort message if working dir is not clean
Valters Vingolds <valters@vingolds.ch>
parents: 30724
diff changeset
   838
        ctx.sub(s).bailifchanged(hint=hint)
4549
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
   839
14635
217b7d83afc3 cmdutil, logmessage: use ui.fin when reading from '-'
Idan Kamara <idankk86@gmail.com>
parents: 14518
diff changeset
   840
def logmessage(ui, opts):
4549
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
   841
    """ get the log message according to -m and -l option """
7667
bd5c37d792e6 cmdutil.logmessage: options should be optional
Alexander Solovyov <piranha@piranha.org.ua>
parents: 7643
diff changeset
   842
    message = opts.get('message')
bd5c37d792e6 cmdutil.logmessage: options should be optional
Alexander Solovyov <piranha@piranha.org.ua>
parents: 7643
diff changeset
   843
    logfile = opts.get('logfile')
4549
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
   844
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
   845
    if message and logfile:
26587
56b2bcea2529 error: get Abort from 'error' instead of 'util'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26579
diff changeset
   846
        raise error.Abort(_('options --message and --logfile are mutually '
4549
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
   847
                           'exclusive'))
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
   848
    if not message and logfile:
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
   849
        try:
32618
d3e1c5b4986c cmdutil: use isstdiofilename() where appropriate
Yuya Nishihara <yuya@tcha.org>
parents: 32584
diff changeset
   850
            if isstdiofilename(logfile):
14635
217b7d83afc3 cmdutil, logmessage: use ui.fin when reading from '-'
Idan Kamara <idankk86@gmail.com>
parents: 14518
diff changeset
   851
                message = ui.fin.read()
4549
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
   852
            else:
14249
f4766e1bb0b3 cmdutil: normalize log message eols when reading from file
Patrick Mezard <pmezard@gmail.com>
parents: 14232
diff changeset
   853
                message = '\n'.join(util.readfile(logfile).splitlines())
25660
328739ea70c3 global: mass rewrite to use modern exception syntax
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25657
diff changeset
   854
        except IOError as inst:
26587
56b2bcea2529 error: get Abort from 'error' instead of 'util'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26579
diff changeset
   855
            raise error.Abort(_("can't read commit message '%s': %s") %
34022
d5b2beca16c0 python3: wrap all uses of <exception>.strerror with strtolocal
Augie Fackler <raf@durin42.com>
parents: 34019
diff changeset
   856
                             (logfile, encoding.strtolocal(inst.strerror)))
4549
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
   857
    return message
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
   858
24180
d8e0c591781c spelling: fixes from proofreading of spell checker issues
Mads Kiilerich <madski@unity3d.com>
parents: 24176
diff changeset
   859
def mergeeditform(ctxorbool, baseformname):
d8e0c591781c spelling: fixes from proofreading of spell checker issues
Mads Kiilerich <madski@unity3d.com>
parents: 24176
diff changeset
   860
    """return appropriate editform name (referencing a committemplate)
d8e0c591781c spelling: fixes from proofreading of spell checker issues
Mads Kiilerich <madski@unity3d.com>
parents: 24176
diff changeset
   861
d8e0c591781c spelling: fixes from proofreading of spell checker issues
Mads Kiilerich <madski@unity3d.com>
parents: 24176
diff changeset
   862
    'ctxorbool' is either a ctx to be committed, or a bool indicating whether
22248
75618a223e18 commit: change "editform" to distinguish merge commits from others
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 22237
diff changeset
   863
    merging is committed.
75618a223e18 commit: change "editform" to distinguish merge commits from others
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 22237
diff changeset
   864
24180
d8e0c591781c spelling: fixes from proofreading of spell checker issues
Mads Kiilerich <madski@unity3d.com>
parents: 24176
diff changeset
   865
    This returns baseformname with '.merge' appended if it is a merge,
d8e0c591781c spelling: fixes from proofreading of spell checker issues
Mads Kiilerich <madski@unity3d.com>
parents: 24176
diff changeset
   866
    otherwise '.normal' is appended.
22248
75618a223e18 commit: change "editform" to distinguish merge commits from others
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 22237
diff changeset
   867
    """
75618a223e18 commit: change "editform" to distinguish merge commits from others
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 22237
diff changeset
   868
    if isinstance(ctxorbool, bool):
75618a223e18 commit: change "editform" to distinguish merge commits from others
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 22237
diff changeset
   869
        if ctxorbool:
24180
d8e0c591781c spelling: fixes from proofreading of spell checker issues
Mads Kiilerich <madski@unity3d.com>
parents: 24176
diff changeset
   870
            return baseformname + ".merge"
22248
75618a223e18 commit: change "editform" to distinguish merge commits from others
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 22237
diff changeset
   871
    elif 1 < len(ctxorbool.parents()):
24180
d8e0c591781c spelling: fixes from proofreading of spell checker issues
Mads Kiilerich <madski@unity3d.com>
parents: 24176
diff changeset
   872
        return baseformname + ".merge"
d8e0c591781c spelling: fixes from proofreading of spell checker issues
Mads Kiilerich <madski@unity3d.com>
parents: 24176
diff changeset
   873
d8e0c591781c spelling: fixes from proofreading of spell checker issues
Mads Kiilerich <madski@unity3d.com>
parents: 24176
diff changeset
   874
    return baseformname + ".normal"
22248
75618a223e18 commit: change "editform" to distinguish merge commits from others
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 22237
diff changeset
   875
21999
6ce282ed801e cmdutil: introduce 'editform' to distinguish the purpose of commit text editing
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21998
diff changeset
   876
def getcommiteditor(edit=False, finishdesc=None, extramsg=None,
6ce282ed801e cmdutil: introduce 'editform' to distinguish the purpose of commit text editing
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21998
diff changeset
   877
                    editform='', **opts):
21419
272785489ed3 cmdutil: enhance "getcommiteditor()" for specific usages in MQ
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21417
diff changeset
   878
    """get appropriate commit message editor according to '--edit' option
272785489ed3 cmdutil: enhance "getcommiteditor()" for specific usages in MQ
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21417
diff changeset
   879
272785489ed3 cmdutil: enhance "getcommiteditor()" for specific usages in MQ
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21417
diff changeset
   880
    'finishdesc' is a function to be called with edited commit message
272785489ed3 cmdutil: enhance "getcommiteditor()" for specific usages in MQ
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21417
diff changeset
   881
    (= 'description' of the new changeset) just after editing, but
272785489ed3 cmdutil: enhance "getcommiteditor()" for specific usages in MQ
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21417
diff changeset
   882
    before checking empty-ness. It should return actual text to be
272785489ed3 cmdutil: enhance "getcommiteditor()" for specific usages in MQ
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21417
diff changeset
   883
    stored into history. This allows to change description before
272785489ed3 cmdutil: enhance "getcommiteditor()" for specific usages in MQ
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21417
diff changeset
   884
    storing.
272785489ed3 cmdutil: enhance "getcommiteditor()" for specific usages in MQ
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21417
diff changeset
   885
272785489ed3 cmdutil: enhance "getcommiteditor()" for specific usages in MQ
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21417
diff changeset
   886
    'extramsg' is a extra message to be shown in the editor instead of
272785489ed3 cmdutil: enhance "getcommiteditor()" for specific usages in MQ
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21417
diff changeset
   887
    'Leave message empty to abort commit' line. 'HG: ' prefix and EOL
272785489ed3 cmdutil: enhance "getcommiteditor()" for specific usages in MQ
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21417
diff changeset
   888
    is automatically added.
272785489ed3 cmdutil: enhance "getcommiteditor()" for specific usages in MQ
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21417
diff changeset
   889
21999
6ce282ed801e cmdutil: introduce 'editform' to distinguish the purpose of commit text editing
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21998
diff changeset
   890
    'editform' is a dot-separated list of names, to distinguish
6ce282ed801e cmdutil: introduce 'editform' to distinguish the purpose of commit text editing
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21998
diff changeset
   891
    the purpose of commit text editing.
6ce282ed801e cmdutil: introduce 'editform' to distinguish the purpose of commit text editing
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21998
diff changeset
   892
21419
272785489ed3 cmdutil: enhance "getcommiteditor()" for specific usages in MQ
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21417
diff changeset
   893
    'getcommiteditor' returns 'commitforceeditor' regardless of
272785489ed3 cmdutil: enhance "getcommiteditor()" for specific usages in MQ
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21417
diff changeset
   894
    'edit', if one of 'finishdesc' or 'extramsg' is specified, because
272785489ed3 cmdutil: enhance "getcommiteditor()" for specific usages in MQ
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21417
diff changeset
   895
    they are specific for usage in MQ.
272785489ed3 cmdutil: enhance "getcommiteditor()" for specific usages in MQ
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21417
diff changeset
   896
    """
272785489ed3 cmdutil: enhance "getcommiteditor()" for specific usages in MQ
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21417
diff changeset
   897
    if edit or finishdesc or extramsg:
272785489ed3 cmdutil: enhance "getcommiteditor()" for specific usages in MQ
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21417
diff changeset
   898
        return lambda r, c, s: commitforceeditor(r, c, s,
272785489ed3 cmdutil: enhance "getcommiteditor()" for specific usages in MQ
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21417
diff changeset
   899
                                                 finishdesc=finishdesc,
21999
6ce282ed801e cmdutil: introduce 'editform' to distinguish the purpose of commit text editing
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21998
diff changeset
   900
                                                 extramsg=extramsg,
6ce282ed801e cmdutil: introduce 'editform' to distinguish the purpose of commit text editing
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21998
diff changeset
   901
                                                 editform=editform)
6ce282ed801e cmdutil: introduce 'editform' to distinguish the purpose of commit text editing
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21998
diff changeset
   902
    elif editform:
6ce282ed801e cmdutil: introduce 'editform' to distinguish the purpose of commit text editing
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21998
diff changeset
   903
        return lambda r, c, s: commiteditor(r, c, s, editform=editform)
21405
dcf20f244c2a cmdutil: introduce "getcommiteditor()" to simplify code paths to choose editor
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21241
diff changeset
   904
    else:
dcf20f244c2a cmdutil: introduce "getcommiteditor()" to simplify code paths to choose editor
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21241
diff changeset
   905
        return commiteditor
dcf20f244c2a cmdutil: introduce "getcommiteditor()" to simplify code paths to choose editor
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21241
diff changeset
   906
37774
d6970628b95f fix: use templater to substitute values in command string
Yuya Nishihara <yuya@tcha.org>
parents: 37763
diff changeset
   907
def _escapecommandtemplate(tmpl):
d6970628b95f fix: use templater to substitute values in command string
Yuya Nishihara <yuya@tcha.org>
parents: 37763
diff changeset
   908
    parts = []
d6970628b95f fix: use templater to substitute values in command string
Yuya Nishihara <yuya@tcha.org>
parents: 37763
diff changeset
   909
    for typ, start, end in templater.scantemplate(tmpl, raw=True):
d6970628b95f fix: use templater to substitute values in command string
Yuya Nishihara <yuya@tcha.org>
parents: 37763
diff changeset
   910
        if typ == b'string':
d6970628b95f fix: use templater to substitute values in command string
Yuya Nishihara <yuya@tcha.org>
parents: 37763
diff changeset
   911
            parts.append(stringutil.escapestr(tmpl[start:end]))
d6970628b95f fix: use templater to substitute values in command string
Yuya Nishihara <yuya@tcha.org>
parents: 37763
diff changeset
   912
        else:
d6970628b95f fix: use templater to substitute values in command string
Yuya Nishihara <yuya@tcha.org>
parents: 37763
diff changeset
   913
            parts.append(tmpl[start:end])
d6970628b95f fix: use templater to substitute values in command string
Yuya Nishihara <yuya@tcha.org>
parents: 37763
diff changeset
   914
    return b''.join(parts)
d6970628b95f fix: use templater to substitute values in command string
Yuya Nishihara <yuya@tcha.org>
parents: 37763
diff changeset
   915
d6970628b95f fix: use templater to substitute values in command string
Yuya Nishihara <yuya@tcha.org>
parents: 37763
diff changeset
   916
def rendercommandtemplate(ui, tmpl, props):
d6970628b95f fix: use templater to substitute values in command string
Yuya Nishihara <yuya@tcha.org>
parents: 37763
diff changeset
   917
    r"""Expand a literal template 'tmpl' in a way suitable for command line
d6970628b95f fix: use templater to substitute values in command string
Yuya Nishihara <yuya@tcha.org>
parents: 37763
diff changeset
   918
d6970628b95f fix: use templater to substitute values in command string
Yuya Nishihara <yuya@tcha.org>
parents: 37763
diff changeset
   919
    '\' in outermost string is not taken as an escape character because it
d6970628b95f fix: use templater to substitute values in command string
Yuya Nishihara <yuya@tcha.org>
parents: 37763
diff changeset
   920
    is a directory separator on Windows.
d6970628b95f fix: use templater to substitute values in command string
Yuya Nishihara <yuya@tcha.org>
parents: 37763
diff changeset
   921
d6970628b95f fix: use templater to substitute values in command string
Yuya Nishihara <yuya@tcha.org>
parents: 37763
diff changeset
   922
    >>> from . import ui as uimod
d6970628b95f fix: use templater to substitute values in command string
Yuya Nishihara <yuya@tcha.org>
parents: 37763
diff changeset
   923
    >>> ui = uimod.ui()
d6970628b95f fix: use templater to substitute values in command string
Yuya Nishihara <yuya@tcha.org>
parents: 37763
diff changeset
   924
    >>> rendercommandtemplate(ui, b'c:\\{path}', {b'path': b'foo'})
d6970628b95f fix: use templater to substitute values in command string
Yuya Nishihara <yuya@tcha.org>
parents: 37763
diff changeset
   925
    'c:\\foo'
d6970628b95f fix: use templater to substitute values in command string
Yuya Nishihara <yuya@tcha.org>
parents: 37763
diff changeset
   926
    >>> rendercommandtemplate(ui, b'{"c:\\{path}"}', {'path': b'foo'})
d6970628b95f fix: use templater to substitute values in command string
Yuya Nishihara <yuya@tcha.org>
parents: 37763
diff changeset
   927
    'c:{path}'
d6970628b95f fix: use templater to substitute values in command string
Yuya Nishihara <yuya@tcha.org>
parents: 37763
diff changeset
   928
    """
d6970628b95f fix: use templater to substitute values in command string
Yuya Nishihara <yuya@tcha.org>
parents: 37763
diff changeset
   929
    if not tmpl:
d6970628b95f fix: use templater to substitute values in command string
Yuya Nishihara <yuya@tcha.org>
parents: 37763
diff changeset
   930
        return tmpl
d6970628b95f fix: use templater to substitute values in command string
Yuya Nishihara <yuya@tcha.org>
parents: 37763
diff changeset
   931
    t = formatter.maketemplater(ui, _escapecommandtemplate(tmpl))
d6970628b95f fix: use templater to substitute values in command string
Yuya Nishihara <yuya@tcha.org>
parents: 37763
diff changeset
   932
    return t.renderdefault(props)
d6970628b95f fix: use templater to substitute values in command string
Yuya Nishihara <yuya@tcha.org>
parents: 37763
diff changeset
   933
36511
aa3294027936 cmdutil: expand filename format string by templater (BC)
Yuya Nishihara <yuya@tcha.org>
parents: 36508
diff changeset
   934
def rendertemplate(ctx, tmpl, props=None):
aa3294027936 cmdutil: expand filename format string by templater (BC)
Yuya Nishihara <yuya@tcha.org>
parents: 36508
diff changeset
   935
    """Expand a literal template 'tmpl' byte-string against one changeset
aa3294027936 cmdutil: expand filename format string by templater (BC)
Yuya Nishihara <yuya@tcha.org>
parents: 36508
diff changeset
   936
aa3294027936 cmdutil: expand filename format string by templater (BC)
Yuya Nishihara <yuya@tcha.org>
parents: 36508
diff changeset
   937
    Each props item must be a stringify-able value or a callable returning
aa3294027936 cmdutil: expand filename format string by templater (BC)
Yuya Nishihara <yuya@tcha.org>
parents: 36508
diff changeset
   938
    such value, i.e. no bare list nor dict should be passed.
aa3294027936 cmdutil: expand filename format string by templater (BC)
Yuya Nishihara <yuya@tcha.org>
parents: 36508
diff changeset
   939
    """
aa3294027936 cmdutil: expand filename format string by templater (BC)
Yuya Nishihara <yuya@tcha.org>
parents: 36508
diff changeset
   940
    repo = ctx.repo()
aa3294027936 cmdutil: expand filename format string by templater (BC)
Yuya Nishihara <yuya@tcha.org>
parents: 36508
diff changeset
   941
    tres = formatter.templateresources(repo.ui, repo)
aa3294027936 cmdutil: expand filename format string by templater (BC)
Yuya Nishihara <yuya@tcha.org>
parents: 36508
diff changeset
   942
    t = formatter.maketemplater(repo.ui, tmpl, defaults=templatekw.keywords,
aa3294027936 cmdutil: expand filename format string by templater (BC)
Yuya Nishihara <yuya@tcha.org>
parents: 36508
diff changeset
   943
                                resources=tres)
37103
be3f33f5e232 templater: switch 'revcache' based on new mapping items
Yuya Nishihara <yuya@tcha.org>
parents: 37088
diff changeset
   944
    mapping = {'ctx': ctx}
36511
aa3294027936 cmdutil: expand filename format string by templater (BC)
Yuya Nishihara <yuya@tcha.org>
parents: 36508
diff changeset
   945
    if props:
aa3294027936 cmdutil: expand filename format string by templater (BC)
Yuya Nishihara <yuya@tcha.org>
parents: 36508
diff changeset
   946
        mapping.update(props)
36988
317382151ac3 templater: rename .render(mapping) to .renderdefault(mapping) (API)
Yuya Nishihara <yuya@tcha.org>
parents: 36986
diff changeset
   947
    return t.renderdefault(mapping)
36511
aa3294027936 cmdutil: expand filename format string by templater (BC)
Yuya Nishihara <yuya@tcha.org>
parents: 36508
diff changeset
   948
aa3294027936 cmdutil: expand filename format string by templater (BC)
Yuya Nishihara <yuya@tcha.org>
parents: 36508
diff changeset
   949
def _buildfntemplate(pat, total=None, seqno=None, revwidth=None, pathname=None):
aa3294027936 cmdutil: expand filename format string by templater (BC)
Yuya Nishihara <yuya@tcha.org>
parents: 36508
diff changeset
   950
    r"""Convert old-style filename format string to template string
aa3294027936 cmdutil: expand filename format string by templater (BC)
Yuya Nishihara <yuya@tcha.org>
parents: 36508
diff changeset
   951
aa3294027936 cmdutil: expand filename format string by templater (BC)
Yuya Nishihara <yuya@tcha.org>
parents: 36508
diff changeset
   952
    >>> _buildfntemplate(b'foo-%b-%n.patch', seqno=0)
aa3294027936 cmdutil: expand filename format string by templater (BC)
Yuya Nishihara <yuya@tcha.org>
parents: 36508
diff changeset
   953
    'foo-{reporoot|basename}-{seqno}.patch'
aa3294027936 cmdutil: expand filename format string by templater (BC)
Yuya Nishihara <yuya@tcha.org>
parents: 36508
diff changeset
   954
    >>> _buildfntemplate(b'%R{tags % "{tag}"}%H')
aa3294027936 cmdutil: expand filename format string by templater (BC)
Yuya Nishihara <yuya@tcha.org>
parents: 36508
diff changeset
   955
    '{rev}{tags % "{tag}"}{node}'
aa3294027936 cmdutil: expand filename format string by templater (BC)
Yuya Nishihara <yuya@tcha.org>
parents: 36508
diff changeset
   956
aa3294027936 cmdutil: expand filename format string by templater (BC)
Yuya Nishihara <yuya@tcha.org>
parents: 36508
diff changeset
   957
    '\' in outermost strings has to be escaped because it is a directory
aa3294027936 cmdutil: expand filename format string by templater (BC)
Yuya Nishihara <yuya@tcha.org>
parents: 36508
diff changeset
   958
    separator on Windows:
aa3294027936 cmdutil: expand filename format string by templater (BC)
Yuya Nishihara <yuya@tcha.org>
parents: 36508
diff changeset
   959
aa3294027936 cmdutil: expand filename format string by templater (BC)
Yuya Nishihara <yuya@tcha.org>
parents: 36508
diff changeset
   960
    >>> _buildfntemplate(b'c:\\tmp\\%R\\%n.patch', seqno=0)
aa3294027936 cmdutil: expand filename format string by templater (BC)
Yuya Nishihara <yuya@tcha.org>
parents: 36508
diff changeset
   961
    'c:\\\\tmp\\\\{rev}\\\\{seqno}.patch'
aa3294027936 cmdutil: expand filename format string by templater (BC)
Yuya Nishihara <yuya@tcha.org>
parents: 36508
diff changeset
   962
    >>> _buildfntemplate(b'\\\\foo\\bar.patch')
aa3294027936 cmdutil: expand filename format string by templater (BC)
Yuya Nishihara <yuya@tcha.org>
parents: 36508
diff changeset
   963
    '\\\\\\\\foo\\\\bar.patch'
aa3294027936 cmdutil: expand filename format string by templater (BC)
Yuya Nishihara <yuya@tcha.org>
parents: 36508
diff changeset
   964
    >>> _buildfntemplate(b'\\{tags % "{tag}"}')
aa3294027936 cmdutil: expand filename format string by templater (BC)
Yuya Nishihara <yuya@tcha.org>
parents: 36508
diff changeset
   965
    '\\\\{tags % "{tag}"}'
aa3294027936 cmdutil: expand filename format string by templater (BC)
Yuya Nishihara <yuya@tcha.org>
parents: 36508
diff changeset
   966
aa3294027936 cmdutil: expand filename format string by templater (BC)
Yuya Nishihara <yuya@tcha.org>
parents: 36508
diff changeset
   967
    but inner strings follow the template rules (i.e. '\' is taken as an
aa3294027936 cmdutil: expand filename format string by templater (BC)
Yuya Nishihara <yuya@tcha.org>
parents: 36508
diff changeset
   968
    escape character):
aa3294027936 cmdutil: expand filename format string by templater (BC)
Yuya Nishihara <yuya@tcha.org>
parents: 36508
diff changeset
   969
aa3294027936 cmdutil: expand filename format string by templater (BC)
Yuya Nishihara <yuya@tcha.org>
parents: 36508
diff changeset
   970
    >>> _buildfntemplate(br'{"c:\tmp"}', seqno=0)
aa3294027936 cmdutil: expand filename format string by templater (BC)
Yuya Nishihara <yuya@tcha.org>
parents: 36508
diff changeset
   971
    '{"c:\\tmp"}'
aa3294027936 cmdutil: expand filename format string by templater (BC)
Yuya Nishihara <yuya@tcha.org>
parents: 36508
diff changeset
   972
    """
2874
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   973
    expander = {
36511
aa3294027936 cmdutil: expand filename format string by templater (BC)
Yuya Nishihara <yuya@tcha.org>
parents: 36508
diff changeset
   974
        b'H': b'{node}',
aa3294027936 cmdutil: expand filename format string by templater (BC)
Yuya Nishihara <yuya@tcha.org>
parents: 36508
diff changeset
   975
        b'R': b'{rev}',
aa3294027936 cmdutil: expand filename format string by templater (BC)
Yuya Nishihara <yuya@tcha.org>
parents: 36508
diff changeset
   976
        b'h': b'{node|short}',
aa3294027936 cmdutil: expand filename format string by templater (BC)
Yuya Nishihara <yuya@tcha.org>
parents: 36508
diff changeset
   977
        b'm': br'{sub(r"[^\w]", "_", desc|firstline)}',
aa3294027936 cmdutil: expand filename format string by templater (BC)
Yuya Nishihara <yuya@tcha.org>
parents: 36508
diff changeset
   978
        b'r': b'{if(revwidth, pad(rev, revwidth, "0", left=True), rev)}',
aa3294027936 cmdutil: expand filename format string by templater (BC)
Yuya Nishihara <yuya@tcha.org>
parents: 36508
diff changeset
   979
        b'%': b'%',
aa3294027936 cmdutil: expand filename format string by templater (BC)
Yuya Nishihara <yuya@tcha.org>
parents: 36508
diff changeset
   980
        b'b': b'{reporoot|basename}',
aa3294027936 cmdutil: expand filename format string by templater (BC)
Yuya Nishihara <yuya@tcha.org>
parents: 36508
diff changeset
   981
    }
36239
428de1a59f2d cmdutil: narrow scope of KeyError in makefilename()
Yuya Nishihara <yuya@tcha.org>
parents: 36207
diff changeset
   982
    if total is not None:
36511
aa3294027936 cmdutil: expand filename format string by templater (BC)
Yuya Nishihara <yuya@tcha.org>
parents: 36508
diff changeset
   983
        expander[b'N'] = b'{total}'
36239
428de1a59f2d cmdutil: narrow scope of KeyError in makefilename()
Yuya Nishihara <yuya@tcha.org>
parents: 36207
diff changeset
   984
    if seqno is not None:
36511
aa3294027936 cmdutil: expand filename format string by templater (BC)
Yuya Nishihara <yuya@tcha.org>
parents: 36508
diff changeset
   985
        expander[b'n'] = b'{seqno}'
36239
428de1a59f2d cmdutil: narrow scope of KeyError in makefilename()
Yuya Nishihara <yuya@tcha.org>
parents: 36207
diff changeset
   986
    if total is not None and seqno is not None:
36511
aa3294027936 cmdutil: expand filename format string by templater (BC)
Yuya Nishihara <yuya@tcha.org>
parents: 36508
diff changeset
   987
        expander[b'n'] = b'{pad(seqno, total|stringify|count, "0", left=True)}'
36239
428de1a59f2d cmdutil: narrow scope of KeyError in makefilename()
Yuya Nishihara <yuya@tcha.org>
parents: 36207
diff changeset
   988
    if pathname is not None:
36511
aa3294027936 cmdutil: expand filename format string by templater (BC)
Yuya Nishihara <yuya@tcha.org>
parents: 36508
diff changeset
   989
        expander[b's'] = b'{pathname|basename}'
aa3294027936 cmdutil: expand filename format string by templater (BC)
Yuya Nishihara <yuya@tcha.org>
parents: 36508
diff changeset
   990
        expander[b'd'] = b'{if(pathname|dirname, pathname|dirname, ".")}'
aa3294027936 cmdutil: expand filename format string by templater (BC)
Yuya Nishihara <yuya@tcha.org>
parents: 36508
diff changeset
   991
        expander[b'p'] = b'{pathname}'
36239
428de1a59f2d cmdutil: narrow scope of KeyError in makefilename()
Yuya Nishihara <yuya@tcha.org>
parents: 36207
diff changeset
   992
428de1a59f2d cmdutil: narrow scope of KeyError in makefilename()
Yuya Nishihara <yuya@tcha.org>
parents: 36207
diff changeset
   993
    newname = []
36511
aa3294027936 cmdutil: expand filename format string by templater (BC)
Yuya Nishihara <yuya@tcha.org>
parents: 36508
diff changeset
   994
    for typ, start, end in templater.scantemplate(pat, raw=True):
aa3294027936 cmdutil: expand filename format string by templater (BC)
Yuya Nishihara <yuya@tcha.org>
parents: 36508
diff changeset
   995
        if typ != b'string':
aa3294027936 cmdutil: expand filename format string by templater (BC)
Yuya Nishihara <yuya@tcha.org>
parents: 36508
diff changeset
   996
            newname.append(pat[start:end])
aa3294027936 cmdutil: expand filename format string by templater (BC)
Yuya Nishihara <yuya@tcha.org>
parents: 36508
diff changeset
   997
            continue
aa3294027936 cmdutil: expand filename format string by templater (BC)
Yuya Nishihara <yuya@tcha.org>
parents: 36508
diff changeset
   998
        i = start
aa3294027936 cmdutil: expand filename format string by templater (BC)
Yuya Nishihara <yuya@tcha.org>
parents: 36508
diff changeset
   999
        while i < end:
aa3294027936 cmdutil: expand filename format string by templater (BC)
Yuya Nishihara <yuya@tcha.org>
parents: 36508
diff changeset
  1000
            n = pat.find(b'%', i, end)
aa3294027936 cmdutil: expand filename format string by templater (BC)
Yuya Nishihara <yuya@tcha.org>
parents: 36508
diff changeset
  1001
            if n < 0:
37084
f0b6fbea00cf stringutil: bulk-replace call sites to point to new module
Yuya Nishihara <yuya@tcha.org>
parents: 36988
diff changeset
  1002
                newname.append(stringutil.escapestr(pat[i:end]))
36511
aa3294027936 cmdutil: expand filename format string by templater (BC)
Yuya Nishihara <yuya@tcha.org>
parents: 36508
diff changeset
  1003
                break
37084
f0b6fbea00cf stringutil: bulk-replace call sites to point to new module
Yuya Nishihara <yuya@tcha.org>
parents: 36988
diff changeset
  1004
            newname.append(stringutil.escapestr(pat[i:n]))
36511
aa3294027936 cmdutil: expand filename format string by templater (BC)
Yuya Nishihara <yuya@tcha.org>
parents: 36508
diff changeset
  1005
            if n + 2 > end:
aa3294027936 cmdutil: expand filename format string by templater (BC)
Yuya Nishihara <yuya@tcha.org>
parents: 36508
diff changeset
  1006
                raise error.Abort(_("incomplete format spec in output "
aa3294027936 cmdutil: expand filename format string by templater (BC)
Yuya Nishihara <yuya@tcha.org>
parents: 36508
diff changeset
  1007
                                    "filename"))
aa3294027936 cmdutil: expand filename format string by templater (BC)
Yuya Nishihara <yuya@tcha.org>
parents: 36508
diff changeset
  1008
            c = pat[n + 1:n + 2]
aa3294027936 cmdutil: expand filename format string by templater (BC)
Yuya Nishihara <yuya@tcha.org>
parents: 36508
diff changeset
  1009
            i = n + 2
36239
428de1a59f2d cmdutil: narrow scope of KeyError in makefilename()
Yuya Nishihara <yuya@tcha.org>
parents: 36207
diff changeset
  1010
            try:
36511
aa3294027936 cmdutil: expand filename format string by templater (BC)
Yuya Nishihara <yuya@tcha.org>
parents: 36508
diff changeset
  1011
                newname.append(expander[c])
36239
428de1a59f2d cmdutil: narrow scope of KeyError in makefilename()
Yuya Nishihara <yuya@tcha.org>
parents: 36207
diff changeset
  1012
            except KeyError:
428de1a59f2d cmdutil: narrow scope of KeyError in makefilename()
Yuya Nishihara <yuya@tcha.org>
parents: 36207
diff changeset
  1013
                raise error.Abort(_("invalid format spec '%%%s' in output "
428de1a59f2d cmdutil: narrow scope of KeyError in makefilename()
Yuya Nishihara <yuya@tcha.org>
parents: 36207
diff changeset
  1014
                                    "filename") % c)
428de1a59f2d cmdutil: narrow scope of KeyError in makefilename()
Yuya Nishihara <yuya@tcha.org>
parents: 36207
diff changeset
  1015
    return ''.join(newname)
2874
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
  1016
36511
aa3294027936 cmdutil: expand filename format string by templater (BC)
Yuya Nishihara <yuya@tcha.org>
parents: 36508
diff changeset
  1017
def makefilename(ctx, pat, **props):
aa3294027936 cmdutil: expand filename format string by templater (BC)
Yuya Nishihara <yuya@tcha.org>
parents: 36508
diff changeset
  1018
    if not pat:
aa3294027936 cmdutil: expand filename format string by templater (BC)
Yuya Nishihara <yuya@tcha.org>
parents: 36508
diff changeset
  1019
        return pat
aa3294027936 cmdutil: expand filename format string by templater (BC)
Yuya Nishihara <yuya@tcha.org>
parents: 36508
diff changeset
  1020
    tmpl = _buildfntemplate(pat, **props)
aa3294027936 cmdutil: expand filename format string by templater (BC)
Yuya Nishihara <yuya@tcha.org>
parents: 36508
diff changeset
  1021
    # BUG: alias expansion shouldn't be made against template fragments
aa3294027936 cmdutil: expand filename format string by templater (BC)
Yuya Nishihara <yuya@tcha.org>
parents: 36508
diff changeset
  1022
    # rewritten from %-format strings, but we have no easy way to partially
aa3294027936 cmdutil: expand filename format string by templater (BC)
Yuya Nishihara <yuya@tcha.org>
parents: 36508
diff changeset
  1023
    # disable the expansion.
aa3294027936 cmdutil: expand filename format string by templater (BC)
Yuya Nishihara <yuya@tcha.org>
parents: 36508
diff changeset
  1024
    return rendertemplate(ctx, tmpl, pycompat.byteskwargs(props))
2874
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
  1025
32539
447bbd970047 cmdutil: extract function checking if pattern should be taken as stdin/out
Yuya Nishihara <yuya@tcha.org>
parents: 32538
diff changeset
  1026
def isstdiofilename(pat):
447bbd970047 cmdutil: extract function checking if pattern should be taken as stdin/out
Yuya Nishihara <yuya@tcha.org>
parents: 32538
diff changeset
  1027
    """True if the given pat looks like a filename denoting stdin/stdout"""
447bbd970047 cmdutil: extract function checking if pattern should be taken as stdin/out
Yuya Nishihara <yuya@tcha.org>
parents: 32538
diff changeset
  1028
    return not pat or pat == '-'
447bbd970047 cmdutil: extract function checking if pattern should be taken as stdin/out
Yuya Nishihara <yuya@tcha.org>
parents: 32538
diff changeset
  1029
27418
2ce4661ac226 cmdutil: reimplement file wrapper that disables close()
Yuya Nishihara <yuya@tcha.org>
parents: 27417
diff changeset
  1030
class _unclosablefile(object):
2ce4661ac226 cmdutil: reimplement file wrapper that disables close()
Yuya Nishihara <yuya@tcha.org>
parents: 27417
diff changeset
  1031
    def __init__(self, fp):
2ce4661ac226 cmdutil: reimplement file wrapper that disables close()
Yuya Nishihara <yuya@tcha.org>
parents: 27417
diff changeset
  1032
        self._fp = fp
2ce4661ac226 cmdutil: reimplement file wrapper that disables close()
Yuya Nishihara <yuya@tcha.org>
parents: 27417
diff changeset
  1033
2ce4661ac226 cmdutil: reimplement file wrapper that disables close()
Yuya Nishihara <yuya@tcha.org>
parents: 27417
diff changeset
  1034
    def close(self):
2ce4661ac226 cmdutil: reimplement file wrapper that disables close()
Yuya Nishihara <yuya@tcha.org>
parents: 27417
diff changeset
  1035
        pass
2ce4661ac226 cmdutil: reimplement file wrapper that disables close()
Yuya Nishihara <yuya@tcha.org>
parents: 27417
diff changeset
  1036
2ce4661ac226 cmdutil: reimplement file wrapper that disables close()
Yuya Nishihara <yuya@tcha.org>
parents: 27417
diff changeset
  1037
    def __iter__(self):
2ce4661ac226 cmdutil: reimplement file wrapper that disables close()
Yuya Nishihara <yuya@tcha.org>
parents: 27417
diff changeset
  1038
        return iter(self._fp)
2ce4661ac226 cmdutil: reimplement file wrapper that disables close()
Yuya Nishihara <yuya@tcha.org>
parents: 27417
diff changeset
  1039
2ce4661ac226 cmdutil: reimplement file wrapper that disables close()
Yuya Nishihara <yuya@tcha.org>
parents: 27417
diff changeset
  1040
    def __getattr__(self, attr):
2ce4661ac226 cmdutil: reimplement file wrapper that disables close()
Yuya Nishihara <yuya@tcha.org>
parents: 27417
diff changeset
  1041
        return getattr(self._fp, attr)
2ce4661ac226 cmdutil: reimplement file wrapper that disables close()
Yuya Nishihara <yuya@tcha.org>
parents: 27417
diff changeset
  1042
30142
3dcaf1c4e90d largefiles: use context for file closing
Mads Kiilerich <madski@unity3d.com>
parents: 30016
diff changeset
  1043
    def __enter__(self):
3dcaf1c4e90d largefiles: use context for file closing
Mads Kiilerich <madski@unity3d.com>
parents: 30016
diff changeset
  1044
        return self
3dcaf1c4e90d largefiles: use context for file closing
Mads Kiilerich <madski@unity3d.com>
parents: 30016
diff changeset
  1045
3dcaf1c4e90d largefiles: use context for file closing
Mads Kiilerich <madski@unity3d.com>
parents: 30016
diff changeset
  1046
    def __exit__(self, exc_type, exc_value, exc_tb):
3dcaf1c4e90d largefiles: use context for file closing
Mads Kiilerich <madski@unity3d.com>
parents: 30016
diff changeset
  1047
        pass
3dcaf1c4e90d largefiles: use context for file closing
Mads Kiilerich <madski@unity3d.com>
parents: 30016
diff changeset
  1048
37601
2e0e61312a25 export: serialize revisions to be exported per destination file
Yuya Nishihara <yuya@tcha.org>
parents: 37600
diff changeset
  1049
def makefileobj(ctx, pat, mode='wb', **props):
13769
8796fb6af67e cmdutil: fix mode handling in make_file
Adrian Buehlmann <adrian@cadifra.com>
parents: 13534
diff changeset
  1050
    writable = mode not in ('r', 'rb')
7319
eae1767cc6a8 export: fixed silent output file overwriting
Ronny Pfannschmidt <Ronny.Pfannschmidt@gmx.de>
parents: 7308
diff changeset
  1051
32539
447bbd970047 cmdutil: extract function checking if pattern should be taken as stdin/out
Yuya Nishihara <yuya@tcha.org>
parents: 32538
diff changeset
  1052
    if isstdiofilename(pat):
36205
976e1cfb2f64 cmdutil: pass ctx to makefileobj() in place of repo/node pair (API)
Yuya Nishihara <yuya@tcha.org>
parents: 36204
diff changeset
  1053
        repo = ctx.repo()
24306
6ddc86eedc3b style: kill ersatz if-else ternary operators
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 24301
diff changeset
  1054
        if writable:
6ddc86eedc3b style: kill ersatz if-else ternary operators
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 24301
diff changeset
  1055
            fp = repo.ui.fout
6ddc86eedc3b style: kill ersatz if-else ternary operators
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 24301
diff changeset
  1056
        else:
6ddc86eedc3b style: kill ersatz if-else ternary operators
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 24301
diff changeset
  1057
            fp = repo.ui.fin
27419
7e2495bf0ad8 cmdutil: do not duplicate stdout by makefileobj()
Yuya Nishihara <yuya@tcha.org>
parents: 27418
diff changeset
  1058
        return _unclosablefile(fp)
36508
d7a23d6184a2 cmdutil: reorder optional arguments passed to makefileobj()
Yuya Nishihara <yuya@tcha.org>
parents: 36507
diff changeset
  1059
    fn = makefilename(ctx, pat, **props)
18613
1a2f4c633410 export: clobber files with -o (bc) (issue3652)
Augie Fackler <raf@durin42.com>
parents: 18538
diff changeset
  1060
    return open(fn, mode)
2882
cf98cd70d2c4 move walk and matchpats from commands to cmdutil.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2874
diff changeset
  1061
14323
a79fea6b3e77 debugindex etc.: add --changelog and --manifest options
Sune Foldager <cryo@cyanite.org>
parents: 14322
diff changeset
  1062
def openrevlog(repo, cmd, file_, opts):
a79fea6b3e77 debugindex etc.: add --changelog and --manifest options
Sune Foldager <cryo@cyanite.org>
parents: 14322
diff changeset
  1063
    """opens the changelog, manifest, a filelog or a given revlog"""
a79fea6b3e77 debugindex etc.: add --changelog and --manifest options
Sune Foldager <cryo@cyanite.org>
parents: 14322
diff changeset
  1064
    cl = opts['changelog']
a79fea6b3e77 debugindex etc.: add --changelog and --manifest options
Sune Foldager <cryo@cyanite.org>
parents: 14322
diff changeset
  1065
    mf = opts['manifest']
25119
49c583ca48c4 treemanifest: add --dir option to debug{revlog,data,index}
Martin von Zweigbergk <martinvonz@google.com>
parents: 25100
diff changeset
  1066
    dir = opts['dir']
14323
a79fea6b3e77 debugindex etc.: add --changelog and --manifest options
Sune Foldager <cryo@cyanite.org>
parents: 14322
diff changeset
  1067
    msg = None
a79fea6b3e77 debugindex etc.: add --changelog and --manifest options
Sune Foldager <cryo@cyanite.org>
parents: 14322
diff changeset
  1068
    if cl and mf:
a79fea6b3e77 debugindex etc.: add --changelog and --manifest options
Sune Foldager <cryo@cyanite.org>
parents: 14322
diff changeset
  1069
        msg = _('cannot specify --changelog and --manifest at the same time')
25119
49c583ca48c4 treemanifest: add --dir option to debug{revlog,data,index}
Martin von Zweigbergk <martinvonz@google.com>
parents: 25100
diff changeset
  1070
    elif cl and dir:
49c583ca48c4 treemanifest: add --dir option to debug{revlog,data,index}
Martin von Zweigbergk <martinvonz@google.com>
parents: 25100
diff changeset
  1071
        msg = _('cannot specify --changelog and --dir at the same time')
29427
33a6b750b5b9 debug: make debug{revlog,index,data} --dir not just a flag
Martin von Zweigbergk <martinvonz@google.com>
parents: 29397
diff changeset
  1072
    elif cl or mf or dir:
14323
a79fea6b3e77 debugindex etc.: add --changelog and --manifest options
Sune Foldager <cryo@cyanite.org>
parents: 14322
diff changeset
  1073
        if file_:
a79fea6b3e77 debugindex etc.: add --changelog and --manifest options
Sune Foldager <cryo@cyanite.org>
parents: 14322
diff changeset
  1074
            msg = _('cannot specify filename with --changelog or --manifest')
a79fea6b3e77 debugindex etc.: add --changelog and --manifest options
Sune Foldager <cryo@cyanite.org>
parents: 14322
diff changeset
  1075
        elif not repo:
25119
49c583ca48c4 treemanifest: add --dir option to debug{revlog,data,index}
Martin von Zweigbergk <martinvonz@google.com>
parents: 25100
diff changeset
  1076
            msg = _('cannot specify --changelog or --manifest or --dir '
14323
a79fea6b3e77 debugindex etc.: add --changelog and --manifest options
Sune Foldager <cryo@cyanite.org>
parents: 14322
diff changeset
  1077
                    'without a repository')
a79fea6b3e77 debugindex etc.: add --changelog and --manifest options
Sune Foldager <cryo@cyanite.org>
parents: 14322
diff changeset
  1078
    if msg:
26587
56b2bcea2529 error: get Abort from 'error' instead of 'util'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26579
diff changeset
  1079
        raise error.Abort(msg)
14323
a79fea6b3e77 debugindex etc.: add --changelog and --manifest options
Sune Foldager <cryo@cyanite.org>
parents: 14322
diff changeset
  1080
a79fea6b3e77 debugindex etc.: add --changelog and --manifest options
Sune Foldager <cryo@cyanite.org>
parents: 14322
diff changeset
  1081
    r = None
a79fea6b3e77 debugindex etc.: add --changelog and --manifest options
Sune Foldager <cryo@cyanite.org>
parents: 14322
diff changeset
  1082
    if repo:
a79fea6b3e77 debugindex etc.: add --changelog and --manifest options
Sune Foldager <cryo@cyanite.org>
parents: 14322
diff changeset
  1083
        if cl:
21033
254f55b64e31 debugrevlog: use unfiltered view for changelog
Matt Mackall <mpm@selenic.com>
parents: 21024
diff changeset
  1084
            r = repo.unfiltered().changelog
25119
49c583ca48c4 treemanifest: add --dir option to debug{revlog,data,index}
Martin von Zweigbergk <martinvonz@google.com>
parents: 25100
diff changeset
  1085
        elif dir:
49c583ca48c4 treemanifest: add --dir option to debug{revlog,data,index}
Martin von Zweigbergk <martinvonz@google.com>
parents: 25100
diff changeset
  1086
            if 'treemanifest' not in repo.requirements:
26587
56b2bcea2529 error: get Abort from 'error' instead of 'util'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26579
diff changeset
  1087
                raise error.Abort(_("--dir can only be used on repos with "
25119
49c583ca48c4 treemanifest: add --dir option to debug{revlog,data,index}
Martin von Zweigbergk <martinvonz@google.com>
parents: 25100
diff changeset
  1088
                                   "treemanifest enabled"))
37270
6ff8bd691fb8 narrow: add trailing slash to dir earlier for debug{revlog,index,data}
Martin von Zweigbergk <martinvonz@google.com>
parents: 37150
diff changeset
  1089
            if not dir.endswith('/'):
6ff8bd691fb8 narrow: add trailing slash to dir earlier for debug{revlog,index,data}
Martin von Zweigbergk <martinvonz@google.com>
parents: 37150
diff changeset
  1090
                dir = dir + '/'
30371
fccc3eea2ddb manifest: delete unused dirlog and _newmanifest functions
Durham Goode <durham@fb.com>
parents: 30340
diff changeset
  1091
            dirlog = repo.manifestlog._revlog.dirlog(dir)
25119
49c583ca48c4 treemanifest: add --dir option to debug{revlog,data,index}
Martin von Zweigbergk <martinvonz@google.com>
parents: 25100
diff changeset
  1092
            if len(dirlog):
49c583ca48c4 treemanifest: add --dir option to debug{revlog,data,index}
Martin von Zweigbergk <martinvonz@google.com>
parents: 25100
diff changeset
  1093
                r = dirlog
14323
a79fea6b3e77 debugindex etc.: add --changelog and --manifest options
Sune Foldager <cryo@cyanite.org>
parents: 14322
diff changeset
  1094
        elif mf:
30375
11b8b740d54a manifest: remove last uses of repo.manifest
Durham Goode <durham@fb.com>
parents: 30371
diff changeset
  1095
            r = repo.manifestlog._revlog
14323
a79fea6b3e77 debugindex etc.: add --changelog and --manifest options
Sune Foldager <cryo@cyanite.org>
parents: 14322
diff changeset
  1096
        elif file_:
a79fea6b3e77 debugindex etc.: add --changelog and --manifest options
Sune Foldager <cryo@cyanite.org>
parents: 14322
diff changeset
  1097
            filelog = repo.file(file_)
a79fea6b3e77 debugindex etc.: add --changelog and --manifest options
Sune Foldager <cryo@cyanite.org>
parents: 14322
diff changeset
  1098
            if len(filelog):
a79fea6b3e77 debugindex etc.: add --changelog and --manifest options
Sune Foldager <cryo@cyanite.org>
parents: 14322
diff changeset
  1099
                r = filelog
a79fea6b3e77 debugindex etc.: add --changelog and --manifest options
Sune Foldager <cryo@cyanite.org>
parents: 14322
diff changeset
  1100
    if not r:
a79fea6b3e77 debugindex etc.: add --changelog and --manifest options
Sune Foldager <cryo@cyanite.org>
parents: 14322
diff changeset
  1101
        if not file_:
a79fea6b3e77 debugindex etc.: add --changelog and --manifest options
Sune Foldager <cryo@cyanite.org>
parents: 14322
diff changeset
  1102
            raise error.CommandError(cmd, _('invalid arguments'))
a79fea6b3e77 debugindex etc.: add --changelog and --manifest options
Sune Foldager <cryo@cyanite.org>
parents: 14322
diff changeset
  1103
        if not os.path.isfile(file_):
26587
56b2bcea2529 error: get Abort from 'error' instead of 'util'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26579
diff changeset
  1104
            raise error.Abort(_("revlog '%s' not found") % file_)
31237
1b08aca7870a vfs: use 'vfs' module directly in 'mercurial.cmdutil'
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31216
diff changeset
  1105
        r = revlog.revlog(vfsmod.vfs(pycompat.getcwd(), audit=False),
14323
a79fea6b3e77 debugindex etc.: add --changelog and --manifest options
Sune Foldager <cryo@cyanite.org>
parents: 14322
diff changeset
  1106
                          file_[:-2] + ".i")
a79fea6b3e77 debugindex etc.: add --changelog and --manifest options
Sune Foldager <cryo@cyanite.org>
parents: 14322
diff changeset
  1107
    return r
a79fea6b3e77 debugindex etc.: add --changelog and --manifest options
Sune Foldager <cryo@cyanite.org>
parents: 14322
diff changeset
  1108
5610
2493a478f395 copy: handle rename internally
Matt Mackall <mpm@selenic.com>
parents: 5609
diff changeset
  1109
def copy(ui, repo, pats, opts, rename=False):
5589
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1110
    # called with the repo lock held
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1111
    #
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1112
    # hgsep => pathname that uses "/" to separate directories
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1113
    # ossep => pathname that uses os.sep to separate directories
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1114
    cwd = repo.getcwd()
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1115
    targets = {}
5607
e9bae5c80ab4 copy: minor cleanups
Matt Mackall <mpm@selenic.com>
parents: 5606
diff changeset
  1116
    after = opts.get("after")
e9bae5c80ab4 copy: minor cleanups
Matt Mackall <mpm@selenic.com>
parents: 5606
diff changeset
  1117
    dryrun = opts.get("dry_run")
11303
a1aad8333864 move working dir/dirstate methods from localrepo to workingctx
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 11290
diff changeset
  1118
    wctx = repo[None]
5589
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1119
5605
e7a9ad999308 copy: refactor okaytocopy into walkpat
Matt Mackall <mpm@selenic.com>
parents: 5604
diff changeset
  1120
    def walkpat(pat):
e7a9ad999308 copy: refactor okaytocopy into walkpat
Matt Mackall <mpm@selenic.com>
parents: 5604
diff changeset
  1121
        srcs = []
24306
6ddc86eedc3b style: kill ersatz if-else ternary operators
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 24301
diff changeset
  1122
        if after:
6ddc86eedc3b style: kill ersatz if-else ternary operators
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 24301
diff changeset
  1123
            badstates = '?'
6ddc86eedc3b style: kill ersatz if-else ternary operators
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 24301
diff changeset
  1124
        else:
6ddc86eedc3b style: kill ersatz if-else ternary operators
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 24301
diff changeset
  1125
            badstates = '?r'
32382
c87db79b9507 cleanup: reuse existing wctx variables instead of calling repo[None]
Martin von Zweigbergk <martinvonz@google.com>
parents: 32375
diff changeset
  1126
        m = scmutil.match(wctx, [pat], opts, globbed=True)
c87db79b9507 cleanup: reuse existing wctx variables instead of calling repo[None]
Martin von Zweigbergk <martinvonz@google.com>
parents: 32375
diff changeset
  1127
        for abs in wctx.walk(m):
5605
e7a9ad999308 copy: refactor okaytocopy into walkpat
Matt Mackall <mpm@selenic.com>
parents: 5604
diff changeset
  1128
            state = repo.dirstate[abs]
6584
29c77e5dfb3c walk: remove rel and exact returns
Matt Mackall <mpm@selenic.com>
parents: 6582
diff changeset
  1129
            rel = m.rel(abs)
29c77e5dfb3c walk: remove rel and exact returns
Matt Mackall <mpm@selenic.com>
parents: 6582
diff changeset
  1130
            exact = m.exact(abs)
11223
0d09f2244805 rename: make --after work if source is already in R state
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 11177
diff changeset
  1131
            if state in badstates:
5605
e7a9ad999308 copy: refactor okaytocopy into walkpat
Matt Mackall <mpm@selenic.com>
parents: 5604
diff changeset
  1132
                if exact and state == '?':
e7a9ad999308 copy: refactor okaytocopy into walkpat
Matt Mackall <mpm@selenic.com>
parents: 5604
diff changeset
  1133
                    ui.warn(_('%s: not copying - file is not managed\n') % rel)
e7a9ad999308 copy: refactor okaytocopy into walkpat
Matt Mackall <mpm@selenic.com>
parents: 5604
diff changeset
  1134
                if exact and state == 'r':
e7a9ad999308 copy: refactor okaytocopy into walkpat
Matt Mackall <mpm@selenic.com>
parents: 5604
diff changeset
  1135
                    ui.warn(_('%s: not copying - file has been marked for'
e7a9ad999308 copy: refactor okaytocopy into walkpat
Matt Mackall <mpm@selenic.com>
parents: 5604
diff changeset
  1136
                              ' remove\n') % rel)
e7a9ad999308 copy: refactor okaytocopy into walkpat
Matt Mackall <mpm@selenic.com>
parents: 5604
diff changeset
  1137
                continue
e7a9ad999308 copy: refactor okaytocopy into walkpat
Matt Mackall <mpm@selenic.com>
parents: 5604
diff changeset
  1138
            # abs: hgsep
e7a9ad999308 copy: refactor okaytocopy into walkpat
Matt Mackall <mpm@selenic.com>
parents: 5604
diff changeset
  1139
            # rel: ossep
e7a9ad999308 copy: refactor okaytocopy into walkpat
Matt Mackall <mpm@selenic.com>
parents: 5604
diff changeset
  1140
            srcs.append((abs, rel, exact))
e7a9ad999308 copy: refactor okaytocopy into walkpat
Matt Mackall <mpm@selenic.com>
parents: 5604
diff changeset
  1141
        return srcs
5589
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1142
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1143
    # abssrc: hgsep
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1144
    # relsrc: ossep
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1145
    # otarget: ossep
5605
e7a9ad999308 copy: refactor okaytocopy into walkpat
Matt Mackall <mpm@selenic.com>
parents: 5604
diff changeset
  1146
    def copyfile(abssrc, relsrc, otarget, exact):
20033
f962870712da pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents: 19944
diff changeset
  1147
        abstarget = pathutil.canonpath(repo.root, cwd, otarget)
16542
e596a631210e dirstate: preserve path components case on renames (issue3402)
Patrick Mezard <patrick@mezard.eu>
parents: 16458
diff changeset
  1148
        if '/' in abstarget:
e596a631210e dirstate: preserve path components case on renames (issue3402)
Patrick Mezard <patrick@mezard.eu>
parents: 16458
diff changeset
  1149
            # We cannot normalize abstarget itself, this would prevent
e596a631210e dirstate: preserve path components case on renames (issue3402)
Patrick Mezard <patrick@mezard.eu>
parents: 16458
diff changeset
  1150
            # case only renames, like a => A.
e596a631210e dirstate: preserve path components case on renames (issue3402)
Patrick Mezard <patrick@mezard.eu>
parents: 16458
diff changeset
  1151
            abspath, absname = abstarget.rsplit('/', 1)
e596a631210e dirstate: preserve path components case on renames (issue3402)
Patrick Mezard <patrick@mezard.eu>
parents: 16458
diff changeset
  1152
            abstarget = repo.dirstate.normalize(abspath) + '/' + absname
5589
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1153
        reltarget = repo.pathto(abstarget, cwd)
5607
e9bae5c80ab4 copy: minor cleanups
Matt Mackall <mpm@selenic.com>
parents: 5606
diff changeset
  1154
        target = repo.wjoin(abstarget)
5589
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1155
        src = repo.wjoin(abssrc)
5608
784eadabd985 copy: simplify inner copy
Matt Mackall <mpm@selenic.com>
parents: 5607
diff changeset
  1156
        state = repo.dirstate[abstarget]
5607
e9bae5c80ab4 copy: minor cleanups
Matt Mackall <mpm@selenic.com>
parents: 5606
diff changeset
  1157
13962
8b252e826c68 add: introduce a warning message for non-portable filenames (issue2756) (BC)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13945
diff changeset
  1158
        scmutil.checkportable(ui, abstarget)
13945
03f3ce7ca2a8 copy: do not copy file if name is disallowed anyway
Adrian Buehlmann <adrian@cadifra.com>
parents: 13878
diff changeset
  1159
5607
e9bae5c80ab4 copy: minor cleanups
Matt Mackall <mpm@selenic.com>
parents: 5606
diff changeset
  1160
        # check for collisions
e9bae5c80ab4 copy: minor cleanups
Matt Mackall <mpm@selenic.com>
parents: 5606
diff changeset
  1161
        prevsrc = targets.get(abstarget)
5589
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1162
        if prevsrc is not None:
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1163
            ui.warn(_('%s: not overwriting - %s collides with %s\n') %
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1164
                    (reltarget, repo.pathto(abssrc, cwd),
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1165
                     repo.pathto(prevsrc, cwd)))
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1166
            return
5607
e9bae5c80ab4 copy: minor cleanups
Matt Mackall <mpm@selenic.com>
parents: 5606
diff changeset
  1167
e9bae5c80ab4 copy: minor cleanups
Matt Mackall <mpm@selenic.com>
parents: 5606
diff changeset
  1168
        # check for overwrites
12342
70236d6fd844 rename: do not overwrite existing broken symlinks
Patrick Mezard <pmezard@gmail.com>
parents: 11950
diff changeset
  1169
        exists = os.path.lexists(target)
16283
6c4dbe28dda3 rename: handle case-changing (issue1717)
Matt Mackall <mpm@selenic.com>
parents: 16165
diff changeset
  1170
        samefile = False
6c4dbe28dda3 rename: handle case-changing (issue1717)
Matt Mackall <mpm@selenic.com>
parents: 16165
diff changeset
  1171
        if exists and abssrc != abstarget:
6c4dbe28dda3 rename: handle case-changing (issue1717)
Matt Mackall <mpm@selenic.com>
parents: 16165
diff changeset
  1172
            if (repo.dirstate.normalize(abssrc) ==
6c4dbe28dda3 rename: handle case-changing (issue1717)
Matt Mackall <mpm@selenic.com>
parents: 16165
diff changeset
  1173
                repo.dirstate.normalize(abstarget)):
6c4dbe28dda3 rename: handle case-changing (issue1717)
Matt Mackall <mpm@selenic.com>
parents: 16165
diff changeset
  1174
                if not rename:
6c4dbe28dda3 rename: handle case-changing (issue1717)
Matt Mackall <mpm@selenic.com>
parents: 16165
diff changeset
  1175
                    ui.warn(_("%s: can't copy - same file\n") % reltarget)
6c4dbe28dda3 rename: handle case-changing (issue1717)
Matt Mackall <mpm@selenic.com>
parents: 16165
diff changeset
  1176
                    return
6c4dbe28dda3 rename: handle case-changing (issue1717)
Matt Mackall <mpm@selenic.com>
parents: 16165
diff changeset
  1177
                exists = False
6c4dbe28dda3 rename: handle case-changing (issue1717)
Matt Mackall <mpm@selenic.com>
parents: 16165
diff changeset
  1178
                samefile = True
6c4dbe28dda3 rename: handle case-changing (issue1717)
Matt Mackall <mpm@selenic.com>
parents: 16165
diff changeset
  1179
8117
2b30d8488819 remove unnecessary outer parenthesis in if-statements
Martin Geisler <mg@lazybytes.net>
parents: 8013
diff changeset
  1180
        if not after and exists or after and state in 'mn':
5589
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1181
            if not opts['force']:
30151
381293e1135e copy: distinguish "file exists" cases and add a hint (BC)
Augie Fackler <augie@google.com>
parents: 30142
diff changeset
  1182
                if state in 'mn':
381293e1135e copy: distinguish "file exists" cases and add a hint (BC)
Augie Fackler <augie@google.com>
parents: 30142
diff changeset
  1183
                    msg = _('%s: not overwriting - file already committed\n')
381293e1135e copy: distinguish "file exists" cases and add a hint (BC)
Augie Fackler <augie@google.com>
parents: 30142
diff changeset
  1184
                    if after:
381293e1135e copy: distinguish "file exists" cases and add a hint (BC)
Augie Fackler <augie@google.com>
parents: 30142
diff changeset
  1185
                        flags = '--after --force'
381293e1135e copy: distinguish "file exists" cases and add a hint (BC)
Augie Fackler <augie@google.com>
parents: 30142
diff changeset
  1186
                    else:
381293e1135e copy: distinguish "file exists" cases and add a hint (BC)
Augie Fackler <augie@google.com>
parents: 30142
diff changeset
  1187
                        flags = '--force'
381293e1135e copy: distinguish "file exists" cases and add a hint (BC)
Augie Fackler <augie@google.com>
parents: 30142
diff changeset
  1188
                    if rename:
381293e1135e copy: distinguish "file exists" cases and add a hint (BC)
Augie Fackler <augie@google.com>
parents: 30142
diff changeset
  1189
                        hint = _('(hg rename %s to replace the file by '
381293e1135e copy: distinguish "file exists" cases and add a hint (BC)
Augie Fackler <augie@google.com>
parents: 30142
diff changeset
  1190
                                 'recording a rename)\n') % flags
381293e1135e copy: distinguish "file exists" cases and add a hint (BC)
Augie Fackler <augie@google.com>
parents: 30142
diff changeset
  1191
                    else:
381293e1135e copy: distinguish "file exists" cases and add a hint (BC)
Augie Fackler <augie@google.com>
parents: 30142
diff changeset
  1192
                        hint = _('(hg copy %s to replace the file by '
381293e1135e copy: distinguish "file exists" cases and add a hint (BC)
Augie Fackler <augie@google.com>
parents: 30142
diff changeset
  1193
                                 'recording a copy)\n') % flags
381293e1135e copy: distinguish "file exists" cases and add a hint (BC)
Augie Fackler <augie@google.com>
parents: 30142
diff changeset
  1194
                else:
381293e1135e copy: distinguish "file exists" cases and add a hint (BC)
Augie Fackler <augie@google.com>
parents: 30142
diff changeset
  1195
                    msg = _('%s: not overwriting - file exists\n')
381293e1135e copy: distinguish "file exists" cases and add a hint (BC)
Augie Fackler <augie@google.com>
parents: 30142
diff changeset
  1196
                    if rename:
381293e1135e copy: distinguish "file exists" cases and add a hint (BC)
Augie Fackler <augie@google.com>
parents: 30142
diff changeset
  1197
                        hint = _('(hg rename --after to record the rename)\n')
381293e1135e copy: distinguish "file exists" cases and add a hint (BC)
Augie Fackler <augie@google.com>
parents: 30142
diff changeset
  1198
                    else:
381293e1135e copy: distinguish "file exists" cases and add a hint (BC)
Augie Fackler <augie@google.com>
parents: 30142
diff changeset
  1199
                        hint = _('(hg copy --after to record the copy)\n')
381293e1135e copy: distinguish "file exists" cases and add a hint (BC)
Augie Fackler <augie@google.com>
parents: 30142
diff changeset
  1200
                ui.warn(msg % reltarget)
381293e1135e copy: distinguish "file exists" cases and add a hint (BC)
Augie Fackler <augie@google.com>
parents: 30142
diff changeset
  1201
                ui.warn(hint)
5589
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1202
                return
5607
e9bae5c80ab4 copy: minor cleanups
Matt Mackall <mpm@selenic.com>
parents: 5606
diff changeset
  1203
e9bae5c80ab4 copy: minor cleanups
Matt Mackall <mpm@selenic.com>
parents: 5606
diff changeset
  1204
        if after:
5608
784eadabd985 copy: simplify inner copy
Matt Mackall <mpm@selenic.com>
parents: 5607
diff changeset
  1205
            if not exists:
11152
e8d10d085f47 cmdutil: Warn when trying to copy/rename --after to a nonexistant file.
Steve Losh <steve@stevelosh.com>
parents: 11061
diff changeset
  1206
                if rename:
e8d10d085f47 cmdutil: Warn when trying to copy/rename --after to a nonexistant file.
Steve Losh <steve@stevelosh.com>
parents: 11061
diff changeset
  1207
                    ui.warn(_('%s: not recording move - %s does not exist\n') %
e8d10d085f47 cmdutil: Warn when trying to copy/rename --after to a nonexistant file.
Steve Losh <steve@stevelosh.com>
parents: 11061
diff changeset
  1208
                            (relsrc, reltarget))
e8d10d085f47 cmdutil: Warn when trying to copy/rename --after to a nonexistant file.
Steve Losh <steve@stevelosh.com>
parents: 11061
diff changeset
  1209
                else:
e8d10d085f47 cmdutil: Warn when trying to copy/rename --after to a nonexistant file.
Steve Losh <steve@stevelosh.com>
parents: 11061
diff changeset
  1210
                    ui.warn(_('%s: not recording copy - %s does not exist\n') %
e8d10d085f47 cmdutil: Warn when trying to copy/rename --after to a nonexistant file.
Steve Losh <steve@stevelosh.com>
parents: 11061
diff changeset
  1211
                            (relsrc, reltarget))
5589
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1212
                return
5608
784eadabd985 copy: simplify inner copy
Matt Mackall <mpm@selenic.com>
parents: 5607
diff changeset
  1213
        elif not dryrun:
5589
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1214
            try:
5608
784eadabd985 copy: simplify inner copy
Matt Mackall <mpm@selenic.com>
parents: 5607
diff changeset
  1215
                if exists:
784eadabd985 copy: simplify inner copy
Matt Mackall <mpm@selenic.com>
parents: 5607
diff changeset
  1216
                    os.unlink(target)
784eadabd985 copy: simplify inner copy
Matt Mackall <mpm@selenic.com>
parents: 5607
diff changeset
  1217
                targetdir = os.path.dirname(target) or '.'
784eadabd985 copy: simplify inner copy
Matt Mackall <mpm@selenic.com>
parents: 5607
diff changeset
  1218
                if not os.path.isdir(targetdir):
784eadabd985 copy: simplify inner copy
Matt Mackall <mpm@selenic.com>
parents: 5607
diff changeset
  1219
                    os.makedirs(targetdir)
16283
6c4dbe28dda3 rename: handle case-changing (issue1717)
Matt Mackall <mpm@selenic.com>
parents: 16165
diff changeset
  1220
                if samefile:
6c4dbe28dda3 rename: handle case-changing (issue1717)
Matt Mackall <mpm@selenic.com>
parents: 16165
diff changeset
  1221
                    tmp = target + "~hgrename"
6c4dbe28dda3 rename: handle case-changing (issue1717)
Matt Mackall <mpm@selenic.com>
parents: 16165
diff changeset
  1222
                    os.rename(src, tmp)
6c4dbe28dda3 rename: handle case-changing (issue1717)
Matt Mackall <mpm@selenic.com>
parents: 16165
diff changeset
  1223
                    os.rename(tmp, target)
6c4dbe28dda3 rename: handle case-changing (issue1717)
Matt Mackall <mpm@selenic.com>
parents: 16165
diff changeset
  1224
                else:
37088
08890706366e copyfile: preserve stat info (mtime, etc.) when doing copies/renames
Kyle Lippincott <spectral@google.com>
parents: 37084
diff changeset
  1225
                    # Preserve stat info on renames, not on copies; this matches
08890706366e copyfile: preserve stat info (mtime, etc.) when doing copies/renames
Kyle Lippincott <spectral@google.com>
parents: 37084
diff changeset
  1226
                    # Linux CLI behavior.
08890706366e copyfile: preserve stat info (mtime, etc.) when doing copies/renames
Kyle Lippincott <spectral@google.com>
parents: 37084
diff changeset
  1227
                    util.copyfile(src, target, copystat=rename)
14518
a67e866f46f9 workingctx: eliminate remove function
Adrian Buehlmann <adrian@cadifra.com>
parents: 14442
diff changeset
  1228
                srcexists = True
25660
328739ea70c3 global: mass rewrite to use modern exception syntax
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25657
diff changeset
  1229
            except IOError as inst:
5589
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1230
                if inst.errno == errno.ENOENT:
24364
135b23868f45 commands: replace "working copy" with "working directory" in help/messages
Yuya Nishihara <yuya@tcha.org>
parents: 24359
diff changeset
  1231
                    ui.warn(_('%s: deleted in working directory\n') % relsrc)
14518
a67e866f46f9 workingctx: eliminate remove function
Adrian Buehlmann <adrian@cadifra.com>
parents: 14442
diff changeset
  1232
                    srcexists = False
5589
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1233
                else:
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1234
                    ui.warn(_('%s: cannot copy - %s\n') %
34022
d5b2beca16c0 python3: wrap all uses of <exception>.strerror with strtolocal
Augie Fackler <raf@durin42.com>
parents: 34019
diff changeset
  1235
                            (relsrc, encoding.strtolocal(inst.strerror)))
5606
447ea621e50e copy: propagate errors properly
Matt Mackall <mpm@selenic.com>
parents: 5605
diff changeset
  1236
                    return True # report a failure
5607
e9bae5c80ab4 copy: minor cleanups
Matt Mackall <mpm@selenic.com>
parents: 5606
diff changeset
  1237
5589
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1238
        if ui.verbose or not exact:
7894
caef5fdf1375 cmdutil: fix untranslatable string in copy
Martin Geisler <mg@daimi.au.dk>
parents: 7879
diff changeset
  1239
            if rename:
caef5fdf1375 cmdutil: fix untranslatable string in copy
Martin Geisler <mg@daimi.au.dk>
parents: 7879
diff changeset
  1240
                ui.status(_('moving %s to %s\n') % (relsrc, reltarget))
caef5fdf1375 cmdutil: fix untranslatable string in copy
Martin Geisler <mg@daimi.au.dk>
parents: 7879
diff changeset
  1241
            else:
caef5fdf1375 cmdutil: fix untranslatable string in copy
Martin Geisler <mg@daimi.au.dk>
parents: 7879
diff changeset
  1242
                ui.status(_('copying %s to %s\n') % (relsrc, reltarget))
5608
784eadabd985 copy: simplify inner copy
Matt Mackall <mpm@selenic.com>
parents: 5607
diff changeset
  1243
5589
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1244
        targets[abstarget] = abssrc
5607
e9bae5c80ab4 copy: minor cleanups
Matt Mackall <mpm@selenic.com>
parents: 5606
diff changeset
  1245
e9bae5c80ab4 copy: minor cleanups
Matt Mackall <mpm@selenic.com>
parents: 5606
diff changeset
  1246
        # fix up dirstate
14321
003d63bb4fa5 scmutil: drop some aliases in cmdutil
Matt Mackall <mpm@selenic.com>
parents: 14320
diff changeset
  1247
        scmutil.dirstatecopy(ui, repo, wctx, abssrc, abstarget,
003d63bb4fa5 scmutil: drop some aliases in cmdutil
Matt Mackall <mpm@selenic.com>
parents: 14320
diff changeset
  1248
                             dryrun=dryrun, cwd=cwd)
5610
2493a478f395 copy: handle rename internally
Matt Mackall <mpm@selenic.com>
parents: 5609
diff changeset
  1249
        if rename and not dryrun:
16283
6c4dbe28dda3 rename: handle case-changing (issue1717)
Matt Mackall <mpm@selenic.com>
parents: 16165
diff changeset
  1250
            if not after and srcexists and not samefile:
31309
8908f985570c vfs: use repo.wvfs.unlinkpath
Mads Kiilerich <madski@unity3d.com>
parents: 31237
diff changeset
  1251
                repo.wvfs.unlinkpath(abssrc)
14518
a67e866f46f9 workingctx: eliminate remove function
Adrian Buehlmann <adrian@cadifra.com>
parents: 14442
diff changeset
  1252
            wctx.forget([abssrc])
5589
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1253
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1254
    # pat: ossep
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1255
    # dest ossep
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1256
    # srcs: list of (hgsep, hgsep, ossep, bool)
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1257
    # return: function that takes hgsep and returns ossep
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1258
    def targetpathfn(pat, dest, srcs):
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1259
        if os.path.isdir(pat):
20033
f962870712da pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents: 19944
diff changeset
  1260
            abspfx = pathutil.canonpath(repo.root, cwd, pat)
5589
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1261
            abspfx = util.localpath(abspfx)
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1262
            if destdirexists:
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1263
                striplen = len(os.path.split(abspfx)[0])
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1264
            else:
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1265
                striplen = len(abspfx)
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1266
            if striplen:
30615
bb77654dc7ae py3: replace os.sep with pycompat.ossep (part 3 of 4)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 30534
diff changeset
  1267
                striplen += len(pycompat.ossep)
5589
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1268
            res = lambda p: os.path.join(dest, util.localpath(p)[striplen:])
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1269
        elif destdirexists:
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1270
            res = lambda p: os.path.join(dest,
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1271
                                         os.path.basename(util.localpath(p)))
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1272
        else:
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1273
            res = lambda p: dest
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1274
        return res
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1275
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1276
    # pat: ossep
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1277
    # dest ossep
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1278
    # srcs: list of (hgsep, hgsep, ossep, bool)
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1279
    # return: function that takes hgsep and returns ossep
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1280
    def targetpathafterfn(pat, dest, srcs):
12085
6f833fc3ccab Consistently import foo as foomod when foo to avoid shadowing
Martin Geisler <mg@aragost.com>
parents: 12032
diff changeset
  1281
        if matchmod.patkind(pat):
5589
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1282
            # a mercurial pattern
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1283
            res = lambda p: os.path.join(dest,
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1284
                                         os.path.basename(util.localpath(p)))
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1285
        else:
20033
f962870712da pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents: 19944
diff changeset
  1286
            abspfx = pathutil.canonpath(repo.root, cwd, pat)
5589
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1287
            if len(abspfx) < len(srcs[0][0]):
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1288
                # A directory. Either the target path contains the last
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1289
                # component of the source path or it does not.
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1290
                def evalpath(striplen):
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1291
                    score = 0
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1292
                    for s in srcs:
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1293
                        t = os.path.join(dest, util.localpath(s[0])[striplen:])
12357
cb59654c2c7a Restore lexists() changes lost in e0ee3e822a9a merge
Patrick Mezard <pmezard@gmail.com>
parents: 12345
diff changeset
  1294
                        if os.path.lexists(t):
5589
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1295
                            score += 1
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1296
                    return score
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1297
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1298
                abspfx = util.localpath(abspfx)
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1299
                striplen = len(abspfx)
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1300
                if striplen:
30615
bb77654dc7ae py3: replace os.sep with pycompat.ossep (part 3 of 4)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 30534
diff changeset
  1301
                    striplen += len(pycompat.ossep)
5589
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1302
                if os.path.isdir(os.path.join(dest, os.path.split(abspfx)[1])):
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1303
                    score = evalpath(striplen)
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1304
                    striplen1 = len(os.path.split(abspfx)[0])
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1305
                    if striplen1:
30615
bb77654dc7ae py3: replace os.sep with pycompat.ossep (part 3 of 4)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 30534
diff changeset
  1306
                        striplen1 += len(pycompat.ossep)
5589
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1307
                    if evalpath(striplen1) > score:
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1308
                        striplen = striplen1
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1309
                res = lambda p: os.path.join(dest,
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1310
                                             util.localpath(p)[striplen:])
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1311
            else:
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1312
                # a file
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1313
                if destdirexists:
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1314
                    res = lambda p: os.path.join(dest,
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1315
                                        os.path.basename(util.localpath(p)))
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1316
                else:
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1317
                    res = lambda p: dest
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1318
        return res
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1319
14321
003d63bb4fa5 scmutil: drop some aliases in cmdutil
Matt Mackall <mpm@selenic.com>
parents: 14320
diff changeset
  1320
    pats = scmutil.expandpats(pats)
5589
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1321
    if not pats:
26587
56b2bcea2529 error: get Abort from 'error' instead of 'util'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26579
diff changeset
  1322
        raise error.Abort(_('no source or destination specified'))
5589
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1323
    if len(pats) == 1:
26587
56b2bcea2529 error: get Abort from 'error' instead of 'util'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26579
diff changeset
  1324
        raise error.Abort(_('no destination specified'))
5589
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1325
    dest = pats.pop()
6258
c24f4b3f156b Fix issue995 (copy --after and symlinks pointing to a directory)
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 6211
diff changeset
  1326
    destdirexists = os.path.isdir(dest) and not os.path.islink(dest)
5589
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1327
    if not destdirexists:
12085
6f833fc3ccab Consistently import foo as foomod when foo to avoid shadowing
Martin Geisler <mg@aragost.com>
parents: 12032
diff changeset
  1328
        if len(pats) > 1 or matchmod.patkind(pats[0]):
26587
56b2bcea2529 error: get Abort from 'error' instead of 'util'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26579
diff changeset
  1329
            raise error.Abort(_('with multiple sources, destination must be an '
5589
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1330
                               'existing directory'))
5843
83c354c4d529 Add endswithsep() and use it instead of using os.sep and os.altsep directly.
Shun-ichi GOTO <shunichi.goto@gmail.com>
parents: 5836
diff changeset
  1331
        if util.endswithsep(dest):
26587
56b2bcea2529 error: get Abort from 'error' instead of 'util'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26579
diff changeset
  1332
            raise error.Abort(_('destination %s is not a directory') % dest)
5607
e9bae5c80ab4 copy: minor cleanups
Matt Mackall <mpm@selenic.com>
parents: 5606
diff changeset
  1333
e9bae5c80ab4 copy: minor cleanups
Matt Mackall <mpm@selenic.com>
parents: 5606
diff changeset
  1334
    tfn = targetpathfn
e9bae5c80ab4 copy: minor cleanups
Matt Mackall <mpm@selenic.com>
parents: 5606
diff changeset
  1335
    if after:
5589
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1336
        tfn = targetpathafterfn
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1337
    copylist = []
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1338
    for pat in pats:
5605
e7a9ad999308 copy: refactor okaytocopy into walkpat
Matt Mackall <mpm@selenic.com>
parents: 5604
diff changeset
  1339
        srcs = walkpat(pat)
5589
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1340
        if not srcs:
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1341
            continue
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1342
        copylist.append((tfn(pat, dest, srcs), srcs))
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1343
    if not copylist:
26587
56b2bcea2529 error: get Abort from 'error' instead of 'util'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26579
diff changeset
  1344
        raise error.Abort(_('no files to copy'))
5589
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1345
5606
447ea621e50e copy: propagate errors properly
Matt Mackall <mpm@selenic.com>
parents: 5605
diff changeset
  1346
    errors = 0
5589
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1347
    for targetpath, srcs in copylist:
5605
e7a9ad999308 copy: refactor okaytocopy into walkpat
Matt Mackall <mpm@selenic.com>
parents: 5604
diff changeset
  1348
        for abssrc, relsrc, exact in srcs:
5606
447ea621e50e copy: propagate errors properly
Matt Mackall <mpm@selenic.com>
parents: 5605
diff changeset
  1349
            if copyfile(abssrc, relsrc, targetpath(abssrc), exact):
447ea621e50e copy: propagate errors properly
Matt Mackall <mpm@selenic.com>
parents: 5605
diff changeset
  1350
                errors += 1
5589
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1351
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1352
    if errors:
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1353
        ui.warn(_('(consider using --after)\n'))
5609
a783d3627144 copy: move rename logic
Matt Mackall <mpm@selenic.com>
parents: 5608
diff changeset
  1354
11177
6a64813276ed commands: initial audit of exit codes
Matt Mackall <mpm@selenic.com>
parents: 11152
diff changeset
  1355
    return errors != 0
5589
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
  1356
26561
1f14920a892c import: allow processing of extra part header during import
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26547
diff changeset
  1357
## facility to let extension process additional data into an import patch
1f14920a892c import: allow processing of extra part header during import
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26547
diff changeset
  1358
# list of identifier to be executed in order
1f14920a892c import: allow processing of extra part header during import
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26547
diff changeset
  1359
extrapreimport = []  # run before commit
26562
dd2f5e014806 import: allow processing of extra part header after import
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26561
diff changeset
  1360
extrapostimport = [] # run after commit
26561
1f14920a892c import: allow processing of extra part header during import
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26547
diff changeset
  1361
# mapping from identifier to actual import function
1f14920a892c import: allow processing of extra part header during import
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26547
diff changeset
  1362
#
1f14920a892c import: allow processing of extra part header during import
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26547
diff changeset
  1363
# 'preimport' are run before the commit is made and are provided the following
1f14920a892c import: allow processing of extra part header during import
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26547
diff changeset
  1364
# arguments:
1f14920a892c import: allow processing of extra part header during import
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26547
diff changeset
  1365
# - repo: the localrepository instance,
1f14920a892c import: allow processing of extra part header during import
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26547
diff changeset
  1366
# - patchdata: data extracted from patch header (cf m.patch.patchheadermap),
26781
1aee2ab0f902 spelling: trivial spell checking
Mads Kiilerich <madski@unity3d.com>
parents: 26750
diff changeset
  1367
# - extra: the future extra dictionary of the changeset, please mutate it,
26561
1f14920a892c import: allow processing of extra part header during import
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26547
diff changeset
  1368
# - opts: the import options.
1f14920a892c import: allow processing of extra part header during import
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26547
diff changeset
  1369
# XXX ideally, we would just pass an ctx ready to be computed, that would allow
1f14920a892c import: allow processing of extra part header during import
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26547
diff changeset
  1370
# mutation of in memory commit and more. Feel free to rework the code to get
1f14920a892c import: allow processing of extra part header during import
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26547
diff changeset
  1371
# there.
1f14920a892c import: allow processing of extra part header during import
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26547
diff changeset
  1372
extrapreimportmap = {}
26562
dd2f5e014806 import: allow processing of extra part header after import
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26561
diff changeset
  1373
# 'postimport' are run after the commit is made and are provided the following
dd2f5e014806 import: allow processing of extra part header after import
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26561
diff changeset
  1374
# argument:
dd2f5e014806 import: allow processing of extra part header after import
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26561
diff changeset
  1375
# - ctx: the changectx created by import.
dd2f5e014806 import: allow processing of extra part header after import
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26561
diff changeset
  1376
extrapostimportmap = {}
26561
1f14920a892c import: allow processing of extra part header during import
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26547
diff changeset
  1377
37620
fd1dd79cff20 cmdutil: pass in parsed patch to tryimportone() (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37604
diff changeset
  1378
def tryimportone(ui, repo, patchdata, parents, opts, msgs, updatefunc):
20500
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
  1379
    """Utility function used by commands.import to import a single patch
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
  1380
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
  1381
    This function is explicitly defined here to help the evolve extension to
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
  1382
    wrap this part of the import logic.
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
  1383
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
  1384
    The API is currently a bit ugly because it a simple code translation from
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
  1385
    the import command. Feel free to make it better.
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
  1386
37620
fd1dd79cff20 cmdutil: pass in parsed patch to tryimportone() (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37604
diff changeset
  1387
    :patchdata: a dictionary containing parsed patch data (such as from
fd1dd79cff20 cmdutil: pass in parsed patch to tryimportone() (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37604
diff changeset
  1388
                ``patch.extract()``)
20500
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
  1389
    :parents: nodes that will be parent of the created commit
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
  1390
    :opts: the full dict of option passed to the import command
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
  1391
    :msgs: list to save commit message to.
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
  1392
           (used in case we need to save it when failing)
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
  1393
    :updatefunc: a function that update a repo to a given node
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
  1394
                 updatefunc(<repo>, <node>)
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
  1395
    """
25930
221491bbaf7e cmdutil: break import cycle
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25795
diff changeset
  1396
    # avoid cycle context -> subrepo -> cmdutil
28322
ebd0e86bdf89 cmdutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 28313
diff changeset
  1397
    from . import context
37620
fd1dd79cff20 cmdutil: pass in parsed patch to tryimportone() (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37604
diff changeset
  1398
fd1dd79cff20 cmdutil: pass in parsed patch to tryimportone() (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37604
diff changeset
  1399
    tmpname = patchdata.get('filename')
fd1dd79cff20 cmdutil: pass in parsed patch to tryimportone() (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37604
diff changeset
  1400
    message = patchdata.get('message')
fd1dd79cff20 cmdutil: pass in parsed patch to tryimportone() (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37604
diff changeset
  1401
    user = opts.get('user') or patchdata.get('user')
fd1dd79cff20 cmdutil: pass in parsed patch to tryimportone() (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37604
diff changeset
  1402
    date = opts.get('date') or patchdata.get('date')
fd1dd79cff20 cmdutil: pass in parsed patch to tryimportone() (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37604
diff changeset
  1403
    branch = patchdata.get('branch')
fd1dd79cff20 cmdutil: pass in parsed patch to tryimportone() (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37604
diff changeset
  1404
    nodeid = patchdata.get('nodeid')
fd1dd79cff20 cmdutil: pass in parsed patch to tryimportone() (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37604
diff changeset
  1405
    p1 = patchdata.get('p1')
fd1dd79cff20 cmdutil: pass in parsed patch to tryimportone() (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37604
diff changeset
  1406
    p2 = patchdata.get('p2')
20500
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
  1407
27613
dffd2ed9a7c4 import: refactor nocommit and importbranch handling
timeless <timeless@mozdev.org>
parents: 27612
diff changeset
  1408
    nocommit = opts.get('no_commit')
dffd2ed9a7c4 import: refactor nocommit and importbranch handling
timeless <timeless@mozdev.org>
parents: 27612
diff changeset
  1409
    importbranch = opts.get('import_branch')
20500
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
  1410
    update = not opts.get('bypass')
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
  1411
    strip = opts["strip"]
24259
5ac8ce04baa2 cmdutil.tryimportone: allow importing relative patches into the working dir
Siddharth Agarwal <sid0@fb.com>
parents: 24222
diff changeset
  1412
    prefix = opts["prefix"]
20500
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
  1413
    sim = float(opts.get('similarity') or 0)
37621
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1414
20500
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
  1415
    if not tmpname:
37621
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1416
        return None, None, False
20500
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
  1417
21553
bee0e1cffdd3 import: add --partial flag to create a changeset despite failed hunks
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 21419
diff changeset
  1418
    rejects = False
bee0e1cffdd3 import: add --partial flag to create a changeset despite failed hunks
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 21419
diff changeset
  1419
37621
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1420
    cmdline_message = logmessage(ui, opts)
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1421
    if cmdline_message:
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1422
        # pickup the cmdline msg
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1423
        message = cmdline_message
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1424
    elif message:
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1425
        # pickup the patch msg
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1426
        message = message.strip()
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1427
    else:
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1428
        # launch the editor
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1429
        message = None
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1430
    ui.debug('message:\n%s\n' % (message or ''))
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1431
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1432
    if len(parents) == 1:
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1433
        parents.append(repo[nullid])
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1434
    if opts.get('exact'):
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1435
        if not nodeid or not p1:
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1436
            raise error.Abort(_('not a Mercurial patch'))
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1437
        p1 = repo[p1]
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1438
        p2 = repo[p2 or nullid]
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1439
    elif p2:
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1440
        try:
20500
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
  1441
            p1 = repo[p1]
37621
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1442
            p2 = repo[p2]
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1443
            # Without any options, consider p2 only if the
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1444
            # patch is being applied on top of the recorded
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1445
            # first parent.
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1446
            if p1 != parents[0]:
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1447
                p1 = parents[0]
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1448
                p2 = repo[nullid]
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1449
        except error.RepoError:
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1450
            p1, p2 = parents
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1451
        if p2.node() == nullid:
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1452
            ui.warn(_("warning: import the patch as a normal revision\n"
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1453
                      "(use --exact to import the patch as a merge)\n"))
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1454
    else:
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1455
        p1, p2 = parents
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1456
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1457
    n = None
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1458
    if update:
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1459
        if p1 != parents[0]:
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1460
            updatefunc(repo, p1.node())
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1461
        if p2 != parents[1]:
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1462
            repo.setparents(p1.node(), p2.node())
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1463
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1464
        if opts.get('exact') or importbranch:
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1465
            repo.dirstate.setbranch(branch or 'default')
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1466
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1467
        partial = opts.get('partial', False)
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1468
        files = set()
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1469
        try:
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1470
            patch.patch(ui, repo, tmpname, strip=strip, prefix=prefix,
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1471
                        files=files, eolmode=None, similarity=sim / 100.0)
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1472
        except error.PatchError as e:
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1473
            if not partial:
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1474
                raise error.Abort(pycompat.bytestr(e))
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1475
            if partial:
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1476
                rejects = True
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1477
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1478
        files = list(files)
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1479
        if nocommit:
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1480
            if message:
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1481
                msgs.append(message)
20500
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
  1482
        else:
37621
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1483
            if opts.get('exact') or p2:
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1484
                # If you got here, you either use --force and know what
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1485
                # you are doing or used --exact or a merge patch while
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1486
                # being updated to its first parent.
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1487
                m = None
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1488
            else:
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1489
                m = scmutil.matchfiles(repo, files or [])
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1490
            editform = mergeeditform(repo[None], 'import.normal')
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1491
            if opts.get('exact'):
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1492
                editor = None
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1493
            else:
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1494
                editor = getcommiteditor(editform=editform,
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1495
                                         **pycompat.strkwargs(opts))
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1496
            extra = {}
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1497
            for idfunc in extrapreimport:
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1498
                extrapreimportmap[idfunc](repo, patchdata, extra, opts)
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1499
            overrides = {}
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1500
            if partial:
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1501
                overrides[('ui', 'allowemptycommit')] = True
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1502
            with repo.ui.configoverride(overrides, 'import'):
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1503
                n = repo.commit(message, user,
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1504
                                date, match=m,
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1505
                                editor=editor, extra=extra)
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1506
                for idfunc in extrapostimport:
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1507
                    extrapostimportmap[idfunc](repo[n])
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1508
    else:
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1509
        if opts.get('exact') or importbranch:
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1510
            branch = branch or 'default'
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1511
        else:
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1512
            branch = p1.branch()
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1513
        store = patch.filestore()
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1514
        try:
20500
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
  1515
            files = set()
21553
bee0e1cffdd3 import: add --partial flag to create a changeset despite failed hunks
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 21419
diff changeset
  1516
            try:
37621
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1517
                patch.patchrepo(ui, repo, p1, store, tmpname, strip, prefix,
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1518
                                files, eolmode=None)
34251
61714510220d error: move patch.PatchError so it can easily implement __bytes__ (API)
Yuya Nishihara <yuya@tcha.org>
parents: 34189
diff changeset
  1519
            except error.PatchError as e:
37621
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1520
                raise error.Abort(stringutil.forcebytestr(e))
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1521
            if opts.get('exact'):
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1522
                editor = None
20500
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
  1523
            else:
37621
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1524
                editor = getcommiteditor(editform='import.bypass')
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1525
            memctx = context.memctx(repo, (p1.node(), p2.node()),
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1526
                                    message,
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1527
                                    files=files,
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1528
                                    filectxfn=store,
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1529
                                    user=user,
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1530
                                    date=date,
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1531
                                    branch=branch,
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1532
                                    editor=editor)
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1533
            n = memctx.commit()
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1534
        finally:
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1535
            store.close()
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1536
    if opts.get('exact') and nocommit:
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1537
        # --exact with --no-commit is still useful in that it does merge
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1538
        # and branch bits
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1539
        ui.warn(_("warning: can't check exact import with --no-commit\n"))
37815
32a75a8a5b0f import: fix crash on --exact check of empty commit (issue5702)
Yuya Nishihara <yuya@tcha.org>
parents: 37778
diff changeset
  1540
    elif opts.get('exact') and (not n or hex(n) != nodeid):
37621
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1541
        raise error.Abort(_('patch is damaged or loses information'))
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1542
    msg = _('applied to working directory')
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1543
    if n:
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1544
        # i18n: refers to a short changeset id
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1545
        msg = _('created %s') % short(n)
5537d8f5e989 patch: make extract() a context manager (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37620
diff changeset
  1546
    return msg, n, rejects
20500
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
  1547
26545
e99c3846d78a export: introduce a generic way to add patch header on export
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26533
diff changeset
  1548
# facility to let extensions include additional data in an exported patch
e99c3846d78a export: introduce a generic way to add patch header on export
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26533
diff changeset
  1549
# list of identifiers to be executed in order
e99c3846d78a export: introduce a generic way to add patch header on export
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26533
diff changeset
  1550
extraexport = []
e99c3846d78a export: introduce a generic way to add patch header on export
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26533
diff changeset
  1551
# mapping from identifier to actual export function
e99c3846d78a export: introduce a generic way to add patch header on export
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26533
diff changeset
  1552
# function as to return a string to be added to the header or None
e99c3846d78a export: introduce a generic way to add patch header on export
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26533
diff changeset
  1553
# it is given two arguments (sequencenumber, changectx)
e99c3846d78a export: introduce a generic way to add patch header on export
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26533
diff changeset
  1554
extraexportmap = {}
e99c3846d78a export: introduce a generic way to add patch header on export
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26533
diff changeset
  1555
37602
52670eaa14b4 export: port _exportsingle() to formatter
Yuya Nishihara <yuya@tcha.org>
parents: 37601
diff changeset
  1556
def _exportsingle(repo, ctx, fm, match, switch_parent, seqno, diffopts):
32662
9d201b39ccd9 export: map wctx.node() to 'ff...' node id (issue5438)
Yuya Nishihara <yuya@tcha.org>
parents: 32658
diff changeset
  1557
    node = scmutil.binnode(ctx)
32433
7feaf5550a9e cmdutil: extract closure that performs the actual export formatting
Augie Fackler <augie@google.com>
parents: 32432
diff changeset
  1558
    parents = [p.node() for p in ctx.parents() if p]
7feaf5550a9e cmdutil: extract closure that performs the actual export formatting
Augie Fackler <augie@google.com>
parents: 32432
diff changeset
  1559
    branch = ctx.branch()
7feaf5550a9e cmdutil: extract closure that performs the actual export formatting
Augie Fackler <augie@google.com>
parents: 32432
diff changeset
  1560
    if switch_parent:
7feaf5550a9e cmdutil: extract closure that performs the actual export formatting
Augie Fackler <augie@google.com>
parents: 32432
diff changeset
  1561
        parents.reverse()
7feaf5550a9e cmdutil: extract closure that performs the actual export formatting
Augie Fackler <augie@google.com>
parents: 32432
diff changeset
  1562
7feaf5550a9e cmdutil: extract closure that performs the actual export formatting
Augie Fackler <augie@google.com>
parents: 32432
diff changeset
  1563
    if parents:
7feaf5550a9e cmdutil: extract closure that performs the actual export formatting
Augie Fackler <augie@google.com>
parents: 32432
diff changeset
  1564
        prev = parents[0]
7feaf5550a9e cmdutil: extract closure that performs the actual export formatting
Augie Fackler <augie@google.com>
parents: 32432
diff changeset
  1565
    else:
7feaf5550a9e cmdutil: extract closure that performs the actual export formatting
Augie Fackler <augie@google.com>
parents: 32432
diff changeset
  1566
        prev = nullid
7feaf5550a9e cmdutil: extract closure that performs the actual export formatting
Augie Fackler <augie@google.com>
parents: 32432
diff changeset
  1567
37602
52670eaa14b4 export: port _exportsingle() to formatter
Yuya Nishihara <yuya@tcha.org>
parents: 37601
diff changeset
  1568
    fm.context(ctx=ctx)
52670eaa14b4 export: port _exportsingle() to formatter
Yuya Nishihara <yuya@tcha.org>
parents: 37601
diff changeset
  1569
    fm.plain('# HG changeset patch\n')
52670eaa14b4 export: port _exportsingle() to formatter
Yuya Nishihara <yuya@tcha.org>
parents: 37601
diff changeset
  1570
    fm.write('user', '# User %s\n', ctx.user())
52670eaa14b4 export: port _exportsingle() to formatter
Yuya Nishihara <yuya@tcha.org>
parents: 37601
diff changeset
  1571
    fm.plain('# Date %d %d\n' % ctx.date())
52670eaa14b4 export: port _exportsingle() to formatter
Yuya Nishihara <yuya@tcha.org>
parents: 37601
diff changeset
  1572
    fm.write('date', '#      %s\n', fm.formatdate(ctx.date()))
52670eaa14b4 export: port _exportsingle() to formatter
Yuya Nishihara <yuya@tcha.org>
parents: 37601
diff changeset
  1573
    fm.condwrite(branch and branch != 'default',
52670eaa14b4 export: port _exportsingle() to formatter
Yuya Nishihara <yuya@tcha.org>
parents: 37601
diff changeset
  1574
                 'branch', '# Branch %s\n', branch)
52670eaa14b4 export: port _exportsingle() to formatter
Yuya Nishihara <yuya@tcha.org>
parents: 37601
diff changeset
  1575
    fm.write('node', '# Node ID %s\n', hex(node))
52670eaa14b4 export: port _exportsingle() to formatter
Yuya Nishihara <yuya@tcha.org>
parents: 37601
diff changeset
  1576
    fm.plain('# Parent  %s\n' % hex(prev))
32433
7feaf5550a9e cmdutil: extract closure that performs the actual export formatting
Augie Fackler <augie@google.com>
parents: 32432
diff changeset
  1577
    if len(parents) > 1:
37602
52670eaa14b4 export: port _exportsingle() to formatter
Yuya Nishihara <yuya@tcha.org>
parents: 37601
diff changeset
  1578
        fm.plain('# Parent  %s\n' % hex(parents[1]))
52670eaa14b4 export: port _exportsingle() to formatter
Yuya Nishihara <yuya@tcha.org>
parents: 37601
diff changeset
  1579
    fm.data(parents=fm.formatlist(pycompat.maplist(hex, parents), name='node'))
52670eaa14b4 export: port _exportsingle() to formatter
Yuya Nishihara <yuya@tcha.org>
parents: 37601
diff changeset
  1580
52670eaa14b4 export: port _exportsingle() to formatter
Yuya Nishihara <yuya@tcha.org>
parents: 37601
diff changeset
  1581
    # TODO: redesign extraexportmap function to support formatter
32433
7feaf5550a9e cmdutil: extract closure that performs the actual export formatting
Augie Fackler <augie@google.com>
parents: 32432
diff changeset
  1582
    for headerid in extraexport:
7feaf5550a9e cmdutil: extract closure that performs the actual export formatting
Augie Fackler <augie@google.com>
parents: 32432
diff changeset
  1583
        header = extraexportmap[headerid](seqno, ctx)
7feaf5550a9e cmdutil: extract closure that performs the actual export formatting
Augie Fackler <augie@google.com>
parents: 32432
diff changeset
  1584
        if header is not None:
37602
52670eaa14b4 export: port _exportsingle() to formatter
Yuya Nishihara <yuya@tcha.org>
parents: 37601
diff changeset
  1585
            fm.plain('# %s\n' % header)
52670eaa14b4 export: port _exportsingle() to formatter
Yuya Nishihara <yuya@tcha.org>
parents: 37601
diff changeset
  1586
52670eaa14b4 export: port _exportsingle() to formatter
Yuya Nishihara <yuya@tcha.org>
parents: 37601
diff changeset
  1587
    fm.write('desc', '%s\n', ctx.description().rstrip())
52670eaa14b4 export: port _exportsingle() to formatter
Yuya Nishihara <yuya@tcha.org>
parents: 37601
diff changeset
  1588
    fm.plain('\n')
52670eaa14b4 export: port _exportsingle() to formatter
Yuya Nishihara <yuya@tcha.org>
parents: 37601
diff changeset
  1589
52670eaa14b4 export: port _exportsingle() to formatter
Yuya Nishihara <yuya@tcha.org>
parents: 37601
diff changeset
  1590
    if fm.isplain():
52670eaa14b4 export: port _exportsingle() to formatter
Yuya Nishihara <yuya@tcha.org>
parents: 37601
diff changeset
  1591
        chunkiter = patch.diffui(repo, prev, node, match, opts=diffopts)
52670eaa14b4 export: port _exportsingle() to formatter
Yuya Nishihara <yuya@tcha.org>
parents: 37601
diff changeset
  1592
        for chunk, label in chunkiter:
52670eaa14b4 export: port _exportsingle() to formatter
Yuya Nishihara <yuya@tcha.org>
parents: 37601
diff changeset
  1593
            fm.plain(chunk, label=label)
52670eaa14b4 export: port _exportsingle() to formatter
Yuya Nishihara <yuya@tcha.org>
parents: 37601
diff changeset
  1594
    else:
52670eaa14b4 export: port _exportsingle() to formatter
Yuya Nishihara <yuya@tcha.org>
parents: 37601
diff changeset
  1595
        chunkiter = patch.diff(repo, prev, node, match, opts=diffopts)
52670eaa14b4 export: port _exportsingle() to formatter
Yuya Nishihara <yuya@tcha.org>
parents: 37601
diff changeset
  1596
        # TODO: make it structured?
52670eaa14b4 export: port _exportsingle() to formatter
Yuya Nishihara <yuya@tcha.org>
parents: 37601
diff changeset
  1597
        fm.data(diff=b''.join(chunkiter))
32433
7feaf5550a9e cmdutil: extract closure that performs the actual export formatting
Augie Fackler <augie@google.com>
parents: 32432
diff changeset
  1598
37604
daafaff4e5be export: enable formatter support (API)
Yuya Nishihara <yuya@tcha.org>
parents: 37603
diff changeset
  1599
def _exportfile(repo, revs, fm, dest, switch_parent, diffopts, match):
37600
7800ed2e4980 export: split cmdutil.export() to single-file and maybe-multiple-files cases
Yuya Nishihara <yuya@tcha.org>
parents: 37599
diff changeset
  1600
    """Export changesets to stdout or a single file"""
7800ed2e4980 export: split cmdutil.export() to single-file and maybe-multiple-files cases
Yuya Nishihara <yuya@tcha.org>
parents: 37599
diff changeset
  1601
    for seqno, rev in enumerate(revs, 1):
7800ed2e4980 export: split cmdutil.export() to single-file and maybe-multiple-files cases
Yuya Nishihara <yuya@tcha.org>
parents: 37599
diff changeset
  1602
        ctx = repo[rev]
7800ed2e4980 export: split cmdutil.export() to single-file and maybe-multiple-files cases
Yuya Nishihara <yuya@tcha.org>
parents: 37599
diff changeset
  1603
        if not dest.startswith('<'):
7800ed2e4980 export: split cmdutil.export() to single-file and maybe-multiple-files cases
Yuya Nishihara <yuya@tcha.org>
parents: 37599
diff changeset
  1604
            repo.ui.note("%s\n" % dest)
37602
52670eaa14b4 export: port _exportsingle() to formatter
Yuya Nishihara <yuya@tcha.org>
parents: 37601
diff changeset
  1605
        fm.startitem()
52670eaa14b4 export: port _exportsingle() to formatter
Yuya Nishihara <yuya@tcha.org>
parents: 37601
diff changeset
  1606
        _exportsingle(repo, ctx, fm, match, switch_parent, seqno, diffopts)
37604
daafaff4e5be export: enable formatter support (API)
Yuya Nishihara <yuya@tcha.org>
parents: 37603
diff changeset
  1607
daafaff4e5be export: enable formatter support (API)
Yuya Nishihara <yuya@tcha.org>
parents: 37603
diff changeset
  1608
def _exportfntemplate(repo, revs, basefm, fntemplate, switch_parent, diffopts,
daafaff4e5be export: enable formatter support (API)
Yuya Nishihara <yuya@tcha.org>
parents: 37603
diff changeset
  1609
                      match):
37600
7800ed2e4980 export: split cmdutil.export() to single-file and maybe-multiple-files cases
Yuya Nishihara <yuya@tcha.org>
parents: 37599
diff changeset
  1610
    """Export changesets to possibly multiple files"""
7800ed2e4980 export: split cmdutil.export() to single-file and maybe-multiple-files cases
Yuya Nishihara <yuya@tcha.org>
parents: 37599
diff changeset
  1611
    total = len(revs)
7800ed2e4980 export: split cmdutil.export() to single-file and maybe-multiple-files cases
Yuya Nishihara <yuya@tcha.org>
parents: 37599
diff changeset
  1612
    revwidth = max(len(str(rev)) for rev in revs)
37601
2e0e61312a25 export: serialize revisions to be exported per destination file
Yuya Nishihara <yuya@tcha.org>
parents: 37600
diff changeset
  1613
    filemap = util.sortdict()  # filename: [(seqno, rev), ...]
37600
7800ed2e4980 export: split cmdutil.export() to single-file and maybe-multiple-files cases
Yuya Nishihara <yuya@tcha.org>
parents: 37599
diff changeset
  1614
7800ed2e4980 export: split cmdutil.export() to single-file and maybe-multiple-files cases
Yuya Nishihara <yuya@tcha.org>
parents: 37599
diff changeset
  1615
    for seqno, rev in enumerate(revs, 1):
7800ed2e4980 export: split cmdutil.export() to single-file and maybe-multiple-files cases
Yuya Nishihara <yuya@tcha.org>
parents: 37599
diff changeset
  1616
        ctx = repo[rev]
37601
2e0e61312a25 export: serialize revisions to be exported per destination file
Yuya Nishihara <yuya@tcha.org>
parents: 37600
diff changeset
  1617
        dest = makefilename(ctx, fntemplate,
2e0e61312a25 export: serialize revisions to be exported per destination file
Yuya Nishihara <yuya@tcha.org>
parents: 37600
diff changeset
  1618
                            total=total, seqno=seqno, revwidth=revwidth)
2e0e61312a25 export: serialize revisions to be exported per destination file
Yuya Nishihara <yuya@tcha.org>
parents: 37600
diff changeset
  1619
        filemap.setdefault(dest, []).append((seqno, rev))
2e0e61312a25 export: serialize revisions to be exported per destination file
Yuya Nishihara <yuya@tcha.org>
parents: 37600
diff changeset
  1620
2e0e61312a25 export: serialize revisions to be exported per destination file
Yuya Nishihara <yuya@tcha.org>
parents: 37600
diff changeset
  1621
    for dest in filemap:
37604
daafaff4e5be export: enable formatter support (API)
Yuya Nishihara <yuya@tcha.org>
parents: 37603
diff changeset
  1622
        with formatter.maybereopen(basefm, dest) as fm:
37601
2e0e61312a25 export: serialize revisions to be exported per destination file
Yuya Nishihara <yuya@tcha.org>
parents: 37600
diff changeset
  1623
            repo.ui.note("%s\n" % dest)
2e0e61312a25 export: serialize revisions to be exported per destination file
Yuya Nishihara <yuya@tcha.org>
parents: 37600
diff changeset
  1624
            for seqno, rev in filemap[dest]:
37602
52670eaa14b4 export: port _exportsingle() to formatter
Yuya Nishihara <yuya@tcha.org>
parents: 37601
diff changeset
  1625
                fm.startitem()
37601
2e0e61312a25 export: serialize revisions to be exported per destination file
Yuya Nishihara <yuya@tcha.org>
parents: 37600
diff changeset
  1626
                ctx = repo[rev]
37602
52670eaa14b4 export: port _exportsingle() to formatter
Yuya Nishihara <yuya@tcha.org>
parents: 37601
diff changeset
  1627
                _exportsingle(repo, ctx, fm, match, switch_parent, seqno,
37601
2e0e61312a25 export: serialize revisions to be exported per destination file
Yuya Nishihara <yuya@tcha.org>
parents: 37600
diff changeset
  1628
                              diffopts)
37600
7800ed2e4980 export: split cmdutil.export() to single-file and maybe-multiple-files cases
Yuya Nishihara <yuya@tcha.org>
parents: 37599
diff changeset
  1629
37604
daafaff4e5be export: enable formatter support (API)
Yuya Nishihara <yuya@tcha.org>
parents: 37603
diff changeset
  1630
def export(repo, revs, basefm, fntemplate='hg-%h.patch', switch_parent=False,
26229
d1530c6e8613 extdiff: enable -I/-X with --patch
Matt Harbison <matt_harbison@yahoo.com>
parents: 26222
diff changeset
  1631
           opts=None, match=None):
32430
1f4be037f558 cmdutil: comprehensively document the interface of export
Augie Fackler <augie@google.com>
parents: 32382
diff changeset
  1632
    '''export changesets as hg patches
1f4be037f558 cmdutil: comprehensively document the interface of export
Augie Fackler <augie@google.com>
parents: 32382
diff changeset
  1633
1f4be037f558 cmdutil: comprehensively document the interface of export
Augie Fackler <augie@google.com>
parents: 32382
diff changeset
  1634
    Args:
1f4be037f558 cmdutil: comprehensively document the interface of export
Augie Fackler <augie@google.com>
parents: 32382
diff changeset
  1635
      repo: The repository from which we're exporting revisions.
1f4be037f558 cmdutil: comprehensively document the interface of export
Augie Fackler <augie@google.com>
parents: 32382
diff changeset
  1636
      revs: A list of revisions to export as revision numbers.
37604
daafaff4e5be export: enable formatter support (API)
Yuya Nishihara <yuya@tcha.org>
parents: 37603
diff changeset
  1637
      basefm: A formatter to which patches should be written.
32431
9fd9f91b0c43 cmdutil: rename template param to export to fntemplate
Augie Fackler <augie@google.com>
parents: 32430
diff changeset
  1638
      fntemplate: An optional string to use for generating patch file names.
32430
1f4be037f558 cmdutil: comprehensively document the interface of export
Augie Fackler <augie@google.com>
parents: 32382
diff changeset
  1639
      switch_parent: If True, show diffs against second parent when not nullid.
1f4be037f558 cmdutil: comprehensively document the interface of export
Augie Fackler <augie@google.com>
parents: 32382
diff changeset
  1640
                     Default is false, which always shows diff against p1.
1f4be037f558 cmdutil: comprehensively document the interface of export
Augie Fackler <augie@google.com>
parents: 32382
diff changeset
  1641
      opts: diff options to use for generating the patch.
1f4be037f558 cmdutil: comprehensively document the interface of export
Augie Fackler <augie@google.com>
parents: 32382
diff changeset
  1642
      match: If specified, only export changes to files matching this matcher.
1f4be037f558 cmdutil: comprehensively document the interface of export
Augie Fackler <augie@google.com>
parents: 32382
diff changeset
  1643
1f4be037f558 cmdutil: comprehensively document the interface of export
Augie Fackler <augie@google.com>
parents: 32382
diff changeset
  1644
    Returns:
1f4be037f558 cmdutil: comprehensively document the interface of export
Augie Fackler <augie@google.com>
parents: 32382
diff changeset
  1645
      Nothing.
1f4be037f558 cmdutil: comprehensively document the interface of export
Augie Fackler <augie@google.com>
parents: 32382
diff changeset
  1646
1f4be037f558 cmdutil: comprehensively document the interface of export
Augie Fackler <augie@google.com>
parents: 32382
diff changeset
  1647
    Side Effect:
1f4be037f558 cmdutil: comprehensively document the interface of export
Augie Fackler <augie@google.com>
parents: 32382
diff changeset
  1648
      "HG Changeset Patch" data is emitted to one of the following
1f4be037f558 cmdutil: comprehensively document the interface of export
Augie Fackler <augie@google.com>
parents: 32382
diff changeset
  1649
      destinations:
32431
9fd9f91b0c43 cmdutil: rename template param to export to fntemplate
Augie Fackler <augie@google.com>
parents: 32430
diff changeset
  1650
        fntemplate specified: Each rev is written to a unique file named using
32430
1f4be037f558 cmdutil: comprehensively document the interface of export
Augie Fackler <augie@google.com>
parents: 32382
diff changeset
  1651
                            the given template.
37604
daafaff4e5be export: enable formatter support (API)
Yuya Nishihara <yuya@tcha.org>
parents: 37603
diff changeset
  1652
        Otherwise: All revs will be written to basefm.
32430
1f4be037f558 cmdutil: comprehensively document the interface of export
Augie Fackler <augie@google.com>
parents: 32382
diff changeset
  1653
    '''
37763
b54404d66f7e export: invoke the file prefetch hook
Matt Harbison <matt_harbison@yahoo.com>
parents: 37762
diff changeset
  1654
    scmutil.prefetchfiles(repo, revs, match)
b54404d66f7e export: invoke the file prefetch hook
Matt Harbison <matt_harbison@yahoo.com>
parents: 37762
diff changeset
  1655
37603
678d760c71ff export: extract function to write patch to file object (API)
Yuya Nishihara <yuya@tcha.org>
parents: 37602
diff changeset
  1656
    if not fntemplate:
37604
daafaff4e5be export: enable formatter support (API)
Yuya Nishihara <yuya@tcha.org>
parents: 37603
diff changeset
  1657
        _exportfile(repo, revs, basefm, '<unnamed>', switch_parent, opts, match)
37600
7800ed2e4980 export: split cmdutil.export() to single-file and maybe-multiple-files cases
Yuya Nishihara <yuya@tcha.org>
parents: 37599
diff changeset
  1658
    else:
37604
daafaff4e5be export: enable formatter support (API)
Yuya Nishihara <yuya@tcha.org>
parents: 37603
diff changeset
  1659
        _exportfntemplate(repo, revs, basefm, fntemplate, switch_parent, opts,
daafaff4e5be export: enable formatter support (API)
Yuya Nishihara <yuya@tcha.org>
parents: 37603
diff changeset
  1660
                          match)
10611
e764f24a45ee patch/diff: move patch.export() to cmdutil.export()
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10608
diff changeset
  1661
37603
678d760c71ff export: extract function to write patch to file object (API)
Yuya Nishihara <yuya@tcha.org>
parents: 37602
diff changeset
  1662
def exportfile(repo, revs, fp, switch_parent=False, opts=None, match=None):
678d760c71ff export: extract function to write patch to file object (API)
Yuya Nishihara <yuya@tcha.org>
parents: 37602
diff changeset
  1663
    """Export changesets to the given file stream"""
37763
b54404d66f7e export: invoke the file prefetch hook
Matt Harbison <matt_harbison@yahoo.com>
parents: 37762
diff changeset
  1664
    scmutil.prefetchfiles(repo, revs, match)
b54404d66f7e export: invoke the file prefetch hook
Matt Harbison <matt_harbison@yahoo.com>
parents: 37762
diff changeset
  1665
37604
daafaff4e5be export: enable formatter support (API)
Yuya Nishihara <yuya@tcha.org>
parents: 37603
diff changeset
  1666
    dest = getattr(fp, 'name', '<unnamed>')
daafaff4e5be export: enable formatter support (API)
Yuya Nishihara <yuya@tcha.org>
parents: 37603
diff changeset
  1667
    with formatter.formatter(repo.ui, fp, 'export', {}) as fm:
daafaff4e5be export: enable formatter support (API)
Yuya Nishihara <yuya@tcha.org>
parents: 37603
diff changeset
  1668
        _exportfile(repo, revs, fm, dest, switch_parent, opts, match)
37603
678d760c71ff export: extract function to write patch to file object (API)
Yuya Nishihara <yuya@tcha.org>
parents: 37602
diff changeset
  1669
29795
142ae01820a3 debugobsolete: add formatter support (issue5134)
Yuya Nishihara <yuya@tcha.org>
parents: 29758
diff changeset
  1670
def showmarker(fm, marker, index=None):
20470
78f4c2b7052f debugobsolete: extract marker display in a dedicated function
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 20392
diff changeset
  1671
    """utility function to display obsolescence marker in a readable way
78f4c2b7052f debugobsolete: extract marker display in a dedicated function
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 20392
diff changeset
  1672
78f4c2b7052f debugobsolete: extract marker display in a dedicated function
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 20392
diff changeset
  1673
    To be used by debug function."""
28613
6433da9c96a9 debugobsolete: add an option to show marker index
Kostia Balytskyi <ikostia@fb.com>
parents: 28608
diff changeset
  1674
    if index is not None:
29795
142ae01820a3 debugobsolete: add formatter support (issue5134)
Yuya Nishihara <yuya@tcha.org>
parents: 29758
diff changeset
  1675
        fm.write('index', '%i ', index)
33856
eae63a9e59da obsmarker: precnode was renamed into prednode
Boris Feld <boris.feld@octobus.net>
parents: 33824
diff changeset
  1676
    fm.write('prednode', '%s ', hex(marker.prednode()))
29795
142ae01820a3 debugobsolete: add formatter support (issue5134)
Yuya Nishihara <yuya@tcha.org>
parents: 29758
diff changeset
  1677
    succs = marker.succnodes()
142ae01820a3 debugobsolete: add formatter support (issue5134)
Yuya Nishihara <yuya@tcha.org>
parents: 29758
diff changeset
  1678
    fm.condwrite(succs, 'succnodes', '%s ',
142ae01820a3 debugobsolete: add formatter support (issue5134)
Yuya Nishihara <yuya@tcha.org>
parents: 29758
diff changeset
  1679
                 fm.formatlist(map(hex, succs), name='node'))
142ae01820a3 debugobsolete: add formatter support (issue5134)
Yuya Nishihara <yuya@tcha.org>
parents: 29758
diff changeset
  1680
    fm.write('flag', '%X ', marker.flags())
22260
2229d757802d debugobsolete: display parents information from markers
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22250
diff changeset
  1681
    parents = marker.parentnodes()
2229d757802d debugobsolete: display parents information from markers
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22250
diff changeset
  1682
    if parents is not None:
29795
142ae01820a3 debugobsolete: add formatter support (issue5134)
Yuya Nishihara <yuya@tcha.org>
parents: 29758
diff changeset
  1683
        fm.write('parentnodes', '{%s} ',
142ae01820a3 debugobsolete: add formatter support (issue5134)
Yuya Nishihara <yuya@tcha.org>
parents: 29758
diff changeset
  1684
                 fm.formatlist(map(hex, parents), name='node', sep=', '))
142ae01820a3 debugobsolete: add formatter support (issue5134)
Yuya Nishihara <yuya@tcha.org>
parents: 29758
diff changeset
  1685
    fm.write('date', '(%s) ', fm.formatdate(marker.date()))
142ae01820a3 debugobsolete: add formatter support (issue5134)
Yuya Nishihara <yuya@tcha.org>
parents: 29758
diff changeset
  1686
    meta = marker.metadata().copy()
142ae01820a3 debugobsolete: add formatter support (issue5134)
Yuya Nishihara <yuya@tcha.org>
parents: 29758
diff changeset
  1687
    meta.pop('date', None)
35904
fc44c2657dc5 py3: drop b'' from repr() of smartset
Yuya Nishihara <yuya@tcha.org>
parents: 35903
diff changeset
  1688
    smeta = util.rapply(pycompat.maybebytestr, meta)
35874
1bee7762fd46 cmdutil: add a kludge to make bytes repr() the same on 2 and 3
Augie Fackler <augie@google.com>
parents: 35746
diff changeset
  1689
    fm.write('metadata', '{%s}', fm.formatdict(smeta, fmt='%r: %r', sep=', '))
29795
142ae01820a3 debugobsolete: add formatter support (issue5134)
Yuya Nishihara <yuya@tcha.org>
parents: 29758
diff changeset
  1690
    fm.plain('\n')
20470
78f4c2b7052f debugobsolete: extract marker display in a dedicated function
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 20392
diff changeset
  1691
3814
120be84f33de Add --date support to update and revert
Matt Mackall <mpm@selenic.com>
parents: 3738
diff changeset
  1692
def finddate(ui, repo, date):
120be84f33de Add --date support to update and revert
Matt Mackall <mpm@selenic.com>
parents: 3738
diff changeset
  1693
    """Find the tipmost changeset that matches the given date spec"""
9667
8743f2e1bc54 merge changes from mpm
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 9666 9665
diff changeset
  1694
36607
c6061cadb400 util: extract all date-related utils in utils/dateutil module
Boris Feld <boris.feld@octobus.net>
parents: 36511
diff changeset
  1695
    df = dateutil.matchdate(date)
14322
a90131b85fd8 scmutil: drop aliases in cmdutil for match functions
Matt Mackall <mpm@selenic.com>
parents: 14321
diff changeset
  1696
    m = scmutil.matchall(repo)
3814
120be84f33de Add --date support to update and revert
Matt Mackall <mpm@selenic.com>
parents: 3738
diff changeset
  1697
    results = {}
9662
f3d60543924f walkchangerevs: move 'add' to callback
Matt Mackall <mpm@selenic.com>
parents: 9656
diff changeset
  1698
f3d60543924f walkchangerevs: move 'add' to callback
Matt Mackall <mpm@selenic.com>
parents: 9656
diff changeset
  1699
    def prep(ctx, fns):
f3d60543924f walkchangerevs: move 'add' to callback
Matt Mackall <mpm@selenic.com>
parents: 9656
diff changeset
  1700
        d = ctx.date()
f3d60543924f walkchangerevs: move 'add' to callback
Matt Mackall <mpm@selenic.com>
parents: 9656
diff changeset
  1701
        if df(d[0]):
9668
2c24471d478c cmdutil: fix bug in finddate() implementation
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 9667
diff changeset
  1702
            results[ctx.rev()] = d
9662
f3d60543924f walkchangerevs: move 'add' to callback
Matt Mackall <mpm@selenic.com>
parents: 9656
diff changeset
  1703
9667
8743f2e1bc54 merge changes from mpm
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 9666 9665
diff changeset
  1704
    for ctx in walkchangerevs(repo, m, {'rev': None}, prep):
9662
f3d60543924f walkchangerevs: move 'add' to callback
Matt Mackall <mpm@selenic.com>
parents: 9656
diff changeset
  1705
        rev = ctx.rev()
f3d60543924f walkchangerevs: move 'add' to callback
Matt Mackall <mpm@selenic.com>
parents: 9656
diff changeset
  1706
        if rev in results:
16937
5487088f0d43 cmdutil: lowercase finddate status message
Martin Geisler <mg@aragost.com>
parents: 16776
diff changeset
  1707
            ui.status(_("found revision %s from %s\n") %
36607
c6061cadb400 util: extract all date-related utils in utils/dateutil module
Boris Feld <boris.feld@octobus.net>
parents: 36511
diff changeset
  1708
                      (rev, dateutil.datestr(results[rev])))
32155
055cca8e167b py3: use %d to format integers into bytestrings
Pulkit Goyal <7895pulkit@gmail.com>
parents: 32153
diff changeset
  1709
            return '%d' % rev
3814
120be84f33de Add --date support to update and revert
Matt Mackall <mpm@selenic.com>
parents: 3738
diff changeset
  1710
26587
56b2bcea2529 error: get Abort from 'error' instead of 'util'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26579
diff changeset
  1711
    raise error.Abort(_("revision matching date not found"))
3814
120be84f33de Add --date support to update and revert
Matt Mackall <mpm@selenic.com>
parents: 3738
diff changeset
  1712
20553
86cefb15e7b5 cmdutil: implemented new lazy increasingwindows
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20500
diff changeset
  1713
def increasingwindows(windowsize=8, sizelimit=512):
86cefb15e7b5 cmdutil: implemented new lazy increasingwindows
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20500
diff changeset
  1714
    while True:
86cefb15e7b5 cmdutil: implemented new lazy increasingwindows
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20500
diff changeset
  1715
        yield windowsize
86cefb15e7b5 cmdutil: implemented new lazy increasingwindows
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20500
diff changeset
  1716
        if windowsize < sizelimit:
86cefb15e7b5 cmdutil: implemented new lazy increasingwindows
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20500
diff changeset
  1717
            windowsize *= 2
16776
5088d0b9a9a1 cmdutil: extract increasing_windows() from walkchangerevs()
Patrick Mezard <patrick@mezard.eu>
parents: 16701
diff changeset
  1718
35682
8273c1a47282 log: duplicate _logrevs() dedicated for walkchangerevs()
Yuya Nishihara <yuya@tcha.org>
parents: 35662
diff changeset
  1719
def _walkrevs(repo, opts):
8273c1a47282 log: duplicate _logrevs() dedicated for walkchangerevs()
Yuya Nishihara <yuya@tcha.org>
parents: 35662
diff changeset
  1720
    # Default --rev value depends on --follow but --follow behavior
8273c1a47282 log: duplicate _logrevs() dedicated for walkchangerevs()
Yuya Nishihara <yuya@tcha.org>
parents: 35662
diff changeset
  1721
    # depends on revisions resolved from --rev...
8273c1a47282 log: duplicate _logrevs() dedicated for walkchangerevs()
Yuya Nishihara <yuya@tcha.org>
parents: 35662
diff changeset
  1722
    follow = opts.get('follow') or opts.get('follow_first')
8273c1a47282 log: duplicate _logrevs() dedicated for walkchangerevs()
Yuya Nishihara <yuya@tcha.org>
parents: 35662
diff changeset
  1723
    if opts.get('rev'):
8273c1a47282 log: duplicate _logrevs() dedicated for walkchangerevs()
Yuya Nishihara <yuya@tcha.org>
parents: 35662
diff changeset
  1724
        revs = scmutil.revrange(repo, opts['rev'])
8273c1a47282 log: duplicate _logrevs() dedicated for walkchangerevs()
Yuya Nishihara <yuya@tcha.org>
parents: 35662
diff changeset
  1725
    elif follow and repo.dirstate.p1() == nullid:
8273c1a47282 log: duplicate _logrevs() dedicated for walkchangerevs()
Yuya Nishihara <yuya@tcha.org>
parents: 35662
diff changeset
  1726
        revs = smartset.baseset()
8273c1a47282 log: duplicate _logrevs() dedicated for walkchangerevs()
Yuya Nishihara <yuya@tcha.org>
parents: 35662
diff changeset
  1727
    elif follow:
8273c1a47282 log: duplicate _logrevs() dedicated for walkchangerevs()
Yuya Nishihara <yuya@tcha.org>
parents: 35662
diff changeset
  1728
        revs = repo.revs('reverse(:.)')
8273c1a47282 log: duplicate _logrevs() dedicated for walkchangerevs()
Yuya Nishihara <yuya@tcha.org>
parents: 35662
diff changeset
  1729
    else:
8273c1a47282 log: duplicate _logrevs() dedicated for walkchangerevs()
Yuya Nishihara <yuya@tcha.org>
parents: 35662
diff changeset
  1730
        revs = smartset.spanset(repo)
8273c1a47282 log: duplicate _logrevs() dedicated for walkchangerevs()
Yuya Nishihara <yuya@tcha.org>
parents: 35662
diff changeset
  1731
        revs.reverse()
8273c1a47282 log: duplicate _logrevs() dedicated for walkchangerevs()
Yuya Nishihara <yuya@tcha.org>
parents: 35662
diff changeset
  1732
    return revs
8273c1a47282 log: duplicate _logrevs() dedicated for walkchangerevs()
Yuya Nishihara <yuya@tcha.org>
parents: 35662
diff changeset
  1733
19290
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1734
class FileWalkError(Exception):
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1735
    pass
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1736
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1737
def walkfilerevs(repo, match, follow, revs, fncache):
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1738
    '''Walks the file history for the matched files.
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1739
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1740
    Returns the changeset revs that are involved in the file history.
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1741
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1742
    Throws FileWalkError if the file history can't be walked using
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1743
    filelogs alone.
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1744
    '''
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1745
    wanted = set()
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1746
    copies = []
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1747
    minrev, maxrev = min(revs), max(revs)
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1748
    def filerevgen(filelog, last):
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1749
        """
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1750
        Only files, no patterns.  Check the history of each file.
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1751
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1752
        Examines filelog entries within minrev, maxrev linkrev range
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1753
        Returns an iterator yielding (linkrev, parentlinkrevs, copied)
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1754
        tuples in backwards order
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1755
        """
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1756
        cl_count = len(repo)
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1757
        revs = []
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1758
        for j in xrange(0, last + 1):
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1759
            linkrev = filelog.linkrev(j)
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1760
            if linkrev < minrev:
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1761
                continue
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1762
            # only yield rev for which we have the changelog, it can
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1763
            # happen while doing "hg log" during a pull or commit
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1764
            if linkrev >= cl_count:
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1765
                break
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1766
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1767
            parentlinkrevs = []
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1768
            for p in filelog.parentrevs(j):
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1769
                if p != nullrev:
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1770
                    parentlinkrevs.append(filelog.linkrev(p))
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1771
            n = filelog.node(j)
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1772
            revs.append((linkrev, parentlinkrevs,
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1773
                         follow and filelog.renamed(n)))
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1774
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1775
        return reversed(revs)
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1776
    def iterfiles():
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1777
        pctx = repo['.']
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1778
        for filename in match.files():
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1779
            if follow:
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1780
                if filename not in pctx:
26587
56b2bcea2529 error: get Abort from 'error' instead of 'util'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26579
diff changeset
  1781
                    raise error.Abort(_('cannot follow file not in parent '
19290
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1782
                                       'revision: "%s"') % filename)
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1783
                yield filename, pctx[filename].filenode()
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1784
            else:
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1785
                yield filename, None
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1786
        for filename_node in copies:
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1787
            yield filename_node
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1788
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1789
    for file_, node in iterfiles():
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1790
        filelog = repo.file(file_)
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1791
        if not len(filelog):
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1792
            if node is None:
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1793
                # A zero count may be a directory or deleted file, so
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1794
                # try to find matching entries on the slow path.
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1795
                if follow:
26587
56b2bcea2529 error: get Abort from 'error' instead of 'util'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26579
diff changeset
  1796
                    raise error.Abort(
19290
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1797
                        _('cannot follow nonexistent file: "%s"') % file_)
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1798
                raise FileWalkError("Cannot walk via filelog")
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1799
            else:
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1800
                continue
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1801
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1802
        if node is None:
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1803
            last = len(filelog) - 1
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1804
        else:
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1805
            last = filelog.rev(node)
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1806
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1807
        # keep track of all ancestors of the file
32291
bd872f64a8ba cleanup: use set literals
Martin von Zweigbergk <martinvonz@google.com>
parents: 32289
diff changeset
  1808
        ancestors = {filelog.linkrev(last)}
19290
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1809
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1810
        # iterate from latest to oldest revision
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1811
        for rev, flparentlinkrevs, copied in filerevgen(filelog, last):
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1812
            if not follow:
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1813
                if rev > maxrev:
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1814
                    continue
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1815
            else:
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1816
                # Note that last might not be the first interesting
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1817
                # rev to us:
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1818
                # if the file has been changed after maxrev, we'll
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1819
                # have linkrev(last) > maxrev, and we still need
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1820
                # to explore the file graph
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1821
                if rev not in ancestors:
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1822
                    continue
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1823
                # XXX insert 1327 fix here
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1824
                if flparentlinkrevs:
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1825
                    ancestors.update(flparentlinkrevs)
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1826
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1827
            fncache.setdefault(rev, []).append(file_)
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1828
            wanted.add(rev)
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1829
            if copied:
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1830
                copies.append(copied)
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1831
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1832
    return wanted
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1833
24391
6c3a93e690c7 walkchangerevs: make followfilter a top-level class
Martin von Zweigbergk <martinvonz@google.com>
parents: 24384
diff changeset
  1834
class _followfilter(object):
6c3a93e690c7 walkchangerevs: make followfilter a top-level class
Martin von Zweigbergk <martinvonz@google.com>
parents: 24384
diff changeset
  1835
    def __init__(self, repo, onlyfirst=False):
6c3a93e690c7 walkchangerevs: make followfilter a top-level class
Martin von Zweigbergk <martinvonz@google.com>
parents: 24384
diff changeset
  1836
        self.repo = repo
6c3a93e690c7 walkchangerevs: make followfilter a top-level class
Martin von Zweigbergk <martinvonz@google.com>
parents: 24384
diff changeset
  1837
        self.startrev = nullrev
6c3a93e690c7 walkchangerevs: make followfilter a top-level class
Martin von Zweigbergk <martinvonz@google.com>
parents: 24384
diff changeset
  1838
        self.roots = set()
6c3a93e690c7 walkchangerevs: make followfilter a top-level class
Martin von Zweigbergk <martinvonz@google.com>
parents: 24384
diff changeset
  1839
        self.onlyfirst = onlyfirst
6c3a93e690c7 walkchangerevs: make followfilter a top-level class
Martin von Zweigbergk <martinvonz@google.com>
parents: 24384
diff changeset
  1840
6c3a93e690c7 walkchangerevs: make followfilter a top-level class
Martin von Zweigbergk <martinvonz@google.com>
parents: 24384
diff changeset
  1841
    def match(self, rev):
6c3a93e690c7 walkchangerevs: make followfilter a top-level class
Martin von Zweigbergk <martinvonz@google.com>
parents: 24384
diff changeset
  1842
        def realparents(rev):
6c3a93e690c7 walkchangerevs: make followfilter a top-level class
Martin von Zweigbergk <martinvonz@google.com>
parents: 24384
diff changeset
  1843
            if self.onlyfirst:
6c3a93e690c7 walkchangerevs: make followfilter a top-level class
Martin von Zweigbergk <martinvonz@google.com>
parents: 24384
diff changeset
  1844
                return self.repo.changelog.parentrevs(rev)[0:1]
6c3a93e690c7 walkchangerevs: make followfilter a top-level class
Martin von Zweigbergk <martinvonz@google.com>
parents: 24384
diff changeset
  1845
            else:
6c3a93e690c7 walkchangerevs: make followfilter a top-level class
Martin von Zweigbergk <martinvonz@google.com>
parents: 24384
diff changeset
  1846
                return filter(lambda x: x != nullrev,
6c3a93e690c7 walkchangerevs: make followfilter a top-level class
Martin von Zweigbergk <martinvonz@google.com>
parents: 24384
diff changeset
  1847
                              self.repo.changelog.parentrevs(rev))
6c3a93e690c7 walkchangerevs: make followfilter a top-level class
Martin von Zweigbergk <martinvonz@google.com>
parents: 24384
diff changeset
  1848
6c3a93e690c7 walkchangerevs: make followfilter a top-level class
Martin von Zweigbergk <martinvonz@google.com>
parents: 24384
diff changeset
  1849
        if self.startrev == nullrev:
6c3a93e690c7 walkchangerevs: make followfilter a top-level class
Martin von Zweigbergk <martinvonz@google.com>
parents: 24384
diff changeset
  1850
            self.startrev = rev
6c3a93e690c7 walkchangerevs: make followfilter a top-level class
Martin von Zweigbergk <martinvonz@google.com>
parents: 24384
diff changeset
  1851
            return True
6c3a93e690c7 walkchangerevs: make followfilter a top-level class
Martin von Zweigbergk <martinvonz@google.com>
parents: 24384
diff changeset
  1852
6c3a93e690c7 walkchangerevs: make followfilter a top-level class
Martin von Zweigbergk <martinvonz@google.com>
parents: 24384
diff changeset
  1853
        if rev > self.startrev:
6c3a93e690c7 walkchangerevs: make followfilter a top-level class
Martin von Zweigbergk <martinvonz@google.com>
parents: 24384
diff changeset
  1854
            # forward: all descendants
6c3a93e690c7 walkchangerevs: make followfilter a top-level class
Martin von Zweigbergk <martinvonz@google.com>
parents: 24384
diff changeset
  1855
            if not self.roots:
6c3a93e690c7 walkchangerevs: make followfilter a top-level class
Martin von Zweigbergk <martinvonz@google.com>
parents: 24384
diff changeset
  1856
                self.roots.add(self.startrev)
6c3a93e690c7 walkchangerevs: make followfilter a top-level class
Martin von Zweigbergk <martinvonz@google.com>
parents: 24384
diff changeset
  1857
            for parent in realparents(rev):
6c3a93e690c7 walkchangerevs: make followfilter a top-level class
Martin von Zweigbergk <martinvonz@google.com>
parents: 24384
diff changeset
  1858
                if parent in self.roots:
6c3a93e690c7 walkchangerevs: make followfilter a top-level class
Martin von Zweigbergk <martinvonz@google.com>
parents: 24384
diff changeset
  1859
                    self.roots.add(rev)
6c3a93e690c7 walkchangerevs: make followfilter a top-level class
Martin von Zweigbergk <martinvonz@google.com>
parents: 24384
diff changeset
  1860
                    return True
6c3a93e690c7 walkchangerevs: make followfilter a top-level class
Martin von Zweigbergk <martinvonz@google.com>
parents: 24384
diff changeset
  1861
        else:
6c3a93e690c7 walkchangerevs: make followfilter a top-level class
Martin von Zweigbergk <martinvonz@google.com>
parents: 24384
diff changeset
  1862
            # backwards: all parents
6c3a93e690c7 walkchangerevs: make followfilter a top-level class
Martin von Zweigbergk <martinvonz@google.com>
parents: 24384
diff changeset
  1863
            if not self.roots:
6c3a93e690c7 walkchangerevs: make followfilter a top-level class
Martin von Zweigbergk <martinvonz@google.com>
parents: 24384
diff changeset
  1864
                self.roots.update(realparents(self.startrev))
6c3a93e690c7 walkchangerevs: make followfilter a top-level class
Martin von Zweigbergk <martinvonz@google.com>
parents: 24384
diff changeset
  1865
            if rev in self.roots:
6c3a93e690c7 walkchangerevs: make followfilter a top-level class
Martin von Zweigbergk <martinvonz@google.com>
parents: 24384
diff changeset
  1866
                self.roots.remove(rev)
6c3a93e690c7 walkchangerevs: make followfilter a top-level class
Martin von Zweigbergk <martinvonz@google.com>
parents: 24384
diff changeset
  1867
                self.roots.update(realparents(rev))
6c3a93e690c7 walkchangerevs: make followfilter a top-level class
Martin von Zweigbergk <martinvonz@google.com>
parents: 24384
diff changeset
  1868
                return True
6c3a93e690c7 walkchangerevs: make followfilter a top-level class
Martin von Zweigbergk <martinvonz@google.com>
parents: 24384
diff changeset
  1869
6c3a93e690c7 walkchangerevs: make followfilter a top-level class
Martin von Zweigbergk <martinvonz@google.com>
parents: 24384
diff changeset
  1870
        return False
6c3a93e690c7 walkchangerevs: make followfilter a top-level class
Martin von Zweigbergk <martinvonz@google.com>
parents: 24384
diff changeset
  1871
9665
1de5ebfa5585 walkchangerevs: drop ui arg
Matt Mackall <mpm@selenic.com>
parents: 9664
diff changeset
  1872
def walkchangerevs(repo, match, opts, prepare):
7807
bd8f44638847 help: miscellaneous language fixes
timeless <timeless@gmail.com>
parents: 7779
diff changeset
  1873
    '''Iterate over files and the revs in which they changed.
3650
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1874
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1875
    Callers most commonly need to iterate backwards over the history
7807
bd8f44638847 help: miscellaneous language fixes
timeless <timeless@gmail.com>
parents: 7779
diff changeset
  1876
    in which they are interested. Doing so has awful (quadratic-looking)
3650
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1877
    performance, so we use iterators in a "windowed" way.
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1878
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1879
    We walk a window of revisions in the desired order.  Within the
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1880
    window, we first walk forwards to gather data, then in the desired
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1881
    order (usually backwards) to display it.
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1882
9662
f3d60543924f walkchangerevs: move 'add' to callback
Matt Mackall <mpm@selenic.com>
parents: 9656
diff changeset
  1883
    This function returns an iterator yielding contexts. Before
f3d60543924f walkchangerevs: move 'add' to callback
Matt Mackall <mpm@selenic.com>
parents: 9656
diff changeset
  1884
    yielding each context, the iterator will first call the prepare
f3d60543924f walkchangerevs: move 'add' to callback
Matt Mackall <mpm@selenic.com>
parents: 9656
diff changeset
  1885
    function on each context in the window in forward order.'''
3650
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1886
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1887
    follow = opts.get('follow') or opts.get('follow_first')
35682
8273c1a47282 log: duplicate _logrevs() dedicated for walkchangerevs()
Yuya Nishihara <yuya@tcha.org>
parents: 35662
diff changeset
  1888
    revs = _walkrevs(repo, opts)
11281
b724b8467b82 walkchangerevs: allow empty query sets
Matt Mackall <mpm@selenic.com>
parents: 11277
diff changeset
  1889
    if not revs:
b724b8467b82 walkchangerevs: allow empty query sets
Matt Mackall <mpm@selenic.com>
parents: 11277
diff changeset
  1890
        return []
8152
08e1baf924ca replace set-like dictionaries with real sets
Martin Geisler <mg@lazybytes.net>
parents: 8119
diff changeset
  1891
    wanted = set()
35458
5bec509dc1ff log: make "slowpath" condition slightly more readable
Yuya Nishihara <yuya@tcha.org>
parents: 35454
diff changeset
  1892
    slowpath = match.anypats() or (not match.always() and opts.get('removed'))
3650
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1893
    fncache = {}
37303
8823615f68a5 log: remove dependence on repo.changectx()
Martin von Zweigbergk <martinvonz@google.com>
parents: 37270
diff changeset
  1894
    change = repo.__getitem__
3650
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1895
11632
f418d2570920 log: document the different phases in walkchangerevs
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 11631
diff changeset
  1896
    # First step is to fill wanted, the set of revisions that we want to yield.
f418d2570920 log: document the different phases in walkchangerevs
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 11631
diff changeset
  1897
    # When it does not induce extra cost, we also fill fncache for revisions in
f418d2570920 log: document the different phases in walkchangerevs
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 11631
diff changeset
  1898
    # wanted: a cache of filenames that were changed (ctx.files()) and that
f418d2570920 log: document the different phases in walkchangerevs
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 11631
diff changeset
  1899
    # match the file filtering conditions.
f418d2570920 log: document the different phases in walkchangerevs
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 11631
diff changeset
  1900
24384
5cb459dc32d2 walkchangerevs: simplify by using match.always() method
Martin von Zweigbergk <martinvonz@google.com>
parents: 24379
diff changeset
  1901
    if match.always():
3650
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1902
        # No files, no patterns.  Display all revs.
20553
86cefb15e7b5 cmdutil: implemented new lazy increasingwindows
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20500
diff changeset
  1903
        wanted = revs
25271
1b1eaf1885df walkchangerevs: simplify with an 'elif'
Martin von Zweigbergk <martinvonz@google.com>
parents: 25259
diff changeset
  1904
    elif not slowpath:
11632
f418d2570920 log: document the different phases in walkchangerevs
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 11631
diff changeset
  1905
        # We only have to read through the filelog to find wanted revisions
f418d2570920 log: document the different phases in walkchangerevs
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 11631
diff changeset
  1906
19290
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1907
        try:
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1908
            wanted = walkfilerevs(repo, match, follow, revs, fncache)
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1909
        except FileWalkError:
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1910
            slowpath = True
11608
183e63112698 log: remove increasing windows usage in fastpath
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 11607
diff changeset
  1911
19290
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1912
            # We decided to fall back to the slowpath because at least one
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1913
            # of the paths was not a file. Check to see if at least one of them
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1914
            # existed in history, otherwise simply return
17746
6d218e47cf9b log: speed up hg log for untracked files (issue1340)
smuralid
parents: 17676
diff changeset
  1915
            for path in match.files():
6d218e47cf9b log: speed up hg log for untracked files (issue1340)
smuralid
parents: 17676
diff changeset
  1916
                if path == '.' or path in repo.store:
6d218e47cf9b log: speed up hg log for untracked files (issue1340)
smuralid
parents: 17676
diff changeset
  1917
                    break
18340
8802277c40ee log: make log work even if first parameter doesn't exist
Mads Kiilerich <mads@kiilerich.com>
parents: 18267
diff changeset
  1918
            else:
8802277c40ee log: make log work even if first parameter doesn't exist
Mads Kiilerich <mads@kiilerich.com>
parents: 18267
diff changeset
  1919
                return []
17746
6d218e47cf9b log: speed up hg log for untracked files (issue1340)
smuralid
parents: 17676
diff changeset
  1920
3650
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1921
    if slowpath:
11632
f418d2570920 log: document the different phases in walkchangerevs
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 11631
diff changeset
  1922
        # We have to read the changelog to match filenames against
f418d2570920 log: document the different phases in walkchangerevs
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 11631
diff changeset
  1923
        # changed files
f418d2570920 log: document the different phases in walkchangerevs
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 11631
diff changeset
  1924
3650
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1925
        if follow:
26587
56b2bcea2529 error: get Abort from 'error' instead of 'util'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26579
diff changeset
  1926
            raise error.Abort(_('can only follow copies/renames for explicit '
8761
0289f384e1e5 Generally replace "file name" with "filename" in help and comments.
timeless <timeless@gmail.com>
parents: 8731
diff changeset
  1927
                               'filenames'))
3650
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1928
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1929
        # The slow path checks files modified in every changeset.
19730
d184bae667e4 log: make file log slow path usable on large repos
Durham Goode <durham@fb.com>
parents: 19511
diff changeset
  1930
        # This is really slow on large repos, so compute the set lazily.
d184bae667e4 log: make file log slow path usable on large repos
Durham Goode <durham@fb.com>
parents: 19511
diff changeset
  1931
        class lazywantedset(object):
d184bae667e4 log: make file log slow path usable on large repos
Durham Goode <durham@fb.com>
parents: 19511
diff changeset
  1932
            def __init__(self):
d184bae667e4 log: make file log slow path usable on large repos
Durham Goode <durham@fb.com>
parents: 19511
diff changeset
  1933
                self.set = set()
d184bae667e4 log: make file log slow path usable on large repos
Durham Goode <durham@fb.com>
parents: 19511
diff changeset
  1934
                self.revs = set(revs)
d184bae667e4 log: make file log slow path usable on large repos
Durham Goode <durham@fb.com>
parents: 19511
diff changeset
  1935
d184bae667e4 log: make file log slow path usable on large repos
Durham Goode <durham@fb.com>
parents: 19511
diff changeset
  1936
            # No need to worry about locality here because it will be accessed
d184bae667e4 log: make file log slow path usable on large repos
Durham Goode <durham@fb.com>
parents: 19511
diff changeset
  1937
            # in the same order as the increasing window below.
d184bae667e4 log: make file log slow path usable on large repos
Durham Goode <durham@fb.com>
parents: 19511
diff changeset
  1938
            def __contains__(self, value):
d184bae667e4 log: make file log slow path usable on large repos
Durham Goode <durham@fb.com>
parents: 19511
diff changeset
  1939
                if value in self.set:
d184bae667e4 log: make file log slow path usable on large repos
Durham Goode <durham@fb.com>
parents: 19511
diff changeset
  1940
                    return True
d184bae667e4 log: make file log slow path usable on large repos
Durham Goode <durham@fb.com>
parents: 19511
diff changeset
  1941
                elif not value in self.revs:
d184bae667e4 log: make file log slow path usable on large repos
Durham Goode <durham@fb.com>
parents: 19511
diff changeset
  1942
                    return False
d184bae667e4 log: make file log slow path usable on large repos
Durham Goode <durham@fb.com>
parents: 19511
diff changeset
  1943
                else:
d184bae667e4 log: make file log slow path usable on large repos
Durham Goode <durham@fb.com>
parents: 19511
diff changeset
  1944
                    self.revs.discard(value)
d184bae667e4 log: make file log slow path usable on large repos
Durham Goode <durham@fb.com>
parents: 19511
diff changeset
  1945
                    ctx = change(value)
36346
f62369667a7c py3: use list comprehensions instead of filter where we need to eagerly filter
Augie Fackler <augie@google.com>
parents: 36239
diff changeset
  1946
                    matches = [f for f in ctx.files() if match(f)]
19730
d184bae667e4 log: make file log slow path usable on large repos
Durham Goode <durham@fb.com>
parents: 19511
diff changeset
  1947
                    if matches:
d184bae667e4 log: make file log slow path usable on large repos
Durham Goode <durham@fb.com>
parents: 19511
diff changeset
  1948
                        fncache[value] = matches
d184bae667e4 log: make file log slow path usable on large repos
Durham Goode <durham@fb.com>
parents: 19511
diff changeset
  1949
                        self.set.add(value)
d184bae667e4 log: make file log slow path usable on large repos
Durham Goode <durham@fb.com>
parents: 19511
diff changeset
  1950
                        return True
d184bae667e4 log: make file log slow path usable on large repos
Durham Goode <durham@fb.com>
parents: 19511
diff changeset
  1951
                    return False
d184bae667e4 log: make file log slow path usable on large repos
Durham Goode <durham@fb.com>
parents: 19511
diff changeset
  1952
d184bae667e4 log: make file log slow path usable on large repos
Durham Goode <durham@fb.com>
parents: 19511
diff changeset
  1953
            def discard(self, value):
d184bae667e4 log: make file log slow path usable on large repos
Durham Goode <durham@fb.com>
parents: 19511
diff changeset
  1954
                self.revs.discard(value)
d184bae667e4 log: make file log slow path usable on large repos
Durham Goode <durham@fb.com>
parents: 19511
diff changeset
  1955
                self.set.discard(value)
d184bae667e4 log: make file log slow path usable on large repos
Durham Goode <durham@fb.com>
parents: 19511
diff changeset
  1956
d184bae667e4 log: make file log slow path usable on large repos
Durham Goode <durham@fb.com>
parents: 19511
diff changeset
  1957
        wanted = lazywantedset()
3650
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1958
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1959
    # it might be worthwhile to do this in the iterator if the rev range
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1960
    # is descending and the prune args are all within that range
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1961
    for rev in opts.get('prune', ()):
16380
84ba30e8c790 cmdutil: use context instead of lookup
Matt Mackall <mpm@selenic.com>
parents: 16304
diff changeset
  1962
        rev = repo[rev].rev()
24391
6c3a93e690c7 walkchangerevs: make followfilter a top-level class
Martin von Zweigbergk <martinvonz@google.com>
parents: 24384
diff changeset
  1963
        ff = _followfilter(repo)
3650
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1964
        stop = min(revs[0], revs[-1])
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10264
diff changeset
  1965
        for x in xrange(rev, stop - 1, -1):
8152
08e1baf924ca replace set-like dictionaries with real sets
Martin Geisler <mg@lazybytes.net>
parents: 8119
diff changeset
  1966
            if ff.match(x):
20553
86cefb15e7b5 cmdutil: implemented new lazy increasingwindows
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20500
diff changeset
  1967
                wanted = wanted - [x]
18710
49ef9d0ca815 cmdutil: use a small initial window with --limit
Bryan O'Sullivan <bryano@fb.com>
parents: 18688
diff changeset
  1968
11632
f418d2570920 log: document the different phases in walkchangerevs
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 11631
diff changeset
  1969
    # Now that wanted is correctly initialized, we can iterate over the
f418d2570920 log: document the different phases in walkchangerevs
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 11631
diff changeset
  1970
    # revision range, yielding only revisions in wanted.
3650
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1971
    def iterate():
25272
6c76c42a5893 walkchangerevs: avoid match.files() in conditions
Martin von Zweigbergk <martinvonz@google.com>
parents: 25271
diff changeset
  1972
        if follow and match.always():
24391
6c3a93e690c7 walkchangerevs: make followfilter a top-level class
Martin von Zweigbergk <martinvonz@google.com>
parents: 24384
diff changeset
  1973
            ff = _followfilter(repo, onlyfirst=opts.get('follow_first'))
3650
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1974
            def want(rev):
8119
af44d0b953c6 cmdutil: return boolean result directly in want function
Martin Geisler <mg@lazybytes.net>
parents: 8117
diff changeset
  1975
                return ff.match(rev) and rev in wanted
3650
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1976
        else:
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1977
            def want(rev):
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1978
                return rev in wanted
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1979
20553
86cefb15e7b5 cmdutil: implemented new lazy increasingwindows
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20500
diff changeset
  1980
        it = iter(revs)
86cefb15e7b5 cmdutil: implemented new lazy increasingwindows
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20500
diff changeset
  1981
        stopiteration = False
86cefb15e7b5 cmdutil: implemented new lazy increasingwindows
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20500
diff changeset
  1982
        for windowsize in increasingwindows():
86cefb15e7b5 cmdutil: implemented new lazy increasingwindows
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20500
diff changeset
  1983
            nrevs = []
86cefb15e7b5 cmdutil: implemented new lazy increasingwindows
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20500
diff changeset
  1984
            for i in xrange(windowsize):
25147
fb7b9a765bb9 walkchangerevs: replace try/except with 'next'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25119
diff changeset
  1985
                rev = next(it, None)
fb7b9a765bb9 walkchangerevs: replace try/except with 'next'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25119
diff changeset
  1986
                if rev is None:
20553
86cefb15e7b5 cmdutil: implemented new lazy increasingwindows
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20500
diff changeset
  1987
                    stopiteration = True
86cefb15e7b5 cmdutil: implemented new lazy increasingwindows
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20500
diff changeset
  1988
                    break
25147
fb7b9a765bb9 walkchangerevs: replace try/except with 'next'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25119
diff changeset
  1989
                elif want(rev):
fb7b9a765bb9 walkchangerevs: replace try/except with 'next'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25119
diff changeset
  1990
                    nrevs.append(rev)
8209
a1a5a57efe90 replace util.sort with sorted built-in
Matt Mackall <mpm@selenic.com>
parents: 8189
diff changeset
  1991
            for rev in sorted(nrevs):
3650
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1992
                fns = fncache.get(rev)
9654
96fe91be9c1e walkchangerevs: yield contexts
Matt Mackall <mpm@selenic.com>
parents: 9653
diff changeset
  1993
                ctx = change(rev)
3650
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1994
                if not fns:
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1995
                    def fns_generator():
9654
96fe91be9c1e walkchangerevs: yield contexts
Matt Mackall <mpm@selenic.com>
parents: 9653
diff changeset
  1996
                        for f in ctx.files():
9652
2cb0cab10d2e walkchangerevs: pull out matchfn
Matt Mackall <mpm@selenic.com>
parents: 9547
diff changeset
  1997
                            if match(f):
3650
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1998
                                yield f
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1999
                    fns = fns_generator()
9662
f3d60543924f walkchangerevs: move 'add' to callback
Matt Mackall <mpm@selenic.com>
parents: 9656
diff changeset
  2000
                prepare(ctx, fns)
3650
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  2001
            for rev in nrevs:
9662
f3d60543924f walkchangerevs: move 'add' to callback
Matt Mackall <mpm@selenic.com>
parents: 9656
diff changeset
  2002
                yield change(rev)
20553
86cefb15e7b5 cmdutil: implemented new lazy increasingwindows
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20500
diff changeset
  2003
86cefb15e7b5 cmdutil: implemented new lazy increasingwindows
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20500
diff changeset
  2004
            if stopiteration:
86cefb15e7b5 cmdutil: implemented new lazy increasingwindows
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20500
diff changeset
  2005
                break
86cefb15e7b5 cmdutil: implemented new lazy increasingwindows
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20500
diff changeset
  2006
9652
2cb0cab10d2e walkchangerevs: pull out matchfn
Matt Mackall <mpm@selenic.com>
parents: 9547
diff changeset
  2007
    return iterate()
5034
c0417a319e39 commands: move commit to cmdutil as wrapper for commit-like functions
Bryan O'Sullivan <bos@serpentine.com>
parents: 4965
diff changeset
  2008
23885
9994f45ba714 add: pass options via keyword args
Matt Harbison <matt_harbison@yahoo.com>
parents: 23876
diff changeset
  2009
def add(ui, repo, match, prefix, explicitonly, **opts):
12270
166b9866580a add: recurse into subrepositories with --subrepos/-S flag
Martin Geisler <mg@lazybytes.net>
parents: 12269
diff changeset
  2010
    join = lambda f: os.path.join(prefix, f)
12269
877236cdd437 add: move main part to cmdutil to make it easier to reuse
Martin Geisler <mg@lazybytes.net>
parents: 12266
diff changeset
  2011
    bad = []
25436
9724cbe2d546 add: replace match.bad() monkey patching with match.badmatch()
Matt Harbison <matt_harbison@yahoo.com>
parents: 25424
diff changeset
  2012
9724cbe2d546 add: replace match.bad() monkey patching with match.badmatch()
Matt Harbison <matt_harbison@yahoo.com>
parents: 25424
diff changeset
  2013
    badfn = lambda x, y: bad.append(x) or match.bad(x, y)
12269
877236cdd437 add: move main part to cmdutil to make it easier to reuse
Martin Geisler <mg@lazybytes.net>
parents: 12266
diff changeset
  2014
    names = []
12270
166b9866580a add: recurse into subrepositories with --subrepos/-S flag
Martin Geisler <mg@lazybytes.net>
parents: 12269
diff changeset
  2015
    wctx = repo[None]
14138
c18204fd35b0 scmutil: introduce casecollisionauditor
Adrian Buehlmann <adrian@cadifra.com>
parents: 14129
diff changeset
  2016
    cca = None
c18204fd35b0 scmutil: introduce casecollisionauditor
Adrian Buehlmann <adrian@cadifra.com>
parents: 14129
diff changeset
  2017
    abort, warn = scmutil.checkportabilityalert(ui)
c18204fd35b0 scmutil: introduce casecollisionauditor
Adrian Buehlmann <adrian@cadifra.com>
parents: 14129
diff changeset
  2018
    if abort or warn:
17201
afd75476939e scmutil: 25% speedup in casecollisionauditor
Joshua Redstone <joshua.redstone@fb.com>
parents: 17182
diff changeset
  2019
        cca = scmutil.casecollisionauditor(ui, abort, repo.dirstate)
25436
9724cbe2d546 add: replace match.bad() monkey patching with match.badmatch()
Matt Harbison <matt_harbison@yahoo.com>
parents: 25424
diff changeset
  2020
26206
ab1c6e4efda4 add: pass full=False to dirstate walk
Durham Goode <durham@fb.com>
parents: 26098
diff changeset
  2021
    badmatch = matchmod.badmatch(match, badfn)
ab1c6e4efda4 add: pass full=False to dirstate walk
Durham Goode <durham@fb.com>
parents: 26098
diff changeset
  2022
    dirstate = repo.dirstate
ab1c6e4efda4 add: pass full=False to dirstate walk
Durham Goode <durham@fb.com>
parents: 26098
diff changeset
  2023
    # We don't want to just call wctx.walk here, since it would return a lot of
ab1c6e4efda4 add: pass full=False to dirstate walk
Durham Goode <durham@fb.com>
parents: 26098
diff changeset
  2024
    # clean files, which we aren't interested in and takes time.
34343
255c761a52db dirstate: use keyword arguments to clarify walk()'s callers
Martin von Zweigbergk <martinvonz@google.com>
parents: 34327
diff changeset
  2025
    for f in sorted(dirstate.walk(badmatch, subrepos=sorted(wctx.substate),
255c761a52db dirstate: use keyword arguments to clarify walk()'s callers
Martin von Zweigbergk <martinvonz@google.com>
parents: 34327
diff changeset
  2026
                                  unknown=True, ignored=False, full=False)):
12269
877236cdd437 add: move main part to cmdutil to make it easier to reuse
Martin Geisler <mg@lazybytes.net>
parents: 12266
diff changeset
  2027
        exact = match.exact(f)
23462
afa3fbbcabd3 add: use lexists so that broken symbolic links are added
John Coomes <john.coomes@oracle.com>
parents: 23453
diff changeset
  2028
        if exact or not explicitonly and f not in wctx and repo.wvfs.lexists(f):
14138
c18204fd35b0 scmutil: introduce casecollisionauditor
Adrian Buehlmann <adrian@cadifra.com>
parents: 14129
diff changeset
  2029
            if cca:
c18204fd35b0 scmutil: introduce casecollisionauditor
Adrian Buehlmann <adrian@cadifra.com>
parents: 14129
diff changeset
  2030
                cca(f)
12269
877236cdd437 add: move main part to cmdutil to make it easier to reuse
Martin Geisler <mg@lazybytes.net>
parents: 12266
diff changeset
  2031
            names.append(f)
877236cdd437 add: move main part to cmdutil to make it easier to reuse
Martin Geisler <mg@lazybytes.net>
parents: 12266
diff changeset
  2032
            if ui.verbose or not exact:
23686
164915e8ef7b narrowmatcher: propagate the rel() method
Matt Harbison <matt_harbison@yahoo.com>
parents: 23674
diff changeset
  2033
                ui.status(_('adding %s\n') % match.rel(f))
12270
166b9866580a add: recurse into subrepositories with --subrepos/-S flag
Martin Geisler <mg@lazybytes.net>
parents: 12269
diff changeset
  2034
18364
6252b4f1c4b4 subrepos: process subrepos in sorted order
Mads Kiilerich <mads@kiilerich.com>
parents: 18340
diff changeset
  2035
    for subpath in sorted(wctx.substate):
15410
9e99d2bbb1b1 add: support adding explicit files in subrepos
David M. Carr <david@carrclan.us>
parents: 15231
diff changeset
  2036
        sub = wctx.sub(subpath)
9e99d2bbb1b1 add: support adding explicit files in subrepos
David M. Carr <david@carrclan.us>
parents: 15231
diff changeset
  2037
        try:
28017
d3f1b7ee5e70 match: rename "narrowmatcher" to "subdirmatcher" (API)
Martin von Zweigbergk <martinvonz@google.com>
parents: 27985
diff changeset
  2038
            submatch = matchmod.subdirmatcher(subpath, match)
32147
a77e61b45384 py3: handle opts correctly for `hg add`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 32144
diff changeset
  2039
            if opts.get(r'subrepos'):
23885
9994f45ba714 add: pass options via keyword args
Matt Harbison <matt_harbison@yahoo.com>
parents: 23876
diff changeset
  2040
                bad.extend(sub.add(ui, submatch, prefix, False, **opts))
15410
9e99d2bbb1b1 add: support adding explicit files in subrepos
David M. Carr <david@carrclan.us>
parents: 15231
diff changeset
  2041
            else:
23885
9994f45ba714 add: pass options via keyword args
Matt Harbison <matt_harbison@yahoo.com>
parents: 23876
diff changeset
  2042
                bad.extend(sub.add(ui, submatch, prefix, True, **opts))
15410
9e99d2bbb1b1 add: support adding explicit files in subrepos
David M. Carr <david@carrclan.us>
parents: 15231
diff changeset
  2043
        except error.LookupError:
9e99d2bbb1b1 add: support adding explicit files in subrepos
David M. Carr <david@carrclan.us>
parents: 15231
diff changeset
  2044
            ui.status(_("skipping missing subrepository: %s\n")
9e99d2bbb1b1 add: support adding explicit files in subrepos
David M. Carr <david@carrclan.us>
parents: 15231
diff changeset
  2045
                           % join(subpath))
12270
166b9866580a add: recurse into subrepositories with --subrepos/-S flag
Martin Geisler <mg@lazybytes.net>
parents: 12269
diff changeset
  2046
32147
a77e61b45384 py3: handle opts correctly for `hg add`
Pulkit Goyal <7895pulkit@gmail.com>
parents: 32144
diff changeset
  2047
    if not opts.get(r'dry_run'):
12270
166b9866580a add: recurse into subrepositories with --subrepos/-S flag
Martin Geisler <mg@lazybytes.net>
parents: 12269
diff changeset
  2048
        rejected = wctx.add(names, prefix)
12269
877236cdd437 add: move main part to cmdutil to make it easier to reuse
Martin Geisler <mg@lazybytes.net>
parents: 12266
diff changeset
  2049
        bad.extend(f for f in rejected if f in match.files())
877236cdd437 add: move main part to cmdutil to make it easier to reuse
Martin Geisler <mg@lazybytes.net>
parents: 12266
diff changeset
  2050
    return bad
877236cdd437 add: move main part to cmdutil to make it easier to reuse
Martin Geisler <mg@lazybytes.net>
parents: 12266
diff changeset
  2051
32005
2406dbba49bd serve: add support for Mercurial subrepositories
Matt Harbison <matt_harbison@yahoo.com>
parents: 31807
diff changeset
  2052
def addwebdirpath(repo, serverpath, webconf):
2406dbba49bd serve: add support for Mercurial subrepositories
Matt Harbison <matt_harbison@yahoo.com>
parents: 31807
diff changeset
  2053
    webconf[serverpath] = repo.root
2406dbba49bd serve: add support for Mercurial subrepositories
Matt Harbison <matt_harbison@yahoo.com>
parents: 31807
diff changeset
  2054
    repo.ui.debug('adding %s = %s\n' % (serverpath, repo.root))
2406dbba49bd serve: add support for Mercurial subrepositories
Matt Harbison <matt_harbison@yahoo.com>
parents: 31807
diff changeset
  2055
2406dbba49bd serve: add support for Mercurial subrepositories
Matt Harbison <matt_harbison@yahoo.com>
parents: 31807
diff changeset
  2056
    for r in repo.revs('filelog("path:.hgsub")'):
2406dbba49bd serve: add support for Mercurial subrepositories
Matt Harbison <matt_harbison@yahoo.com>
parents: 31807
diff changeset
  2057
        ctx = repo[r]
2406dbba49bd serve: add support for Mercurial subrepositories
Matt Harbison <matt_harbison@yahoo.com>
parents: 31807
diff changeset
  2058
        for subpath in ctx.substate:
2406dbba49bd serve: add support for Mercurial subrepositories
Matt Harbison <matt_harbison@yahoo.com>
parents: 31807
diff changeset
  2059
            ctx.sub(subpath).addwebdirpath(serverpath, webconf)
2406dbba49bd serve: add support for Mercurial subrepositories
Matt Harbison <matt_harbison@yahoo.com>
parents: 31807
diff changeset
  2060
37778
f10cb49951e1 forget: rename --confirm to --interactive
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 37774
diff changeset
  2061
def forget(ui, repo, match, prefix, explicitonly, dryrun, interactive):
f10cb49951e1 forget: rename --confirm to --interactive
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 37774
diff changeset
  2062
    if dryrun and interactive:
f10cb49951e1 forget: rename --confirm to --interactive
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 37774
diff changeset
  2063
        raise error.Abort(_("cannot specify both --dry-run and --interactive"))
15912
2bd54ffaa27e forget: fix subrepo recursion for explicit path handling
David M. Carr <david@carrclan.us>
parents: 15911
diff changeset
  2064
    join = lambda f: os.path.join(prefix, f)
2bd54ffaa27e forget: fix subrepo recursion for explicit path handling
David M. Carr <david@carrclan.us>
parents: 15911
diff changeset
  2065
    bad = []
25437
9c1bcd95b3ff forget: replace match.bad() monkey patching with match.badmatch()
Matt Harbison <matt_harbison@yahoo.com>
parents: 25436
diff changeset
  2066
    badfn = lambda x, y: bad.append(x) or match.bad(x, y)
15912
2bd54ffaa27e forget: fix subrepo recursion for explicit path handling
David M. Carr <david@carrclan.us>
parents: 15911
diff changeset
  2067
    wctx = repo[None]
2bd54ffaa27e forget: fix subrepo recursion for explicit path handling
David M. Carr <david@carrclan.us>
parents: 15911
diff changeset
  2068
    forgot = []
25437
9c1bcd95b3ff forget: replace match.bad() monkey patching with match.badmatch()
Matt Harbison <matt_harbison@yahoo.com>
parents: 25436
diff changeset
  2069
9c1bcd95b3ff forget: replace match.bad() monkey patching with match.badmatch()
Matt Harbison <matt_harbison@yahoo.com>
parents: 25436
diff changeset
  2070
    s = repo.status(match=matchmod.badmatch(match, badfn), clean=True)
32174
e4a4ebfd9d8e forget: access status fields by name, not index
Martin von Zweigbergk <martinvonz@google.com>
parents: 32155
diff changeset
  2071
    forget = sorted(s.modified + s.added + s.deleted + s.clean)
15912
2bd54ffaa27e forget: fix subrepo recursion for explicit path handling
David M. Carr <david@carrclan.us>
parents: 15911
diff changeset
  2072
    if explicitonly:
2bd54ffaa27e forget: fix subrepo recursion for explicit path handling
David M. Carr <david@carrclan.us>
parents: 15911
diff changeset
  2073
        forget = [f for f in forget if match.exact(f)]
2bd54ffaa27e forget: fix subrepo recursion for explicit path handling
David M. Carr <david@carrclan.us>
parents: 15911
diff changeset
  2074
18364
6252b4f1c4b4 subrepos: process subrepos in sorted order
Mads Kiilerich <mads@kiilerich.com>
parents: 18340
diff changeset
  2075
    for subpath in sorted(wctx.substate):
15912
2bd54ffaa27e forget: fix subrepo recursion for explicit path handling
David M. Carr <david@carrclan.us>
parents: 15911
diff changeset
  2076
        sub = wctx.sub(subpath)
2bd54ffaa27e forget: fix subrepo recursion for explicit path handling
David M. Carr <david@carrclan.us>
parents: 15911
diff changeset
  2077
        try:
28017
d3f1b7ee5e70 match: rename "narrowmatcher" to "subdirmatcher" (API)
Martin von Zweigbergk <martinvonz@google.com>
parents: 27985
diff changeset
  2078
            submatch = matchmod.subdirmatcher(subpath, match)
37778
f10cb49951e1 forget: rename --confirm to --interactive
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 37774
diff changeset
  2079
            subbad, subforgot = sub.forget(submatch, prefix, dryrun=dryrun,
f10cb49951e1 forget: rename --confirm to --interactive
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 37774
diff changeset
  2080
                                           interactive=interactive)
15912
2bd54ffaa27e forget: fix subrepo recursion for explicit path handling
David M. Carr <david@carrclan.us>
parents: 15911
diff changeset
  2081
            bad.extend([subpath + '/' + f for f in subbad])
2bd54ffaa27e forget: fix subrepo recursion for explicit path handling
David M. Carr <david@carrclan.us>
parents: 15911
diff changeset
  2082
            forgot.extend([subpath + '/' + f for f in subforgot])
2bd54ffaa27e forget: fix subrepo recursion for explicit path handling
David M. Carr <david@carrclan.us>
parents: 15911
diff changeset
  2083
        except error.LookupError:
2bd54ffaa27e forget: fix subrepo recursion for explicit path handling
David M. Carr <david@carrclan.us>
parents: 15911
diff changeset
  2084
            ui.status(_("skipping missing subrepository: %s\n")
2bd54ffaa27e forget: fix subrepo recursion for explicit path handling
David M. Carr <david@carrclan.us>
parents: 15911
diff changeset
  2085
                           % join(subpath))
2bd54ffaa27e forget: fix subrepo recursion for explicit path handling
David M. Carr <david@carrclan.us>
parents: 15911
diff changeset
  2086
16070
f11eee00c652 forget: show warning messages for forgetting in subrepo correctly
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15912
diff changeset
  2087
    if not explicitonly:
f11eee00c652 forget: show warning messages for forgetting in subrepo correctly
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15912
diff changeset
  2088
        for f in match.files():
23673
69cd91d04117 forget: use vfs instead of os.path + match.rel() for filesystem checks
Matt Harbison <matt_harbison@yahoo.com>
parents: 23579
diff changeset
  2089
            if f not in repo.dirstate and not repo.wvfs.isdir(f):
15912
2bd54ffaa27e forget: fix subrepo recursion for explicit path handling
David M. Carr <david@carrclan.us>
parents: 15911
diff changeset
  2090
                if f not in forgot:
23673
69cd91d04117 forget: use vfs instead of os.path + match.rel() for filesystem checks
Matt Harbison <matt_harbison@yahoo.com>
parents: 23579
diff changeset
  2091
                    if repo.wvfs.exists(f):
24548
c780a63f61ca forget: cleanup the output for an inexact case match on icasefs
Matt Harbison <matt_harbison@yahoo.com>
parents: 24169
diff changeset
  2092
                        # Don't complain if the exact case match wasn't given.
c780a63f61ca forget: cleanup the output for an inexact case match on icasefs
Matt Harbison <matt_harbison@yahoo.com>
parents: 24169
diff changeset
  2093
                        # But don't do this until after checking 'forgot', so
c780a63f61ca forget: cleanup the output for an inexact case match on icasefs
Matt Harbison <matt_harbison@yahoo.com>
parents: 24169
diff changeset
  2094
                        # that subrepo files aren't normalized, and this op is
c780a63f61ca forget: cleanup the output for an inexact case match on icasefs
Matt Harbison <matt_harbison@yahoo.com>
parents: 24169
diff changeset
  2095
                        # purely from data cached by the status walk above.
c780a63f61ca forget: cleanup the output for an inexact case match on icasefs
Matt Harbison <matt_harbison@yahoo.com>
parents: 24169
diff changeset
  2096
                        if repo.dirstate.normalize(f) in repo.dirstate:
c780a63f61ca forget: cleanup the output for an inexact case match on icasefs
Matt Harbison <matt_harbison@yahoo.com>
parents: 24169
diff changeset
  2097
                            continue
15912
2bd54ffaa27e forget: fix subrepo recursion for explicit path handling
David M. Carr <david@carrclan.us>
parents: 15911
diff changeset
  2098
                        ui.warn(_('not removing %s: '
2bd54ffaa27e forget: fix subrepo recursion for explicit path handling
David M. Carr <david@carrclan.us>
parents: 15911
diff changeset
  2099
                                  'file is already untracked\n')
23686
164915e8ef7b narrowmatcher: propagate the rel() method
Matt Harbison <matt_harbison@yahoo.com>
parents: 23674
diff changeset
  2100
                                % match.rel(f))
15912
2bd54ffaa27e forget: fix subrepo recursion for explicit path handling
David M. Carr <david@carrclan.us>
parents: 15911
diff changeset
  2101
                    bad.append(f)
2bd54ffaa27e forget: fix subrepo recursion for explicit path handling
David M. Carr <david@carrclan.us>
parents: 15911
diff changeset
  2102
37778
f10cb49951e1 forget: rename --confirm to --interactive
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 37774
diff changeset
  2103
    if interactive:
37756
e7bf5a73e4e1 forget: add --confirm option
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 37621
diff changeset
  2104
        responses = _('[Ynsa?]'
e7bf5a73e4e1 forget: add --confirm option
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 37621
diff changeset
  2105
                      '$$ &Yes, forget this file'
e7bf5a73e4e1 forget: add --confirm option
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 37621
diff changeset
  2106
                      '$$ &No, skip this file'
e7bf5a73e4e1 forget: add --confirm option
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 37621
diff changeset
  2107
                      '$$ &Skip remaining files'
e7bf5a73e4e1 forget: add --confirm option
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 37621
diff changeset
  2108
                      '$$ Include &all remaining files'
e7bf5a73e4e1 forget: add --confirm option
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 37621
diff changeset
  2109
                      '$$ &? (display help)')
e7bf5a73e4e1 forget: add --confirm option
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 37621
diff changeset
  2110
        for filename in forget[:]:
e7bf5a73e4e1 forget: add --confirm option
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 37621
diff changeset
  2111
            r = ui.promptchoice(_('forget %s %s') % (filename, responses))
e7bf5a73e4e1 forget: add --confirm option
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 37621
diff changeset
  2112
            if r == 4: # ?
e7bf5a73e4e1 forget: add --confirm option
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 37621
diff changeset
  2113
                while r == 4:
e7bf5a73e4e1 forget: add --confirm option
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 37621
diff changeset
  2114
                    for c, t in ui.extractchoices(responses)[1]:
e7bf5a73e4e1 forget: add --confirm option
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 37621
diff changeset
  2115
                        ui.write('%s - %s\n' % (c, encoding.lower(t)))
e7bf5a73e4e1 forget: add --confirm option
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 37621
diff changeset
  2116
                    r = ui.promptchoice(_('forget %s %s') % (filename,
e7bf5a73e4e1 forget: add --confirm option
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 37621
diff changeset
  2117
                                                                 responses))
e7bf5a73e4e1 forget: add --confirm option
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 37621
diff changeset
  2118
            if r == 0: # yes
e7bf5a73e4e1 forget: add --confirm option
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 37621
diff changeset
  2119
                continue
e7bf5a73e4e1 forget: add --confirm option
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 37621
diff changeset
  2120
            elif r == 1: # no
e7bf5a73e4e1 forget: add --confirm option
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 37621
diff changeset
  2121
                forget.remove(filename)
e7bf5a73e4e1 forget: add --confirm option
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 37621
diff changeset
  2122
            elif r == 2: # Skip
e7bf5a73e4e1 forget: add --confirm option
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 37621
diff changeset
  2123
                fnindex = forget.index(filename)
e7bf5a73e4e1 forget: add --confirm option
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 37621
diff changeset
  2124
                del forget[fnindex:]
e7bf5a73e4e1 forget: add --confirm option
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 37621
diff changeset
  2125
                break
e7bf5a73e4e1 forget: add --confirm option
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 37621
diff changeset
  2126
            elif r == 3: # All
e7bf5a73e4e1 forget: add --confirm option
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 37621
diff changeset
  2127
                break
e7bf5a73e4e1 forget: add --confirm option
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 37621
diff changeset
  2128
15912
2bd54ffaa27e forget: fix subrepo recursion for explicit path handling
David M. Carr <david@carrclan.us>
parents: 15911
diff changeset
  2129
    for f in forget:
37778
f10cb49951e1 forget: rename --confirm to --interactive
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 37774
diff changeset
  2130
        if ui.verbose or not match.exact(f) or interactive:
23686
164915e8ef7b narrowmatcher: propagate the rel() method
Matt Harbison <matt_harbison@yahoo.com>
parents: 23674
diff changeset
  2131
            ui.status(_('removing %s\n') % match.rel(f))
15912
2bd54ffaa27e forget: fix subrepo recursion for explicit path handling
David M. Carr <david@carrclan.us>
parents: 15911
diff changeset
  2132
36939
45bfcd16f27e forget: add --dry-run mode
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 36719
diff changeset
  2133
    if not dryrun:
45bfcd16f27e forget: add --dry-run mode
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 36719
diff changeset
  2134
        rejected = wctx.forget(forget, prefix)
45bfcd16f27e forget: add --dry-run mode
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 36719
diff changeset
  2135
        bad.extend(f for f in rejected if f in match.files())
45bfcd16f27e forget: add --dry-run mode
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 36719
diff changeset
  2136
        forgot.extend(f for f in forget if f not in rejected)
15912
2bd54ffaa27e forget: fix subrepo recursion for explicit path handling
David M. Carr <david@carrclan.us>
parents: 15911
diff changeset
  2137
    return bad, forgot
2bd54ffaa27e forget: fix subrepo recursion for explicit path handling
David M. Carr <david@carrclan.us>
parents: 15911
diff changeset
  2138
24413
a8595176dd64 subrepo: add basic support to hgsubrepo for the files command
Matt Harbison <matt_harbison@yahoo.com>
parents: 24391
diff changeset
  2139
def files(ui, ctx, m, fm, fmt, subrepos):
24275
e1cb460a3524 files: split reusable implementation into cmdutil for subrepo support
Matt Harbison <matt_harbison@yahoo.com>
parents: 24272
diff changeset
  2140
    rev = ctx.rev()
e1cb460a3524 files: split reusable implementation into cmdutil for subrepo support
Matt Harbison <matt_harbison@yahoo.com>
parents: 24272
diff changeset
  2141
    ret = 1
24301
18b5b2c9d921 files: replace 'ctx._repo' with 'ctx.repo()'
Matt Harbison <matt_harbison@yahoo.com>
parents: 24275
diff changeset
  2142
    ds = ctx.repo().dirstate
24275
e1cb460a3524 files: split reusable implementation into cmdutil for subrepo support
Matt Harbison <matt_harbison@yahoo.com>
parents: 24272
diff changeset
  2143
e1cb460a3524 files: split reusable implementation into cmdutil for subrepo support
Matt Harbison <matt_harbison@yahoo.com>
parents: 24272
diff changeset
  2144
    for f in ctx.matches(m):
e1cb460a3524 files: split reusable implementation into cmdutil for subrepo support
Matt Harbison <matt_harbison@yahoo.com>
parents: 24272
diff changeset
  2145
        if rev is None and ds[f] == 'r':
e1cb460a3524 files: split reusable implementation into cmdutil for subrepo support
Matt Harbison <matt_harbison@yahoo.com>
parents: 24272
diff changeset
  2146
            continue
e1cb460a3524 files: split reusable implementation into cmdutil for subrepo support
Matt Harbison <matt_harbison@yahoo.com>
parents: 24272
diff changeset
  2147
        fm.startitem()
e1cb460a3524 files: split reusable implementation into cmdutil for subrepo support
Matt Harbison <matt_harbison@yahoo.com>
parents: 24272
diff changeset
  2148
        if ui.verbose:
e1cb460a3524 files: split reusable implementation into cmdutil for subrepo support
Matt Harbison <matt_harbison@yahoo.com>
parents: 24272
diff changeset
  2149
            fc = ctx[f]
e1cb460a3524 files: split reusable implementation into cmdutil for subrepo support
Matt Harbison <matt_harbison@yahoo.com>
parents: 24272
diff changeset
  2150
            fm.write('size flags', '% 10d % 1s ', fc.size(), fc.flags())
e1cb460a3524 files: split reusable implementation into cmdutil for subrepo support
Matt Harbison <matt_harbison@yahoo.com>
parents: 24272
diff changeset
  2151
        fm.data(abspath=f)
e1cb460a3524 files: split reusable implementation into cmdutil for subrepo support
Matt Harbison <matt_harbison@yahoo.com>
parents: 24272
diff changeset
  2152
        fm.write('path', fmt, m.rel(f))
e1cb460a3524 files: split reusable implementation into cmdutil for subrepo support
Matt Harbison <matt_harbison@yahoo.com>
parents: 24272
diff changeset
  2153
        ret = 0
e1cb460a3524 files: split reusable implementation into cmdutil for subrepo support
Matt Harbison <matt_harbison@yahoo.com>
parents: 24272
diff changeset
  2154
25228
63a57a2727b6 files: recurse into subrepos automatically with an explicit path
Matt Harbison <matt_harbison@yahoo.com>
parents: 25169
diff changeset
  2155
    for subpath in sorted(ctx.substate):
29802
35560189677c subrepo: cleanup of subrepo filematcher logic
Hannes Oldenburg <hannes.christian.oldenburg@gmail.com>
parents: 29795
diff changeset
  2156
        submatch = matchmod.subdirmatcher(subpath, m)
35560189677c subrepo: cleanup of subrepo filematcher logic
Hannes Oldenburg <hannes.christian.oldenburg@gmail.com>
parents: 29795
diff changeset
  2157
        if (subrepos or m.exact(subpath) or any(submatch.files())):
24413
a8595176dd64 subrepo: add basic support to hgsubrepo for the files command
Matt Harbison <matt_harbison@yahoo.com>
parents: 24391
diff changeset
  2158
            sub = ctx.sub(subpath)
a8595176dd64 subrepo: add basic support to hgsubrepo for the files command
Matt Harbison <matt_harbison@yahoo.com>
parents: 24391
diff changeset
  2159
            try:
28387
97175d9bf7cf files: don't recurse into subrepos without a path or -S (issue5127)
Matt Harbison <matt_harbison@yahoo.com>
parents: 28253
diff changeset
  2160
                recurse = m.exact(subpath) or subrepos
97175d9bf7cf files: don't recurse into subrepos without a path or -S (issue5127)
Matt Harbison <matt_harbison@yahoo.com>
parents: 28253
diff changeset
  2161
                if sub.printfiles(ui, submatch, fm, fmt, recurse) == 0:
24413
a8595176dd64 subrepo: add basic support to hgsubrepo for the files command
Matt Harbison <matt_harbison@yahoo.com>
parents: 24391
diff changeset
  2162
                    ret = 0
a8595176dd64 subrepo: add basic support to hgsubrepo for the files command
Matt Harbison <matt_harbison@yahoo.com>
parents: 24391
diff changeset
  2163
            except error.LookupError:
a8595176dd64 subrepo: add basic support to hgsubrepo for the files command
Matt Harbison <matt_harbison@yahoo.com>
parents: 24391
diff changeset
  2164
                ui.status(_("skipping missing subrepository: %s\n")
a8595176dd64 subrepo: add basic support to hgsubrepo for the files command
Matt Harbison <matt_harbison@yahoo.com>
parents: 24391
diff changeset
  2165
                               % m.abs(subpath))
a8595176dd64 subrepo: add basic support to hgsubrepo for the files command
Matt Harbison <matt_harbison@yahoo.com>
parents: 24391
diff changeset
  2166
24275
e1cb460a3524 files: split reusable implementation into cmdutil for subrepo support
Matt Harbison <matt_harbison@yahoo.com>
parents: 24272
diff changeset
  2167
    return ret
e1cb460a3524 files: split reusable implementation into cmdutil for subrepo support
Matt Harbison <matt_harbison@yahoo.com>
parents: 24272
diff changeset
  2168
37150
335e19c6b7fa remove: add dry-run functionality
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 37103
diff changeset
  2169
def remove(ui, repo, m, prefix, after, force, subrepos, dryrun, warnings=None):
23325
4165cfd67519 remove: recurse into subrepositories with --subrepos/-S flag
Matt Harbison <matt_harbison@yahoo.com>
parents: 23289
diff changeset
  2170
    join = lambda f: os.path.join(prefix, f)
23289
ae5d0a22ee7e remove: move most of the implementation into cmdutils.remove()
Matt Harbison <matt_harbison@yahoo.com>
parents: 23258
diff changeset
  2171
    ret = 0
ae5d0a22ee7e remove: move most of the implementation into cmdutils.remove()
Matt Harbison <matt_harbison@yahoo.com>
parents: 23258
diff changeset
  2172
    s = repo.status(match=m, clean=True)
ae5d0a22ee7e remove: move most of the implementation into cmdutils.remove()
Matt Harbison <matt_harbison@yahoo.com>
parents: 23258
diff changeset
  2173
    modified, added, deleted, clean = s[0], s[1], s[3], s[6]
ae5d0a22ee7e remove: move most of the implementation into cmdutils.remove()
Matt Harbison <matt_harbison@yahoo.com>
parents: 23258
diff changeset
  2174
23325
4165cfd67519 remove: recurse into subrepositories with --subrepos/-S flag
Matt Harbison <matt_harbison@yahoo.com>
parents: 23289
diff changeset
  2175
    wctx = repo[None]
4165cfd67519 remove: recurse into subrepositories with --subrepos/-S flag
Matt Harbison <matt_harbison@yahoo.com>
parents: 23289
diff changeset
  2176
28607
a88959ae5938 remove: queue warnings until after status messages (issue5140) (API)
timeless <timeless@mozdev.org>
parents: 28601
diff changeset
  2177
    if warnings is None:
a88959ae5938 remove: queue warnings until after status messages (issue5140) (API)
timeless <timeless@mozdev.org>
parents: 28601
diff changeset
  2178
        warnings = []
a88959ae5938 remove: queue warnings until after status messages (issue5140) (API)
timeless <timeless@mozdev.org>
parents: 28601
diff changeset
  2179
        warn = True
a88959ae5938 remove: queue warnings until after status messages (issue5140) (API)
timeless <timeless@mozdev.org>
parents: 28601
diff changeset
  2180
    else:
a88959ae5938 remove: queue warnings until after status messages (issue5140) (API)
timeless <timeless@mozdev.org>
parents: 28601
diff changeset
  2181
        warn = False
a88959ae5938 remove: queue warnings until after status messages (issue5140) (API)
timeless <timeless@mozdev.org>
parents: 28601
diff changeset
  2182
28608
62e73d42bd14 remove: add progress support
timeless <timeless@mozdev.org>
parents: 28607
diff changeset
  2183
    subs = sorted(wctx.substate)
62e73d42bd14 remove: add progress support
timeless <timeless@mozdev.org>
parents: 28607
diff changeset
  2184
    total = len(subs)
62e73d42bd14 remove: add progress support
timeless <timeless@mozdev.org>
parents: 28607
diff changeset
  2185
    count = 0
62e73d42bd14 remove: add progress support
timeless <timeless@mozdev.org>
parents: 28607
diff changeset
  2186
    for subpath in subs:
62e73d42bd14 remove: add progress support
timeless <timeless@mozdev.org>
parents: 28607
diff changeset
  2187
        count += 1
29802
35560189677c subrepo: cleanup of subrepo filematcher logic
Hannes Oldenburg <hannes.christian.oldenburg@gmail.com>
parents: 29795
diff changeset
  2188
        submatch = matchmod.subdirmatcher(subpath, m)
35560189677c subrepo: cleanup of subrepo filematcher logic
Hannes Oldenburg <hannes.christian.oldenburg@gmail.com>
parents: 29795
diff changeset
  2189
        if subrepos or m.exact(subpath) or any(submatch.files()):
28608
62e73d42bd14 remove: add progress support
timeless <timeless@mozdev.org>
parents: 28607
diff changeset
  2190
            ui.progress(_('searching'), count, total=total, unit=_('subrepos'))
23325
4165cfd67519 remove: recurse into subrepositories with --subrepos/-S flag
Matt Harbison <matt_harbison@yahoo.com>
parents: 23289
diff changeset
  2191
            sub = wctx.sub(subpath)
4165cfd67519 remove: recurse into subrepositories with --subrepos/-S flag
Matt Harbison <matt_harbison@yahoo.com>
parents: 23289
diff changeset
  2192
            try:
28607
a88959ae5938 remove: queue warnings until after status messages (issue5140) (API)
timeless <timeless@mozdev.org>
parents: 28601
diff changeset
  2193
                if sub.removefiles(submatch, prefix, after, force, subrepos,
37150
335e19c6b7fa remove: add dry-run functionality
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 37103
diff changeset
  2194
                                   dryrun, warnings):
23325
4165cfd67519 remove: recurse into subrepositories with --subrepos/-S flag
Matt Harbison <matt_harbison@yahoo.com>
parents: 23289
diff changeset
  2195
                    ret = 1
4165cfd67519 remove: recurse into subrepositories with --subrepos/-S flag
Matt Harbison <matt_harbison@yahoo.com>
parents: 23289
diff changeset
  2196
            except error.LookupError:
28607
a88959ae5938 remove: queue warnings until after status messages (issue5140) (API)
timeless <timeless@mozdev.org>
parents: 28601
diff changeset
  2197
                warnings.append(_("skipping missing subrepository: %s\n")
23325
4165cfd67519 remove: recurse into subrepositories with --subrepos/-S flag
Matt Harbison <matt_harbison@yahoo.com>
parents: 23289
diff changeset
  2198
                               % join(subpath))
28608
62e73d42bd14 remove: add progress support
timeless <timeless@mozdev.org>
parents: 28607
diff changeset
  2199
    ui.progress(_('searching'), None)
23325
4165cfd67519 remove: recurse into subrepositories with --subrepos/-S flag
Matt Harbison <matt_harbison@yahoo.com>
parents: 23289
diff changeset
  2200
23289
ae5d0a22ee7e remove: move most of the implementation into cmdutils.remove()
Matt Harbison <matt_harbison@yahoo.com>
parents: 23258
diff changeset
  2201
    # warn about failure to delete explicit files/dirs
24635
21e1ece30f8c util: move dirs() and finddirs() from scmutil to util
Drew Gottlieb <drgott@google.com>
parents: 24603
diff changeset
  2202
    deleteddirs = util.dirs(deleted)
28608
62e73d42bd14 remove: add progress support
timeless <timeless@mozdev.org>
parents: 28607
diff changeset
  2203
    files = m.files()
62e73d42bd14 remove: add progress support
timeless <timeless@mozdev.org>
parents: 28607
diff changeset
  2204
    total = len(files)
62e73d42bd14 remove: add progress support
timeless <timeless@mozdev.org>
parents: 28607
diff changeset
  2205
    count = 0
62e73d42bd14 remove: add progress support
timeless <timeless@mozdev.org>
parents: 28607
diff changeset
  2206
    for f in files:
23325
4165cfd67519 remove: recurse into subrepositories with --subrepos/-S flag
Matt Harbison <matt_harbison@yahoo.com>
parents: 23289
diff changeset
  2207
        def insubrepo():
4165cfd67519 remove: recurse into subrepositories with --subrepos/-S flag
Matt Harbison <matt_harbison@yahoo.com>
parents: 23289
diff changeset
  2208
            for subpath in wctx.substate:
29622
9c2cc107547f cmdutil: warnings not issued in remove if subrepopath overlaps
Hannes Oldenburg <hannes.christian.oldenburg@gmail.com>
parents: 29498
diff changeset
  2209
                if f.startswith(subpath + '/'):
23325
4165cfd67519 remove: recurse into subrepositories with --subrepos/-S flag
Matt Harbison <matt_harbison@yahoo.com>
parents: 23289
diff changeset
  2210
                    return True
4165cfd67519 remove: recurse into subrepositories with --subrepos/-S flag
Matt Harbison <matt_harbison@yahoo.com>
parents: 23289
diff changeset
  2211
            return False
4165cfd67519 remove: recurse into subrepositories with --subrepos/-S flag
Matt Harbison <matt_harbison@yahoo.com>
parents: 23289
diff changeset
  2212
28608
62e73d42bd14 remove: add progress support
timeless <timeless@mozdev.org>
parents: 28607
diff changeset
  2213
        count += 1
62e73d42bd14 remove: add progress support
timeless <timeless@mozdev.org>
parents: 28607
diff changeset
  2214
        ui.progress(_('deleting'), count, total=total, unit=_('files'))
24955
1df233bcb7f6 remove: use ctx.hasdir(f) instead of 'f in ctx.dirs()'
Martin von Zweigbergk <martinvonz@google.com>
parents: 24947
diff changeset
  2215
        isdir = f in deleteddirs or wctx.hasdir(f)
29622
9c2cc107547f cmdutil: warnings not issued in remove if subrepopath overlaps
Hannes Oldenburg <hannes.christian.oldenburg@gmail.com>
parents: 29498
diff changeset
  2216
        if (f in repo.dirstate or isdir or f == '.'
9c2cc107547f cmdutil: warnings not issued in remove if subrepopath overlaps
Hannes Oldenburg <hannes.christian.oldenburg@gmail.com>
parents: 29498
diff changeset
  2217
            or insubrepo() or f in subs):
23289
ae5d0a22ee7e remove: move most of the implementation into cmdutils.remove()
Matt Harbison <matt_harbison@yahoo.com>
parents: 23258
diff changeset
  2218
            continue
23325
4165cfd67519 remove: recurse into subrepositories with --subrepos/-S flag
Matt Harbison <matt_harbison@yahoo.com>
parents: 23289
diff changeset
  2219
23674
6e36b9fc7869 remove: use vfs instead of os.path + match.rel() for filesystem checks
Matt Harbison <matt_harbison@yahoo.com>
parents: 23673
diff changeset
  2220
        if repo.wvfs.exists(f):
6e36b9fc7869 remove: use vfs instead of os.path + match.rel() for filesystem checks
Matt Harbison <matt_harbison@yahoo.com>
parents: 23673
diff changeset
  2221
            if repo.wvfs.isdir(f):
28607
a88959ae5938 remove: queue warnings until after status messages (issue5140) (API)
timeless <timeless@mozdev.org>
parents: 28601
diff changeset
  2222
                warnings.append(_('not removing %s: no tracked files\n')
23686
164915e8ef7b narrowmatcher: propagate the rel() method
Matt Harbison <matt_harbison@yahoo.com>
parents: 23674
diff changeset
  2223
                        % m.rel(f))
23289
ae5d0a22ee7e remove: move most of the implementation into cmdutils.remove()
Matt Harbison <matt_harbison@yahoo.com>
parents: 23258
diff changeset
  2224
            else:
28607
a88959ae5938 remove: queue warnings until after status messages (issue5140) (API)
timeless <timeless@mozdev.org>
parents: 28601
diff changeset
  2225
                warnings.append(_('not removing %s: file is untracked\n')
23686
164915e8ef7b narrowmatcher: propagate the rel() method
Matt Harbison <matt_harbison@yahoo.com>
parents: 23674
diff changeset
  2226
                        % m.rel(f))
23289
ae5d0a22ee7e remove: move most of the implementation into cmdutils.remove()
Matt Harbison <matt_harbison@yahoo.com>
parents: 23258
diff changeset
  2227
        # missing files will generate a warning elsewhere
ae5d0a22ee7e remove: move most of the implementation into cmdutils.remove()
Matt Harbison <matt_harbison@yahoo.com>
parents: 23258
diff changeset
  2228
        ret = 1
28608
62e73d42bd14 remove: add progress support
timeless <timeless@mozdev.org>
parents: 28607
diff changeset
  2229
    ui.progress(_('deleting'), None)
23289
ae5d0a22ee7e remove: move most of the implementation into cmdutils.remove()
Matt Harbison <matt_harbison@yahoo.com>
parents: 23258
diff changeset
  2230
ae5d0a22ee7e remove: move most of the implementation into cmdutils.remove()
Matt Harbison <matt_harbison@yahoo.com>
parents: 23258
diff changeset
  2231
    if force:
ae5d0a22ee7e remove: move most of the implementation into cmdutils.remove()
Matt Harbison <matt_harbison@yahoo.com>
parents: 23258
diff changeset
  2232
        list = modified + deleted + clean + added
ae5d0a22ee7e remove: move most of the implementation into cmdutils.remove()
Matt Harbison <matt_harbison@yahoo.com>
parents: 23258
diff changeset
  2233
    elif after:
ae5d0a22ee7e remove: move most of the implementation into cmdutils.remove()
Matt Harbison <matt_harbison@yahoo.com>
parents: 23258
diff changeset
  2234
        list = deleted
28608
62e73d42bd14 remove: add progress support
timeless <timeless@mozdev.org>
parents: 28607
diff changeset
  2235
        remaining = modified + added + clean
62e73d42bd14 remove: add progress support
timeless <timeless@mozdev.org>
parents: 28607
diff changeset
  2236
        total = len(remaining)
62e73d42bd14 remove: add progress support
timeless <timeless@mozdev.org>
parents: 28607
diff changeset
  2237
        count = 0
62e73d42bd14 remove: add progress support
timeless <timeless@mozdev.org>
parents: 28607
diff changeset
  2238
        for f in remaining:
62e73d42bd14 remove: add progress support
timeless <timeless@mozdev.org>
parents: 28607
diff changeset
  2239
            count += 1
62e73d42bd14 remove: add progress support
timeless <timeless@mozdev.org>
parents: 28607
diff changeset
  2240
            ui.progress(_('skipping'), count, total=total, unit=_('files'))
35123
7a58608281dd remove: print message for each file in verbose mode only while using `-A` (BC)
pavanpc@fb.com
parents: 35107
diff changeset
  2241
            if ui.verbose or (f in files):
7a58608281dd remove: print message for each file in verbose mode only while using `-A` (BC)
pavanpc@fb.com
parents: 35107
diff changeset
  2242
                warnings.append(_('not removing %s: file still exists\n')
7a58608281dd remove: print message for each file in verbose mode only while using `-A` (BC)
pavanpc@fb.com
parents: 35107
diff changeset
  2243
                                % m.rel(f))
23289
ae5d0a22ee7e remove: move most of the implementation into cmdutils.remove()
Matt Harbison <matt_harbison@yahoo.com>
parents: 23258
diff changeset
  2244
            ret = 1
28608
62e73d42bd14 remove: add progress support
timeless <timeless@mozdev.org>
parents: 28607
diff changeset
  2245
        ui.progress(_('skipping'), None)
23289
ae5d0a22ee7e remove: move most of the implementation into cmdutils.remove()
Matt Harbison <matt_harbison@yahoo.com>
parents: 23258
diff changeset
  2246
    else:
ae5d0a22ee7e remove: move most of the implementation into cmdutils.remove()
Matt Harbison <matt_harbison@yahoo.com>
parents: 23258
diff changeset
  2247
        list = deleted + clean
28608
62e73d42bd14 remove: add progress support
timeless <timeless@mozdev.org>
parents: 28607
diff changeset
  2248
        total = len(modified) + len(added)
62e73d42bd14 remove: add progress support
timeless <timeless@mozdev.org>
parents: 28607
diff changeset
  2249
        count = 0
23289
ae5d0a22ee7e remove: move most of the implementation into cmdutils.remove()
Matt Harbison <matt_harbison@yahoo.com>
parents: 23258
diff changeset
  2250
        for f in modified:
28608
62e73d42bd14 remove: add progress support
timeless <timeless@mozdev.org>
parents: 28607
diff changeset
  2251
            count += 1
62e73d42bd14 remove: add progress support
timeless <timeless@mozdev.org>
parents: 28607
diff changeset
  2252
            ui.progress(_('skipping'), count, total=total, unit=_('files'))
28607
a88959ae5938 remove: queue warnings until after status messages (issue5140) (API)
timeless <timeless@mozdev.org>
parents: 28601
diff changeset
  2253
            warnings.append(_('not removing %s: file is modified (use -f'
23686
164915e8ef7b narrowmatcher: propagate the rel() method
Matt Harbison <matt_harbison@yahoo.com>
parents: 23674
diff changeset
  2254
                      ' to force removal)\n') % m.rel(f))
23289
ae5d0a22ee7e remove: move most of the implementation into cmdutils.remove()
Matt Harbison <matt_harbison@yahoo.com>
parents: 23258
diff changeset
  2255
            ret = 1
ae5d0a22ee7e remove: move most of the implementation into cmdutils.remove()
Matt Harbison <matt_harbison@yahoo.com>
parents: 23258
diff changeset
  2256
        for f in added:
28608
62e73d42bd14 remove: add progress support
timeless <timeless@mozdev.org>
parents: 28607
diff changeset
  2257
            count += 1
62e73d42bd14 remove: add progress support
timeless <timeless@mozdev.org>
parents: 28607
diff changeset
  2258
            ui.progress(_('skipping'), count, total=total, unit=_('files'))
29963
e824de573112 remove: specify hg in added warning
timeless <timeless@mozdev.org>
parents: 29956
diff changeset
  2259
            warnings.append(_("not removing %s: file has been marked for add"
e824de573112 remove: specify hg in added warning
timeless <timeless@mozdev.org>
parents: 29956
diff changeset
  2260
                      " (use 'hg forget' to undo add)\n") % m.rel(f))
23289
ae5d0a22ee7e remove: move most of the implementation into cmdutils.remove()
Matt Harbison <matt_harbison@yahoo.com>
parents: 23258
diff changeset
  2261
            ret = 1
28608
62e73d42bd14 remove: add progress support
timeless <timeless@mozdev.org>
parents: 28607
diff changeset
  2262
        ui.progress(_('skipping'), None)
62e73d42bd14 remove: add progress support
timeless <timeless@mozdev.org>
parents: 28607
diff changeset
  2263
62e73d42bd14 remove: add progress support
timeless <timeless@mozdev.org>
parents: 28607
diff changeset
  2264
    list = sorted(list)
62e73d42bd14 remove: add progress support
timeless <timeless@mozdev.org>
parents: 28607
diff changeset
  2265
    total = len(list)
62e73d42bd14 remove: add progress support
timeless <timeless@mozdev.org>
parents: 28607
diff changeset
  2266
    count = 0
62e73d42bd14 remove: add progress support
timeless <timeless@mozdev.org>
parents: 28607
diff changeset
  2267
    for f in list:
62e73d42bd14 remove: add progress support
timeless <timeless@mozdev.org>
parents: 28607
diff changeset
  2268
        count += 1
23289
ae5d0a22ee7e remove: move most of the implementation into cmdutils.remove()
Matt Harbison <matt_harbison@yahoo.com>
parents: 23258
diff changeset
  2269
        if ui.verbose or not m.exact(f):
28608
62e73d42bd14 remove: add progress support
timeless <timeless@mozdev.org>
parents: 28607
diff changeset
  2270
            ui.progress(_('deleting'), count, total=total, unit=_('files'))
23686
164915e8ef7b narrowmatcher: propagate the rel() method
Matt Harbison <matt_harbison@yahoo.com>
parents: 23674
diff changeset
  2271
            ui.status(_('removing %s\n') % m.rel(f))
28608
62e73d42bd14 remove: add progress support
timeless <timeless@mozdev.org>
parents: 28607
diff changeset
  2272
    ui.progress(_('deleting'), None)
23289
ae5d0a22ee7e remove: move most of the implementation into cmdutils.remove()
Matt Harbison <matt_harbison@yahoo.com>
parents: 23258
diff changeset
  2273
37150
335e19c6b7fa remove: add dry-run functionality
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 37103
diff changeset
  2274
    if not dryrun:
335e19c6b7fa remove: add dry-run functionality
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 37103
diff changeset
  2275
        with repo.wlock():
335e19c6b7fa remove: add dry-run functionality
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 37103
diff changeset
  2276
            if not after:
335e19c6b7fa remove: add dry-run functionality
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 37103
diff changeset
  2277
                for f in list:
335e19c6b7fa remove: add dry-run functionality
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 37103
diff changeset
  2278
                    if f in added:
335e19c6b7fa remove: add dry-run functionality
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 37103
diff changeset
  2279
                        continue # we never unlink added files on remove
335e19c6b7fa remove: add dry-run functionality
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 37103
diff changeset
  2280
                    repo.wvfs.unlinkpath(f, ignoremissing=True)
335e19c6b7fa remove: add dry-run functionality
Sushil khanchi <sushilkhanchi97@gmail.com>
parents: 37103
diff changeset
  2281
            repo[None].forget(list)
23289
ae5d0a22ee7e remove: move most of the implementation into cmdutils.remove()
Matt Harbison <matt_harbison@yahoo.com>
parents: 23258
diff changeset
  2282
28607
a88959ae5938 remove: queue warnings until after status messages (issue5140) (API)
timeless <timeless@mozdev.org>
parents: 28601
diff changeset
  2283
    if warn:
a88959ae5938 remove: queue warnings until after status messages (issue5140) (API)
timeless <timeless@mozdev.org>
parents: 28601
diff changeset
  2284
        for warning in warnings:
a88959ae5938 remove: queue warnings until after status messages (issue5140) (API)
timeless <timeless@mozdev.org>
parents: 28601
diff changeset
  2285
            ui.warn(warning)
a88959ae5938 remove: queue warnings until after status messages (issue5140) (API)
timeless <timeless@mozdev.org>
parents: 28601
diff changeset
  2286
23289
ae5d0a22ee7e remove: move most of the implementation into cmdutils.remove()
Matt Harbison <matt_harbison@yahoo.com>
parents: 23258
diff changeset
  2287
    return ret
ae5d0a22ee7e remove: move most of the implementation into cmdutils.remove()
Matt Harbison <matt_harbison@yahoo.com>
parents: 23258
diff changeset
  2288
35662
91f0979f16c0 cat: factor out a function that populates the formatter
Matt Harbison <matt_harbison@yahoo.com>
parents: 35645
diff changeset
  2289
def _updatecatformatter(fm, ctx, matcher, path, decode):
91f0979f16c0 cat: factor out a function that populates the formatter
Matt Harbison <matt_harbison@yahoo.com>
parents: 35645
diff changeset
  2290
    """Hook for adding data to the formatter used by ``hg cat``.
91f0979f16c0 cat: factor out a function that populates the formatter
Matt Harbison <matt_harbison@yahoo.com>
parents: 35645
diff changeset
  2291
91f0979f16c0 cat: factor out a function that populates the formatter
Matt Harbison <matt_harbison@yahoo.com>
parents: 35645
diff changeset
  2292
    Extensions (e.g., lfs) can wrap this to inject keywords/data, but must call
91f0979f16c0 cat: factor out a function that populates the formatter
Matt Harbison <matt_harbison@yahoo.com>
parents: 35645
diff changeset
  2293
    this method first."""
91f0979f16c0 cat: factor out a function that populates the formatter
Matt Harbison <matt_harbison@yahoo.com>
parents: 35645
diff changeset
  2294
    data = ctx[path].data()
91f0979f16c0 cat: factor out a function that populates the formatter
Matt Harbison <matt_harbison@yahoo.com>
parents: 35645
diff changeset
  2295
    if decode:
91f0979f16c0 cat: factor out a function that populates the formatter
Matt Harbison <matt_harbison@yahoo.com>
parents: 35645
diff changeset
  2296
        data = ctx.repo().wwritedata(path, data)
91f0979f16c0 cat: factor out a function that populates the formatter
Matt Harbison <matt_harbison@yahoo.com>
parents: 35645
diff changeset
  2297
    fm.startitem()
91f0979f16c0 cat: factor out a function that populates the formatter
Matt Harbison <matt_harbison@yahoo.com>
parents: 35645
diff changeset
  2298
    fm.write('data', '%s', data)
91f0979f16c0 cat: factor out a function that populates the formatter
Matt Harbison <matt_harbison@yahoo.com>
parents: 35645
diff changeset
  2299
    fm.data(abspath=path, path=matcher.rel(path))
91f0979f16c0 cat: factor out a function that populates the formatter
Matt Harbison <matt_harbison@yahoo.com>
parents: 35645
diff changeset
  2300
32584
746e12a767b3 cat: add formatter support
Yuya Nishihara <yuya@tcha.org>
parents: 32583
diff changeset
  2301
def cat(ui, repo, ctx, matcher, basefm, fntemplate, prefix, **opts):
21040
bdf5ed5246d2 cat: move most of the implementation into cmdutils.cat()
Matt Harbison <matt_harbison@yahoo.com>
parents: 21036
diff changeset
  2302
    err = 1
35350
82ee401135dd py3: handle keyword arguments correctly in cmdutil.py
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35277
diff changeset
  2303
    opts = pycompat.byteskwargs(opts)
21040
bdf5ed5246d2 cat: move most of the implementation into cmdutils.cat()
Matt Harbison <matt_harbison@yahoo.com>
parents: 21036
diff changeset
  2304
bdf5ed5246d2 cat: move most of the implementation into cmdutils.cat()
Matt Harbison <matt_harbison@yahoo.com>
parents: 21036
diff changeset
  2305
    def write(path):
32584
746e12a767b3 cat: add formatter support
Yuya Nishihara <yuya@tcha.org>
parents: 32583
diff changeset
  2306
        filename = None
32582
7f4435078a8f cat: stop using makefileobj()
Yuya Nishihara <yuya@tcha.org>
parents: 32540
diff changeset
  2307
        if fntemplate:
36204
33ed8b511185 cmdutil: pass ctx to makefilename() in place of repo/node pair (API)
Yuya Nishihara <yuya@tcha.org>
parents: 36203
diff changeset
  2308
            filename = makefilename(ctx, fntemplate,
32582
7f4435078a8f cat: stop using makefileobj()
Yuya Nishihara <yuya@tcha.org>
parents: 32540
diff changeset
  2309
                                    pathname=os.path.join(prefix, path))
35007
407ec7f3ff02 cmdutil: create dirs for templated cat file output
Ryan McElroy <rmcelroy@fb.com>
parents: 34996
diff changeset
  2310
            # attempt to create the directory if it does not already exist
407ec7f3ff02 cmdutil: create dirs for templated cat file output
Ryan McElroy <rmcelroy@fb.com>
parents: 34996
diff changeset
  2311
            try:
407ec7f3ff02 cmdutil: create dirs for templated cat file output
Ryan McElroy <rmcelroy@fb.com>
parents: 34996
diff changeset
  2312
                os.makedirs(os.path.dirname(filename))
407ec7f3ff02 cmdutil: create dirs for templated cat file output
Ryan McElroy <rmcelroy@fb.com>
parents: 34996
diff changeset
  2313
            except OSError:
407ec7f3ff02 cmdutil: create dirs for templated cat file output
Ryan McElroy <rmcelroy@fb.com>
parents: 34996
diff changeset
  2314
                pass
37597
d110167610db formatter: carry opts to file-based formatters by basefm
Yuya Nishihara <yuya@tcha.org>
parents: 37470
diff changeset
  2315
        with formatter.maybereopen(basefm, filename) as fm:
35662
91f0979f16c0 cat: factor out a function that populates the formatter
Matt Harbison <matt_harbison@yahoo.com>
parents: 35645
diff changeset
  2316
            _updatecatformatter(fm, ctx, matcher, path, opts.get('decode'))
21040
bdf5ed5246d2 cat: move most of the implementation into cmdutils.cat()
Matt Harbison <matt_harbison@yahoo.com>
parents: 21036
diff changeset
  2317
bdf5ed5246d2 cat: move most of the implementation into cmdutils.cat()
Matt Harbison <matt_harbison@yahoo.com>
parents: 21036
diff changeset
  2318
    # Automation often uses hg cat on single files, so special case it
bdf5ed5246d2 cat: move most of the implementation into cmdutils.cat()
Matt Harbison <matt_harbison@yahoo.com>
parents: 21036
diff changeset
  2319
    # for performance to avoid the cost of parsing the manifest.
bdf5ed5246d2 cat: move most of the implementation into cmdutils.cat()
Matt Harbison <matt_harbison@yahoo.com>
parents: 21036
diff changeset
  2320
    if len(matcher.files()) == 1 and not matcher.anypats():
bdf5ed5246d2 cat: move most of the implementation into cmdutils.cat()
Matt Harbison <matt_harbison@yahoo.com>
parents: 21036
diff changeset
  2321
        file = matcher.files()[0]
30340
608ba935e041 manifest: remove manifest.find
Durham Goode <durham@fb.com>
parents: 30332
diff changeset
  2322
        mfl = repo.manifestlog
24718
a4191e0c728f cat: use ctx.manifestnode() in place of ctx._changeset[0]
Yuya Nishihara <yuya@tcha.org>
parents: 24711
diff changeset
  2323
        mfnode = ctx.manifestnode()
30340
608ba935e041 manifest: remove manifest.find
Durham Goode <durham@fb.com>
parents: 30332
diff changeset
  2324
        try:
608ba935e041 manifest: remove manifest.find
Durham Goode <durham@fb.com>
parents: 30332
diff changeset
  2325
            if mfnode and mfl[mfnode].find(file)[0]:
37762
7269b87f817c scmutil: teach the file prefetch hook to handle multiple commits
Matt Harbison <matt_harbison@yahoo.com>
parents: 37756
diff changeset
  2326
                scmutil.prefetchfiles(repo, [ctx.rev()], matcher)
30340
608ba935e041 manifest: remove manifest.find
Durham Goode <durham@fb.com>
parents: 30332
diff changeset
  2327
                write(file)
608ba935e041 manifest: remove manifest.find
Durham Goode <durham@fb.com>
parents: 30332
diff changeset
  2328
                return 0
608ba935e041 manifest: remove manifest.find
Durham Goode <durham@fb.com>
parents: 30332
diff changeset
  2329
        except KeyError:
608ba935e041 manifest: remove manifest.find
Durham Goode <durham@fb.com>
parents: 30332
diff changeset
  2330
            pass
21040
bdf5ed5246d2 cat: move most of the implementation into cmdutils.cat()
Matt Harbison <matt_harbison@yahoo.com>
parents: 21036
diff changeset
  2331
37762
7269b87f817c scmutil: teach the file prefetch hook to handle multiple commits
Matt Harbison <matt_harbison@yahoo.com>
parents: 37756
diff changeset
  2332
    scmutil.prefetchfiles(repo, [ctx.rev()], matcher)
7269b87f817c scmutil: teach the file prefetch hook to handle multiple commits
Matt Harbison <matt_harbison@yahoo.com>
parents: 37756
diff changeset
  2333
7269b87f817c scmutil: teach the file prefetch hook to handle multiple commits
Matt Harbison <matt_harbison@yahoo.com>
parents: 37756
diff changeset
  2334
    for abs in ctx.walk(matcher):
21040
bdf5ed5246d2 cat: move most of the implementation into cmdutils.cat()
Matt Harbison <matt_harbison@yahoo.com>
parents: 21036
diff changeset
  2335
        write(abs)
bdf5ed5246d2 cat: move most of the implementation into cmdutils.cat()
Matt Harbison <matt_harbison@yahoo.com>
parents: 21036
diff changeset
  2336
        err = 0
21041
a2cc3c08c3ac cat: support cat with explicit paths in subrepos
Matt Harbison <matt_harbison@yahoo.com>
parents: 21040
diff changeset
  2337
a2cc3c08c3ac cat: support cat with explicit paths in subrepos
Matt Harbison <matt_harbison@yahoo.com>
parents: 21040
diff changeset
  2338
    for subpath in sorted(ctx.substate):
a2cc3c08c3ac cat: support cat with explicit paths in subrepos
Matt Harbison <matt_harbison@yahoo.com>
parents: 21040
diff changeset
  2339
        sub = ctx.sub(subpath)
a2cc3c08c3ac cat: support cat with explicit paths in subrepos
Matt Harbison <matt_harbison@yahoo.com>
parents: 21040
diff changeset
  2340
        try:
28017
d3f1b7ee5e70 match: rename "narrowmatcher" to "subdirmatcher" (API)
Martin von Zweigbergk <martinvonz@google.com>
parents: 27985
diff changeset
  2341
            submatch = matchmod.subdirmatcher(subpath, matcher)
21041
a2cc3c08c3ac cat: support cat with explicit paths in subrepos
Matt Harbison <matt_harbison@yahoo.com>
parents: 21040
diff changeset
  2342
32584
746e12a767b3 cat: add formatter support
Yuya Nishihara <yuya@tcha.org>
parents: 32583
diff changeset
  2343
            if not sub.cat(submatch, basefm, fntemplate,
35350
82ee401135dd py3: handle keyword arguments correctly in cmdutil.py
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35277
diff changeset
  2344
                           os.path.join(prefix, sub._path),
82ee401135dd py3: handle keyword arguments correctly in cmdutil.py
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35277
diff changeset
  2345
                           **pycompat.strkwargs(opts)):
21041
a2cc3c08c3ac cat: support cat with explicit paths in subrepos
Matt Harbison <matt_harbison@yahoo.com>
parents: 21040
diff changeset
  2346
                err = 0
a2cc3c08c3ac cat: support cat with explicit paths in subrepos
Matt Harbison <matt_harbison@yahoo.com>
parents: 21040
diff changeset
  2347
        except error.RepoLookupError:
a2cc3c08c3ac cat: support cat with explicit paths in subrepos
Matt Harbison <matt_harbison@yahoo.com>
parents: 21040
diff changeset
  2348
            ui.status(_("skipping missing subrepository: %s\n")
a2cc3c08c3ac cat: support cat with explicit paths in subrepos
Matt Harbison <matt_harbison@yahoo.com>
parents: 21040
diff changeset
  2349
                           % os.path.join(prefix, subpath))
a2cc3c08c3ac cat: support cat with explicit paths in subrepos
Matt Harbison <matt_harbison@yahoo.com>
parents: 21040
diff changeset
  2350
21040
bdf5ed5246d2 cat: move most of the implementation into cmdutils.cat()
Matt Harbison <matt_harbison@yahoo.com>
parents: 21036
diff changeset
  2351
    return err
bdf5ed5246d2 cat: move most of the implementation into cmdutils.cat()
Matt Harbison <matt_harbison@yahoo.com>
parents: 21036
diff changeset
  2352
5034
c0417a319e39 commands: move commit to cmdutil as wrapper for commit-like functions
Bryan O'Sullivan <bos@serpentine.com>
parents: 4965
diff changeset
  2353
def commit(ui, repo, commitfunc, pats, opts):
c0417a319e39 commands: move commit to cmdutil as wrapper for commit-like functions
Bryan O'Sullivan <bos@serpentine.com>
parents: 4965
diff changeset
  2354
    '''commit the specified files or all outstanding changes'''
6139
989467e8e3a9 Fix bad behaviour when specifying an invalid date (issue700)
Thomas Arendsen Hein <thomas@intevation.de>
parents: 6112
diff changeset
  2355
    date = opts.get('date')
989467e8e3a9 Fix bad behaviour when specifying an invalid date (issue700)
Thomas Arendsen Hein <thomas@intevation.de>
parents: 6112
diff changeset
  2356
    if date:
36607
c6061cadb400 util: extract all date-related utils in utils/dateutil module
Boris Feld <boris.feld@octobus.net>
parents: 36511
diff changeset
  2357
        opts['date'] = dateutil.parsedate(date)
14635
217b7d83afc3 cmdutil, logmessage: use ui.fin when reading from '-'
Idan Kamara <idankk86@gmail.com>
parents: 14518
diff changeset
  2358
    message = logmessage(ui, opts)
23533
891aaa7c0c70 scmutil: pass a matcher to scmutil.addremove() instead of a list of patterns
Matt Harbison <matt_harbison@yahoo.com>
parents: 23509
diff changeset
  2359
    matcher = scmutil.match(repo[None], pats, opts)
5034
c0417a319e39 commands: move commit to cmdutil as wrapper for commit-like functions
Bryan O'Sullivan <bos@serpentine.com>
parents: 4965
diff changeset
  2360
33617
5ac845ca059a commit: don't let failed commit with --addremove update dirstate (issue5645)
Martin von Zweigbergk <martinvonz@google.com>
parents: 33548
diff changeset
  2361
    dsguard = None
5829
784073457a0f cmdutil.commit: extract 'addremove' from opts carefully
Kirill Smelkov <kirr@mns.spb.ru>
parents: 5797
diff changeset
  2362
    # extract addremove carefully -- this function can be called from a command
784073457a0f cmdutil.commit: extract 'addremove' from opts carefully
Kirill Smelkov <kirr@mns.spb.ru>
parents: 5797
diff changeset
  2363
    # that doesn't support addremove
33823
5d286eb7009f commit: move dirstateguard creation out of try-block
Martin von Zweigbergk <martinvonz@google.com>
parents: 33783
diff changeset
  2364
    if opts.get('addremove'):
5d286eb7009f commit: move dirstateguard creation out of try-block
Martin von Zweigbergk <martinvonz@google.com>
parents: 33783
diff changeset
  2365
        dsguard = dirstateguard.dirstateguard(repo, 'commit')
33824
158dddc635ff commit: use context manager with dirstateguard
Martin von Zweigbergk <martinvonz@google.com>
parents: 33823
diff changeset
  2366
    with dsguard or util.nullcontextmanager():
33823
5d286eb7009f commit: move dirstateguard creation out of try-block
Martin von Zweigbergk <martinvonz@google.com>
parents: 33783
diff changeset
  2367
        if dsguard:
33617
5ac845ca059a commit: don't let failed commit with --addremove update dirstate (issue5645)
Martin von Zweigbergk <martinvonz@google.com>
parents: 33548
diff changeset
  2368
            if scmutil.addremove(repo, matcher, "", opts) != 0:
5ac845ca059a commit: don't let failed commit with --addremove update dirstate (issue5645)
Martin von Zweigbergk <martinvonz@google.com>
parents: 33548
diff changeset
  2369
                raise error.Abort(
5ac845ca059a commit: don't let failed commit with --addremove update dirstate (issue5645)
Martin von Zweigbergk <martinvonz@google.com>
parents: 33548
diff changeset
  2370
                    _("failed to mark all new/missing files as added/removed"))
5ac845ca059a commit: don't let failed commit with --addremove update dirstate (issue5645)
Martin von Zweigbergk <martinvonz@google.com>
parents: 33548
diff changeset
  2371
33824
158dddc635ff commit: use context manager with dirstateguard
Martin von Zweigbergk <martinvonz@google.com>
parents: 33823
diff changeset
  2372
        return commitfunc(ui, repo, message, matcher, opts)
8407
223000a687b0 commit: move commit editor to cmdutil, pass as function
Matt Mackall <mpm@selenic.com>
parents: 8390
diff changeset
  2373
29819
2cec6eaf3610 cmdutil: extract samefile function from amend()
Hannes Oldenburg <hannes.christian.oldenburg@gmail.com>
parents: 29802
diff changeset
  2374
def samefile(f, ctx1, ctx2):
2cec6eaf3610 cmdutil: extract samefile function from amend()
Hannes Oldenburg <hannes.christian.oldenburg@gmail.com>
parents: 29802
diff changeset
  2375
    if f in ctx1.manifest():
2cec6eaf3610 cmdutil: extract samefile function from amend()
Hannes Oldenburg <hannes.christian.oldenburg@gmail.com>
parents: 29802
diff changeset
  2376
        a = ctx1.filectx(f)
2cec6eaf3610 cmdutil: extract samefile function from amend()
Hannes Oldenburg <hannes.christian.oldenburg@gmail.com>
parents: 29802
diff changeset
  2377
        if f in ctx2.manifest():
2cec6eaf3610 cmdutil: extract samefile function from amend()
Hannes Oldenburg <hannes.christian.oldenburg@gmail.com>
parents: 29802
diff changeset
  2378
            b = ctx2.filectx(f)
2cec6eaf3610 cmdutil: extract samefile function from amend()
Hannes Oldenburg <hannes.christian.oldenburg@gmail.com>
parents: 29802
diff changeset
  2379
            return (not a.cmp(b)
2cec6eaf3610 cmdutil: extract samefile function from amend()
Hannes Oldenburg <hannes.christian.oldenburg@gmail.com>
parents: 29802
diff changeset
  2380
                    and a.flags() == b.flags())
2cec6eaf3610 cmdutil: extract samefile function from amend()
Hannes Oldenburg <hannes.christian.oldenburg@gmail.com>
parents: 29802
diff changeset
  2381
        else:
2cec6eaf3610 cmdutil: extract samefile function from amend()
Hannes Oldenburg <hannes.christian.oldenburg@gmail.com>
parents: 29802
diff changeset
  2382
            return False
2cec6eaf3610 cmdutil: extract samefile function from amend()
Hannes Oldenburg <hannes.christian.oldenburg@gmail.com>
parents: 29802
diff changeset
  2383
    else:
2cec6eaf3610 cmdutil: extract samefile function from amend()
Hannes Oldenburg <hannes.christian.oldenburg@gmail.com>
parents: 29802
diff changeset
  2384
        return f not in ctx2.manifest()
2cec6eaf3610 cmdutil: extract samefile function from amend()
Hannes Oldenburg <hannes.christian.oldenburg@gmail.com>
parents: 29802
diff changeset
  2385
34086
a39dce4a76b8 cmdutil: remove redundant commitfunc parameter in amend (API)
Saurabh Singh <singhsrb@fb.com>
parents: 34085
diff changeset
  2386
def amend(ui, repo, old, extra, pats, opts):
25930
221491bbaf7e cmdutil: break import cycle
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25795
diff changeset
  2387
    # avoid cycle context -> subrepo -> cmdutil
28322
ebd0e86bdf89 cmdutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 28313
diff changeset
  2388
    from . import context
25930
221491bbaf7e cmdutil: break import cycle
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25795
diff changeset
  2389
23101
b564330d4b1f amend: abort early if no username is configured with evolve enabled (issue4211)
Matt Harbison <matt_harbison@yahoo.com>
parents: 22951
diff changeset
  2390
    # amend will reuse the existing user if not specified, but the obsolete
b564330d4b1f amend: abort early if no username is configured with evolve enabled (issue4211)
Matt Harbison <matt_harbison@yahoo.com>
parents: 22951
diff changeset
  2391
    # marker creation requires that the current user's name is specified.
24379
8c445d8a915b obsolete: remove last instance of _enabled
Durham Goode <durham@fb.com>
parents: 24364
diff changeset
  2392
    if obsolete.isenabled(repo, obsolete.createmarkersopt):
23101
b564330d4b1f amend: abort early if no username is configured with evolve enabled (issue4211)
Matt Harbison <matt_harbison@yahoo.com>
parents: 22951
diff changeset
  2393
        ui.username() # raise exception if username not set
b564330d4b1f amend: abort early if no username is configured with evolve enabled (issue4211)
Matt Harbison <matt_harbison@yahoo.com>
parents: 22951
diff changeset
  2394
16458
55982f62651f commit: add option to amend the working dir parent
Idan Kamara <idankk86@gmail.com>
parents: 16430
diff changeset
  2395
    ui.note(_('amending changeset %s\n') % old)
55982f62651f commit: add option to amend the working dir parent
Idan Kamara <idankk86@gmail.com>
parents: 16430
diff changeset
  2396
    base = old.p1()
55982f62651f commit: add option to amend the working dir parent
Idan Kamara <idankk86@gmail.com>
parents: 16430
diff changeset
  2397
33509
a3acacbd0ff3 bookmark: remove a useless 'recordchange' in the amend code
Boris Feld <boris.feld@octobus.net>
parents: 33499
diff changeset
  2398
    with repo.wlock(), repo.lock(), repo.transaction('amend'):
33438
8056481caa81 codemod: simplify nested withs
Jun Wu <quark@fb.com>
parents: 33362
diff changeset
  2399
        # Participating changesets:
8056481caa81 codemod: simplify nested withs
Jun Wu <quark@fb.com>
parents: 33362
diff changeset
  2400
        #
34085
e8a7c1a0565a cmdutil: remove the redundant commit during amend
Saurabh Singh <singhsrb@fb.com>
parents: 34083
diff changeset
  2401
        # wctx     o - workingctx that contains changes from working copy
e8a7c1a0565a cmdutil: remove the redundant commit during amend
Saurabh Singh <singhsrb@fb.com>
parents: 34083
diff changeset
  2402
        #          |   to go into amending commit
33438
8056481caa81 codemod: simplify nested withs
Jun Wu <quark@fb.com>
parents: 33362
diff changeset
  2403
        #          |
8056481caa81 codemod: simplify nested withs
Jun Wu <quark@fb.com>
parents: 33362
diff changeset
  2404
        # old      o - changeset to amend
8056481caa81 codemod: simplify nested withs
Jun Wu <quark@fb.com>
parents: 33362
diff changeset
  2405
        #          |
34056
7e9ccb1670e3 amend: rectify comment
Saurabh Singh <singhsrb@fb.com>
parents: 34055
diff changeset
  2406
        # base     o - first parent of the changeset to amend
34085
e8a7c1a0565a cmdutil: remove the redundant commit during amend
Saurabh Singh <singhsrb@fb.com>
parents: 34083
diff changeset
  2407
        wctx = repo[None]
33438
8056481caa81 codemod: simplify nested withs
Jun Wu <quark@fb.com>
parents: 33362
diff changeset
  2408
35196
5cc14407a739 amend: make a copy of "extra" to avoid mutating an input
Martin von Zweigbergk <martinvonz@google.com>
parents: 35163
diff changeset
  2409
        # Copy to avoid mutating input
5cc14407a739 amend: make a copy of "extra" to avoid mutating an input
Martin von Zweigbergk <martinvonz@google.com>
parents: 35163
diff changeset
  2410
        extra = extra.copy()
33438
8056481caa81 codemod: simplify nested withs
Jun Wu <quark@fb.com>
parents: 33362
diff changeset
  2411
        # Update extra dict from amended commit (e.g. to preserve graft
8056481caa81 codemod: simplify nested withs
Jun Wu <quark@fb.com>
parents: 33362
diff changeset
  2412
        # source)
8056481caa81 codemod: simplify nested withs
Jun Wu <quark@fb.com>
parents: 33362
diff changeset
  2413
        extra.update(old.extra())
8056481caa81 codemod: simplify nested withs
Jun Wu <quark@fb.com>
parents: 33362
diff changeset
  2414
34085
e8a7c1a0565a cmdutil: remove the redundant commit during amend
Saurabh Singh <singhsrb@fb.com>
parents: 34083
diff changeset
  2415
        # Also update it from the from the wctx
e8a7c1a0565a cmdutil: remove the redundant commit during amend
Saurabh Singh <singhsrb@fb.com>
parents: 34083
diff changeset
  2416
        extra.update(wctx.extra())
e8a7c1a0565a cmdutil: remove the redundant commit during amend
Saurabh Singh <singhsrb@fb.com>
parents: 34083
diff changeset
  2417
e8a7c1a0565a cmdutil: remove the redundant commit during amend
Saurabh Singh <singhsrb@fb.com>
parents: 34083
diff changeset
  2418
        user = opts.get('user') or old.user()
e8a7c1a0565a cmdutil: remove the redundant commit during amend
Saurabh Singh <singhsrb@fb.com>
parents: 34083
diff changeset
  2419
        date = opts.get('date') or old.date()
33438
8056481caa81 codemod: simplify nested withs
Jun Wu <quark@fb.com>
parents: 33362
diff changeset
  2420
34121
ae95853c250a cmdutil: fix amend when passing a date
Boris Feld <boris.feld@octobus.net>
parents: 34086
diff changeset
  2421
        # Parse the date to allow comparison between date and old.date()
36607
c6061cadb400 util: extract all date-related utils in utils/dateutil module
Boris Feld <boris.feld@octobus.net>
parents: 36511
diff changeset
  2422
        date = dateutil.parsedate(date)
34121
ae95853c250a cmdutil: fix amend when passing a date
Boris Feld <boris.feld@octobus.net>
parents: 34086
diff changeset
  2423
33438
8056481caa81 codemod: simplify nested withs
Jun Wu <quark@fb.com>
parents: 33362
diff changeset
  2424
        if len(old.parents()) > 1:
8056481caa81 codemod: simplify nested withs
Jun Wu <quark@fb.com>
parents: 33362
diff changeset
  2425
            # ctx.files() isn't reliable for merges, so fall back to the
8056481caa81 codemod: simplify nested withs
Jun Wu <quark@fb.com>
parents: 33362
diff changeset
  2426
            # slower repo.status() method
8056481caa81 codemod: simplify nested withs
Jun Wu <quark@fb.com>
parents: 33362
diff changeset
  2427
            files = set([fn for st in repo.status(base, old)[:3]
8056481caa81 codemod: simplify nested withs
Jun Wu <quark@fb.com>
parents: 33362
diff changeset
  2428
                         for fn in st])
8056481caa81 codemod: simplify nested withs
Jun Wu <quark@fb.com>
parents: 33362
diff changeset
  2429
        else:
8056481caa81 codemod: simplify nested withs
Jun Wu <quark@fb.com>
parents: 33362
diff changeset
  2430
            files = set(old.files())
8056481caa81 codemod: simplify nested withs
Jun Wu <quark@fb.com>
parents: 33362
diff changeset
  2431
34085
e8a7c1a0565a cmdutil: remove the redundant commit during amend
Saurabh Singh <singhsrb@fb.com>
parents: 34083
diff changeset
  2432
        # add/remove the files to the working copy if the "addremove" option
e8a7c1a0565a cmdutil: remove the redundant commit during amend
Saurabh Singh <singhsrb@fb.com>
parents: 34083
diff changeset
  2433
        # was specified.
e8a7c1a0565a cmdutil: remove the redundant commit during amend
Saurabh Singh <singhsrb@fb.com>
parents: 34083
diff changeset
  2434
        matcher = scmutil.match(wctx, pats, opts)
e8a7c1a0565a cmdutil: remove the redundant commit during amend
Saurabh Singh <singhsrb@fb.com>
parents: 34083
diff changeset
  2435
        if (opts.get('addremove')
e8a7c1a0565a cmdutil: remove the redundant commit during amend
Saurabh Singh <singhsrb@fb.com>
parents: 34083
diff changeset
  2436
            and scmutil.addremove(repo, matcher, "", opts)):
e8a7c1a0565a cmdutil: remove the redundant commit during amend
Saurabh Singh <singhsrb@fb.com>
parents: 34083
diff changeset
  2437
            raise error.Abort(
e8a7c1a0565a cmdutil: remove the redundant commit during amend
Saurabh Singh <singhsrb@fb.com>
parents: 34083
diff changeset
  2438
                _("failed to mark all new/missing files as added/removed"))
e8a7c1a0565a cmdutil: remove the redundant commit during amend
Saurabh Singh <singhsrb@fb.com>
parents: 34083
diff changeset
  2439
35026
691524f0bbf6 amend: update .hgsubstate before committing a memctx (issue5677)
Yuya Nishihara <yuya@tcha.org>
parents: 35023
diff changeset
  2440
        # Check subrepos. This depends on in-place wctx._status update in
691524f0bbf6 amend: update .hgsubstate before committing a memctx (issue5677)
Yuya Nishihara <yuya@tcha.org>
parents: 35023
diff changeset
  2441
        # subrepo.precommit(). To minimize the risk of this hack, we do
691524f0bbf6 amend: update .hgsubstate before committing a memctx (issue5677)
Yuya Nishihara <yuya@tcha.org>
parents: 35023
diff changeset
  2442
        # nothing if .hgsub does not exist.
691524f0bbf6 amend: update .hgsubstate before committing a memctx (issue5677)
Yuya Nishihara <yuya@tcha.org>
parents: 35023
diff changeset
  2443
        if '.hgsub' in wctx or '.hgsub' in old:
36009
55e8efa2451a subrepo: split non-core functions to new module
Yuya Nishihara <yuya@tcha.org>
parents: 35954
diff changeset
  2444
            subs, commitsubs, newsubstate = subrepoutil.precommit(
35026
691524f0bbf6 amend: update .hgsubstate before committing a memctx (issue5677)
Yuya Nishihara <yuya@tcha.org>
parents: 35023
diff changeset
  2445
                ui, wctx, wctx._status, matcher)
691524f0bbf6 amend: update .hgsubstate before committing a memctx (issue5677)
Yuya Nishihara <yuya@tcha.org>
parents: 35023
diff changeset
  2446
            # amend should abort if commitsubrepos is enabled
691524f0bbf6 amend: update .hgsubstate before committing a memctx (issue5677)
Yuya Nishihara <yuya@tcha.org>
parents: 35023
diff changeset
  2447
            assert not commitsubs
691524f0bbf6 amend: update .hgsubstate before committing a memctx (issue5677)
Yuya Nishihara <yuya@tcha.org>
parents: 35023
diff changeset
  2448
            if subs:
36009
55e8efa2451a subrepo: split non-core functions to new module
Yuya Nishihara <yuya@tcha.org>
parents: 35954
diff changeset
  2449
                subrepoutil.writestate(repo, newsubstate)
35026
691524f0bbf6 amend: update .hgsubstate before committing a memctx (issue5677)
Yuya Nishihara <yuya@tcha.org>
parents: 35023
diff changeset
  2450
36844
eeb87b24aea7 amend: abort if unresolved merge conflicts found (issue5805)
Yuya Nishihara <yuya@tcha.org>
parents: 35746
diff changeset
  2451
        ms = mergemod.mergestate.read(repo)
eeb87b24aea7 amend: abort if unresolved merge conflicts found (issue5805)
Yuya Nishihara <yuya@tcha.org>
parents: 35746
diff changeset
  2452
        mergeutil.checkunresolved(ms)
eeb87b24aea7 amend: abort if unresolved merge conflicts found (issue5805)
Yuya Nishihara <yuya@tcha.org>
parents: 35746
diff changeset
  2453
34085
e8a7c1a0565a cmdutil: remove the redundant commit during amend
Saurabh Singh <singhsrb@fb.com>
parents: 34083
diff changeset
  2454
        filestoamend = set(f for f in wctx.files() if matcher(f))
e8a7c1a0565a cmdutil: remove the redundant commit during amend
Saurabh Singh <singhsrb@fb.com>
parents: 34083
diff changeset
  2455
e8a7c1a0565a cmdutil: remove the redundant commit during amend
Saurabh Singh <singhsrb@fb.com>
parents: 34083
diff changeset
  2456
        changes = (len(filestoamend) > 0)
e8a7c1a0565a cmdutil: remove the redundant commit during amend
Saurabh Singh <singhsrb@fb.com>
parents: 34083
diff changeset
  2457
        if changes:
33438
8056481caa81 codemod: simplify nested withs
Jun Wu <quark@fb.com>
parents: 33362
diff changeset
  2458
            # Recompute copies (avoid recording a -> b -> a)
34085
e8a7c1a0565a cmdutil: remove the redundant commit during amend
Saurabh Singh <singhsrb@fb.com>
parents: 34083
diff changeset
  2459
            copied = copies.pathcopies(base, wctx, matcher)
33438
8056481caa81 codemod: simplify nested withs
Jun Wu <quark@fb.com>
parents: 33362
diff changeset
  2460
            if old.p2:
34085
e8a7c1a0565a cmdutil: remove the redundant commit during amend
Saurabh Singh <singhsrb@fb.com>
parents: 34083
diff changeset
  2461
                copied.update(copies.pathcopies(old.p2(), wctx, matcher))
33438
8056481caa81 codemod: simplify nested withs
Jun Wu <quark@fb.com>
parents: 33362
diff changeset
  2462
8056481caa81 codemod: simplify nested withs
Jun Wu <quark@fb.com>
parents: 33362
diff changeset
  2463
            # Prune files which were reverted by the updates: if old
34085
e8a7c1a0565a cmdutil: remove the redundant commit during amend
Saurabh Singh <singhsrb@fb.com>
parents: 34083
diff changeset
  2464
            # introduced file X and the file was renamed in the working
e8a7c1a0565a cmdutil: remove the redundant commit during amend
Saurabh Singh <singhsrb@fb.com>
parents: 34083
diff changeset
  2465
            # copy, then those two files are the same and
33438
8056481caa81 codemod: simplify nested withs
Jun Wu <quark@fb.com>
parents: 33362
diff changeset
  2466
            # we can discard X from our list of files. Likewise if X
35023
5f40efa472db amend: do not drop missing files (issue5732)
Yuya Nishihara <yuya@tcha.org>
parents: 35022
diff changeset
  2467
            # was removed, it's no longer relevant. If X is missing (aka
5f40efa472db amend: do not drop missing files (issue5732)
Yuya Nishihara <yuya@tcha.org>
parents: 35022
diff changeset
  2468
            # deleted), old X must be preserved.
34085
e8a7c1a0565a cmdutil: remove the redundant commit during amend
Saurabh Singh <singhsrb@fb.com>
parents: 34083
diff changeset
  2469
            files.update(filestoamend)
35023
5f40efa472db amend: do not drop missing files (issue5732)
Yuya Nishihara <yuya@tcha.org>
parents: 35022
diff changeset
  2470
            files = [f for f in files if (not samefile(f, wctx, base)
5f40efa472db amend: do not drop missing files (issue5732)
Yuya Nishihara <yuya@tcha.org>
parents: 35022
diff changeset
  2471
                                          or f in wctx.deleted())]
33438
8056481caa81 codemod: simplify nested withs
Jun Wu <quark@fb.com>
parents: 33362
diff changeset
  2472
8056481caa81 codemod: simplify nested withs
Jun Wu <quark@fb.com>
parents: 33362
diff changeset
  2473
            def filectxfn(repo, ctx_, path):
8056481caa81 codemod: simplify nested withs
Jun Wu <quark@fb.com>
parents: 33362
diff changeset
  2474
                try:
34085
e8a7c1a0565a cmdutil: remove the redundant commit during amend
Saurabh Singh <singhsrb@fb.com>
parents: 34083
diff changeset
  2475
                    # If the file being considered is not amongst the files
e8a7c1a0565a cmdutil: remove the redundant commit during amend
Saurabh Singh <singhsrb@fb.com>
parents: 34083
diff changeset
  2476
                    # to be amended, we should return the file context from the
e8a7c1a0565a cmdutil: remove the redundant commit during amend
Saurabh Singh <singhsrb@fb.com>
parents: 34083
diff changeset
  2477
                    # old changeset. This avoids issues when only some files in
e8a7c1a0565a cmdutil: remove the redundant commit during amend
Saurabh Singh <singhsrb@fb.com>
parents: 34083
diff changeset
  2478
                    # the working copy are being amended but there are also
e8a7c1a0565a cmdutil: remove the redundant commit during amend
Saurabh Singh <singhsrb@fb.com>
parents: 34083
diff changeset
  2479
                    # changes to other files from the old changeset.
e8a7c1a0565a cmdutil: remove the redundant commit during amend
Saurabh Singh <singhsrb@fb.com>
parents: 34083
diff changeset
  2480
                    if path not in filestoamend:
e8a7c1a0565a cmdutil: remove the redundant commit during amend
Saurabh Singh <singhsrb@fb.com>
parents: 34083
diff changeset
  2481
                        return old.filectx(path)
e8a7c1a0565a cmdutil: remove the redundant commit during amend
Saurabh Singh <singhsrb@fb.com>
parents: 34083
diff changeset
  2482
35022
0ba3b928f9a9 amend: do not take untracked files as modified or clean (issue5732)
Yuya Nishihara <yuya@tcha.org>
parents: 34981
diff changeset
  2483
                    # Return None for removed files.
0ba3b928f9a9 amend: do not take untracked files as modified or clean (issue5732)
Yuya Nishihara <yuya@tcha.org>
parents: 34981
diff changeset
  2484
                    if path in wctx.removed():
0ba3b928f9a9 amend: do not take untracked files as modified or clean (issue5732)
Yuya Nishihara <yuya@tcha.org>
parents: 34981
diff changeset
  2485
                        return None
0ba3b928f9a9 amend: do not take untracked files as modified or clean (issue5732)
Yuya Nishihara <yuya@tcha.org>
parents: 34981
diff changeset
  2486
34085
e8a7c1a0565a cmdutil: remove the redundant commit during amend
Saurabh Singh <singhsrb@fb.com>
parents: 34083
diff changeset
  2487
                    fctx = wctx[path]
33438
8056481caa81 codemod: simplify nested withs
Jun Wu <quark@fb.com>
parents: 33362
diff changeset
  2488
                    flags = fctx.flags()
35400
8a0cac20a1ad memfilectx: make changectx argument mandatory in constructor (API)
Martin von Zweigbergk <martinvonz@google.com>
parents: 35350
diff changeset
  2489
                    mctx = context.memfilectx(repo, ctx_,
33438
8056481caa81 codemod: simplify nested withs
Jun Wu <quark@fb.com>
parents: 33362
diff changeset
  2490
                                              fctx.path(), fctx.data(),
8056481caa81 codemod: simplify nested withs
Jun Wu <quark@fb.com>
parents: 33362
diff changeset
  2491
                                              islink='l' in flags,
8056481caa81 codemod: simplify nested withs
Jun Wu <quark@fb.com>
parents: 33362
diff changeset
  2492
                                              isexec='x' in flags,
8056481caa81 codemod: simplify nested withs
Jun Wu <quark@fb.com>
parents: 33362
diff changeset
  2493
                                              copied=copied.get(path))
8056481caa81 codemod: simplify nested withs
Jun Wu <quark@fb.com>
parents: 33362
diff changeset
  2494
                    return mctx
8056481caa81 codemod: simplify nested withs
Jun Wu <quark@fb.com>
parents: 33362
diff changeset
  2495
                except KeyError:
8056481caa81 codemod: simplify nested withs
Jun Wu <quark@fb.com>
parents: 33362
diff changeset
  2496
                    return None
8056481caa81 codemod: simplify nested withs
Jun Wu <quark@fb.com>
parents: 33362
diff changeset
  2497
        else:
8056481caa81 codemod: simplify nested withs
Jun Wu <quark@fb.com>
parents: 33362
diff changeset
  2498
            ui.note(_('copying changeset %s to %s\n') % (old, base))
8056481caa81 codemod: simplify nested withs
Jun Wu <quark@fb.com>
parents: 33362
diff changeset
  2499
8056481caa81 codemod: simplify nested withs
Jun Wu <quark@fb.com>
parents: 33362
diff changeset
  2500
            # Use version of files as in the old cset
8056481caa81 codemod: simplify nested withs
Jun Wu <quark@fb.com>
parents: 33362
diff changeset
  2501
            def filectxfn(repo, ctx_, path):
8056481caa81 codemod: simplify nested withs
Jun Wu <quark@fb.com>
parents: 33362
diff changeset
  2502
                try:
8056481caa81 codemod: simplify nested withs
Jun Wu <quark@fb.com>
parents: 33362
diff changeset
  2503
                    return old.filectx(path)
8056481caa81 codemod: simplify nested withs
Jun Wu <quark@fb.com>
parents: 33362
diff changeset
  2504
                except KeyError:
8056481caa81 codemod: simplify nested withs
Jun Wu <quark@fb.com>
parents: 33362
diff changeset
  2505
                    return None
8056481caa81 codemod: simplify nested withs
Jun Wu <quark@fb.com>
parents: 33362
diff changeset
  2506
34085
e8a7c1a0565a cmdutil: remove the redundant commit during amend
Saurabh Singh <singhsrb@fb.com>
parents: 34083
diff changeset
  2507
        # See if we got a message from -m or -l, if not, open the editor with
e8a7c1a0565a cmdutil: remove the redundant commit during amend
Saurabh Singh <singhsrb@fb.com>
parents: 34083
diff changeset
  2508
        # the message of the changeset to amend.
e8a7c1a0565a cmdutil: remove the redundant commit during amend
Saurabh Singh <singhsrb@fb.com>
parents: 34083
diff changeset
  2509
        message = logmessage(ui, opts)
e8a7c1a0565a cmdutil: remove the redundant commit during amend
Saurabh Singh <singhsrb@fb.com>
parents: 34083
diff changeset
  2510
33438
8056481caa81 codemod: simplify nested withs
Jun Wu <quark@fb.com>
parents: 33362
diff changeset
  2511
        editform = mergeeditform(old, 'commit.amend')
8056481caa81 codemod: simplify nested withs
Jun Wu <quark@fb.com>
parents: 33362
diff changeset
  2512
        editor = getcommiteditor(editform=editform,
8056481caa81 codemod: simplify nested withs
Jun Wu <quark@fb.com>
parents: 33362
diff changeset
  2513
                                 **pycompat.strkwargs(opts))
34085
e8a7c1a0565a cmdutil: remove the redundant commit during amend
Saurabh Singh <singhsrb@fb.com>
parents: 34083
diff changeset
  2514
33438
8056481caa81 codemod: simplify nested withs
Jun Wu <quark@fb.com>
parents: 33362
diff changeset
  2515
        if not message:
8056481caa81 codemod: simplify nested withs
Jun Wu <quark@fb.com>
parents: 33362
diff changeset
  2516
            editor = getcommiteditor(edit=True, editform=editform)
8056481caa81 codemod: simplify nested withs
Jun Wu <quark@fb.com>
parents: 33362
diff changeset
  2517
            message = old.description()
8056481caa81 codemod: simplify nested withs
Jun Wu <quark@fb.com>
parents: 33362
diff changeset
  2518
8056481caa81 codemod: simplify nested withs
Jun Wu <quark@fb.com>
parents: 33362
diff changeset
  2519
        pureextra = extra.copy()
8056481caa81 codemod: simplify nested withs
Jun Wu <quark@fb.com>
parents: 33362
diff changeset
  2520
        extra['amend_source'] = old.hex()
8056481caa81 codemod: simplify nested withs
Jun Wu <quark@fb.com>
parents: 33362
diff changeset
  2521
8056481caa81 codemod: simplify nested withs
Jun Wu <quark@fb.com>
parents: 33362
diff changeset
  2522
        new = context.memctx(repo,
8056481caa81 codemod: simplify nested withs
Jun Wu <quark@fb.com>
parents: 33362
diff changeset
  2523
                             parents=[base.node(), old.p2().node()],
8056481caa81 codemod: simplify nested withs
Jun Wu <quark@fb.com>
parents: 33362
diff changeset
  2524
                             text=message,
8056481caa81 codemod: simplify nested withs
Jun Wu <quark@fb.com>
parents: 33362
diff changeset
  2525
                             files=files,
8056481caa81 codemod: simplify nested withs
Jun Wu <quark@fb.com>
parents: 33362
diff changeset
  2526
                             filectxfn=filectxfn,
8056481caa81 codemod: simplify nested withs
Jun Wu <quark@fb.com>
parents: 33362
diff changeset
  2527
                             user=user,
8056481caa81 codemod: simplify nested withs
Jun Wu <quark@fb.com>
parents: 33362
diff changeset
  2528
                             date=date,
8056481caa81 codemod: simplify nested withs
Jun Wu <quark@fb.com>
parents: 33362
diff changeset
  2529
                             extra=extra,
8056481caa81 codemod: simplify nested withs
Jun Wu <quark@fb.com>
parents: 33362
diff changeset
  2530
                             editor=editor)
8056481caa81 codemod: simplify nested withs
Jun Wu <quark@fb.com>
parents: 33362
diff changeset
  2531
8056481caa81 codemod: simplify nested withs
Jun Wu <quark@fb.com>
parents: 33362
diff changeset
  2532
        newdesc = changelog.stripdesc(new.description())
34085
e8a7c1a0565a cmdutil: remove the redundant commit during amend
Saurabh Singh <singhsrb@fb.com>
parents: 34083
diff changeset
  2533
        if ((not changes)
33438
8056481caa81 codemod: simplify nested withs
Jun Wu <quark@fb.com>
parents: 33362
diff changeset
  2534
            and newdesc == old.description()
8056481caa81 codemod: simplify nested withs
Jun Wu <quark@fb.com>
parents: 33362
diff changeset
  2535
            and user == old.user()
8056481caa81 codemod: simplify nested withs
Jun Wu <quark@fb.com>
parents: 33362
diff changeset
  2536
            and date == old.date()
8056481caa81 codemod: simplify nested withs
Jun Wu <quark@fb.com>
parents: 33362
diff changeset
  2537
            and pureextra == old.extra()):
8056481caa81 codemod: simplify nested withs
Jun Wu <quark@fb.com>
parents: 33362
diff changeset
  2538
            # nothing changed. continuing here would create a new node
8056481caa81 codemod: simplify nested withs
Jun Wu <quark@fb.com>
parents: 33362
diff changeset
  2539
            # anyway because of the amend_source noise.
17472
965fbe04fd96 amend: wrap all commit operations in a single transaction
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17471
diff changeset
  2540
            #
33438
8056481caa81 codemod: simplify nested withs
Jun Wu <quark@fb.com>
parents: 33362
diff changeset
  2541
            # This not what we expect from amend.
8056481caa81 codemod: simplify nested withs
Jun Wu <quark@fb.com>
parents: 33362
diff changeset
  2542
            return old.node()
8056481caa81 codemod: simplify nested withs
Jun Wu <quark@fb.com>
parents: 33362
diff changeset
  2543
34081
5dc6ac6555e6 amend: use context manager for config override
Martin von Zweigbergk <martinvonz@google.com>
parents: 34080
diff changeset
  2544
        if opts.get('secret'):
5dc6ac6555e6 amend: use context manager for config override
Martin von Zweigbergk <martinvonz@google.com>
parents: 34080
diff changeset
  2545
            commitphase = 'secret'
5dc6ac6555e6 amend: use context manager for config override
Martin von Zweigbergk <martinvonz@google.com>
parents: 34080
diff changeset
  2546
        else:
5dc6ac6555e6 amend: use context manager for config override
Martin von Zweigbergk <martinvonz@google.com>
parents: 34080
diff changeset
  2547
            commitphase = old.phase()
5dc6ac6555e6 amend: use context manager for config override
Martin von Zweigbergk <martinvonz@google.com>
parents: 34080
diff changeset
  2548
        overrides = {('phases', 'new-commit'): commitphase}
5dc6ac6555e6 amend: use context manager for config override
Martin von Zweigbergk <martinvonz@google.com>
parents: 34080
diff changeset
  2549
        with ui.configoverride(overrides, 'amend'):
33438
8056481caa81 codemod: simplify nested withs
Jun Wu <quark@fb.com>
parents: 33362
diff changeset
  2550
            newid = repo.commitctx(new)
34055
ae92e5c0441c amend: removing redundant if condition
Saurabh Singh <singhsrb@fb.com>
parents: 34029
diff changeset
  2551
ae92e5c0441c amend: removing redundant if condition
Saurabh Singh <singhsrb@fb.com>
parents: 34029
diff changeset
  2552
        # Reroute the working copy parent to the new changeset
ae92e5c0441c amend: removing redundant if condition
Saurabh Singh <singhsrb@fb.com>
parents: 34029
diff changeset
  2553
        repo.setparents(newid, nullid)
ae92e5c0441c amend: removing redundant if condition
Saurabh Singh <singhsrb@fb.com>
parents: 34029
diff changeset
  2554
        mapping = {old.node(): (newid,)}
34794
5781e0931c16 cmdutil: pass metadata from amend() to cleanupnodes
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34698
diff changeset
  2555
        obsmetadata = None
5781e0931c16 cmdutil: pass metadata from amend() to cleanupnodes
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34698
diff changeset
  2556
        if opts.get('note'):
5781e0931c16 cmdutil: pass metadata from amend() to cleanupnodes
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34698
diff changeset
  2557
            obsmetadata = {'note': opts['note']}
5781e0931c16 cmdutil: pass metadata from amend() to cleanupnodes
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34698
diff changeset
  2558
        scmutil.cleanupnodes(repo, mapping, 'amend', metadata=obsmetadata)
34055
ae92e5c0441c amend: removing redundant if condition
Saurabh Singh <singhsrb@fb.com>
parents: 34029
diff changeset
  2559
34085
e8a7c1a0565a cmdutil: remove the redundant commit during amend
Saurabh Singh <singhsrb@fb.com>
parents: 34083
diff changeset
  2560
        # Fixing the dirstate because localrepo.commitctx does not update
e8a7c1a0565a cmdutil: remove the redundant commit during amend
Saurabh Singh <singhsrb@fb.com>
parents: 34083
diff changeset
  2561
        # it. This is rather convenient because we did not need to update
e8a7c1a0565a cmdutil: remove the redundant commit during amend
Saurabh Singh <singhsrb@fb.com>
parents: 34083
diff changeset
  2562
        # the dirstate for all the files in the new commit which commitctx
e8a7c1a0565a cmdutil: remove the redundant commit during amend
Saurabh Singh <singhsrb@fb.com>
parents: 34083
diff changeset
  2563
        # could have done if it updated the dirstate. Now, we can
e8a7c1a0565a cmdutil: remove the redundant commit during amend
Saurabh Singh <singhsrb@fb.com>
parents: 34083
diff changeset
  2564
        # selectively update the dirstate only for the amended files.
e8a7c1a0565a cmdutil: remove the redundant commit during amend
Saurabh Singh <singhsrb@fb.com>
parents: 34083
diff changeset
  2565
        dirstate = repo.dirstate
e8a7c1a0565a cmdutil: remove the redundant commit during amend
Saurabh Singh <singhsrb@fb.com>
parents: 34083
diff changeset
  2566
e8a7c1a0565a cmdutil: remove the redundant commit during amend
Saurabh Singh <singhsrb@fb.com>
parents: 34083
diff changeset
  2567
        # Update the state of the files which were added and
e8a7c1a0565a cmdutil: remove the redundant commit during amend
Saurabh Singh <singhsrb@fb.com>
parents: 34083
diff changeset
  2568
        # and modified in the amend to "normal" in the dirstate.
e8a7c1a0565a cmdutil: remove the redundant commit during amend
Saurabh Singh <singhsrb@fb.com>
parents: 34083
diff changeset
  2569
        normalfiles = set(wctx.modified() + wctx.added()) & filestoamend
e8a7c1a0565a cmdutil: remove the redundant commit during amend
Saurabh Singh <singhsrb@fb.com>
parents: 34083
diff changeset
  2570
        for f in normalfiles:
e8a7c1a0565a cmdutil: remove the redundant commit during amend
Saurabh Singh <singhsrb@fb.com>
parents: 34083
diff changeset
  2571
            dirstate.normal(f)
e8a7c1a0565a cmdutil: remove the redundant commit during amend
Saurabh Singh <singhsrb@fb.com>
parents: 34083
diff changeset
  2572
e8a7c1a0565a cmdutil: remove the redundant commit during amend
Saurabh Singh <singhsrb@fb.com>
parents: 34083
diff changeset
  2573
        # Update the state of files which were removed in the amend
e8a7c1a0565a cmdutil: remove the redundant commit during amend
Saurabh Singh <singhsrb@fb.com>
parents: 34083
diff changeset
  2574
        # to "removed" in the dirstate.
e8a7c1a0565a cmdutil: remove the redundant commit during amend
Saurabh Singh <singhsrb@fb.com>
parents: 34083
diff changeset
  2575
        removedfiles = set(wctx.removed()) & filestoamend
e8a7c1a0565a cmdutil: remove the redundant commit during amend
Saurabh Singh <singhsrb@fb.com>
parents: 34083
diff changeset
  2576
        for f in removedfiles:
e8a7c1a0565a cmdutil: remove the redundant commit during amend
Saurabh Singh <singhsrb@fb.com>
parents: 34083
diff changeset
  2577
            dirstate.drop(f)
e8a7c1a0565a cmdutil: remove the redundant commit during amend
Saurabh Singh <singhsrb@fb.com>
parents: 34083
diff changeset
  2578
16458
55982f62651f commit: add option to amend the working dir parent
Idan Kamara <idankk86@gmail.com>
parents: 16430
diff changeset
  2579
    return newid
55982f62651f commit: add option to amend the working dir parent
Idan Kamara <idankk86@gmail.com>
parents: 16430
diff changeset
  2580
21999
6ce282ed801e cmdutil: introduce 'editform' to distinguish the purpose of commit text editing
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21998
diff changeset
  2581
def commiteditor(repo, ctx, subs, editform=''):
8407
223000a687b0 commit: move commit editor to cmdutil, pass as function
Matt Mackall <mpm@selenic.com>
parents: 8390
diff changeset
  2582
    if ctx.description():
223000a687b0 commit: move commit editor to cmdutil, pass as function
Matt Mackall <mpm@selenic.com>
parents: 8390
diff changeset
  2583
        return ctx.description()
26742
bec1a579ebc4 commit: abort when a committemplate is not changed
Tony Tung <tonytung@fb.com>
parents: 26639
diff changeset
  2584
    return commitforceeditor(repo, ctx, subs, editform=editform,
bec1a579ebc4 commit: abort when a committemplate is not changed
Tony Tung <tonytung@fb.com>
parents: 26639
diff changeset
  2585
                             unchangedmessagedetection=True)
8407
223000a687b0 commit: move commit editor to cmdutil, pass as function
Matt Mackall <mpm@selenic.com>
parents: 8390
diff changeset
  2586
21999
6ce282ed801e cmdutil: introduce 'editform' to distinguish the purpose of commit text editing
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21998
diff changeset
  2587
def commitforceeditor(repo, ctx, subs, finishdesc=None, extramsg=None,
26742
bec1a579ebc4 commit: abort when a committemplate is not changed
Tony Tung <tonytung@fb.com>
parents: 26639
diff changeset
  2588
                      editform='', unchangedmessagedetection=False):
21923
e582e20cd3e6 commiteditor: refactor default extramsg
Matt Mackall <mpm@selenic.com>
parents: 21878
diff changeset
  2589
    if not extramsg:
e582e20cd3e6 commiteditor: refactor default extramsg
Matt Mackall <mpm@selenic.com>
parents: 21878
diff changeset
  2590
        extramsg = _("Leave message empty to abort commit.")
22012
9d92b9d1e282 cmdutil: look commit template definition up by specified 'editform'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 22011
diff changeset
  2591
9d92b9d1e282 cmdutil: look commit template definition up by specified 'editform'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 22011
diff changeset
  2592
    forms = [e for e in editform.split('.') if e]
9d92b9d1e282 cmdutil: look commit template definition up by specified 'editform'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 22011
diff changeset
  2593
    forms.insert(0, 'changeset')
26742
bec1a579ebc4 commit: abort when a committemplate is not changed
Tony Tung <tonytung@fb.com>
parents: 26639
diff changeset
  2594
    templatetext = None
22012
9d92b9d1e282 cmdutil: look commit template definition up by specified 'editform'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 22011
diff changeset
  2595
    while forms:
32878
a3a36bcf122e commit: select template by spec.ref name
Yuya Nishihara <yuya@tcha.org>
parents: 32877
diff changeset
  2596
        ref = '.'.join(forms)
a3a36bcf122e commit: select template by spec.ref name
Yuya Nishihara <yuya@tcha.org>
parents: 32877
diff changeset
  2597
        if repo.ui.config('committemplate', ref):
26742
bec1a579ebc4 commit: abort when a committemplate is not changed
Tony Tung <tonytung@fb.com>
parents: 26639
diff changeset
  2598
            templatetext = committext = buildcommittemplate(
32878
a3a36bcf122e commit: select template by spec.ref name
Yuya Nishihara <yuya@tcha.org>
parents: 32877
diff changeset
  2599
                repo, ctx, subs, extramsg, ref)
22012
9d92b9d1e282 cmdutil: look commit template definition up by specified 'editform'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 22011
diff changeset
  2600
            break
9d92b9d1e282 cmdutil: look commit template definition up by specified 'editform'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 22011
diff changeset
  2601
        forms.pop()
21924
5375ba75df40 cmdutil: make commit message shown in text editor customizable by template
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21923
diff changeset
  2602
    else:
5375ba75df40 cmdutil: make commit message shown in text editor customizable by template
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21923
diff changeset
  2603
        committext = buildcommittext(repo, ctx, subs, extramsg)
21869
e353fac7db26 cmdutil: separate building commit text from 'commitforceeditor'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21832
diff changeset
  2604
e353fac7db26 cmdutil: separate building commit text from 'commitforceeditor'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21832
diff changeset
  2605
    # run editor in the repository root
30519
20a42325fdef py3: use pycompat.getcwd() instead of os.getcwd()
Pulkit Goyal <7895pulkit@gmail.com>
parents: 30506
diff changeset
  2606
    olddir = pycompat.getcwd()
21869
e353fac7db26 cmdutil: separate building commit text from 'commitforceeditor'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21832
diff changeset
  2607
    os.chdir(repo.root)
26750
9f9ec4abe700 cmdutil: make in-memory changes visible to external editor (issue4378)
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 26746
diff changeset
  2608
9f9ec4abe700 cmdutil: make in-memory changes visible to external editor (issue4378)
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 26746
diff changeset
  2609
    # make in-memory changes visible to external process
9f9ec4abe700 cmdutil: make in-memory changes visible to external editor (issue4378)
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 26746
diff changeset
  2610
    tr = repo.currenttransaction()
9f9ec4abe700 cmdutil: make in-memory changes visible to external editor (issue4378)
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 26746
diff changeset
  2611
    repo.dirstate.write(tr)
9f9ec4abe700 cmdutil: make in-memory changes visible to external editor (issue4378)
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 26746
diff changeset
  2612
    pending = tr and tr.writepending() and repo.root
9f9ec4abe700 cmdutil: make in-memory changes visible to external editor (issue4378)
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 26746
diff changeset
  2613
26742
bec1a579ebc4 commit: abort when a committemplate is not changed
Tony Tung <tonytung@fb.com>
parents: 26639
diff changeset
  2614
    editortext = repo.ui.edit(committext, ctx.user(), ctx.extra(),
30836
565c07036066 cmdutil: add tmpdir parament to ui.edit calls
Sean Farley <sean@farley.io>
parents: 30833
diff changeset
  2615
                              editform=editform, pending=pending,
34029
6e6452bc441d editor: use an unambiguous path suffix for editor files
Michael Bolin <mbolin@fb.com>
parents: 34022
diff changeset
  2616
                              repopath=repo.path, action='commit')
30724
ee47e951c6f9 commit: fix unmodified message detection for the "--- >8 ----" magic
Yuya Nishihara <yuya@tcha.org>
parents: 30720
diff changeset
  2617
    text = editortext
30703
5c85c93cdd61 cmdutil: add special string that ignores rest of text
Sean Farley <sean@farley.io>
parents: 30695
diff changeset
  2618
5c85c93cdd61 cmdutil: add special string that ignores rest of text
Sean Farley <sean@farley.io>
parents: 30695
diff changeset
  2619
    # strip away anything below this special string (used for editors that want
5c85c93cdd61 cmdutil: add special string that ignores rest of text
Sean Farley <sean@farley.io>
parents: 30695
diff changeset
  2620
    # to display the diff)
30724
ee47e951c6f9 commit: fix unmodified message detection for the "--- >8 ----" magic
Yuya Nishihara <yuya@tcha.org>
parents: 30720
diff changeset
  2621
    stripbelow = re.search(_linebelow, text, flags=re.MULTILINE)
30703
5c85c93cdd61 cmdutil: add special string that ignores rest of text
Sean Farley <sean@farley.io>
parents: 30695
diff changeset
  2622
    if stripbelow:
30724
ee47e951c6f9 commit: fix unmodified message detection for the "--- >8 ----" magic
Yuya Nishihara <yuya@tcha.org>
parents: 30720
diff changeset
  2623
        text = text[:stripbelow.start()]
ee47e951c6f9 commit: fix unmodified message detection for the "--- >8 ----" magic
Yuya Nishihara <yuya@tcha.org>
parents: 30720
diff changeset
  2624
ee47e951c6f9 commit: fix unmodified message detection for the "--- >8 ----" magic
Yuya Nishihara <yuya@tcha.org>
parents: 30720
diff changeset
  2625
    text = re.sub("(?m)^HG:.*(\n|$)", "", text)
21869
e353fac7db26 cmdutil: separate building commit text from 'commitforceeditor'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21832
diff changeset
  2626
    os.chdir(olddir)
e353fac7db26 cmdutil: separate building commit text from 'commitforceeditor'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21832
diff changeset
  2627
e353fac7db26 cmdutil: separate building commit text from 'commitforceeditor'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21832
diff changeset
  2628
    if finishdesc:
e353fac7db26 cmdutil: separate building commit text from 'commitforceeditor'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21832
diff changeset
  2629
        text = finishdesc(text)
e353fac7db26 cmdutil: separate building commit text from 'commitforceeditor'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21832
diff changeset
  2630
    if not text.strip():
26587
56b2bcea2529 error: get Abort from 'error' instead of 'util'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26579
diff changeset
  2631
        raise error.Abort(_("empty commit message"))
26742
bec1a579ebc4 commit: abort when a committemplate is not changed
Tony Tung <tonytung@fb.com>
parents: 26639
diff changeset
  2632
    if unchangedmessagedetection and editortext == templatetext:
bec1a579ebc4 commit: abort when a committemplate is not changed
Tony Tung <tonytung@fb.com>
parents: 26639
diff changeset
  2633
        raise error.Abort(_("commit message unchanged"))
21869
e353fac7db26 cmdutil: separate building commit text from 'commitforceeditor'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21832
diff changeset
  2634
e353fac7db26 cmdutil: separate building commit text from 'commitforceeditor'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21832
diff changeset
  2635
    return text
e353fac7db26 cmdutil: separate building commit text from 'commitforceeditor'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21832
diff changeset
  2636
32878
a3a36bcf122e commit: select template by spec.ref name
Yuya Nishihara <yuya@tcha.org>
parents: 32877
diff changeset
  2637
def buildcommittemplate(repo, ctx, subs, extramsg, ref):
21924
5375ba75df40 cmdutil: make commit message shown in text editor customizable by template
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21923
diff changeset
  2638
    ui = repo.ui
32878
a3a36bcf122e commit: select template by spec.ref name
Yuya Nishihara <yuya@tcha.org>
parents: 32877
diff changeset
  2639
    spec = formatter.templatespec(ref, None, None)
35954
386c1e45e671 logcmdutil: drop default arguments from changesetdisplayer/templater() calls
Yuya Nishihara <yuya@tcha.org>
parents: 35926
diff changeset
  2640
    t = logcmdutil.changesettemplater(ui, repo, spec)
32878
a3a36bcf122e commit: select template by spec.ref name
Yuya Nishihara <yuya@tcha.org>
parents: 32877
diff changeset
  2641
    t.t.cache.update((k, templater.unquotestring(v))
a3a36bcf122e commit: select template by spec.ref name
Yuya Nishihara <yuya@tcha.org>
parents: 32877
diff changeset
  2642
                     for k, v in repo.ui.configitems('committemplate'))
22013
de5cee8ba088 cmdutil: use '[committemplate]' section like as map file for style definition
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 22012
diff changeset
  2643
21924
5375ba75df40 cmdutil: make commit message shown in text editor customizable by template
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21923
diff changeset
  2644
    if not extramsg:
5375ba75df40 cmdutil: make commit message shown in text editor customizable by template
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21923
diff changeset
  2645
        extramsg = '' # ensure that extramsg is string
5375ba75df40 cmdutil: make commit message shown in text editor customizable by template
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21923
diff changeset
  2646
5375ba75df40 cmdutil: make commit message shown in text editor customizable by template
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21923
diff changeset
  2647
    ui.pushbuffer()
5375ba75df40 cmdutil: make commit message shown in text editor customizable by template
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21923
diff changeset
  2648
    t.show(ctx, extramsg=extramsg)
5375ba75df40 cmdutil: make commit message shown in text editor customizable by template
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21923
diff changeset
  2649
    return ui.popbuffer()
5375ba75df40 cmdutil: make commit message shown in text editor customizable by template
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21923
diff changeset
  2650
26426
0486c16ce621 cmdutil: handle multiline translations of HG: messages safely
timeless@mozdev.org
parents: 26389
diff changeset
  2651
def hgprefix(msg):
0486c16ce621 cmdutil: handle multiline translations of HG: messages safely
timeless@mozdev.org
parents: 26389
diff changeset
  2652
    return "\n".join(["HG: %s" % a for a in msg.split("\n") if a])
0486c16ce621 cmdutil: handle multiline translations of HG: messages safely
timeless@mozdev.org
parents: 26389
diff changeset
  2653
21869
e353fac7db26 cmdutil: separate building commit text from 'commitforceeditor'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21832
diff changeset
  2654
def buildcommittext(repo, ctx, subs, extramsg):
8407
223000a687b0 commit: move commit editor to cmdutil, pass as function
Matt Mackall <mpm@selenic.com>
parents: 8390
diff changeset
  2655
    edittext = []
8707
0550dfe4fca1 commit: editor reads file lists from provided context
Matt Mackall <mpm@selenic.com>
parents: 8680
diff changeset
  2656
    modified, added, removed = ctx.modified(), ctx.added(), ctx.removed()
8407
223000a687b0 commit: move commit editor to cmdutil, pass as function
Matt Mackall <mpm@selenic.com>
parents: 8390
diff changeset
  2657
    if ctx.description():
223000a687b0 commit: move commit editor to cmdutil, pass as function
Matt Mackall <mpm@selenic.com>
parents: 8390
diff changeset
  2658
        edittext.append(ctx.description())
223000a687b0 commit: move commit editor to cmdutil, pass as function
Matt Mackall <mpm@selenic.com>
parents: 8390
diff changeset
  2659
    edittext.append("")
223000a687b0 commit: move commit editor to cmdutil, pass as function
Matt Mackall <mpm@selenic.com>
parents: 8390
diff changeset
  2660
    edittext.append("") # Empty line between message and comments.
26426
0486c16ce621 cmdutil: handle multiline translations of HG: messages safely
timeless@mozdev.org
parents: 26389
diff changeset
  2661
    edittext.append(hgprefix(_("Enter commit message."
0486c16ce621 cmdutil: handle multiline translations of HG: messages safely
timeless@mozdev.org
parents: 26389
diff changeset
  2662
                      "  Lines beginning with 'HG:' are removed.")))
0486c16ce621 cmdutil: handle multiline translations of HG: messages safely
timeless@mozdev.org
parents: 26389
diff changeset
  2663
    edittext.append(hgprefix(extramsg))
8407
223000a687b0 commit: move commit editor to cmdutil, pass as function
Matt Mackall <mpm@selenic.com>
parents: 8390
diff changeset
  2664
    edittext.append("HG: --")
26426
0486c16ce621 cmdutil: handle multiline translations of HG: messages safely
timeless@mozdev.org
parents: 26389
diff changeset
  2665
    edittext.append(hgprefix(_("user: %s") % ctx.user()))
8407
223000a687b0 commit: move commit editor to cmdutil, pass as function
Matt Mackall <mpm@selenic.com>
parents: 8390
diff changeset
  2666
    if ctx.p2():
26426
0486c16ce621 cmdutil: handle multiline translations of HG: messages safely
timeless@mozdev.org
parents: 26389
diff changeset
  2667
        edittext.append(hgprefix(_("branch merge")))
8407
223000a687b0 commit: move commit editor to cmdutil, pass as function
Matt Mackall <mpm@selenic.com>
parents: 8390
diff changeset
  2668
    if ctx.branch():
26426
0486c16ce621 cmdutil: handle multiline translations of HG: messages safely
timeless@mozdev.org
parents: 26389
diff changeset
  2669
        edittext.append(hgprefix(_("branch '%s'") % ctx.branch()))
24986
fb9b7b937b3e bookmarks: simplify iscurrent to isactivewdirparent (API)
Ryan McElroy <rmcelroy@fb.com>
parents: 24955
diff changeset
  2670
    if bookmarks.isactivewdirparent(repo):
26426
0486c16ce621 cmdutil: handle multiline translations of HG: messages safely
timeless@mozdev.org
parents: 26389
diff changeset
  2671
        edittext.append(hgprefix(_("bookmark '%s'") % repo._activebookmark))
0486c16ce621 cmdutil: handle multiline translations of HG: messages safely
timeless@mozdev.org
parents: 26389
diff changeset
  2672
    edittext.extend([hgprefix(_("subrepo %s") % s) for s in subs])
0486c16ce621 cmdutil: handle multiline translations of HG: messages safely
timeless@mozdev.org
parents: 26389
diff changeset
  2673
    edittext.extend([hgprefix(_("added %s") % f) for f in added])
0486c16ce621 cmdutil: handle multiline translations of HG: messages safely
timeless@mozdev.org
parents: 26389
diff changeset
  2674
    edittext.extend([hgprefix(_("changed %s") % f) for f in modified])
0486c16ce621 cmdutil: handle multiline translations of HG: messages safely
timeless@mozdev.org
parents: 26389
diff changeset
  2675
    edittext.extend([hgprefix(_("removed %s") % f) for f in removed])
8707
0550dfe4fca1 commit: editor reads file lists from provided context
Matt Mackall <mpm@selenic.com>
parents: 8680
diff changeset
  2676
    if not added and not modified and not removed:
26426
0486c16ce621 cmdutil: handle multiline translations of HG: messages safely
timeless@mozdev.org
parents: 26389
diff changeset
  2677
        edittext.append(hgprefix(_("no files changed")))
8407
223000a687b0 commit: move commit editor to cmdutil, pass as function
Matt Mackall <mpm@selenic.com>
parents: 8390
diff changeset
  2678
    edittext.append("")
223000a687b0 commit: move commit editor to cmdutil, pass as function
Matt Mackall <mpm@selenic.com>
parents: 8390
diff changeset
  2679
21869
e353fac7db26 cmdutil: separate building commit text from 'commitforceeditor'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21832
diff changeset
  2680
    return "\n".join(edittext)
14297
2daa5179e73f commands: use a decorator to build table incrementally
Adrian Buehlmann <adrian@cadifra.com>
parents: 14291
diff changeset
  2681
26324
4a8e21578e14 addremove: remove a mutable default argument
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26229
diff changeset
  2682
def commitstatus(repo, node, branch, bheads=None, opts=None):
4a8e21578e14 addremove: remove a mutable default argument
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26229
diff changeset
  2683
    if opts is None:
4a8e21578e14 addremove: remove a mutable default argument
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26229
diff changeset
  2684
        opts = {}
18688
79107fad06aa commit: factor out status printing into a helper function
Kevin Bullock <kbullock@ringworld.org>
parents: 18648
diff changeset
  2685
    ctx = repo[node]
79107fad06aa commit: factor out status printing into a helper function
Kevin Bullock <kbullock@ringworld.org>
parents: 18648
diff changeset
  2686
    parents = ctx.parents()
79107fad06aa commit: factor out status printing into a helper function
Kevin Bullock <kbullock@ringworld.org>
parents: 18648
diff changeset
  2687
79107fad06aa commit: factor out status printing into a helper function
Kevin Bullock <kbullock@ringworld.org>
parents: 18648
diff changeset
  2688
    if (not opts.get('amend') and bheads and node not in bheads and not
79107fad06aa commit: factor out status printing into a helper function
Kevin Bullock <kbullock@ringworld.org>
parents: 18648
diff changeset
  2689
        [x for x in parents if x.node() in bheads and x.branch() == branch]):
79107fad06aa commit: factor out status printing into a helper function
Kevin Bullock <kbullock@ringworld.org>
parents: 18648
diff changeset
  2690
        repo.ui.status(_('created new head\n'))
79107fad06aa commit: factor out status printing into a helper function
Kevin Bullock <kbullock@ringworld.org>
parents: 18648
diff changeset
  2691
        # The message is not printed for initial roots. For the other
79107fad06aa commit: factor out status printing into a helper function
Kevin Bullock <kbullock@ringworld.org>
parents: 18648
diff changeset
  2692
        # changesets, it is printed in the following situations:
79107fad06aa commit: factor out status printing into a helper function
Kevin Bullock <kbullock@ringworld.org>
parents: 18648
diff changeset
  2693
        #
79107fad06aa commit: factor out status printing into a helper function
Kevin Bullock <kbullock@ringworld.org>
parents: 18648
diff changeset
  2694
        # Par column: for the 2 parents with ...
79107fad06aa commit: factor out status printing into a helper function
Kevin Bullock <kbullock@ringworld.org>
parents: 18648
diff changeset
  2695
        #   N: null or no parent
79107fad06aa commit: factor out status printing into a helper function
Kevin Bullock <kbullock@ringworld.org>
parents: 18648
diff changeset
  2696
        #   B: parent is on another named branch
79107fad06aa commit: factor out status printing into a helper function
Kevin Bullock <kbullock@ringworld.org>
parents: 18648
diff changeset
  2697
        #   C: parent is a regular non head changeset
79107fad06aa commit: factor out status printing into a helper function
Kevin Bullock <kbullock@ringworld.org>
parents: 18648
diff changeset
  2698
        #   H: parent was a branch head of the current branch
79107fad06aa commit: factor out status printing into a helper function
Kevin Bullock <kbullock@ringworld.org>
parents: 18648
diff changeset
  2699
        # Msg column: whether we print "created new head" message
79107fad06aa commit: factor out status printing into a helper function
Kevin Bullock <kbullock@ringworld.org>
parents: 18648
diff changeset
  2700
        # In the following, it is assumed that there already exists some
79107fad06aa commit: factor out status printing into a helper function
Kevin Bullock <kbullock@ringworld.org>
parents: 18648
diff changeset
  2701
        # initial branch heads of the current branch, otherwise nothing is
79107fad06aa commit: factor out status printing into a helper function
Kevin Bullock <kbullock@ringworld.org>
parents: 18648
diff changeset
  2702
        # printed anyway.
79107fad06aa commit: factor out status printing into a helper function
Kevin Bullock <kbullock@ringworld.org>
parents: 18648
diff changeset
  2703
        #
79107fad06aa commit: factor out status printing into a helper function
Kevin Bullock <kbullock@ringworld.org>
parents: 18648
diff changeset
  2704
        # Par Msg Comment
79107fad06aa commit: factor out status printing into a helper function
Kevin Bullock <kbullock@ringworld.org>
parents: 18648
diff changeset
  2705
        # N N  y  additional topo root
79107fad06aa commit: factor out status printing into a helper function
Kevin Bullock <kbullock@ringworld.org>
parents: 18648
diff changeset
  2706
        #
79107fad06aa commit: factor out status printing into a helper function
Kevin Bullock <kbullock@ringworld.org>
parents: 18648
diff changeset
  2707
        # B N  y  additional branch root
79107fad06aa commit: factor out status printing into a helper function
Kevin Bullock <kbullock@ringworld.org>
parents: 18648
diff changeset
  2708
        # C N  y  additional topo head
79107fad06aa commit: factor out status printing into a helper function
Kevin Bullock <kbullock@ringworld.org>
parents: 18648
diff changeset
  2709
        # H N  n  usual case
79107fad06aa commit: factor out status printing into a helper function
Kevin Bullock <kbullock@ringworld.org>
parents: 18648
diff changeset
  2710
        #
79107fad06aa commit: factor out status printing into a helper function
Kevin Bullock <kbullock@ringworld.org>
parents: 18648
diff changeset
  2711
        # B B  y  weird additional branch root
79107fad06aa commit: factor out status printing into a helper function
Kevin Bullock <kbullock@ringworld.org>
parents: 18648
diff changeset
  2712
        # C B  y  branch merge
79107fad06aa commit: factor out status printing into a helper function
Kevin Bullock <kbullock@ringworld.org>
parents: 18648
diff changeset
  2713
        # H B  n  merge with named branch
79107fad06aa commit: factor out status printing into a helper function
Kevin Bullock <kbullock@ringworld.org>
parents: 18648
diff changeset
  2714
        #
79107fad06aa commit: factor out status printing into a helper function
Kevin Bullock <kbullock@ringworld.org>
parents: 18648
diff changeset
  2715
        # C C  y  additional head from merge
79107fad06aa commit: factor out status printing into a helper function
Kevin Bullock <kbullock@ringworld.org>
parents: 18648
diff changeset
  2716
        # C H  n  merge with a head
79107fad06aa commit: factor out status printing into a helper function
Kevin Bullock <kbullock@ringworld.org>
parents: 18648
diff changeset
  2717
        #
79107fad06aa commit: factor out status printing into a helper function
Kevin Bullock <kbullock@ringworld.org>
parents: 18648
diff changeset
  2718
        # H H  n  head merge: head count decreases
79107fad06aa commit: factor out status printing into a helper function
Kevin Bullock <kbullock@ringworld.org>
parents: 18648
diff changeset
  2719
79107fad06aa commit: factor out status printing into a helper function
Kevin Bullock <kbullock@ringworld.org>
parents: 18648
diff changeset
  2720
    if not opts.get('close_branch'):
79107fad06aa commit: factor out status printing into a helper function
Kevin Bullock <kbullock@ringworld.org>
parents: 18648
diff changeset
  2721
        for r in parents:
79107fad06aa commit: factor out status printing into a helper function
Kevin Bullock <kbullock@ringworld.org>
parents: 18648
diff changeset
  2722
            if r.closesbranch() and r.branch() == branch:
36406
fd2191d870ff cmdutil: use ctx.rev() instead of %d % ctx
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36402
diff changeset
  2723
                repo.ui.status(_('reopening closed branch head %d\n') % r.rev())
18688
79107fad06aa commit: factor out status printing into a helper function
Kevin Bullock <kbullock@ringworld.org>
parents: 18648
diff changeset
  2724
79107fad06aa commit: factor out status printing into a helper function
Kevin Bullock <kbullock@ringworld.org>
parents: 18648
diff changeset
  2725
    if repo.ui.debugflag:
36402
ef6ae3f64c23 cmdutil: use ctx.rev() instead of int(ctx)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36346
diff changeset
  2726
        repo.ui.write(_('committed changeset %d:%s\n') % (ctx.rev(), ctx.hex()))
18688
79107fad06aa commit: factor out status printing into a helper function
Kevin Bullock <kbullock@ringworld.org>
parents: 18648
diff changeset
  2727
    elif repo.ui.verbose:
36402
ef6ae3f64c23 cmdutil: use ctx.rev() instead of int(ctx)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36346
diff changeset
  2728
        repo.ui.write(_('committed changeset %d:%s\n') % (ctx.rev(), ctx))
18688
79107fad06aa commit: factor out status printing into a helper function
Kevin Bullock <kbullock@ringworld.org>
parents: 18648
diff changeset
  2729
27943
02c5f8ad00ac commit: factor the post commit status check into a cmdutil method
Matt Harbison <matt_harbison@yahoo.com>
parents: 27868
diff changeset
  2730
def postcommitstatus(repo, pats, opts):
02c5f8ad00ac commit: factor the post commit status check into a cmdutil method
Matt Harbison <matt_harbison@yahoo.com>
parents: 27868
diff changeset
  2731
    return repo.status(match=scmutil.match(repo[None], pats, opts))
02c5f8ad00ac commit: factor the post commit status check into a cmdutil method
Matt Harbison <matt_harbison@yahoo.com>
parents: 27868
diff changeset
  2732
16304
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2733
def revert(ui, repo, ctx, parents, *pats, **opts):
35147
3da4bd50103d py3: fix handling of keyword arguments in revert
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35123
diff changeset
  2734
    opts = pycompat.byteskwargs(opts)
16304
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2735
    parent, p2 = parents
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2736
    node = ctx.node()
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2737
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2738
    mf = ctx.manifest()
21579
87a972b5c039 revert: use p2 as parent when reverting against it
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 21578
diff changeset
  2739
    if node == p2:
87a972b5c039 revert: use p2 as parent when reverting against it
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 21578
diff changeset
  2740
        parent = p2
16304
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2741
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2742
    # need all matching names in dirstate and manifest of target rev,
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2743
    # so have to walk both. do not print errors if files exist in one
24451
c3bbafef25d6 revert: comment that filesets are always evaluated against workingctx
Yuya Nishihara <yuya@tcha.org>
parents: 24450
diff changeset
  2744
    # but not other. in both cases, filesets should be evaluated against
c3bbafef25d6 revert: comment that filesets are always evaluated against workingctx
Yuya Nishihara <yuya@tcha.org>
parents: 24450
diff changeset
  2745
    # workingctx to get consistent result (issue4497). this means 'set:**'
c3bbafef25d6 revert: comment that filesets are always evaluated against workingctx
Yuya Nishihara <yuya@tcha.org>
parents: 24450
diff changeset
  2746
    # cannot be used to select missing files from target rev.
16304
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2747
21575
8262c2a39ab8 revert: add some inline comments
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 21574
diff changeset
  2748
    # `names` is a mapping for all elements in working copy and target revision
8262c2a39ab8 revert: add some inline comments
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 21574
diff changeset
  2749
    # The mapping is in the form:
8262c2a39ab8 revert: add some inline comments
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 21574
diff changeset
  2750
    #   <asb path in repo> -> (<path from CWD>, <exactly specified by matcher?>)
16304
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2751
    names = {}
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2752
27803
a8e8950ebd4d with: use context manager for wlock in revert
Bryan O'Sullivan <bryano@fb.com>
parents: 27802
diff changeset
  2753
    with repo.wlock():
21575
8262c2a39ab8 revert: add some inline comments
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 21574
diff changeset
  2754
        ## filling of the `names` mapping
8262c2a39ab8 revert: add some inline comments
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 21574
diff changeset
  2755
        # walk dirstate to fill `names`
16304
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2756
24475
06cbff4674a3 revert: fix --interactive on local modification (issue4576)
Laurent Charignon <lcharignon@fb.com>
parents: 24472
diff changeset
  2757
        interactive = opts.get('interactive', False)
24449
bab983bb6fd1 revert: define 'wctx' a little earlier and use it more
Martin von Zweigbergk <martinvonz@google.com>
parents: 24438
diff changeset
  2758
        wctx = repo[None]
bab983bb6fd1 revert: define 'wctx' a little earlier and use it more
Martin von Zweigbergk <martinvonz@google.com>
parents: 24438
diff changeset
  2759
        m = scmutil.match(wctx, pats, opts)
24479
871485bd03fd revert: move calculation of targetsubs earlier
Matt Mackall <mpm@selenic.com>
parents: 24476
diff changeset
  2760
871485bd03fd revert: move calculation of targetsubs earlier
Matt Mackall <mpm@selenic.com>
parents: 24476
diff changeset
  2761
        # we'll need this later
871485bd03fd revert: move calculation of targetsubs earlier
Matt Mackall <mpm@selenic.com>
parents: 24476
diff changeset
  2762
        targetsubs = sorted(s for s in wctx.substate if m(s))
871485bd03fd revert: move calculation of targetsubs earlier
Matt Mackall <mpm@selenic.com>
parents: 24476
diff changeset
  2763
24450
961790c35b4f revert: take fast path also when not reverting to '.'
Martin von Zweigbergk <martinvonz@google.com>
parents: 24449
diff changeset
  2764
        if not m.always():
32362
7b3c27af90c2 cmdutil: use repo[None].walk instead of repo.walk
Augie Fackler <augie@google.com>
parents: 32343
diff changeset
  2765
            matcher = matchmod.badmatch(m, lambda x, y: False)
32382
c87db79b9507 cleanup: reuse existing wctx variables instead of calling repo[None]
Martin von Zweigbergk <martinvonz@google.com>
parents: 32375
diff changeset
  2766
            for abs in wctx.walk(matcher):
22573
f528bfb25b45 revert: special case 'hg revert --all'
Durham Goode <durham@fb.com>
parents: 22551
diff changeset
  2767
                names[abs] = m.rel(abs), m.exact(abs)
f528bfb25b45 revert: special case 'hg revert --all'
Durham Goode <durham@fb.com>
parents: 22551
diff changeset
  2768
f528bfb25b45 revert: special case 'hg revert --all'
Durham Goode <durham@fb.com>
parents: 22551
diff changeset
  2769
            # walk target manifest to fill `names`
f528bfb25b45 revert: special case 'hg revert --all'
Durham Goode <durham@fb.com>
parents: 22551
diff changeset
  2770
f528bfb25b45 revert: special case 'hg revert --all'
Durham Goode <durham@fb.com>
parents: 22551
diff changeset
  2771
            def badfn(path, msg):
f528bfb25b45 revert: special case 'hg revert --all'
Durham Goode <durham@fb.com>
parents: 22551
diff changeset
  2772
                if path in names:
f528bfb25b45 revert: special case 'hg revert --all'
Durham Goode <durham@fb.com>
parents: 22551
diff changeset
  2773
                    return
f528bfb25b45 revert: special case 'hg revert --all'
Durham Goode <durham@fb.com>
parents: 22551
diff changeset
  2774
                if path in ctx.substate:
16304
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2775
                    return
22573
f528bfb25b45 revert: special case 'hg revert --all'
Durham Goode <durham@fb.com>
parents: 22551
diff changeset
  2776
                path_ = path + '/'
f528bfb25b45 revert: special case 'hg revert --all'
Durham Goode <durham@fb.com>
parents: 22551
diff changeset
  2777
                for f in names:
f528bfb25b45 revert: special case 'hg revert --all'
Durham Goode <durham@fb.com>
parents: 22551
diff changeset
  2778
                    if f.startswith(path_):
f528bfb25b45 revert: special case 'hg revert --all'
Durham Goode <durham@fb.com>
parents: 22551
diff changeset
  2779
                        return
f528bfb25b45 revert: special case 'hg revert --all'
Durham Goode <durham@fb.com>
parents: 22551
diff changeset
  2780
                ui.warn("%s: %s\n" % (m.rel(path), msg))
f528bfb25b45 revert: special case 'hg revert --all'
Durham Goode <durham@fb.com>
parents: 22551
diff changeset
  2781
25439
aaede04c0ba6 revert: replace match.bad() monkey patching with match.badmatch()
Matt Harbison <matt_harbison@yahoo.com>
parents: 25438
diff changeset
  2782
            for abs in ctx.walk(matchmod.badmatch(m, badfn)):
22573
f528bfb25b45 revert: special case 'hg revert --all'
Durham Goode <durham@fb.com>
parents: 22551
diff changeset
  2783
                if abs not in names:
f528bfb25b45 revert: special case 'hg revert --all'
Durham Goode <durham@fb.com>
parents: 22551
diff changeset
  2784
                    names[abs] = m.rel(abs), m.exact(abs)
f528bfb25b45 revert: special case 'hg revert --all'
Durham Goode <durham@fb.com>
parents: 22551
diff changeset
  2785
f528bfb25b45 revert: special case 'hg revert --all'
Durham Goode <durham@fb.com>
parents: 22551
diff changeset
  2786
            # Find status of all file in `names`.
f528bfb25b45 revert: special case 'hg revert --all'
Durham Goode <durham@fb.com>
parents: 22551
diff changeset
  2787
            m = scmutil.matchfiles(repo, names)
f528bfb25b45 revert: special case 'hg revert --all'
Durham Goode <durham@fb.com>
parents: 22551
diff changeset
  2788
f528bfb25b45 revert: special case 'hg revert --all'
Durham Goode <durham@fb.com>
parents: 22551
diff changeset
  2789
            changes = repo.status(node1=node, match=m,
f528bfb25b45 revert: special case 'hg revert --all'
Durham Goode <durham@fb.com>
parents: 22551
diff changeset
  2790
                                  unknown=True, ignored=True, clean=True)
f528bfb25b45 revert: special case 'hg revert --all'
Durham Goode <durham@fb.com>
parents: 22551
diff changeset
  2791
        else:
24450
961790c35b4f revert: take fast path also when not reverting to '.'
Martin von Zweigbergk <martinvonz@google.com>
parents: 24449
diff changeset
  2792
            changes = repo.status(node1=node, match=m)
22573
f528bfb25b45 revert: special case 'hg revert --all'
Durham Goode <durham@fb.com>
parents: 22551
diff changeset
  2793
            for kind in changes:
f528bfb25b45 revert: special case 'hg revert --all'
Durham Goode <durham@fb.com>
parents: 22551
diff changeset
  2794
                for abs in kind:
f528bfb25b45 revert: special case 'hg revert --all'
Durham Goode <durham@fb.com>
parents: 22551
diff changeset
  2795
                    names[abs] = m.rel(abs), m.exact(abs)
f528bfb25b45 revert: special case 'hg revert --all'
Durham Goode <durham@fb.com>
parents: 22551
diff changeset
  2796
f528bfb25b45 revert: special case 'hg revert --all'
Durham Goode <durham@fb.com>
parents: 22551
diff changeset
  2797
            m = scmutil.matchfiles(repo, names)
f528bfb25b45 revert: special case 'hg revert --all'
Durham Goode <durham@fb.com>
parents: 22551
diff changeset
  2798
23374
aa0a430d9c75 revert: access status fields by name rather than index
Martin von Zweigbergk <martinvonz@google.com>
parents: 23327
diff changeset
  2799
        modified = set(changes.modified)
aa0a430d9c75 revert: access status fields by name rather than index
Martin von Zweigbergk <martinvonz@google.com>
parents: 23327
diff changeset
  2800
        added    = set(changes.added)
aa0a430d9c75 revert: access status fields by name rather than index
Martin von Zweigbergk <martinvonz@google.com>
parents: 23327
diff changeset
  2801
        removed  = set(changes.removed)
aa0a430d9c75 revert: access status fields by name rather than index
Martin von Zweigbergk <martinvonz@google.com>
parents: 23327
diff changeset
  2802
        _deleted = set(changes.deleted)
aa0a430d9c75 revert: access status fields by name rather than index
Martin von Zweigbergk <martinvonz@google.com>
parents: 23327
diff changeset
  2803
        unknown  = set(changes.unknown)
aa0a430d9c75 revert: access status fields by name rather than index
Martin von Zweigbergk <martinvonz@google.com>
parents: 23327
diff changeset
  2804
        unknown.update(changes.ignored)
aa0a430d9c75 revert: access status fields by name rather than index
Martin von Zweigbergk <martinvonz@google.com>
parents: 23327
diff changeset
  2805
        clean    = set(changes.clean)
22610
0f323ed8effd revert: track added files with local modifications
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22609
diff changeset
  2806
        modadded = set()
22185
afead12e724b revert: triage "deleted" files into more appropriate categories
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22173
diff changeset
  2807
24180
d8e0c591781c spelling: fixes from proofreading of spell checker issues
Mads Kiilerich <madski@unity3d.com>
parents: 24176
diff changeset
  2808
        # We need to account for the state of the file in the dirstate,
d8e0c591781c spelling: fixes from proofreading of spell checker issues
Mads Kiilerich <madski@unity3d.com>
parents: 24176
diff changeset
  2809
        # even when we revert against something else than parent. This will
22155
530390629842 revert: call status against revert target too
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22154
diff changeset
  2810
        # slightly alter the behavior of revert (doing back up or not, delete
23139
e53f6b72a0e4 spelling: fixes from proofreading of spell checker issues
Mads Kiilerich <madski@unity3d.com>
parents: 23101
diff changeset
  2811
        # or just forget etc).
22155
530390629842 revert: call status against revert target too
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22154
diff changeset
  2812
        if parent == node:
530390629842 revert: call status against revert target too
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22154
diff changeset
  2813
            dsmodified = modified
530390629842 revert: call status against revert target too
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22154
diff changeset
  2814
            dsadded = added
530390629842 revert: call status against revert target too
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22154
diff changeset
  2815
            dsremoved = removed
23403
edf29f9c15f0 revert: look for copy information for all local modifications
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23101
diff changeset
  2816
            # store all local modifications, useful later for rename detection
edf29f9c15f0 revert: look for copy information for all local modifications
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23101
diff changeset
  2817
            localchanges = dsmodified | dsadded
22155
530390629842 revert: call status against revert target too
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22154
diff changeset
  2818
            modified, added, removed = set(), set(), set()
530390629842 revert: call status against revert target too
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22154
diff changeset
  2819
        else:
530390629842 revert: call status against revert target too
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22154
diff changeset
  2820
            changes = repo.status(node1=parent, match=m)
23374
aa0a430d9c75 revert: access status fields by name rather than index
Martin von Zweigbergk <martinvonz@google.com>
parents: 23327
diff changeset
  2821
            dsmodified = set(changes.modified)
aa0a430d9c75 revert: access status fields by name rather than index
Martin von Zweigbergk <martinvonz@google.com>
parents: 23327
diff changeset
  2822
            dsadded    = set(changes.added)
aa0a430d9c75 revert: access status fields by name rather than index
Martin von Zweigbergk <martinvonz@google.com>
parents: 23327
diff changeset
  2823
            dsremoved  = set(changes.removed)
23403
edf29f9c15f0 revert: look for copy information for all local modifications
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23101
diff changeset
  2824
            # store all local modifications, useful later for rename detection
edf29f9c15f0 revert: look for copy information for all local modifications
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23101
diff changeset
  2825
            localchanges = dsmodified | dsadded
16304
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2826
22188
0ad619c5e1a4 revert: use "remove" information from both statuses
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22187
diff changeset
  2827
            # only take into account for removes between wc and target
0ad619c5e1a4 revert: use "remove" information from both statuses
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22187
diff changeset
  2828
            clean |= dsremoved - removed
0ad619c5e1a4 revert: use "remove" information from both statuses
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22187
diff changeset
  2829
            dsremoved &= removed
0ad619c5e1a4 revert: use "remove" information from both statuses
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22187
diff changeset
  2830
            # distinct between dirstate remove and other
0ad619c5e1a4 revert: use "remove" information from both statuses
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22187
diff changeset
  2831
            removed -= dsremoved
0ad619c5e1a4 revert: use "remove" information from both statuses
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22187
diff changeset
  2832
22610
0f323ed8effd revert: track added files with local modifications
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22609
diff changeset
  2833
            modadded = added & dsmodified
0f323ed8effd revert: track added files with local modifications
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22609
diff changeset
  2834
            added -= modadded
0f323ed8effd revert: track added files with local modifications
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22609
diff changeset
  2835
22190
55308ab8117c revert: use modified information from both statuses
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22189
diff changeset
  2836
            # tell newly modified apart.
55308ab8117c revert: use modified information from both statuses
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22189
diff changeset
  2837
            dsmodified &= modified
29103
f2aa1c3e7e77 cmdutil: typo fix in comment
Augie Fackler <augie@google.com>
parents: 28999
diff changeset
  2838
            dsmodified |= modified & dsadded # dirstate added may need backup
22190
55308ab8117c revert: use modified information from both statuses
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22189
diff changeset
  2839
            modified -= dsmodified
55308ab8117c revert: use modified information from both statuses
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22189
diff changeset
  2840
22488
6c52ed3f888e revert: split between newly added file and file added in other changeset
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22485
diff changeset
  2841
            # We need to wait for some post-processing to update this set
6c52ed3f888e revert: split between newly added file and file added in other changeset
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22485
diff changeset
  2842
            # before making the distinction. The dirstate will be used for
6c52ed3f888e revert: split between newly added file and file added in other changeset
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22485
diff changeset
  2843
            # that purpose.
22208
d3659b3795e9 revert: simplify handling of `added` files
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22205
diff changeset
  2844
            dsadded = added
d3659b3795e9 revert: simplify handling of `added` files
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22205
diff changeset
  2845
22209
06fbd9518bc5 revert: detect files added during a merge
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22208
diff changeset
  2846
        # in case of merge, files that are actually added can be reported as
06fbd9518bc5 revert: detect files added during a merge
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22208
diff changeset
  2847
        # modified, we need to post process the result
06fbd9518bc5 revert: detect files added during a merge
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22208
diff changeset
  2848
        if p2 != nullid:
31134
c22253c4c1b8 revert: remove set(mf) because it's O(manifest)
Durham Goode <durham@fb.com>
parents: 31078
diff changeset
  2849
            mergeadd = set(dsmodified)
c22253c4c1b8 revert: remove set(mf) because it's O(manifest)
Durham Goode <durham@fb.com>
parents: 31078
diff changeset
  2850
            for path in dsmodified:
c22253c4c1b8 revert: remove set(mf) because it's O(manifest)
Durham Goode <durham@fb.com>
parents: 31078
diff changeset
  2851
                if path in mf:
c22253c4c1b8 revert: remove set(mf) because it's O(manifest)
Durham Goode <durham@fb.com>
parents: 31078
diff changeset
  2852
                    mergeadd.remove(path)
22209
06fbd9518bc5 revert: detect files added during a merge
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22208
diff changeset
  2853
            dsadded |= mergeadd
06fbd9518bc5 revert: detect files added during a merge
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22208
diff changeset
  2854
            dsmodified -= mergeadd
16304
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2855
21575
8262c2a39ab8 revert: add some inline comments
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 21574
diff changeset
  2856
        # if f is a rename, update `names` to also revert the source
16304
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2857
        cwd = repo.getcwd()
23403
edf29f9c15f0 revert: look for copy information for all local modifications
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23101
diff changeset
  2858
        for f in localchanges:
16304
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2859
            src = repo.dirstate.copied(f)
22213
f1debbcd71cd revert: add an XXX about rename tracking
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22212
diff changeset
  2860
            # XXX should we check for rename down to target node?
16304
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2861
            if src and src not in names and repo.dirstate[src] == 'r':
22154
fc422de25773 revert: prefix variable names for dirstate status with "ds"
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22153
diff changeset
  2862
                dsremoved.add(src)
16304
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2863
                names[src] = (repo.pathto(src, cwd), True)
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2864
31157
accdd5e62066 revert: move code dealing with deletions closer together
Martin von Zweigbergk <martinvonz@google.com>
parents: 31134
diff changeset
  2865
        # determine the exact nature of the deleted changesets
accdd5e62066 revert: move code dealing with deletions closer together
Martin von Zweigbergk <martinvonz@google.com>
parents: 31134
diff changeset
  2866
        deladded = set(_deleted)
accdd5e62066 revert: move code dealing with deletions closer together
Martin von Zweigbergk <martinvonz@google.com>
parents: 31134
diff changeset
  2867
        for path in _deleted:
accdd5e62066 revert: move code dealing with deletions closer together
Martin von Zweigbergk <martinvonz@google.com>
parents: 31134
diff changeset
  2868
            if path in mf:
accdd5e62066 revert: move code dealing with deletions closer together
Martin von Zweigbergk <martinvonz@google.com>
parents: 31134
diff changeset
  2869
                deladded.remove(path)
accdd5e62066 revert: move code dealing with deletions closer together
Martin von Zweigbergk <martinvonz@google.com>
parents: 31134
diff changeset
  2870
        deleted = _deleted - deladded
accdd5e62066 revert: move code dealing with deletions closer together
Martin von Zweigbergk <martinvonz@google.com>
parents: 31134
diff changeset
  2871
22488
6c52ed3f888e revert: split between newly added file and file added in other changeset
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22485
diff changeset
  2872
        # distinguish between file to forget and the other
6c52ed3f888e revert: split between newly added file and file added in other changeset
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22485
diff changeset
  2873
        added = set()
6c52ed3f888e revert: split between newly added file and file added in other changeset
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22485
diff changeset
  2874
        for abs in dsadded:
6c52ed3f888e revert: split between newly added file and file added in other changeset
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22485
diff changeset
  2875
            if repo.dirstate[abs] != 'a':
6c52ed3f888e revert: split between newly added file and file added in other changeset
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22485
diff changeset
  2876
                added.add(abs)
6c52ed3f888e revert: split between newly added file and file added in other changeset
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22485
diff changeset
  2877
        dsadded -= added
6c52ed3f888e revert: split between newly added file and file added in other changeset
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22485
diff changeset
  2878
22490
bcab7bc7280e revert: explicitly track added but deleted file
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22489
diff changeset
  2879
        for abs in deladded:
16304
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2880
            if repo.dirstate[abs] == 'a':
22490
bcab7bc7280e revert: explicitly track added but deleted file
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22489
diff changeset
  2881
                dsadded.add(abs)
bcab7bc7280e revert: explicitly track added but deleted file
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22489
diff changeset
  2882
        deladded -= dsadded
bcab7bc7280e revert: explicitly track added but deleted file
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22489
diff changeset
  2883
22396
c0213f2cb942 revert: detect unknown files in the same path as files marked as removed
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22395
diff changeset
  2884
        # For files marked as removed, we check if an unknown file is present at
c0213f2cb942 revert: detect unknown files in the same path as files marked as removed
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22395
diff changeset
  2885
        # the same path. If a such file exists it may need to be backed up.
c0213f2cb942 revert: detect unknown files in the same path as files marked as removed
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22395
diff changeset
  2886
        # Making the distinction at this stage helps have simpler backup
c0213f2cb942 revert: detect unknown files in the same path as files marked as removed
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22395
diff changeset
  2887
        # logic.
c0213f2cb942 revert: detect unknown files in the same path as files marked as removed
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22395
diff changeset
  2888
        removunk = set()
c0213f2cb942 revert: detect unknown files in the same path as files marked as removed
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22395
diff changeset
  2889
        for abs in removed:
c0213f2cb942 revert: detect unknown files in the same path as files marked as removed
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22395
diff changeset
  2890
            target = repo.wjoin(abs)
c0213f2cb942 revert: detect unknown files in the same path as files marked as removed
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22395
diff changeset
  2891
            if os.path.lexists(target):
c0213f2cb942 revert: detect unknown files in the same path as files marked as removed
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22395
diff changeset
  2892
                removunk.add(abs)
c0213f2cb942 revert: detect unknown files in the same path as files marked as removed
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22395
diff changeset
  2893
        removed -= removunk
c0213f2cb942 revert: detect unknown files in the same path as files marked as removed
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22395
diff changeset
  2894
c0213f2cb942 revert: detect unknown files in the same path as files marked as removed
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22395
diff changeset
  2895
        dsremovunk = set()
c0213f2cb942 revert: detect unknown files in the same path as files marked as removed
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22395
diff changeset
  2896
        for abs in dsremoved:
c0213f2cb942 revert: detect unknown files in the same path as files marked as removed
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22395
diff changeset
  2897
            target = repo.wjoin(abs)
c0213f2cb942 revert: detect unknown files in the same path as files marked as removed
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22395
diff changeset
  2898
            if os.path.lexists(target):
c0213f2cb942 revert: detect unknown files in the same path as files marked as removed
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22395
diff changeset
  2899
                dsremovunk.add(abs)
c0213f2cb942 revert: detect unknown files in the same path as files marked as removed
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22395
diff changeset
  2900
        dsremoved -= dsremovunk
16304
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2901
21575
8262c2a39ab8 revert: add some inline comments
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 21574
diff changeset
  2902
        # action to be actually performed by revert
8262c2a39ab8 revert: add some inline comments
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 21574
diff changeset
  2903
        # (<list of file>, message>) tuple
21576
33395a7e5527 revert: group action into a single dictionary
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 21575
diff changeset
  2904
        actions = {'revert': ([], _('reverting %s\n')),
33395a7e5527 revert: group action into a single dictionary
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 21575
diff changeset
  2905
                   'add': ([], _('adding %s\n')),
22489
0d57bf80c7cb revert: have an explicit action for "forget"
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22488
diff changeset
  2906
                   'remove': ([], _('removing %s\n')),
22491
5e16fe6fdd32 revert: add a `drop` action
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22490
diff changeset
  2907
                   'drop': ([], _('removing %s\n')),
22489
0d57bf80c7cb revert: have an explicit action for "forget"
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22488
diff changeset
  2908
                   'forget': ([], _('forgetting %s\n')),
22231
10d9e7908a3c revert: use actions[...] in all disptable cases
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22230
diff changeset
  2909
                   'undelete': ([], _('undeleting %s\n')),
22234
fe9fc29ac2d0 revert: add a message to noop action
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22233
diff changeset
  2910
                   'noop': (None, _('no changes needed to %s\n')),
22236
3c24fb96900f revert: handle unknown files through status
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22235
diff changeset
  2911
                   'unknown': (None, _('file not managed: %s\n')),
22231
10d9e7908a3c revert: use actions[...] in all disptable cases
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22230
diff changeset
  2912
                  }
16304
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2913
22608
bf0ecb224316 revert: small refactoring in the way backup value are handled
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22588
diff changeset
  2914
        # "constant" that convey the backup strategy.
bf0ecb224316 revert: small refactoring in the way backup value are handled
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22588
diff changeset
  2915
        # All set to `discard` if `no-backup` is set do avoid checking
bf0ecb224316 revert: small refactoring in the way backup value are handled
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22588
diff changeset
  2916
        # no_backup lower in the code.
22609
3760ebf786b8 revert: distinguish between "check" and "backup" strategy
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22608
diff changeset
  2917
        # These values are ordered for comparison purposes
29498
1b38cfde9530 revert: don't backup if no files reverted in interactive mode (issue4793)
skarlage <skarlage@fb.com>
parents: 29427
diff changeset
  2918
        backupinteractive = 3 # do backup if interactively modified
22608
bf0ecb224316 revert: small refactoring in the way backup value are handled
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22588
diff changeset
  2919
        backup = 2  # unconditionally do backup
22609
3760ebf786b8 revert: distinguish between "check" and "backup" strategy
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22608
diff changeset
  2920
        check = 1   # check if the existing file differs from target
22608
bf0ecb224316 revert: small refactoring in the way backup value are handled
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22588
diff changeset
  2921
        discard = 0 # never do backup
bf0ecb224316 revert: small refactoring in the way backup value are handled
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22588
diff changeset
  2922
        if opts.get('no_backup'):
29498
1b38cfde9530 revert: don't backup if no files reverted in interactive mode (issue4793)
skarlage <skarlage@fb.com>
parents: 29427
diff changeset
  2923
            backupinteractive = backup = check = discard
1b38cfde9530 revert: don't backup if no files reverted in interactive mode (issue4793)
skarlage <skarlage@fb.com>
parents: 29427
diff changeset
  2924
        if interactive:
1b38cfde9530 revert: don't backup if no files reverted in interactive mode (issue4793)
skarlage <skarlage@fb.com>
parents: 29427
diff changeset
  2925
            dsmodifiedbackup = backupinteractive
1b38cfde9530 revert: don't backup if no files reverted in interactive mode (issue4793)
skarlage <skarlage@fb.com>
parents: 29427
diff changeset
  2926
        else:
1b38cfde9530 revert: don't backup if no files reverted in interactive mode (issue4793)
skarlage <skarlage@fb.com>
parents: 29427
diff changeset
  2927
            dsmodifiedbackup = backup
1b38cfde9530 revert: don't backup if no files reverted in interactive mode (issue4793)
skarlage <skarlage@fb.com>
parents: 29427
diff changeset
  2928
        tobackup = set()
16304
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2929
22611
2ff28e07d7d6 revert: properly back up added files with local modification
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22610
diff changeset
  2930
        backupanddel = actions['remove']
2ff28e07d7d6 revert: properly back up added files with local modification
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22610
diff changeset
  2931
        if not opts.get('no_backup'):
2ff28e07d7d6 revert: properly back up added files with local modification
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22610
diff changeset
  2932
            backupanddel = actions['drop']
2ff28e07d7d6 revert: properly back up added files with local modification
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22610
diff changeset
  2933
16304
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2934
        disptable = (
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2935
            # dispatch table:
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2936
            #   file state
22153
fc8bc2787528 revert: move manifest membership condition outside of the loop
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22013
diff changeset
  2937
            #   action
fc8bc2787528 revert: move manifest membership condition outside of the loop
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22013
diff changeset
  2938
            #   make backup
22371
81ad62defef5 revert: add documentation in the dispatch table
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22370
diff changeset
  2939
81ad62defef5 revert: add documentation in the dispatch table
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22370
diff changeset
  2940
            ## Sets that results that will change file on disk
81ad62defef5 revert: add documentation in the dispatch table
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22370
diff changeset
  2941
            # Modified compared to target, no local change
22372
8da5864dcfda revert: add more padding in the dispatch list
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22371
diff changeset
  2942
            (modified,      actions['revert'],   discard),
22397
1db04829bdc1 revert: distinguish between deleted file and locally modified
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22396
diff changeset
  2943
            # Modified compared to target, but local file is deleted
1db04829bdc1 revert: distinguish between deleted file and locally modified
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22396
diff changeset
  2944
            (deleted,       actions['revert'],   discard),
22371
81ad62defef5 revert: add documentation in the dispatch table
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22370
diff changeset
  2945
            # Modified compared to target, local change
29498
1b38cfde9530 revert: don't backup if no files reverted in interactive mode (issue4793)
skarlage <skarlage@fb.com>
parents: 29427
diff changeset
  2946
            (dsmodified,    actions['revert'],   dsmodifiedbackup),
22371
81ad62defef5 revert: add documentation in the dispatch table
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22370
diff changeset
  2947
            # Added since target
22488
6c52ed3f888e revert: split between newly added file and file added in other changeset
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22485
diff changeset
  2948
            (added,         actions['remove'],   discard),
6c52ed3f888e revert: split between newly added file and file added in other changeset
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22485
diff changeset
  2949
            # Added in working directory
22489
0d57bf80c7cb revert: have an explicit action for "forget"
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22488
diff changeset
  2950
            (dsadded,       actions['forget'],   discard),
22610
0f323ed8effd revert: track added files with local modifications
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22609
diff changeset
  2951
            # Added since target, have local modification
22611
2ff28e07d7d6 revert: properly back up added files with local modification
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22610
diff changeset
  2952
            (modadded,      backupanddel,        backup),
22490
bcab7bc7280e revert: explicitly track added but deleted file
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22489
diff changeset
  2953
            # Added since target but file is missing in working directory
22491
5e16fe6fdd32 revert: add a `drop` action
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22490
diff changeset
  2954
            (deladded,      actions['drop'],   discard),
22371
81ad62defef5 revert: add documentation in the dispatch table
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22370
diff changeset
  2955
            # Removed since  target, before working copy parent
22396
c0213f2cb942 revert: detect unknown files in the same path as files marked as removed
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22395
diff changeset
  2956
            (removed,       actions['add'],      discard),
c0213f2cb942 revert: detect unknown files in the same path as files marked as removed
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22395
diff changeset
  2957
            # Same as `removed` but an unknown file exists at the same path
22609
3760ebf786b8 revert: distinguish between "check" and "backup" strategy
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22608
diff changeset
  2958
            (removunk,      actions['add'],      check),
22371
81ad62defef5 revert: add documentation in the dispatch table
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22370
diff changeset
  2959
            # Removed since targe, marked as such in working copy parent
22396
c0213f2cb942 revert: detect unknown files in the same path as files marked as removed
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22395
diff changeset
  2960
            (dsremoved,     actions['undelete'], discard),
c0213f2cb942 revert: detect unknown files in the same path as files marked as removed
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22395
diff changeset
  2961
            # Same as `dsremoved` but an unknown file exists at the same path
22609
3760ebf786b8 revert: distinguish between "check" and "backup" strategy
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22608
diff changeset
  2962
            (dsremovunk,    actions['undelete'], check),
22371
81ad62defef5 revert: add documentation in the dispatch table
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22370
diff changeset
  2963
            ## the following sets does not result in any file changes
81ad62defef5 revert: add documentation in the dispatch table
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22370
diff changeset
  2964
            # File with no modification
22372
8da5864dcfda revert: add more padding in the dispatch list
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22371
diff changeset
  2965
            (clean,         actions['noop'],     discard),
22371
81ad62defef5 revert: add documentation in the dispatch table
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22370
diff changeset
  2966
            # Existing file, not tracked anywhere
22372
8da5864dcfda revert: add more padding in the dispatch list
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22371
diff changeset
  2967
            (unknown,       actions['unknown'],  discard),
16304
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2968
            )
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2969
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2970
        for abs, (rel, exact) in sorted(names.items()):
21575
8262c2a39ab8 revert: add some inline comments
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 21574
diff changeset
  2971
            # target file to be touch on disk (relative to cwd)
16304
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2972
            target = repo.wjoin(abs)
21575
8262c2a39ab8 revert: add some inline comments
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 21574
diff changeset
  2973
            # search the entry in the dispatch table.
22212
f18aca03ddd9 revert: inline a now useless closure
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22211
diff changeset
  2974
            # if the file is in any of these sets, it was touched in the working
21575
8262c2a39ab8 revert: add some inline comments
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 21574
diff changeset
  2975
            # directory parent and we are sure it needs to be reverted.
22232
91df98701e9e revert: explode the action tuple in the for loop
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22231
diff changeset
  2976
            for table, (xlist, msg), dobackup in disptable:
16304
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2977
                if abs not in table:
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2978
                    continue
22233
4ab61b24e20c revert: simplify loop conditional
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22232
diff changeset
  2979
                if xlist is not None:
4ab61b24e20c revert: simplify loop conditional
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22232
diff changeset
  2980
                    xlist.append(abs)
29498
1b38cfde9530 revert: don't backup if no files reverted in interactive mode (issue4793)
skarlage <skarlage@fb.com>
parents: 29427
diff changeset
  2981
                    if dobackup:
1b38cfde9530 revert: don't backup if no files reverted in interactive mode (issue4793)
skarlage <skarlage@fb.com>
parents: 29427
diff changeset
  2982
                        # If in interactive mode, don't automatically create
1b38cfde9530 revert: don't backup if no files reverted in interactive mode (issue4793)
skarlage <skarlage@fb.com>
parents: 29427
diff changeset
  2983
                        # .orig files (issue4793)
1b38cfde9530 revert: don't backup if no files reverted in interactive mode (issue4793)
skarlage <skarlage@fb.com>
parents: 29427
diff changeset
  2984
                        if dobackup == backupinteractive:
1b38cfde9530 revert: don't backup if no files reverted in interactive mode (issue4793)
skarlage <skarlage@fb.com>
parents: 29427
diff changeset
  2985
                            tobackup.add(abs)
1b38cfde9530 revert: don't backup if no files reverted in interactive mode (issue4793)
skarlage <skarlage@fb.com>
parents: 29427
diff changeset
  2986
                        elif (backup <= dobackup or wctx[abs].cmp(ctx[abs])):
27651
07fc2f2134ba origpath: move from cmdutil to scmutil
Siddharth Agarwal <sid0@fb.com>
parents: 27637
diff changeset
  2987
                            bakname = scmutil.origpath(ui, repo, rel)
22609
3760ebf786b8 revert: distinguish between "check" and "backup" strategy
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22608
diff changeset
  2988
                            ui.note(_('saving current version of %s as %s\n') %
3760ebf786b8 revert: distinguish between "check" and "backup" strategy
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22608
diff changeset
  2989
                                    (rel, bakname))
3760ebf786b8 revert: distinguish between "check" and "backup" strategy
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22608
diff changeset
  2990
                            if not opts.get('dry_run'):
24475
06cbff4674a3 revert: fix --interactive on local modification (issue4576)
Laurent Charignon <lcharignon@fb.com>
parents: 24472
diff changeset
  2991
                                if interactive:
06cbff4674a3 revert: fix --interactive on local modification (issue4576)
Laurent Charignon <lcharignon@fb.com>
parents: 24472
diff changeset
  2992
                                    util.copyfile(target, bakname)
06cbff4674a3 revert: fix --interactive on local modification (issue4576)
Laurent Charignon <lcharignon@fb.com>
parents: 24472
diff changeset
  2993
                                else:
06cbff4674a3 revert: fix --interactive on local modification (issue4576)
Laurent Charignon <lcharignon@fb.com>
parents: 24472
diff changeset
  2994
                                    util.rename(target, bakname)
22233
4ab61b24e20c revert: simplify loop conditional
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22232
diff changeset
  2995
                    if ui.verbose or not exact:
35197
bfd072c52e03 py3: use bytes in place of basestring
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35196
diff changeset
  2996
                        if not isinstance(msg, bytes):
22233
4ab61b24e20c revert: simplify loop conditional
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22232
diff changeset
  2997
                            msg = msg(abs)
4ab61b24e20c revert: simplify loop conditional
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22232
diff changeset
  2998
                        ui.status(msg % rel)
4ab61b24e20c revert: simplify loop conditional
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22232
diff changeset
  2999
                elif exact:
22234
fe9fc29ac2d0 revert: add a message to noop action
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22233
diff changeset
  3000
                    ui.warn(msg % rel)
16304
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  3001
                break
21575
8262c2a39ab8 revert: add some inline comments
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 21574
diff changeset
  3002
16304
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  3003
        if not opts.get('dry_run'):
23965
6156edaa82aa revert: move prefetch to after the actions logic
Durham Goode <durham@fb.com>
parents: 23955
diff changeset
  3004
            needdata = ('revert', 'add', 'undelete')
35923
efbd04238029 cmdutil: convert _revertprefetch() to a generic stored file hook (API)
Matt Harbison <matt_harbison@yahoo.com>
parents: 35910
diff changeset
  3005
            if _revertprefetch is not _revertprefetchstub:
36137
f52a9336ac5f cmdutil: convert the prefetchfiles() hook to a callback mechanism (API)
Matt Harbison <matt_harbison@yahoo.com>
parents: 36010
diff changeset
  3006
                ui.deprecwarn("'cmdutil._revertprefetch' is deprecated, "
f52a9336ac5f cmdutil: convert the prefetchfiles() hook to a callback mechanism (API)
Matt Harbison <matt_harbison@yahoo.com>
parents: 36010
diff changeset
  3007
                              "add a callback to 'scmutil.fileprefetchhooks'",
f52a9336ac5f cmdutil: convert the prefetchfiles() hook to a callback mechanism (API)
Matt Harbison <matt_harbison@yahoo.com>
parents: 36010
diff changeset
  3008
                              '4.6', stacklevel=1)
35923
efbd04238029 cmdutil: convert _revertprefetch() to a generic stored file hook (API)
Matt Harbison <matt_harbison@yahoo.com>
parents: 35910
diff changeset
  3009
                _revertprefetch(repo, ctx,
efbd04238029 cmdutil: convert _revertprefetch() to a generic stored file hook (API)
Matt Harbison <matt_harbison@yahoo.com>
parents: 35910
diff changeset
  3010
                                *[actions[name][0] for name in needdata])
efbd04238029 cmdutil: convert _revertprefetch() to a generic stored file hook (API)
Matt Harbison <matt_harbison@yahoo.com>
parents: 35910
diff changeset
  3011
            oplist = [actions[name][0] for name in needdata]
37762
7269b87f817c scmutil: teach the file prefetch hook to handle multiple commits
Matt Harbison <matt_harbison@yahoo.com>
parents: 37756
diff changeset
  3012
            prefetch = scmutil.prefetchfiles
7269b87f817c scmutil: teach the file prefetch hook to handle multiple commits
Matt Harbison <matt_harbison@yahoo.com>
parents: 37756
diff changeset
  3013
            matchfiles = scmutil.matchfiles
7269b87f817c scmutil: teach the file prefetch hook to handle multiple commits
Matt Harbison <matt_harbison@yahoo.com>
parents: 37756
diff changeset
  3014
            prefetch(repo, [ctx.rev()],
7269b87f817c scmutil: teach the file prefetch hook to handle multiple commits
Matt Harbison <matt_harbison@yahoo.com>
parents: 37756
diff changeset
  3015
                     matchfiles(repo,
7269b87f817c scmutil: teach the file prefetch hook to handle multiple commits
Matt Harbison <matt_harbison@yahoo.com>
parents: 37756
diff changeset
  3016
                                [f for sublist in oplist for f in sublist]))
29498
1b38cfde9530 revert: don't backup if no files reverted in interactive mode (issue4793)
skarlage <skarlage@fb.com>
parents: 29427
diff changeset
  3017
            _performrevert(repo, parents, ctx, actions, interactive, tobackup)
19129
bd19587a3347 revert: ensure that copies and renames are honored (issue3920)
Bryan O'Sullivan <bryano@fb.com>
parents: 19024
diff changeset
  3018
24134
afed5d2e7985 revert: display full subrepo output with --dry-run
Matt Harbison <matt_harbison@yahoo.com>
parents: 24064
diff changeset
  3019
        if targetsubs:
afed5d2e7985 revert: display full subrepo output with --dry-run
Matt Harbison <matt_harbison@yahoo.com>
parents: 24064
diff changeset
  3020
            # Revert the subrepos on the revert list
afed5d2e7985 revert: display full subrepo output with --dry-run
Matt Harbison <matt_harbison@yahoo.com>
parents: 24064
diff changeset
  3021
            for sub in targetsubs:
24463
06d199e66bbc revert: handle subrepos missing in the given --rev
Matt Harbison <matt_harbison@yahoo.com>
parents: 24455
diff changeset
  3022
                try:
35147
3da4bd50103d py3: fix handling of keyword arguments in revert
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35123
diff changeset
  3023
                    wctx.sub(sub).revert(ctx.substate[sub], *pats,
3da4bd50103d py3: fix handling of keyword arguments in revert
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35123
diff changeset
  3024
                                         **pycompat.strkwargs(opts))
24463
06d199e66bbc revert: handle subrepos missing in the given --rev
Matt Harbison <matt_harbison@yahoo.com>
parents: 24455
diff changeset
  3025
                except KeyError:
26587
56b2bcea2529 error: get Abort from 'error' instead of 'util'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26579
diff changeset
  3026
                    raise error.Abort("subrepository '%s' does not exist in %s!"
24463
06d199e66bbc revert: handle subrepos missing in the given --rev
Matt Harbison <matt_harbison@yahoo.com>
parents: 24455
diff changeset
  3027
                                      % (sub, short(ctx.node())))
16304
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  3028
35923
efbd04238029 cmdutil: convert _revertprefetch() to a generic stored file hook (API)
Matt Harbison <matt_harbison@yahoo.com>
parents: 35910
diff changeset
  3029
def _revertprefetchstub(repo, ctx, *files):
efbd04238029 cmdutil: convert _revertprefetch() to a generic stored file hook (API)
Matt Harbison <matt_harbison@yahoo.com>
parents: 35910
diff changeset
  3030
    """Stub method for detecting extension wrapping of _revertprefetch(), to
efbd04238029 cmdutil: convert _revertprefetch() to a generic stored file hook (API)
Matt Harbison <matt_harbison@yahoo.com>
parents: 35910
diff changeset
  3031
    issue a deprecation warning."""
efbd04238029 cmdutil: convert _revertprefetch() to a generic stored file hook (API)
Matt Harbison <matt_harbison@yahoo.com>
parents: 35910
diff changeset
  3032
efbd04238029 cmdutil: convert _revertprefetch() to a generic stored file hook (API)
Matt Harbison <matt_harbison@yahoo.com>
parents: 35910
diff changeset
  3033
_revertprefetch = _revertprefetchstub
22370
45e02cfad4bd revert: add a way for external extensions to prefetch file data
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22305
diff changeset
  3034
29498
1b38cfde9530 revert: don't backup if no files reverted in interactive mode (issue4793)
skarlage <skarlage@fb.com>
parents: 29427
diff changeset
  3035
def _performrevert(repo, parents, ctx, actions, interactive=False,
1b38cfde9530 revert: don't backup if no files reverted in interactive mode (issue4793)
skarlage <skarlage@fb.com>
parents: 29427
diff changeset
  3036
                   tobackup=None):
21576
33395a7e5527 revert: group action into a single dictionary
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 21575
diff changeset
  3037
    """function that actually perform all the actions computed for revert
20571
d4893e64f300 revert: extract actual revert in its own function
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20553
diff changeset
  3038
d4893e64f300 revert: extract actual revert in its own function
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20553
diff changeset
  3039
    This is an independent function to let extension to plug in and react to
d4893e64f300 revert: extract actual revert in its own function
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20553
diff changeset
  3040
    the imminent revert.
d4893e64f300 revert: extract actual revert in its own function
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20553
diff changeset
  3041
21024
7731a2281cf0 spelling: fixes from spell checker
Mads Kiilerich <madski@unity3d.com>
parents: 20790
diff changeset
  3042
    Make sure you have the working directory locked when calling this function.
20571
d4893e64f300 revert: extract actual revert in its own function
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20553
diff changeset
  3043
    """
d4893e64f300 revert: extract actual revert in its own function
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20553
diff changeset
  3044
    parent, p2 = parents
d4893e64f300 revert: extract actual revert in its own function
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20553
diff changeset
  3045
    node = ctx.node()
27985
79139c7a88bd revert: makes interactive mode ask to forget added files (issue4936)
liscju <piotr.listkiewicz@gmail.com>
parents: 27973
diff changeset
  3046
    excluded_files = []
79139c7a88bd revert: makes interactive mode ask to forget added files (issue4936)
liscju <piotr.listkiewicz@gmail.com>
parents: 27973
diff changeset
  3047
20571
d4893e64f300 revert: extract actual revert in its own function
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20553
diff changeset
  3048
    def checkout(f):
d4893e64f300 revert: extract actual revert in its own function
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20553
diff changeset
  3049
        fc = ctx[f]
25755
72d395e399c1 cmdutil: remove useless dirstate.normallookup() invocation in revert()
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 25739
diff changeset
  3050
        repo.wwrite(f, fc.data(), fc.flags())
20571
d4893e64f300 revert: extract actual revert in its own function
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20553
diff changeset
  3051
30532
66b162fa3ffa revert: prompt before removing files in interactive mode
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 30531
diff changeset
  3052
    def doremove(f):
66b162fa3ffa revert: prompt before removing files in interactive mode
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 30531
diff changeset
  3053
        try:
31309
8908f985570c vfs: use repo.wvfs.unlinkpath
Mads Kiilerich <madski@unity3d.com>
parents: 31237
diff changeset
  3054
            repo.wvfs.unlinkpath(f)
30532
66b162fa3ffa revert: prompt before removing files in interactive mode
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 30531
diff changeset
  3055
        except OSError:
66b162fa3ffa revert: prompt before removing files in interactive mode
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 30531
diff changeset
  3056
            pass
66b162fa3ffa revert: prompt before removing files in interactive mode
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 30531
diff changeset
  3057
        repo.dirstate.remove(f)
66b162fa3ffa revert: prompt before removing files in interactive mode
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 30531
diff changeset
  3058
33649
377e8ddaebef pathauditor: disable cache of audited paths by default (issue5628)
Yuya Nishihara <yuya@tcha.org>
parents: 33617
diff changeset
  3059
    audit_path = pathutil.pathauditor(repo.root, cached=True)
22489
0d57bf80c7cb revert: have an explicit action for "forget"
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22488
diff changeset
  3060
    for f in actions['forget'][0]:
27985
79139c7a88bd revert: makes interactive mode ask to forget added files (issue4936)
liscju <piotr.listkiewicz@gmail.com>
parents: 27973
diff changeset
  3061
        if interactive:
30530
74013a831872 style: avoid an unnecessary line split
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 30519
diff changeset
  3062
            choice = repo.ui.promptchoice(
30531
841092fd6b85 revert: indicate the default choice when prompting to forget files
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 30530
diff changeset
  3063
                _("forget added file %s (Yn)?$$ &Yes $$ &No") % f)
27985
79139c7a88bd revert: makes interactive mode ask to forget added files (issue4936)
liscju <piotr.listkiewicz@gmail.com>
parents: 27973
diff changeset
  3064
            if choice == 0:
79139c7a88bd revert: makes interactive mode ask to forget added files (issue4936)
liscju <piotr.listkiewicz@gmail.com>
parents: 27973
diff changeset
  3065
                repo.dirstate.drop(f)
79139c7a88bd revert: makes interactive mode ask to forget added files (issue4936)
liscju <piotr.listkiewicz@gmail.com>
parents: 27973
diff changeset
  3066
            else:
36194
39b3aab6231e revert: use an exact matcher in interactive diff selection (issue5789)
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 36141
diff changeset
  3067
                excluded_files.append(f)
27985
79139c7a88bd revert: makes interactive mode ask to forget added files (issue4936)
liscju <piotr.listkiewicz@gmail.com>
parents: 27973
diff changeset
  3068
        else:
79139c7a88bd revert: makes interactive mode ask to forget added files (issue4936)
liscju <piotr.listkiewicz@gmail.com>
parents: 27973
diff changeset
  3069
            repo.dirstate.drop(f)
21576
33395a7e5527 revert: group action into a single dictionary
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 21575
diff changeset
  3070
    for f in actions['remove'][0]:
20571
d4893e64f300 revert: extract actual revert in its own function
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20553
diff changeset
  3071
        audit_path(f)
30532
66b162fa3ffa revert: prompt before removing files in interactive mode
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 30531
diff changeset
  3072
        if interactive:
66b162fa3ffa revert: prompt before removing files in interactive mode
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 30531
diff changeset
  3073
            choice = repo.ui.promptchoice(
66b162fa3ffa revert: prompt before removing files in interactive mode
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 30531
diff changeset
  3074
                _("remove added file %s (Yn)?$$ &Yes $$ &No") % f)
66b162fa3ffa revert: prompt before removing files in interactive mode
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 30531
diff changeset
  3075
            if choice == 0:
66b162fa3ffa revert: prompt before removing files in interactive mode
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 30531
diff changeset
  3076
                doremove(f)
66b162fa3ffa revert: prompt before removing files in interactive mode
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 30531
diff changeset
  3077
            else:
36194
39b3aab6231e revert: use an exact matcher in interactive diff selection (issue5789)
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 36141
diff changeset
  3078
                excluded_files.append(f)
30532
66b162fa3ffa revert: prompt before removing files in interactive mode
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 30531
diff changeset
  3079
        else:
66b162fa3ffa revert: prompt before removing files in interactive mode
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 30531
diff changeset
  3080
            doremove(f)
22491
5e16fe6fdd32 revert: add a `drop` action
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22490
diff changeset
  3081
    for f in actions['drop'][0]:
5e16fe6fdd32 revert: add a `drop` action
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22490
diff changeset
  3082
        audit_path(f)
20571
d4893e64f300 revert: extract actual revert in its own function
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20553
diff changeset
  3083
        repo.dirstate.remove(f)
d4893e64f300 revert: extract actual revert in its own function
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20553
diff changeset
  3084
d4893e64f300 revert: extract actual revert in its own function
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20553
diff changeset
  3085
    normal = None
d4893e64f300 revert: extract actual revert in its own function
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20553
diff changeset
  3086
    if node == parent:
d4893e64f300 revert: extract actual revert in its own function
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20553
diff changeset
  3087
        # We're reverting to our parent. If possible, we'd like status
d4893e64f300 revert: extract actual revert in its own function
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20553
diff changeset
  3088
        # to report the file as clean. We have to use normallookup for
d4893e64f300 revert: extract actual revert in its own function
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20553
diff changeset
  3089
        # merges to avoid losing information about merged/dirty files.
d4893e64f300 revert: extract actual revert in its own function
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20553
diff changeset
  3090
        if p2 != nullid:
d4893e64f300 revert: extract actual revert in its own function
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20553
diff changeset
  3091
            normal = repo.dirstate.normallookup
d4893e64f300 revert: extract actual revert in its own function
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20553
diff changeset
  3092
        else:
d4893e64f300 revert: extract actual revert in its own function
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20553
diff changeset
  3093
            normal = repo.dirstate.normal
24359
521fe8287dd5 revert: add flag to make revert interactive
Laurent Charignon <lcharignon@fb.com>
parents: 24358
diff changeset
  3094
25259
5b05f10c5024 revert: fix edition of newly added file during --interactive
Laurent Charignon <lcharignon@fb.com>
parents: 25258
diff changeset
  3095
    newlyaddedandmodifiedfiles = set()
24359
521fe8287dd5 revert: add flag to make revert interactive
Laurent Charignon <lcharignon@fb.com>
parents: 24358
diff changeset
  3096
    if interactive:
521fe8287dd5 revert: add flag to make revert interactive
Laurent Charignon <lcharignon@fb.com>
parents: 24358
diff changeset
  3097
        # Prompt the user for changes to revert
36194
39b3aab6231e revert: use an exact matcher in interactive diff selection (issue5789)
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 36141
diff changeset
  3098
        torevert = [f for f in actions['revert'][0] if f not in excluded_files]
39b3aab6231e revert: use an exact matcher in interactive diff selection (issue5789)
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 36141
diff changeset
  3099
        m = scmutil.matchfiles(repo, torevert)
25258
f37a69ec3f47 revert: make revert --interactive use git style diff
Laurent Charignon <lcharignon@fb.com>
parents: 25257
diff changeset
  3100
        diffopts = patch.difffeatureopts(repo.ui, whitespace=True)
f37a69ec3f47 revert: make revert --interactive use git style diff
Laurent Charignon <lcharignon@fb.com>
parents: 25257
diff changeset
  3101
        diffopts.nodates = True
f37a69ec3f47 revert: make revert --interactive use git style diff
Laurent Charignon <lcharignon@fb.com>
parents: 25257
diff changeset
  3102
        diffopts.git = True
31196
3af9a9628ecf revert: always display hunks reversed when reverting to parent
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31171
diff changeset
  3103
        operation = 'discard'
3af9a9628ecf revert: always display hunks reversed when reverting to parent
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31171
diff changeset
  3104
        reversehunks = True
3af9a9628ecf revert: always display hunks reversed when reverting to parent
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 31171
diff changeset
  3105
        if node != parent:
34968
3649c3f2cd90 revert: do not reverse hunks in interactive when REV is not parent (issue5096)
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 34928
diff changeset
  3106
            operation = 'apply'
3649c3f2cd90 revert: do not reverse hunks in interactive when REV is not parent (issue5096)
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 34928
diff changeset
  3107
            reversehunks = False
25424
69609f43c752 revert: add an experimental config to use inverted selection
Laurent Charignon <lcharignon@fb.com>
parents: 25358
diff changeset
  3108
        if reversehunks:
69609f43c752 revert: add an experimental config to use inverted selection
Laurent Charignon <lcharignon@fb.com>
parents: 25358
diff changeset
  3109
            diff = patch.diff(repo, ctx.node(), None, m, opts=diffopts)
69609f43c752 revert: add an experimental config to use inverted selection
Laurent Charignon <lcharignon@fb.com>
parents: 25358
diff changeset
  3110
        else:
69609f43c752 revert: add an experimental config to use inverted selection
Laurent Charignon <lcharignon@fb.com>
parents: 25358
diff changeset
  3111
            diff = patch.diff(repo, None, ctx.node(), m, opts=diffopts)
24359
521fe8287dd5 revert: add flag to make revert interactive
Laurent Charignon <lcharignon@fb.com>
parents: 24358
diff changeset
  3112
        originalchunks = patch.parsepatch(diff)
25424
69609f43c752 revert: add an experimental config to use inverted selection
Laurent Charignon <lcharignon@fb.com>
parents: 25358
diff changeset
  3113
24359
521fe8287dd5 revert: add flag to make revert interactive
Laurent Charignon <lcharignon@fb.com>
parents: 24358
diff changeset
  3114
        try:
25424
69609f43c752 revert: add an experimental config to use inverted selection
Laurent Charignon <lcharignon@fb.com>
parents: 25358
diff changeset
  3115
29283
14eee72c8d52 revert: use "discard"/"revert" verb when reverting interactively (issue5143)
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 29216
diff changeset
  3116
            chunks, opts = recordfilter(repo.ui, originalchunks,
14eee72c8d52 revert: use "discard"/"revert" verb when reverting interactively (issue5143)
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 29216
diff changeset
  3117
                                        operation=operation)
25424
69609f43c752 revert: add an experimental config to use inverted selection
Laurent Charignon <lcharignon@fb.com>
parents: 25358
diff changeset
  3118
            if reversehunks:
69609f43c752 revert: add an experimental config to use inverted selection
Laurent Charignon <lcharignon@fb.com>
parents: 25358
diff changeset
  3119
                chunks = patch.reversehunks(chunks)
69609f43c752 revert: add an experimental config to use inverted selection
Laurent Charignon <lcharignon@fb.com>
parents: 25358
diff changeset
  3120
34251
61714510220d error: move patch.PatchError so it can easily implement __bytes__ (API)
Yuya Nishihara <yuya@tcha.org>
parents: 34189
diff changeset
  3121
        except error.PatchError as err:
26587
56b2bcea2529 error: get Abort from 'error' instead of 'util'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26579
diff changeset
  3122
            raise error.Abort(_('error parsing patch: %s') % err)
24359
521fe8287dd5 revert: add flag to make revert interactive
Laurent Charignon <lcharignon@fb.com>
parents: 24358
diff changeset
  3123
25259
5b05f10c5024 revert: fix edition of newly added file during --interactive
Laurent Charignon <lcharignon@fb.com>
parents: 25258
diff changeset
  3124
        newlyaddedandmodifiedfiles = newandmodified(chunks, originalchunks)
29498
1b38cfde9530 revert: don't backup if no files reverted in interactive mode (issue4793)
skarlage <skarlage@fb.com>
parents: 29427
diff changeset
  3125
        if tobackup is None:
1b38cfde9530 revert: don't backup if no files reverted in interactive mode (issue4793)
skarlage <skarlage@fb.com>
parents: 29427
diff changeset
  3126
            tobackup = set()
24359
521fe8287dd5 revert: add flag to make revert interactive
Laurent Charignon <lcharignon@fb.com>
parents: 24358
diff changeset
  3127
        # Apply changes
28861
86db5cb55d46 pycompat: switch to util.stringio for py3 compat
timeless <timeless@mozdev.org>
parents: 28837
diff changeset
  3128
        fp = stringio()
24359
521fe8287dd5 revert: add flag to make revert interactive
Laurent Charignon <lcharignon@fb.com>
parents: 24358
diff changeset
  3129
        for c in chunks:
29498
1b38cfde9530 revert: don't backup if no files reverted in interactive mode (issue4793)
skarlage <skarlage@fb.com>
parents: 29427
diff changeset
  3130
            # Create a backup file only if this hunk should be backed up
1b38cfde9530 revert: don't backup if no files reverted in interactive mode (issue4793)
skarlage <skarlage@fb.com>
parents: 29427
diff changeset
  3131
            if ishunk(c) and c.header.filename() in tobackup:
1b38cfde9530 revert: don't backup if no files reverted in interactive mode (issue4793)
skarlage <skarlage@fb.com>
parents: 29427
diff changeset
  3132
                abs = c.header.filename()
1b38cfde9530 revert: don't backup if no files reverted in interactive mode (issue4793)
skarlage <skarlage@fb.com>
parents: 29427
diff changeset
  3133
                target = repo.wjoin(abs)
1b38cfde9530 revert: don't backup if no files reverted in interactive mode (issue4793)
skarlage <skarlage@fb.com>
parents: 29427
diff changeset
  3134
                bakname = scmutil.origpath(repo.ui, repo, m.rel(abs))
1b38cfde9530 revert: don't backup if no files reverted in interactive mode (issue4793)
skarlage <skarlage@fb.com>
parents: 29427
diff changeset
  3135
                util.copyfile(target, bakname)
1b38cfde9530 revert: don't backup if no files reverted in interactive mode (issue4793)
skarlage <skarlage@fb.com>
parents: 29427
diff changeset
  3136
                tobackup.remove(abs)
24359
521fe8287dd5 revert: add flag to make revert interactive
Laurent Charignon <lcharignon@fb.com>
parents: 24358
diff changeset
  3137
            c.write(fp)
521fe8287dd5 revert: add flag to make revert interactive
Laurent Charignon <lcharignon@fb.com>
parents: 24358
diff changeset
  3138
        dopatch = fp.tell()
521fe8287dd5 revert: add flag to make revert interactive
Laurent Charignon <lcharignon@fb.com>
parents: 24358
diff changeset
  3139
        fp.seek(0)
521fe8287dd5 revert: add flag to make revert interactive
Laurent Charignon <lcharignon@fb.com>
parents: 24358
diff changeset
  3140
        if dopatch:
521fe8287dd5 revert: add flag to make revert interactive
Laurent Charignon <lcharignon@fb.com>
parents: 24358
diff changeset
  3141
            try:
521fe8287dd5 revert: add flag to make revert interactive
Laurent Charignon <lcharignon@fb.com>
parents: 24358
diff changeset
  3142
                patch.internalpatch(repo.ui, repo, fp, 1, eolmode=None)
34251
61714510220d error: move patch.PatchError so it can easily implement __bytes__ (API)
Yuya Nishihara <yuya@tcha.org>
parents: 34189
diff changeset
  3143
            except error.PatchError as err:
36719
390d16ea7c76 py3: use pycompat.bytestr instead of str
Pulkit Goyal <7895pulkit@gmail.com>
parents: 36663
diff changeset
  3144
                raise error.Abort(pycompat.bytestr(err))
24359
521fe8287dd5 revert: add flag to make revert interactive
Laurent Charignon <lcharignon@fb.com>
parents: 24358
diff changeset
  3145
        del fp
521fe8287dd5 revert: add flag to make revert interactive
Laurent Charignon <lcharignon@fb.com>
parents: 24358
diff changeset
  3146
    else:
521fe8287dd5 revert: add flag to make revert interactive
Laurent Charignon <lcharignon@fb.com>
parents: 24358
diff changeset
  3147
        for f in actions['revert'][0]:
25755
72d395e399c1 cmdutil: remove useless dirstate.normallookup() invocation in revert()
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 25739
diff changeset
  3148
            checkout(f)
24359
521fe8287dd5 revert: add flag to make revert interactive
Laurent Charignon <lcharignon@fb.com>
parents: 24358
diff changeset
  3149
            if normal:
521fe8287dd5 revert: add flag to make revert interactive
Laurent Charignon <lcharignon@fb.com>
parents: 24358
diff changeset
  3150
                normal(f)
20571
d4893e64f300 revert: extract actual revert in its own function
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20553
diff changeset
  3151
21576
33395a7e5527 revert: group action into a single dictionary
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 21575
diff changeset
  3152
    for f in actions['add'][0]:
25259
5b05f10c5024 revert: fix edition of newly added file during --interactive
Laurent Charignon <lcharignon@fb.com>
parents: 25258
diff changeset
  3153
        # Don't checkout modified files, they are already created by the diff
5b05f10c5024 revert: fix edition of newly added file during --interactive
Laurent Charignon <lcharignon@fb.com>
parents: 25258
diff changeset
  3154
        if f not in newlyaddedandmodifiedfiles:
5b05f10c5024 revert: fix edition of newly added file during --interactive
Laurent Charignon <lcharignon@fb.com>
parents: 25258
diff changeset
  3155
            checkout(f)
5b05f10c5024 revert: fix edition of newly added file during --interactive
Laurent Charignon <lcharignon@fb.com>
parents: 25258
diff changeset
  3156
            repo.dirstate.add(f)
20571
d4893e64f300 revert: extract actual revert in its own function
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20553
diff changeset
  3157
d4893e64f300 revert: extract actual revert in its own function
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20553
diff changeset
  3158
    normal = repo.dirstate.normallookup
d4893e64f300 revert: extract actual revert in its own function
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20553
diff changeset
  3159
    if node == parent and p2 == nullid:
d4893e64f300 revert: extract actual revert in its own function
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20553
diff changeset
  3160
        normal = repo.dirstate.normal
21576
33395a7e5527 revert: group action into a single dictionary
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 21575
diff changeset
  3161
    for f in actions['undelete'][0]:
20571
d4893e64f300 revert: extract actual revert in its own function
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20553
diff changeset
  3162
        checkout(f)
d4893e64f300 revert: extract actual revert in its own function
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20553
diff changeset
  3163
        normal(f)
d4893e64f300 revert: extract actual revert in its own function
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20553
diff changeset
  3164
d4893e64f300 revert: extract actual revert in its own function
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20553
diff changeset
  3165
    copied = copies.pathcopies(repo[parent], ctx)
d4893e64f300 revert: extract actual revert in its own function
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20553
diff changeset
  3166
21576
33395a7e5527 revert: group action into a single dictionary
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 21575
diff changeset
  3167
    for f in actions['add'][0] + actions['undelete'][0] + actions['revert'][0]:
20571
d4893e64f300 revert: extract actual revert in its own function
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20553
diff changeset
  3168
        if f in copied:
d4893e64f300 revert: extract actual revert in its own function
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20553
diff changeset
  3169
            repo.dirstate.copy(copied[f], f)
d4893e64f300 revert: extract actual revert in its own function
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20553
diff changeset
  3170
32343
d47d7d3bd07b extensions: show deprecation warning for the use of cmdutil.command
Yuya Nishihara <yuya@tcha.org>
parents: 32337
diff changeset
  3171
class command(registrar.command):
35107
b22a0d9e0a83 docs: add args/returns docs for some cmdutil, context, and registrar functions
rlevasseur@google.com
parents: 35007
diff changeset
  3172
    """deprecated: used registrar.command instead"""
32343
d47d7d3bd07b extensions: show deprecation warning for the use of cmdutil.command
Yuya Nishihara <yuya@tcha.org>
parents: 32337
diff changeset
  3173
    def _doregister(self, func, name, *args, **kwargs):
d47d7d3bd07b extensions: show deprecation warning for the use of cmdutil.command
Yuya Nishihara <yuya@tcha.org>
parents: 32337
diff changeset
  3174
        func._deprecatedregistrar = True  # flag for deprecwarn in extensions.py
d47d7d3bd07b extensions: show deprecation warning for the use of cmdutil.command
Yuya Nishihara <yuya@tcha.org>
parents: 32337
diff changeset
  3175
        return super(command, self)._doregister(func, name, *args, **kwargs)
19211
3bfd7f1e7485 summary: augment output with info from extensions
Bryan O'Sullivan <bryano@fb.com>
parents: 19129
diff changeset
  3176
21051
1004d3cd65fd outgoing: introduce "outgoinghooks" to avoid redundant outgoing check
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21047
diff changeset
  3177
# a list of (ui, repo, otherpeer, opts, missing) functions called by
1004d3cd65fd outgoing: introduce "outgoinghooks" to avoid redundant outgoing check
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21047
diff changeset
  3178
# commands.outgoing.  "missing" is "missing" of the result of
1004d3cd65fd outgoing: introduce "outgoinghooks" to avoid redundant outgoing check
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21047
diff changeset
  3179
# "findcommonoutgoing()"
1004d3cd65fd outgoing: introduce "outgoinghooks" to avoid redundant outgoing check
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21047
diff changeset
  3180
outgoinghooks = util.hooks()
1004d3cd65fd outgoing: introduce "outgoinghooks" to avoid redundant outgoing check
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21047
diff changeset
  3181
19211
3bfd7f1e7485 summary: augment output with info from extensions
Bryan O'Sullivan <bryano@fb.com>
parents: 19129
diff changeset
  3182
# a list of (ui, repo) functions called by commands.summary
3bfd7f1e7485 summary: augment output with info from extensions
Bryan O'Sullivan <bryano@fb.com>
parents: 19129
diff changeset
  3183
summaryhooks = util.hooks()
19474
894fd1a7c533 cmdutil: core functionality to block during multistep commands (issue3955)
Matt Mackall <mpm@selenic.com>
parents: 19293
diff changeset
  3184
21047
f0003f989e72 summary: introduce "summaryremotehooks" to avoid redundant incoming/outgoing check
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21041
diff changeset
  3185
# a list of (ui, repo, opts, changes) functions called by commands.summary.
f0003f989e72 summary: introduce "summaryremotehooks" to avoid redundant incoming/outgoing check
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21041
diff changeset
  3186
#
f0003f989e72 summary: introduce "summaryremotehooks" to avoid redundant incoming/outgoing check
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21041
diff changeset
  3187
# functions should return tuple of booleans below, if 'changes' is None:
f0003f989e72 summary: introduce "summaryremotehooks" to avoid redundant incoming/outgoing check
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21041
diff changeset
  3188
#  (whether-incomings-are-needed, whether-outgoings-are-needed)
f0003f989e72 summary: introduce "summaryremotehooks" to avoid redundant incoming/outgoing check
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21041
diff changeset
  3189
#
f0003f989e72 summary: introduce "summaryremotehooks" to avoid redundant incoming/outgoing check
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21041
diff changeset
  3190
# otherwise, 'changes' is a tuple of tuples below:
f0003f989e72 summary: introduce "summaryremotehooks" to avoid redundant incoming/outgoing check
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21041
diff changeset
  3191
#  - (sourceurl, sourcebranch, sourcepeer, incoming)
f0003f989e72 summary: introduce "summaryremotehooks" to avoid redundant incoming/outgoing check
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21041
diff changeset
  3192
#  - (desturl,   destbranch,   destpeer,   outgoing)
f0003f989e72 summary: introduce "summaryremotehooks" to avoid redundant incoming/outgoing check
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21041
diff changeset
  3193
summaryremotehooks = util.hooks()
f0003f989e72 summary: introduce "summaryremotehooks" to avoid redundant incoming/outgoing check
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21041
diff changeset
  3194
19474
894fd1a7c533 cmdutil: core functionality to block during multistep commands (issue3955)
Matt Mackall <mpm@selenic.com>
parents: 19293
diff changeset
  3195
# A list of state files kept by multistep operations like graft.
894fd1a7c533 cmdutil: core functionality to block during multistep commands (issue3955)
Matt Mackall <mpm@selenic.com>
parents: 19293
diff changeset
  3196
# Since graft cannot be aborted, it is considered 'clearable' by update.
894fd1a7c533 cmdutil: core functionality to block during multistep commands (issue3955)
Matt Mackall <mpm@selenic.com>
parents: 19293
diff changeset
  3197
# note: bisect is intentionally excluded
19496
607191a45f8c checkunfinished: accommodate histedit quirk
Matt Mackall <mpm@selenic.com>
parents: 19482
diff changeset
  3198
# (state file, clearable, allowcommit, error, hint)
19474
894fd1a7c533 cmdutil: core functionality to block during multistep commands (issue3955)
Matt Mackall <mpm@selenic.com>
parents: 19293
diff changeset
  3199
unfinishedstates = [
19496
607191a45f8c checkunfinished: accommodate histedit quirk
Matt Mackall <mpm@selenic.com>
parents: 19482
diff changeset
  3200
    ('graftstate', True, False, _('graft in progress'),
19482
499fc471296b update: add tracking of interrupted updates (issue3113)
Matt Mackall <mpm@selenic.com>
parents: 19474
diff changeset
  3201
     _("use 'hg graft --continue' or 'hg update' to abort")),
19496
607191a45f8c checkunfinished: accommodate histedit quirk
Matt Mackall <mpm@selenic.com>
parents: 19482
diff changeset
  3202
    ('updatestate', True, False, _('last update was interrupted'),
19482
499fc471296b update: add tracking of interrupted updates (issue3113)
Matt Mackall <mpm@selenic.com>
parents: 19474
diff changeset
  3203
     _("use 'hg update' to get a consistent checkout"))
19474
894fd1a7c533 cmdutil: core functionality to block during multistep commands (issue3955)
Matt Mackall <mpm@selenic.com>
parents: 19293
diff changeset
  3204
    ]
894fd1a7c533 cmdutil: core functionality to block during multistep commands (issue3955)
Matt Mackall <mpm@selenic.com>
parents: 19293
diff changeset
  3205
19496
607191a45f8c checkunfinished: accommodate histedit quirk
Matt Mackall <mpm@selenic.com>
parents: 19482
diff changeset
  3206
def checkunfinished(repo, commit=False):
19474
894fd1a7c533 cmdutil: core functionality to block during multistep commands (issue3955)
Matt Mackall <mpm@selenic.com>
parents: 19293
diff changeset
  3207
    '''Look for an unfinished multistep operation, like graft, and abort
894fd1a7c533 cmdutil: core functionality to block during multistep commands (issue3955)
Matt Mackall <mpm@selenic.com>
parents: 19293
diff changeset
  3208
    if found. It's probably good to check this right before
894fd1a7c533 cmdutil: core functionality to block during multistep commands (issue3955)
Matt Mackall <mpm@selenic.com>
parents: 19293
diff changeset
  3209
    bailifchanged().
894fd1a7c533 cmdutil: core functionality to block during multistep commands (issue3955)
Matt Mackall <mpm@selenic.com>
parents: 19293
diff changeset
  3210
    '''
38182
79c54e7c0c52 rebase: prioritize indicating an interrupted rebase over update (issue5838)
Matt Harbison <matt_harbison@yahoo.com>
parents: 37815
diff changeset
  3211
    # Check for non-clearable states first, so things like rebase will take
79c54e7c0c52 rebase: prioritize indicating an interrupted rebase over update (issue5838)
Matt Harbison <matt_harbison@yahoo.com>
parents: 37815
diff changeset
  3212
    # precedence over update.
19496
607191a45f8c checkunfinished: accommodate histedit quirk
Matt Mackall <mpm@selenic.com>
parents: 19482
diff changeset
  3213
    for f, clearable, allowcommit, msg, hint in unfinishedstates:
38182
79c54e7c0c52 rebase: prioritize indicating an interrupted rebase over update (issue5838)
Matt Harbison <matt_harbison@yahoo.com>
parents: 37815
diff changeset
  3214
        if clearable or (commit and allowcommit):
79c54e7c0c52 rebase: prioritize indicating an interrupted rebase over update (issue5838)
Matt Harbison <matt_harbison@yahoo.com>
parents: 37815
diff changeset
  3215
            continue
79c54e7c0c52 rebase: prioritize indicating an interrupted rebase over update (issue5838)
Matt Harbison <matt_harbison@yahoo.com>
parents: 37815
diff changeset
  3216
        if repo.vfs.exists(f):
79c54e7c0c52 rebase: prioritize indicating an interrupted rebase over update (issue5838)
Matt Harbison <matt_harbison@yahoo.com>
parents: 37815
diff changeset
  3217
            raise error.Abort(msg, hint=hint)
79c54e7c0c52 rebase: prioritize indicating an interrupted rebase over update (issue5838)
Matt Harbison <matt_harbison@yahoo.com>
parents: 37815
diff changeset
  3218
79c54e7c0c52 rebase: prioritize indicating an interrupted rebase over update (issue5838)
Matt Harbison <matt_harbison@yahoo.com>
parents: 37815
diff changeset
  3219
    for f, clearable, allowcommit, msg, hint in unfinishedstates:
79c54e7c0c52 rebase: prioritize indicating an interrupted rebase over update (issue5838)
Matt Harbison <matt_harbison@yahoo.com>
parents: 37815
diff changeset
  3220
        if not clearable or (commit and allowcommit):
19496
607191a45f8c checkunfinished: accommodate histedit quirk
Matt Mackall <mpm@selenic.com>
parents: 19482
diff changeset
  3221
            continue
19474
894fd1a7c533 cmdutil: core functionality to block during multistep commands (issue3955)
Matt Mackall <mpm@selenic.com>
parents: 19293
diff changeset
  3222
        if repo.vfs.exists(f):
26587
56b2bcea2529 error: get Abort from 'error' instead of 'util'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26579
diff changeset
  3223
            raise error.Abort(msg, hint=hint)
19474
894fd1a7c533 cmdutil: core functionality to block during multistep commands (issue3955)
Matt Mackall <mpm@selenic.com>
parents: 19293
diff changeset
  3224
894fd1a7c533 cmdutil: core functionality to block during multistep commands (issue3955)
Matt Mackall <mpm@selenic.com>
parents: 19293
diff changeset
  3225
def clearunfinished(repo):
894fd1a7c533 cmdutil: core functionality to block during multistep commands (issue3955)
Matt Mackall <mpm@selenic.com>
parents: 19293
diff changeset
  3226
    '''Check for unfinished operations (as above), and clear the ones
894fd1a7c533 cmdutil: core functionality to block during multistep commands (issue3955)
Matt Mackall <mpm@selenic.com>
parents: 19293
diff changeset
  3227
    that are clearable.
894fd1a7c533 cmdutil: core functionality to block during multistep commands (issue3955)
Matt Mackall <mpm@selenic.com>
parents: 19293
diff changeset
  3228
    '''
19496
607191a45f8c checkunfinished: accommodate histedit quirk
Matt Mackall <mpm@selenic.com>
parents: 19482
diff changeset
  3229
    for f, clearable, allowcommit, msg, hint in unfinishedstates:
19474
894fd1a7c533 cmdutil: core functionality to block during multistep commands (issue3955)
Matt Mackall <mpm@selenic.com>
parents: 19293
diff changeset
  3230
        if not clearable and repo.vfs.exists(f):
26587
56b2bcea2529 error: get Abort from 'error' instead of 'util'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26579
diff changeset
  3231
            raise error.Abort(msg, hint=hint)
19496
607191a45f8c checkunfinished: accommodate histedit quirk
Matt Mackall <mpm@selenic.com>
parents: 19482
diff changeset
  3232
    for f, clearable, allowcommit, msg, hint in unfinishedstates:
19474
894fd1a7c533 cmdutil: core functionality to block during multistep commands (issue3955)
Matt Mackall <mpm@selenic.com>
parents: 19293
diff changeset
  3233
        if clearable and repo.vfs.exists(f):
31320
1b0db28dadf1 cmdutil: directly use repo.vfs.join
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 31309
diff changeset
  3234
            util.unlink(repo.vfs.join(f))
24991
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
  3235
27624
d46db4390715 resolve: suggest the next action
timeless <timeless@mozdev.org>
parents: 27622
diff changeset
  3236
afterresolvedstates = [
27625
cdb9493a7e2f graft: hook afterresolvedstates
timeless <timeless@mozdev.org>
parents: 27624
diff changeset
  3237
    ('graftstate',
cdb9493a7e2f graft: hook afterresolvedstates
timeless <timeless@mozdev.org>
parents: 27624
diff changeset
  3238
     _('hg graft --continue')),
27624
d46db4390715 resolve: suggest the next action
timeless <timeless@mozdev.org>
parents: 27622
diff changeset
  3239
    ]
d46db4390715 resolve: suggest the next action
timeless <timeless@mozdev.org>
parents: 27622
diff changeset
  3240
28120
ed4d06f180b8 cmdutil: provide a way to report how to continue
timeless <timeless@mozdev.org>
parents: 28027
diff changeset
  3241
def howtocontinue(repo):
ed4d06f180b8 cmdutil: provide a way to report how to continue
timeless <timeless@mozdev.org>
parents: 28027
diff changeset
  3242
    '''Check for an unfinished operation and return the command to finish
ed4d06f180b8 cmdutil: provide a way to report how to continue
timeless <timeless@mozdev.org>
parents: 28027
diff changeset
  3243
    it.
ed4d06f180b8 cmdutil: provide a way to report how to continue
timeless <timeless@mozdev.org>
parents: 28027
diff changeset
  3244
30332
318a24b52eeb spelling: fixes of non-dictionary words
Mads Kiilerich <madski@unity3d.com>
parents: 30272
diff changeset
  3245
    afterresolvedstates tuples define a .hg/{file} and the corresponding
28120
ed4d06f180b8 cmdutil: provide a way to report how to continue
timeless <timeless@mozdev.org>
parents: 28027
diff changeset
  3246
    command needed to finish it.
ed4d06f180b8 cmdutil: provide a way to report how to continue
timeless <timeless@mozdev.org>
parents: 28027
diff changeset
  3247
ed4d06f180b8 cmdutil: provide a way to report how to continue
timeless <timeless@mozdev.org>
parents: 28027
diff changeset
  3248
    Returns a (msg, warning) tuple. 'msg' is a string and 'warning' is
ed4d06f180b8 cmdutil: provide a way to report how to continue
timeless <timeless@mozdev.org>
parents: 28027
diff changeset
  3249
    a boolean.
ed4d06f180b8 cmdutil: provide a way to report how to continue
timeless <timeless@mozdev.org>
parents: 28027
diff changeset
  3250
    '''
ed4d06f180b8 cmdutil: provide a way to report how to continue
timeless <timeless@mozdev.org>
parents: 28027
diff changeset
  3251
    contmsg = _("continue: %s")
27624
d46db4390715 resolve: suggest the next action
timeless <timeless@mozdev.org>
parents: 27622
diff changeset
  3252
    for f, msg in afterresolvedstates:
d46db4390715 resolve: suggest the next action
timeless <timeless@mozdev.org>
parents: 27622
diff changeset
  3253
        if repo.vfs.exists(f):
28120
ed4d06f180b8 cmdutil: provide a way to report how to continue
timeless <timeless@mozdev.org>
parents: 28027
diff changeset
  3254
            return contmsg % msg, True
33362
e48fb90f80c8 cmdutil: simplify the dirty check in howtocontinue()
Matt Harbison <matt_harbison@yahoo.com>
parents: 33334
diff changeset
  3255
    if repo[None].dirty(missing=True, merge=False, branch=False):
28120
ed4d06f180b8 cmdutil: provide a way to report how to continue
timeless <timeless@mozdev.org>
parents: 28027
diff changeset
  3256
        return contmsg % _("hg commit"), False
ed4d06f180b8 cmdutil: provide a way to report how to continue
timeless <timeless@mozdev.org>
parents: 28027
diff changeset
  3257
    return None, None
ed4d06f180b8 cmdutil: provide a way to report how to continue
timeless <timeless@mozdev.org>
parents: 28027
diff changeset
  3258
ed4d06f180b8 cmdutil: provide a way to report how to continue
timeless <timeless@mozdev.org>
parents: 28027
diff changeset
  3259
def checkafterresolved(repo):
ed4d06f180b8 cmdutil: provide a way to report how to continue
timeless <timeless@mozdev.org>
parents: 28027
diff changeset
  3260
    '''Inform the user about the next action after completing hg resolve
ed4d06f180b8 cmdutil: provide a way to report how to continue
timeless <timeless@mozdev.org>
parents: 28027
diff changeset
  3261
ed4d06f180b8 cmdutil: provide a way to report how to continue
timeless <timeless@mozdev.org>
parents: 28027
diff changeset
  3262
    If there's a matching afterresolvedstates, howtocontinue will yield
ed4d06f180b8 cmdutil: provide a way to report how to continue
timeless <timeless@mozdev.org>
parents: 28027
diff changeset
  3263
    repo.ui.warn as the reporter.
ed4d06f180b8 cmdutil: provide a way to report how to continue
timeless <timeless@mozdev.org>
parents: 28027
diff changeset
  3264
ed4d06f180b8 cmdutil: provide a way to report how to continue
timeless <timeless@mozdev.org>
parents: 28027
diff changeset
  3265
    Otherwise, it will yield repo.ui.note.
ed4d06f180b8 cmdutil: provide a way to report how to continue
timeless <timeless@mozdev.org>
parents: 28027
diff changeset
  3266
    '''
ed4d06f180b8 cmdutil: provide a way to report how to continue
timeless <timeless@mozdev.org>
parents: 28027
diff changeset
  3267
    msg, warning = howtocontinue(repo)
ed4d06f180b8 cmdutil: provide a way to report how to continue
timeless <timeless@mozdev.org>
parents: 28027
diff changeset
  3268
    if msg is not None:
ed4d06f180b8 cmdutil: provide a way to report how to continue
timeless <timeless@mozdev.org>
parents: 28027
diff changeset
  3269
        if warning:
ed4d06f180b8 cmdutil: provide a way to report how to continue
timeless <timeless@mozdev.org>
parents: 28027
diff changeset
  3270
            repo.ui.warn("%s\n" % msg)
ed4d06f180b8 cmdutil: provide a way to report how to continue
timeless <timeless@mozdev.org>
parents: 28027
diff changeset
  3271
        else:
ed4d06f180b8 cmdutil: provide a way to report how to continue
timeless <timeless@mozdev.org>
parents: 28027
diff changeset
  3272
            repo.ui.note("%s\n" % msg)
ed4d06f180b8 cmdutil: provide a way to report how to continue
timeless <timeless@mozdev.org>
parents: 28027
diff changeset
  3273
ed4d06f180b8 cmdutil: provide a way to report how to continue
timeless <timeless@mozdev.org>
parents: 28027
diff changeset
  3274
def wrongtooltocontinue(repo, task):
ed4d06f180b8 cmdutil: provide a way to report how to continue
timeless <timeless@mozdev.org>
parents: 28027
diff changeset
  3275
    '''Raise an abort suggesting how to properly continue if there is an
ed4d06f180b8 cmdutil: provide a way to report how to continue
timeless <timeless@mozdev.org>
parents: 28027
diff changeset
  3276
    active task.
ed4d06f180b8 cmdutil: provide a way to report how to continue
timeless <timeless@mozdev.org>
parents: 28027
diff changeset
  3277
ed4d06f180b8 cmdutil: provide a way to report how to continue
timeless <timeless@mozdev.org>
parents: 28027
diff changeset
  3278
    Uses howtocontinue() to find the active task.
ed4d06f180b8 cmdutil: provide a way to report how to continue
timeless <timeless@mozdev.org>
parents: 28027
diff changeset
  3279
ed4d06f180b8 cmdutil: provide a way to report how to continue
timeless <timeless@mozdev.org>
parents: 28027
diff changeset
  3280
    If there's no task (repo.ui.note for 'hg commit'), it does not offer
ed4d06f180b8 cmdutil: provide a way to report how to continue
timeless <timeless@mozdev.org>
parents: 28027
diff changeset
  3281
    a hint.
ed4d06f180b8 cmdutil: provide a way to report how to continue
timeless <timeless@mozdev.org>
parents: 28027
diff changeset
  3282
    '''
ed4d06f180b8 cmdutil: provide a way to report how to continue
timeless <timeless@mozdev.org>
parents: 28027
diff changeset
  3283
    after = howtocontinue(repo)
ed4d06f180b8 cmdutil: provide a way to report how to continue
timeless <timeless@mozdev.org>
parents: 28027
diff changeset
  3284
    hint = None
ed4d06f180b8 cmdutil: provide a way to report how to continue
timeless <timeless@mozdev.org>
parents: 28027
diff changeset
  3285
    if after[1]:
ed4d06f180b8 cmdutil: provide a way to report how to continue
timeless <timeless@mozdev.org>
parents: 28027
diff changeset
  3286
        hint = after[0]
ed4d06f180b8 cmdutil: provide a way to report how to continue
timeless <timeless@mozdev.org>
parents: 28027
diff changeset
  3287
    raise error.Abort(_('no %s in progress') % task, hint=hint)
35910
b82c6dd15f5d cmdutil: introduce deprecated aliases
Boris Feld <boris.feld@octobus.net>
parents: 35904
diff changeset
  3288
b82c6dd15f5d cmdutil: introduce deprecated aliases
Boris Feld <boris.feld@octobus.net>
parents: 35904
diff changeset
  3289
class changeset_printer(logcmdutil.changesetprinter):
b82c6dd15f5d cmdutil: introduce deprecated aliases
Boris Feld <boris.feld@octobus.net>
parents: 35904
diff changeset
  3290
b82c6dd15f5d cmdutil: introduce deprecated aliases
Boris Feld <boris.feld@octobus.net>
parents: 35904
diff changeset
  3291
    def __init__(self, ui, *args, **kwargs):
b82c6dd15f5d cmdutil: introduce deprecated aliases
Boris Feld <boris.feld@octobus.net>
parents: 35904
diff changeset
  3292
        msg = ("'cmdutil.changeset_printer' is deprecated, "
b82c6dd15f5d cmdutil: introduce deprecated aliases
Boris Feld <boris.feld@octobus.net>
parents: 35904
diff changeset
  3293
               "use 'logcmdutil.logcmdutil'")
b82c6dd15f5d cmdutil: introduce deprecated aliases
Boris Feld <boris.feld@octobus.net>
parents: 35904
diff changeset
  3294
        ui.deprecwarn(msg, "4.6")
b82c6dd15f5d cmdutil: introduce deprecated aliases
Boris Feld <boris.feld@octobus.net>
parents: 35904
diff changeset
  3295
        super(changeset_printer, self).__init__(ui, *args, **kwargs)
b82c6dd15f5d cmdutil: introduce deprecated aliases
Boris Feld <boris.feld@octobus.net>
parents: 35904
diff changeset
  3296
b82c6dd15f5d cmdutil: introduce deprecated aliases
Boris Feld <boris.feld@octobus.net>
parents: 35904
diff changeset
  3297
def displaygraph(ui, *args, **kwargs):
b82c6dd15f5d cmdutil: introduce deprecated aliases
Boris Feld <boris.feld@octobus.net>
parents: 35904
diff changeset
  3298
    msg = ("'cmdutil.displaygraph' is deprecated, "
b82c6dd15f5d cmdutil: introduce deprecated aliases
Boris Feld <boris.feld@octobus.net>
parents: 35904
diff changeset
  3299
           "use 'logcmdutil.displaygraph'")
b82c6dd15f5d cmdutil: introduce deprecated aliases
Boris Feld <boris.feld@octobus.net>
parents: 35904
diff changeset
  3300
    ui.deprecwarn(msg, "4.6")
b82c6dd15f5d cmdutil: introduce deprecated aliases
Boris Feld <boris.feld@octobus.net>
parents: 35904
diff changeset
  3301
    return logcmdutil.displaygraph(ui, *args, **kwargs)
b82c6dd15f5d cmdutil: introduce deprecated aliases
Boris Feld <boris.feld@octobus.net>
parents: 35904
diff changeset
  3302
b82c6dd15f5d cmdutil: introduce deprecated aliases
Boris Feld <boris.feld@octobus.net>
parents: 35904
diff changeset
  3303
def show_changeset(ui, *args, **kwargs):
b82c6dd15f5d cmdutil: introduce deprecated aliases
Boris Feld <boris.feld@octobus.net>
parents: 35904
diff changeset
  3304
    msg = ("'cmdutil.show_changeset' is deprecated, "
b82c6dd15f5d cmdutil: introduce deprecated aliases
Boris Feld <boris.feld@octobus.net>
parents: 35904
diff changeset
  3305
           "use 'logcmdutil.changesetdisplayer'")
b82c6dd15f5d cmdutil: introduce deprecated aliases
Boris Feld <boris.feld@octobus.net>
parents: 35904
diff changeset
  3306
    ui.deprecwarn(msg, "4.6")
b82c6dd15f5d cmdutil: introduce deprecated aliases
Boris Feld <boris.feld@octobus.net>
parents: 35904
diff changeset
  3307
    return logcmdutil.changesetdisplayer(ui, *args, **kwargs)