mercurial/cmdutil.py
author FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
Mon, 05 May 2014 21:26:40 +0900
changeset 21241 244b177a152e
parent 21240 1a833fcf5a14
child 21405 dcf20f244c2a
permissions -rw-r--r--
cmdutil: omit redundant "savecommitmessage()" in "tryimportone()" The preceding patch causes that "makememctx()" with "editor" argument saves (manually edited) commit message into ".hg/last-message.txt": saving itself is executed indirectly in "memctx.__init__()". This makes it redundant to invoke "savecommitmessage()" on caller side of "makememctx()". This patch omits such redundant "savecommitmessage()" invocation in "tryimportone()". "tryimportone()" uses one of "commiteditor" or "commitforceeditor" as "editor" argument, and this causes saving commit message always.
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
6211
f89fd07fc51d Expand import * to allow Pyflakes to find problems
Joel Rosdahl <joel@rosdahl.net>
parents: 6190
diff changeset
     8
from node import hex, nullid, nullrev, short
3891
6b4127c7d52a Simplify i18n imports
Matt Mackall <mpm@selenic.com>
parents: 3877
diff changeset
     9
from i18n import _
14269
66257848c154 cmdutil: fix errors reported by pyflakes test
Sune Foldager <cryo@cyanite.org>
parents: 14259
diff changeset
    10
import os, sys, errno, re, tempfile
15214
231aac5280ba rebase: move updatedirstate into cmdutil so it can be shared
Matt Mackall <mpm@selenic.com>
parents: 14986
diff changeset
    11
import util, scmutil, templater, patch, error, templatekw, revlog, copies
12085
6f833fc3ccab Consistently import foo as foomod when foo to avoid shadowing
Martin Geisler <mg@aragost.com>
parents: 12032
diff changeset
    12
import match as matchmod
20392
d4f804caa0ed itersubrepos: move to scmutil to break a direct import cycle
Augie Fackler <raf@durin42.com>
parents: 20364
diff changeset
    13
import context, repair, graphmod, revset, phases, obsolete, pathutil
17811
a8aba2921456 amend: add noise in extra to avoid creating obsolescence cycle (issue3664)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 17788
diff changeset
    14
import changelog
18538
94317c2d53b8 commit: show active bookmark in commit editor helper text
Antonio Zanardo <zanardo@gmail.com>
parents: 18364
diff changeset
    15
import bookmarks
17471
ad1561723dde amend: lock the repository during the whole process
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17468
diff changeset
    16
import lock as lockmod
2874
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    17
10401
6252852b4332 mq: add -Q option to all commands not in norepo
Brendan Cully <brendan@kublai.com>
parents: 10344
diff changeset
    18
def parsealiases(cmd):
6252852b4332 mq: add -Q option to all commands not in norepo
Brendan Cully <brendan@kublai.com>
parents: 10344
diff changeset
    19
    return cmd.lstrip("^").split("|")
6252852b4332 mq: add -Q option to all commands not in norepo
Brendan Cully <brendan@kublai.com>
parents: 10344
diff changeset
    20
7213
b4c035057d34 findcmd: have dispatch look up strict flag
Matt Mackall <mpm@selenic.com>
parents: 7121
diff changeset
    21
def findpossible(cmd, table, strict=False):
4549
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
    22
    """
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
    23
    Return cmd -> (aliases, command table entry)
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
    24
    for each matching command.
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
    25
    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
    26
    """
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
    27
    choice = {}
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
    28
    debugchoice = {}
15600
195dbd1cef0c alias: shortcut command matching show shadowing works properly (issue3104)
Matt Mackall <mpm@selenic.com>
parents: 15231
diff changeset
    29
195dbd1cef0c alias: shortcut command matching show shadowing works properly (issue3104)
Matt Mackall <mpm@selenic.com>
parents: 15231
diff changeset
    30
    if cmd in table:
195dbd1cef0c alias: shortcut command matching show shadowing works properly (issue3104)
Matt Mackall <mpm@selenic.com>
parents: 15231
diff changeset
    31
        # 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
    32
        keys = [cmd]
195dbd1cef0c alias: shortcut command matching show shadowing works properly (issue3104)
Matt Mackall <mpm@selenic.com>
parents: 15231
diff changeset
    33
    else:
195dbd1cef0c alias: shortcut command matching show shadowing works properly (issue3104)
Matt Mackall <mpm@selenic.com>
parents: 15231
diff changeset
    34
        keys = table.keys()
195dbd1cef0c alias: shortcut command matching show shadowing works properly (issue3104)
Matt Mackall <mpm@selenic.com>
parents: 15231
diff changeset
    35
195dbd1cef0c alias: shortcut command matching show shadowing works properly (issue3104)
Matt Mackall <mpm@selenic.com>
parents: 15231
diff changeset
    36
    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
    37
        aliases = parsealiases(e)
4549
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
    38
        found = None
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
    39
        if cmd in aliases:
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
    40
            found = cmd
7213
b4c035057d34 findcmd: have dispatch look up strict flag
Matt Mackall <mpm@selenic.com>
parents: 7121
diff changeset
    41
        elif not strict:
4549
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
    42
            for a in aliases:
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
    43
                if a.startswith(cmd):
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
    44
                    found = a
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
    45
                    break
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
    46
        if found is not None:
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
    47
            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
    48
                debugchoice[found] = (aliases, table[e])
4549
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
    49
            else:
5178
18a9fbb5cd78 dispatch: move command dispatching into its own module
Matt Mackall <mpm@selenic.com>
parents: 5177
diff changeset
    50
                choice[found] = (aliases, table[e])
4549
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
    51
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
    52
    if not choice and debugchoice:
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
    53
        choice = debugchoice
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
    54
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
    55
    return choice
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
    56
7213
b4c035057d34 findcmd: have dispatch look up strict flag
Matt Mackall <mpm@selenic.com>
parents: 7121
diff changeset
    57
def findcmd(cmd, table, strict=True):
4549
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
    58
    """Return (aliases, command table entry) for command string."""
7213
b4c035057d34 findcmd: have dispatch look up strict flag
Matt Mackall <mpm@selenic.com>
parents: 7121
diff changeset
    59
    choice = findpossible(cmd, table, strict)
4549
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
    60
5915
d0576d065993 Prefer i in d over d.has_key(i)
Christian Ebert <blacktrash@gmx.net>
parents: 5843
diff changeset
    61
    if cmd in choice:
4549
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
    62
        return choice[cmd]
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
    63
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
    64
    if len(choice) > 1:
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
    65
        clist = choice.keys()
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
    66
        clist.sort()
7643
9a1ea6587557 error: move UnknownCommand and AmbiguousCommand
Matt Mackall <mpm@selenic.com>
parents: 7404
diff changeset
    67
        raise error.AmbiguousCommand(cmd, clist)
4549
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
    68
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
    69
    if choice:
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
    70
        return choice.values()[0]
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
    71
7643
9a1ea6587557 error: move UnknownCommand and AmbiguousCommand
Matt Mackall <mpm@selenic.com>
parents: 7404
diff changeset
    72
    raise error.UnknownCommand(cmd)
4549
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
    73
10402
d216fa04e48a mq: make init -Q do what qinit -c did
Brendan Cully <brendan@kublai.com>
parents: 10401
diff changeset
    74
def findrepo(p):
d216fa04e48a mq: make init -Q do what qinit -c did
Brendan Cully <brendan@kublai.com>
parents: 10401
diff changeset
    75
    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
    76
        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
    77
        if p == oldp:
d216fa04e48a mq: make init -Q do what qinit -c did
Brendan Cully <brendan@kublai.com>
parents: 10401
diff changeset
    78
            return None
d216fa04e48a mq: make init -Q do what qinit -c did
Brendan Cully <brendan@kublai.com>
parents: 10401
diff changeset
    79
d216fa04e48a mq: make init -Q do what qinit -c did
Brendan Cully <brendan@kublai.com>
parents: 10401
diff changeset
    80
    return p
d216fa04e48a mq: make init -Q do what qinit -c did
Brendan Cully <brendan@kublai.com>
parents: 10401
diff changeset
    81
14289
d68ddccf276b cmdutil: bail_if_changed to bailifchanged
Matt Mackall <mpm@selenic.com>
parents: 14269
diff changeset
    82
def bailifchanged(repo):
13878
a8d13ee0ce68 misc: replace .parents()[0] with p1()
Matt Mackall <mpm@selenic.com>
parents: 13769
diff changeset
    83
    if repo.dirstate.p2() != nullid:
5716
be367cbafe70 cmdutil: make bail_if_changed bail on uncommitted merge
Matt Mackall <mpm@selenic.com>
parents: 5610
diff changeset
    84
        raise util.Abort(_('outstanding uncommitted merge'))
4549
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
    85
    modified, added, removed, deleted = repo.status()[:4]
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
    86
    if modified or added or removed or deleted:
19804
061ce98c888d cmdutil.bailifchanged: standardize error message for dirty working dir
Siddharth Agarwal <sid0@fb.com>
parents: 19730
diff changeset
    87
        raise util.Abort(_('uncommitted changes'))
15231
cd6f10dccf16 cmdutil.bailifchanged: abort for dirty subrepos
Eric Roshan Eisner <ede@alum.mit.edu>
parents: 15214
diff changeset
    88
    ctx = repo[None]
18364
6252b4f1c4b4 subrepos: process subrepos in sorted order
Mads Kiilerich <mads@kiilerich.com>
parents: 18340
diff changeset
    89
    for s in sorted(ctx.substate):
15231
cd6f10dccf16 cmdutil.bailifchanged: abort for dirty subrepos
Eric Roshan Eisner <ede@alum.mit.edu>
parents: 15214
diff changeset
    90
        if ctx.sub(s).dirty():
cd6f10dccf16 cmdutil.bailifchanged: abort for dirty subrepos
Eric Roshan Eisner <ede@alum.mit.edu>
parents: 15214
diff changeset
    91
            raise util.Abort(_("uncommitted changes in subrepo %s") % s)
4549
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
    92
14635
217b7d83afc3 cmdutil, logmessage: use ui.fin when reading from '-'
Idan Kamara <idankk86@gmail.com>
parents: 14518
diff changeset
    93
def logmessage(ui, opts):
4549
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
    94
    """ 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
    95
    message = opts.get('message')
bd5c37d792e6 cmdutil.logmessage: options should be optional
Alexander Solovyov <piranha@piranha.org.ua>
parents: 7643
diff changeset
    96
    logfile = opts.get('logfile')
4549
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
    97
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
    98
    if message and logfile:
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
    99
        raise util.Abort(_('options --message and --logfile are mutually '
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
   100
                           'exclusive'))
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
   101
    if not message and logfile:
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
   102
        try:
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
   103
            if logfile == '-':
14635
217b7d83afc3 cmdutil, logmessage: use ui.fin when reading from '-'
Idan Kamara <idankk86@gmail.com>
parents: 14518
diff changeset
   104
                message = ui.fin.read()
4549
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
   105
            else:
14249
f4766e1bb0b3 cmdutil: normalize log message eols when reading from file
Patrick Mezard <pmezard@gmail.com>
parents: 14232
diff changeset
   106
                message = '\n'.join(util.readfile(logfile).splitlines())
4549
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
   107
        except IOError, inst:
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
   108
            raise util.Abort(_("can't read commit message '%s': %s") %
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
   109
                             (logfile, inst.strerror))
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
   110
    return message
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
   111
6190
a79d9408806f Move finding/checking the log limit to cmdutil
Thomas Arendsen Hein <thomas@intevation.de>
parents: 6145
diff changeset
   112
def loglimit(opts):
a79d9408806f Move finding/checking the log limit to cmdutil
Thomas Arendsen Hein <thomas@intevation.de>
parents: 6145
diff changeset
   113
    """get the log limit according to option -l/--limit"""
a79d9408806f Move finding/checking the log limit to cmdutil
Thomas Arendsen Hein <thomas@intevation.de>
parents: 6145
diff changeset
   114
    limit = opts.get('limit')
a79d9408806f Move finding/checking the log limit to cmdutil
Thomas Arendsen Hein <thomas@intevation.de>
parents: 6145
diff changeset
   115
    if limit:
a79d9408806f Move finding/checking the log limit to cmdutil
Thomas Arendsen Hein <thomas@intevation.de>
parents: 6145
diff changeset
   116
        try:
a79d9408806f Move finding/checking the log limit to cmdutil
Thomas Arendsen Hein <thomas@intevation.de>
parents: 6145
diff changeset
   117
            limit = int(limit)
a79d9408806f Move finding/checking the log limit to cmdutil
Thomas Arendsen Hein <thomas@intevation.de>
parents: 6145
diff changeset
   118
        except ValueError:
a79d9408806f Move finding/checking the log limit to cmdutil
Thomas Arendsen Hein <thomas@intevation.de>
parents: 6145
diff changeset
   119
            raise util.Abort(_('limit must be a positive integer'))
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10264
diff changeset
   120
        if limit <= 0:
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10264
diff changeset
   121
            raise util.Abort(_('limit must be positive'))
6190
a79d9408806f Move finding/checking the log limit to cmdutil
Thomas Arendsen Hein <thomas@intevation.de>
parents: 6145
diff changeset
   122
    else:
10111
27457d31ae3f cmdutil: replace sys.maxint with None as default value in loglimit
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 10061
diff changeset
   123
        limit = None
6190
a79d9408806f Move finding/checking the log limit to cmdutil
Thomas Arendsen Hein <thomas@intevation.de>
parents: 6145
diff changeset
   124
    return limit
a79d9408806f Move finding/checking the log limit to cmdutil
Thomas Arendsen Hein <thomas@intevation.de>
parents: 6145
diff changeset
   125
14986
70e11de6964d export: add %m to file format string (first line of the commit message)
Andrzej Bieniek <andyhelp@gmail.com>
parents: 14948
diff changeset
   126
def makefilename(repo, pat, node, desc=None,
2874
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   127
                  total=None, seqno=None, revwidth=None, pathname=None):
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   128
    node_expander = {
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   129
        'H': lambda: hex(node),
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   130
        'R': lambda: str(repo.changelog.rev(node)),
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   131
        'h': lambda: short(node),
14986
70e11de6964d export: add %m to file format string (first line of the commit message)
Andrzej Bieniek <andyhelp@gmail.com>
parents: 14948
diff changeset
   132
        'm': lambda: re.sub('[^\w]', '_', str(desc))
2874
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   133
        }
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   134
    expander = {
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   135
        '%': lambda: '%',
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   136
        'b': lambda: os.path.basename(repo.root),
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   137
        }
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   138
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   139
    try:
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   140
        if node:
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   141
            expander.update(node_expander)
4836
0e2d0a78f81a archive: make the %r escape work.
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4825
diff changeset
   142
        if node:
2874
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   143
            expander['r'] = (lambda:
4836
0e2d0a78f81a archive: make the %r escape work.
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4825
diff changeset
   144
                    str(repo.changelog.rev(node)).zfill(revwidth or 0))
2874
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   145
        if total is not None:
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   146
            expander['N'] = lambda: str(total)
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   147
        if seqno is not None:
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   148
            expander['n'] = lambda: str(seqno)
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   149
        if total is not None and seqno is not None:
3673
eb0b4a2d70a9 white space and line break cleanups
Thomas Arendsen Hein <thomas@intevation.de>
parents: 3657
diff changeset
   150
            expander['n'] = lambda: str(seqno).zfill(len(str(total)))
2874
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   151
        if pathname is not None:
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   152
            expander['s'] = lambda: os.path.basename(pathname)
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   153
            expander['d'] = lambda: os.path.dirname(pathname) or '.'
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   154
            expander['p'] = lambda: pathname
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   155
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   156
        newname = []
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   157
        patlen = len(pat)
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   158
        i = 0
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   159
        while i < patlen:
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   160
            c = pat[i]
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   161
            if c == '%':
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   162
                i += 1
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   163
                c = pat[i]
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   164
                c = expander[c]()
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   165
            newname.append(c)
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   166
            i += 1
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   167
        return ''.join(newname)
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   168
    except KeyError, inst:
8761
0289f384e1e5 Generally replace "file name" with "filename" in help and comments.
timeless <timeless@gmail.com>
parents: 8731
diff changeset
   169
        raise util.Abort(_("invalid format spec '%%%s' in output filename") %
3072
bc3fe3b5b785 Never apply string formatting to generated errors with util.Abort.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2958
diff changeset
   170
                         inst.args[0])
2874
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   171
14986
70e11de6964d export: add %m to file format string (first line of the commit message)
Andrzej Bieniek <andyhelp@gmail.com>
parents: 14948
diff changeset
   172
def makefileobj(repo, pat, node=None, desc=None, total=None,
19944
b7f76db06dc0 cmdutil: fix makefileobj not to clobber default modemap dict
Yuya Nishihara <yuya@tcha.org>
parents: 19894
diff changeset
   173
                seqno=None, revwidth=None, mode='wb', modemap=None,
18613
1a2f4c633410 export: clobber files with -o (bc) (issue3652)
Augie Fackler <raf@durin42.com>
parents: 18538
diff changeset
   174
                pathname=None):
7319
eae1767cc6a8 export: fixed silent output file overwriting
Ronny Pfannschmidt <Ronny.Pfannschmidt@gmx.de>
parents: 7308
diff changeset
   175
13769
8796fb6af67e cmdutil: fix mode handling in make_file
Adrian Buehlmann <adrian@cadifra.com>
parents: 13534
diff changeset
   176
    writable = mode not in ('r', 'rb')
7319
eae1767cc6a8 export: fixed silent output file overwriting
Ronny Pfannschmidt <Ronny.Pfannschmidt@gmx.de>
parents: 7308
diff changeset
   177
2874
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   178
    if not pat or pat == '-':
14637
5e9d691229d5 cmdutil: use ui descriptors in makefileobj
Idan Kamara <idankk86@gmail.com>
parents: 14635
diff changeset
   179
        fp = writable and repo.ui.fout or repo.ui.fin
14948
32302480b402 cmdutil: use safehasattr instead of hasattr
Augie Fackler <durin42@gmail.com>
parents: 14671
diff changeset
   180
        if util.safehasattr(fp, 'fileno'):
14638
1bdbca0b6604 cmdutil: return a dummy, closable file object if it cannot be duped
Idan Kamara <idankk86@gmail.com>
parents: 14637
diff changeset
   181
            return os.fdopen(os.dup(fp.fileno()), mode)
1bdbca0b6604 cmdutil: return a dummy, closable file object if it cannot be duped
Idan Kamara <idankk86@gmail.com>
parents: 14637
diff changeset
   182
        else:
1bdbca0b6604 cmdutil: return a dummy, closable file object if it cannot be duped
Idan Kamara <idankk86@gmail.com>
parents: 14637
diff changeset
   183
            # if this fp can't be duped properly, return
1bdbca0b6604 cmdutil: return a dummy, closable file object if it cannot be duped
Idan Kamara <idankk86@gmail.com>
parents: 14637
diff changeset
   184
            # a dummy object that can be closed
1bdbca0b6604 cmdutil: return a dummy, closable file object if it cannot be duped
Idan Kamara <idankk86@gmail.com>
parents: 14637
diff changeset
   185
            class wrappedfileobj(object):
1bdbca0b6604 cmdutil: return a dummy, closable file object if it cannot be duped
Idan Kamara <idankk86@gmail.com>
parents: 14637
diff changeset
   186
                noop = lambda x: None
1bdbca0b6604 cmdutil: return a dummy, closable file object if it cannot be duped
Idan Kamara <idankk86@gmail.com>
parents: 14637
diff changeset
   187
                def __init__(self, f):
1bdbca0b6604 cmdutil: return a dummy, closable file object if it cannot be duped
Idan Kamara <idankk86@gmail.com>
parents: 14637
diff changeset
   188
                    self.f = f
1bdbca0b6604 cmdutil: return a dummy, closable file object if it cannot be duped
Idan Kamara <idankk86@gmail.com>
parents: 14637
diff changeset
   189
                def __getattr__(self, attr):
1bdbca0b6604 cmdutil: return a dummy, closable file object if it cannot be duped
Idan Kamara <idankk86@gmail.com>
parents: 14637
diff changeset
   190
                    if attr == 'close':
1bdbca0b6604 cmdutil: return a dummy, closable file object if it cannot be duped
Idan Kamara <idankk86@gmail.com>
parents: 14637
diff changeset
   191
                        return self.noop
1bdbca0b6604 cmdutil: return a dummy, closable file object if it cannot be duped
Idan Kamara <idankk86@gmail.com>
parents: 14637
diff changeset
   192
                    else:
1bdbca0b6604 cmdutil: return a dummy, closable file object if it cannot be duped
Idan Kamara <idankk86@gmail.com>
parents: 14637
diff changeset
   193
                        return getattr(self.f, attr)
1bdbca0b6604 cmdutil: return a dummy, closable file object if it cannot be duped
Idan Kamara <idankk86@gmail.com>
parents: 14637
diff changeset
   194
1bdbca0b6604 cmdutil: return a dummy, closable file object if it cannot be duped
Idan Kamara <idankk86@gmail.com>
parents: 14637
diff changeset
   195
            return wrappedfileobj(fp)
14948
32302480b402 cmdutil: use safehasattr instead of hasattr
Augie Fackler <durin42@gmail.com>
parents: 14671
diff changeset
   196
    if util.safehasattr(pat, 'write') and writable:
2874
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   197
        return pat
14948
32302480b402 cmdutil: use safehasattr instead of hasattr
Augie Fackler <durin42@gmail.com>
parents: 14671
diff changeset
   198
    if util.safehasattr(pat, 'read') and 'r' in mode:
2874
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   199
        return pat
18613
1a2f4c633410 export: clobber files with -o (bc) (issue3652)
Augie Fackler <raf@durin42.com>
parents: 18538
diff changeset
   200
    fn = makefilename(repo, pat, node, desc, total, seqno, revwidth, pathname)
19944
b7f76db06dc0 cmdutil: fix makefileobj not to clobber default modemap dict
Yuya Nishihara <yuya@tcha.org>
parents: 19894
diff changeset
   201
    if modemap is not None:
b7f76db06dc0 cmdutil: fix makefileobj not to clobber default modemap dict
Yuya Nishihara <yuya@tcha.org>
parents: 19894
diff changeset
   202
        mode = modemap.get(fn, mode)
b7f76db06dc0 cmdutil: fix makefileobj not to clobber default modemap dict
Yuya Nishihara <yuya@tcha.org>
parents: 19894
diff changeset
   203
        if mode == 'wb':
b7f76db06dc0 cmdutil: fix makefileobj not to clobber default modemap dict
Yuya Nishihara <yuya@tcha.org>
parents: 19894
diff changeset
   204
            modemap[fn] = 'ab'
18613
1a2f4c633410 export: clobber files with -o (bc) (issue3652)
Augie Fackler <raf@durin42.com>
parents: 18538
diff changeset
   205
    return open(fn, mode)
2882
cf98cd70d2c4 move walk and matchpats from commands to cmdutil.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2874
diff changeset
   206
14323
a79fea6b3e77 debugindex etc.: add --changelog and --manifest options
Sune Foldager <cryo@cyanite.org>
parents: 14322
diff changeset
   207
def openrevlog(repo, cmd, file_, opts):
a79fea6b3e77 debugindex etc.: add --changelog and --manifest options
Sune Foldager <cryo@cyanite.org>
parents: 14322
diff changeset
   208
    """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
   209
    cl = opts['changelog']
a79fea6b3e77 debugindex etc.: add --changelog and --manifest options
Sune Foldager <cryo@cyanite.org>
parents: 14322
diff changeset
   210
    mf = opts['manifest']
a79fea6b3e77 debugindex etc.: add --changelog and --manifest options
Sune Foldager <cryo@cyanite.org>
parents: 14322
diff changeset
   211
    msg = None
a79fea6b3e77 debugindex etc.: add --changelog and --manifest options
Sune Foldager <cryo@cyanite.org>
parents: 14322
diff changeset
   212
    if cl and mf:
a79fea6b3e77 debugindex etc.: add --changelog and --manifest options
Sune Foldager <cryo@cyanite.org>
parents: 14322
diff changeset
   213
        msg = _('cannot specify --changelog and --manifest at the same time')
a79fea6b3e77 debugindex etc.: add --changelog and --manifest options
Sune Foldager <cryo@cyanite.org>
parents: 14322
diff changeset
   214
    elif cl or mf:
a79fea6b3e77 debugindex etc.: add --changelog and --manifest options
Sune Foldager <cryo@cyanite.org>
parents: 14322
diff changeset
   215
        if file_:
a79fea6b3e77 debugindex etc.: add --changelog and --manifest options
Sune Foldager <cryo@cyanite.org>
parents: 14322
diff changeset
   216
            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
   217
        elif not repo:
a79fea6b3e77 debugindex etc.: add --changelog and --manifest options
Sune Foldager <cryo@cyanite.org>
parents: 14322
diff changeset
   218
            msg = _('cannot specify --changelog or --manifest '
a79fea6b3e77 debugindex etc.: add --changelog and --manifest options
Sune Foldager <cryo@cyanite.org>
parents: 14322
diff changeset
   219
                    'without a repository')
a79fea6b3e77 debugindex etc.: add --changelog and --manifest options
Sune Foldager <cryo@cyanite.org>
parents: 14322
diff changeset
   220
    if msg:
a79fea6b3e77 debugindex etc.: add --changelog and --manifest options
Sune Foldager <cryo@cyanite.org>
parents: 14322
diff changeset
   221
        raise util.Abort(msg)
a79fea6b3e77 debugindex etc.: add --changelog and --manifest options
Sune Foldager <cryo@cyanite.org>
parents: 14322
diff changeset
   222
a79fea6b3e77 debugindex etc.: add --changelog and --manifest options
Sune Foldager <cryo@cyanite.org>
parents: 14322
diff changeset
   223
    r = None
a79fea6b3e77 debugindex etc.: add --changelog and --manifest options
Sune Foldager <cryo@cyanite.org>
parents: 14322
diff changeset
   224
    if repo:
a79fea6b3e77 debugindex etc.: add --changelog and --manifest options
Sune Foldager <cryo@cyanite.org>
parents: 14322
diff changeset
   225
        if cl:
21033
254f55b64e31 debugrevlog: use unfiltered view for changelog
Matt Mackall <mpm@selenic.com>
parents: 21024
diff changeset
   226
            r = repo.unfiltered().changelog
14323
a79fea6b3e77 debugindex etc.: add --changelog and --manifest options
Sune Foldager <cryo@cyanite.org>
parents: 14322
diff changeset
   227
        elif mf:
a79fea6b3e77 debugindex etc.: add --changelog and --manifest options
Sune Foldager <cryo@cyanite.org>
parents: 14322
diff changeset
   228
            r = repo.manifest
a79fea6b3e77 debugindex etc.: add --changelog and --manifest options
Sune Foldager <cryo@cyanite.org>
parents: 14322
diff changeset
   229
        elif file_:
a79fea6b3e77 debugindex etc.: add --changelog and --manifest options
Sune Foldager <cryo@cyanite.org>
parents: 14322
diff changeset
   230
            filelog = repo.file(file_)
a79fea6b3e77 debugindex etc.: add --changelog and --manifest options
Sune Foldager <cryo@cyanite.org>
parents: 14322
diff changeset
   231
            if len(filelog):
a79fea6b3e77 debugindex etc.: add --changelog and --manifest options
Sune Foldager <cryo@cyanite.org>
parents: 14322
diff changeset
   232
                r = filelog
a79fea6b3e77 debugindex etc.: add --changelog and --manifest options
Sune Foldager <cryo@cyanite.org>
parents: 14322
diff changeset
   233
    if not r:
a79fea6b3e77 debugindex etc.: add --changelog and --manifest options
Sune Foldager <cryo@cyanite.org>
parents: 14322
diff changeset
   234
        if not file_:
a79fea6b3e77 debugindex etc.: add --changelog and --manifest options
Sune Foldager <cryo@cyanite.org>
parents: 14322
diff changeset
   235
            raise error.CommandError(cmd, _('invalid arguments'))
a79fea6b3e77 debugindex etc.: add --changelog and --manifest options
Sune Foldager <cryo@cyanite.org>
parents: 14322
diff changeset
   236
        if not os.path.isfile(file_):
a79fea6b3e77 debugindex etc.: add --changelog and --manifest options
Sune Foldager <cryo@cyanite.org>
parents: 14322
diff changeset
   237
            raise util.Abort(_("revlog '%s' not found") % file_)
a79fea6b3e77 debugindex etc.: add --changelog and --manifest options
Sune Foldager <cryo@cyanite.org>
parents: 14322
diff changeset
   238
        r = revlog.revlog(scmutil.opener(os.getcwd(), audit=False),
a79fea6b3e77 debugindex etc.: add --changelog and --manifest options
Sune Foldager <cryo@cyanite.org>
parents: 14322
diff changeset
   239
                          file_[:-2] + ".i")
a79fea6b3e77 debugindex etc.: add --changelog and --manifest options
Sune Foldager <cryo@cyanite.org>
parents: 14322
diff changeset
   240
    return r
a79fea6b3e77 debugindex etc.: add --changelog and --manifest options
Sune Foldager <cryo@cyanite.org>
parents: 14322
diff changeset
   241
5610
2493a478f395 copy: handle rename internally
Matt Mackall <mpm@selenic.com>
parents: 5609
diff changeset
   242
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
   243
    # called with the repo lock held
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   244
    #
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   245
    # hgsep => pathname that uses "/" to separate directories
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   246
    # 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
   247
    cwd = repo.getcwd()
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   248
    targets = {}
5607
e9bae5c80ab4 copy: minor cleanups
Matt Mackall <mpm@selenic.com>
parents: 5606
diff changeset
   249
    after = opts.get("after")
e9bae5c80ab4 copy: minor cleanups
Matt Mackall <mpm@selenic.com>
parents: 5606
diff changeset
   250
    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
   251
    wctx = repo[None]
5589
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   252
5605
e7a9ad999308 copy: refactor okaytocopy into walkpat
Matt Mackall <mpm@selenic.com>
parents: 5604
diff changeset
   253
    def walkpat(pat):
e7a9ad999308 copy: refactor okaytocopy into walkpat
Matt Mackall <mpm@selenic.com>
parents: 5604
diff changeset
   254
        srcs = []
11223
0d09f2244805 rename: make --after work if source is already in R state
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 11177
diff changeset
   255
        badstates = after and '?' or '?r'
14671
35c2cc322ba8 scmutil: switch match users to supplying contexts
Matt Mackall <mpm@selenic.com>
parents: 14638
diff changeset
   256
        m = scmutil.match(repo[None], [pat], opts, globbed=True)
6586
d3463007d368 walk: return a single value
Matt Mackall <mpm@selenic.com>
parents: 6585
diff changeset
   257
        for abs in repo.walk(m):
5605
e7a9ad999308 copy: refactor okaytocopy into walkpat
Matt Mackall <mpm@selenic.com>
parents: 5604
diff changeset
   258
            state = repo.dirstate[abs]
6584
29c77e5dfb3c walk: remove rel and exact returns
Matt Mackall <mpm@selenic.com>
parents: 6582
diff changeset
   259
            rel = m.rel(abs)
29c77e5dfb3c walk: remove rel and exact returns
Matt Mackall <mpm@selenic.com>
parents: 6582
diff changeset
   260
            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
   261
            if state in badstates:
5605
e7a9ad999308 copy: refactor okaytocopy into walkpat
Matt Mackall <mpm@selenic.com>
parents: 5604
diff changeset
   262
                if exact and state == '?':
e7a9ad999308 copy: refactor okaytocopy into walkpat
Matt Mackall <mpm@selenic.com>
parents: 5604
diff changeset
   263
                    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
   264
                if exact and state == 'r':
e7a9ad999308 copy: refactor okaytocopy into walkpat
Matt Mackall <mpm@selenic.com>
parents: 5604
diff changeset
   265
                    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
   266
                              ' remove\n') % rel)
e7a9ad999308 copy: refactor okaytocopy into walkpat
Matt Mackall <mpm@selenic.com>
parents: 5604
diff changeset
   267
                continue
e7a9ad999308 copy: refactor okaytocopy into walkpat
Matt Mackall <mpm@selenic.com>
parents: 5604
diff changeset
   268
            # abs: hgsep
e7a9ad999308 copy: refactor okaytocopy into walkpat
Matt Mackall <mpm@selenic.com>
parents: 5604
diff changeset
   269
            # rel: ossep
e7a9ad999308 copy: refactor okaytocopy into walkpat
Matt Mackall <mpm@selenic.com>
parents: 5604
diff changeset
   270
            srcs.append((abs, rel, exact))
e7a9ad999308 copy: refactor okaytocopy into walkpat
Matt Mackall <mpm@selenic.com>
parents: 5604
diff changeset
   271
        return srcs
5589
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   272
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   273
    # abssrc: hgsep
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   274
    # relsrc: ossep
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   275
    # otarget: ossep
5605
e7a9ad999308 copy: refactor okaytocopy into walkpat
Matt Mackall <mpm@selenic.com>
parents: 5604
diff changeset
   276
    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
   277
        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
   278
        if '/' in abstarget:
e596a631210e dirstate: preserve path components case on renames (issue3402)
Patrick Mezard <patrick@mezard.eu>
parents: 16458
diff changeset
   279
            # 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
   280
            # case only renames, like a => A.
e596a631210e dirstate: preserve path components case on renames (issue3402)
Patrick Mezard <patrick@mezard.eu>
parents: 16458
diff changeset
   281
            abspath, absname = abstarget.rsplit('/', 1)
e596a631210e dirstate: preserve path components case on renames (issue3402)
Patrick Mezard <patrick@mezard.eu>
parents: 16458
diff changeset
   282
            abstarget = repo.dirstate.normalize(abspath) + '/' + absname
5589
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   283
        reltarget = repo.pathto(abstarget, cwd)
5607
e9bae5c80ab4 copy: minor cleanups
Matt Mackall <mpm@selenic.com>
parents: 5606
diff changeset
   284
        target = repo.wjoin(abstarget)
5589
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   285
        src = repo.wjoin(abssrc)
5608
784eadabd985 copy: simplify inner copy
Matt Mackall <mpm@selenic.com>
parents: 5607
diff changeset
   286
        state = repo.dirstate[abstarget]
5607
e9bae5c80ab4 copy: minor cleanups
Matt Mackall <mpm@selenic.com>
parents: 5606
diff changeset
   287
13962
8b252e826c68 add: introduce a warning message for non-portable filenames (issue2756) (BC)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13945
diff changeset
   288
        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
   289
5607
e9bae5c80ab4 copy: minor cleanups
Matt Mackall <mpm@selenic.com>
parents: 5606
diff changeset
   290
        # check for collisions
e9bae5c80ab4 copy: minor cleanups
Matt Mackall <mpm@selenic.com>
parents: 5606
diff changeset
   291
        prevsrc = targets.get(abstarget)
5589
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   292
        if prevsrc is not None:
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   293
            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
   294
                    (reltarget, repo.pathto(abssrc, cwd),
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   295
                     repo.pathto(prevsrc, cwd)))
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   296
            return
5607
e9bae5c80ab4 copy: minor cleanups
Matt Mackall <mpm@selenic.com>
parents: 5606
diff changeset
   297
e9bae5c80ab4 copy: minor cleanups
Matt Mackall <mpm@selenic.com>
parents: 5606
diff changeset
   298
        # check for overwrites
12342
70236d6fd844 rename: do not overwrite existing broken symlinks
Patrick Mezard <pmezard@gmail.com>
parents: 11950
diff changeset
   299
        exists = os.path.lexists(target)
16283
6c4dbe28dda3 rename: handle case-changing (issue1717)
Matt Mackall <mpm@selenic.com>
parents: 16165
diff changeset
   300
        samefile = False
6c4dbe28dda3 rename: handle case-changing (issue1717)
Matt Mackall <mpm@selenic.com>
parents: 16165
diff changeset
   301
        if exists and abssrc != abstarget:
6c4dbe28dda3 rename: handle case-changing (issue1717)
Matt Mackall <mpm@selenic.com>
parents: 16165
diff changeset
   302
            if (repo.dirstate.normalize(abssrc) ==
6c4dbe28dda3 rename: handle case-changing (issue1717)
Matt Mackall <mpm@selenic.com>
parents: 16165
diff changeset
   303
                repo.dirstate.normalize(abstarget)):
6c4dbe28dda3 rename: handle case-changing (issue1717)
Matt Mackall <mpm@selenic.com>
parents: 16165
diff changeset
   304
                if not rename:
6c4dbe28dda3 rename: handle case-changing (issue1717)
Matt Mackall <mpm@selenic.com>
parents: 16165
diff changeset
   305
                    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
   306
                    return
6c4dbe28dda3 rename: handle case-changing (issue1717)
Matt Mackall <mpm@selenic.com>
parents: 16165
diff changeset
   307
                exists = False
6c4dbe28dda3 rename: handle case-changing (issue1717)
Matt Mackall <mpm@selenic.com>
parents: 16165
diff changeset
   308
                samefile = True
6c4dbe28dda3 rename: handle case-changing (issue1717)
Matt Mackall <mpm@selenic.com>
parents: 16165
diff changeset
   309
8117
2b30d8488819 remove unnecessary outer parenthesis in if-statements
Martin Geisler <mg@lazybytes.net>
parents: 8013
diff changeset
   310
        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
   311
            if not opts['force']:
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   312
                ui.warn(_('%s: not overwriting - file exists\n') %
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   313
                        reltarget)
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   314
                return
5607
e9bae5c80ab4 copy: minor cleanups
Matt Mackall <mpm@selenic.com>
parents: 5606
diff changeset
   315
e9bae5c80ab4 copy: minor cleanups
Matt Mackall <mpm@selenic.com>
parents: 5606
diff changeset
   316
        if after:
5608
784eadabd985 copy: simplify inner copy
Matt Mackall <mpm@selenic.com>
parents: 5607
diff changeset
   317
            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
   318
                if rename:
e8d10d085f47 cmdutil: Warn when trying to copy/rename --after to a nonexistant file.
Steve Losh <steve@stevelosh.com>
parents: 11061
diff changeset
   319
                    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
   320
                            (relsrc, reltarget))
e8d10d085f47 cmdutil: Warn when trying to copy/rename --after to a nonexistant file.
Steve Losh <steve@stevelosh.com>
parents: 11061
diff changeset
   321
                else:
e8d10d085f47 cmdutil: Warn when trying to copy/rename --after to a nonexistant file.
Steve Losh <steve@stevelosh.com>
parents: 11061
diff changeset
   322
                    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
   323
                            (relsrc, reltarget))
5589
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   324
                return
5608
784eadabd985 copy: simplify inner copy
Matt Mackall <mpm@selenic.com>
parents: 5607
diff changeset
   325
        elif not dryrun:
5589
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   326
            try:
5608
784eadabd985 copy: simplify inner copy
Matt Mackall <mpm@selenic.com>
parents: 5607
diff changeset
   327
                if exists:
784eadabd985 copy: simplify inner copy
Matt Mackall <mpm@selenic.com>
parents: 5607
diff changeset
   328
                    os.unlink(target)
784eadabd985 copy: simplify inner copy
Matt Mackall <mpm@selenic.com>
parents: 5607
diff changeset
   329
                targetdir = os.path.dirname(target) or '.'
784eadabd985 copy: simplify inner copy
Matt Mackall <mpm@selenic.com>
parents: 5607
diff changeset
   330
                if not os.path.isdir(targetdir):
784eadabd985 copy: simplify inner copy
Matt Mackall <mpm@selenic.com>
parents: 5607
diff changeset
   331
                    os.makedirs(targetdir)
16283
6c4dbe28dda3 rename: handle case-changing (issue1717)
Matt Mackall <mpm@selenic.com>
parents: 16165
diff changeset
   332
                if samefile:
6c4dbe28dda3 rename: handle case-changing (issue1717)
Matt Mackall <mpm@selenic.com>
parents: 16165
diff changeset
   333
                    tmp = target + "~hgrename"
6c4dbe28dda3 rename: handle case-changing (issue1717)
Matt Mackall <mpm@selenic.com>
parents: 16165
diff changeset
   334
                    os.rename(src, tmp)
6c4dbe28dda3 rename: handle case-changing (issue1717)
Matt Mackall <mpm@selenic.com>
parents: 16165
diff changeset
   335
                    os.rename(tmp, target)
6c4dbe28dda3 rename: handle case-changing (issue1717)
Matt Mackall <mpm@selenic.com>
parents: 16165
diff changeset
   336
                else:
6c4dbe28dda3 rename: handle case-changing (issue1717)
Matt Mackall <mpm@selenic.com>
parents: 16165
diff changeset
   337
                    util.copyfile(src, target)
14518
a67e866f46f9 workingctx: eliminate remove function
Adrian Buehlmann <adrian@cadifra.com>
parents: 14442
diff changeset
   338
                srcexists = True
5589
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   339
            except IOError, inst:
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   340
                if inst.errno == errno.ENOENT:
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   341
                    ui.warn(_('%s: deleted in working copy\n') % relsrc)
14518
a67e866f46f9 workingctx: eliminate remove function
Adrian Buehlmann <adrian@cadifra.com>
parents: 14442
diff changeset
   342
                    srcexists = False
5589
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   343
                else:
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   344
                    ui.warn(_('%s: cannot copy - %s\n') %
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   345
                            (relsrc, inst.strerror))
5606
447ea621e50e copy: propagate errors properly
Matt Mackall <mpm@selenic.com>
parents: 5605
diff changeset
   346
                    return True # report a failure
5607
e9bae5c80ab4 copy: minor cleanups
Matt Mackall <mpm@selenic.com>
parents: 5606
diff changeset
   347
5589
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   348
        if ui.verbose or not exact:
7894
caef5fdf1375 cmdutil: fix untranslatable string in copy
Martin Geisler <mg@daimi.au.dk>
parents: 7879
diff changeset
   349
            if rename:
caef5fdf1375 cmdutil: fix untranslatable string in copy
Martin Geisler <mg@daimi.au.dk>
parents: 7879
diff changeset
   350
                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
   351
            else:
caef5fdf1375 cmdutil: fix untranslatable string in copy
Martin Geisler <mg@daimi.au.dk>
parents: 7879
diff changeset
   352
                ui.status(_('copying %s to %s\n') % (relsrc, reltarget))
5608
784eadabd985 copy: simplify inner copy
Matt Mackall <mpm@selenic.com>
parents: 5607
diff changeset
   353
5589
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   354
        targets[abstarget] = abssrc
5607
e9bae5c80ab4 copy: minor cleanups
Matt Mackall <mpm@selenic.com>
parents: 5606
diff changeset
   355
e9bae5c80ab4 copy: minor cleanups
Matt Mackall <mpm@selenic.com>
parents: 5606
diff changeset
   356
        # fix up dirstate
14321
003d63bb4fa5 scmutil: drop some aliases in cmdutil
Matt Mackall <mpm@selenic.com>
parents: 14320
diff changeset
   357
        scmutil.dirstatecopy(ui, repo, wctx, abssrc, abstarget,
003d63bb4fa5 scmutil: drop some aliases in cmdutil
Matt Mackall <mpm@selenic.com>
parents: 14320
diff changeset
   358
                             dryrun=dryrun, cwd=cwd)
5610
2493a478f395 copy: handle rename internally
Matt Mackall <mpm@selenic.com>
parents: 5609
diff changeset
   359
        if rename and not dryrun:
16283
6c4dbe28dda3 rename: handle case-changing (issue1717)
Matt Mackall <mpm@selenic.com>
parents: 16165
diff changeset
   360
            if not after and srcexists and not samefile:
14518
a67e866f46f9 workingctx: eliminate remove function
Adrian Buehlmann <adrian@cadifra.com>
parents: 14442
diff changeset
   361
                util.unlinkpath(repo.wjoin(abssrc))
a67e866f46f9 workingctx: eliminate remove function
Adrian Buehlmann <adrian@cadifra.com>
parents: 14442
diff changeset
   362
            wctx.forget([abssrc])
5589
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   363
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   364
    # pat: ossep
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   365
    # dest ossep
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   366
    # srcs: list of (hgsep, hgsep, ossep, bool)
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   367
    # return: function that takes hgsep and returns ossep
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   368
    def targetpathfn(pat, dest, srcs):
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   369
        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
   370
            abspfx = pathutil.canonpath(repo.root, cwd, pat)
5589
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   371
            abspfx = util.localpath(abspfx)
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   372
            if destdirexists:
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   373
                striplen = len(os.path.split(abspfx)[0])
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   374
            else:
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   375
                striplen = len(abspfx)
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   376
            if striplen:
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   377
                striplen += len(os.sep)
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   378
            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
   379
        elif destdirexists:
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   380
            res = lambda p: os.path.join(dest,
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   381
                                         os.path.basename(util.localpath(p)))
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   382
        else:
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   383
            res = lambda p: dest
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   384
        return res
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   385
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   386
    # pat: ossep
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   387
    # dest ossep
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   388
    # srcs: list of (hgsep, hgsep, ossep, bool)
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   389
    # return: function that takes hgsep and returns ossep
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   390
    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
   391
        if matchmod.patkind(pat):
5589
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   392
            # a mercurial pattern
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   393
            res = lambda p: os.path.join(dest,
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   394
                                         os.path.basename(util.localpath(p)))
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   395
        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
   396
            abspfx = pathutil.canonpath(repo.root, cwd, pat)
5589
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   397
            if len(abspfx) < len(srcs[0][0]):
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   398
                # 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
   399
                # 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
   400
                def evalpath(striplen):
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   401
                    score = 0
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   402
                    for s in srcs:
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   403
                        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
   404
                        if os.path.lexists(t):
5589
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   405
                            score += 1
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   406
                    return score
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   407
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   408
                abspfx = util.localpath(abspfx)
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   409
                striplen = len(abspfx)
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   410
                if striplen:
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   411
                    striplen += len(os.sep)
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   412
                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
   413
                    score = evalpath(striplen)
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   414
                    striplen1 = len(os.path.split(abspfx)[0])
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   415
                    if striplen1:
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   416
                        striplen1 += len(os.sep)
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   417
                    if evalpath(striplen1) > score:
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   418
                        striplen = striplen1
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   419
                res = lambda p: os.path.join(dest,
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   420
                                             util.localpath(p)[striplen:])
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   421
            else:
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   422
                # a file
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   423
                if destdirexists:
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   424
                    res = lambda p: os.path.join(dest,
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   425
                                        os.path.basename(util.localpath(p)))
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   426
                else:
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   427
                    res = lambda p: dest
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   428
        return res
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   429
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   430
14321
003d63bb4fa5 scmutil: drop some aliases in cmdutil
Matt Mackall <mpm@selenic.com>
parents: 14320
diff changeset
   431
    pats = scmutil.expandpats(pats)
5589
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   432
    if not pats:
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   433
        raise util.Abort(_('no source or destination specified'))
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   434
    if len(pats) == 1:
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   435
        raise util.Abort(_('no destination specified'))
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   436
    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
   437
    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
   438
    if not destdirexists:
12085
6f833fc3ccab Consistently import foo as foomod when foo to avoid shadowing
Martin Geisler <mg@aragost.com>
parents: 12032
diff changeset
   439
        if len(pats) > 1 or matchmod.patkind(pats[0]):
5589
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   440
            raise util.Abort(_('with multiple sources, destination must be an '
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   441
                               '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
   442
        if util.endswithsep(dest):
5589
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   443
            raise util.Abort(_('destination %s is not a directory') % dest)
5607
e9bae5c80ab4 copy: minor cleanups
Matt Mackall <mpm@selenic.com>
parents: 5606
diff changeset
   444
e9bae5c80ab4 copy: minor cleanups
Matt Mackall <mpm@selenic.com>
parents: 5606
diff changeset
   445
    tfn = targetpathfn
e9bae5c80ab4 copy: minor cleanups
Matt Mackall <mpm@selenic.com>
parents: 5606
diff changeset
   446
    if after:
5589
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   447
        tfn = targetpathafterfn
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   448
    copylist = []
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   449
    for pat in pats:
5605
e7a9ad999308 copy: refactor okaytocopy into walkpat
Matt Mackall <mpm@selenic.com>
parents: 5604
diff changeset
   450
        srcs = walkpat(pat)
5589
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   451
        if not srcs:
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   452
            continue
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   453
        copylist.append((tfn(pat, dest, srcs), srcs))
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   454
    if not copylist:
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   455
        raise util.Abort(_('no files to copy'))
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   456
5606
447ea621e50e copy: propagate errors properly
Matt Mackall <mpm@selenic.com>
parents: 5605
diff changeset
   457
    errors = 0
5589
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   458
    for targetpath, srcs in copylist:
5605
e7a9ad999308 copy: refactor okaytocopy into walkpat
Matt Mackall <mpm@selenic.com>
parents: 5604
diff changeset
   459
        for abssrc, relsrc, exact in srcs:
5606
447ea621e50e copy: propagate errors properly
Matt Mackall <mpm@selenic.com>
parents: 5605
diff changeset
   460
            if copyfile(abssrc, relsrc, targetpath(abssrc), exact):
447ea621e50e copy: propagate errors properly
Matt Mackall <mpm@selenic.com>
parents: 5605
diff changeset
   461
                errors += 1
5589
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   462
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   463
    if errors:
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   464
        ui.warn(_('(consider using --after)\n'))
5609
a783d3627144 copy: move rename logic
Matt Mackall <mpm@selenic.com>
parents: 5608
diff changeset
   465
11177
6a64813276ed commands: initial audit of exit codes
Matt Mackall <mpm@selenic.com>
parents: 11152
diff changeset
   466
    return errors != 0
5589
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   467
9513
ae88c721f916 cmdutil: service: add an optional runargs argument to pass the command to run
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9367
diff changeset
   468
def service(opts, parentfn=None, initfn=None, runfn=None, logfile=None,
10012
2bfe1a23dafa cmdutil: service: add appendpid parameter to append pids to pid file
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9975
diff changeset
   469
    runargs=None, appendpid=False):
4380
e89f9afc462b Refactor commands.serve to allow other commands to run as services.
Bryan O'Sullivan <bos@serpentine.com>
parents: 4355
diff changeset
   470
    '''Run a command as a service.'''
e89f9afc462b Refactor commands.serve to allow other commands to run as services.
Bryan O'Sullivan <bos@serpentine.com>
parents: 4355
diff changeset
   471
19867
edce20ebe1f3 cmdutil.service: move pidfile writing to a local function
Siddharth Agarwal <sid0@fb.com>
parents: 19804
diff changeset
   472
    def writepid(pid):
edce20ebe1f3 cmdutil.service: move pidfile writing to a local function
Siddharth Agarwal <sid0@fb.com>
parents: 19804
diff changeset
   473
        if opts['pid_file']:
edce20ebe1f3 cmdutil.service: move pidfile writing to a local function
Siddharth Agarwal <sid0@fb.com>
parents: 19804
diff changeset
   474
            mode = appendpid and 'a' or 'w'
edce20ebe1f3 cmdutil.service: move pidfile writing to a local function
Siddharth Agarwal <sid0@fb.com>
parents: 19804
diff changeset
   475
            fp = open(opts['pid_file'], mode)
edce20ebe1f3 cmdutil.service: move pidfile writing to a local function
Siddharth Agarwal <sid0@fb.com>
parents: 19804
diff changeset
   476
            fp.write(str(pid) + '\n')
edce20ebe1f3 cmdutil.service: move pidfile writing to a local function
Siddharth Agarwal <sid0@fb.com>
parents: 19804
diff changeset
   477
            fp.close()
edce20ebe1f3 cmdutil.service: move pidfile writing to a local function
Siddharth Agarwal <sid0@fb.com>
parents: 19804
diff changeset
   478
4380
e89f9afc462b Refactor commands.serve to allow other commands to run as services.
Bryan O'Sullivan <bos@serpentine.com>
parents: 4355
diff changeset
   479
    if opts['daemon'] and not opts['daemon_pipefds']:
10238
e22695b4472f cmdutil: replace unix pipe handshake with file lock
Patrick Mezard <pmezard@gmail.com>
parents: 10237
diff changeset
   480
        # Signal child process startup with file removal
e22695b4472f cmdutil: replace unix pipe handshake with file lock
Patrick Mezard <pmezard@gmail.com>
parents: 10237
diff changeset
   481
        lockfd, lockpath = tempfile.mkstemp(prefix='hg-service-')
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10264
diff changeset
   482
        os.close(lockfd)
10238
e22695b4472f cmdutil: replace unix pipe handshake with file lock
Patrick Mezard <pmezard@gmail.com>
parents: 10237
diff changeset
   483
        try:
e22695b4472f cmdutil: replace unix pipe handshake with file lock
Patrick Mezard <pmezard@gmail.com>
parents: 10237
diff changeset
   484
            if not runargs:
10239
8e4be44a676f Find right hg command for detached process
Patrick Mezard <pmezard@gmail.com>
parents: 10238
diff changeset
   485
                runargs = util.hgcmd() + sys.argv[1:]
10238
e22695b4472f cmdutil: replace unix pipe handshake with file lock
Patrick Mezard <pmezard@gmail.com>
parents: 10237
diff changeset
   486
            runargs.append('--daemon-pipefds=%s' % lockpath)
e22695b4472f cmdutil: replace unix pipe handshake with file lock
Patrick Mezard <pmezard@gmail.com>
parents: 10237
diff changeset
   487
            # Don't pass --cwd to the child process, because we've already
e22695b4472f cmdutil: replace unix pipe handshake with file lock
Patrick Mezard <pmezard@gmail.com>
parents: 10237
diff changeset
   488
            # changed directory.
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10264
diff changeset
   489
            for i in xrange(1, len(runargs)):
10238
e22695b4472f cmdutil: replace unix pipe handshake with file lock
Patrick Mezard <pmezard@gmail.com>
parents: 10237
diff changeset
   490
                if runargs[i].startswith('--cwd='):
e22695b4472f cmdutil: replace unix pipe handshake with file lock
Patrick Mezard <pmezard@gmail.com>
parents: 10237
diff changeset
   491
                    del runargs[i]
e22695b4472f cmdutil: replace unix pipe handshake with file lock
Patrick Mezard <pmezard@gmail.com>
parents: 10237
diff changeset
   492
                    break
e22695b4472f cmdutil: replace unix pipe handshake with file lock
Patrick Mezard <pmezard@gmail.com>
parents: 10237
diff changeset
   493
                elif runargs[i].startswith('--cwd'):
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10264
diff changeset
   494
                    del runargs[i:i + 2]
10238
e22695b4472f cmdutil: replace unix pipe handshake with file lock
Patrick Mezard <pmezard@gmail.com>
parents: 10237
diff changeset
   495
                    break
10344
9501cde4c034 util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents: 10282
diff changeset
   496
            def condfn():
9501cde4c034 util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents: 10282
diff changeset
   497
                return not os.path.exists(lockpath)
9501cde4c034 util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents: 10282
diff changeset
   498
            pid = util.rundetached(runargs, condfn)
9501cde4c034 util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents: 10282
diff changeset
   499
            if pid < 0:
9501cde4c034 util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents: 10282
diff changeset
   500
                raise util.Abort(_('child process failed to start'))
19868
0532c8f8e911 cmdutil.service: move pidfile writing to the parent in daemon mode
Siddharth Agarwal <sid0@fb.com>
parents: 19867
diff changeset
   501
            writepid(pid)
10238
e22695b4472f cmdutil: replace unix pipe handshake with file lock
Patrick Mezard <pmezard@gmail.com>
parents: 10237
diff changeset
   502
        finally:
e22695b4472f cmdutil: replace unix pipe handshake with file lock
Patrick Mezard <pmezard@gmail.com>
parents: 10237
diff changeset
   503
            try:
e22695b4472f cmdutil: replace unix pipe handshake with file lock
Patrick Mezard <pmezard@gmail.com>
parents: 10237
diff changeset
   504
                os.unlink(lockpath)
e22695b4472f cmdutil: replace unix pipe handshake with file lock
Patrick Mezard <pmezard@gmail.com>
parents: 10237
diff changeset
   505
            except OSError, e:
e22695b4472f cmdutil: replace unix pipe handshake with file lock
Patrick Mezard <pmezard@gmail.com>
parents: 10237
diff changeset
   506
                if e.errno != errno.ENOENT:
e22695b4472f cmdutil: replace unix pipe handshake with file lock
Patrick Mezard <pmezard@gmail.com>
parents: 10237
diff changeset
   507
                    raise
4380
e89f9afc462b Refactor commands.serve to allow other commands to run as services.
Bryan O'Sullivan <bos@serpentine.com>
parents: 4355
diff changeset
   508
        if parentfn:
e89f9afc462b Refactor commands.serve to allow other commands to run as services.
Bryan O'Sullivan <bos@serpentine.com>
parents: 4355
diff changeset
   509
            return parentfn(pid)
e89f9afc462b Refactor commands.serve to allow other commands to run as services.
Bryan O'Sullivan <bos@serpentine.com>
parents: 4355
diff changeset
   510
        else:
9896
2c2f7593ffc4 cmdutil.service: do not _exit(0) in the parent process
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9668
diff changeset
   511
            return
4380
e89f9afc462b Refactor commands.serve to allow other commands to run as services.
Bryan O'Sullivan <bos@serpentine.com>
parents: 4355
diff changeset
   512
e89f9afc462b Refactor commands.serve to allow other commands to run as services.
Bryan O'Sullivan <bos@serpentine.com>
parents: 4355
diff changeset
   513
    if initfn:
e89f9afc462b Refactor commands.serve to allow other commands to run as services.
Bryan O'Sullivan <bos@serpentine.com>
parents: 4355
diff changeset
   514
        initfn()
e89f9afc462b Refactor commands.serve to allow other commands to run as services.
Bryan O'Sullivan <bos@serpentine.com>
parents: 4355
diff changeset
   515
19868
0532c8f8e911 cmdutil.service: move pidfile writing to the parent in daemon mode
Siddharth Agarwal <sid0@fb.com>
parents: 19867
diff changeset
   516
    if not opts['daemon']:
0532c8f8e911 cmdutil.service: move pidfile writing to the parent in daemon mode
Siddharth Agarwal <sid0@fb.com>
parents: 19867
diff changeset
   517
        writepid(os.getpid())
4380
e89f9afc462b Refactor commands.serve to allow other commands to run as services.
Bryan O'Sullivan <bos@serpentine.com>
parents: 4355
diff changeset
   518
e89f9afc462b Refactor commands.serve to allow other commands to run as services.
Bryan O'Sullivan <bos@serpentine.com>
parents: 4355
diff changeset
   519
    if opts['daemon_pipefds']:
10238
e22695b4472f cmdutil: replace unix pipe handshake with file lock
Patrick Mezard <pmezard@gmail.com>
parents: 10237
diff changeset
   520
        lockpath = opts['daemon_pipefds']
4380
e89f9afc462b Refactor commands.serve to allow other commands to run as services.
Bryan O'Sullivan <bos@serpentine.com>
parents: 4355
diff changeset
   521
        try:
e89f9afc462b Refactor commands.serve to allow other commands to run as services.
Bryan O'Sullivan <bos@serpentine.com>
parents: 4355
diff changeset
   522
            os.setsid()
e89f9afc462b Refactor commands.serve to allow other commands to run as services.
Bryan O'Sullivan <bos@serpentine.com>
parents: 4355
diff changeset
   523
        except AttributeError:
e89f9afc462b Refactor commands.serve to allow other commands to run as services.
Bryan O'Sullivan <bos@serpentine.com>
parents: 4355
diff changeset
   524
            pass
10238
e22695b4472f cmdutil: replace unix pipe handshake with file lock
Patrick Mezard <pmezard@gmail.com>
parents: 10237
diff changeset
   525
        os.unlink(lockpath)
10240
3af4b39afe2a cmdutil: hide child window created by win32 spawndetached()
Patrick Mezard <pmezard@gmail.com>
parents: 10239
diff changeset
   526
        util.hidewindow()
4380
e89f9afc462b Refactor commands.serve to allow other commands to run as services.
Bryan O'Sullivan <bos@serpentine.com>
parents: 4355
diff changeset
   527
        sys.stdout.flush()
e89f9afc462b Refactor commands.serve to allow other commands to run as services.
Bryan O'Sullivan <bos@serpentine.com>
parents: 4355
diff changeset
   528
        sys.stderr.flush()
8789
e0ed17984a48 cmdutil: service: logfile option to redirect stdout & stderr in a file
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8778
diff changeset
   529
17391
fc24c10424d2 util: replace util.nulldev with os.devnull
Ross Lagerwall <rosslagerwall@gmail.com>
parents: 17308
diff changeset
   530
        nullfd = os.open(os.devnull, os.O_RDWR)
8789
e0ed17984a48 cmdutil: service: logfile option to redirect stdout & stderr in a file
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8778
diff changeset
   531
        logfilefd = nullfd
e0ed17984a48 cmdutil: service: logfile option to redirect stdout & stderr in a file
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8778
diff changeset
   532
        if logfile:
e0ed17984a48 cmdutil: service: logfile option to redirect stdout & stderr in a file
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8778
diff changeset
   533
            logfilefd = os.open(logfile, os.O_RDWR | os.O_CREAT | os.O_APPEND)
e0ed17984a48 cmdutil: service: logfile option to redirect stdout & stderr in a file
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8778
diff changeset
   534
        os.dup2(nullfd, 0)
e0ed17984a48 cmdutil: service: logfile option to redirect stdout & stderr in a file
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8778
diff changeset
   535
        os.dup2(logfilefd, 1)
e0ed17984a48 cmdutil: service: logfile option to redirect stdout & stderr in a file
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8778
diff changeset
   536
        os.dup2(logfilefd, 2)
e0ed17984a48 cmdutil: service: logfile option to redirect stdout & stderr in a file
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8778
diff changeset
   537
        if nullfd not in (0, 1, 2):
e0ed17984a48 cmdutil: service: logfile option to redirect stdout & stderr in a file
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8778
diff changeset
   538
            os.close(nullfd)
e0ed17984a48 cmdutil: service: logfile option to redirect stdout & stderr in a file
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8778
diff changeset
   539
        if logfile and logfilefd not in (0, 1, 2):
e0ed17984a48 cmdutil: service: logfile option to redirect stdout & stderr in a file
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8778
diff changeset
   540
            os.close(logfilefd)
4380
e89f9afc462b Refactor commands.serve to allow other commands to run as services.
Bryan O'Sullivan <bos@serpentine.com>
parents: 4355
diff changeset
   541
e89f9afc462b Refactor commands.serve to allow other commands to run as services.
Bryan O'Sullivan <bos@serpentine.com>
parents: 4355
diff changeset
   542
    if runfn:
e89f9afc462b Refactor commands.serve to allow other commands to run as services.
Bryan O'Sullivan <bos@serpentine.com>
parents: 4355
diff changeset
   543
        return runfn()
e89f9afc462b Refactor commands.serve to allow other commands to run as services.
Bryan O'Sullivan <bos@serpentine.com>
parents: 4355
diff changeset
   544
20500
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   545
def tryimportone(ui, repo, hunk, parents, opts, msgs, updatefunc):
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   546
    """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
   547
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   548
    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
   549
    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
   550
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   551
    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
   552
    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
   553
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   554
    :hunk: a patch (as a binary string)
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   555
    :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
   556
    :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
   557
    :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
   558
           (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
   559
    :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
   560
                 updatefunc(<repo>, <node>)
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   561
    """
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   562
    tmpname, message, user, date, branch, nodeid, p1, p2 = \
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   563
        patch.extract(ui, hunk)
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   564
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   565
    editor = commiteditor
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   566
    if opts.get('edit'):
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   567
        editor = commitforceeditor
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   568
    update = not opts.get('bypass')
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   569
    strip = opts["strip"]
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   570
    sim = float(opts.get('similarity') or 0)
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   571
    if not tmpname:
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   572
        return (None, None)
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   573
    msg = _('applied to working directory')
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   574
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   575
    try:
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   576
        cmdline_message = logmessage(ui, opts)
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   577
        if cmdline_message:
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   578
            # pickup the cmdline msg
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   579
            message = cmdline_message
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   580
        elif message:
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   581
            # pickup the patch msg
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   582
            message = message.strip()
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   583
        else:
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   584
            # launch the editor
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   585
            message = None
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   586
        ui.debug('message:\n%s\n' % message)
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   587
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   588
        if len(parents) == 1:
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   589
            parents.append(repo[nullid])
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   590
        if opts.get('exact'):
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   591
            if not nodeid or not p1:
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   592
                raise util.Abort(_('not a Mercurial patch'))
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   593
            p1 = repo[p1]
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   594
            p2 = repo[p2 or nullid]
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   595
        elif p2:
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   596
            try:
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   597
                p1 = repo[p1]
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   598
                p2 = repo[p2]
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   599
                # Without any options, consider p2 only if the
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   600
                # patch is being applied on top of the recorded
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   601
                # first parent.
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   602
                if p1 != parents[0]:
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   603
                    p1 = parents[0]
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   604
                    p2 = repo[nullid]
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   605
            except error.RepoError:
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   606
                p1, p2 = parents
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   607
        else:
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   608
            p1, p2 = parents
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   609
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   610
        n = None
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   611
        if update:
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   612
            if p1 != parents[0]:
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   613
                updatefunc(repo, p1.node())
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   614
            if p2 != parents[1]:
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   615
                repo.setparents(p1.node(), p2.node())
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   616
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   617
            if opts.get('exact') or opts.get('import_branch'):
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   618
                repo.dirstate.setbranch(branch or 'default')
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   619
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   620
            files = set()
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   621
            patch.patch(ui, repo, tmpname, strip=strip, files=files,
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   622
                        eolmode=None, similarity=sim / 100.0)
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   623
            files = list(files)
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   624
            if opts.get('no_commit'):
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   625
                if message:
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   626
                    msgs.append(message)
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   627
            else:
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   628
                if opts.get('exact') or p2:
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   629
                    # If you got here, you either use --force and know what
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   630
                    # you are doing or used --exact or a merge patch while
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   631
                    # being updated to its first parent.
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   632
                    m = None
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   633
                else:
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   634
                    m = scmutil.matchfiles(repo, files or [])
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   635
                n = repo.commit(message, opts.get('user') or user,
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   636
                                opts.get('date') or date, match=m,
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   637
                                editor=editor)
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   638
        else:
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   639
            if opts.get('exact') or opts.get('import_branch'):
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   640
                branch = branch or 'default'
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   641
            else:
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   642
                branch = p1.branch()
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   643
            store = patch.filestore()
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   644
            try:
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   645
                files = set()
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   646
                try:
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   647
                    patch.patchrepo(ui, repo, p1, store, tmpname, strip,
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   648
                                    files, eolmode=None)
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   649
                except patch.PatchError, e:
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   650
                    raise util.Abort(str(e))
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   651
                memctx = context.makememctx(repo, (p1.node(), p2.node()),
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   652
                                            message,
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   653
                                            opts.get('user') or user,
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   654
                                            opts.get('date') or date,
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   655
                                            branch, files, store,
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   656
                                            editor=commiteditor)
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   657
                n = memctx.commit()
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   658
            finally:
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   659
                store.close()
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   660
        if opts.get('exact') and hex(n) != nodeid:
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   661
            raise util.Abort(_('patch is damaged or loses information'))
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   662
        if n:
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   663
            # i18n: refers to a short changeset id
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   664
            msg = _('created %s') % short(n)
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   665
        return (msg, n)
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   666
    finally:
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   667
        os.unlink(tmpname)
ce3f3082ec45 import: move tryone closure in cmdutil
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 20470
diff changeset
   668
10611
e764f24a45ee patch/diff: move patch.export() to cmdutil.export()
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10608
diff changeset
   669
def export(repo, revs, template='hg-%h.patch', fp=None, switch_parent=False,
e764f24a45ee patch/diff: move patch.export() to cmdutil.export()
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10608
diff changeset
   670
           opts=None):
e764f24a45ee patch/diff: move patch.export() to cmdutil.export()
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10608
diff changeset
   671
    '''export changesets as hg patches.'''
e764f24a45ee patch/diff: move patch.export() to cmdutil.export()
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10608
diff changeset
   672
e764f24a45ee patch/diff: move patch.export() to cmdutil.export()
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10608
diff changeset
   673
    total = len(revs)
e764f24a45ee patch/diff: move patch.export() to cmdutil.export()
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10608
diff changeset
   674
    revwidth = max([len(str(rev)) for rev in revs])
18613
1a2f4c633410 export: clobber files with -o (bc) (issue3652)
Augie Fackler <raf@durin42.com>
parents: 18538
diff changeset
   675
    filemode = {}
10611
e764f24a45ee patch/diff: move patch.export() to cmdutil.export()
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10608
diff changeset
   676
e764f24a45ee patch/diff: move patch.export() to cmdutil.export()
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10608
diff changeset
   677
    def single(rev, seqno, fp):
e764f24a45ee patch/diff: move patch.export() to cmdutil.export()
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10608
diff changeset
   678
        ctx = repo[rev]
e764f24a45ee patch/diff: move patch.export() to cmdutil.export()
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10608
diff changeset
   679
        node = ctx.node()
e764f24a45ee patch/diff: move patch.export() to cmdutil.export()
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10608
diff changeset
   680
        parents = [p.node() for p in ctx.parents() if p]
e764f24a45ee patch/diff: move patch.export() to cmdutil.export()
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10608
diff changeset
   681
        branch = ctx.branch()
e764f24a45ee patch/diff: move patch.export() to cmdutil.export()
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10608
diff changeset
   682
        if switch_parent:
e764f24a45ee patch/diff: move patch.export() to cmdutil.export()
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10608
diff changeset
   683
            parents.reverse()
e764f24a45ee patch/diff: move patch.export() to cmdutil.export()
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10608
diff changeset
   684
        prev = (parents and parents[0]) or nullid
e764f24a45ee patch/diff: move patch.export() to cmdutil.export()
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10608
diff changeset
   685
13400
14f3795a5ed7 explicitly close files
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 13386
diff changeset
   686
        shouldclose = False
17460
a306837f8c87 color: enabled color support for export command (issue1507)
Ankur Dahiya <ankurd@fb.com>
parents: 17424
diff changeset
   687
        if not fp and len(template) > 0:
14986
70e11de6964d export: add %m to file format string (first line of the commit message)
Andrzej Bieniek <andyhelp@gmail.com>
parents: 14948
diff changeset
   688
            desc_lines = ctx.description().rstrip().split('\n')
70e11de6964d export: add %m to file format string (first line of the commit message)
Andrzej Bieniek <andyhelp@gmail.com>
parents: 14948
diff changeset
   689
            desc = desc_lines[0]    #Commit always has a first line.
70e11de6964d export: add %m to file format string (first line of the commit message)
Andrzej Bieniek <andyhelp@gmail.com>
parents: 14948
diff changeset
   690
            fp = makefileobj(repo, template, node, desc=desc, total=total,
18613
1a2f4c633410 export: clobber files with -o (bc) (issue3652)
Augie Fackler <raf@durin42.com>
parents: 18538
diff changeset
   691
                             seqno=seqno, revwidth=revwidth, mode='wb',
1a2f4c633410 export: clobber files with -o (bc) (issue3652)
Augie Fackler <raf@durin42.com>
parents: 18538
diff changeset
   692
                             modemap=filemode)
13467
31aa2e5b0750 export: only close files which export itself has opened
Waqas Hussain <waqas20@gmail.com>
parents: 13400
diff changeset
   693
            if fp != template:
31aa2e5b0750 export: only close files which export itself has opened
Waqas Hussain <waqas20@gmail.com>
parents: 13400
diff changeset
   694
                shouldclose = True
17460
a306837f8c87 color: enabled color support for export command (issue1507)
Ankur Dahiya <ankurd@fb.com>
parents: 17424
diff changeset
   695
        if fp and fp != sys.stdout and util.safehasattr(fp, 'name'):
10611
e764f24a45ee patch/diff: move patch.export() to cmdutil.export()
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10608
diff changeset
   696
            repo.ui.note("%s\n" % fp.name)
e764f24a45ee patch/diff: move patch.export() to cmdutil.export()
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10608
diff changeset
   697
17460
a306837f8c87 color: enabled color support for export command (issue1507)
Ankur Dahiya <ankurd@fb.com>
parents: 17424
diff changeset
   698
        if not fp:
a306837f8c87 color: enabled color support for export command (issue1507)
Ankur Dahiya <ankurd@fb.com>
parents: 17424
diff changeset
   699
            write = repo.ui.write
a306837f8c87 color: enabled color support for export command (issue1507)
Ankur Dahiya <ankurd@fb.com>
parents: 17424
diff changeset
   700
        else:
a306837f8c87 color: enabled color support for export command (issue1507)
Ankur Dahiya <ankurd@fb.com>
parents: 17424
diff changeset
   701
            def write(s, **kw):
a306837f8c87 color: enabled color support for export command (issue1507)
Ankur Dahiya <ankurd@fb.com>
parents: 17424
diff changeset
   702
                fp.write(s)
a306837f8c87 color: enabled color support for export command (issue1507)
Ankur Dahiya <ankurd@fb.com>
parents: 17424
diff changeset
   703
a306837f8c87 color: enabled color support for export command (issue1507)
Ankur Dahiya <ankurd@fb.com>
parents: 17424
diff changeset
   704
a306837f8c87 color: enabled color support for export command (issue1507)
Ankur Dahiya <ankurd@fb.com>
parents: 17424
diff changeset
   705
        write("# HG changeset patch\n")
a306837f8c87 color: enabled color support for export command (issue1507)
Ankur Dahiya <ankurd@fb.com>
parents: 17424
diff changeset
   706
        write("# User %s\n" % ctx.user())
a306837f8c87 color: enabled color support for export command (issue1507)
Ankur Dahiya <ankurd@fb.com>
parents: 17424
diff changeset
   707
        write("# Date %d %d\n" % ctx.date())
18648
76b69cccb07a export: show 'Date' header in a format that also is readable for humans
Mads Kiilerich <mads@kiilerich.com>
parents: 18613
diff changeset
   708
        write("#      %s\n" % util.datestr(ctx.date()))
11821
15aa42aaae4c cmdutil: remove unnecessary parenthesis
Martin Geisler <mg@aragost.com>
parents: 11635
diff changeset
   709
        if branch and branch != 'default':
17460
a306837f8c87 color: enabled color support for export command (issue1507)
Ankur Dahiya <ankurd@fb.com>
parents: 17424
diff changeset
   710
            write("# Branch %s\n" % branch)
a306837f8c87 color: enabled color support for export command (issue1507)
Ankur Dahiya <ankurd@fb.com>
parents: 17424
diff changeset
   711
        write("# Node ID %s\n" % hex(node))
a306837f8c87 color: enabled color support for export command (issue1507)
Ankur Dahiya <ankurd@fb.com>
parents: 17424
diff changeset
   712
        write("# Parent  %s\n" % hex(prev))
10611
e764f24a45ee patch/diff: move patch.export() to cmdutil.export()
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10608
diff changeset
   713
        if len(parents) > 1:
17460
a306837f8c87 color: enabled color support for export command (issue1507)
Ankur Dahiya <ankurd@fb.com>
parents: 17424
diff changeset
   714
            write("# Parent  %s\n" % hex(parents[1]))
a306837f8c87 color: enabled color support for export command (issue1507)
Ankur Dahiya <ankurd@fb.com>
parents: 17424
diff changeset
   715
        write(ctx.description().rstrip())
a306837f8c87 color: enabled color support for export command (issue1507)
Ankur Dahiya <ankurd@fb.com>
parents: 17424
diff changeset
   716
        write("\n\n")
10611
e764f24a45ee patch/diff: move patch.export() to cmdutil.export()
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10608
diff changeset
   717
17460
a306837f8c87 color: enabled color support for export command (issue1507)
Ankur Dahiya <ankurd@fb.com>
parents: 17424
diff changeset
   718
        for chunk, label in patch.diffui(repo, prev, node, opts=opts):
a306837f8c87 color: enabled color support for export command (issue1507)
Ankur Dahiya <ankurd@fb.com>
parents: 17424
diff changeset
   719
            write(chunk, label=label)
10611
e764f24a45ee patch/diff: move patch.export() to cmdutil.export()
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10608
diff changeset
   720
13400
14f3795a5ed7 explicitly close files
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 13386
diff changeset
   721
        if shouldclose:
14f3795a5ed7 explicitly close files
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 13386
diff changeset
   722
            fp.close()
13081
79184986658c export: flush the file pointer between patches
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 13047
diff changeset
   723
10611
e764f24a45ee patch/diff: move patch.export() to cmdutil.export()
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10608
diff changeset
   724
    for seqno, rev in enumerate(revs):
e764f24a45ee patch/diff: move patch.export() to cmdutil.export()
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10608
diff changeset
   725
        single(rev, seqno + 1, fp)
e764f24a45ee patch/diff: move patch.export() to cmdutil.export()
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10608
diff changeset
   726
11050
5d35f7d93514 commands: refactor diff --stat and qdiff --stat
Yuya Nishihara <yuya@tcha.org>
parents: 11017
diff changeset
   727
def diffordiffstat(ui, repo, diffopts, node1, node2, match,
12167
d2c5b0927c28 diff: recurse into subrepositories with --subrepos/-S flag
Martin Geisler <mg@lazybytes.net>
parents: 12164
diff changeset
   728
                   changes=None, stat=False, fp=None, prefix='',
d2c5b0927c28 diff: recurse into subrepositories with --subrepos/-S flag
Martin Geisler <mg@lazybytes.net>
parents: 12164
diff changeset
   729
                   listsubrepos=False):
11050
5d35f7d93514 commands: refactor diff --stat and qdiff --stat
Yuya Nishihara <yuya@tcha.org>
parents: 11017
diff changeset
   730
    '''show diff or diffstat.'''
5d35f7d93514 commands: refactor diff --stat and qdiff --stat
Yuya Nishihara <yuya@tcha.org>
parents: 11017
diff changeset
   731
    if fp is None:
5d35f7d93514 commands: refactor diff --stat and qdiff --stat
Yuya Nishihara <yuya@tcha.org>
parents: 11017
diff changeset
   732
        write = ui.write
5d35f7d93514 commands: refactor diff --stat and qdiff --stat
Yuya Nishihara <yuya@tcha.org>
parents: 11017
diff changeset
   733
    else:
5d35f7d93514 commands: refactor diff --stat and qdiff --stat
Yuya Nishihara <yuya@tcha.org>
parents: 11017
diff changeset
   734
        def write(s, **kw):
5d35f7d93514 commands: refactor diff --stat and qdiff --stat
Yuya Nishihara <yuya@tcha.org>
parents: 11017
diff changeset
   735
            fp.write(s)
5d35f7d93514 commands: refactor diff --stat and qdiff --stat
Yuya Nishihara <yuya@tcha.org>
parents: 11017
diff changeset
   736
5d35f7d93514 commands: refactor diff --stat and qdiff --stat
Yuya Nishihara <yuya@tcha.org>
parents: 11017
diff changeset
   737
    if stat:
11950
d157e040ac4c log: fix the bug 'hg log --stat -p == hg log --stat'
Alecs King <alecsk@gmail.com>
parents: 11488
diff changeset
   738
        diffopts = diffopts.copy(context=0)
11050
5d35f7d93514 commands: refactor diff --stat and qdiff --stat
Yuya Nishihara <yuya@tcha.org>
parents: 11017
diff changeset
   739
        width = 80
5d35f7d93514 commands: refactor diff --stat and qdiff --stat
Yuya Nishihara <yuya@tcha.org>
parents: 11017
diff changeset
   740
        if not ui.plain():
12689
c52c629ce19e termwidth: move to ui.ui from util
Augie Fackler <durin42@gmail.com>
parents: 12619
diff changeset
   741
            width = ui.termwidth()
12167
d2c5b0927c28 diff: recurse into subrepositories with --subrepos/-S flag
Martin Geisler <mg@lazybytes.net>
parents: 12164
diff changeset
   742
        chunks = patch.diff(repo, node1, node2, match, changes, diffopts,
d2c5b0927c28 diff: recurse into subrepositories with --subrepos/-S flag
Martin Geisler <mg@lazybytes.net>
parents: 12164
diff changeset
   743
                            prefix=prefix)
11050
5d35f7d93514 commands: refactor diff --stat and qdiff --stat
Yuya Nishihara <yuya@tcha.org>
parents: 11017
diff changeset
   744
        for chunk, label in patch.diffstatui(util.iterlines(chunks),
5d35f7d93514 commands: refactor diff --stat and qdiff --stat
Yuya Nishihara <yuya@tcha.org>
parents: 11017
diff changeset
   745
                                             width=width,
5d35f7d93514 commands: refactor diff --stat and qdiff --stat
Yuya Nishihara <yuya@tcha.org>
parents: 11017
diff changeset
   746
                                             git=diffopts.git):
5d35f7d93514 commands: refactor diff --stat and qdiff --stat
Yuya Nishihara <yuya@tcha.org>
parents: 11017
diff changeset
   747
            write(chunk, label=label)
5d35f7d93514 commands: refactor diff --stat and qdiff --stat
Yuya Nishihara <yuya@tcha.org>
parents: 11017
diff changeset
   748
    else:
5d35f7d93514 commands: refactor diff --stat and qdiff --stat
Yuya Nishihara <yuya@tcha.org>
parents: 11017
diff changeset
   749
        for chunk, label in patch.diffui(repo, node1, node2, match,
12167
d2c5b0927c28 diff: recurse into subrepositories with --subrepos/-S flag
Martin Geisler <mg@lazybytes.net>
parents: 12164
diff changeset
   750
                                         changes, diffopts, prefix=prefix):
11050
5d35f7d93514 commands: refactor diff --stat and qdiff --stat
Yuya Nishihara <yuya@tcha.org>
parents: 11017
diff changeset
   751
            write(chunk, label=label)
5d35f7d93514 commands: refactor diff --stat and qdiff --stat
Yuya Nishihara <yuya@tcha.org>
parents: 11017
diff changeset
   752
12167
d2c5b0927c28 diff: recurse into subrepositories with --subrepos/-S flag
Martin Geisler <mg@lazybytes.net>
parents: 12164
diff changeset
   753
    if listsubrepos:
d2c5b0927c28 diff: recurse into subrepositories with --subrepos/-S flag
Martin Geisler <mg@lazybytes.net>
parents: 12164
diff changeset
   754
        ctx1 = repo[node1]
12175
c0a8f9dea0f6 subrepos: handle modified but uncommitted .hgsub
Martin Geisler <mg@lazybytes.net>
parents: 12167
diff changeset
   755
        ctx2 = repo[node2]
20392
d4f804caa0ed itersubrepos: move to scmutil to break a direct import cycle
Augie Fackler <raf@durin42.com>
parents: 20364
diff changeset
   756
        for subpath, sub in scmutil.itersubrepos(ctx1, ctx2):
15698
43e068c15619 diff: when diffing a revision with a deleted subrepo, maintain the node context (issue3153)
Alistair Bell <alistair.bell@netronome.com>
parents: 15634
diff changeset
   757
            tempnode2 = node2
15634
cfc15cbecc5e diff: don't crash when diffing a revision with a deleted subrepo (issue3153)
Renato Cunha <renato@renatocunha.com>
parents: 15600
diff changeset
   758
            try:
cfc15cbecc5e diff: don't crash when diffing a revision with a deleted subrepo (issue3153)
Renato Cunha <renato@renatocunha.com>
parents: 15600
diff changeset
   759
                if node2 is not None:
15698
43e068c15619 diff: when diffing a revision with a deleted subrepo, maintain the node context (issue3153)
Alistair Bell <alistair.bell@netronome.com>
parents: 15634
diff changeset
   760
                    tempnode2 = ctx2.substate[subpath][1]
15634
cfc15cbecc5e diff: don't crash when diffing a revision with a deleted subrepo (issue3153)
Renato Cunha <renato@renatocunha.com>
parents: 15600
diff changeset
   761
            except KeyError:
cfc15cbecc5e diff: don't crash when diffing a revision with a deleted subrepo (issue3153)
Renato Cunha <renato@renatocunha.com>
parents: 15600
diff changeset
   762
                # A subrepo that existed in node1 was deleted between node1 and
cfc15cbecc5e diff: don't crash when diffing a revision with a deleted subrepo (issue3153)
Renato Cunha <renato@renatocunha.com>
parents: 15600
diff changeset
   763
                # node2 (inclusive). Thus, ctx2's substate won't contain that
cfc15cbecc5e diff: don't crash when diffing a revision with a deleted subrepo (issue3153)
Renato Cunha <renato@renatocunha.com>
parents: 15600
diff changeset
   764
                # subpath. The best we can do is to ignore it.
15698
43e068c15619 diff: when diffing a revision with a deleted subrepo, maintain the node context (issue3153)
Alistair Bell <alistair.bell@netronome.com>
parents: 15634
diff changeset
   765
                tempnode2 = None
12167
d2c5b0927c28 diff: recurse into subrepositories with --subrepos/-S flag
Martin Geisler <mg@lazybytes.net>
parents: 12164
diff changeset
   766
            submatch = matchmod.narrowmatcher(subpath, match)
18006
0c10cf819146 subrepo: add argument to "diff()" to pass "ui" of caller side (issue3712) (API)
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 17924
diff changeset
   767
            sub.diff(ui, diffopts, tempnode2, submatch, changes=changes,
12167
d2c5b0927c28 diff: recurse into subrepositories with --subrepos/-S flag
Martin Geisler <mg@lazybytes.net>
parents: 12164
diff changeset
   768
                     stat=stat, fp=fp, prefix=prefix)
d2c5b0927c28 diff: recurse into subrepositories with --subrepos/-S flag
Martin Geisler <mg@lazybytes.net>
parents: 12164
diff changeset
   769
3643
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   770
class changeset_printer(object):
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   771
    '''show changeset information when templating not requested.'''
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   772
7762
fece056bf240 add --git option to commands supporting --patch (log, incoming, history, tip)
Jim Correia <jim.correia@pobox.com>
parents: 7667
diff changeset
   773
    def __init__(self, ui, repo, patch, diffopts, buffered):
3643
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   774
        self.ui = ui
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   775
        self.repo = repo
3645
b984dcb1df71 Refactor log ui buffering and patch display
Matt Mackall <mpm@selenic.com>
parents: 3643
diff changeset
   776
        self.buffered = buffered
b984dcb1df71 Refactor log ui buffering and patch display
Matt Mackall <mpm@selenic.com>
parents: 3643
diff changeset
   777
        self.patch = patch
7762
fece056bf240 add --git option to commands supporting --patch (log, incoming, history, tip)
Jim Correia <jim.correia@pobox.com>
parents: 7667
diff changeset
   778
        self.diffopts = diffopts
3738
cb48cd27d3f4 use ui buffering in changeset printer
Matt Mackall <mpm@selenic.com>
parents: 3718
diff changeset
   779
        self.header = {}
cb48cd27d3f4 use ui buffering in changeset printer
Matt Mackall <mpm@selenic.com>
parents: 3718
diff changeset
   780
        self.hunk = {}
cb48cd27d3f4 use ui buffering in changeset printer
Matt Mackall <mpm@selenic.com>
parents: 3718
diff changeset
   781
        self.lastheader = None
10152
56284451a22c Added support for templatevar "footer" to cmdutil.py
Robert Bachmann <rbachm@gmail.com>
parents: 10111
diff changeset
   782
        self.footer = None
3645
b984dcb1df71 Refactor log ui buffering and patch display
Matt Mackall <mpm@selenic.com>
parents: 3643
diff changeset
   783
b984dcb1df71 Refactor log ui buffering and patch display
Matt Mackall <mpm@selenic.com>
parents: 3643
diff changeset
   784
    def flush(self, rev):
3738
cb48cd27d3f4 use ui buffering in changeset printer
Matt Mackall <mpm@selenic.com>
parents: 3718
diff changeset
   785
        if rev in self.header:
cb48cd27d3f4 use ui buffering in changeset printer
Matt Mackall <mpm@selenic.com>
parents: 3718
diff changeset
   786
            h = self.header[rev]
cb48cd27d3f4 use ui buffering in changeset printer
Matt Mackall <mpm@selenic.com>
parents: 3718
diff changeset
   787
            if h != self.lastheader:
cb48cd27d3f4 use ui buffering in changeset printer
Matt Mackall <mpm@selenic.com>
parents: 3718
diff changeset
   788
                self.lastheader = h
cb48cd27d3f4 use ui buffering in changeset printer
Matt Mackall <mpm@selenic.com>
parents: 3718
diff changeset
   789
                self.ui.write(h)
cb48cd27d3f4 use ui buffering in changeset printer
Matt Mackall <mpm@selenic.com>
parents: 3718
diff changeset
   790
            del self.header[rev]
cb48cd27d3f4 use ui buffering in changeset printer
Matt Mackall <mpm@selenic.com>
parents: 3718
diff changeset
   791
        if rev in self.hunk:
cb48cd27d3f4 use ui buffering in changeset printer
Matt Mackall <mpm@selenic.com>
parents: 3718
diff changeset
   792
            self.ui.write(self.hunk[rev])
cb48cd27d3f4 use ui buffering in changeset printer
Matt Mackall <mpm@selenic.com>
parents: 3718
diff changeset
   793
            del self.hunk[rev]
cb48cd27d3f4 use ui buffering in changeset printer
Matt Mackall <mpm@selenic.com>
parents: 3718
diff changeset
   794
            return 1
cb48cd27d3f4 use ui buffering in changeset printer
Matt Mackall <mpm@selenic.com>
parents: 3718
diff changeset
   795
        return 0
3643
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   796
10152
56284451a22c Added support for templatevar "footer" to cmdutil.py
Robert Bachmann <rbachm@gmail.com>
parents: 10111
diff changeset
   797
    def close(self):
56284451a22c Added support for templatevar "footer" to cmdutil.py
Robert Bachmann <rbachm@gmail.com>
parents: 10111
diff changeset
   798
        if self.footer:
56284451a22c Added support for templatevar "footer" to cmdutil.py
Robert Bachmann <rbachm@gmail.com>
parents: 10111
diff changeset
   799
            self.ui.write(self.footer)
56284451a22c Added support for templatevar "footer" to cmdutil.py
Robert Bachmann <rbachm@gmail.com>
parents: 10111
diff changeset
   800
11488
f786fc4b8764 log: follow filenames through renames (issue647)
Mads Kiilerich <mads@kiilerich.com>
parents: 11465
diff changeset
   801
    def show(self, ctx, copies=None, matchfn=None, **props):
3738
cb48cd27d3f4 use ui buffering in changeset printer
Matt Mackall <mpm@selenic.com>
parents: 3718
diff changeset
   802
        if self.buffered:
cb48cd27d3f4 use ui buffering in changeset printer
Matt Mackall <mpm@selenic.com>
parents: 3718
diff changeset
   803
            self.ui.pushbuffer()
11488
f786fc4b8764 log: follow filenames through renames (issue647)
Mads Kiilerich <mads@kiilerich.com>
parents: 11465
diff changeset
   804
            self._show(ctx, copies, matchfn, props)
10819
36c6a667d733 cmdutil: make use of output labeling in changeset_printer
Brodie Rao <brodie@bitheap.org>
parents: 10724
diff changeset
   805
            self.hunk[ctx.rev()] = self.ui.popbuffer(labeled=True)
3738
cb48cd27d3f4 use ui buffering in changeset printer
Matt Mackall <mpm@selenic.com>
parents: 3718
diff changeset
   806
        else:
11488
f786fc4b8764 log: follow filenames through renames (issue647)
Mads Kiilerich <mads@kiilerich.com>
parents: 11465
diff changeset
   807
            self._show(ctx, copies, matchfn, props)
3738
cb48cd27d3f4 use ui buffering in changeset printer
Matt Mackall <mpm@selenic.com>
parents: 3718
diff changeset
   808
11488
f786fc4b8764 log: follow filenames through renames (issue647)
Mads Kiilerich <mads@kiilerich.com>
parents: 11465
diff changeset
   809
    def _show(self, ctx, copies, matchfn, props):
3643
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   810
        '''show a single changeset or file revision'''
7369
87158be081b8 cmdutil: use change contexts for cset-printer and cset-templater
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7361
diff changeset
   811
        changenode = ctx.node()
87158be081b8 cmdutil: use change contexts for cset-printer and cset-templater
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7361
diff changeset
   812
        rev = ctx.rev()
3643
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   813
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   814
        if self.ui.quiet:
10819
36c6a667d733 cmdutil: make use of output labeling in changeset_printer
Brodie Rao <brodie@bitheap.org>
parents: 10724
diff changeset
   815
            self.ui.write("%d:%s\n" % (rev, short(changenode)),
36c6a667d733 cmdutil: make use of output labeling in changeset_printer
Brodie Rao <brodie@bitheap.org>
parents: 10724
diff changeset
   816
                          label='log.node')
3643
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   817
            return
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   818
7369
87158be081b8 cmdutil: use change contexts for cset-printer and cset-templater
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7361
diff changeset
   819
        log = self.repo.changelog
9547
f57640bf10d4 cmdutil: changeset_printer: use methods of filectx/changectx.
Greg Ward <greg-hg@gerg.ca>
parents: 9536
diff changeset
   820
        date = util.datestr(ctx.date())
3643
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   821
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   822
        hexfunc = self.ui.debugflag and hex or short
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   823
4825
3cf94964c56b hg log: Move filtering implicit parents to own method and use it in templater.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 4824
diff changeset
   824
        parents = [(p, hexfunc(log.node(p)))
3cf94964c56b hg log: Move filtering implicit parents to own method and use it in templater.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 4824
diff changeset
   825
                   for p in self._meaningful_parentrevs(log, rev)]
3643
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   826
17891
8f85151ce201 i18n: add "i18n" comment to column positioning messages of "hg log"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 17863
diff changeset
   827
        # i18n: column positioning for "hg log"
10819
36c6a667d733 cmdutil: make use of output labeling in changeset_printer
Brodie Rao <brodie@bitheap.org>
parents: 10724
diff changeset
   828
        self.ui.write(_("changeset:   %d:%s\n") % (rev, hexfunc(changenode)),
17788
9912baaae7df color: add additional changeset.phase label to log.changeset and log.parent
Sean Farley <sean.michael.farley@gmail.com>
parents: 17746
diff changeset
   829
                      label='log.changeset changeset.%s' % ctx.phasestr())
3643
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   830
9637
64425c5a9257 cmdutil: minor refactoring of changeset_printer._show
Adrian Buehlmann <adrian@cadifra.com>
parents: 9547
diff changeset
   831
        branch = ctx.branch()
4176
f9bbcebcacea "default" is the default branch name
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4055
diff changeset
   832
        # don't show the default branch name
f9bbcebcacea "default" is the default branch name
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4055
diff changeset
   833
        if branch != 'default':
17891
8f85151ce201 i18n: add "i18n" comment to column positioning messages of "hg log"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 17863
diff changeset
   834
            # i18n: column positioning for "hg log"
10819
36c6a667d733 cmdutil: make use of output labeling in changeset_printer
Brodie Rao <brodie@bitheap.org>
parents: 10724
diff changeset
   835
            self.ui.write(_("branch:      %s\n") % branch,
36c6a667d733 cmdutil: make use of output labeling in changeset_printer
Brodie Rao <brodie@bitheap.org>
parents: 10724
diff changeset
   836
                          label='log.branch')
13386
f78bc5ddbe4f templater: add bookmarks to templates and default output
David Soria Parra <dsp@php.net>
parents: 13121
diff changeset
   837
        for bookmark in self.repo.nodebookmarks(changenode):
17891
8f85151ce201 i18n: add "i18n" comment to column positioning messages of "hg log"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 17863
diff changeset
   838
            # i18n: column positioning for "hg log"
13386
f78bc5ddbe4f templater: add bookmarks to templates and default output
David Soria Parra <dsp@php.net>
parents: 13121
diff changeset
   839
            self.ui.write(_("bookmark:    %s\n") % bookmark,
f78bc5ddbe4f templater: add bookmarks to templates and default output
David Soria Parra <dsp@php.net>
parents: 13121
diff changeset
   840
                    label='log.bookmark')
3643
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   841
        for tag in self.repo.nodetags(changenode):
17891
8f85151ce201 i18n: add "i18n" comment to column positioning messages of "hg log"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 17863
diff changeset
   842
            # i18n: column positioning for "hg log"
10819
36c6a667d733 cmdutil: make use of output labeling in changeset_printer
Brodie Rao <brodie@bitheap.org>
parents: 10724
diff changeset
   843
            self.ui.write(_("tag:         %s\n") % tag,
36c6a667d733 cmdutil: make use of output labeling in changeset_printer
Brodie Rao <brodie@bitheap.org>
parents: 10724
diff changeset
   844
                          label='log.tag')
15907
51fc43253a52 changeset_printer: display changeset phase on debug level
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 15777
diff changeset
   845
        if self.ui.debugflag and ctx.phase():
17891
8f85151ce201 i18n: add "i18n" comment to column positioning messages of "hg log"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 17863
diff changeset
   846
            # i18n: column positioning for "hg log"
15907
51fc43253a52 changeset_printer: display changeset phase on debug level
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 15777
diff changeset
   847
            self.ui.write(_("phase:       %s\n") % _(ctx.phasestr()),
51fc43253a52 changeset_printer: display changeset phase on debug level
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 15777
diff changeset
   848
                          label='log.phase')
3643
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   849
        for parent in parents:
17891
8f85151ce201 i18n: add "i18n" comment to column positioning messages of "hg log"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 17863
diff changeset
   850
            # i18n: column positioning for "hg log"
10819
36c6a667d733 cmdutil: make use of output labeling in changeset_printer
Brodie Rao <brodie@bitheap.org>
parents: 10724
diff changeset
   851
            self.ui.write(_("parent:      %d:%s\n") % parent,
17788
9912baaae7df color: add additional changeset.phase label to log.changeset and log.parent
Sean Farley <sean.michael.farley@gmail.com>
parents: 17746
diff changeset
   852
                          label='log.parent changeset.%s' % ctx.phasestr())
3643
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   853
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   854
        if self.ui.debugflag:
9547
f57640bf10d4 cmdutil: changeset_printer: use methods of filectx/changectx.
Greg Ward <greg-hg@gerg.ca>
parents: 9536
diff changeset
   855
            mnode = ctx.manifestnode()
17891
8f85151ce201 i18n: add "i18n" comment to column positioning messages of "hg log"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 17863
diff changeset
   856
            # i18n: column positioning for "hg log"
3643
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   857
            self.ui.write(_("manifest:    %d:%s\n") %
10819
36c6a667d733 cmdutil: make use of output labeling in changeset_printer
Brodie Rao <brodie@bitheap.org>
parents: 10724
diff changeset
   858
                          (self.repo.manifest.rev(mnode), hex(mnode)),
36c6a667d733 cmdutil: make use of output labeling in changeset_printer
Brodie Rao <brodie@bitheap.org>
parents: 10724
diff changeset
   859
                          label='ui.debug log.manifest')
17891
8f85151ce201 i18n: add "i18n" comment to column positioning messages of "hg log"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 17863
diff changeset
   860
        # i18n: column positioning for "hg log"
10819
36c6a667d733 cmdutil: make use of output labeling in changeset_printer
Brodie Rao <brodie@bitheap.org>
parents: 10724
diff changeset
   861
        self.ui.write(_("user:        %s\n") % ctx.user(),
36c6a667d733 cmdutil: make use of output labeling in changeset_printer
Brodie Rao <brodie@bitheap.org>
parents: 10724
diff changeset
   862
                      label='log.user')
17891
8f85151ce201 i18n: add "i18n" comment to column positioning messages of "hg log"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 17863
diff changeset
   863
        # i18n: column positioning for "hg log"
10819
36c6a667d733 cmdutil: make use of output labeling in changeset_printer
Brodie Rao <brodie@bitheap.org>
parents: 10724
diff changeset
   864
        self.ui.write(_("date:        %s\n") % date,
36c6a667d733 cmdutil: make use of output labeling in changeset_printer
Brodie Rao <brodie@bitheap.org>
parents: 10724
diff changeset
   865
                      label='log.date')
3643
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   866
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   867
        if self.ui.debugflag:
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   868
            files = self.repo.status(log.parents(changenode)[0], changenode)[:3]
17891
8f85151ce201 i18n: add "i18n" comment to column positioning messages of "hg log"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 17863
diff changeset
   869
            for key, value in zip([# i18n: column positioning for "hg log"
8f85151ce201 i18n: add "i18n" comment to column positioning messages of "hg log"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 17863
diff changeset
   870
                                   _("files:"),
8f85151ce201 i18n: add "i18n" comment to column positioning messages of "hg log"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 17863
diff changeset
   871
                                   # i18n: column positioning for "hg log"
8f85151ce201 i18n: add "i18n" comment to column positioning messages of "hg log"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 17863
diff changeset
   872
                                   _("files+:"),
8f85151ce201 i18n: add "i18n" comment to column positioning messages of "hg log"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 17863
diff changeset
   873
                                   # i18n: column positioning for "hg log"
8f85151ce201 i18n: add "i18n" comment to column positioning messages of "hg log"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 17863
diff changeset
   874
                                   _("files-:")], files):
3643
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   875
                if value:
10819
36c6a667d733 cmdutil: make use of output labeling in changeset_printer
Brodie Rao <brodie@bitheap.org>
parents: 10724
diff changeset
   876
                    self.ui.write("%-12s %s\n" % (key, " ".join(value)),
36c6a667d733 cmdutil: make use of output labeling in changeset_printer
Brodie Rao <brodie@bitheap.org>
parents: 10724
diff changeset
   877
                                  label='ui.debug log.files')
9547
f57640bf10d4 cmdutil: changeset_printer: use methods of filectx/changectx.
Greg Ward <greg-hg@gerg.ca>
parents: 9536
diff changeset
   878
        elif ctx.files() and self.ui.verbose:
17891
8f85151ce201 i18n: add "i18n" comment to column positioning messages of "hg log"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 17863
diff changeset
   879
            # i18n: column positioning for "hg log"
10819
36c6a667d733 cmdutil: make use of output labeling in changeset_printer
Brodie Rao <brodie@bitheap.org>
parents: 10724
diff changeset
   880
            self.ui.write(_("files:       %s\n") % " ".join(ctx.files()),
36c6a667d733 cmdutil: make use of output labeling in changeset_printer
Brodie Rao <brodie@bitheap.org>
parents: 10724
diff changeset
   881
                          label='ui.note log.files')
3643
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   882
        if copies and self.ui.verbose:
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   883
            copies = ['%s (%s)' % c for c in copies]
17891
8f85151ce201 i18n: add "i18n" comment to column positioning messages of "hg log"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 17863
diff changeset
   884
            # i18n: column positioning for "hg log"
10819
36c6a667d733 cmdutil: make use of output labeling in changeset_printer
Brodie Rao <brodie@bitheap.org>
parents: 10724
diff changeset
   885
            self.ui.write(_("copies:      %s\n") % ' '.join(copies),
36c6a667d733 cmdutil: make use of output labeling in changeset_printer
Brodie Rao <brodie@bitheap.org>
parents: 10724
diff changeset
   886
                          label='ui.note log.copies')
3643
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   887
9637
64425c5a9257 cmdutil: minor refactoring of changeset_printer._show
Adrian Buehlmann <adrian@cadifra.com>
parents: 9547
diff changeset
   888
        extra = ctx.extra()
3643
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   889
        if extra and self.ui.debugflag:
8209
a1a5a57efe90 replace util.sort with sorted built-in
Matt Mackall <mpm@selenic.com>
parents: 8189
diff changeset
   890
            for key, value in sorted(extra.items()):
17891
8f85151ce201 i18n: add "i18n" comment to column positioning messages of "hg log"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 17863
diff changeset
   891
                # i18n: column positioning for "hg log"
3643
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   892
                self.ui.write(_("extra:       %s=%s\n")
10819
36c6a667d733 cmdutil: make use of output labeling in changeset_printer
Brodie Rao <brodie@bitheap.org>
parents: 10724
diff changeset
   893
                              % (key, value.encode('string_escape')),
36c6a667d733 cmdutil: make use of output labeling in changeset_printer
Brodie Rao <brodie@bitheap.org>
parents: 10724
diff changeset
   894
                              label='ui.debug log.extra')
3643
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   895
9547
f57640bf10d4 cmdutil: changeset_printer: use methods of filectx/changectx.
Greg Ward <greg-hg@gerg.ca>
parents: 9536
diff changeset
   896
        description = ctx.description().strip()
3643
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   897
        if description:
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   898
            if self.ui.verbose:
10819
36c6a667d733 cmdutil: make use of output labeling in changeset_printer
Brodie Rao <brodie@bitheap.org>
parents: 10724
diff changeset
   899
                self.ui.write(_("description:\n"),
36c6a667d733 cmdutil: make use of output labeling in changeset_printer
Brodie Rao <brodie@bitheap.org>
parents: 10724
diff changeset
   900
                              label='ui.note log.description')
36c6a667d733 cmdutil: make use of output labeling in changeset_printer
Brodie Rao <brodie@bitheap.org>
parents: 10724
diff changeset
   901
                self.ui.write(description,
36c6a667d733 cmdutil: make use of output labeling in changeset_printer
Brodie Rao <brodie@bitheap.org>
parents: 10724
diff changeset
   902
                              label='ui.note log.description')
3643
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   903
                self.ui.write("\n\n")
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   904
            else:
17891
8f85151ce201 i18n: add "i18n" comment to column positioning messages of "hg log"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 17863
diff changeset
   905
                # i18n: column positioning for "hg log"
3643
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   906
                self.ui.write(_("summary:     %s\n") %
10819
36c6a667d733 cmdutil: make use of output labeling in changeset_printer
Brodie Rao <brodie@bitheap.org>
parents: 10724
diff changeset
   907
                              description.splitlines()[0],
36c6a667d733 cmdutil: make use of output labeling in changeset_printer
Brodie Rao <brodie@bitheap.org>
parents: 10724
diff changeset
   908
                              label='log.summary')
3643
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   909
        self.ui.write("\n")
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   910
11488
f786fc4b8764 log: follow filenames through renames (issue647)
Mads Kiilerich <mads@kiilerich.com>
parents: 11465
diff changeset
   911
        self.showpatch(changenode, matchfn)
3645
b984dcb1df71 Refactor log ui buffering and patch display
Matt Mackall <mpm@selenic.com>
parents: 3643
diff changeset
   912
11488
f786fc4b8764 log: follow filenames through renames (issue647)
Mads Kiilerich <mads@kiilerich.com>
parents: 11465
diff changeset
   913
    def showpatch(self, node, matchfn):
f786fc4b8764 log: follow filenames through renames (issue647)
Mads Kiilerich <mads@kiilerich.com>
parents: 11465
diff changeset
   914
        if not matchfn:
f786fc4b8764 log: follow filenames through renames (issue647)
Mads Kiilerich <mads@kiilerich.com>
parents: 11465
diff changeset
   915
            matchfn = self.patch
f786fc4b8764 log: follow filenames through renames (issue647)
Mads Kiilerich <mads@kiilerich.com>
parents: 11465
diff changeset
   916
        if matchfn:
11061
51d0387523c6 log: add --stat for diffstat output
Yuya Nishihara <yuya@tcha.org>
parents: 11059
diff changeset
   917
            stat = self.diffopts.get('stat')
11950
d157e040ac4c log: fix the bug 'hg log --stat -p == hg log --stat'
Alecs King <alecsk@gmail.com>
parents: 11488
diff changeset
   918
            diff = self.diffopts.get('patch')
11061
51d0387523c6 log: add --stat for diffstat output
Yuya Nishihara <yuya@tcha.org>
parents: 11059
diff changeset
   919
            diffopts = patch.diffopts(self.ui, self.diffopts)
3645
b984dcb1df71 Refactor log ui buffering and patch display
Matt Mackall <mpm@selenic.com>
parents: 3643
diff changeset
   920
            prev = self.repo.changelog.parents(node)[0]
11950
d157e040ac4c log: fix the bug 'hg log --stat -p == hg log --stat'
Alecs King <alecsk@gmail.com>
parents: 11488
diff changeset
   921
            if stat:
d157e040ac4c log: fix the bug 'hg log --stat -p == hg log --stat'
Alecs King <alecsk@gmail.com>
parents: 11488
diff changeset
   922
                diffordiffstat(self.ui, self.repo, diffopts, prev, node,
d157e040ac4c log: fix the bug 'hg log --stat -p == hg log --stat'
Alecs King <alecsk@gmail.com>
parents: 11488
diff changeset
   923
                               match=matchfn, stat=True)
d157e040ac4c log: fix the bug 'hg log --stat -p == hg log --stat'
Alecs King <alecsk@gmail.com>
parents: 11488
diff changeset
   924
            if diff:
d157e040ac4c log: fix the bug 'hg log --stat -p == hg log --stat'
Alecs King <alecsk@gmail.com>
parents: 11488
diff changeset
   925
                if stat:
d157e040ac4c log: fix the bug 'hg log --stat -p == hg log --stat'
Alecs King <alecsk@gmail.com>
parents: 11488
diff changeset
   926
                    self.ui.write("\n")
d157e040ac4c log: fix the bug 'hg log --stat -p == hg log --stat'
Alecs King <alecsk@gmail.com>
parents: 11488
diff changeset
   927
                diffordiffstat(self.ui, self.repo, diffopts, prev, node,
d157e040ac4c log: fix the bug 'hg log --stat -p == hg log --stat'
Alecs King <alecsk@gmail.com>
parents: 11488
diff changeset
   928
                               match=matchfn, stat=False)
3645
b984dcb1df71 Refactor log ui buffering and patch display
Matt Mackall <mpm@selenic.com>
parents: 3643
diff changeset
   929
            self.ui.write("\n")
b984dcb1df71 Refactor log ui buffering and patch display
Matt Mackall <mpm@selenic.com>
parents: 3643
diff changeset
   930
4825
3cf94964c56b hg log: Move filtering implicit parents to own method and use it in templater.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 4824
diff changeset
   931
    def _meaningful_parentrevs(self, log, rev):
3cf94964c56b hg log: Move filtering implicit parents to own method and use it in templater.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 4824
diff changeset
   932
        """Return list of meaningful (or all if debug) parentrevs for rev.
3cf94964c56b hg log: Move filtering implicit parents to own method and use it in templater.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 4824
diff changeset
   933
3cf94964c56b hg log: Move filtering implicit parents to own method and use it in templater.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 4824
diff changeset
   934
        For merges (two non-nullrev revisions) both parents are meaningful.
3cf94964c56b hg log: Move filtering implicit parents to own method and use it in templater.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 4824
diff changeset
   935
        Otherwise the first parent revision is considered meaningful if it
3cf94964c56b hg log: Move filtering implicit parents to own method and use it in templater.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 4824
diff changeset
   936
        is not the preceding revision.
3cf94964c56b hg log: Move filtering implicit parents to own method and use it in templater.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 4824
diff changeset
   937
        """
3cf94964c56b hg log: Move filtering implicit parents to own method and use it in templater.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 4824
diff changeset
   938
        parents = log.parentrevs(rev)
3cf94964c56b hg log: Move filtering implicit parents to own method and use it in templater.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 4824
diff changeset
   939
        if not self.ui.debugflag and parents[1] == nullrev:
3cf94964c56b hg log: Move filtering implicit parents to own method and use it in templater.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 4824
diff changeset
   940
            if parents[0] >= rev - 1:
3cf94964c56b hg log: Move filtering implicit parents to own method and use it in templater.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 4824
diff changeset
   941
                parents = []
3cf94964c56b hg log: Move filtering implicit parents to own method and use it in templater.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 4824
diff changeset
   942
            else:
3cf94964c56b hg log: Move filtering implicit parents to own method and use it in templater.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 4824
diff changeset
   943
                parents = [parents[0]]
3cf94964c56b hg log: Move filtering implicit parents to own method and use it in templater.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 4824
diff changeset
   944
        return parents
3cf94964c56b hg log: Move filtering implicit parents to own method and use it in templater.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 4824
diff changeset
   945
3cf94964c56b hg log: Move filtering implicit parents to own method and use it in templater.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 4824
diff changeset
   946
3645
b984dcb1df71 Refactor log ui buffering and patch display
Matt Mackall <mpm@selenic.com>
parents: 3643
diff changeset
   947
class changeset_templater(changeset_printer):
3643
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   948
    '''format changeset information.'''
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   949
20667
e96e9f805c19 changeset_templater: remove use_template method
Matt Mackall <mpm@selenic.com>
parents: 20666
diff changeset
   950
    def __init__(self, ui, repo, patch, diffopts, tmpl, mapfile, buffered):
7762
fece056bf240 add --git option to commands supporting --patch (log, incoming, history, tip)
Jim Correia <jim.correia@pobox.com>
parents: 7667
diff changeset
   951
        changeset_printer.__init__(self, ui, repo, patch, diffopts, buffered)
8360
acc202b71619 templater: provide the standard template filters by default
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 8312
diff changeset
   952
        formatnode = ui.debugflag and (lambda x: x) or (lambda x: x[:12])
10061
9e2ab10728a2 Make {file_copies} usable as a --template key
Patrick Mezard <pmezard@gmail.com>
parents: 10060
diff changeset
   953
        defaulttempl = {
9e2ab10728a2 Make {file_copies} usable as a --template key
Patrick Mezard <pmezard@gmail.com>
parents: 10060
diff changeset
   954
            'parent': '{rev}:{node|formatnode} ',
9e2ab10728a2 Make {file_copies} usable as a --template key
Patrick Mezard <pmezard@gmail.com>
parents: 10060
diff changeset
   955
            'manifest': '{rev}:{node|formatnode}',
9e2ab10728a2 Make {file_copies} usable as a --template key
Patrick Mezard <pmezard@gmail.com>
parents: 10060
diff changeset
   956
            'file_copy': '{name} ({source})',
9e2ab10728a2 Make {file_copies} usable as a --template key
Patrick Mezard <pmezard@gmail.com>
parents: 10060
diff changeset
   957
            'extra': '{key}={value|stringescape}'
9e2ab10728a2 Make {file_copies} usable as a --template key
Patrick Mezard <pmezard@gmail.com>
parents: 10060
diff changeset
   958
            }
9e2ab10728a2 Make {file_copies} usable as a --template key
Patrick Mezard <pmezard@gmail.com>
parents: 10060
diff changeset
   959
        # filecopy is preserved for compatibility reasons
9e2ab10728a2 Make {file_copies} usable as a --template key
Patrick Mezard <pmezard@gmail.com>
parents: 10060
diff changeset
   960
        defaulttempl['filecopy'] = defaulttempl['file_copy']
8360
acc202b71619 templater: provide the standard template filters by default
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 8312
diff changeset
   961
        self.t = templater.templater(mapfile, {'formatnode': formatnode},
10061
9e2ab10728a2 Make {file_copies} usable as a --template key
Patrick Mezard <pmezard@gmail.com>
parents: 10060
diff changeset
   962
                                     cache=defaulttempl)
20667
e96e9f805c19 changeset_templater: remove use_template method
Matt Mackall <mpm@selenic.com>
parents: 20666
diff changeset
   963
        if tmpl:
e96e9f805c19 changeset_templater: remove use_template method
Matt Mackall <mpm@selenic.com>
parents: 20666
diff changeset
   964
            self.t.cache['changeset'] = tmpl
3643
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   965
20667
e96e9f805c19 changeset_templater: remove use_template method
Matt Mackall <mpm@selenic.com>
parents: 20666
diff changeset
   966
        self.cache = {}
3643
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   967
7878
8c09952cd39a templater: use contexts consistently throughout changeset_templater
Alexander Solovyov <piranha@piranha.org.ua>
parents: 7807
diff changeset
   968
    def _meaningful_parentrevs(self, ctx):
8c09952cd39a templater: use contexts consistently throughout changeset_templater
Alexander Solovyov <piranha@piranha.org.ua>
parents: 7807
diff changeset
   969
        """Return list of meaningful (or all if debug) parentrevs for rev.
8c09952cd39a templater: use contexts consistently throughout changeset_templater
Alexander Solovyov <piranha@piranha.org.ua>
parents: 7807
diff changeset
   970
        """
8c09952cd39a templater: use contexts consistently throughout changeset_templater
Alexander Solovyov <piranha@piranha.org.ua>
parents: 7807
diff changeset
   971
        parents = ctx.parents()
8c09952cd39a templater: use contexts consistently throughout changeset_templater
Alexander Solovyov <piranha@piranha.org.ua>
parents: 7807
diff changeset
   972
        if len(parents) > 1:
8c09952cd39a templater: use contexts consistently throughout changeset_templater
Alexander Solovyov <piranha@piranha.org.ua>
parents: 7807
diff changeset
   973
            return parents
8c09952cd39a templater: use contexts consistently throughout changeset_templater
Alexander Solovyov <piranha@piranha.org.ua>
parents: 7807
diff changeset
   974
        if self.ui.debugflag:
8c09952cd39a templater: use contexts consistently throughout changeset_templater
Alexander Solovyov <piranha@piranha.org.ua>
parents: 7807
diff changeset
   975
            return [parents[0], self.repo['null']]
8c09952cd39a templater: use contexts consistently throughout changeset_templater
Alexander Solovyov <piranha@piranha.org.ua>
parents: 7807
diff changeset
   976
        if parents[0].rev() >= ctx.rev() - 1:
8c09952cd39a templater: use contexts consistently throughout changeset_templater
Alexander Solovyov <piranha@piranha.org.ua>
parents: 7807
diff changeset
   977
            return []
8c09952cd39a templater: use contexts consistently throughout changeset_templater
Alexander Solovyov <piranha@piranha.org.ua>
parents: 7807
diff changeset
   978
        return parents
8c09952cd39a templater: use contexts consistently throughout changeset_templater
Alexander Solovyov <piranha@piranha.org.ua>
parents: 7807
diff changeset
   979
11488
f786fc4b8764 log: follow filenames through renames (issue647)
Mads Kiilerich <mads@kiilerich.com>
parents: 11465
diff changeset
   980
    def _show(self, ctx, copies, matchfn, props):
3643
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   981
        '''show a single changeset or file revision'''
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   982
10053
5c5c6295533d cmdutil: replace showlist() closure with a function
Patrick Mezard <pmezard@gmail.com>
parents: 10026
diff changeset
   983
        showlist = templatekw.showlist
3643
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   984
10058
c829563b3118 cmdutil: extract file copies closure into templatekw
Patrick Mezard <pmezard@gmail.com>
parents: 10057
diff changeset
   985
        # showparents() behaviour depends on ui trace level which
c829563b3118 cmdutil: extract file copies closure into templatekw
Patrick Mezard <pmezard@gmail.com>
parents: 10057
diff changeset
   986
        # causes unexpected behaviours at templating level and makes
c829563b3118 cmdutil: extract file copies closure into templatekw
Patrick Mezard <pmezard@gmail.com>
parents: 10057
diff changeset
   987
        # it harder to extract it in a standalone function. Its
c829563b3118 cmdutil: extract file copies closure into templatekw
Patrick Mezard <pmezard@gmail.com>
parents: 10057
diff changeset
   988
        # behaviour cannot be changed so leave it here for now.
3643
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   989
        def showparents(**args):
10260
fe699ca08a45 templatekw: fix extras, manifest and showlist args (issue1989)
Patrick Mezard <pmezard@gmail.com>
parents: 10250
diff changeset
   990
            ctx = args['ctx']
7878
8c09952cd39a templater: use contexts consistently throughout changeset_templater
Alexander Solovyov <piranha@piranha.org.ua>
parents: 7807
diff changeset
   991
            parents = [[('rev', p.rev()), ('node', p.hex())]
8c09952cd39a templater: use contexts consistently throughout changeset_templater
Alexander Solovyov <piranha@piranha.org.ua>
parents: 7807
diff changeset
   992
                       for p in self._meaningful_parentrevs(ctx)]
3643
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   993
            return showlist('parent', parents, **args)
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   994
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   995
        props = props.copy()
10054
1a85861f59af cmdutil: extract ctx dependent closures into templatekw
Patrick Mezard <pmezard@gmail.com>
parents: 10053
diff changeset
   996
        props.update(templatekw.keywords)
10058
c829563b3118 cmdutil: extract file copies closure into templatekw
Patrick Mezard <pmezard@gmail.com>
parents: 10057
diff changeset
   997
        props['parents'] = showparents
10053
5c5c6295533d cmdutil: replace showlist() closure with a function
Patrick Mezard <pmezard@gmail.com>
parents: 10026
diff changeset
   998
        props['templ'] = self.t
10054
1a85861f59af cmdutil: extract ctx dependent closures into templatekw
Patrick Mezard <pmezard@gmail.com>
parents: 10053
diff changeset
   999
        props['ctx'] = ctx
10055
e400a511e63a cmdutil: extract repo dependent closures in templatekw
Patrick Mezard <pmezard@gmail.com>
parents: 10054
diff changeset
  1000
        props['repo'] = self.repo
10058
c829563b3118 cmdutil: extract file copies closure into templatekw
Patrick Mezard <pmezard@gmail.com>
parents: 10057
diff changeset
  1001
        props['revcache'] = {'copies': copies}
10057
babc00a82c5e cmdutil: extract latest tags closures in templatekw
Patrick Mezard <pmezard@gmail.com>
parents: 10056
diff changeset
  1002
        props['cache'] = self.cache
3643
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
  1003
8013
9ec25db32b4e cmdutil: prevent code repetition by abstraction in changeset_templater
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7967
diff changeset
  1004
        # find correct templates for current mode
9ec25db32b4e cmdutil: prevent code repetition by abstraction in changeset_templater
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7967
diff changeset
  1005
9ec25db32b4e cmdutil: prevent code repetition by abstraction in changeset_templater
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7967
diff changeset
  1006
        tmplmodes = [
9ec25db32b4e cmdutil: prevent code repetition by abstraction in changeset_templater
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7967
diff changeset
  1007
            (True, None),
9ec25db32b4e cmdutil: prevent code repetition by abstraction in changeset_templater
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7967
diff changeset
  1008
            (self.ui.verbose, 'verbose'),
9ec25db32b4e cmdutil: prevent code repetition by abstraction in changeset_templater
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7967
diff changeset
  1009
            (self.ui.quiet, 'quiet'),
9ec25db32b4e cmdutil: prevent code repetition by abstraction in changeset_templater
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7967
diff changeset
  1010
            (self.ui.debugflag, 'debug'),
9ec25db32b4e cmdutil: prevent code repetition by abstraction in changeset_templater
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7967
diff changeset
  1011
        ]
9ec25db32b4e cmdutil: prevent code repetition by abstraction in changeset_templater
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7967
diff changeset
  1012
10152
56284451a22c Added support for templatevar "footer" to cmdutil.py
Robert Bachmann <rbachm@gmail.com>
parents: 10111
diff changeset
  1013
        types = {'header': '', 'footer':'', 'changeset': 'changeset'}
8013
9ec25db32b4e cmdutil: prevent code repetition by abstraction in changeset_templater
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7967
diff changeset
  1014
        for mode, postfix  in tmplmodes:
9ec25db32b4e cmdutil: prevent code repetition by abstraction in changeset_templater
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7967
diff changeset
  1015
            for type in types:
9ec25db32b4e cmdutil: prevent code repetition by abstraction in changeset_templater
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7967
diff changeset
  1016
                cur = postfix and ('%s_%s' % (type, postfix)) or type
9ec25db32b4e cmdutil: prevent code repetition by abstraction in changeset_templater
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7967
diff changeset
  1017
                if mode and cur in self.t:
9ec25db32b4e cmdutil: prevent code repetition by abstraction in changeset_templater
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7967
diff changeset
  1018
                    types[type] = cur
9ec25db32b4e cmdutil: prevent code repetition by abstraction in changeset_templater
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7967
diff changeset
  1019
3643
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
  1020
        try:
8013
9ec25db32b4e cmdutil: prevent code repetition by abstraction in changeset_templater
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7967
diff changeset
  1021
9ec25db32b4e cmdutil: prevent code repetition by abstraction in changeset_templater
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7967
diff changeset
  1022
            # write header
9ec25db32b4e cmdutil: prevent code repetition by abstraction in changeset_templater
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7967
diff changeset
  1023
            if types['header']:
9ec25db32b4e cmdutil: prevent code repetition by abstraction in changeset_templater
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7967
diff changeset
  1024
                h = templater.stringify(self.t(types['header'], **props))
3645
b984dcb1df71 Refactor log ui buffering and patch display
Matt Mackall <mpm@selenic.com>
parents: 3643
diff changeset
  1025
                if self.buffered:
7878
8c09952cd39a templater: use contexts consistently throughout changeset_templater
Alexander Solovyov <piranha@piranha.org.ua>
parents: 7807
diff changeset
  1026
                    self.header[ctx.rev()] = h
3645
b984dcb1df71 Refactor log ui buffering and patch display
Matt Mackall <mpm@selenic.com>
parents: 3643
diff changeset
  1027
                else:
11465
ace5bd98bee3 heads: fix templating of headers again (issue2130)
Simon Howkins <simonh@symbian.org>
parents: 11441
diff changeset
  1028
                    if self.lastheader != h:
ace5bd98bee3 heads: fix templating of headers again (issue2130)
Simon Howkins <simonh@symbian.org>
parents: 11441
diff changeset
  1029
                        self.lastheader = h
11441
d74fe370ab04 cmdutil: only output style header once in non-buffered mode (issue2130)
Simon Howkins <simonh@symbian.org>
parents: 11410
diff changeset
  1030
                        self.ui.write(h)
8013
9ec25db32b4e cmdutil: prevent code repetition by abstraction in changeset_templater
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7967
diff changeset
  1031
9ec25db32b4e cmdutil: prevent code repetition by abstraction in changeset_templater
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7967
diff changeset
  1032
            # write changeset metadata, then patch if requested
9ec25db32b4e cmdutil: prevent code repetition by abstraction in changeset_templater
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7967
diff changeset
  1033
            key = types['changeset']
3645
b984dcb1df71 Refactor log ui buffering and patch display
Matt Mackall <mpm@selenic.com>
parents: 3643
diff changeset
  1034
            self.ui.write(templater.stringify(self.t(key, **props)))
11488
f786fc4b8764 log: follow filenames through renames (issue647)
Mads Kiilerich <mads@kiilerich.com>
parents: 11465
diff changeset
  1035
            self.showpatch(ctx.node(), matchfn)
8013
9ec25db32b4e cmdutil: prevent code repetition by abstraction in changeset_templater
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7967
diff changeset
  1036
10160
48653dea23dd Bugfix and test for hg log XML output
Robert Bachmann <rbachm@gmail.com>
parents: 10152
diff changeset
  1037
            if types['footer']:
10152
56284451a22c Added support for templatevar "footer" to cmdutil.py
Robert Bachmann <rbachm@gmail.com>
parents: 10111
diff changeset
  1038
                if not self.footer:
56284451a22c Added support for templatevar "footer" to cmdutil.py
Robert Bachmann <rbachm@gmail.com>
parents: 10111
diff changeset
  1039
                    self.footer = templater.stringify(self.t(types['footer'],
56284451a22c Added support for templatevar "footer" to cmdutil.py
Robert Bachmann <rbachm@gmail.com>
parents: 10111
diff changeset
  1040
                                                      **props))
56284451a22c Added support for templatevar "footer" to cmdutil.py
Robert Bachmann <rbachm@gmail.com>
parents: 10111
diff changeset
  1041
3643
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
  1042
        except KeyError, inst:
8013
9ec25db32b4e cmdutil: prevent code repetition by abstraction in changeset_templater
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7967
diff changeset
  1043
            msg = _("%s: no key named '%s'")
9ec25db32b4e cmdutil: prevent code repetition by abstraction in changeset_templater
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7967
diff changeset
  1044
            raise util.Abort(msg % (self.t.mapfile, inst.args[0]))
3643
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
  1045
        except SyntaxError, inst:
10829
56fffc9c8928 cmdutil: do not translate trivial string
Martin Geisler <mg@lazybytes.net>
parents: 10819
diff changeset
  1046
            raise util.Abort('%s: %s' % (self.t.mapfile, inst.args[0]))
3643
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
  1047
20666
e3eb480a9391 cmdutil: make helper function to process template args
Matt Mackall <mpm@selenic.com>
parents: 20604
diff changeset
  1048
def gettemplate(ui, tmpl, style):
e3eb480a9391 cmdutil: make helper function to process template args
Matt Mackall <mpm@selenic.com>
parents: 20604
diff changeset
  1049
    """
e3eb480a9391 cmdutil: make helper function to process template args
Matt Mackall <mpm@selenic.com>
parents: 20604
diff changeset
  1050
    Find the template matching the given template spec or style.
e3eb480a9391 cmdutil: make helper function to process template args
Matt Mackall <mpm@selenic.com>
parents: 20604
diff changeset
  1051
    """
e3eb480a9391 cmdutil: make helper function to process template args
Matt Mackall <mpm@selenic.com>
parents: 20604
diff changeset
  1052
e3eb480a9391 cmdutil: make helper function to process template args
Matt Mackall <mpm@selenic.com>
parents: 20604
diff changeset
  1053
    # ui settings
e3eb480a9391 cmdutil: make helper function to process template args
Matt Mackall <mpm@selenic.com>
parents: 20604
diff changeset
  1054
    if not tmpl and not style:
e3eb480a9391 cmdutil: make helper function to process template args
Matt Mackall <mpm@selenic.com>
parents: 20604
diff changeset
  1055
        tmpl = ui.config('ui', 'logtemplate')
e3eb480a9391 cmdutil: make helper function to process template args
Matt Mackall <mpm@selenic.com>
parents: 20604
diff changeset
  1056
        if tmpl:
e3eb480a9391 cmdutil: make helper function to process template args
Matt Mackall <mpm@selenic.com>
parents: 20604
diff changeset
  1057
            try:
e3eb480a9391 cmdutil: make helper function to process template args
Matt Mackall <mpm@selenic.com>
parents: 20604
diff changeset
  1058
                tmpl = templater.parsestring(tmpl)
e3eb480a9391 cmdutil: make helper function to process template args
Matt Mackall <mpm@selenic.com>
parents: 20604
diff changeset
  1059
            except SyntaxError:
e3eb480a9391 cmdutil: make helper function to process template args
Matt Mackall <mpm@selenic.com>
parents: 20604
diff changeset
  1060
                tmpl = templater.parsestring(tmpl, quoted=False)
20668
3a35ba2681ec templating: make -T much more flexible
Matt Mackall <mpm@selenic.com>
parents: 20667
diff changeset
  1061
            return tmpl, None
20666
e3eb480a9391 cmdutil: make helper function to process template args
Matt Mackall <mpm@selenic.com>
parents: 20604
diff changeset
  1062
        else:
e3eb480a9391 cmdutil: make helper function to process template args
Matt Mackall <mpm@selenic.com>
parents: 20604
diff changeset
  1063
            style = util.expandpath(ui.config('ui', 'style', ''))
e3eb480a9391 cmdutil: make helper function to process template args
Matt Mackall <mpm@selenic.com>
parents: 20604
diff changeset
  1064
e3eb480a9391 cmdutil: make helper function to process template args
Matt Mackall <mpm@selenic.com>
parents: 20604
diff changeset
  1065
    if style:
e3eb480a9391 cmdutil: make helper function to process template args
Matt Mackall <mpm@selenic.com>
parents: 20604
diff changeset
  1066
        mapfile = style
e3eb480a9391 cmdutil: make helper function to process template args
Matt Mackall <mpm@selenic.com>
parents: 20604
diff changeset
  1067
        if not os.path.split(mapfile)[0]:
e3eb480a9391 cmdutil: make helper function to process template args
Matt Mackall <mpm@selenic.com>
parents: 20604
diff changeset
  1068
            mapname = (templater.templatepath('map-cmdline.' + mapfile)
e3eb480a9391 cmdutil: make helper function to process template args
Matt Mackall <mpm@selenic.com>
parents: 20604
diff changeset
  1069
                       or templater.templatepath(mapfile))
e3eb480a9391 cmdutil: make helper function to process template args
Matt Mackall <mpm@selenic.com>
parents: 20604
diff changeset
  1070
            if mapname:
e3eb480a9391 cmdutil: make helper function to process template args
Matt Mackall <mpm@selenic.com>
parents: 20604
diff changeset
  1071
                mapfile = mapname
e3eb480a9391 cmdutil: make helper function to process template args
Matt Mackall <mpm@selenic.com>
parents: 20604
diff changeset
  1072
        return None, mapfile
e3eb480a9391 cmdutil: make helper function to process template args
Matt Mackall <mpm@selenic.com>
parents: 20604
diff changeset
  1073
20668
3a35ba2681ec templating: make -T much more flexible
Matt Mackall <mpm@selenic.com>
parents: 20667
diff changeset
  1074
    if not tmpl:
3a35ba2681ec templating: make -T much more flexible
Matt Mackall <mpm@selenic.com>
parents: 20667
diff changeset
  1075
        return None, None
3a35ba2681ec templating: make -T much more flexible
Matt Mackall <mpm@selenic.com>
parents: 20667
diff changeset
  1076
3a35ba2681ec templating: make -T much more flexible
Matt Mackall <mpm@selenic.com>
parents: 20667
diff changeset
  1077
    # looks like a literal template?
3a35ba2681ec templating: make -T much more flexible
Matt Mackall <mpm@selenic.com>
parents: 20667
diff changeset
  1078
    if '{' in tmpl:
3a35ba2681ec templating: make -T much more flexible
Matt Mackall <mpm@selenic.com>
parents: 20667
diff changeset
  1079
        return tmpl, None
3a35ba2681ec templating: make -T much more flexible
Matt Mackall <mpm@selenic.com>
parents: 20667
diff changeset
  1080
3a35ba2681ec templating: make -T much more flexible
Matt Mackall <mpm@selenic.com>
parents: 20667
diff changeset
  1081
    # perhaps a stock style?
3a35ba2681ec templating: make -T much more flexible
Matt Mackall <mpm@selenic.com>
parents: 20667
diff changeset
  1082
    if not os.path.split(tmpl)[0]:
3a35ba2681ec templating: make -T much more flexible
Matt Mackall <mpm@selenic.com>
parents: 20667
diff changeset
  1083
        mapname = (templater.templatepath('map-cmdline.' + tmpl)
3a35ba2681ec templating: make -T much more flexible
Matt Mackall <mpm@selenic.com>
parents: 20667
diff changeset
  1084
                   or templater.templatepath(tmpl))
3a35ba2681ec templating: make -T much more flexible
Matt Mackall <mpm@selenic.com>
parents: 20667
diff changeset
  1085
        if mapname and os.path.isfile(mapname):
3a35ba2681ec templating: make -T much more flexible
Matt Mackall <mpm@selenic.com>
parents: 20667
diff changeset
  1086
            return None, mapname
3a35ba2681ec templating: make -T much more flexible
Matt Mackall <mpm@selenic.com>
parents: 20667
diff changeset
  1087
3a35ba2681ec templating: make -T much more flexible
Matt Mackall <mpm@selenic.com>
parents: 20667
diff changeset
  1088
    # perhaps it's a reference to [templates]
3a35ba2681ec templating: make -T much more flexible
Matt Mackall <mpm@selenic.com>
parents: 20667
diff changeset
  1089
    t = ui.config('templates', tmpl)
3a35ba2681ec templating: make -T much more flexible
Matt Mackall <mpm@selenic.com>
parents: 20667
diff changeset
  1090
    if t:
3a35ba2681ec templating: make -T much more flexible
Matt Mackall <mpm@selenic.com>
parents: 20667
diff changeset
  1091
        try:
3a35ba2681ec templating: make -T much more flexible
Matt Mackall <mpm@selenic.com>
parents: 20667
diff changeset
  1092
            tmpl = templater.parsestring(t)
3a35ba2681ec templating: make -T much more flexible
Matt Mackall <mpm@selenic.com>
parents: 20667
diff changeset
  1093
        except SyntaxError:
3a35ba2681ec templating: make -T much more flexible
Matt Mackall <mpm@selenic.com>
parents: 20667
diff changeset
  1094
            tmpl = templater.parsestring(t, quoted=False)
3a35ba2681ec templating: make -T much more flexible
Matt Mackall <mpm@selenic.com>
parents: 20667
diff changeset
  1095
        return tmpl, None
3a35ba2681ec templating: make -T much more flexible
Matt Mackall <mpm@selenic.com>
parents: 20667
diff changeset
  1096
3a35ba2681ec templating: make -T much more flexible
Matt Mackall <mpm@selenic.com>
parents: 20667
diff changeset
  1097
    # perhaps it's a path to a map or a template
3a35ba2681ec templating: make -T much more flexible
Matt Mackall <mpm@selenic.com>
parents: 20667
diff changeset
  1098
    if ('/' in tmpl or '\\' in tmpl) and os.path.isfile(tmpl):
3a35ba2681ec templating: make -T much more flexible
Matt Mackall <mpm@selenic.com>
parents: 20667
diff changeset
  1099
        # is it a mapfile for a style?
3a35ba2681ec templating: make -T much more flexible
Matt Mackall <mpm@selenic.com>
parents: 20667
diff changeset
  1100
        if os.path.basename(tmpl).startswith("map-"):
3a35ba2681ec templating: make -T much more flexible
Matt Mackall <mpm@selenic.com>
parents: 20667
diff changeset
  1101
            return None, os.path.realpath(tmpl)
3a35ba2681ec templating: make -T much more flexible
Matt Mackall <mpm@selenic.com>
parents: 20667
diff changeset
  1102
        tmpl = open(tmpl).read()
3a35ba2681ec templating: make -T much more flexible
Matt Mackall <mpm@selenic.com>
parents: 20667
diff changeset
  1103
        return tmpl, None
3a35ba2681ec templating: make -T much more flexible
Matt Mackall <mpm@selenic.com>
parents: 20667
diff changeset
  1104
3a35ba2681ec templating: make -T much more flexible
Matt Mackall <mpm@selenic.com>
parents: 20667
diff changeset
  1105
    # constant string?
20666
e3eb480a9391 cmdutil: make helper function to process template args
Matt Mackall <mpm@selenic.com>
parents: 20604
diff changeset
  1106
    return tmpl, None
e3eb480a9391 cmdutil: make helper function to process template args
Matt Mackall <mpm@selenic.com>
parents: 20604
diff changeset
  1107
11488
f786fc4b8764 log: follow filenames through renames (issue647)
Mads Kiilerich <mads@kiilerich.com>
parents: 11465
diff changeset
  1108
def show_changeset(ui, repo, opts, buffered=False):
3643
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
  1109
    """show one changeset using template or regular display.
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
  1110
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
  1111
    Display format will be the first non-empty hit of:
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
  1112
    1. option 'template'
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
  1113
    2. option 'style'
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
  1114
    3. [ui] setting 'logtemplate'
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
  1115
    4. [ui] setting 'style'
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
  1116
    If all of these values are either the unset or the empty string,
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
  1117
    regular display via changeset_printer() is done.
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
  1118
    """
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
  1119
    # options
19894
df91e2df6ba3 cmdutil: use None as default value for "function pointer" instead of False
Mads Kiilerich <madski@unity3d.com>
parents: 19868
diff changeset
  1120
    patch = None
11061
51d0387523c6 log: add --stat for diffstat output
Yuya Nishihara <yuya@tcha.org>
parents: 11059
diff changeset
  1121
    if opts.get('patch') or opts.get('stat'):
14322
a90131b85fd8 scmutil: drop aliases in cmdutil for match functions
Matt Mackall <mpm@selenic.com>
parents: 14321
diff changeset
  1122
        patch = scmutil.matchall(repo)
3837
7df171ea50cd Fix log regression where log -p file showed diffs for other files
Matt Mackall <mpm@selenic.com>
parents: 3827
diff changeset
  1123
20666
e3eb480a9391 cmdutil: make helper function to process template args
Matt Mackall <mpm@selenic.com>
parents: 20604
diff changeset
  1124
    tmpl, mapfile = gettemplate(ui, opts.get('template'), opts.get('style'))
7967
c03f42159afa cmdutil: refactor handling of templating in show_changeset()
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7948
diff changeset
  1125
20666
e3eb480a9391 cmdutil: make helper function to process template args
Matt Mackall <mpm@selenic.com>
parents: 20604
diff changeset
  1126
    if not tmpl and not mapfile:
7967
c03f42159afa cmdutil: refactor handling of templating in show_changeset()
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7948
diff changeset
  1127
        return changeset_printer(ui, repo, patch, opts, buffered)
3643
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
  1128
7967
c03f42159afa cmdutil: refactor handling of templating in show_changeset()
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7948
diff changeset
  1129
    try:
20667
e96e9f805c19 changeset_templater: remove use_template method
Matt Mackall <mpm@selenic.com>
parents: 20666
diff changeset
  1130
        t = changeset_templater(ui, repo, patch, opts, tmpl, mapfile, buffered)
7967
c03f42159afa cmdutil: refactor handling of templating in show_changeset()
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7948
diff changeset
  1131
    except SyntaxError, inst:
c03f42159afa cmdutil: refactor handling of templating in show_changeset()
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7948
diff changeset
  1132
        raise util.Abort(inst.args[0])
c03f42159afa cmdutil: refactor handling of templating in show_changeset()
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7948
diff changeset
  1133
    return t
3643
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
  1134
20470
78f4c2b7052f debugobsolete: extract marker display in a dedicated function
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 20392
diff changeset
  1135
def showmarker(ui, marker):
78f4c2b7052f debugobsolete: extract marker display in a dedicated function
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 20392
diff changeset
  1136
    """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
  1137
78f4c2b7052f debugobsolete: extract marker display in a dedicated function
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 20392
diff changeset
  1138
    To be used by debug function."""
78f4c2b7052f debugobsolete: extract marker display in a dedicated function
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 20392
diff changeset
  1139
    ui.write(hex(marker.precnode()))
78f4c2b7052f debugobsolete: extract marker display in a dedicated function
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 20392
diff changeset
  1140
    for repl in marker.succnodes():
78f4c2b7052f debugobsolete: extract marker display in a dedicated function
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 20392
diff changeset
  1141
        ui.write(' ')
78f4c2b7052f debugobsolete: extract marker display in a dedicated function
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 20392
diff changeset
  1142
        ui.write(hex(repl))
78f4c2b7052f debugobsolete: extract marker display in a dedicated function
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 20392
diff changeset
  1143
    ui.write(' %X ' % marker._data[2])
78f4c2b7052f debugobsolete: extract marker display in a dedicated function
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 20392
diff changeset
  1144
    ui.write('{%s}' % (', '.join('%r: %r' % t for t in
78f4c2b7052f debugobsolete: extract marker display in a dedicated function
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 20392
diff changeset
  1145
                                 sorted(marker.metadata().items()))))
78f4c2b7052f debugobsolete: extract marker display in a dedicated function
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 20392
diff changeset
  1146
    ui.write('\n')
78f4c2b7052f debugobsolete: extract marker display in a dedicated function
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 20392
diff changeset
  1147
3814
120be84f33de Add --date support to update and revert
Matt Mackall <mpm@selenic.com>
parents: 3738
diff changeset
  1148
def finddate(ui, repo, date):
120be84f33de Add --date support to update and revert
Matt Mackall <mpm@selenic.com>
parents: 3738
diff changeset
  1149
    """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
  1150
5836
c5c9a022bd9a Tweak finddate to pass date directly.
mark.williamson@cl.cam.ac.uk
parents: 5829
diff changeset
  1151
    df = util.matchdate(date)
14322
a90131b85fd8 scmutil: drop aliases in cmdutil for match functions
Matt Mackall <mpm@selenic.com>
parents: 14321
diff changeset
  1152
    m = scmutil.matchall(repo)
3814
120be84f33de Add --date support to update and revert
Matt Mackall <mpm@selenic.com>
parents: 3738
diff changeset
  1153
    results = {}
9662
f3d60543924f walkchangerevs: move 'add' to callback
Matt Mackall <mpm@selenic.com>
parents: 9656
diff changeset
  1154
f3d60543924f walkchangerevs: move 'add' to callback
Matt Mackall <mpm@selenic.com>
parents: 9656
diff changeset
  1155
    def prep(ctx, fns):
f3d60543924f walkchangerevs: move 'add' to callback
Matt Mackall <mpm@selenic.com>
parents: 9656
diff changeset
  1156
        d = ctx.date()
f3d60543924f walkchangerevs: move 'add' to callback
Matt Mackall <mpm@selenic.com>
parents: 9656
diff changeset
  1157
        if df(d[0]):
9668
2c24471d478c cmdutil: fix bug in finddate() implementation
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 9667
diff changeset
  1158
            results[ctx.rev()] = d
9662
f3d60543924f walkchangerevs: move 'add' to callback
Matt Mackall <mpm@selenic.com>
parents: 9656
diff changeset
  1159
9667
8743f2e1bc54 merge changes from mpm
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 9666 9665
diff changeset
  1160
    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
  1161
        rev = ctx.rev()
f3d60543924f walkchangerevs: move 'add' to callback
Matt Mackall <mpm@selenic.com>
parents: 9656
diff changeset
  1162
        if rev in results:
16937
5487088f0d43 cmdutil: lowercase finddate status message
Martin Geisler <mg@aragost.com>
parents: 16776
diff changeset
  1163
            ui.status(_("found revision %s from %s\n") %
9662
f3d60543924f walkchangerevs: move 'add' to callback
Matt Mackall <mpm@selenic.com>
parents: 9656
diff changeset
  1164
                      (rev, util.datestr(results[rev])))
f3d60543924f walkchangerevs: move 'add' to callback
Matt Mackall <mpm@selenic.com>
parents: 9656
diff changeset
  1165
            return str(rev)
3814
120be84f33de Add --date support to update and revert
Matt Mackall <mpm@selenic.com>
parents: 3738
diff changeset
  1166
120be84f33de Add --date support to update and revert
Matt Mackall <mpm@selenic.com>
parents: 3738
diff changeset
  1167
    raise util.Abort(_("revision matching date not found"))
120be84f33de Add --date support to update and revert
Matt Mackall <mpm@selenic.com>
parents: 3738
diff changeset
  1168
20553
86cefb15e7b5 cmdutil: implemented new lazy increasingwindows
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20500
diff changeset
  1169
def increasingwindows(windowsize=8, sizelimit=512):
86cefb15e7b5 cmdutil: implemented new lazy increasingwindows
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20500
diff changeset
  1170
    while True:
86cefb15e7b5 cmdutil: implemented new lazy increasingwindows
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20500
diff changeset
  1171
        yield windowsize
86cefb15e7b5 cmdutil: implemented new lazy increasingwindows
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20500
diff changeset
  1172
        if windowsize < sizelimit:
86cefb15e7b5 cmdutil: implemented new lazy increasingwindows
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20500
diff changeset
  1173
            windowsize *= 2
16776
5088d0b9a9a1 cmdutil: extract increasing_windows() from walkchangerevs()
Patrick Mezard <patrick@mezard.eu>
parents: 16701
diff changeset
  1174
19290
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1175
class FileWalkError(Exception):
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1176
    pass
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1177
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1178
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
  1179
    '''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
  1180
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1181
    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
  1182
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1183
    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
  1184
    filelogs alone.
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1185
    '''
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1186
    wanted = set()
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1187
    copies = []
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1188
    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
  1189
    def filerevgen(filelog, last):
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1190
        """
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1191
        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
  1192
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1193
        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
  1194
        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
  1195
        tuples in backwards order
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1196
        """
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1197
        cl_count = len(repo)
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1198
        revs = []
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1199
        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
  1200
            linkrev = filelog.linkrev(j)
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1201
            if linkrev < minrev:
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1202
                continue
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1203
            # 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
  1204
            # 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
  1205
            if linkrev >= cl_count:
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1206
                break
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1207
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1208
            parentlinkrevs = []
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1209
            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
  1210
                if p != nullrev:
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1211
                    parentlinkrevs.append(filelog.linkrev(p))
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1212
            n = filelog.node(j)
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1213
            revs.append((linkrev, parentlinkrevs,
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1214
                         follow and filelog.renamed(n)))
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1215
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1216
        return reversed(revs)
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1217
    def iterfiles():
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1218
        pctx = repo['.']
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1219
        for filename in match.files():
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1220
            if follow:
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1221
                if filename not in pctx:
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1222
                    raise util.Abort(_('cannot follow file not in parent '
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1223
                                       'revision: "%s"') % filename)
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1224
                yield filename, pctx[filename].filenode()
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1225
            else:
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1226
                yield filename, None
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1227
        for filename_node in copies:
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1228
            yield filename_node
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1229
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1230
    for file_, node in iterfiles():
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1231
        filelog = repo.file(file_)
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1232
        if not len(filelog):
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1233
            if node is None:
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1234
                # 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
  1235
                # 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
  1236
                if follow:
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1237
                    raise util.Abort(
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1238
                        _('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
  1239
                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
  1240
            else:
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1241
                continue
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1242
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1243
        if node is None:
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1244
            last = len(filelog) - 1
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1245
        else:
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1246
            last = filelog.rev(node)
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1247
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1248
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1249
        # keep track of all ancestors of the file
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1250
        ancestors = set([filelog.linkrev(last)])
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1251
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1252
        # 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
  1253
        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
  1254
            if not follow:
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1255
                if rev > maxrev:
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1256
                    continue
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1257
            else:
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1258
                # 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
  1259
                # rev to us:
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1260
                # 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
  1261
                # 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
  1262
                # to explore the file graph
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1263
                if rev not in ancestors:
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1264
                    continue
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1265
                # XXX insert 1327 fix here
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1266
                if flparentlinkrevs:
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1267
                    ancestors.update(flparentlinkrevs)
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1268
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1269
            fncache.setdefault(rev, []).append(file_)
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1270
            wanted.add(rev)
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1271
            if copied:
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1272
                copies.append(copied)
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1273
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1274
    return wanted
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1275
9665
1de5ebfa5585 walkchangerevs: drop ui arg
Matt Mackall <mpm@selenic.com>
parents: 9664
diff changeset
  1276
def walkchangerevs(repo, match, opts, prepare):
7807
bd8f44638847 help: miscellaneous language fixes
timeless <timeless@gmail.com>
parents: 7779
diff changeset
  1277
    '''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
  1278
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1279
    Callers most commonly need to iterate backwards over the history
7807
bd8f44638847 help: miscellaneous language fixes
timeless <timeless@gmail.com>
parents: 7779
diff changeset
  1280
    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
  1281
    performance, so we use iterators in a "windowed" way.
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1282
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1283
    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
  1284
    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
  1285
    order (usually backwards) to display it.
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1286
9662
f3d60543924f walkchangerevs: move 'add' to callback
Matt Mackall <mpm@selenic.com>
parents: 9656
diff changeset
  1287
    This function returns an iterator yielding contexts. Before
f3d60543924f walkchangerevs: move 'add' to callback
Matt Mackall <mpm@selenic.com>
parents: 9656
diff changeset
  1288
    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
  1289
    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
  1290
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1291
    follow = opts.get('follow') or opts.get('follow_first')
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1292
17676
f87683a1b02a clfilter: remove any explicit revision number from default cmdutil range
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 17675
diff changeset
  1293
    if opts.get('rev'):
f87683a1b02a clfilter: remove any explicit revision number from default cmdutil range
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 17675
diff changeset
  1294
        revs = scmutil.revrange(repo, opts.get('rev'))
f87683a1b02a clfilter: remove any explicit revision number from default cmdutil range
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 17675
diff changeset
  1295
    elif follow:
f87683a1b02a clfilter: remove any explicit revision number from default cmdutil range
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 17675
diff changeset
  1296
        revs = repo.revs('reverse(:.)')
3650
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1297
    else:
20704
623ed0ed793e cmdutil: changed walkchangerevs to use spanset instead of baseset
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20668
diff changeset
  1298
        revs = revset.spanset(repo)
17676
f87683a1b02a clfilter: remove any explicit revision number from default cmdutil range
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 17675
diff changeset
  1299
        revs.reverse()
11281
b724b8467b82 walkchangerevs: allow empty query sets
Matt Mackall <mpm@selenic.com>
parents: 11277
diff changeset
  1300
    if not revs:
b724b8467b82 walkchangerevs: allow empty query sets
Matt Mackall <mpm@selenic.com>
parents: 11277
diff changeset
  1301
        return []
8152
08e1baf924ca replace set-like dictionaries with real sets
Martin Geisler <mg@lazybytes.net>
parents: 8119
diff changeset
  1302
    wanted = set()
9652
2cb0cab10d2e walkchangerevs: pull out matchfn
Matt Mackall <mpm@selenic.com>
parents: 9547
diff changeset
  1303
    slowpath = match.anypats() or (match.files() and opts.get('removed'))
3650
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1304
    fncache = {}
16108
f7e0d95d0a0b log: remove caching of all visited revisions (issue3253)
Matt Mackall <mpm@selenic.com>
parents: 16070
diff changeset
  1305
    change = repo.changectx
3650
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1306
11632
f418d2570920 log: document the different phases in walkchangerevs
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 11631
diff changeset
  1307
    # 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
  1308
    # 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
  1309
    # 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
  1310
    # match the file filtering conditions.
f418d2570920 log: document the different phases in walkchangerevs
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 11631
diff changeset
  1311
9652
2cb0cab10d2e walkchangerevs: pull out matchfn
Matt Mackall <mpm@selenic.com>
parents: 9547
diff changeset
  1312
    if not slowpath and not match.files():
3650
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1313
        # No files, no patterns.  Display all revs.
20553
86cefb15e7b5 cmdutil: implemented new lazy increasingwindows
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20500
diff changeset
  1314
        wanted = revs
9665
1de5ebfa5585 walkchangerevs: drop ui arg
Matt Mackall <mpm@selenic.com>
parents: 9664
diff changeset
  1315
16381
64c8ae09162e log: bypass file scan part of fastpath when no files
Matt Mackall <mpm@selenic.com>
parents: 16380
diff changeset
  1316
    if not slowpath and match.files():
11632
f418d2570920 log: document the different phases in walkchangerevs
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 11631
diff changeset
  1317
        # 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
  1318
19290
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1319
        try:
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1320
            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
  1321
        except FileWalkError:
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1322
            slowpath = True
11608
183e63112698 log: remove increasing windows usage in fastpath
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 11607
diff changeset
  1323
19290
f21f4a1b6c24 log: move log file walk to its own function
Durham Goode <durham@fb.com>
parents: 19211
diff changeset
  1324
            # 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
  1325
            # 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
  1326
            # existed in history, otherwise simply return
17746
6d218e47cf9b log: speed up hg log for untracked files (issue1340)
smuralid
parents: 17676
diff changeset
  1327
            for path in match.files():
6d218e47cf9b log: speed up hg log for untracked files (issue1340)
smuralid
parents: 17676
diff changeset
  1328
                if path == '.' or path in repo.store:
6d218e47cf9b log: speed up hg log for untracked files (issue1340)
smuralid
parents: 17676
diff changeset
  1329
                    break
18340
8802277c40ee log: make log work even if first parameter doesn't exist
Mads Kiilerich <mads@kiilerich.com>
parents: 18267
diff changeset
  1330
            else:
8802277c40ee log: make log work even if first parameter doesn't exist
Mads Kiilerich <mads@kiilerich.com>
parents: 18267
diff changeset
  1331
                return []
17746
6d218e47cf9b log: speed up hg log for untracked files (issue1340)
smuralid
parents: 17676
diff changeset
  1332
3650
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1333
    if slowpath:
11632
f418d2570920 log: document the different phases in walkchangerevs
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 11631
diff changeset
  1334
        # 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
  1335
        # changed files
f418d2570920 log: document the different phases in walkchangerevs
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 11631
diff changeset
  1336
3650
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1337
        if follow:
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1338
            raise util.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
  1339
                               'filenames'))
3650
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1340
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1341
        # 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
  1342
        # 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
  1343
        class lazywantedset(object):
d184bae667e4 log: make file log slow path usable on large repos
Durham Goode <durham@fb.com>
parents: 19511
diff changeset
  1344
            def __init__(self):
d184bae667e4 log: make file log slow path usable on large repos
Durham Goode <durham@fb.com>
parents: 19511
diff changeset
  1345
                self.set = set()
d184bae667e4 log: make file log slow path usable on large repos
Durham Goode <durham@fb.com>
parents: 19511
diff changeset
  1346
                self.revs = set(revs)
d184bae667e4 log: make file log slow path usable on large repos
Durham Goode <durham@fb.com>
parents: 19511
diff changeset
  1347
d184bae667e4 log: make file log slow path usable on large repos
Durham Goode <durham@fb.com>
parents: 19511
diff changeset
  1348
            # 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
  1349
            # 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
  1350
            def __contains__(self, value):
d184bae667e4 log: make file log slow path usable on large repos
Durham Goode <durham@fb.com>
parents: 19511
diff changeset
  1351
                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
  1352
                    return True
d184bae667e4 log: make file log slow path usable on large repos
Durham Goode <durham@fb.com>
parents: 19511
diff changeset
  1353
                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
  1354
                    return False
d184bae667e4 log: make file log slow path usable on large repos
Durham Goode <durham@fb.com>
parents: 19511
diff changeset
  1355
                else:
d184bae667e4 log: make file log slow path usable on large repos
Durham Goode <durham@fb.com>
parents: 19511
diff changeset
  1356
                    self.revs.discard(value)
d184bae667e4 log: make file log slow path usable on large repos
Durham Goode <durham@fb.com>
parents: 19511
diff changeset
  1357
                    ctx = change(value)
d184bae667e4 log: make file log slow path usable on large repos
Durham Goode <durham@fb.com>
parents: 19511
diff changeset
  1358
                    matches = filter(match, ctx.files())
d184bae667e4 log: make file log slow path usable on large repos
Durham Goode <durham@fb.com>
parents: 19511
diff changeset
  1359
                    if matches:
d184bae667e4 log: make file log slow path usable on large repos
Durham Goode <durham@fb.com>
parents: 19511
diff changeset
  1360
                        fncache[value] = matches
d184bae667e4 log: make file log slow path usable on large repos
Durham Goode <durham@fb.com>
parents: 19511
diff changeset
  1361
                        self.set.add(value)
d184bae667e4 log: make file log slow path usable on large repos
Durham Goode <durham@fb.com>
parents: 19511
diff changeset
  1362
                        return True
d184bae667e4 log: make file log slow path usable on large repos
Durham Goode <durham@fb.com>
parents: 19511
diff changeset
  1363
                    return False
d184bae667e4 log: make file log slow path usable on large repos
Durham Goode <durham@fb.com>
parents: 19511
diff changeset
  1364
d184bae667e4 log: make file log slow path usable on large repos
Durham Goode <durham@fb.com>
parents: 19511
diff changeset
  1365
            def discard(self, value):
d184bae667e4 log: make file log slow path usable on large repos
Durham Goode <durham@fb.com>
parents: 19511
diff changeset
  1366
                self.revs.discard(value)
d184bae667e4 log: make file log slow path usable on large repos
Durham Goode <durham@fb.com>
parents: 19511
diff changeset
  1367
                self.set.discard(value)
d184bae667e4 log: make file log slow path usable on large repos
Durham Goode <durham@fb.com>
parents: 19511
diff changeset
  1368
d184bae667e4 log: make file log slow path usable on large repos
Durham Goode <durham@fb.com>
parents: 19511
diff changeset
  1369
        wanted = lazywantedset()
3650
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1370
8778
c5f36402daad use new style classes
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 8761
diff changeset
  1371
    class followfilter(object):
3650
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1372
        def __init__(self, onlyfirst=False):
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1373
            self.startrev = nullrev
10024
2b630e4c8f2f log --follow: use a set instead of a list
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10012
diff changeset
  1374
            self.roots = set()
3650
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1375
            self.onlyfirst = onlyfirst
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1376
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1377
        def match(self, rev):
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1378
            def realparents(rev):
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1379
                if self.onlyfirst:
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1380
                    return repo.changelog.parentrevs(rev)[0:1]
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1381
                else:
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1382
                    return filter(lambda x: x != nullrev,
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1383
                                  repo.changelog.parentrevs(rev))
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1384
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1385
            if self.startrev == nullrev:
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1386
                self.startrev = rev
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1387
                return True
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1388
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1389
            if rev > self.startrev:
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1390
                # forward: all descendants
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1391
                if not self.roots:
10024
2b630e4c8f2f log --follow: use a set instead of a list
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10012
diff changeset
  1392
                    self.roots.add(self.startrev)
3650
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1393
                for parent in realparents(rev):
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1394
                    if parent in self.roots:
10024
2b630e4c8f2f log --follow: use a set instead of a list
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10012
diff changeset
  1395
                        self.roots.add(rev)
3650
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1396
                        return True
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1397
            else:
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1398
                # backwards: all parents
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1399
                if not self.roots:
10024
2b630e4c8f2f log --follow: use a set instead of a list
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10012
diff changeset
  1400
                    self.roots.update(realparents(self.startrev))
3650
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1401
                if rev in self.roots:
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1402
                    self.roots.remove(rev)
10024
2b630e4c8f2f log --follow: use a set instead of a list
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10012
diff changeset
  1403
                    self.roots.update(realparents(rev))
3650
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1404
                    return True
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1405
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1406
            return False
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1407
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1408
    # 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
  1409
    # 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
  1410
    for rev in opts.get('prune', ()):
16380
84ba30e8c790 cmdutil: use context instead of lookup
Matt Mackall <mpm@selenic.com>
parents: 16304
diff changeset
  1411
        rev = repo[rev].rev()
3650
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1412
        ff = followfilter()
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1413
        stop = min(revs[0], revs[-1])
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10264
diff changeset
  1414
        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
  1415
            if ff.match(x):
20553
86cefb15e7b5 cmdutil: implemented new lazy increasingwindows
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20500
diff changeset
  1416
                wanted = wanted - [x]
18710
49ef9d0ca815 cmdutil: use a small initial window with --limit
Bryan O'Sullivan <bryano@fb.com>
parents: 18688
diff changeset
  1417
11632
f418d2570920 log: document the different phases in walkchangerevs
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 11631
diff changeset
  1418
    # 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
  1419
    # revision range, yielding only revisions in wanted.
3650
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1420
    def iterate():
9652
2cb0cab10d2e walkchangerevs: pull out matchfn
Matt Mackall <mpm@selenic.com>
parents: 9547
diff changeset
  1421
        if follow and not match.files():
3650
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1422
            ff = followfilter(onlyfirst=opts.get('follow_first'))
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1423
            def want(rev):
8119
af44d0b953c6 cmdutil: return boolean result directly in want function
Martin Geisler <mg@lazybytes.net>
parents: 8117
diff changeset
  1424
                return ff.match(rev) and rev in wanted
3650
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1425
        else:
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1426
            def want(rev):
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1427
                return rev in wanted
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1428
20553
86cefb15e7b5 cmdutil: implemented new lazy increasingwindows
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20500
diff changeset
  1429
        it = iter(revs)
86cefb15e7b5 cmdutil: implemented new lazy increasingwindows
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20500
diff changeset
  1430
        stopiteration = False
86cefb15e7b5 cmdutil: implemented new lazy increasingwindows
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20500
diff changeset
  1431
        for windowsize in increasingwindows():
86cefb15e7b5 cmdutil: implemented new lazy increasingwindows
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20500
diff changeset
  1432
            nrevs = []
86cefb15e7b5 cmdutil: implemented new lazy increasingwindows
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20500
diff changeset
  1433
            for i in xrange(windowsize):
86cefb15e7b5 cmdutil: implemented new lazy increasingwindows
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20500
diff changeset
  1434
                try:
86cefb15e7b5 cmdutil: implemented new lazy increasingwindows
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20500
diff changeset
  1435
                    rev = it.next()
86cefb15e7b5 cmdutil: implemented new lazy increasingwindows
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20500
diff changeset
  1436
                    if want(rev):
86cefb15e7b5 cmdutil: implemented new lazy increasingwindows
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20500
diff changeset
  1437
                        nrevs.append(rev)
86cefb15e7b5 cmdutil: implemented new lazy increasingwindows
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20500
diff changeset
  1438
                except (StopIteration):
86cefb15e7b5 cmdutil: implemented new lazy increasingwindows
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20500
diff changeset
  1439
                    stopiteration = True
86cefb15e7b5 cmdutil: implemented new lazy increasingwindows
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20500
diff changeset
  1440
                    break
8209
a1a5a57efe90 replace util.sort with sorted built-in
Matt Mackall <mpm@selenic.com>
parents: 8189
diff changeset
  1441
            for rev in sorted(nrevs):
3650
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1442
                fns = fncache.get(rev)
9654
96fe91be9c1e walkchangerevs: yield contexts
Matt Mackall <mpm@selenic.com>
parents: 9653
diff changeset
  1443
                ctx = change(rev)
3650
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1444
                if not fns:
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1445
                    def fns_generator():
9654
96fe91be9c1e walkchangerevs: yield contexts
Matt Mackall <mpm@selenic.com>
parents: 9653
diff changeset
  1446
                        for f in ctx.files():
9652
2cb0cab10d2e walkchangerevs: pull out matchfn
Matt Mackall <mpm@selenic.com>
parents: 9547
diff changeset
  1447
                            if match(f):
3650
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1448
                                yield f
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1449
                    fns = fns_generator()
9662
f3d60543924f walkchangerevs: move 'add' to callback
Matt Mackall <mpm@selenic.com>
parents: 9656
diff changeset
  1450
                prepare(ctx, fns)
3650
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1451
            for rev in nrevs:
9662
f3d60543924f walkchangerevs: move 'add' to callback
Matt Mackall <mpm@selenic.com>
parents: 9656
diff changeset
  1452
                yield change(rev)
20553
86cefb15e7b5 cmdutil: implemented new lazy increasingwindows
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20500
diff changeset
  1453
86cefb15e7b5 cmdutil: implemented new lazy increasingwindows
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20500
diff changeset
  1454
            if stopiteration:
86cefb15e7b5 cmdutil: implemented new lazy increasingwindows
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20500
diff changeset
  1455
                break
86cefb15e7b5 cmdutil: implemented new lazy increasingwindows
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20500
diff changeset
  1456
9652
2cb0cab10d2e walkchangerevs: pull out matchfn
Matt Mackall <mpm@selenic.com>
parents: 9547
diff changeset
  1457
    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
  1458
21108
e5ad36a845af cmdutil: changed _makegraphlogrevset to _makelogrevset
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 21051
diff changeset
  1459
def _makelogfilematcher(repo, pats, followfirst):
17180
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1460
    # When displaying a revision with --patch --follow FILE, we have
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1461
    # to know which file of the revision must be diffed. With
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1462
    # --follow, we want the names of the ancestors of FILE in the
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1463
    # revision, stored in "fcache". "fcache" is populated by
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1464
    # reproducing the graph traversal already done by --follow revset
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1465
    # and relating linkrevs to file names (which is not "correct" but
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1466
    # good enough).
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1467
    fcache = {}
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1468
    fcacheready = [False]
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1469
    pctx = repo['.']
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1470
    wctx = repo[None]
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1471
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1472
    def populate():
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1473
        for fn in pats:
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1474
            for i in ((pctx[fn],), pctx[fn].ancestors(followfirst=followfirst)):
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1475
                for c in i:
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1476
                    fcache.setdefault(c.linkrev(), set()).add(c.path())
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1477
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1478
    def filematcher(rev):
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1479
        if not fcacheready[0]:
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1480
            # Lazy initialization
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1481
            fcacheready[0] = True
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1482
            populate()
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1483
        return scmutil.match(wctx, fcache.get(rev, []), default='path')
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1484
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1485
    return filematcher
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1486
21108
e5ad36a845af cmdutil: changed _makegraphlogrevset to _makelogrevset
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 21051
diff changeset
  1487
def _makelogrevset(repo, pats, opts, revs):
17180
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1488
    """Return (expr, filematcher) where expr is a revset string built
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1489
    from log options and file patterns or None. If --stat or --patch
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1490
    are not passed filematcher is None. Otherwise it is a callable
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1491
    taking a revision number and returning a match objects filtering
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1492
    the files to be detailed when displaying the revision.
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1493
    """
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1494
    opt2revset = {
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1495
        'no_merges':        ('not merge()', None),
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1496
        'only_merges':      ('merge()', None),
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1497
        '_ancestors':       ('ancestors(%(val)s)', None),
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1498
        '_fancestors':      ('_firstancestors(%(val)s)', None),
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1499
        '_descendants':     ('descendants(%(val)s)', None),
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1500
        '_fdescendants':    ('_firstdescendants(%(val)s)', None),
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1501
        '_matchfiles':      ('_matchfiles(%(val)s)', None),
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1502
        'date':             ('date(%(val)r)', None),
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1503
        'branch':           ('branch(%(val)r)', ' or '),
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1504
        '_patslog':         ('filelog(%(val)r)', ' or '),
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1505
        '_patsfollow':      ('follow(%(val)r)', ' or '),
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1506
        '_patsfollowfirst': ('_followfirst(%(val)r)', ' or '),
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1507
        'keyword':          ('keyword(%(val)r)', ' or '),
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1508
        'prune':            ('not (%(val)r or ancestors(%(val)r))', ' and '),
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1509
        'user':             ('user(%(val)r)', ' or '),
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1510
        }
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1511
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1512
    opts = dict(opts)
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1513
    # follow or not follow?
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1514
    follow = opts.get('follow') or opts.get('follow_first')
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1515
    followfirst = opts.get('follow_first') and 1 or 0
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1516
    # --follow with FILE behaviour depends on revs...
20756
e7833e63bb42 cmdutil: changed code in _makegraphlogrevset not to use getitem
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20755
diff changeset
  1517
    it = iter(revs)
e7833e63bb42 cmdutil: changed code in _makegraphlogrevset not to use getitem
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20755
diff changeset
  1518
    startrev = it.next()
e7833e63bb42 cmdutil: changed code in _makegraphlogrevset not to use getitem
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20755
diff changeset
  1519
    try:
e7833e63bb42 cmdutil: changed code in _makegraphlogrevset not to use getitem
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20755
diff changeset
  1520
        followdescendants = startrev < it.next()
e7833e63bb42 cmdutil: changed code in _makegraphlogrevset not to use getitem
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20755
diff changeset
  1521
    except (StopIteration):
e7833e63bb42 cmdutil: changed code in _makegraphlogrevset not to use getitem
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20755
diff changeset
  1522
        followdescendants = False
17180
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1523
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1524
    # branch and only_branch are really aliases and must be handled at
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1525
    # the same time
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1526
    opts['branch'] = opts.get('branch', []) + opts.get('only_branch', [])
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1527
    opts['branch'] = [repo.lookupbranch(b) for b in opts['branch']]
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1528
    # pats/include/exclude are passed to match.match() directly in
17424
e7cfe3587ea4 fix trivial spelling errors
Mads Kiilerich <mads@kiilerich.com>
parents: 17391
diff changeset
  1529
    # _matchfiles() revset but walkchangerevs() builds its matcher with
17180
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1530
    # scmutil.match(). The difference is input pats are globbed on
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1531
    # platforms without shell expansion (windows).
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1532
    pctx = repo[None]
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1533
    match, pats = scmutil.matchandpats(pctx, pats, opts)
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1534
    slowpath = match.anypats() or (match.files() and opts.get('removed'))
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1535
    if not slowpath:
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1536
        for f in match.files():
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1537
            if follow and f not in pctx:
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1538
                raise util.Abort(_('cannot follow file not in parent '
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1539
                                   'revision: "%s"') % f)
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1540
            filelog = repo.file(f)
19293
446ab88d3f1c filelog: switch 'not len(filerevlog)' to 'not filerevlog'
Durham Goode <durham@fb.com>
parents: 19290
diff changeset
  1541
            if not filelog:
17180
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1542
                # A zero count may be a directory or deleted file, so
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1543
                # try to find matching entries on the slow path.
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1544
                if follow:
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1545
                    raise util.Abort(
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1546
                        _('cannot follow nonexistent file: "%s"') % f)
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1547
                slowpath = True
17746
6d218e47cf9b log: speed up hg log for untracked files (issue1340)
smuralid
parents: 17676
diff changeset
  1548
6d218e47cf9b log: speed up hg log for untracked files (issue1340)
smuralid
parents: 17676
diff changeset
  1549
        # We decided to fall back to the slowpath because at least one
6d218e47cf9b log: speed up hg log for untracked files (issue1340)
smuralid
parents: 17676
diff changeset
  1550
        # of the paths was not a file. Check to see if at least one of them
6d218e47cf9b log: speed up hg log for untracked files (issue1340)
smuralid
parents: 17676
diff changeset
  1551
        # existed in history - in that case, we'll continue down the
6d218e47cf9b log: speed up hg log for untracked files (issue1340)
smuralid
parents: 17676
diff changeset
  1552
        # slowpath; otherwise, we can turn off the slowpath
6d218e47cf9b log: speed up hg log for untracked files (issue1340)
smuralid
parents: 17676
diff changeset
  1553
        if slowpath:
6d218e47cf9b log: speed up hg log for untracked files (issue1340)
smuralid
parents: 17676
diff changeset
  1554
            for path in match.files():
6d218e47cf9b log: speed up hg log for untracked files (issue1340)
smuralid
parents: 17676
diff changeset
  1555
                if path == '.' or path in repo.store:
6d218e47cf9b log: speed up hg log for untracked files (issue1340)
smuralid
parents: 17676
diff changeset
  1556
                    break
6d218e47cf9b log: speed up hg log for untracked files (issue1340)
smuralid
parents: 17676
diff changeset
  1557
            else:
6d218e47cf9b log: speed up hg log for untracked files (issue1340)
smuralid
parents: 17676
diff changeset
  1558
                slowpath = False
6d218e47cf9b log: speed up hg log for untracked files (issue1340)
smuralid
parents: 17676
diff changeset
  1559
17180
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1560
    if slowpath:
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1561
        # See walkchangerevs() slow path.
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1562
        #
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1563
        if follow:
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1564
            raise util.Abort(_('can only follow copies/renames for explicit '
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1565
                               'filenames'))
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1566
        # pats/include/exclude cannot be represented as separate
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1567
        # revset expressions as their filtering logic applies at file
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1568
        # level. For instance "-I a -X a" matches a revision touching
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1569
        # "a" and "b" while "file(a) and not file(b)" does
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1570
        # not. Besides, filesets are evaluated against the working
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1571
        # directory.
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1572
        matchargs = ['r:', 'd:relpath']
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1573
        for p in pats:
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1574
            matchargs.append('p:' + p)
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1575
        for p in opts.get('include', []):
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1576
            matchargs.append('i:' + p)
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1577
        for p in opts.get('exclude', []):
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1578
            matchargs.append('x:' + p)
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1579
        matchargs = ','.join(('%r' % p) for p in matchargs)
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1580
        opts['_matchfiles'] = matchargs
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1581
    else:
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1582
        if follow:
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1583
            fpats = ('_patsfollow', '_patsfollowfirst')
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1584
            fnopats = (('_ancestors', '_fancestors'),
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1585
                       ('_descendants', '_fdescendants'))
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1586
            if pats:
17424
e7cfe3587ea4 fix trivial spelling errors
Mads Kiilerich <mads@kiilerich.com>
parents: 17391
diff changeset
  1587
                # follow() revset interprets its file argument as a
17180
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1588
                # manifest entry, so use match.files(), not pats.
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1589
                opts[fpats[followfirst]] = list(match.files())
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1590
            else:
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1591
                opts[fnopats[followdescendants][followfirst]] = str(startrev)
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1592
        else:
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1593
            opts['_patslog'] = list(pats)
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1594
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1595
    filematcher = None
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1596
    if opts.get('patch') or opts.get('stat'):
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1597
        if follow:
21108
e5ad36a845af cmdutil: changed _makegraphlogrevset to _makelogrevset
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 21051
diff changeset
  1598
            filematcher = _makelogfilematcher(repo, pats, followfirst)
17180
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1599
        else:
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1600
            filematcher = lambda rev: match
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1601
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1602
    expr = []
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1603
    for op, val in opts.iteritems():
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1604
        if not val:
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1605
            continue
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1606
        if op not in opt2revset:
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1607
            continue
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1608
        revop, andor = opt2revset[op]
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1609
        if '%(val)' not in revop:
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1610
            expr.append(revop)
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1611
        else:
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1612
            if not isinstance(val, list):
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1613
                e = revop % {'val': val}
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1614
            else:
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1615
                e = '(' + andor.join((revop % {'val': v}) for v in val) + ')'
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1616
            expr.append(e)
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1617
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1618
    if expr:
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1619
        expr = '(' + ' and '.join(expr) + ')'
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1620
    else:
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1621
        expr = None
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1622
    return expr, filematcher
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1623
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1624
def getgraphlogrevs(repo, pats, opts):
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1625
    """Return (revs, expr, filematcher) where revs is an iterable of
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1626
    revision numbers, expr is a revset string built from log options
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1627
    and file patterns or None, and used to filter 'revs'. If --stat or
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1628
    --patch are not passed filematcher is None. Otherwise it is a
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1629
    callable taking a revision number and returning a match objects
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1630
    filtering the files to be detailed when displaying the revision.
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1631
    """
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1632
    if not len(repo):
18171
9d350f2d9458 cmdutil: stop pretending we can calculate revs for graphlog lazily
Siddharth Agarwal <sid0@fb.com>
parents: 18170
diff changeset
  1633
        return [], None, None
18172
e6c5e0092469 cmdutil: make getgraphlogrevs limit-aware
Siddharth Agarwal <sid0@fb.com>
parents: 18171
diff changeset
  1634
    limit = loglimit(opts)
17180
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1635
    # Default --rev value depends on --follow but --follow behaviour
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1636
    # depends on revisions resolved from --rev...
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1637
    follow = opts.get('follow') or opts.get('follow_first')
18169
ae663cba9a8d cmdutil: make getgraphlogrevs return revs in descending order
Siddharth Agarwal <sid0@fb.com>
parents: 18031
diff changeset
  1638
    possiblyunsorted = False # whether revs might need sorting
17180
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1639
    if opts.get('rev'):
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1640
        revs = scmutil.revrange(repo, opts['rev'])
21108
e5ad36a845af cmdutil: changed _makegraphlogrevset to _makelogrevset
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 21051
diff changeset
  1641
        # Don't sort here because _makelogrevset might depend on the
18169
ae663cba9a8d cmdutil: make getgraphlogrevs return revs in descending order
Siddharth Agarwal <sid0@fb.com>
parents: 18031
diff changeset
  1642
        # order of revs
ae663cba9a8d cmdutil: make getgraphlogrevs return revs in descending order
Siddharth Agarwal <sid0@fb.com>
parents: 18031
diff changeset
  1643
        possiblyunsorted = True
17180
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1644
    else:
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1645
        if follow and len(repo) > 0:
17676
f87683a1b02a clfilter: remove any explicit revision number from default cmdutil range
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 17675
diff changeset
  1646
            revs = repo.revs('reverse(:.)')
17180
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1647
        else:
20757
3813a1dd9eb9 cmdutil: changed revset for spanset
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20756
diff changeset
  1648
            revs = revset.spanset(repo)
17675
8575f4a2126e clfilter: remove usage of `range` in favor of iteration over changelog
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 17475
diff changeset
  1649
            revs.reverse()
17180
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1650
    if not revs:
20759
74139960c302 getgraphlogrevs: return an empty baseset instead of a empty list
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20758
diff changeset
  1651
        return revset.baseset(), None, None
21108
e5ad36a845af cmdutil: changed _makegraphlogrevset to _makelogrevset
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 21051
diff changeset
  1652
    expr, filematcher = _makelogrevset(repo, pats, opts, revs)
18169
ae663cba9a8d cmdutil: make getgraphlogrevs return revs in descending order
Siddharth Agarwal <sid0@fb.com>
parents: 18031
diff changeset
  1653
    if possiblyunsorted:
ae663cba9a8d cmdutil: make getgraphlogrevs return revs in descending order
Siddharth Agarwal <sid0@fb.com>
parents: 18031
diff changeset
  1654
        revs.sort(reverse=True)
17180
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1655
    if expr:
18171
9d350f2d9458 cmdutil: stop pretending we can calculate revs for graphlog lazily
Siddharth Agarwal <sid0@fb.com>
parents: 18170
diff changeset
  1656
        # Revset matchers often operate faster on revisions in changelog
9d350f2d9458 cmdutil: stop pretending we can calculate revs for graphlog lazily
Siddharth Agarwal <sid0@fb.com>
parents: 18170
diff changeset
  1657
        # order, because most filters deal with the changelog.
9d350f2d9458 cmdutil: stop pretending we can calculate revs for graphlog lazily
Siddharth Agarwal <sid0@fb.com>
parents: 18170
diff changeset
  1658
        revs.reverse()
17180
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1659
        matcher = revset.match(repo.ui, expr)
18171
9d350f2d9458 cmdutil: stop pretending we can calculate revs for graphlog lazily
Siddharth Agarwal <sid0@fb.com>
parents: 18170
diff changeset
  1660
        # Revset matches can reorder revisions. "A or B" typically returns
9d350f2d9458 cmdutil: stop pretending we can calculate revs for graphlog lazily
Siddharth Agarwal <sid0@fb.com>
parents: 18170
diff changeset
  1661
        # returns the revision matching A then the revision matching B. Sort
9d350f2d9458 cmdutil: stop pretending we can calculate revs for graphlog lazily
Siddharth Agarwal <sid0@fb.com>
parents: 18170
diff changeset
  1662
        # again to fix that.
9d350f2d9458 cmdutil: stop pretending we can calculate revs for graphlog lazily
Siddharth Agarwal <sid0@fb.com>
parents: 18170
diff changeset
  1663
        revs = matcher(repo, revs)
9d350f2d9458 cmdutil: stop pretending we can calculate revs for graphlog lazily
Siddharth Agarwal <sid0@fb.com>
parents: 18170
diff changeset
  1664
        revs.sort(reverse=True)
18243
b3b1b8e127e5 log: use "hidden" filtering instead of manual check at display time
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18235
diff changeset
  1665
    if limit is not None:
20755
cfd03c069e08 cmdutil: changed code in getgraphlogrevs not to use getitem
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20710
diff changeset
  1666
        limitedrevs = revset.baseset()
cfd03c069e08 cmdutil: changed code in getgraphlogrevs not to use getitem
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20710
diff changeset
  1667
        for idx, rev in enumerate(revs):
cfd03c069e08 cmdutil: changed code in getgraphlogrevs not to use getitem
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20710
diff changeset
  1668
            if idx >= limit:
cfd03c069e08 cmdutil: changed code in getgraphlogrevs not to use getitem
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20710
diff changeset
  1669
                break
cfd03c069e08 cmdutil: changed code in getgraphlogrevs not to use getitem
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20710
diff changeset
  1670
            limitedrevs.append(rev)
cfd03c069e08 cmdutil: changed code in getgraphlogrevs not to use getitem
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20710
diff changeset
  1671
        revs = limitedrevs
18172
e6c5e0092469 cmdutil: make getgraphlogrevs limit-aware
Siddharth Agarwal <sid0@fb.com>
parents: 18171
diff changeset
  1672
17180
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1673
    return revs, expr, filematcher
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1674
21127
69402eb72115 log: changed implementation to use graphlog code
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 21108
diff changeset
  1675
def getlogrevs(repo, pats, opts):
69402eb72115 log: changed implementation to use graphlog code
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 21108
diff changeset
  1676
    """Return (revs, expr, filematcher) where revs is an iterable of
69402eb72115 log: changed implementation to use graphlog code
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 21108
diff changeset
  1677
    revision numbers, expr is a revset string built from log options
69402eb72115 log: changed implementation to use graphlog code
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 21108
diff changeset
  1678
    and file patterns or None, and used to filter 'revs'. If --stat or
69402eb72115 log: changed implementation to use graphlog code
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 21108
diff changeset
  1679
    --patch are not passed filematcher is None. Otherwise it is a
69402eb72115 log: changed implementation to use graphlog code
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 21108
diff changeset
  1680
    callable taking a revision number and returning a match objects
69402eb72115 log: changed implementation to use graphlog code
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 21108
diff changeset
  1681
    filtering the files to be detailed when displaying the revision.
69402eb72115 log: changed implementation to use graphlog code
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 21108
diff changeset
  1682
    """
69402eb72115 log: changed implementation to use graphlog code
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 21108
diff changeset
  1683
    limit = loglimit(opts)
69402eb72115 log: changed implementation to use graphlog code
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 21108
diff changeset
  1684
    # Default --rev value depends on --follow but --follow behaviour
69402eb72115 log: changed implementation to use graphlog code
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 21108
diff changeset
  1685
    # depends on revisions resolved from --rev...
69402eb72115 log: changed implementation to use graphlog code
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 21108
diff changeset
  1686
    follow = opts.get('follow') or opts.get('follow_first')
69402eb72115 log: changed implementation to use graphlog code
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 21108
diff changeset
  1687
    if opts.get('rev'):
69402eb72115 log: changed implementation to use graphlog code
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 21108
diff changeset
  1688
        revs = scmutil.revrange(repo, opts['rev'])
69402eb72115 log: changed implementation to use graphlog code
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 21108
diff changeset
  1689
    elif follow:
69402eb72115 log: changed implementation to use graphlog code
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 21108
diff changeset
  1690
        revs = revset.baseset(repo.revs('reverse(:.)'))
69402eb72115 log: changed implementation to use graphlog code
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 21108
diff changeset
  1691
    else:
69402eb72115 log: changed implementation to use graphlog code
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 21108
diff changeset
  1692
        revs = revset.spanset(repo)
69402eb72115 log: changed implementation to use graphlog code
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 21108
diff changeset
  1693
        revs.reverse()
69402eb72115 log: changed implementation to use graphlog code
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 21108
diff changeset
  1694
    if not revs:
69402eb72115 log: changed implementation to use graphlog code
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 21108
diff changeset
  1695
        return revset.baseset([]), None, None
69402eb72115 log: changed implementation to use graphlog code
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 21108
diff changeset
  1696
    expr, filematcher = _makelogrevset(repo, pats, opts, revs)
69402eb72115 log: changed implementation to use graphlog code
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 21108
diff changeset
  1697
    if expr:
69402eb72115 log: changed implementation to use graphlog code
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 21108
diff changeset
  1698
        # Revset matchers often operate faster on revisions in changelog
69402eb72115 log: changed implementation to use graphlog code
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 21108
diff changeset
  1699
        # order, because most filters deal with the changelog.
69402eb72115 log: changed implementation to use graphlog code
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 21108
diff changeset
  1700
        if not opts.get('rev'):
69402eb72115 log: changed implementation to use graphlog code
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 21108
diff changeset
  1701
            revs.reverse()
69402eb72115 log: changed implementation to use graphlog code
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 21108
diff changeset
  1702
        matcher = revset.match(repo.ui, expr)
69402eb72115 log: changed implementation to use graphlog code
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 21108
diff changeset
  1703
        # Revset matches can reorder revisions. "A or B" typically returns
69402eb72115 log: changed implementation to use graphlog code
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 21108
diff changeset
  1704
        # returns the revision matching A then the revision matching B. Sort
69402eb72115 log: changed implementation to use graphlog code
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 21108
diff changeset
  1705
        # again to fix that.
69402eb72115 log: changed implementation to use graphlog code
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 21108
diff changeset
  1706
        revs = matcher(repo, revs)
69402eb72115 log: changed implementation to use graphlog code
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 21108
diff changeset
  1707
        if not opts.get('rev'):
69402eb72115 log: changed implementation to use graphlog code
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 21108
diff changeset
  1708
            revs.sort(reverse=True)
69402eb72115 log: changed implementation to use graphlog code
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 21108
diff changeset
  1709
    if limit is not None:
69402eb72115 log: changed implementation to use graphlog code
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 21108
diff changeset
  1710
        count = 0
69402eb72115 log: changed implementation to use graphlog code
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 21108
diff changeset
  1711
        limitedrevs = revset.baseset([])
69402eb72115 log: changed implementation to use graphlog code
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 21108
diff changeset
  1712
        it = iter(revs)
69402eb72115 log: changed implementation to use graphlog code
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 21108
diff changeset
  1713
        while count < limit:
69402eb72115 log: changed implementation to use graphlog code
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 21108
diff changeset
  1714
            try:
69402eb72115 log: changed implementation to use graphlog code
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 21108
diff changeset
  1715
                limitedrevs.append(it.next())
69402eb72115 log: changed implementation to use graphlog code
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 21108
diff changeset
  1716
            except (StopIteration):
69402eb72115 log: changed implementation to use graphlog code
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 21108
diff changeset
  1717
                break
69402eb72115 log: changed implementation to use graphlog code
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 21108
diff changeset
  1718
            count += 1
69402eb72115 log: changed implementation to use graphlog code
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 21108
diff changeset
  1719
        revs = limitedrevs
69402eb72115 log: changed implementation to use graphlog code
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 21108
diff changeset
  1720
69402eb72115 log: changed implementation to use graphlog code
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 21108
diff changeset
  1721
    return revs, expr, filematcher
69402eb72115 log: changed implementation to use graphlog code
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 21108
diff changeset
  1722
17180
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1723
def displaygraph(ui, dag, displayer, showparents, edgefn, getrenamed=None,
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1724
                 filematcher=None):
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1725
    seen, state = [], graphmod.asciistate()
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1726
    for rev, type, ctx, parents in dag:
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1727
        char = 'o'
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1728
        if ctx.node() in showparents:
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1729
            char = '@'
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1730
        elif ctx.obsolete():
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1731
            char = 'x'
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1732
        copies = None
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1733
        if getrenamed and ctx.rev():
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1734
            copies = []
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1735
            for fn in ctx.files():
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1736
                rename = getrenamed(fn, ctx.rev())
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1737
                if rename:
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1738
                    copies.append((fn, rename[0]))
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1739
        revmatchfn = None
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1740
        if filematcher is not None:
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1741
            revmatchfn = filematcher(ctx.rev())
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1742
        displayer.show(ctx, copies=copies, matchfn=revmatchfn)
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1743
        lines = displayer.hunk.pop(rev).split('\n')
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1744
        if not lines[-1]:
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1745
            del lines[-1]
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1746
        displayer.flush(rev)
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1747
        edges = edgefn(type, char, lines, seen, rev, parents)
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1748
        for type, char, lines, coldata in edges:
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1749
            graphmod.ascii(ui, state, type, char, lines, coldata)
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1750
    displayer.close()
ae0629161090 graphlog: extract revset/support functions into cmdutil
Patrick Mezard <patrick@mezard.eu>
parents: 17059
diff changeset
  1751
17181
6f71167292f2 log: support --graph without graphlog extension
Patrick Mezard <patrick@mezard.eu>
parents: 17180
diff changeset
  1752
def graphlog(ui, repo, *pats, **opts):
6f71167292f2 log: support --graph without graphlog extension
Patrick Mezard <patrick@mezard.eu>
parents: 17180
diff changeset
  1753
    # Parameters are identical to log command ones
6f71167292f2 log: support --graph without graphlog extension
Patrick Mezard <patrick@mezard.eu>
parents: 17180
diff changeset
  1754
    revs, expr, filematcher = getgraphlogrevs(repo, pats, opts)
6f71167292f2 log: support --graph without graphlog extension
Patrick Mezard <patrick@mezard.eu>
parents: 17180
diff changeset
  1755
    revdag = graphmod.dagwalker(repo, revs)
6f71167292f2 log: support --graph without graphlog extension
Patrick Mezard <patrick@mezard.eu>
parents: 17180
diff changeset
  1756
6f71167292f2 log: support --graph without graphlog extension
Patrick Mezard <patrick@mezard.eu>
parents: 17180
diff changeset
  1757
    getrenamed = None
6f71167292f2 log: support --graph without graphlog extension
Patrick Mezard <patrick@mezard.eu>
parents: 17180
diff changeset
  1758
    if opts.get('copies'):
6f71167292f2 log: support --graph without graphlog extension
Patrick Mezard <patrick@mezard.eu>
parents: 17180
diff changeset
  1759
        endrev = None
6f71167292f2 log: support --graph without graphlog extension
Patrick Mezard <patrick@mezard.eu>
parents: 17180
diff changeset
  1760
        if opts.get('rev'):
20760
d5fa413346e7 cmdutil: changed max method for lazy call
Lucas Moscovicz <lmoscovicz@fb.com>
parents: 20759
diff changeset
  1761
            endrev = scmutil.revrange(repo, opts.get('rev')).max() + 1
17181
6f71167292f2 log: support --graph without graphlog extension
Patrick Mezard <patrick@mezard.eu>
parents: 17180
diff changeset
  1762
        getrenamed = templatekw.getrenamedfn(repo, endrev=endrev)
6f71167292f2 log: support --graph without graphlog extension
Patrick Mezard <patrick@mezard.eu>
parents: 17180
diff changeset
  1763
    displayer = show_changeset(ui, repo, opts, buffered=True)
6f71167292f2 log: support --graph without graphlog extension
Patrick Mezard <patrick@mezard.eu>
parents: 17180
diff changeset
  1764
    showparents = [ctx.node() for ctx in repo[None].parents()]
6f71167292f2 log: support --graph without graphlog extension
Patrick Mezard <patrick@mezard.eu>
parents: 17180
diff changeset
  1765
    displaygraph(ui, revdag, displayer, showparents,
6f71167292f2 log: support --graph without graphlog extension
Patrick Mezard <patrick@mezard.eu>
parents: 17180
diff changeset
  1766
                 graphmod.asciiedges, getrenamed, filematcher)
6f71167292f2 log: support --graph without graphlog extension
Patrick Mezard <patrick@mezard.eu>
parents: 17180
diff changeset
  1767
17182
cdf1532d89c6 incoming/outgoing: handle --graph in core
Patrick Mezard <patrick@mezard.eu>
parents: 17181
diff changeset
  1768
def checkunsupportedgraphflags(pats, opts):
cdf1532d89c6 incoming/outgoing: handle --graph in core
Patrick Mezard <patrick@mezard.eu>
parents: 17181
diff changeset
  1769
    for op in ["newest_first"]:
cdf1532d89c6 incoming/outgoing: handle --graph in core
Patrick Mezard <patrick@mezard.eu>
parents: 17181
diff changeset
  1770
        if op in opts and opts[op]:
cdf1532d89c6 incoming/outgoing: handle --graph in core
Patrick Mezard <patrick@mezard.eu>
parents: 17181
diff changeset
  1771
            raise util.Abort(_("-G/--graph option is incompatible with --%s")
cdf1532d89c6 incoming/outgoing: handle --graph in core
Patrick Mezard <patrick@mezard.eu>
parents: 17181
diff changeset
  1772
                             % op.replace("_", "-"))
cdf1532d89c6 incoming/outgoing: handle --graph in core
Patrick Mezard <patrick@mezard.eu>
parents: 17181
diff changeset
  1773
cdf1532d89c6 incoming/outgoing: handle --graph in core
Patrick Mezard <patrick@mezard.eu>
parents: 17181
diff changeset
  1774
def graphrevs(repo, nodes, opts):
cdf1532d89c6 incoming/outgoing: handle --graph in core
Patrick Mezard <patrick@mezard.eu>
parents: 17181
diff changeset
  1775
    limit = loglimit(opts)
cdf1532d89c6 incoming/outgoing: handle --graph in core
Patrick Mezard <patrick@mezard.eu>
parents: 17181
diff changeset
  1776
    nodes.reverse()
cdf1532d89c6 incoming/outgoing: handle --graph in core
Patrick Mezard <patrick@mezard.eu>
parents: 17181
diff changeset
  1777
    if limit is not None:
cdf1532d89c6 incoming/outgoing: handle --graph in core
Patrick Mezard <patrick@mezard.eu>
parents: 17181
diff changeset
  1778
        nodes = nodes[:limit]
cdf1532d89c6 incoming/outgoing: handle --graph in core
Patrick Mezard <patrick@mezard.eu>
parents: 17181
diff changeset
  1779
    return graphmod.nodes(repo, nodes)
cdf1532d89c6 incoming/outgoing: handle --graph in core
Patrick Mezard <patrick@mezard.eu>
parents: 17181
diff changeset
  1780
15911
c654eac03452 add: fix subrepo recursion for explicit path handling
David M. Carr <david@carrclan.us>
parents: 15907
diff changeset
  1781
def add(ui, repo, match, dryrun, listsubrepos, prefix, explicitonly):
12270
166b9866580a add: recurse into subrepositories with --subrepos/-S flag
Martin Geisler <mg@lazybytes.net>
parents: 12269
diff changeset
  1782
    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
  1783
    bad = []
877236cdd437 add: move main part to cmdutil to make it easier to reuse
Martin Geisler <mg@lazybytes.net>
parents: 12266
diff changeset
  1784
    oldbad = match.bad
877236cdd437 add: move main part to cmdutil to make it easier to reuse
Martin Geisler <mg@lazybytes.net>
parents: 12266
diff changeset
  1785
    match.bad = lambda x, y: bad.append(x) or oldbad(x, y)
877236cdd437 add: move main part to cmdutil to make it easier to reuse
Martin Geisler <mg@lazybytes.net>
parents: 12266
diff changeset
  1786
    names = []
12270
166b9866580a add: recurse into subrepositories with --subrepos/-S flag
Martin Geisler <mg@lazybytes.net>
parents: 12269
diff changeset
  1787
    wctx = repo[None]
14138
c18204fd35b0 scmutil: introduce casecollisionauditor
Adrian Buehlmann <adrian@cadifra.com>
parents: 14129
diff changeset
  1788
    cca = None
c18204fd35b0 scmutil: introduce casecollisionauditor
Adrian Buehlmann <adrian@cadifra.com>
parents: 14129
diff changeset
  1789
    abort, warn = scmutil.checkportabilityalert(ui)
c18204fd35b0 scmutil: introduce casecollisionauditor
Adrian Buehlmann <adrian@cadifra.com>
parents: 14129
diff changeset
  1790
    if abort or warn:
17201
afd75476939e scmutil: 25% speedup in casecollisionauditor
Joshua Redstone <joshua.redstone@fb.com>
parents: 17182
diff changeset
  1791
        cca = scmutil.casecollisionauditor(ui, abort, repo.dirstate)
12269
877236cdd437 add: move main part to cmdutil to make it easier to reuse
Martin Geisler <mg@lazybytes.net>
parents: 12266
diff changeset
  1792
    for f in repo.walk(match):
877236cdd437 add: move main part to cmdutil to make it easier to reuse
Martin Geisler <mg@lazybytes.net>
parents: 12266
diff changeset
  1793
        exact = match.exact(f)
15911
c654eac03452 add: fix subrepo recursion for explicit path handling
David M. Carr <david@carrclan.us>
parents: 15907
diff changeset
  1794
        if exact or not explicitonly and f not in repo.dirstate:
14138
c18204fd35b0 scmutil: introduce casecollisionauditor
Adrian Buehlmann <adrian@cadifra.com>
parents: 14129
diff changeset
  1795
            if cca:
c18204fd35b0 scmutil: introduce casecollisionauditor
Adrian Buehlmann <adrian@cadifra.com>
parents: 14129
diff changeset
  1796
                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
  1797
            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
  1798
            if ui.verbose or not exact:
12270
166b9866580a add: recurse into subrepositories with --subrepos/-S flag
Martin Geisler <mg@lazybytes.net>
parents: 12269
diff changeset
  1799
                ui.status(_('adding %s\n') % match.rel(join(f)))
166b9866580a add: recurse into subrepositories with --subrepos/-S flag
Martin Geisler <mg@lazybytes.net>
parents: 12269
diff changeset
  1800
18364
6252b4f1c4b4 subrepos: process subrepos in sorted order
Mads Kiilerich <mads@kiilerich.com>
parents: 18340
diff changeset
  1801
    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
  1802
        sub = wctx.sub(subpath)
9e99d2bbb1b1 add: support adding explicit files in subrepos
David M. Carr <david@carrclan.us>
parents: 15231
diff changeset
  1803
        try:
9e99d2bbb1b1 add: support adding explicit files in subrepos
David M. Carr <david@carrclan.us>
parents: 15231
diff changeset
  1804
            submatch = matchmod.narrowmatcher(subpath, match)
9e99d2bbb1b1 add: support adding explicit files in subrepos
David M. Carr <david@carrclan.us>
parents: 15231
diff changeset
  1805
            if listsubrepos:
15911
c654eac03452 add: fix subrepo recursion for explicit path handling
David M. Carr <david@carrclan.us>
parents: 15907
diff changeset
  1806
                bad.extend(sub.add(ui, submatch, dryrun, listsubrepos, prefix,
c654eac03452 add: fix subrepo recursion for explicit path handling
David M. Carr <david@carrclan.us>
parents: 15907
diff changeset
  1807
                                   False))
15410
9e99d2bbb1b1 add: support adding explicit files in subrepos
David M. Carr <david@carrclan.us>
parents: 15231
diff changeset
  1808
            else:
15911
c654eac03452 add: fix subrepo recursion for explicit path handling
David M. Carr <david@carrclan.us>
parents: 15907
diff changeset
  1809
                bad.extend(sub.add(ui, submatch, dryrun, listsubrepos, prefix,
c654eac03452 add: fix subrepo recursion for explicit path handling
David M. Carr <david@carrclan.us>
parents: 15907
diff changeset
  1810
                                   True))
15410
9e99d2bbb1b1 add: support adding explicit files in subrepos
David M. Carr <david@carrclan.us>
parents: 15231
diff changeset
  1811
        except error.LookupError:
9e99d2bbb1b1 add: support adding explicit files in subrepos
David M. Carr <david@carrclan.us>
parents: 15231
diff changeset
  1812
            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
  1813
                           % join(subpath))
12270
166b9866580a add: recurse into subrepositories with --subrepos/-S flag
Martin Geisler <mg@lazybytes.net>
parents: 12269
diff changeset
  1814
12269
877236cdd437 add: move main part to cmdutil to make it easier to reuse
Martin Geisler <mg@lazybytes.net>
parents: 12266
diff changeset
  1815
    if not dryrun:
12270
166b9866580a add: recurse into subrepositories with --subrepos/-S flag
Martin Geisler <mg@lazybytes.net>
parents: 12269
diff changeset
  1816
        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
  1817
        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
  1818
    return bad
877236cdd437 add: move main part to cmdutil to make it easier to reuse
Martin Geisler <mg@lazybytes.net>
parents: 12266
diff changeset
  1819
15912
2bd54ffaa27e forget: fix subrepo recursion for explicit path handling
David M. Carr <david@carrclan.us>
parents: 15911
diff changeset
  1820
def forget(ui, repo, match, prefix, explicitonly):
2bd54ffaa27e forget: fix subrepo recursion for explicit path handling
David M. Carr <david@carrclan.us>
parents: 15911
diff changeset
  1821
    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
  1822
    bad = []
2bd54ffaa27e forget: fix subrepo recursion for explicit path handling
David M. Carr <david@carrclan.us>
parents: 15911
diff changeset
  1823
    oldbad = match.bad
2bd54ffaa27e forget: fix subrepo recursion for explicit path handling
David M. Carr <david@carrclan.us>
parents: 15911
diff changeset
  1824
    match.bad = lambda x, y: bad.append(x) or oldbad(x, y)
2bd54ffaa27e forget: fix subrepo recursion for explicit path handling
David M. Carr <david@carrclan.us>
parents: 15911
diff changeset
  1825
    wctx = repo[None]
2bd54ffaa27e forget: fix subrepo recursion for explicit path handling
David M. Carr <david@carrclan.us>
parents: 15911
diff changeset
  1826
    forgot = []
2bd54ffaa27e forget: fix subrepo recursion for explicit path handling
David M. Carr <david@carrclan.us>
parents: 15911
diff changeset
  1827
    s = repo.status(match=match, clean=True)
2bd54ffaa27e forget: fix subrepo recursion for explicit path handling
David M. Carr <david@carrclan.us>
parents: 15911
diff changeset
  1828
    forget = sorted(s[0] + s[1] + s[3] + s[6])
2bd54ffaa27e forget: fix subrepo recursion for explicit path handling
David M. Carr <david@carrclan.us>
parents: 15911
diff changeset
  1829
    if explicitonly:
2bd54ffaa27e forget: fix subrepo recursion for explicit path handling
David M. Carr <david@carrclan.us>
parents: 15911
diff changeset
  1830
        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
  1831
18364
6252b4f1c4b4 subrepos: process subrepos in sorted order
Mads Kiilerich <mads@kiilerich.com>
parents: 18340
diff changeset
  1832
    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
  1833
        sub = wctx.sub(subpath)
2bd54ffaa27e forget: fix subrepo recursion for explicit path handling
David M. Carr <david@carrclan.us>
parents: 15911
diff changeset
  1834
        try:
2bd54ffaa27e forget: fix subrepo recursion for explicit path handling
David M. Carr <david@carrclan.us>
parents: 15911
diff changeset
  1835
            submatch = matchmod.narrowmatcher(subpath, match)
2bd54ffaa27e forget: fix subrepo recursion for explicit path handling
David M. Carr <david@carrclan.us>
parents: 15911
diff changeset
  1836
            subbad, subforgot = sub.forget(ui, submatch, prefix)
2bd54ffaa27e forget: fix subrepo recursion for explicit path handling
David M. Carr <david@carrclan.us>
parents: 15911
diff changeset
  1837
            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
  1838
            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
  1839
        except error.LookupError:
2bd54ffaa27e forget: fix subrepo recursion for explicit path handling
David M. Carr <david@carrclan.us>
parents: 15911
diff changeset
  1840
            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
  1841
                           % join(subpath))
2bd54ffaa27e forget: fix subrepo recursion for explicit path handling
David M. Carr <david@carrclan.us>
parents: 15911
diff changeset
  1842
16070
f11eee00c652 forget: show warning messages for forgetting in subrepo correctly
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15912
diff changeset
  1843
    if not explicitonly:
f11eee00c652 forget: show warning messages for forgetting in subrepo correctly
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15912
diff changeset
  1844
        for f in match.files():
15912
2bd54ffaa27e forget: fix subrepo recursion for explicit path handling
David M. Carr <david@carrclan.us>
parents: 15911
diff changeset
  1845
            if f not in repo.dirstate and not os.path.isdir(match.rel(join(f))):
2bd54ffaa27e forget: fix subrepo recursion for explicit path handling
David M. Carr <david@carrclan.us>
parents: 15911
diff changeset
  1846
                if f not in forgot:
2bd54ffaa27e forget: fix subrepo recursion for explicit path handling
David M. Carr <david@carrclan.us>
parents: 15911
diff changeset
  1847
                    if os.path.exists(match.rel(join(f))):
2bd54ffaa27e forget: fix subrepo recursion for explicit path handling
David M. Carr <david@carrclan.us>
parents: 15911
diff changeset
  1848
                        ui.warn(_('not removing %s: '
2bd54ffaa27e forget: fix subrepo recursion for explicit path handling
David M. Carr <david@carrclan.us>
parents: 15911
diff changeset
  1849
                                  'file is already untracked\n')
2bd54ffaa27e forget: fix subrepo recursion for explicit path handling
David M. Carr <david@carrclan.us>
parents: 15911
diff changeset
  1850
                                % match.rel(join(f)))
2bd54ffaa27e forget: fix subrepo recursion for explicit path handling
David M. Carr <david@carrclan.us>
parents: 15911
diff changeset
  1851
                    bad.append(f)
2bd54ffaa27e forget: fix subrepo recursion for explicit path handling
David M. Carr <david@carrclan.us>
parents: 15911
diff changeset
  1852
2bd54ffaa27e forget: fix subrepo recursion for explicit path handling
David M. Carr <david@carrclan.us>
parents: 15911
diff changeset
  1853
    for f in forget:
2bd54ffaa27e forget: fix subrepo recursion for explicit path handling
David M. Carr <david@carrclan.us>
parents: 15911
diff changeset
  1854
        if ui.verbose or not match.exact(f):
2bd54ffaa27e forget: fix subrepo recursion for explicit path handling
David M. Carr <david@carrclan.us>
parents: 15911
diff changeset
  1855
            ui.status(_('removing %s\n') % match.rel(join(f)))
2bd54ffaa27e forget: fix subrepo recursion for explicit path handling
David M. Carr <david@carrclan.us>
parents: 15911
diff changeset
  1856
2bd54ffaa27e forget: fix subrepo recursion for explicit path handling
David M. Carr <david@carrclan.us>
parents: 15911
diff changeset
  1857
    rejected = wctx.forget(forget, prefix)
2bd54ffaa27e forget: fix subrepo recursion for explicit path handling
David M. Carr <david@carrclan.us>
parents: 15911
diff changeset
  1858
    bad.extend(f for f in rejected if f in match.files())
2bd54ffaa27e forget: fix subrepo recursion for explicit path handling
David M. Carr <david@carrclan.us>
parents: 15911
diff changeset
  1859
    forgot.extend(forget)
2bd54ffaa27e forget: fix subrepo recursion for explicit path handling
David M. Carr <david@carrclan.us>
parents: 15911
diff changeset
  1860
    return bad, forgot
2bd54ffaa27e forget: fix subrepo recursion for explicit path handling
David M. Carr <david@carrclan.us>
parents: 15911
diff changeset
  1861
21041
a2cc3c08c3ac cat: support cat with explicit paths in subrepos
Matt Harbison <matt_harbison@yahoo.com>
parents: 21040
diff changeset
  1862
def cat(ui, repo, ctx, matcher, prefix, **opts):
21040
bdf5ed5246d2 cat: move most of the implementation into cmdutils.cat()
Matt Harbison <matt_harbison@yahoo.com>
parents: 21036
diff changeset
  1863
    err = 1
bdf5ed5246d2 cat: move most of the implementation into cmdutils.cat()
Matt Harbison <matt_harbison@yahoo.com>
parents: 21036
diff changeset
  1864
bdf5ed5246d2 cat: move most of the implementation into cmdutils.cat()
Matt Harbison <matt_harbison@yahoo.com>
parents: 21036
diff changeset
  1865
    def write(path):
21041
a2cc3c08c3ac cat: support cat with explicit paths in subrepos
Matt Harbison <matt_harbison@yahoo.com>
parents: 21040
diff changeset
  1866
        fp = makefileobj(repo, opts.get('output'), ctx.node(),
a2cc3c08c3ac cat: support cat with explicit paths in subrepos
Matt Harbison <matt_harbison@yahoo.com>
parents: 21040
diff changeset
  1867
                         pathname=os.path.join(prefix, path))
21040
bdf5ed5246d2 cat: move most of the implementation into cmdutils.cat()
Matt Harbison <matt_harbison@yahoo.com>
parents: 21036
diff changeset
  1868
        data = ctx[path].data()
bdf5ed5246d2 cat: move most of the implementation into cmdutils.cat()
Matt Harbison <matt_harbison@yahoo.com>
parents: 21036
diff changeset
  1869
        if opts.get('decode'):
bdf5ed5246d2 cat: move most of the implementation into cmdutils.cat()
Matt Harbison <matt_harbison@yahoo.com>
parents: 21036
diff changeset
  1870
            data = repo.wwritedata(path, data)
bdf5ed5246d2 cat: move most of the implementation into cmdutils.cat()
Matt Harbison <matt_harbison@yahoo.com>
parents: 21036
diff changeset
  1871
        fp.write(data)
bdf5ed5246d2 cat: move most of the implementation into cmdutils.cat()
Matt Harbison <matt_harbison@yahoo.com>
parents: 21036
diff changeset
  1872
        fp.close()
bdf5ed5246d2 cat: move most of the implementation into cmdutils.cat()
Matt Harbison <matt_harbison@yahoo.com>
parents: 21036
diff changeset
  1873
bdf5ed5246d2 cat: move most of the implementation into cmdutils.cat()
Matt Harbison <matt_harbison@yahoo.com>
parents: 21036
diff changeset
  1874
    # 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
  1875
    # 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
  1876
    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
  1877
        file = matcher.files()[0]
bdf5ed5246d2 cat: move most of the implementation into cmdutils.cat()
Matt Harbison <matt_harbison@yahoo.com>
parents: 21036
diff changeset
  1878
        mf = repo.manifest
bdf5ed5246d2 cat: move most of the implementation into cmdutils.cat()
Matt Harbison <matt_harbison@yahoo.com>
parents: 21036
diff changeset
  1879
        mfnode = ctx._changeset[0]
bdf5ed5246d2 cat: move most of the implementation into cmdutils.cat()
Matt Harbison <matt_harbison@yahoo.com>
parents: 21036
diff changeset
  1880
        if mf.find(mfnode, file)[0]:
bdf5ed5246d2 cat: move most of the implementation into cmdutils.cat()
Matt Harbison <matt_harbison@yahoo.com>
parents: 21036
diff changeset
  1881
            write(file)
bdf5ed5246d2 cat: move most of the implementation into cmdutils.cat()
Matt Harbison <matt_harbison@yahoo.com>
parents: 21036
diff changeset
  1882
            return 0
bdf5ed5246d2 cat: move most of the implementation into cmdutils.cat()
Matt Harbison <matt_harbison@yahoo.com>
parents: 21036
diff changeset
  1883
21041
a2cc3c08c3ac cat: support cat with explicit paths in subrepos
Matt Harbison <matt_harbison@yahoo.com>
parents: 21040
diff changeset
  1884
    # Don't warn about "missing" files that are really in subrepos
a2cc3c08c3ac cat: support cat with explicit paths in subrepos
Matt Harbison <matt_harbison@yahoo.com>
parents: 21040
diff changeset
  1885
    bad = matcher.bad
a2cc3c08c3ac cat: support cat with explicit paths in subrepos
Matt Harbison <matt_harbison@yahoo.com>
parents: 21040
diff changeset
  1886
a2cc3c08c3ac cat: support cat with explicit paths in subrepos
Matt Harbison <matt_harbison@yahoo.com>
parents: 21040
diff changeset
  1887
    def badfn(path, msg):
a2cc3c08c3ac cat: support cat with explicit paths in subrepos
Matt Harbison <matt_harbison@yahoo.com>
parents: 21040
diff changeset
  1888
        for subpath in ctx.substate:
a2cc3c08c3ac cat: support cat with explicit paths in subrepos
Matt Harbison <matt_harbison@yahoo.com>
parents: 21040
diff changeset
  1889
            if path.startswith(subpath):
a2cc3c08c3ac cat: support cat with explicit paths in subrepos
Matt Harbison <matt_harbison@yahoo.com>
parents: 21040
diff changeset
  1890
                return
a2cc3c08c3ac cat: support cat with explicit paths in subrepos
Matt Harbison <matt_harbison@yahoo.com>
parents: 21040
diff changeset
  1891
        bad(path, msg)
a2cc3c08c3ac cat: support cat with explicit paths in subrepos
Matt Harbison <matt_harbison@yahoo.com>
parents: 21040
diff changeset
  1892
a2cc3c08c3ac cat: support cat with explicit paths in subrepos
Matt Harbison <matt_harbison@yahoo.com>
parents: 21040
diff changeset
  1893
    matcher.bad = badfn
a2cc3c08c3ac cat: support cat with explicit paths in subrepos
Matt Harbison <matt_harbison@yahoo.com>
parents: 21040
diff changeset
  1894
21040
bdf5ed5246d2 cat: move most of the implementation into cmdutils.cat()
Matt Harbison <matt_harbison@yahoo.com>
parents: 21036
diff changeset
  1895
    for abs in ctx.walk(matcher):
bdf5ed5246d2 cat: move most of the implementation into cmdutils.cat()
Matt Harbison <matt_harbison@yahoo.com>
parents: 21036
diff changeset
  1896
        write(abs)
bdf5ed5246d2 cat: move most of the implementation into cmdutils.cat()
Matt Harbison <matt_harbison@yahoo.com>
parents: 21036
diff changeset
  1897
        err = 0
21041
a2cc3c08c3ac cat: support cat with explicit paths in subrepos
Matt Harbison <matt_harbison@yahoo.com>
parents: 21040
diff changeset
  1898
a2cc3c08c3ac cat: support cat with explicit paths in subrepos
Matt Harbison <matt_harbison@yahoo.com>
parents: 21040
diff changeset
  1899
    matcher.bad = bad
a2cc3c08c3ac cat: support cat with explicit paths in subrepos
Matt Harbison <matt_harbison@yahoo.com>
parents: 21040
diff changeset
  1900
a2cc3c08c3ac cat: support cat with explicit paths in subrepos
Matt Harbison <matt_harbison@yahoo.com>
parents: 21040
diff changeset
  1901
    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
  1902
        sub = ctx.sub(subpath)
a2cc3c08c3ac cat: support cat with explicit paths in subrepos
Matt Harbison <matt_harbison@yahoo.com>
parents: 21040
diff changeset
  1903
        try:
a2cc3c08c3ac cat: support cat with explicit paths in subrepos
Matt Harbison <matt_harbison@yahoo.com>
parents: 21040
diff changeset
  1904
            submatch = matchmod.narrowmatcher(subpath, matcher)
a2cc3c08c3ac cat: support cat with explicit paths in subrepos
Matt Harbison <matt_harbison@yahoo.com>
parents: 21040
diff changeset
  1905
a2cc3c08c3ac cat: support cat with explicit paths in subrepos
Matt Harbison <matt_harbison@yahoo.com>
parents: 21040
diff changeset
  1906
            if not sub.cat(ui, submatch, os.path.join(prefix, sub._path),
a2cc3c08c3ac cat: support cat with explicit paths in subrepos
Matt Harbison <matt_harbison@yahoo.com>
parents: 21040
diff changeset
  1907
                           **opts):
a2cc3c08c3ac cat: support cat with explicit paths in subrepos
Matt Harbison <matt_harbison@yahoo.com>
parents: 21040
diff changeset
  1908
                err = 0
a2cc3c08c3ac cat: support cat with explicit paths in subrepos
Matt Harbison <matt_harbison@yahoo.com>
parents: 21040
diff changeset
  1909
        except error.RepoLookupError:
a2cc3c08c3ac cat: support cat with explicit paths in subrepos
Matt Harbison <matt_harbison@yahoo.com>
parents: 21040
diff changeset
  1910
            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
  1911
                           % os.path.join(prefix, subpath))
a2cc3c08c3ac cat: support cat with explicit paths in subrepos
Matt Harbison <matt_harbison@yahoo.com>
parents: 21040
diff changeset
  1912
21040
bdf5ed5246d2 cat: move most of the implementation into cmdutils.cat()
Matt Harbison <matt_harbison@yahoo.com>
parents: 21036
diff changeset
  1913
    return err
bdf5ed5246d2 cat: move most of the implementation into cmdutils.cat()
Matt Harbison <matt_harbison@yahoo.com>
parents: 21036
diff changeset
  1914
18852
300844cb1a56 duplicatecopies: fix arg name and docstring
Siddharth Agarwal <sid0@fb.com>
parents: 18710
diff changeset
  1915
def duplicatecopies(repo, rev, fromrev):
300844cb1a56 duplicatecopies: fix arg name and docstring
Siddharth Agarwal <sid0@fb.com>
parents: 18710
diff changeset
  1916
    '''reproduce copies from fromrev to rev in the dirstate'''
300844cb1a56 duplicatecopies: fix arg name and docstring
Siddharth Agarwal <sid0@fb.com>
parents: 18710
diff changeset
  1917
    for dst, src in copies.pathcopies(repo[fromrev], repo[rev]).iteritems():
18853
78d760aa3607 duplicatecopies: do not mark items not in the dirstate as copies
Siddharth Agarwal <sid0@fb.com>
parents: 18852
diff changeset
  1918
        # copies.pathcopies returns backward renames, so dst might not
78d760aa3607 duplicatecopies: do not mark items not in the dirstate as copies
Siddharth Agarwal <sid0@fb.com>
parents: 18852
diff changeset
  1919
        # actually be in the dirstate
78d760aa3607 duplicatecopies: do not mark items not in the dirstate as copies
Siddharth Agarwal <sid0@fb.com>
parents: 18852
diff changeset
  1920
        if repo.dirstate[dst] in "nma":
78d760aa3607 duplicatecopies: do not mark items not in the dirstate as copies
Siddharth Agarwal <sid0@fb.com>
parents: 18852
diff changeset
  1921
            repo.dirstate.copy(src, dst)
15214
231aac5280ba rebase: move updatedirstate into cmdutil so it can be shared
Matt Mackall <mpm@selenic.com>
parents: 14986
diff changeset
  1922
5034
c0417a319e39 commands: move commit to cmdutil as wrapper for commit-like functions
Bryan O'Sullivan <bos@serpentine.com>
parents: 4965
diff changeset
  1923
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
  1924
    '''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
  1925
    date = opts.get('date')
989467e8e3a9 Fix bad behaviour when specifying an invalid date (issue700)
Thomas Arendsen Hein <thomas@intevation.de>
parents: 6112
diff changeset
  1926
    if date:
989467e8e3a9 Fix bad behaviour when specifying an invalid date (issue700)
Thomas Arendsen Hein <thomas@intevation.de>
parents: 6112
diff changeset
  1927
        opts['date'] = util.parsedate(date)
14635
217b7d83afc3 cmdutil, logmessage: use ui.fin when reading from '-'
Idan Kamara <idankk86@gmail.com>
parents: 14518
diff changeset
  1928
    message = logmessage(ui, opts)
5034
c0417a319e39 commands: move commit to cmdutil as wrapper for commit-like functions
Bryan O'Sullivan <bos@serpentine.com>
parents: 4965
diff changeset
  1929
5829
784073457a0f cmdutil.commit: extract 'addremove' from opts carefully
Kirill Smelkov <kirr@mns.spb.ru>
parents: 5797
diff changeset
  1930
    # 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
  1931
    # that doesn't support addremove
784073457a0f cmdutil.commit: extract 'addremove' from opts carefully
Kirill Smelkov <kirr@mns.spb.ru>
parents: 5797
diff changeset
  1932
    if opts.get('addremove'):
14321
003d63bb4fa5 scmutil: drop some aliases in cmdutil
Matt Mackall <mpm@selenic.com>
parents: 14320
diff changeset
  1933
        scmutil.addremove(repo, pats, opts)
5829
784073457a0f cmdutil.commit: extract 'addremove' from opts carefully
Kirill Smelkov <kirr@mns.spb.ru>
parents: 5797
diff changeset
  1934
14671
35c2cc322ba8 scmutil: switch match users to supplying contexts
Matt Mackall <mpm@selenic.com>
parents: 14638
diff changeset
  1935
    return commitfunc(ui, repo, message,
35c2cc322ba8 scmutil: switch match users to supplying contexts
Matt Mackall <mpm@selenic.com>
parents: 14638
diff changeset
  1936
                      scmutil.match(repo[None], pats, opts), opts)
8407
223000a687b0 commit: move commit editor to cmdutil, pass as function
Matt Mackall <mpm@selenic.com>
parents: 8390
diff changeset
  1937
16458
55982f62651f commit: add option to amend the working dir parent
Idan Kamara <idankk86@gmail.com>
parents: 16430
diff changeset
  1938
def amend(ui, repo, commitfunc, old, extra, pats, opts):
55982f62651f commit: add option to amend the working dir parent
Idan Kamara <idankk86@gmail.com>
parents: 16430
diff changeset
  1939
    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
  1940
    base = old.p1()
55982f62651f commit: add option to amend the working dir parent
Idan Kamara <idankk86@gmail.com>
parents: 16430
diff changeset
  1941
18197
153659e86a5f amend: invalidate dirstate in case of failure (issue3670)
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 18006
diff changeset
  1942
    wlock = lock = newid = None
16458
55982f62651f commit: add option to amend the working dir parent
Idan Kamara <idankk86@gmail.com>
parents: 16430
diff changeset
  1943
    try:
17471
ad1561723dde amend: lock the repository during the whole process
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17468
diff changeset
  1944
        wlock = repo.wlock()
ad1561723dde amend: lock the repository during the whole process
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17468
diff changeset
  1945
        lock = repo.lock()
17472
965fbe04fd96 amend: wrap all commit operations in a single transaction
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17471
diff changeset
  1946
        tr = repo.transaction('amend')
17049
2440822446ce amend: disable hooks when creating intermediate commit (issue3501)
Idan Kamara <idankk86@gmail.com>
parents: 16678
diff changeset
  1947
        try:
17473
9732473aa24b amend: use an explicit commit message for temporary amending commit
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 17472
diff changeset
  1948
            # See if we got a message from -m or -l, if not, open the editor
9732473aa24b amend: use an explicit commit message for temporary amending commit
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 17472
diff changeset
  1949
            # with the message of the changeset to amend
9732473aa24b amend: use an explicit commit message for temporary amending commit
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 17472
diff changeset
  1950
            message = logmessage(ui, opts)
17863
034e55bbf7c0 amend: fix incompatibity between logfile and message option (issue3675)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 17812
diff changeset
  1951
            # ensure logfile does not conflict with later enforcement of the
034e55bbf7c0 amend: fix incompatibity between logfile and message option (issue3675)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 17812
diff changeset
  1952
            # message. potential logfile content has been processed by
034e55bbf7c0 amend: fix incompatibity between logfile and message option (issue3675)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 17812
diff changeset
  1953
            # `logmessage` anyway.
034e55bbf7c0 amend: fix incompatibity between logfile and message option (issue3675)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 17812
diff changeset
  1954
            opts.pop('logfile')
17472
965fbe04fd96 amend: wrap all commit operations in a single transaction
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17471
diff changeset
  1955
            # First, do a regular commit to record all changes in the working
965fbe04fd96 amend: wrap all commit operations in a single transaction
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17471
diff changeset
  1956
            # directory (if there are any)
965fbe04fd96 amend: wrap all commit operations in a single transaction
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17471
diff changeset
  1957
            ui.callhooks = False
18198
9b4adaef0db9 amend: prevent loss of bookmark on failed amend
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 18197
diff changeset
  1958
            currentbookmark = repo._bookmarkcurrent
17472
965fbe04fd96 amend: wrap all commit operations in a single transaction
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17471
diff changeset
  1959
            try:
18198
9b4adaef0db9 amend: prevent loss of bookmark on failed amend
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 18197
diff changeset
  1960
                repo._bookmarkcurrent = None
17473
9732473aa24b amend: use an explicit commit message for temporary amending commit
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 17472
diff changeset
  1961
                opts['message'] = 'temporary amend commit for %s' % old
17472
965fbe04fd96 amend: wrap all commit operations in a single transaction
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17471
diff changeset
  1962
                node = commit(ui, repo, commitfunc, pats, opts)
965fbe04fd96 amend: wrap all commit operations in a single transaction
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17471
diff changeset
  1963
            finally:
18198
9b4adaef0db9 amend: prevent loss of bookmark on failed amend
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 18197
diff changeset
  1964
                repo._bookmarkcurrent = currentbookmark
17472
965fbe04fd96 amend: wrap all commit operations in a single transaction
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17471
diff changeset
  1965
                ui.callhooks = True
965fbe04fd96 amend: wrap all commit operations in a single transaction
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17471
diff changeset
  1966
            ctx = repo[node]
16458
55982f62651f commit: add option to amend the working dir parent
Idan Kamara <idankk86@gmail.com>
parents: 16430
diff changeset
  1967
17472
965fbe04fd96 amend: wrap all commit operations in a single transaction
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17471
diff changeset
  1968
            # Participating changesets:
965fbe04fd96 amend: wrap all commit operations in a single transaction
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17471
diff changeset
  1969
            #
965fbe04fd96 amend: wrap all commit operations in a single transaction
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17471
diff changeset
  1970
            # node/ctx o - new (intermediate) commit that contains changes
965fbe04fd96 amend: wrap all commit operations in a single transaction
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17471
diff changeset
  1971
            #          |   from working dir to go into amending commit
965fbe04fd96 amend: wrap all commit operations in a single transaction
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17471
diff changeset
  1972
            #          |   (or a workingctx if there were no changes)
965fbe04fd96 amend: wrap all commit operations in a single transaction
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17471
diff changeset
  1973
            #          |
965fbe04fd96 amend: wrap all commit operations in a single transaction
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17471
diff changeset
  1974
            # old      o - changeset to amend
965fbe04fd96 amend: wrap all commit operations in a single transaction
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17471
diff changeset
  1975
            #          |
965fbe04fd96 amend: wrap all commit operations in a single transaction
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17471
diff changeset
  1976
            # base     o - parent of amending changeset
16458
55982f62651f commit: add option to amend the working dir parent
Idan Kamara <idankk86@gmail.com>
parents: 16430
diff changeset
  1977
17472
965fbe04fd96 amend: wrap all commit operations in a single transaction
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17471
diff changeset
  1978
            # Update extra dict from amended commit (e.g. to preserve graft
965fbe04fd96 amend: wrap all commit operations in a single transaction
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17471
diff changeset
  1979
            # source)
965fbe04fd96 amend: wrap all commit operations in a single transaction
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17471
diff changeset
  1980
            extra.update(old.extra())
16630
f30226b1a46a amend: preserve extra dict (issue3430)
Idan Kamara <idankk86@gmail.com>
parents: 16553
diff changeset
  1981
17472
965fbe04fd96 amend: wrap all commit operations in a single transaction
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17471
diff changeset
  1982
            # Also update it from the intermediate commit or from the wctx
965fbe04fd96 amend: wrap all commit operations in a single transaction
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17471
diff changeset
  1983
            extra.update(ctx.extra())
16630
f30226b1a46a amend: preserve extra dict (issue3430)
Idan Kamara <idankk86@gmail.com>
parents: 16553
diff changeset
  1984
18909
3a72c89a83ec amend: support amending merge changesets (issue3778)
Brodie Rao <brodie@sf.io>
parents: 18853
diff changeset
  1985
            if len(old.parents()) > 1:
3a72c89a83ec amend: support amending merge changesets (issue3778)
Brodie Rao <brodie@sf.io>
parents: 18853
diff changeset
  1986
                # ctx.files() isn't reliable for merges, so fall back to the
3a72c89a83ec amend: support amending merge changesets (issue3778)
Brodie Rao <brodie@sf.io>
parents: 18853
diff changeset
  1987
                # slower repo.status() method
3a72c89a83ec amend: support amending merge changesets (issue3778)
Brodie Rao <brodie@sf.io>
parents: 18853
diff changeset
  1988
                files = set([fn for st in repo.status(base, old)[:3]
3a72c89a83ec amend: support amending merge changesets (issue3778)
Brodie Rao <brodie@sf.io>
parents: 18853
diff changeset
  1989
                             for fn in st])
3a72c89a83ec amend: support amending merge changesets (issue3778)
Brodie Rao <brodie@sf.io>
parents: 18853
diff changeset
  1990
            else:
3a72c89a83ec amend: support amending merge changesets (issue3778)
Brodie Rao <brodie@sf.io>
parents: 18853
diff changeset
  1991
                files = set(old.files())
16458
55982f62651f commit: add option to amend the working dir parent
Idan Kamara <idankk86@gmail.com>
parents: 16430
diff changeset
  1992
17472
965fbe04fd96 amend: wrap all commit operations in a single transaction
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17471
diff changeset
  1993
            # Second, we use either the commit we just did, or if there were no
965fbe04fd96 amend: wrap all commit operations in a single transaction
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17471
diff changeset
  1994
            # changes the parent of the working directory as the version of the
965fbe04fd96 amend: wrap all commit operations in a single transaction
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17471
diff changeset
  1995
            # files in the final amend commit
965fbe04fd96 amend: wrap all commit operations in a single transaction
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17471
diff changeset
  1996
            if node:
965fbe04fd96 amend: wrap all commit operations in a single transaction
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17471
diff changeset
  1997
                ui.note(_('copying changeset %s to %s\n') % (ctx, base))
16458
55982f62651f commit: add option to amend the working dir parent
Idan Kamara <idankk86@gmail.com>
parents: 16430
diff changeset
  1998
17472
965fbe04fd96 amend: wrap all commit operations in a single transaction
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17471
diff changeset
  1999
                user = ctx.user()
965fbe04fd96 amend: wrap all commit operations in a single transaction
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17471
diff changeset
  2000
                date = ctx.date()
965fbe04fd96 amend: wrap all commit operations in a single transaction
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17471
diff changeset
  2001
                # Recompute copies (avoid recording a -> b -> a)
965fbe04fd96 amend: wrap all commit operations in a single transaction
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17471
diff changeset
  2002
                copied = copies.pathcopies(base, ctx)
16458
55982f62651f commit: add option to amend the working dir parent
Idan Kamara <idankk86@gmail.com>
parents: 16430
diff changeset
  2003
17472
965fbe04fd96 amend: wrap all commit operations in a single transaction
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17471
diff changeset
  2004
                # Prune files which were reverted by the updates: if old
965fbe04fd96 amend: wrap all commit operations in a single transaction
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17471
diff changeset
  2005
                # introduced file X and our intermediate commit, node,
965fbe04fd96 amend: wrap all commit operations in a single transaction
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17471
diff changeset
  2006
                # renamed that file, then those two files are the same and
965fbe04fd96 amend: wrap all commit operations in a single transaction
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17471
diff changeset
  2007
                # we can discard X from our list of files. Likewise if X
965fbe04fd96 amend: wrap all commit operations in a single transaction
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17471
diff changeset
  2008
                # was deleted, it's no longer relevant
965fbe04fd96 amend: wrap all commit operations in a single transaction
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17471
diff changeset
  2009
                files.update(ctx.files())
16458
55982f62651f commit: add option to amend the working dir parent
Idan Kamara <idankk86@gmail.com>
parents: 16430
diff changeset
  2010
17472
965fbe04fd96 amend: wrap all commit operations in a single transaction
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17471
diff changeset
  2011
                def samefile(f):
965fbe04fd96 amend: wrap all commit operations in a single transaction
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17471
diff changeset
  2012
                    if f in ctx.manifest():
965fbe04fd96 amend: wrap all commit operations in a single transaction
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17471
diff changeset
  2013
                        a = ctx.filectx(f)
965fbe04fd96 amend: wrap all commit operations in a single transaction
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17471
diff changeset
  2014
                        if f in base.manifest():
965fbe04fd96 amend: wrap all commit operations in a single transaction
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17471
diff changeset
  2015
                            b = base.filectx(f)
965fbe04fd96 amend: wrap all commit operations in a single transaction
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17471
diff changeset
  2016
                            return (not a.cmp(b)
965fbe04fd96 amend: wrap all commit operations in a single transaction
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17471
diff changeset
  2017
                                    and a.flags() == b.flags())
965fbe04fd96 amend: wrap all commit operations in a single transaction
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17471
diff changeset
  2018
                        else:
965fbe04fd96 amend: wrap all commit operations in a single transaction
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17471
diff changeset
  2019
                            return False
16458
55982f62651f commit: add option to amend the working dir parent
Idan Kamara <idankk86@gmail.com>
parents: 16430
diff changeset
  2020
                    else:
17472
965fbe04fd96 amend: wrap all commit operations in a single transaction
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17471
diff changeset
  2021
                        return f not in base.manifest()
965fbe04fd96 amend: wrap all commit operations in a single transaction
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17471
diff changeset
  2022
                files = [f for f in files if not samefile(f)]
16458
55982f62651f commit: add option to amend the working dir parent
Idan Kamara <idankk86@gmail.com>
parents: 16430
diff changeset
  2023
17472
965fbe04fd96 amend: wrap all commit operations in a single transaction
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17471
diff changeset
  2024
                def filectxfn(repo, ctx_, path):
965fbe04fd96 amend: wrap all commit operations in a single transaction
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17471
diff changeset
  2025
                    try:
965fbe04fd96 amend: wrap all commit operations in a single transaction
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17471
diff changeset
  2026
                        fctx = ctx[path]
965fbe04fd96 amend: wrap all commit operations in a single transaction
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17471
diff changeset
  2027
                        flags = fctx.flags()
965fbe04fd96 amend: wrap all commit operations in a single transaction
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17471
diff changeset
  2028
                        mctx = context.memfilectx(fctx.path(), fctx.data(),
965fbe04fd96 amend: wrap all commit operations in a single transaction
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17471
diff changeset
  2029
                                                  islink='l' in flags,
965fbe04fd96 amend: wrap all commit operations in a single transaction
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17471
diff changeset
  2030
                                                  isexec='x' in flags,
965fbe04fd96 amend: wrap all commit operations in a single transaction
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17471
diff changeset
  2031
                                                  copied=copied.get(path))
965fbe04fd96 amend: wrap all commit operations in a single transaction
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17471
diff changeset
  2032
                        return mctx
965fbe04fd96 amend: wrap all commit operations in a single transaction
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17471
diff changeset
  2033
                    except KeyError:
965fbe04fd96 amend: wrap all commit operations in a single transaction
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17471
diff changeset
  2034
                        raise IOError
965fbe04fd96 amend: wrap all commit operations in a single transaction
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17471
diff changeset
  2035
            else:
965fbe04fd96 amend: wrap all commit operations in a single transaction
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17471
diff changeset
  2036
                ui.note(_('copying changeset %s to %s\n') % (old, base))
16458
55982f62651f commit: add option to amend the working dir parent
Idan Kamara <idankk86@gmail.com>
parents: 16430
diff changeset
  2037
17472
965fbe04fd96 amend: wrap all commit operations in a single transaction
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17471
diff changeset
  2038
                # Use version of files as in the old cset
965fbe04fd96 amend: wrap all commit operations in a single transaction
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17471
diff changeset
  2039
                def filectxfn(repo, ctx_, path):
965fbe04fd96 amend: wrap all commit operations in a single transaction
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17471
diff changeset
  2040
                    try:
965fbe04fd96 amend: wrap all commit operations in a single transaction
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17471
diff changeset
  2041
                        return old.filectx(path)
965fbe04fd96 amend: wrap all commit operations in a single transaction
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17471
diff changeset
  2042
                    except KeyError:
965fbe04fd96 amend: wrap all commit operations in a single transaction
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17471
diff changeset
  2043
                        raise IOError
16458
55982f62651f commit: add option to amend the working dir parent
Idan Kamara <idankk86@gmail.com>
parents: 16430
diff changeset
  2044
17472
965fbe04fd96 amend: wrap all commit operations in a single transaction
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17471
diff changeset
  2045
                user = opts.get('user') or old.user()
965fbe04fd96 amend: wrap all commit operations in a single transaction
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17471
diff changeset
  2046
                date = opts.get('date') or old.date()
21240
1a833fcf5a14 amend: use "editor" argument for "memctx.__init__" to save commit message
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21127
diff changeset
  2047
            editor = commiteditor
17473
9732473aa24b amend: use an explicit commit message for temporary amending commit
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 17472
diff changeset
  2048
            if not message:
21240
1a833fcf5a14 amend: use "editor" argument for "memctx.__init__" to save commit message
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21127
diff changeset
  2049
                editor = commitforceeditor
17473
9732473aa24b amend: use an explicit commit message for temporary amending commit
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 17472
diff changeset
  2050
                message = old.description()
21036
a1a1bd09e4f4 amend: invoke editor forcibly when "--edit" option is specified
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21033
diff changeset
  2051
            elif opts.get('edit'):
21240
1a833fcf5a14 amend: use "editor" argument for "memctx.__init__" to save commit message
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21127
diff changeset
  2052
                editor = commitforceeditor
16458
55982f62651f commit: add option to amend the working dir parent
Idan Kamara <idankk86@gmail.com>
parents: 16430
diff changeset
  2053
17811
a8aba2921456 amend: add noise in extra to avoid creating obsolescence cycle (issue3664)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 17788
diff changeset
  2054
            pureextra = extra.copy()
a8aba2921456 amend: add noise in extra to avoid creating obsolescence cycle (issue3664)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 17788
diff changeset
  2055
            extra['amend_source'] = old.hex()
a8aba2921456 amend: add noise in extra to avoid creating obsolescence cycle (issue3664)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 17788
diff changeset
  2056
17472
965fbe04fd96 amend: wrap all commit operations in a single transaction
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17471
diff changeset
  2057
            new = context.memctx(repo,
18909
3a72c89a83ec amend: support amending merge changesets (issue3778)
Brodie Rao <brodie@sf.io>
parents: 18853
diff changeset
  2058
                                 parents=[base.node(), old.p2().node()],
17472
965fbe04fd96 amend: wrap all commit operations in a single transaction
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17471
diff changeset
  2059
                                 text=message,
965fbe04fd96 amend: wrap all commit operations in a single transaction
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17471
diff changeset
  2060
                                 files=files,
965fbe04fd96 amend: wrap all commit operations in a single transaction
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17471
diff changeset
  2061
                                 filectxfn=filectxfn,
965fbe04fd96 amend: wrap all commit operations in a single transaction
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17471
diff changeset
  2062
                                 user=user,
965fbe04fd96 amend: wrap all commit operations in a single transaction
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17471
diff changeset
  2063
                                 date=date,
21240
1a833fcf5a14 amend: use "editor" argument for "memctx.__init__" to save commit message
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21127
diff changeset
  2064
                                 extra=extra,
1a833fcf5a14 amend: use "editor" argument for "memctx.__init__" to save commit message
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21127
diff changeset
  2065
                                 editor=editor)
17811
a8aba2921456 amend: add noise in extra to avoid creating obsolescence cycle (issue3664)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 17788
diff changeset
  2066
a8aba2921456 amend: add noise in extra to avoid creating obsolescence cycle (issue3664)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 17788
diff changeset
  2067
            newdesc =  changelog.stripdesc(new.description())
a8aba2921456 amend: add noise in extra to avoid creating obsolescence cycle (issue3664)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 17788
diff changeset
  2068
            if ((not node)
a8aba2921456 amend: add noise in extra to avoid creating obsolescence cycle (issue3664)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 17788
diff changeset
  2069
                and newdesc == old.description()
a8aba2921456 amend: add noise in extra to avoid creating obsolescence cycle (issue3664)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 17788
diff changeset
  2070
                and user == old.user()
a8aba2921456 amend: add noise in extra to avoid creating obsolescence cycle (issue3664)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 17788
diff changeset
  2071
                and date == old.date()
a8aba2921456 amend: add noise in extra to avoid creating obsolescence cycle (issue3664)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 17788
diff changeset
  2072
                and pureextra == old.extra()):
a8aba2921456 amend: add noise in extra to avoid creating obsolescence cycle (issue3664)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 17788
diff changeset
  2073
                # nothing changed. continuing here would create a new node
a8aba2921456 amend: add noise in extra to avoid creating obsolescence cycle (issue3664)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 17788
diff changeset
  2074
                # anyway because of the amend_source noise.
a8aba2921456 amend: add noise in extra to avoid creating obsolescence cycle (issue3664)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 17788
diff changeset
  2075
                #
a8aba2921456 amend: add noise in extra to avoid creating obsolescence cycle (issue3664)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 17788
diff changeset
  2076
                # This not what we expect from amend.
a8aba2921456 amend: add noise in extra to avoid creating obsolescence cycle (issue3664)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 17788
diff changeset
  2077
                return old.node()
a8aba2921456 amend: add noise in extra to avoid creating obsolescence cycle (issue3664)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 17788
diff changeset
  2078
17472
965fbe04fd96 amend: wrap all commit operations in a single transaction
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17471
diff changeset
  2079
            ph = repo.ui.config('phases', 'new-commit', phases.draft)
965fbe04fd96 amend: wrap all commit operations in a single transaction
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17471
diff changeset
  2080
            try:
20700
b0153cb8b64e commit: create new amend changeset as secret correctly for "--secret" option
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20072
diff changeset
  2081
                if opts.get('secret'):
b0153cb8b64e commit: create new amend changeset as secret correctly for "--secret" option
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20072
diff changeset
  2082
                    commitphase = 'secret'
b0153cb8b64e commit: create new amend changeset as secret correctly for "--secret" option
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20072
diff changeset
  2083
                else:
b0153cb8b64e commit: create new amend changeset as secret correctly for "--secret" option
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20072
diff changeset
  2084
                    commitphase = old.phase()
20790
49f2d5644f04 config: set a 'source' in most cases where config don't come from file but code
Mads Kiilerich <madski@unity3d.com>
parents: 20773
diff changeset
  2085
                repo.ui.setconfig('phases', 'new-commit', commitphase, 'amend')
17472
965fbe04fd96 amend: wrap all commit operations in a single transaction
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17471
diff changeset
  2086
                newid = repo.commitctx(new)
965fbe04fd96 amend: wrap all commit operations in a single transaction
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17471
diff changeset
  2087
            finally:
20790
49f2d5644f04 config: set a 'source' in most cases where config don't come from file but code
Mads Kiilerich <madski@unity3d.com>
parents: 20773
diff changeset
  2088
                repo.ui.setconfig('phases', 'new-commit', ph, 'amend')
17472
965fbe04fd96 amend: wrap all commit operations in a single transaction
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17471
diff changeset
  2089
            if newid != old.node():
965fbe04fd96 amend: wrap all commit operations in a single transaction
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17471
diff changeset
  2090
                # Reroute the working copy parent to the new changeset
965fbe04fd96 amend: wrap all commit operations in a single transaction
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17471
diff changeset
  2091
                repo.setparents(newid, nullid)
965fbe04fd96 amend: wrap all commit operations in a single transaction
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17471
diff changeset
  2092
965fbe04fd96 amend: wrap all commit operations in a single transaction
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17471
diff changeset
  2093
                # Move bookmarks from old parent to amend commit
965fbe04fd96 amend: wrap all commit operations in a single transaction
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17471
diff changeset
  2094
                bms = repo.nodebookmarks(old.node())
965fbe04fd96 amend: wrap all commit operations in a single transaction
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17471
diff changeset
  2095
                if bms:
17922
7f5dab94e48c bookmarks: introduce a bmstore to manage bookmark persistence
Augie Fackler <raf@durin42.com>
parents: 17891
diff changeset
  2096
                    marks = repo._bookmarks
17472
965fbe04fd96 amend: wrap all commit operations in a single transaction
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17471
diff changeset
  2097
                    for bm in bms:
17922
7f5dab94e48c bookmarks: introduce a bmstore to manage bookmark persistence
Augie Fackler <raf@durin42.com>
parents: 17891
diff changeset
  2098
                        marks[bm] = newid
7f5dab94e48c bookmarks: introduce a bmstore to manage bookmark persistence
Augie Fackler <raf@durin42.com>
parents: 17891
diff changeset
  2099
                    marks.write()
17472
965fbe04fd96 amend: wrap all commit operations in a single transaction
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17471
diff changeset
  2100
            #commit the whole amend process
17475
63e45aee46d4 amend: add obsolete support
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 17473
diff changeset
  2101
            if obsolete._enabled and newid != old.node():
63e45aee46d4 amend: add obsolete support
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 17473
diff changeset
  2102
                # mark the new changeset as successor of the rewritten one
63e45aee46d4 amend: add obsolete support
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 17473
diff changeset
  2103
                new = repo[newid]
63e45aee46d4 amend: add obsolete support
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 17473
diff changeset
  2104
                obs = [(old, (new,))]
63e45aee46d4 amend: add obsolete support
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 17473
diff changeset
  2105
                if node:
17812
578fcc22b469 amend: do a bare kill of temporary changeset
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 17811
diff changeset
  2106
                    obs.append((ctx, ()))
17475
63e45aee46d4 amend: add obsolete support
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 17473
diff changeset
  2107
63e45aee46d4 amend: add obsolete support
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 17473
diff changeset
  2108
                obsolete.createmarkers(repo, obs)
17472
965fbe04fd96 amend: wrap all commit operations in a single transaction
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17471
diff changeset
  2109
            tr.close()
17461
bacde764fba0 amend: preserve phase of amended revision (issue3602)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 17391
diff changeset
  2110
        finally:
17472
965fbe04fd96 amend: wrap all commit operations in a single transaction
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17471
diff changeset
  2111
            tr.release()
17475
63e45aee46d4 amend: add obsolete support
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 17473
diff changeset
  2112
        if (not obsolete._enabled) and newid != old.node():
63e45aee46d4 amend: add obsolete support
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 17473
diff changeset
  2113
            # Strip the intermediate commit (if there was one) and the amended
63e45aee46d4 amend: add obsolete support
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 17473
diff changeset
  2114
            # commit
17471
ad1561723dde amend: lock the repository during the whole process
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17468
diff changeset
  2115
            if node:
ad1561723dde amend: lock the repository during the whole process
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17468
diff changeset
  2116
                ui.note(_('stripping intermediate changeset %s\n') % ctx)
ad1561723dde amend: lock the repository during the whole process
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17468
diff changeset
  2117
            ui.note(_('stripping amended changeset %s\n') % old)
ad1561723dde amend: lock the repository during the whole process
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17468
diff changeset
  2118
            repair.strip(ui, repo, old.node(), topic='amend-backup')
16458
55982f62651f commit: add option to amend the working dir parent
Idan Kamara <idankk86@gmail.com>
parents: 16430
diff changeset
  2119
    finally:
18197
153659e86a5f amend: invalidate dirstate in case of failure (issue3670)
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 18006
diff changeset
  2120
        if newid is None:
153659e86a5f amend: invalidate dirstate in case of failure (issue3670)
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 18006
diff changeset
  2121
            repo.dirstate.invalidate()
19024
ab04e87a5f3b amend: fix unlocking order - first lock then wlock
Mads Kiilerich <madski@unity3d.com>
parents: 18991
diff changeset
  2122
        lockmod.release(lock, wlock)
16458
55982f62651f commit: add option to amend the working dir parent
Idan Kamara <idankk86@gmail.com>
parents: 16430
diff changeset
  2123
    return newid
55982f62651f commit: add option to amend the working dir parent
Idan Kamara <idankk86@gmail.com>
parents: 16430
diff changeset
  2124
8994
4a1187d3cb00 commit: report modified subrepos in commit editor
Matt Mackall <mpm@selenic.com>
parents: 8990
diff changeset
  2125
def commiteditor(repo, ctx, subs):
8407
223000a687b0 commit: move commit editor to cmdutil, pass as function
Matt Mackall <mpm@selenic.com>
parents: 8390
diff changeset
  2126
    if ctx.description():
223000a687b0 commit: move commit editor to cmdutil, pass as function
Matt Mackall <mpm@selenic.com>
parents: 8390
diff changeset
  2127
        return ctx.description()
8994
4a1187d3cb00 commit: report modified subrepos in commit editor
Matt Mackall <mpm@selenic.com>
parents: 8990
diff changeset
  2128
    return commitforceeditor(repo, ctx, subs)
8407
223000a687b0 commit: move commit editor to cmdutil, pass as function
Matt Mackall <mpm@selenic.com>
parents: 8390
diff changeset
  2129
8994
4a1187d3cb00 commit: report modified subrepos in commit editor
Matt Mackall <mpm@selenic.com>
parents: 8990
diff changeset
  2130
def commitforceeditor(repo, ctx, subs):
8407
223000a687b0 commit: move commit editor to cmdutil, pass as function
Matt Mackall <mpm@selenic.com>
parents: 8390
diff changeset
  2131
    edittext = []
8707
0550dfe4fca1 commit: editor reads file lists from provided context
Matt Mackall <mpm@selenic.com>
parents: 8680
diff changeset
  2132
    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
  2133
    if ctx.description():
223000a687b0 commit: move commit editor to cmdutil, pass as function
Matt Mackall <mpm@selenic.com>
parents: 8390
diff changeset
  2134
        edittext.append(ctx.description())
223000a687b0 commit: move commit editor to cmdutil, pass as function
Matt Mackall <mpm@selenic.com>
parents: 8390
diff changeset
  2135
    edittext.append("")
223000a687b0 commit: move commit editor to cmdutil, pass as function
Matt Mackall <mpm@selenic.com>
parents: 8390
diff changeset
  2136
    edittext.append("") # Empty line between message and comments.
223000a687b0 commit: move commit editor to cmdutil, pass as function
Matt Mackall <mpm@selenic.com>
parents: 8390
diff changeset
  2137
    edittext.append(_("HG: Enter commit message."
223000a687b0 commit: move commit editor to cmdutil, pass as function
Matt Mackall <mpm@selenic.com>
parents: 8390
diff changeset
  2138
                      "  Lines beginning with 'HG:' are removed."))
8535
5b6a6ed4f185 cmdutil: mark string for translation
Martin Geisler <mg@lazybytes.net>
parents: 8497
diff changeset
  2139
    edittext.append(_("HG: Leave message empty to abort commit."))
8407
223000a687b0 commit: move commit editor to cmdutil, pass as function
Matt Mackall <mpm@selenic.com>
parents: 8390
diff changeset
  2140
    edittext.append("HG: --")
223000a687b0 commit: move commit editor to cmdutil, pass as function
Matt Mackall <mpm@selenic.com>
parents: 8390
diff changeset
  2141
    edittext.append(_("HG: user: %s") % ctx.user())
223000a687b0 commit: move commit editor to cmdutil, pass as function
Matt Mackall <mpm@selenic.com>
parents: 8390
diff changeset
  2142
    if ctx.p2():
223000a687b0 commit: move commit editor to cmdutil, pass as function
Matt Mackall <mpm@selenic.com>
parents: 8390
diff changeset
  2143
        edittext.append(_("HG: branch merge"))
223000a687b0 commit: move commit editor to cmdutil, pass as function
Matt Mackall <mpm@selenic.com>
parents: 8390
diff changeset
  2144
    if ctx.branch():
13047
6c375e07d673 branch: operate on branch names in local string space where possible
Matt Mackall <mpm@selenic.com>
parents: 12973
diff changeset
  2145
        edittext.append(_("HG: branch '%s'") % ctx.branch())
18538
94317c2d53b8 commit: show active bookmark in commit editor helper text
Antonio Zanardo <zanardo@gmail.com>
parents: 18364
diff changeset
  2146
    if bookmarks.iscurrent(repo):
94317c2d53b8 commit: show active bookmark in commit editor helper text
Antonio Zanardo <zanardo@gmail.com>
parents: 18364
diff changeset
  2147
        edittext.append(_("HG: bookmark '%s'") % repo._bookmarkcurrent)
8994
4a1187d3cb00 commit: report modified subrepos in commit editor
Matt Mackall <mpm@selenic.com>
parents: 8990
diff changeset
  2148
    edittext.extend([_("HG: subrepo %s") % s for s in subs])
8407
223000a687b0 commit: move commit editor to cmdutil, pass as function
Matt Mackall <mpm@selenic.com>
parents: 8390
diff changeset
  2149
    edittext.extend([_("HG: added %s") % f for f in added])
8707
0550dfe4fca1 commit: editor reads file lists from provided context
Matt Mackall <mpm@selenic.com>
parents: 8680
diff changeset
  2150
    edittext.extend([_("HG: changed %s") % f for f in modified])
8407
223000a687b0 commit: move commit editor to cmdutil, pass as function
Matt Mackall <mpm@selenic.com>
parents: 8390
diff changeset
  2151
    edittext.extend([_("HG: 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
  2152
    if not added and not modified and not removed:
8407
223000a687b0 commit: move commit editor to cmdutil, pass as function
Matt Mackall <mpm@selenic.com>
parents: 8390
diff changeset
  2153
        edittext.append(_("HG: no files changed"))
223000a687b0 commit: move commit editor to cmdutil, pass as function
Matt Mackall <mpm@selenic.com>
parents: 8390
diff changeset
  2154
    edittext.append("")
223000a687b0 commit: move commit editor to cmdutil, pass as function
Matt Mackall <mpm@selenic.com>
parents: 8390
diff changeset
  2155
    # run editor in the repository root
223000a687b0 commit: move commit editor to cmdutil, pass as function
Matt Mackall <mpm@selenic.com>
parents: 8390
diff changeset
  2156
    olddir = os.getcwd()
223000a687b0 commit: move commit editor to cmdutil, pass as function
Matt Mackall <mpm@selenic.com>
parents: 8390
diff changeset
  2157
    os.chdir(repo.root)
20604
4991cfc90f59 cmdutil: make commitforceeditor() to pass revision extras to ui.edit()
Alexander Drozdov <al.drozdov@gmail.com>
parents: 20571
diff changeset
  2158
    text = repo.ui.edit("\n".join(edittext), ctx.user(), ctx.extra())
12900
4ff61287bde2 commit: handle missing newline on last commit comment
Matt Mackall <mpm@selenic.com>
parents: 12874
diff changeset
  2159
    text = re.sub("(?m)^HG:.*(\n|$)", "", text)
8407
223000a687b0 commit: move commit editor to cmdutil, pass as function
Matt Mackall <mpm@selenic.com>
parents: 8390
diff changeset
  2160
    os.chdir(olddir)
223000a687b0 commit: move commit editor to cmdutil, pass as function
Matt Mackall <mpm@selenic.com>
parents: 8390
diff changeset
  2161
223000a687b0 commit: move commit editor to cmdutil, pass as function
Matt Mackall <mpm@selenic.com>
parents: 8390
diff changeset
  2162
    if not text.strip():
223000a687b0 commit: move commit editor to cmdutil, pass as function
Matt Mackall <mpm@selenic.com>
parents: 8390
diff changeset
  2163
        raise util.Abort(_("empty commit message"))
223000a687b0 commit: move commit editor to cmdutil, pass as function
Matt Mackall <mpm@selenic.com>
parents: 8390
diff changeset
  2164
223000a687b0 commit: move commit editor to cmdutil, pass as function
Matt Mackall <mpm@selenic.com>
parents: 8390
diff changeset
  2165
    return text
14297
2daa5179e73f commands: use a decorator to build table incrementally
Adrian Buehlmann <adrian@cadifra.com>
parents: 14291
diff changeset
  2166
18688
79107fad06aa commit: factor out status printing into a helper function
Kevin Bullock <kbullock@ringworld.org>
parents: 18648
diff changeset
  2167
def commitstatus(repo, node, branch, bheads=None, opts={}):
79107fad06aa commit: factor out status printing into a helper function
Kevin Bullock <kbullock@ringworld.org>
parents: 18648
diff changeset
  2168
    ctx = repo[node]
79107fad06aa commit: factor out status printing into a helper function
Kevin Bullock <kbullock@ringworld.org>
parents: 18648
diff changeset
  2169
    parents = ctx.parents()
79107fad06aa commit: factor out status printing into a helper function
Kevin Bullock <kbullock@ringworld.org>
parents: 18648
diff changeset
  2170
79107fad06aa commit: factor out status printing into a helper function
Kevin Bullock <kbullock@ringworld.org>
parents: 18648
diff changeset
  2171
    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
  2172
        [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
  2173
        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
  2174
        # 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
  2175
        # 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
  2176
        #
79107fad06aa commit: factor out status printing into a helper function
Kevin Bullock <kbullock@ringworld.org>
parents: 18648
diff changeset
  2177
        # 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
  2178
        #   N: null or no parent
79107fad06aa commit: factor out status printing into a helper function
Kevin Bullock <kbullock@ringworld.org>
parents: 18648
diff changeset
  2179
        #   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
  2180
        #   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
  2181
        #   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
  2182
        # 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
  2183
        # 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
  2184
        # 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
  2185
        # printed anyway.
79107fad06aa commit: factor out status printing into a helper function
Kevin Bullock <kbullock@ringworld.org>
parents: 18648
diff changeset
  2186
        #
79107fad06aa commit: factor out status printing into a helper function
Kevin Bullock <kbullock@ringworld.org>
parents: 18648
diff changeset
  2187
        # Par Msg Comment
79107fad06aa commit: factor out status printing into a helper function
Kevin Bullock <kbullock@ringworld.org>
parents: 18648
diff changeset
  2188
        # 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
  2189
        #
79107fad06aa commit: factor out status printing into a helper function
Kevin Bullock <kbullock@ringworld.org>
parents: 18648
diff changeset
  2190
        # 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
  2191
        # 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
  2192
        # H N  n  usual case
79107fad06aa commit: factor out status printing into a helper function
Kevin Bullock <kbullock@ringworld.org>
parents: 18648
diff changeset
  2193
        #
79107fad06aa commit: factor out status printing into a helper function
Kevin Bullock <kbullock@ringworld.org>
parents: 18648
diff changeset
  2194
        # 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
  2195
        # C B  y  branch merge
79107fad06aa commit: factor out status printing into a helper function
Kevin Bullock <kbullock@ringworld.org>
parents: 18648
diff changeset
  2196
        # 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
  2197
        #
79107fad06aa commit: factor out status printing into a helper function
Kevin Bullock <kbullock@ringworld.org>
parents: 18648
diff changeset
  2198
        # 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
  2199
        # 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
  2200
        #
79107fad06aa commit: factor out status printing into a helper function
Kevin Bullock <kbullock@ringworld.org>
parents: 18648
diff changeset
  2201
        # 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
  2202
79107fad06aa commit: factor out status printing into a helper function
Kevin Bullock <kbullock@ringworld.org>
parents: 18648
diff changeset
  2203
    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
  2204
        for r in parents:
79107fad06aa commit: factor out status printing into a helper function
Kevin Bullock <kbullock@ringworld.org>
parents: 18648
diff changeset
  2205
            if r.closesbranch() and r.branch() == branch:
79107fad06aa commit: factor out status printing into a helper function
Kevin Bullock <kbullock@ringworld.org>
parents: 18648
diff changeset
  2206
                repo.ui.status(_('reopening closed branch head %d\n') % r)
79107fad06aa commit: factor out status printing into a helper function
Kevin Bullock <kbullock@ringworld.org>
parents: 18648
diff changeset
  2207
79107fad06aa commit: factor out status printing into a helper function
Kevin Bullock <kbullock@ringworld.org>
parents: 18648
diff changeset
  2208
    if repo.ui.debugflag:
79107fad06aa commit: factor out status printing into a helper function
Kevin Bullock <kbullock@ringworld.org>
parents: 18648
diff changeset
  2209
        repo.ui.write(_('committed changeset %d:%s\n') % (int(ctx), ctx.hex()))
79107fad06aa commit: factor out status printing into a helper function
Kevin Bullock <kbullock@ringworld.org>
parents: 18648
diff changeset
  2210
    elif repo.ui.verbose:
79107fad06aa commit: factor out status printing into a helper function
Kevin Bullock <kbullock@ringworld.org>
parents: 18648
diff changeset
  2211
        repo.ui.write(_('committed changeset %d:%s\n') % (int(ctx), ctx))
79107fad06aa commit: factor out status printing into a helper function
Kevin Bullock <kbullock@ringworld.org>
parents: 18648
diff changeset
  2212
16304
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2213
def revert(ui, repo, ctx, parents, *pats, **opts):
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2214
    parent, p2 = parents
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2215
    node = ctx.node()
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2216
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2217
    mf = ctx.manifest()
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2218
    if node == parent:
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2219
        pmf = mf
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2220
    else:
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2221
        pmf = None
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2222
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2223
    # 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
  2224
    # so have to walk both. do not print errors if files exist in one
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2225
    # but not other.
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2226
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2227
    names = {}
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2228
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2229
    wlock = repo.wlock()
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2230
    try:
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2231
        # walk dirstate.
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2232
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2233
        m = scmutil.match(repo[None], pats, opts)
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2234
        m.bad = lambda x, y: False
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2235
        for abs in repo.walk(m):
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2236
            names[abs] = m.rel(abs), m.exact(abs)
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2237
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2238
        # walk target manifest.
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2239
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2240
        def badfn(path, msg):
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2241
            if path in names:
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2242
                return
16583
146a00c162a0 revert: don't re-create changeset context
Kevin Bullock <kbullock@ringworld.org>
parents: 16553
diff changeset
  2243
            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
  2244
                return
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2245
            path_ = path + '/'
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2246
            for f in names:
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2247
                if f.startswith(path_):
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2248
                    return
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2249
            ui.warn("%s: %s\n" % (m.rel(path), msg))
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2250
16583
146a00c162a0 revert: don't re-create changeset context
Kevin Bullock <kbullock@ringworld.org>
parents: 16553
diff changeset
  2251
        m = scmutil.match(ctx, pats, opts)
16304
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2252
        m.bad = badfn
16583
146a00c162a0 revert: don't re-create changeset context
Kevin Bullock <kbullock@ringworld.org>
parents: 16553
diff changeset
  2253
        for abs in ctx.walk(m):
16304
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2254
            if abs not in names:
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2255
                names[abs] = m.rel(abs), m.exact(abs)
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2256
16430
6883c2363f44 revert: add support for reverting subrepos without --no-backup and/or --all
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16429
diff changeset
  2257
        # get the list of subrepos that must be reverted
18364
6252b4f1c4b4 subrepos: process subrepos in sorted order
Mads Kiilerich <mads@kiilerich.com>
parents: 18340
diff changeset
  2258
        targetsubs = sorted(s for s in ctx.substate if m(s))
16304
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2259
        m = scmutil.matchfiles(repo, names)
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2260
        changes = repo.status(match=m)[:4]
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2261
        modified, added, removed, deleted = map(set, changes)
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2262
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2263
        # if f is a rename, also revert the source
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2264
        cwd = repo.getcwd()
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2265
        for f in added:
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2266
            src = repo.dirstate.copied(f)
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2267
            if src and src not in names and repo.dirstate[src] == 'r':
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2268
                removed.add(src)
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2269
                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
  2270
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2271
        def removeforget(abs):
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2272
            if repo.dirstate[abs] == 'a':
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2273
                return _('forgetting %s\n')
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2274
            return _('removing %s\n')
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2275
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2276
        revert = ([], _('reverting %s\n'))
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2277
        add = ([], _('adding %s\n'))
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2278
        remove = ([], removeforget)
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2279
        undelete = ([], _('undeleting %s\n'))
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2280
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2281
        disptable = (
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2282
            # dispatch table:
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2283
            #   file state
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2284
            #   action if in target manifest
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2285
            #   action if not in target manifest
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2286
            #   make backup if in target manifest
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2287
            #   make backup if not in target manifest
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2288
            (modified, revert, remove, True, True),
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2289
            (added, revert, remove, True, False),
19510
8b190adb7ee3 revert: make backup when unforgetting a file (issue3423)
Matt Mackall <mpm@selenic.com>
parents: 19496
diff changeset
  2290
            (removed, undelete, None, True, False),
16304
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2291
            (deleted, revert, remove, False, False),
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2292
            )
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2293
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2294
        for abs, (rel, exact) in sorted(names.items()):
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2295
            mfentry = mf.get(abs)
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2296
            target = repo.wjoin(abs)
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2297
            def handle(xlist, dobackup):
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2298
                xlist[0].append(abs)
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2299
                if (dobackup and not opts.get('no_backup') and
19510
8b190adb7ee3 revert: make backup when unforgetting a file (issue3423)
Matt Mackall <mpm@selenic.com>
parents: 19496
diff changeset
  2300
                    os.path.lexists(target) and
19511
ca2dfc2f63eb revert: fix largefiles breakage
Matt Mackall <mpm@selenic.com>
parents: 19510
diff changeset
  2301
                    abs in ctx and repo[None][abs].cmp(ctx[abs])):
16304
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2302
                    bakname = "%s.orig" % rel
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2303
                    ui.note(_('saving current version of %s as %s\n') %
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2304
                            (rel, bakname))
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2305
                    if not opts.get('dry_run'):
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2306
                        util.rename(target, bakname)
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2307
                if ui.verbose or not exact:
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2308
                    msg = xlist[1]
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2309
                    if not isinstance(msg, basestring):
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2310
                        msg = msg(abs)
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2311
                    ui.status(msg % rel)
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2312
            for table, hitlist, misslist, backuphit, backupmiss in disptable:
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2313
                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
  2314
                    continue
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2315
                # file has changed in dirstate
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2316
                if mfentry:
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2317
                    handle(hitlist, backuphit)
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2318
                elif misslist is not None:
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2319
                    handle(misslist, backupmiss)
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2320
                break
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2321
            else:
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2322
                if abs not in repo.dirstate:
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2323
                    if mfentry:
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2324
                        handle(add, True)
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2325
                    elif exact:
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2326
                        ui.warn(_('file not managed: %s\n') % rel)
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2327
                    continue
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2328
                # file has not changed in dirstate
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2329
                if node == parent:
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2330
                    if exact:
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2331
                        ui.warn(_('no changes needed to %s\n') % rel)
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2332
                    continue
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2333
                if pmf is None:
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2334
                    # only need parent manifest in this unlikely case,
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2335
                    # so do not read by default
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2336
                    pmf = repo[parent].manifest()
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2337
                if abs in pmf and mfentry:
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2338
                    # if version of file is same in parent and target
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2339
                    # manifests, do nothing
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2340
                    if (pmf[abs] != mfentry or
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2341
                        pmf.flags(abs) != mf.flags(abs)):
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2342
                        handle(revert, False)
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2343
                else:
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2344
                    handle(remove, False)
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2345
        if not opts.get('dry_run'):
20571
d4893e64f300 revert: extract actual revert in its own function
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20553
diff changeset
  2346
            _performrevert(repo, parents, ctx, revert, add, remove, undelete)
19129
bd19587a3347 revert: ensure that copies and renames are honored (issue3920)
Bryan O'Sullivan <bryano@fb.com>
parents: 19024
diff changeset
  2347
16429
71dcce391a44 revert: add support for reverting subrepos
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16381
diff changeset
  2348
            if targetsubs:
71dcce391a44 revert: add support for reverting subrepos
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16381
diff changeset
  2349
                # Revert the subrepos on the revert list
71dcce391a44 revert: add support for reverting subrepos
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16381
diff changeset
  2350
                for sub in targetsubs:
71dcce391a44 revert: add support for reverting subrepos
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16381
diff changeset
  2351
                    ctx.sub(sub).revert(ui, ctx.substate[sub], *pats, **opts)
16304
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2352
    finally:
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2353
        wlock.release()
a740fa28d718 revert: move bulk of revert command from commands to cmdutil
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 16283
diff changeset
  2354
20571
d4893e64f300 revert: extract actual revert in its own function
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20553
diff changeset
  2355
def _performrevert(repo, parents, ctx, revert, add, remove, undelete):
d4893e64f300 revert: extract actual revert in its own function
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20553
diff changeset
  2356
    """function that actually perform all the action computed for revert
d4893e64f300 revert: extract actual revert in its own function
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20553
diff changeset
  2357
d4893e64f300 revert: extract actual revert in its own function
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20553
diff changeset
  2358
    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
  2359
    the imminent revert.
d4893e64f300 revert: extract actual revert in its own function
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20553
diff changeset
  2360
21024
7731a2281cf0 spelling: fixes from spell checker
Mads Kiilerich <madski@unity3d.com>
parents: 20790
diff changeset
  2361
    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
  2362
    """
d4893e64f300 revert: extract actual revert in its own function
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20553
diff changeset
  2363
    parent, p2 = parents
d4893e64f300 revert: extract actual revert in its own function
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20553
diff changeset
  2364
    node = ctx.node()
d4893e64f300 revert: extract actual revert in its own function
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20553
diff changeset
  2365
    def checkout(f):
d4893e64f300 revert: extract actual revert in its own function
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20553
diff changeset
  2366
        fc = ctx[f]
d4893e64f300 revert: extract actual revert in its own function
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20553
diff changeset
  2367
        repo.wwrite(f, fc.data(), fc.flags())
d4893e64f300 revert: extract actual revert in its own function
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20553
diff changeset
  2368
d4893e64f300 revert: extract actual revert in its own function
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20553
diff changeset
  2369
    audit_path = pathutil.pathauditor(repo.root)
d4893e64f300 revert: extract actual revert in its own function
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20553
diff changeset
  2370
    for f in remove[0]:
d4893e64f300 revert: extract actual revert in its own function
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20553
diff changeset
  2371
        if repo.dirstate[f] == 'a':
d4893e64f300 revert: extract actual revert in its own function
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20553
diff changeset
  2372
            repo.dirstate.drop(f)
d4893e64f300 revert: extract actual revert in its own function
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20553
diff changeset
  2373
            continue
d4893e64f300 revert: extract actual revert in its own function
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20553
diff changeset
  2374
        audit_path(f)
d4893e64f300 revert: extract actual revert in its own function
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20553
diff changeset
  2375
        try:
d4893e64f300 revert: extract actual revert in its own function
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20553
diff changeset
  2376
            util.unlinkpath(repo.wjoin(f))
d4893e64f300 revert: extract actual revert in its own function
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20553
diff changeset
  2377
        except OSError:
d4893e64f300 revert: extract actual revert in its own function
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20553
diff changeset
  2378
            pass
d4893e64f300 revert: extract actual revert in its own function
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20553
diff changeset
  2379
        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
  2380
d4893e64f300 revert: extract actual revert in its own function
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20553
diff changeset
  2381
    normal = None
d4893e64f300 revert: extract actual revert in its own function
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20553
diff changeset
  2382
    if node == parent:
d4893e64f300 revert: extract actual revert in its own function
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20553
diff changeset
  2383
        # 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
  2384
        # 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
  2385
        # 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
  2386
        if p2 != nullid:
d4893e64f300 revert: extract actual revert in its own function
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20553
diff changeset
  2387
            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
  2388
        else:
d4893e64f300 revert: extract actual revert in its own function
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20553
diff changeset
  2389
            normal = repo.dirstate.normal
d4893e64f300 revert: extract actual revert in its own function
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20553
diff changeset
  2390
    for f in revert[0]:
d4893e64f300 revert: extract actual revert in its own function
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20553
diff changeset
  2391
        checkout(f)
d4893e64f300 revert: extract actual revert in its own function
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20553
diff changeset
  2392
        if normal:
d4893e64f300 revert: extract actual revert in its own function
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20553
diff changeset
  2393
            normal(f)
d4893e64f300 revert: extract actual revert in its own function
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20553
diff changeset
  2394
d4893e64f300 revert: extract actual revert in its own function
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20553
diff changeset
  2395
    for f in add[0]:
d4893e64f300 revert: extract actual revert in its own function
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20553
diff changeset
  2396
        checkout(f)
d4893e64f300 revert: extract actual revert in its own function
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20553
diff changeset
  2397
        repo.dirstate.add(f)
d4893e64f300 revert: extract actual revert in its own function
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20553
diff changeset
  2398
d4893e64f300 revert: extract actual revert in its own function
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20553
diff changeset
  2399
    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
  2400
    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
  2401
        normal = repo.dirstate.normal
d4893e64f300 revert: extract actual revert in its own function
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20553
diff changeset
  2402
    for f in undelete[0]:
d4893e64f300 revert: extract actual revert in its own function
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20553
diff changeset
  2403
        checkout(f)
d4893e64f300 revert: extract actual revert in its own function
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20553
diff changeset
  2404
        normal(f)
d4893e64f300 revert: extract actual revert in its own function
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20553
diff changeset
  2405
d4893e64f300 revert: extract actual revert in its own function
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20553
diff changeset
  2406
    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
  2407
d4893e64f300 revert: extract actual revert in its own function
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20553
diff changeset
  2408
    for f in add[0] + undelete[0] + revert[0]:
d4893e64f300 revert: extract actual revert in its own function
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20553
diff changeset
  2409
        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
  2410
            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
  2411
14297
2daa5179e73f commands: use a decorator to build table incrementally
Adrian Buehlmann <adrian@cadifra.com>
parents: 14291
diff changeset
  2412
def command(table):
2daa5179e73f commands: use a decorator to build table incrementally
Adrian Buehlmann <adrian@cadifra.com>
parents: 14291
diff changeset
  2413
    '''returns a function object bound to table which can be used as
2daa5179e73f commands: use a decorator to build table incrementally
Adrian Buehlmann <adrian@cadifra.com>
parents: 14291
diff changeset
  2414
    a decorator for populating table as a command table'''
2daa5179e73f commands: use a decorator to build table incrementally
Adrian Buehlmann <adrian@cadifra.com>
parents: 14291
diff changeset
  2415
18235
9807e7d596c3 cmdutil: make options argument optional
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18206
diff changeset
  2416
    def cmd(name, options=(), synopsis=None):
14297
2daa5179e73f commands: use a decorator to build table incrementally
Adrian Buehlmann <adrian@cadifra.com>
parents: 14291
diff changeset
  2417
        def decorator(func):
2daa5179e73f commands: use a decorator to build table incrementally
Adrian Buehlmann <adrian@cadifra.com>
parents: 14291
diff changeset
  2418
            if synopsis:
18235
9807e7d596c3 cmdutil: make options argument optional
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18206
diff changeset
  2419
                table[name] = func, list(options), synopsis
14297
2daa5179e73f commands: use a decorator to build table incrementally
Adrian Buehlmann <adrian@cadifra.com>
parents: 14291
diff changeset
  2420
            else:
18235
9807e7d596c3 cmdutil: make options argument optional
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18206
diff changeset
  2421
                table[name] = func, list(options)
14297
2daa5179e73f commands: use a decorator to build table incrementally
Adrian Buehlmann <adrian@cadifra.com>
parents: 14291
diff changeset
  2422
            return func
2daa5179e73f commands: use a decorator to build table incrementally
Adrian Buehlmann <adrian@cadifra.com>
parents: 14291
diff changeset
  2423
        return decorator
2daa5179e73f commands: use a decorator to build table incrementally
Adrian Buehlmann <adrian@cadifra.com>
parents: 14291
diff changeset
  2424
2daa5179e73f commands: use a decorator to build table incrementally
Adrian Buehlmann <adrian@cadifra.com>
parents: 14291
diff changeset
  2425
    return cmd
19211
3bfd7f1e7485 summary: augment output with info from extensions
Bryan O'Sullivan <bryano@fb.com>
parents: 19129
diff changeset
  2426
21051
1004d3cd65fd outgoing: introduce "outgoinghooks" to avoid redundant outgoing check
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21047
diff changeset
  2427
# 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
  2428
# 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
  2429
# "findcommonoutgoing()"
1004d3cd65fd outgoing: introduce "outgoinghooks" to avoid redundant outgoing check
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21047
diff changeset
  2430
outgoinghooks = util.hooks()
1004d3cd65fd outgoing: introduce "outgoinghooks" to avoid redundant outgoing check
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21047
diff changeset
  2431
19211
3bfd7f1e7485 summary: augment output with info from extensions
Bryan O'Sullivan <bryano@fb.com>
parents: 19129
diff changeset
  2432
# 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
  2433
summaryhooks = util.hooks()
19474
894fd1a7c533 cmdutil: core functionality to block during multistep commands (issue3955)
Matt Mackall <mpm@selenic.com>
parents: 19293
diff changeset
  2434
21047
f0003f989e72 summary: introduce "summaryremotehooks" to avoid redundant incoming/outgoing check
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21041
diff changeset
  2435
# 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
  2436
#
f0003f989e72 summary: introduce "summaryremotehooks" to avoid redundant incoming/outgoing check
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21041
diff changeset
  2437
# 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
  2438
#  (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
  2439
#
f0003f989e72 summary: introduce "summaryremotehooks" to avoid redundant incoming/outgoing check
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21041
diff changeset
  2440
# 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
  2441
#  - (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
  2442
#  - (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
  2443
summaryremotehooks = util.hooks()
f0003f989e72 summary: introduce "summaryremotehooks" to avoid redundant incoming/outgoing check
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21041
diff changeset
  2444
19474
894fd1a7c533 cmdutil: core functionality to block during multistep commands (issue3955)
Matt Mackall <mpm@selenic.com>
parents: 19293
diff changeset
  2445
# 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
  2446
# 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
  2447
# note: bisect is intentionally excluded
19496
607191a45f8c checkunfinished: accommodate histedit quirk
Matt Mackall <mpm@selenic.com>
parents: 19482
diff changeset
  2448
# (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
  2449
unfinishedstates = [
19496
607191a45f8c checkunfinished: accommodate histedit quirk
Matt Mackall <mpm@selenic.com>
parents: 19482
diff changeset
  2450
    ('graftstate', True, False, _('graft in progress'),
19482
499fc471296b update: add tracking of interrupted updates (issue3113)
Matt Mackall <mpm@selenic.com>
parents: 19474
diff changeset
  2451
     _("use 'hg graft --continue' or 'hg update' to abort")),
19496
607191a45f8c checkunfinished: accommodate histedit quirk
Matt Mackall <mpm@selenic.com>
parents: 19482
diff changeset
  2452
    ('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
  2453
     _("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
  2454
    ]
894fd1a7c533 cmdutil: core functionality to block during multistep commands (issue3955)
Matt Mackall <mpm@selenic.com>
parents: 19293
diff changeset
  2455
19496
607191a45f8c checkunfinished: accommodate histedit quirk
Matt Mackall <mpm@selenic.com>
parents: 19482
diff changeset
  2456
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
  2457
    '''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
  2458
    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
  2459
    bailifchanged().
894fd1a7c533 cmdutil: core functionality to block during multistep commands (issue3955)
Matt Mackall <mpm@selenic.com>
parents: 19293
diff changeset
  2460
    '''
19496
607191a45f8c checkunfinished: accommodate histedit quirk
Matt Mackall <mpm@selenic.com>
parents: 19482
diff changeset
  2461
    for f, clearable, allowcommit, msg, hint in unfinishedstates:
607191a45f8c checkunfinished: accommodate histedit quirk
Matt Mackall <mpm@selenic.com>
parents: 19482
diff changeset
  2462
        if commit and allowcommit:
607191a45f8c checkunfinished: accommodate histedit quirk
Matt Mackall <mpm@selenic.com>
parents: 19482
diff changeset
  2463
            continue
19474
894fd1a7c533 cmdutil: core functionality to block during multistep commands (issue3955)
Matt Mackall <mpm@selenic.com>
parents: 19293
diff changeset
  2464
        if repo.vfs.exists(f):
894fd1a7c533 cmdutil: core functionality to block during multistep commands (issue3955)
Matt Mackall <mpm@selenic.com>
parents: 19293
diff changeset
  2465
            raise util.Abort(msg, hint=hint)
894fd1a7c533 cmdutil: core functionality to block during multistep commands (issue3955)
Matt Mackall <mpm@selenic.com>
parents: 19293
diff changeset
  2466
894fd1a7c533 cmdutil: core functionality to block during multistep commands (issue3955)
Matt Mackall <mpm@selenic.com>
parents: 19293
diff changeset
  2467
def clearunfinished(repo):
894fd1a7c533 cmdutil: core functionality to block during multistep commands (issue3955)
Matt Mackall <mpm@selenic.com>
parents: 19293
diff changeset
  2468
    '''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
  2469
    that are clearable.
894fd1a7c533 cmdutil: core functionality to block during multistep commands (issue3955)
Matt Mackall <mpm@selenic.com>
parents: 19293
diff changeset
  2470
    '''
19496
607191a45f8c checkunfinished: accommodate histedit quirk
Matt Mackall <mpm@selenic.com>
parents: 19482
diff changeset
  2471
    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
  2472
        if not clearable and repo.vfs.exists(f):
894fd1a7c533 cmdutil: core functionality to block during multistep commands (issue3955)
Matt Mackall <mpm@selenic.com>
parents: 19293
diff changeset
  2473
            raise util.Abort(msg, hint=hint)
19496
607191a45f8c checkunfinished: accommodate histedit quirk
Matt Mackall <mpm@selenic.com>
parents: 19482
diff changeset
  2474
    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
  2475
        if clearable and repo.vfs.exists(f):
894fd1a7c533 cmdutil: core functionality to block during multistep commands (issue3955)
Matt Mackall <mpm@selenic.com>
parents: 19293
diff changeset
  2476
            util.unlink(repo.join(f))