mercurial/cmdutil.py
author Matt Mackall <mpm@selenic.com>
Wed, 24 Nov 2010 15:56:32 -0600
changeset 13047 6c375e07d673
parent 12973 6e0a9f9227bd
child 13081 79184986658c
permissions -rw-r--r--
branch: operate on branch names in local string space where possible Previously, branch names were ideally manipulated as UTF-8 strings, because they were stored as UTF-8 in the dirstate and the changelog and could not be safely converted to the local encoding and back. However, only about 80% of branch name code was actually using the right encoding conventions. This patch uses the localstr addition to allow working on branch names as local strings, which simplifies handling so that the previously incorrect code becomes correct.
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 _
10463
5ddde896a19d remove unused imports
Brodie Rao <me+hg@dackz.net>
parents: 10402
diff changeset
    10
import os, sys, errno, re, glob, tempfile
11231
1107888a1ad1 cmdutil: cleanup imports
Alexander Solovyov <piranha@piranha.org.ua>
parents: 11223
diff changeset
    11
import util, templater, patch, error, encoding, templatekw
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
12176
ecab10820983 subrepos: add function for iterating over ctx subrepos
Martin Geisler <mg@lazybytes.net>
parents: 12175
diff changeset
    13
import similar, revset, subrepo
2874
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    14
3090
eeaf9bcdfa25 Move revision parsing into cmdutil.
Brendan Cully <brendan@kublai.com>
parents: 3072
diff changeset
    15
revrangesep = ':'
eeaf9bcdfa25 Move revision parsing into cmdutil.
Brendan Cully <brendan@kublai.com>
parents: 3072
diff changeset
    16
10401
6252852b4332 mq: add -Q option to all commands not in norepo
Brendan Cully <brendan@kublai.com>
parents: 10344
diff changeset
    17
def parsealiases(cmd):
6252852b4332 mq: add -Q option to all commands not in norepo
Brendan Cully <brendan@kublai.com>
parents: 10344
diff changeset
    18
    return cmd.lstrip("^").split("|")
6252852b4332 mq: add -Q option to all commands not in norepo
Brendan Cully <brendan@kublai.com>
parents: 10344
diff changeset
    19
7213
b4c035057d34 findcmd: have dispatch look up strict flag
Matt Mackall <mpm@selenic.com>
parents: 7121
diff changeset
    20
def findpossible(cmd, table, strict=False):
4549
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
    21
    """
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
    22
    Return cmd -> (aliases, command table entry)
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
    23
    for each matching command.
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
    24
    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
    25
    """
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
    26
    choice = {}
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
    27
    debugchoice = {}
5178
18a9fbb5cd78 dispatch: move command dispatching into its own module
Matt Mackall <mpm@selenic.com>
parents: 5177
diff changeset
    28
    for e in table.keys():
10401
6252852b4332 mq: add -Q option to all commands not in norepo
Brendan Cully <brendan@kublai.com>
parents: 10344
diff changeset
    29
        aliases = parsealiases(e)
4549
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
    30
        found = None
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
    31
        if cmd in aliases:
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
    32
            found = cmd
7213
b4c035057d34 findcmd: have dispatch look up strict flag
Matt Mackall <mpm@selenic.com>
parents: 7121
diff changeset
    33
        elif not strict:
4549
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
    34
            for a in aliases:
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
    35
                if a.startswith(cmd):
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
    36
                    found = a
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
    37
                    break
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
    38
        if found is not None:
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
    39
            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
    40
                debugchoice[found] = (aliases, table[e])
4549
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
    41
            else:
5178
18a9fbb5cd78 dispatch: move command dispatching into its own module
Matt Mackall <mpm@selenic.com>
parents: 5177
diff changeset
    42
                choice[found] = (aliases, table[e])
4549
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
    43
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
    44
    if not choice and debugchoice:
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
    45
        choice = debugchoice
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
    46
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
    47
    return choice
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
    48
7213
b4c035057d34 findcmd: have dispatch look up strict flag
Matt Mackall <mpm@selenic.com>
parents: 7121
diff changeset
    49
def findcmd(cmd, table, strict=True):
4549
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
    50
    """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
    51
    choice = findpossible(cmd, table, strict)
4549
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
    52
5915
d0576d065993 Prefer i in d over d.has_key(i)
Christian Ebert <blacktrash@gmx.net>
parents: 5843
diff changeset
    53
    if cmd in choice:
4549
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
    54
        return choice[cmd]
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
    55
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
    56
    if len(choice) > 1:
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
    57
        clist = choice.keys()
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
    58
        clist.sort()
7643
9a1ea6587557 error: move UnknownCommand and AmbiguousCommand
Matt Mackall <mpm@selenic.com>
parents: 7404
diff changeset
    59
        raise error.AmbiguousCommand(cmd, clist)
4549
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
    60
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
    61
    if choice:
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
    62
        return choice.values()[0]
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
    63
7643
9a1ea6587557 error: move UnknownCommand and AmbiguousCommand
Matt Mackall <mpm@selenic.com>
parents: 7404
diff changeset
    64
    raise error.UnknownCommand(cmd)
4549
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
    65
10402
d216fa04e48a mq: make init -Q do what qinit -c did
Brendan Cully <brendan@kublai.com>
parents: 10401
diff changeset
    66
def findrepo(p):
d216fa04e48a mq: make init -Q do what qinit -c did
Brendan Cully <brendan@kublai.com>
parents: 10401
diff changeset
    67
    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
    68
        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
    69
        if p == oldp:
d216fa04e48a mq: make init -Q do what qinit -c did
Brendan Cully <brendan@kublai.com>
parents: 10401
diff changeset
    70
            return None
d216fa04e48a mq: make init -Q do what qinit -c did
Brendan Cully <brendan@kublai.com>
parents: 10401
diff changeset
    71
d216fa04e48a mq: make init -Q do what qinit -c did
Brendan Cully <brendan@kublai.com>
parents: 10401
diff changeset
    72
    return p
d216fa04e48a mq: make init -Q do what qinit -c did
Brendan Cully <brendan@kublai.com>
parents: 10401
diff changeset
    73
4549
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
    74
def bail_if_changed(repo):
5716
be367cbafe70 cmdutil: make bail_if_changed bail on uncommitted merge
Matt Mackall <mpm@selenic.com>
parents: 5610
diff changeset
    75
    if repo.dirstate.parents()[1] != nullid:
be367cbafe70 cmdutil: make bail_if_changed bail on uncommitted merge
Matt Mackall <mpm@selenic.com>
parents: 5610
diff changeset
    76
        raise util.Abort(_('outstanding uncommitted merge'))
4549
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
    77
    modified, added, removed, deleted = repo.status()[:4]
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
    78
    if modified or added or removed or deleted:
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
    79
        raise util.Abort(_("outstanding uncommitted changes"))
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
    80
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
    81
def logmessage(opts):
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
    82
    """ 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
    83
    message = opts.get('message')
bd5c37d792e6 cmdutil.logmessage: options should be optional
Alexander Solovyov <piranha@piranha.org.ua>
parents: 7643
diff changeset
    84
    logfile = opts.get('logfile')
4549
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
    85
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
    86
    if message and logfile:
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
    87
        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
    88
                           'exclusive'))
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
    89
    if not message and logfile:
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
    90
        try:
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
    91
            if logfile == '-':
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
    92
                message = sys.stdin.read()
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
    93
            else:
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
    94
                message = open(logfile).read()
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
    95
        except IOError, inst:
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
    96
            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
    97
                             (logfile, inst.strerror))
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
    98
    return message
0c61124ad877 dispatch: move dispatching code to cmdutil
Matt Mackall <mpm@selenic.com>
parents: 4548
diff changeset
    99
6190
a79d9408806f Move finding/checking the log limit to cmdutil
Thomas Arendsen Hein <thomas@intevation.de>
parents: 6145
diff changeset
   100
def loglimit(opts):
a79d9408806f Move finding/checking the log limit to cmdutil
Thomas Arendsen Hein <thomas@intevation.de>
parents: 6145
diff changeset
   101
    """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
   102
    limit = opts.get('limit')
a79d9408806f Move finding/checking the log limit to cmdutil
Thomas Arendsen Hein <thomas@intevation.de>
parents: 6145
diff changeset
   103
    if limit:
a79d9408806f Move finding/checking the log limit to cmdutil
Thomas Arendsen Hein <thomas@intevation.de>
parents: 6145
diff changeset
   104
        try:
a79d9408806f Move finding/checking the log limit to cmdutil
Thomas Arendsen Hein <thomas@intevation.de>
parents: 6145
diff changeset
   105
            limit = int(limit)
a79d9408806f Move finding/checking the log limit to cmdutil
Thomas Arendsen Hein <thomas@intevation.de>
parents: 6145
diff changeset
   106
        except ValueError:
a79d9408806f Move finding/checking the log limit to cmdutil
Thomas Arendsen Hein <thomas@intevation.de>
parents: 6145
diff changeset
   107
            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
   108
        if limit <= 0:
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10264
diff changeset
   109
            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
   110
    else:
10111
27457d31ae3f cmdutil: replace sys.maxint with None as default value in loglimit
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 10061
diff changeset
   111
        limit = None
6190
a79d9408806f Move finding/checking the log limit to cmdutil
Thomas Arendsen Hein <thomas@intevation.de>
parents: 6145
diff changeset
   112
    return limit
a79d9408806f Move finding/checking the log limit to cmdutil
Thomas Arendsen Hein <thomas@intevation.de>
parents: 6145
diff changeset
   113
12619
7178f6fedb9d cat: fix cat without -r, broken by 0ae35296fbf4
Brodie Rao <brodie@bitheap.org>
parents: 12618
diff changeset
   114
def revsingle(repo, revspec, default='.'):
12618
0ae35296fbf4 revsets: introduce revsingle helper
Matt Mackall <mpm@selenic.com>
parents: 12617
diff changeset
   115
    if not revspec:
0ae35296fbf4 revsets: introduce revsingle helper
Matt Mackall <mpm@selenic.com>
parents: 12617
diff changeset
   116
        return repo[default]
0ae35296fbf4 revsets: introduce revsingle helper
Matt Mackall <mpm@selenic.com>
parents: 12617
diff changeset
   117
0ae35296fbf4 revsets: introduce revsingle helper
Matt Mackall <mpm@selenic.com>
parents: 12617
diff changeset
   118
    l = revrange(repo, [revspec])
0ae35296fbf4 revsets: introduce revsingle helper
Matt Mackall <mpm@selenic.com>
parents: 12617
diff changeset
   119
    if len(l) < 1:
12774
642afc8f50e7 cmdutil: mark string for i18n
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 12689
diff changeset
   120
        raise util.Abort(_('empty revision set'))
12618
0ae35296fbf4 revsets: introduce revsingle helper
Matt Mackall <mpm@selenic.com>
parents: 12617
diff changeset
   121
    return repo[l[-1]]
0ae35296fbf4 revsets: introduce revsingle helper
Matt Mackall <mpm@selenic.com>
parents: 12617
diff changeset
   122
3707
67f44b825784 Removed unused ui parameter from revpair/revrange and fix its users.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 3673
diff changeset
   123
def revpair(repo, revs):
3090
eeaf9bcdfa25 Move revision parsing into cmdutil.
Brendan Cully <brendan@kublai.com>
parents: 3072
diff changeset
   124
    if not revs:
eeaf9bcdfa25 Move revision parsing into cmdutil.
Brendan Cully <brendan@kublai.com>
parents: 3072
diff changeset
   125
        return repo.dirstate.parents()[0], None
12617
2063d36b406e revsets: make revpair revsets-aware
Matt Mackall <mpm@selenic.com>
parents: 12357
diff changeset
   126
2063d36b406e revsets: make revpair revsets-aware
Matt Mackall <mpm@selenic.com>
parents: 12357
diff changeset
   127
    l = revrange(repo, revs)
2063d36b406e revsets: make revpair revsets-aware
Matt Mackall <mpm@selenic.com>
parents: 12357
diff changeset
   128
2063d36b406e revsets: make revpair revsets-aware
Matt Mackall <mpm@selenic.com>
parents: 12357
diff changeset
   129
    if len(l) == 0:
2063d36b406e revsets: make revpair revsets-aware
Matt Mackall <mpm@selenic.com>
parents: 12357
diff changeset
   130
        return repo.dirstate.parents()[0], None
2063d36b406e revsets: make revpair revsets-aware
Matt Mackall <mpm@selenic.com>
parents: 12357
diff changeset
   131
2063d36b406e revsets: make revpair revsets-aware
Matt Mackall <mpm@selenic.com>
parents: 12357
diff changeset
   132
    if len(l) == 1:
2063d36b406e revsets: make revpair revsets-aware
Matt Mackall <mpm@selenic.com>
parents: 12357
diff changeset
   133
        return repo.lookup(l[0]), None
2063d36b406e revsets: make revpair revsets-aware
Matt Mackall <mpm@selenic.com>
parents: 12357
diff changeset
   134
2063d36b406e revsets: make revpair revsets-aware
Matt Mackall <mpm@selenic.com>
parents: 12357
diff changeset
   135
    return repo.lookup(l[0]), repo.lookup(l[-1])
3090
eeaf9bcdfa25 Move revision parsing into cmdutil.
Brendan Cully <brendan@kublai.com>
parents: 3072
diff changeset
   136
3707
67f44b825784 Removed unused ui parameter from revpair/revrange and fix its users.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 3673
diff changeset
   137
def revrange(repo, revs):
3090
eeaf9bcdfa25 Move revision parsing into cmdutil.
Brendan Cully <brendan@kublai.com>
parents: 3072
diff changeset
   138
    """Yield revision as strings from a list of revision specifications."""
3525
cf0f8d9256c7 simplify revrange and revpair
Matt Mackall <mpm@selenic.com>
parents: 3524
diff changeset
   139
cf0f8d9256c7 simplify revrange and revpair
Matt Mackall <mpm@selenic.com>
parents: 3524
diff changeset
   140
    def revfix(repo, val, defval):
3718
7db88b094b14 fix hg log -r ''
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 3707
diff changeset
   141
        if not val and val != 0 and defval is not None:
3525
cf0f8d9256c7 simplify revrange and revpair
Matt Mackall <mpm@selenic.com>
parents: 3524
diff changeset
   142
            return defval
cf0f8d9256c7 simplify revrange and revpair
Matt Mackall <mpm@selenic.com>
parents: 3524
diff changeset
   143
        return repo.changelog.rev(repo.lookup(val))
cf0f8d9256c7 simplify revrange and revpair
Matt Mackall <mpm@selenic.com>
parents: 3524
diff changeset
   144
8368
52e6117a9940 cmdutil: replace pseudo-set by real set
Martin Geisler <mg@lazybytes.net>
parents: 8360
diff changeset
   145
    seen, l = set(), []
3090
eeaf9bcdfa25 Move revision parsing into cmdutil.
Brendan Cully <brendan@kublai.com>
parents: 3072
diff changeset
   146
    for spec in revs:
11405
bf5d88c466e0 revrange: attempt to parse old-style queries as a first pass
Matt Mackall <mpm@selenic.com>
parents: 11303
diff changeset
   147
        # attempt to parse old-style ranges first to deal with
bf5d88c466e0 revrange: attempt to parse old-style queries as a first pass
Matt Mackall <mpm@selenic.com>
parents: 11303
diff changeset
   148
        # things like old-tag which contain query metacharacters
bf5d88c466e0 revrange: attempt to parse old-style queries as a first pass
Matt Mackall <mpm@selenic.com>
parents: 11303
diff changeset
   149
        try:
12925
6eab8f0df2ca commands: add revset support to most commands
Matt Mackall <mpm@selenic.com>
parents: 12900
diff changeset
   150
            if isinstance(spec, int):
6eab8f0df2ca commands: add revset support to most commands
Matt Mackall <mpm@selenic.com>
parents: 12900
diff changeset
   151
                seen.add(spec)
6eab8f0df2ca commands: add revset support to most commands
Matt Mackall <mpm@selenic.com>
parents: 12900
diff changeset
   152
                l.append(spec)
6eab8f0df2ca commands: add revset support to most commands
Matt Mackall <mpm@selenic.com>
parents: 12900
diff changeset
   153
                continue
6eab8f0df2ca commands: add revset support to most commands
Matt Mackall <mpm@selenic.com>
parents: 12900
diff changeset
   154
11405
bf5d88c466e0 revrange: attempt to parse old-style queries as a first pass
Matt Mackall <mpm@selenic.com>
parents: 11303
diff changeset
   155
            if revrangesep in spec:
bf5d88c466e0 revrange: attempt to parse old-style queries as a first pass
Matt Mackall <mpm@selenic.com>
parents: 11303
diff changeset
   156
                start, end = spec.split(revrangesep, 1)
bf5d88c466e0 revrange: attempt to parse old-style queries as a first pass
Matt Mackall <mpm@selenic.com>
parents: 11303
diff changeset
   157
                start = revfix(repo, start, 0)
bf5d88c466e0 revrange: attempt to parse old-style queries as a first pass
Matt Mackall <mpm@selenic.com>
parents: 11303
diff changeset
   158
                end = revfix(repo, end, len(repo) - 1)
bf5d88c466e0 revrange: attempt to parse old-style queries as a first pass
Matt Mackall <mpm@selenic.com>
parents: 11303
diff changeset
   159
                step = start > end and -1 or 1
bf5d88c466e0 revrange: attempt to parse old-style queries as a first pass
Matt Mackall <mpm@selenic.com>
parents: 11303
diff changeset
   160
                for rev in xrange(start, end + step, step):
bf5d88c466e0 revrange: attempt to parse old-style queries as a first pass
Matt Mackall <mpm@selenic.com>
parents: 11303
diff changeset
   161
                    if rev in seen:
bf5d88c466e0 revrange: attempt to parse old-style queries as a first pass
Matt Mackall <mpm@selenic.com>
parents: 11303
diff changeset
   162
                        continue
bf5d88c466e0 revrange: attempt to parse old-style queries as a first pass
Matt Mackall <mpm@selenic.com>
parents: 11303
diff changeset
   163
                    seen.add(rev)
bf5d88c466e0 revrange: attempt to parse old-style queries as a first pass
Matt Mackall <mpm@selenic.com>
parents: 11303
diff changeset
   164
                    l.append(rev)
bf5d88c466e0 revrange: attempt to parse old-style queries as a first pass
Matt Mackall <mpm@selenic.com>
parents: 11303
diff changeset
   165
                continue
11410
38d4c9b953fe revrange: fix up empty query again
Matt Mackall <mpm@selenic.com>
parents: 11405
diff changeset
   166
            elif spec and spec in repo: # single unquoted rev
11405
bf5d88c466e0 revrange: attempt to parse old-style queries as a first pass
Matt Mackall <mpm@selenic.com>
parents: 11303
diff changeset
   167
                rev = revfix(repo, spec, None)
3090
eeaf9bcdfa25 Move revision parsing into cmdutil.
Brendan Cully <brendan@kublai.com>
parents: 3072
diff changeset
   168
                if rev in seen:
eeaf9bcdfa25 Move revision parsing into cmdutil.
Brendan Cully <brendan@kublai.com>
parents: 3072
diff changeset
   169
                    continue
8368
52e6117a9940 cmdutil: replace pseudo-set by real set
Martin Geisler <mg@lazybytes.net>
parents: 8360
diff changeset
   170
                seen.add(rev)
3526
68341c06bc61 Make revrange return a list of ints so that callers don't have to convert
Matt Mackall <mpm@selenic.com>
parents: 3525
diff changeset
   171
                l.append(rev)
11410
38d4c9b953fe revrange: fix up empty query again
Matt Mackall <mpm@selenic.com>
parents: 11405
diff changeset
   172
                continue
11405
bf5d88c466e0 revrange: attempt to parse old-style queries as a first pass
Matt Mackall <mpm@selenic.com>
parents: 11303
diff changeset
   173
        except error.RepoLookupError:
bf5d88c466e0 revrange: attempt to parse old-style queries as a first pass
Matt Mackall <mpm@selenic.com>
parents: 11303
diff changeset
   174
            pass
bf5d88c466e0 revrange: attempt to parse old-style queries as a first pass
Matt Mackall <mpm@selenic.com>
parents: 11303
diff changeset
   175
bf5d88c466e0 revrange: attempt to parse old-style queries as a first pass
Matt Mackall <mpm@selenic.com>
parents: 11303
diff changeset
   176
        # fall through to new-style queries if old-style fails
bf5d88c466e0 revrange: attempt to parse old-style queries as a first pass
Matt Mackall <mpm@selenic.com>
parents: 11303
diff changeset
   177
        m = revset.match(spec)
bf5d88c466e0 revrange: attempt to parse old-style queries as a first pass
Matt Mackall <mpm@selenic.com>
parents: 11303
diff changeset
   178
        for r in m(repo, range(len(repo))):
bf5d88c466e0 revrange: attempt to parse old-style queries as a first pass
Matt Mackall <mpm@selenic.com>
parents: 11303
diff changeset
   179
            if r not in seen:
bf5d88c466e0 revrange: attempt to parse old-style queries as a first pass
Matt Mackall <mpm@selenic.com>
parents: 11303
diff changeset
   180
                l.append(r)
bf5d88c466e0 revrange: attempt to parse old-style queries as a first pass
Matt Mackall <mpm@selenic.com>
parents: 11303
diff changeset
   181
        seen.update(l)
3526
68341c06bc61 Make revrange return a list of ints so that callers don't have to convert
Matt Mackall <mpm@selenic.com>
parents: 3525
diff changeset
   182
68341c06bc61 Make revrange return a list of ints so that callers don't have to convert
Matt Mackall <mpm@selenic.com>
parents: 3525
diff changeset
   183
    return l
3090
eeaf9bcdfa25 Move revision parsing into cmdutil.
Brendan Cully <brendan@kublai.com>
parents: 3072
diff changeset
   184
2874
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   185
def make_filename(repo, pat, node,
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   186
                  total=None, seqno=None, revwidth=None, pathname=None):
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   187
    node_expander = {
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   188
        'H': lambda: hex(node),
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   189
        'R': lambda: str(repo.changelog.rev(node)),
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   190
        'h': lambda: short(node),
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   191
        }
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   192
    expander = {
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   193
        '%': lambda: '%',
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   194
        'b': lambda: os.path.basename(repo.root),
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   195
        }
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   196
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   197
    try:
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   198
        if node:
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   199
            expander.update(node_expander)
4836
0e2d0a78f81a archive: make the %r escape work.
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4825
diff changeset
   200
        if node:
2874
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   201
            expander['r'] = (lambda:
4836
0e2d0a78f81a archive: make the %r escape work.
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4825
diff changeset
   202
                    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
   203
        if total is not None:
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   204
            expander['N'] = lambda: str(total)
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   205
        if seqno is not None:
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   206
            expander['n'] = lambda: str(seqno)
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   207
        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
   208
            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
   209
        if pathname is not None:
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   210
            expander['s'] = lambda: os.path.basename(pathname)
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   211
            expander['d'] = lambda: os.path.dirname(pathname) or '.'
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   212
            expander['p'] = lambda: pathname
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   213
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   214
        newname = []
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   215
        patlen = len(pat)
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   216
        i = 0
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   217
        while i < patlen:
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   218
            c = pat[i]
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   219
            if c == '%':
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   220
                i += 1
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   221
                c = pat[i]
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   222
                c = expander[c]()
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   223
            newname.append(c)
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   224
            i += 1
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   225
        return ''.join(newname)
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   226
    except KeyError, inst:
8761
0289f384e1e5 Generally replace "file name" with "filename" in help and comments.
timeless <timeless@gmail.com>
parents: 8731
diff changeset
   227
        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
   228
                         inst.args[0])
2874
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   229
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   230
def make_file(repo, pat, node=None,
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   231
              total=None, seqno=None, revwidth=None, mode='wb', pathname=None):
7319
eae1767cc6a8 export: fixed silent output file overwriting
Ronny Pfannschmidt <Ronny.Pfannschmidt@gmx.de>
parents: 7308
diff changeset
   232
eae1767cc6a8 export: fixed silent output file overwriting
Ronny Pfannschmidt <Ronny.Pfannschmidt@gmx.de>
parents: 7308
diff changeset
   233
    writable = 'w' in mode or 'a' in mode
eae1767cc6a8 export: fixed silent output file overwriting
Ronny Pfannschmidt <Ronny.Pfannschmidt@gmx.de>
parents: 7308
diff changeset
   234
2874
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   235
    if not pat or pat == '-':
7319
eae1767cc6a8 export: fixed silent output file overwriting
Ronny Pfannschmidt <Ronny.Pfannschmidt@gmx.de>
parents: 7308
diff changeset
   236
        return writable and sys.stdout or sys.stdin
eae1767cc6a8 export: fixed silent output file overwriting
Ronny Pfannschmidt <Ronny.Pfannschmidt@gmx.de>
parents: 7308
diff changeset
   237
    if hasattr(pat, 'write') and writable:
2874
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   238
        return pat
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   239
    if hasattr(pat, 'read') and 'r' in mode:
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   240
        return pat
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   241
    return open(make_filename(repo, pat, node, total, seqno, revwidth,
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   242
                              pathname),
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   243
                mode)
2882
cf98cd70d2c4 move walk and matchpats from commands to cmdutil.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2874
diff changeset
   244
8614
573734e7e6d0 cmdutils: Take over glob expansion duties from util
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   245
def expandpats(pats):
573734e7e6d0 cmdutils: Take over glob expansion duties from util
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   246
    if not util.expandglobs:
573734e7e6d0 cmdutils: Take over glob expansion duties from util
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   247
        return list(pats)
573734e7e6d0 cmdutils: Take over glob expansion duties from util
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   248
    ret = []
573734e7e6d0 cmdutils: Take over glob expansion duties from util
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   249
    for p in pats:
12085
6f833fc3ccab Consistently import foo as foomod when foo to avoid shadowing
Martin Geisler <mg@aragost.com>
parents: 12032
diff changeset
   250
        kind, name = matchmod._patsplit(p, None)
8614
573734e7e6d0 cmdutils: Take over glob expansion duties from util
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   251
        if kind is None:
9118
78e54b9f3a62 cmdutil: fall back to filename if glob expand has errors
Steve Borho <steve@borho.org>
parents: 8994
diff changeset
   252
            try:
78e54b9f3a62 cmdutil: fall back to filename if glob expand has errors
Steve Borho <steve@borho.org>
parents: 8994
diff changeset
   253
                globbed = glob.glob(name)
78e54b9f3a62 cmdutil: fall back to filename if glob expand has errors
Steve Borho <steve@borho.org>
parents: 8994
diff changeset
   254
            except re.error:
78e54b9f3a62 cmdutil: fall back to filename if glob expand has errors
Steve Borho <steve@borho.org>
parents: 8994
diff changeset
   255
                globbed = [name]
8614
573734e7e6d0 cmdutils: Take over glob expansion duties from util
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   256
            if globbed:
573734e7e6d0 cmdutils: Take over glob expansion duties from util
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   257
                ret.extend(globbed)
573734e7e6d0 cmdutils: Take over glob expansion duties from util
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   258
                continue
573734e7e6d0 cmdutils: Take over glob expansion duties from util
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   259
        ret.append(p)
573734e7e6d0 cmdutils: Take over glob expansion duties from util
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   260
    return ret
573734e7e6d0 cmdutils: Take over glob expansion duties from util
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   261
6579
0159b7a36184 walk: pass match object to cmdutil.walk
Matt Mackall <mpm@selenic.com>
parents: 6578
diff changeset
   262
def match(repo, pats=[], opts={}, globbed=False, default='relpath'):
6575
e08e0367ba15 walk: kill util.cmdmatcher and _matcher
Matt Mackall <mpm@selenic.com>
parents: 6566
diff changeset
   263
    if not globbed and default == 'relpath':
8614
573734e7e6d0 cmdutils: Take over glob expansion duties from util
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   264
        pats = expandpats(pats or [])
12085
6f833fc3ccab Consistently import foo as foomod when foo to avoid shadowing
Martin Geisler <mg@aragost.com>
parents: 12032
diff changeset
   265
    m = matchmod.match(repo.root, repo.getcwd(), pats,
12164
1849b6147831 cmdutil: use repo.auditor when constructing match object
Martin Geisler <mg@lazybytes.net>
parents: 12085
diff changeset
   266
                       opts.get('include'), opts.get('exclude'), default,
1849b6147831 cmdutil: use repo.auditor when constructing match object
Martin Geisler <mg@lazybytes.net>
parents: 12085
diff changeset
   267
                       auditor=repo.auditor)
6578
f242d3684f83 walk: begin refactoring badmatch handling
Matt Mackall <mpm@selenic.com>
parents: 6577
diff changeset
   268
    def badfn(f, msg):
f242d3684f83 walk: begin refactoring badmatch handling
Matt Mackall <mpm@selenic.com>
parents: 6577
diff changeset
   269
        repo.ui.warn("%s: %s\n" % (m.rel(f), msg))
f242d3684f83 walk: begin refactoring badmatch handling
Matt Mackall <mpm@selenic.com>
parents: 6577
diff changeset
   270
    m.bad = badfn
6579
0159b7a36184 walk: pass match object to cmdutil.walk
Matt Mackall <mpm@selenic.com>
parents: 6578
diff changeset
   271
    return m
2882
cf98cd70d2c4 move walk and matchpats from commands to cmdutil.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2874
diff changeset
   272
6597
371415a2b959 match: use helpers for cmdutil
Matt Mackall <mpm@selenic.com>
parents: 6586
diff changeset
   273
def matchall(repo):
12085
6f833fc3ccab Consistently import foo as foomod when foo to avoid shadowing
Martin Geisler <mg@aragost.com>
parents: 12032
diff changeset
   274
    return matchmod.always(repo.root, repo.getcwd())
6597
371415a2b959 match: use helpers for cmdutil
Matt Mackall <mpm@selenic.com>
parents: 6586
diff changeset
   275
371415a2b959 match: use helpers for cmdutil
Matt Mackall <mpm@selenic.com>
parents: 6586
diff changeset
   276
def matchfiles(repo, files):
12085
6f833fc3ccab Consistently import foo as foomod when foo to avoid shadowing
Martin Geisler <mg@aragost.com>
parents: 12032
diff changeset
   277
    return matchmod.exact(repo.root, repo.getcwd(), files)
2883
c2932ad5476a move commands.addremove_lock to cmdutil.addremove
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2882
diff changeset
   278
4917
126f527b3ba3 Make repo locks recursive, eliminate all passing of lock/wlock
Matt Mackall <mpm@selenic.com>
parents: 4906
diff changeset
   279
def addremove(repo, pats=[], opts={}, dry_run=None, similarity=None):
2883
c2932ad5476a move commands.addremove_lock to cmdutil.addremove
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2882
diff changeset
   280
    if dry_run is None:
c2932ad5476a move commands.addremove_lock to cmdutil.addremove
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2882
diff changeset
   281
        dry_run = opts.get('dry_run')
2958
ff3ea21a981a addremove: add -s/--similarity option
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2957
diff changeset
   282
    if similarity is None:
ff3ea21a981a addremove: add -s/--similarity option
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2957
diff changeset
   283
        similarity = float(opts.get('similarity') or 0)
8990
627399330c7d addremove: build lists of already added and removed files too (issue1696)
Matt Mackall <mpm@selenic.com>
parents: 8989
diff changeset
   284
    # we'd use status here, except handling of symlinks and ignore is tricky
627399330c7d addremove: build lists of already added and removed files too (issue1696)
Matt Mackall <mpm@selenic.com>
parents: 8989
diff changeset
   285
    added, unknown, deleted, removed = [], [], [], []
6651
7f0dd352fb4d addremove: correctly handle intermediate symlinks
Maxim Dounin <mdounin@mdounin.ru>
parents: 6566
diff changeset
   286
    audit_path = util.path_auditor(repo.root)
6579
0159b7a36184 walk: pass match object to cmdutil.walk
Matt Mackall <mpm@selenic.com>
parents: 6578
diff changeset
   287
    m = match(repo, pats, opts)
6586
d3463007d368 walk: return a single value
Matt Mackall <mpm@selenic.com>
parents: 6585
diff changeset
   288
    for abs in repo.walk(m):
4522
591322269fed Use absolute paths in addremove.
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4478
diff changeset
   289
        target = repo.wjoin(abs)
6651
7f0dd352fb4d addremove: correctly handle intermediate symlinks
Maxim Dounin <mdounin@mdounin.ru>
parents: 6566
diff changeset
   290
        good = True
7f0dd352fb4d addremove: correctly handle intermediate symlinks
Maxim Dounin <mdounin@mdounin.ru>
parents: 6566
diff changeset
   291
        try:
7f0dd352fb4d addremove: correctly handle intermediate symlinks
Maxim Dounin <mdounin@mdounin.ru>
parents: 6566
diff changeset
   292
            audit_path(abs)
7f0dd352fb4d addremove: correctly handle intermediate symlinks
Maxim Dounin <mdounin@mdounin.ru>
parents: 6566
diff changeset
   293
        except:
7f0dd352fb4d addremove: correctly handle intermediate symlinks
Maxim Dounin <mdounin@mdounin.ru>
parents: 6566
diff changeset
   294
            good = False
6584
29c77e5dfb3c walk: remove rel and exact returns
Matt Mackall <mpm@selenic.com>
parents: 6582
diff changeset
   295
        rel = m.rel(abs)
29c77e5dfb3c walk: remove rel and exact returns
Matt Mackall <mpm@selenic.com>
parents: 6582
diff changeset
   296
        exact = m.exact(abs)
6656
2cbe0f72c379 Merge with crew-stable
Patrick Mezard <pmezard@gmail.com>
parents: 6603 6651
diff changeset
   297
        if good and abs not in repo.dirstate:
8988
1247751d9bf8 addremove: normalize some variable names
Matt Mackall <mpm@selenic.com>
parents: 8987
diff changeset
   298
            unknown.append(abs)
2883
c2932ad5476a move commands.addremove_lock to cmdutil.addremove
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2882
diff changeset
   299
            if repo.ui.verbose or not exact:
c2932ad5476a move commands.addremove_lock to cmdutil.addremove
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2882
diff changeset
   300
                repo.ui.status(_('adding %s\n') % ((pats and rel) or abs))
12032
ad787252fed6 util: remove lexists, Python 2.4 introduced os.path.lexists
Martin Geisler <mg@lazybytes.net>
parents: 11958
diff changeset
   301
        elif repo.dirstate[abs] != 'r' and (not good or not os.path.lexists(target)
5487
7a64931e2d76 Fix file-changed-to-dir and dir-to-file commits (issue660).
Maxim Dounin <mdounin@mdounin.ru>
parents: 5248
diff changeset
   302
            or (os.path.isdir(target) and not os.path.islink(target))):
8988
1247751d9bf8 addremove: normalize some variable names
Matt Mackall <mpm@selenic.com>
parents: 8987
diff changeset
   303
            deleted.append(abs)
2883
c2932ad5476a move commands.addremove_lock to cmdutil.addremove
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2882
diff changeset
   304
            if repo.ui.verbose or not exact:
c2932ad5476a move commands.addremove_lock to cmdutil.addremove
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2882
diff changeset
   305
                repo.ui.status(_('removing %s\n') % ((pats and rel) or abs))
8990
627399330c7d addremove: build lists of already added and removed files too (issue1696)
Matt Mackall <mpm@selenic.com>
parents: 8989
diff changeset
   306
        # for finding renames
627399330c7d addremove: build lists of already added and removed files too (issue1696)
Matt Mackall <mpm@selenic.com>
parents: 8989
diff changeset
   307
        elif repo.dirstate[abs] == 'r':
627399330c7d addremove: build lists of already added and removed files too (issue1696)
Matt Mackall <mpm@selenic.com>
parents: 8989
diff changeset
   308
            removed.append(abs)
627399330c7d addremove: build lists of already added and removed files too (issue1696)
Matt Mackall <mpm@selenic.com>
parents: 8989
diff changeset
   309
        elif repo.dirstate[abs] == 'a':
627399330c7d addremove: build lists of already added and removed files too (issue1696)
Matt Mackall <mpm@selenic.com>
parents: 8989
diff changeset
   310
            added.append(abs)
10606
5868dd69fb03 addremove: atomically update the dirstate
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10463
diff changeset
   311
    copies = {}
2958
ff3ea21a981a addremove: add -s/--similarity option
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2957
diff changeset
   312
    if similarity > 0:
11059
ef4aa90b1e58 Move 'findrenames' code into its own file.
David Greenaway <hg-dev@davidgreenaway.com>
parents: 11050
diff changeset
   313
        for old, new, score in similar.findrenames(repo,
ef4aa90b1e58 Move 'findrenames' code into its own file.
David Greenaway <hg-dev@davidgreenaway.com>
parents: 11050
diff changeset
   314
                added + unknown, removed + deleted, similarity):
8941
37a9d551346c addremove: drop some silly variable assignments
Matt Mackall <mpm@selenic.com>
parents: 8798
diff changeset
   315
            if repo.ui.verbose or not m.exact(old) or not m.exact(new):
2958
ff3ea21a981a addremove: add -s/--similarity option
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2957
diff changeset
   316
                repo.ui.status(_('recording removal of %s as rename to %s '
ff3ea21a981a addremove: add -s/--similarity option
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2957
diff changeset
   317
                                 '(%d%% similar)\n') %
8941
37a9d551346c addremove: drop some silly variable assignments
Matt Mackall <mpm@selenic.com>
parents: 8798
diff changeset
   318
                               (m.rel(old), m.rel(new), score * 100))
10606
5868dd69fb03 addremove: atomically update the dirstate
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10463
diff changeset
   319
            copies[new] = old
5868dd69fb03 addremove: atomically update the dirstate
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10463
diff changeset
   320
5868dd69fb03 addremove: atomically update the dirstate
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10463
diff changeset
   321
    if not dry_run:
11303
a1aad8333864 move working dir/dirstate methods from localrepo to workingctx
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 11290
diff changeset
   322
        wctx = repo[None]
10606
5868dd69fb03 addremove: atomically update the dirstate
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10463
diff changeset
   323
        wlock = repo.wlock()
5868dd69fb03 addremove: atomically update the dirstate
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10463
diff changeset
   324
        try:
11303
a1aad8333864 move working dir/dirstate methods from localrepo to workingctx
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 11290
diff changeset
   325
            wctx.remove(deleted)
a1aad8333864 move working dir/dirstate methods from localrepo to workingctx
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 11290
diff changeset
   326
            wctx.add(unknown)
10606
5868dd69fb03 addremove: atomically update the dirstate
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10463
diff changeset
   327
            for new, old in copies.iteritems():
11303
a1aad8333864 move working dir/dirstate methods from localrepo to workingctx
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 11290
diff changeset
   328
                wctx.copy(old, new)
10606
5868dd69fb03 addremove: atomically update the dirstate
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10463
diff changeset
   329
        finally:
5868dd69fb03 addremove: atomically update the dirstate
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10463
diff changeset
   330
            wlock.release()
3643
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   331
12266
00658492e2aa patch: break import cycle with cmdutil
Martin Geisler <mg@lazybytes.net>
parents: 12209
diff changeset
   332
def updatedir(ui, repo, patches, similarity=0):
00658492e2aa patch: break import cycle with cmdutil
Martin Geisler <mg@lazybytes.net>
parents: 12209
diff changeset
   333
    '''Update dirstate after patch application according to metadata'''
00658492e2aa patch: break import cycle with cmdutil
Martin Geisler <mg@lazybytes.net>
parents: 12209
diff changeset
   334
    if not patches:
00658492e2aa patch: break import cycle with cmdutil
Martin Geisler <mg@lazybytes.net>
parents: 12209
diff changeset
   335
        return
00658492e2aa patch: break import cycle with cmdutil
Martin Geisler <mg@lazybytes.net>
parents: 12209
diff changeset
   336
    copies = []
00658492e2aa patch: break import cycle with cmdutil
Martin Geisler <mg@lazybytes.net>
parents: 12209
diff changeset
   337
    removes = set()
00658492e2aa patch: break import cycle with cmdutil
Martin Geisler <mg@lazybytes.net>
parents: 12209
diff changeset
   338
    cfiles = patches.keys()
00658492e2aa patch: break import cycle with cmdutil
Martin Geisler <mg@lazybytes.net>
parents: 12209
diff changeset
   339
    cwd = repo.getcwd()
00658492e2aa patch: break import cycle with cmdutil
Martin Geisler <mg@lazybytes.net>
parents: 12209
diff changeset
   340
    if cwd:
00658492e2aa patch: break import cycle with cmdutil
Martin Geisler <mg@lazybytes.net>
parents: 12209
diff changeset
   341
        cfiles = [util.pathto(repo.root, cwd, f) for f in patches.keys()]
00658492e2aa patch: break import cycle with cmdutil
Martin Geisler <mg@lazybytes.net>
parents: 12209
diff changeset
   342
    for f in patches:
00658492e2aa patch: break import cycle with cmdutil
Martin Geisler <mg@lazybytes.net>
parents: 12209
diff changeset
   343
        gp = patches[f]
00658492e2aa patch: break import cycle with cmdutil
Martin Geisler <mg@lazybytes.net>
parents: 12209
diff changeset
   344
        if not gp:
00658492e2aa patch: break import cycle with cmdutil
Martin Geisler <mg@lazybytes.net>
parents: 12209
diff changeset
   345
            continue
00658492e2aa patch: break import cycle with cmdutil
Martin Geisler <mg@lazybytes.net>
parents: 12209
diff changeset
   346
        if gp.op == 'RENAME':
00658492e2aa patch: break import cycle with cmdutil
Martin Geisler <mg@lazybytes.net>
parents: 12209
diff changeset
   347
            copies.append((gp.oldpath, gp.path))
00658492e2aa patch: break import cycle with cmdutil
Martin Geisler <mg@lazybytes.net>
parents: 12209
diff changeset
   348
            removes.add(gp.oldpath)
00658492e2aa patch: break import cycle with cmdutil
Martin Geisler <mg@lazybytes.net>
parents: 12209
diff changeset
   349
        elif gp.op == 'COPY':
00658492e2aa patch: break import cycle with cmdutil
Martin Geisler <mg@lazybytes.net>
parents: 12209
diff changeset
   350
            copies.append((gp.oldpath, gp.path))
00658492e2aa patch: break import cycle with cmdutil
Martin Geisler <mg@lazybytes.net>
parents: 12209
diff changeset
   351
        elif gp.op == 'DELETE':
00658492e2aa patch: break import cycle with cmdutil
Martin Geisler <mg@lazybytes.net>
parents: 12209
diff changeset
   352
            removes.add(gp.path)
00658492e2aa patch: break import cycle with cmdutil
Martin Geisler <mg@lazybytes.net>
parents: 12209
diff changeset
   353
00658492e2aa patch: break import cycle with cmdutil
Martin Geisler <mg@lazybytes.net>
parents: 12209
diff changeset
   354
    wctx = repo[None]
00658492e2aa patch: break import cycle with cmdutil
Martin Geisler <mg@lazybytes.net>
parents: 12209
diff changeset
   355
    for src, dst in copies:
12874
bb7bf43b72fb patch: fix copies when patching over uncommitted changed (issue2459)
Patrick Mezard <pmezard@gmail.com>
parents: 12774
diff changeset
   356
        dirstatecopy(ui, repo, wctx, src, dst, cwd=cwd)
12266
00658492e2aa patch: break import cycle with cmdutil
Martin Geisler <mg@lazybytes.net>
parents: 12209
diff changeset
   357
    if (not similarity) and removes:
00658492e2aa patch: break import cycle with cmdutil
Martin Geisler <mg@lazybytes.net>
parents: 12209
diff changeset
   358
        wctx.remove(sorted(removes), True)
00658492e2aa patch: break import cycle with cmdutil
Martin Geisler <mg@lazybytes.net>
parents: 12209
diff changeset
   359
00658492e2aa patch: break import cycle with cmdutil
Martin Geisler <mg@lazybytes.net>
parents: 12209
diff changeset
   360
    for f in patches:
00658492e2aa patch: break import cycle with cmdutil
Martin Geisler <mg@lazybytes.net>
parents: 12209
diff changeset
   361
        gp = patches[f]
00658492e2aa patch: break import cycle with cmdutil
Martin Geisler <mg@lazybytes.net>
parents: 12209
diff changeset
   362
        if gp and gp.mode:
00658492e2aa patch: break import cycle with cmdutil
Martin Geisler <mg@lazybytes.net>
parents: 12209
diff changeset
   363
            islink, isexec = gp.mode
00658492e2aa patch: break import cycle with cmdutil
Martin Geisler <mg@lazybytes.net>
parents: 12209
diff changeset
   364
            dst = repo.wjoin(gp.path)
00658492e2aa patch: break import cycle with cmdutil
Martin Geisler <mg@lazybytes.net>
parents: 12209
diff changeset
   365
            # patch won't create empty files
12357
cb59654c2c7a Restore lexists() changes lost in e0ee3e822a9a merge
Patrick Mezard <pmezard@gmail.com>
parents: 12345
diff changeset
   366
            if gp.op == 'ADD' and not os.path.lexists(dst):
12266
00658492e2aa patch: break import cycle with cmdutil
Martin Geisler <mg@lazybytes.net>
parents: 12209
diff changeset
   367
                flags = (isexec and 'x' or '') + (islink and 'l' or '')
00658492e2aa patch: break import cycle with cmdutil
Martin Geisler <mg@lazybytes.net>
parents: 12209
diff changeset
   368
                repo.wwrite(gp.path, '', flags)
00658492e2aa patch: break import cycle with cmdutil
Martin Geisler <mg@lazybytes.net>
parents: 12209
diff changeset
   369
            util.set_flags(dst, islink, isexec)
00658492e2aa patch: break import cycle with cmdutil
Martin Geisler <mg@lazybytes.net>
parents: 12209
diff changeset
   370
    addremove(repo, cfiles, similarity=similarity)
00658492e2aa patch: break import cycle with cmdutil
Martin Geisler <mg@lazybytes.net>
parents: 12209
diff changeset
   371
    files = patches.keys()
00658492e2aa patch: break import cycle with cmdutil
Martin Geisler <mg@lazybytes.net>
parents: 12209
diff changeset
   372
    files.extend([r for r in removes if r not in files])
00658492e2aa patch: break import cycle with cmdutil
Martin Geisler <mg@lazybytes.net>
parents: 12209
diff changeset
   373
    return sorted(files)
00658492e2aa patch: break import cycle with cmdutil
Martin Geisler <mg@lazybytes.net>
parents: 12209
diff changeset
   374
12874
bb7bf43b72fb patch: fix copies when patching over uncommitted changed (issue2459)
Patrick Mezard <pmezard@gmail.com>
parents: 12774
diff changeset
   375
def dirstatecopy(ui, repo, wctx, src, dst, dryrun=False, cwd=None):
bb7bf43b72fb patch: fix copies when patching over uncommitted changed (issue2459)
Patrick Mezard <pmezard@gmail.com>
parents: 12774
diff changeset
   376
    """Update the dirstate to reflect the intent of copying src to dst. For
bb7bf43b72fb patch: fix copies when patching over uncommitted changed (issue2459)
Patrick Mezard <pmezard@gmail.com>
parents: 12774
diff changeset
   377
    different reasons it might not end with dst being marked as copied from src.
bb7bf43b72fb patch: fix copies when patching over uncommitted changed (issue2459)
Patrick Mezard <pmezard@gmail.com>
parents: 12774
diff changeset
   378
    """
bb7bf43b72fb patch: fix copies when patching over uncommitted changed (issue2459)
Patrick Mezard <pmezard@gmail.com>
parents: 12774
diff changeset
   379
    origsrc = repo.dirstate.copied(src) or src
bb7bf43b72fb patch: fix copies when patching over uncommitted changed (issue2459)
Patrick Mezard <pmezard@gmail.com>
parents: 12774
diff changeset
   380
    if dst == origsrc: # copying back a copy?
bb7bf43b72fb patch: fix copies when patching over uncommitted changed (issue2459)
Patrick Mezard <pmezard@gmail.com>
parents: 12774
diff changeset
   381
        if repo.dirstate[dst] not in 'mn' and not dryrun:
bb7bf43b72fb patch: fix copies when patching over uncommitted changed (issue2459)
Patrick Mezard <pmezard@gmail.com>
parents: 12774
diff changeset
   382
            repo.dirstate.normallookup(dst)
bb7bf43b72fb patch: fix copies when patching over uncommitted changed (issue2459)
Patrick Mezard <pmezard@gmail.com>
parents: 12774
diff changeset
   383
    else:
bb7bf43b72fb patch: fix copies when patching over uncommitted changed (issue2459)
Patrick Mezard <pmezard@gmail.com>
parents: 12774
diff changeset
   384
        if repo.dirstate[origsrc] == 'a' and origsrc == src:
bb7bf43b72fb patch: fix copies when patching over uncommitted changed (issue2459)
Patrick Mezard <pmezard@gmail.com>
parents: 12774
diff changeset
   385
            if not ui.quiet:
bb7bf43b72fb patch: fix copies when patching over uncommitted changed (issue2459)
Patrick Mezard <pmezard@gmail.com>
parents: 12774
diff changeset
   386
                ui.warn(_("%s has not been committed yet, so no copy "
bb7bf43b72fb patch: fix copies when patching over uncommitted changed (issue2459)
Patrick Mezard <pmezard@gmail.com>
parents: 12774
diff changeset
   387
                          "data will be stored for %s.\n")
bb7bf43b72fb patch: fix copies when patching over uncommitted changed (issue2459)
Patrick Mezard <pmezard@gmail.com>
parents: 12774
diff changeset
   388
                        % (repo.pathto(origsrc, cwd), repo.pathto(dst, cwd)))
bb7bf43b72fb patch: fix copies when patching over uncommitted changed (issue2459)
Patrick Mezard <pmezard@gmail.com>
parents: 12774
diff changeset
   389
            if repo.dirstate[dst] in '?r' and not dryrun:
bb7bf43b72fb patch: fix copies when patching over uncommitted changed (issue2459)
Patrick Mezard <pmezard@gmail.com>
parents: 12774
diff changeset
   390
                wctx.add([dst])
bb7bf43b72fb patch: fix copies when patching over uncommitted changed (issue2459)
Patrick Mezard <pmezard@gmail.com>
parents: 12774
diff changeset
   391
        elif not dryrun:
bb7bf43b72fb patch: fix copies when patching over uncommitted changed (issue2459)
Patrick Mezard <pmezard@gmail.com>
parents: 12774
diff changeset
   392
            wctx.copy(origsrc, dst)
bb7bf43b72fb patch: fix copies when patching over uncommitted changed (issue2459)
Patrick Mezard <pmezard@gmail.com>
parents: 12774
diff changeset
   393
5610
2493a478f395 copy: handle rename internally
Matt Mackall <mpm@selenic.com>
parents: 5609
diff changeset
   394
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
   395
    # called with the repo lock held
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   396
    #
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   397
    # hgsep => pathname that uses "/" to separate directories
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   398
    # 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
   399
    cwd = repo.getcwd()
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   400
    targets = {}
5607
e9bae5c80ab4 copy: minor cleanups
Matt Mackall <mpm@selenic.com>
parents: 5606
diff changeset
   401
    after = opts.get("after")
e9bae5c80ab4 copy: minor cleanups
Matt Mackall <mpm@selenic.com>
parents: 5606
diff changeset
   402
    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
   403
    wctx = repo[None]
5589
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   404
5605
e7a9ad999308 copy: refactor okaytocopy into walkpat
Matt Mackall <mpm@selenic.com>
parents: 5604
diff changeset
   405
    def walkpat(pat):
e7a9ad999308 copy: refactor okaytocopy into walkpat
Matt Mackall <mpm@selenic.com>
parents: 5604
diff changeset
   406
        srcs = []
11223
0d09f2244805 rename: make --after work if source is already in R state
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 11177
diff changeset
   407
        badstates = after and '?' or '?r'
6579
0159b7a36184 walk: pass match object to cmdutil.walk
Matt Mackall <mpm@selenic.com>
parents: 6578
diff changeset
   408
        m = match(repo, [pat], opts, globbed=True)
6586
d3463007d368 walk: return a single value
Matt Mackall <mpm@selenic.com>
parents: 6585
diff changeset
   409
        for abs in repo.walk(m):
5605
e7a9ad999308 copy: refactor okaytocopy into walkpat
Matt Mackall <mpm@selenic.com>
parents: 5604
diff changeset
   410
            state = repo.dirstate[abs]
6584
29c77e5dfb3c walk: remove rel and exact returns
Matt Mackall <mpm@selenic.com>
parents: 6582
diff changeset
   411
            rel = m.rel(abs)
29c77e5dfb3c walk: remove rel and exact returns
Matt Mackall <mpm@selenic.com>
parents: 6582
diff changeset
   412
            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
   413
            if state in badstates:
5605
e7a9ad999308 copy: refactor okaytocopy into walkpat
Matt Mackall <mpm@selenic.com>
parents: 5604
diff changeset
   414
                if exact and state == '?':
e7a9ad999308 copy: refactor okaytocopy into walkpat
Matt Mackall <mpm@selenic.com>
parents: 5604
diff changeset
   415
                    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
   416
                if exact and state == 'r':
e7a9ad999308 copy: refactor okaytocopy into walkpat
Matt Mackall <mpm@selenic.com>
parents: 5604
diff changeset
   417
                    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
   418
                              ' remove\n') % rel)
e7a9ad999308 copy: refactor okaytocopy into walkpat
Matt Mackall <mpm@selenic.com>
parents: 5604
diff changeset
   419
                continue
e7a9ad999308 copy: refactor okaytocopy into walkpat
Matt Mackall <mpm@selenic.com>
parents: 5604
diff changeset
   420
            # abs: hgsep
e7a9ad999308 copy: refactor okaytocopy into walkpat
Matt Mackall <mpm@selenic.com>
parents: 5604
diff changeset
   421
            # rel: ossep
e7a9ad999308 copy: refactor okaytocopy into walkpat
Matt Mackall <mpm@selenic.com>
parents: 5604
diff changeset
   422
            srcs.append((abs, rel, exact))
e7a9ad999308 copy: refactor okaytocopy into walkpat
Matt Mackall <mpm@selenic.com>
parents: 5604
diff changeset
   423
        return srcs
5589
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   424
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   425
    # abssrc: hgsep
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   426
    # relsrc: ossep
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   427
    # otarget: ossep
5605
e7a9ad999308 copy: refactor okaytocopy into walkpat
Matt Mackall <mpm@selenic.com>
parents: 5604
diff changeset
   428
    def copyfile(abssrc, relsrc, otarget, exact):
5589
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   429
        abstarget = util.canonpath(repo.root, cwd, otarget)
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   430
        reltarget = repo.pathto(abstarget, cwd)
5607
e9bae5c80ab4 copy: minor cleanups
Matt Mackall <mpm@selenic.com>
parents: 5606
diff changeset
   431
        target = repo.wjoin(abstarget)
5589
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   432
        src = repo.wjoin(abssrc)
5608
784eadabd985 copy: simplify inner copy
Matt Mackall <mpm@selenic.com>
parents: 5607
diff changeset
   433
        state = repo.dirstate[abstarget]
5607
e9bae5c80ab4 copy: minor cleanups
Matt Mackall <mpm@selenic.com>
parents: 5606
diff changeset
   434
e9bae5c80ab4 copy: minor cleanups
Matt Mackall <mpm@selenic.com>
parents: 5606
diff changeset
   435
        # check for collisions
e9bae5c80ab4 copy: minor cleanups
Matt Mackall <mpm@selenic.com>
parents: 5606
diff changeset
   436
        prevsrc = targets.get(abstarget)
5589
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   437
        if prevsrc is not None:
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   438
            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
   439
                    (reltarget, repo.pathto(abssrc, cwd),
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   440
                     repo.pathto(prevsrc, cwd)))
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   441
            return
5607
e9bae5c80ab4 copy: minor cleanups
Matt Mackall <mpm@selenic.com>
parents: 5606
diff changeset
   442
e9bae5c80ab4 copy: minor cleanups
Matt Mackall <mpm@selenic.com>
parents: 5606
diff changeset
   443
        # check for overwrites
12342
70236d6fd844 rename: do not overwrite existing broken symlinks
Patrick Mezard <pmezard@gmail.com>
parents: 11950
diff changeset
   444
        exists = os.path.lexists(target)
8117
2b30d8488819 remove unnecessary outer parenthesis in if-statements
Martin Geisler <mg@lazybytes.net>
parents: 8013
diff changeset
   445
        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
   446
            if not opts['force']:
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   447
                ui.warn(_('%s: not overwriting - file exists\n') %
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   448
                        reltarget)
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   449
                return
5607
e9bae5c80ab4 copy: minor cleanups
Matt Mackall <mpm@selenic.com>
parents: 5606
diff changeset
   450
e9bae5c80ab4 copy: minor cleanups
Matt Mackall <mpm@selenic.com>
parents: 5606
diff changeset
   451
        if after:
5608
784eadabd985 copy: simplify inner copy
Matt Mackall <mpm@selenic.com>
parents: 5607
diff changeset
   452
            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
   453
                if rename:
e8d10d085f47 cmdutil: Warn when trying to copy/rename --after to a nonexistant file.
Steve Losh <steve@stevelosh.com>
parents: 11061
diff changeset
   454
                    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
   455
                            (relsrc, reltarget))
e8d10d085f47 cmdutil: Warn when trying to copy/rename --after to a nonexistant file.
Steve Losh <steve@stevelosh.com>
parents: 11061
diff changeset
   456
                else:
e8d10d085f47 cmdutil: Warn when trying to copy/rename --after to a nonexistant file.
Steve Losh <steve@stevelosh.com>
parents: 11061
diff changeset
   457
                    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
   458
                            (relsrc, reltarget))
5589
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   459
                return
5608
784eadabd985 copy: simplify inner copy
Matt Mackall <mpm@selenic.com>
parents: 5607
diff changeset
   460
        elif not dryrun:
5589
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   461
            try:
5608
784eadabd985 copy: simplify inner copy
Matt Mackall <mpm@selenic.com>
parents: 5607
diff changeset
   462
                if exists:
784eadabd985 copy: simplify inner copy
Matt Mackall <mpm@selenic.com>
parents: 5607
diff changeset
   463
                    os.unlink(target)
784eadabd985 copy: simplify inner copy
Matt Mackall <mpm@selenic.com>
parents: 5607
diff changeset
   464
                targetdir = os.path.dirname(target) or '.'
784eadabd985 copy: simplify inner copy
Matt Mackall <mpm@selenic.com>
parents: 5607
diff changeset
   465
                if not os.path.isdir(targetdir):
784eadabd985 copy: simplify inner copy
Matt Mackall <mpm@selenic.com>
parents: 5607
diff changeset
   466
                    os.makedirs(targetdir)
784eadabd985 copy: simplify inner copy
Matt Mackall <mpm@selenic.com>
parents: 5607
diff changeset
   467
                util.copyfile(src, target)
5589
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   468
            except IOError, inst:
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   469
                if inst.errno == errno.ENOENT:
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   470
                    ui.warn(_('%s: deleted in working copy\n') % relsrc)
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   471
                else:
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   472
                    ui.warn(_('%s: cannot copy - %s\n') %
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   473
                            (relsrc, inst.strerror))
5606
447ea621e50e copy: propagate errors properly
Matt Mackall <mpm@selenic.com>
parents: 5605
diff changeset
   474
                    return True # report a failure
5607
e9bae5c80ab4 copy: minor cleanups
Matt Mackall <mpm@selenic.com>
parents: 5606
diff changeset
   475
5589
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   476
        if ui.verbose or not exact:
7894
caef5fdf1375 cmdutil: fix untranslatable string in copy
Martin Geisler <mg@daimi.au.dk>
parents: 7879
diff changeset
   477
            if rename:
caef5fdf1375 cmdutil: fix untranslatable string in copy
Martin Geisler <mg@daimi.au.dk>
parents: 7879
diff changeset
   478
                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
   479
            else:
caef5fdf1375 cmdutil: fix untranslatable string in copy
Martin Geisler <mg@daimi.au.dk>
parents: 7879
diff changeset
   480
                ui.status(_('copying %s to %s\n') % (relsrc, reltarget))
5608
784eadabd985 copy: simplify inner copy
Matt Mackall <mpm@selenic.com>
parents: 5607
diff changeset
   481
5589
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   482
        targets[abstarget] = abssrc
5607
e9bae5c80ab4 copy: minor cleanups
Matt Mackall <mpm@selenic.com>
parents: 5606
diff changeset
   483
e9bae5c80ab4 copy: minor cleanups
Matt Mackall <mpm@selenic.com>
parents: 5606
diff changeset
   484
        # fix up dirstate
12874
bb7bf43b72fb patch: fix copies when patching over uncommitted changed (issue2459)
Patrick Mezard <pmezard@gmail.com>
parents: 12774
diff changeset
   485
        dirstatecopy(ui, repo, wctx, abssrc, abstarget, dryrun=dryrun, cwd=cwd)
5610
2493a478f395 copy: handle rename internally
Matt Mackall <mpm@selenic.com>
parents: 5609
diff changeset
   486
        if rename and not dryrun:
11303
a1aad8333864 move working dir/dirstate methods from localrepo to workingctx
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 11290
diff changeset
   487
            wctx.remove([abssrc], not after)
5589
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   488
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   489
    # pat: ossep
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   490
    # dest ossep
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   491
    # srcs: list of (hgsep, hgsep, ossep, bool)
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   492
    # return: function that takes hgsep and returns ossep
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   493
    def targetpathfn(pat, dest, srcs):
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   494
        if os.path.isdir(pat):
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   495
            abspfx = util.canonpath(repo.root, cwd, pat)
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   496
            abspfx = util.localpath(abspfx)
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   497
            if destdirexists:
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   498
                striplen = len(os.path.split(abspfx)[0])
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   499
            else:
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   500
                striplen = len(abspfx)
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   501
            if striplen:
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   502
                striplen += len(os.sep)
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   503
            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
   504
        elif destdirexists:
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   505
            res = lambda p: os.path.join(dest,
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   506
                                         os.path.basename(util.localpath(p)))
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   507
        else:
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   508
            res = lambda p: dest
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   509
        return res
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   510
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   511
    # pat: ossep
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   512
    # dest ossep
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   513
    # srcs: list of (hgsep, hgsep, ossep, bool)
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   514
    # return: function that takes hgsep and returns ossep
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   515
    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
   516
        if matchmod.patkind(pat):
5589
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   517
            # a mercurial pattern
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   518
            res = lambda p: os.path.join(dest,
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   519
                                         os.path.basename(util.localpath(p)))
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   520
        else:
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   521
            abspfx = util.canonpath(repo.root, cwd, pat)
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   522
            if len(abspfx) < len(srcs[0][0]):
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   523
                # 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
   524
                # 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
   525
                def evalpath(striplen):
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   526
                    score = 0
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   527
                    for s in srcs:
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   528
                        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
   529
                        if os.path.lexists(t):
5589
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   530
                            score += 1
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   531
                    return score
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   532
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   533
                abspfx = util.localpath(abspfx)
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   534
                striplen = len(abspfx)
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   535
                if striplen:
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   536
                    striplen += len(os.sep)
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   537
                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
   538
                    score = evalpath(striplen)
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   539
                    striplen1 = len(os.path.split(abspfx)[0])
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   540
                    if striplen1:
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   541
                        striplen1 += len(os.sep)
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   542
                    if evalpath(striplen1) > score:
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   543
                        striplen = striplen1
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   544
                res = lambda p: os.path.join(dest,
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   545
                                             util.localpath(p)[striplen:])
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   546
            else:
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   547
                # a file
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   548
                if destdirexists:
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   549
                    res = lambda p: os.path.join(dest,
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   550
                                        os.path.basename(util.localpath(p)))
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   551
                else:
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   552
                    res = lambda p: dest
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   553
        return res
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   554
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   555
8614
573734e7e6d0 cmdutils: Take over glob expansion duties from util
Matt Mackall <mpm@selenic.com>
parents: 8568
diff changeset
   556
    pats = expandpats(pats)
5589
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   557
    if not pats:
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   558
        raise util.Abort(_('no source or destination specified'))
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   559
    if len(pats) == 1:
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   560
        raise util.Abort(_('no destination specified'))
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   561
    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
   562
    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
   563
    if not destdirexists:
12085
6f833fc3ccab Consistently import foo as foomod when foo to avoid shadowing
Martin Geisler <mg@aragost.com>
parents: 12032
diff changeset
   564
        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
   565
            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
   566
                               '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
   567
        if util.endswithsep(dest):
5589
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   568
            raise util.Abort(_('destination %s is not a directory') % dest)
5607
e9bae5c80ab4 copy: minor cleanups
Matt Mackall <mpm@selenic.com>
parents: 5606
diff changeset
   569
e9bae5c80ab4 copy: minor cleanups
Matt Mackall <mpm@selenic.com>
parents: 5606
diff changeset
   570
    tfn = targetpathfn
e9bae5c80ab4 copy: minor cleanups
Matt Mackall <mpm@selenic.com>
parents: 5606
diff changeset
   571
    if after:
5589
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   572
        tfn = targetpathafterfn
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   573
    copylist = []
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   574
    for pat in pats:
5605
e7a9ad999308 copy: refactor okaytocopy into walkpat
Matt Mackall <mpm@selenic.com>
parents: 5604
diff changeset
   575
        srcs = walkpat(pat)
5589
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   576
        if not srcs:
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   577
            continue
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   578
        copylist.append((tfn(pat, dest, srcs), srcs))
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   579
    if not copylist:
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   580
        raise util.Abort(_('no files to copy'))
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   581
5606
447ea621e50e copy: propagate errors properly
Matt Mackall <mpm@selenic.com>
parents: 5605
diff changeset
   582
    errors = 0
5589
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   583
    for targetpath, srcs in copylist:
5605
e7a9ad999308 copy: refactor okaytocopy into walkpat
Matt Mackall <mpm@selenic.com>
parents: 5604
diff changeset
   584
        for abssrc, relsrc, exact in srcs:
5606
447ea621e50e copy: propagate errors properly
Matt Mackall <mpm@selenic.com>
parents: 5605
diff changeset
   585
            if copyfile(abssrc, relsrc, targetpath(abssrc), exact):
447ea621e50e copy: propagate errors properly
Matt Mackall <mpm@selenic.com>
parents: 5605
diff changeset
   586
                errors += 1
5589
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   587
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   588
    if errors:
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   589
        ui.warn(_('(consider using --after)\n'))
5609
a783d3627144 copy: move rename logic
Matt Mackall <mpm@selenic.com>
parents: 5608
diff changeset
   590
11177
6a64813276ed commands: initial audit of exit codes
Matt Mackall <mpm@selenic.com>
parents: 11152
diff changeset
   591
    return errors != 0
5589
9981b6b19ecf move commands.docopy to cmdutil.copy
Matt Mackall <mpm@selenic.com>
parents: 5550
diff changeset
   592
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
   593
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
   594
    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
   595
    '''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
   596
e89f9afc462b Refactor commands.serve to allow other commands to run as services.
Bryan O'Sullivan <bos@serpentine.com>
parents: 4355
diff changeset
   597
    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
   598
        # 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
   599
        lockfd, lockpath = tempfile.mkstemp(prefix='hg-service-')
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10264
diff changeset
   600
        os.close(lockfd)
10238
e22695b4472f cmdutil: replace unix pipe handshake with file lock
Patrick Mezard <pmezard@gmail.com>
parents: 10237
diff changeset
   601
        try:
e22695b4472f cmdutil: replace unix pipe handshake with file lock
Patrick Mezard <pmezard@gmail.com>
parents: 10237
diff changeset
   602
            if not runargs:
10239
8e4be44a676f Find right hg command for detached process
Patrick Mezard <pmezard@gmail.com>
parents: 10238
diff changeset
   603
                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
   604
            runargs.append('--daemon-pipefds=%s' % lockpath)
e22695b4472f cmdutil: replace unix pipe handshake with file lock
Patrick Mezard <pmezard@gmail.com>
parents: 10237
diff changeset
   605
            # 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
   606
            # changed directory.
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10264
diff changeset
   607
            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
   608
                if runargs[i].startswith('--cwd='):
e22695b4472f cmdutil: replace unix pipe handshake with file lock
Patrick Mezard <pmezard@gmail.com>
parents: 10237
diff changeset
   609
                    del runargs[i]
e22695b4472f cmdutil: replace unix pipe handshake with file lock
Patrick Mezard <pmezard@gmail.com>
parents: 10237
diff changeset
   610
                    break
e22695b4472f cmdutil: replace unix pipe handshake with file lock
Patrick Mezard <pmezard@gmail.com>
parents: 10237
diff changeset
   611
                elif runargs[i].startswith('--cwd'):
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10264
diff changeset
   612
                    del runargs[i:i + 2]
10238
e22695b4472f cmdutil: replace unix pipe handshake with file lock
Patrick Mezard <pmezard@gmail.com>
parents: 10237
diff changeset
   613
                    break
10344
9501cde4c034 util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents: 10282
diff changeset
   614
            def condfn():
9501cde4c034 util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents: 10282
diff changeset
   615
                return not os.path.exists(lockpath)
9501cde4c034 util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents: 10282
diff changeset
   616
            pid = util.rundetached(runargs, condfn)
9501cde4c034 util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents: 10282
diff changeset
   617
            if pid < 0:
9501cde4c034 util: make spawndetached() handle subprocess early terminations
Patrick Mezard <pmezard@gmail.com>
parents: 10282
diff changeset
   618
                raise util.Abort(_('child process failed to start'))
10238
e22695b4472f cmdutil: replace unix pipe handshake with file lock
Patrick Mezard <pmezard@gmail.com>
parents: 10237
diff changeset
   619
        finally:
e22695b4472f cmdutil: replace unix pipe handshake with file lock
Patrick Mezard <pmezard@gmail.com>
parents: 10237
diff changeset
   620
            try:
e22695b4472f cmdutil: replace unix pipe handshake with file lock
Patrick Mezard <pmezard@gmail.com>
parents: 10237
diff changeset
   621
                os.unlink(lockpath)
e22695b4472f cmdutil: replace unix pipe handshake with file lock
Patrick Mezard <pmezard@gmail.com>
parents: 10237
diff changeset
   622
            except OSError, e:
e22695b4472f cmdutil: replace unix pipe handshake with file lock
Patrick Mezard <pmezard@gmail.com>
parents: 10237
diff changeset
   623
                if e.errno != errno.ENOENT:
e22695b4472f cmdutil: replace unix pipe handshake with file lock
Patrick Mezard <pmezard@gmail.com>
parents: 10237
diff changeset
   624
                    raise
4380
e89f9afc462b Refactor commands.serve to allow other commands to run as services.
Bryan O'Sullivan <bos@serpentine.com>
parents: 4355
diff changeset
   625
        if parentfn:
e89f9afc462b Refactor commands.serve to allow other commands to run as services.
Bryan O'Sullivan <bos@serpentine.com>
parents: 4355
diff changeset
   626
            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
   627
        else:
9896
2c2f7593ffc4 cmdutil.service: do not _exit(0) in the parent process
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9668
diff changeset
   628
            return
4380
e89f9afc462b Refactor commands.serve to allow other commands to run as services.
Bryan O'Sullivan <bos@serpentine.com>
parents: 4355
diff changeset
   629
e89f9afc462b Refactor commands.serve to allow other commands to run as services.
Bryan O'Sullivan <bos@serpentine.com>
parents: 4355
diff changeset
   630
    if initfn:
e89f9afc462b Refactor commands.serve to allow other commands to run as services.
Bryan O'Sullivan <bos@serpentine.com>
parents: 4355
diff changeset
   631
        initfn()
e89f9afc462b Refactor commands.serve to allow other commands to run as services.
Bryan O'Sullivan <bos@serpentine.com>
parents: 4355
diff changeset
   632
e89f9afc462b Refactor commands.serve to allow other commands to run as services.
Bryan O'Sullivan <bos@serpentine.com>
parents: 4355
diff changeset
   633
    if opts['pid_file']:
10012
2bfe1a23dafa cmdutil: service: add appendpid parameter to append pids to pid file
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9975
diff changeset
   634
        mode = appendpid and 'a' or 'w'
2bfe1a23dafa cmdutil: service: add appendpid parameter to append pids to pid file
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9975
diff changeset
   635
        fp = open(opts['pid_file'], mode)
4380
e89f9afc462b Refactor commands.serve to allow other commands to run as services.
Bryan O'Sullivan <bos@serpentine.com>
parents: 4355
diff changeset
   636
        fp.write(str(os.getpid()) + '\n')
e89f9afc462b Refactor commands.serve to allow other commands to run as services.
Bryan O'Sullivan <bos@serpentine.com>
parents: 4355
diff changeset
   637
        fp.close()
e89f9afc462b Refactor commands.serve to allow other commands to run as services.
Bryan O'Sullivan <bos@serpentine.com>
parents: 4355
diff changeset
   638
e89f9afc462b Refactor commands.serve to allow other commands to run as services.
Bryan O'Sullivan <bos@serpentine.com>
parents: 4355
diff changeset
   639
    if opts['daemon_pipefds']:
10238
e22695b4472f cmdutil: replace unix pipe handshake with file lock
Patrick Mezard <pmezard@gmail.com>
parents: 10237
diff changeset
   640
        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
   641
        try:
e89f9afc462b Refactor commands.serve to allow other commands to run as services.
Bryan O'Sullivan <bos@serpentine.com>
parents: 4355
diff changeset
   642
            os.setsid()
e89f9afc462b Refactor commands.serve to allow other commands to run as services.
Bryan O'Sullivan <bos@serpentine.com>
parents: 4355
diff changeset
   643
        except AttributeError:
e89f9afc462b Refactor commands.serve to allow other commands to run as services.
Bryan O'Sullivan <bos@serpentine.com>
parents: 4355
diff changeset
   644
            pass
10238
e22695b4472f cmdutil: replace unix pipe handshake with file lock
Patrick Mezard <pmezard@gmail.com>
parents: 10237
diff changeset
   645
        os.unlink(lockpath)
10240
3af4b39afe2a cmdutil: hide child window created by win32 spawndetached()
Patrick Mezard <pmezard@gmail.com>
parents: 10239
diff changeset
   646
        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
   647
        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
   648
        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
   649
e0ed17984a48 cmdutil: service: logfile option to redirect stdout & stderr in a file
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8778
diff changeset
   650
        nullfd = os.open(util.nulldev, os.O_RDWR)
e0ed17984a48 cmdutil: service: logfile option to redirect stdout & stderr in a file
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8778
diff changeset
   651
        logfilefd = nullfd
e0ed17984a48 cmdutil: service: logfile option to redirect stdout & stderr in a file
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8778
diff changeset
   652
        if logfile:
e0ed17984a48 cmdutil: service: logfile option to redirect stdout & stderr in a file
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 8778
diff changeset
   653
            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
   654
        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
   655
        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
   656
        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
   657
        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
   658
            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
   659
        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
   660
            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
   661
e89f9afc462b Refactor commands.serve to allow other commands to run as services.
Bryan O'Sullivan <bos@serpentine.com>
parents: 4355
diff changeset
   662
    if runfn:
e89f9afc462b Refactor commands.serve to allow other commands to run as services.
Bryan O'Sullivan <bos@serpentine.com>
parents: 4355
diff changeset
   663
        return runfn()
e89f9afc462b Refactor commands.serve to allow other commands to run as services.
Bryan O'Sullivan <bos@serpentine.com>
parents: 4355
diff changeset
   664
10611
e764f24a45ee patch/diff: move patch.export() to cmdutil.export()
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10608
diff changeset
   665
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
   666
           opts=None):
e764f24a45ee patch/diff: move patch.export() to cmdutil.export()
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10608
diff changeset
   667
    '''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
   668
e764f24a45ee patch/diff: move patch.export() to cmdutil.export()
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10608
diff changeset
   669
    total = len(revs)
e764f24a45ee patch/diff: move patch.export() to cmdutil.export()
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10608
diff changeset
   670
    revwidth = max([len(str(rev)) for rev in revs])
e764f24a45ee patch/diff: move patch.export() to cmdutil.export()
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10608
diff changeset
   671
e764f24a45ee patch/diff: move patch.export() to cmdutil.export()
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10608
diff changeset
   672
    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
   673
        ctx = repo[rev]
e764f24a45ee patch/diff: move patch.export() to cmdutil.export()
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10608
diff changeset
   674
        node = ctx.node()
e764f24a45ee patch/diff: move patch.export() to cmdutil.export()
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10608
diff changeset
   675
        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
   676
        branch = ctx.branch()
e764f24a45ee patch/diff: move patch.export() to cmdutil.export()
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10608
diff changeset
   677
        if switch_parent:
e764f24a45ee patch/diff: move patch.export() to cmdutil.export()
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10608
diff changeset
   678
            parents.reverse()
e764f24a45ee patch/diff: move patch.export() to cmdutil.export()
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10608
diff changeset
   679
        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
   680
e764f24a45ee patch/diff: move patch.export() to cmdutil.export()
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10608
diff changeset
   681
        if not fp:
e764f24a45ee patch/diff: move patch.export() to cmdutil.export()
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10608
diff changeset
   682
            fp = make_file(repo, template, node, total=total, seqno=seqno,
e764f24a45ee patch/diff: move patch.export() to cmdutil.export()
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10608
diff changeset
   683
                           revwidth=revwidth, mode='ab')
e764f24a45ee patch/diff: move patch.export() to cmdutil.export()
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10608
diff changeset
   684
        if fp != sys.stdout and hasattr(fp, 'name'):
e764f24a45ee patch/diff: move patch.export() to cmdutil.export()
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10608
diff changeset
   685
            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
   686
e764f24a45ee patch/diff: move patch.export() to cmdutil.export()
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10608
diff changeset
   687
        fp.write("# HG changeset patch\n")
e764f24a45ee patch/diff: move patch.export() to cmdutil.export()
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10608
diff changeset
   688
        fp.write("# User %s\n" % ctx.user())
e764f24a45ee patch/diff: move patch.export() to cmdutil.export()
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10608
diff changeset
   689
        fp.write("# Date %d %d\n" % ctx.date())
11821
15aa42aaae4c cmdutil: remove unnecessary parenthesis
Martin Geisler <mg@aragost.com>
parents: 11635
diff changeset
   690
        if branch and branch != 'default':
10611
e764f24a45ee patch/diff: move patch.export() to cmdutil.export()
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10608
diff changeset
   691
            fp.write("# Branch %s\n" % branch)
e764f24a45ee patch/diff: move patch.export() to cmdutil.export()
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10608
diff changeset
   692
        fp.write("# Node ID %s\n" % hex(node))
e764f24a45ee patch/diff: move patch.export() to cmdutil.export()
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10608
diff changeset
   693
        fp.write("# Parent  %s\n" % hex(prev))
e764f24a45ee patch/diff: move patch.export() to cmdutil.export()
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10608
diff changeset
   694
        if len(parents) > 1:
e764f24a45ee patch/diff: move patch.export() to cmdutil.export()
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10608
diff changeset
   695
            fp.write("# Parent  %s\n" % hex(parents[1]))
e764f24a45ee patch/diff: move patch.export() to cmdutil.export()
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10608
diff changeset
   696
        fp.write(ctx.description().rstrip())
e764f24a45ee patch/diff: move patch.export() to cmdutil.export()
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10608
diff changeset
   697
        fp.write("\n\n")
e764f24a45ee patch/diff: move patch.export() to cmdutil.export()
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10608
diff changeset
   698
e764f24a45ee patch/diff: move patch.export() to cmdutil.export()
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10608
diff changeset
   699
        for chunk in patch.diff(repo, prev, node, opts=opts):
e764f24a45ee patch/diff: move patch.export() to cmdutil.export()
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10608
diff changeset
   700
            fp.write(chunk)
e764f24a45ee patch/diff: move patch.export() to cmdutil.export()
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10608
diff changeset
   701
e764f24a45ee patch/diff: move patch.export() to cmdutil.export()
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10608
diff changeset
   702
    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
   703
        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
   704
11050
5d35f7d93514 commands: refactor diff --stat and qdiff --stat
Yuya Nishihara <yuya@tcha.org>
parents: 11017
diff changeset
   705
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
   706
                   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
   707
                   listsubrepos=False):
11050
5d35f7d93514 commands: refactor diff --stat and qdiff --stat
Yuya Nishihara <yuya@tcha.org>
parents: 11017
diff changeset
   708
    '''show diff or diffstat.'''
5d35f7d93514 commands: refactor diff --stat and qdiff --stat
Yuya Nishihara <yuya@tcha.org>
parents: 11017
diff changeset
   709
    if fp is None:
5d35f7d93514 commands: refactor diff --stat and qdiff --stat
Yuya Nishihara <yuya@tcha.org>
parents: 11017
diff changeset
   710
        write = ui.write
5d35f7d93514 commands: refactor diff --stat and qdiff --stat
Yuya Nishihara <yuya@tcha.org>
parents: 11017
diff changeset
   711
    else:
5d35f7d93514 commands: refactor diff --stat and qdiff --stat
Yuya Nishihara <yuya@tcha.org>
parents: 11017
diff changeset
   712
        def write(s, **kw):
5d35f7d93514 commands: refactor diff --stat and qdiff --stat
Yuya Nishihara <yuya@tcha.org>
parents: 11017
diff changeset
   713
            fp.write(s)
5d35f7d93514 commands: refactor diff --stat and qdiff --stat
Yuya Nishihara <yuya@tcha.org>
parents: 11017
diff changeset
   714
5d35f7d93514 commands: refactor diff --stat and qdiff --stat
Yuya Nishihara <yuya@tcha.org>
parents: 11017
diff changeset
   715
    if stat:
11950
d157e040ac4c log: fix the bug 'hg log --stat -p == hg log --stat'
Alecs King <alecsk@gmail.com>
parents: 11488
diff changeset
   716
        diffopts = diffopts.copy(context=0)
11050
5d35f7d93514 commands: refactor diff --stat and qdiff --stat
Yuya Nishihara <yuya@tcha.org>
parents: 11017
diff changeset
   717
        width = 80
5d35f7d93514 commands: refactor diff --stat and qdiff --stat
Yuya Nishihara <yuya@tcha.org>
parents: 11017
diff changeset
   718
        if not ui.plain():
12689
c52c629ce19e termwidth: move to ui.ui from util
Augie Fackler <durin42@gmail.com>
parents: 12619
diff changeset
   719
            width = ui.termwidth()
12167
d2c5b0927c28 diff: recurse into subrepositories with --subrepos/-S flag
Martin Geisler <mg@lazybytes.net>
parents: 12164
diff changeset
   720
        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
   721
                            prefix=prefix)
11050
5d35f7d93514 commands: refactor diff --stat and qdiff --stat
Yuya Nishihara <yuya@tcha.org>
parents: 11017
diff changeset
   722
        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
   723
                                             width=width,
5d35f7d93514 commands: refactor diff --stat and qdiff --stat
Yuya Nishihara <yuya@tcha.org>
parents: 11017
diff changeset
   724
                                             git=diffopts.git):
5d35f7d93514 commands: refactor diff --stat and qdiff --stat
Yuya Nishihara <yuya@tcha.org>
parents: 11017
diff changeset
   725
            write(chunk, label=label)
5d35f7d93514 commands: refactor diff --stat and qdiff --stat
Yuya Nishihara <yuya@tcha.org>
parents: 11017
diff changeset
   726
    else:
5d35f7d93514 commands: refactor diff --stat and qdiff --stat
Yuya Nishihara <yuya@tcha.org>
parents: 11017
diff changeset
   727
        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
   728
                                         changes, diffopts, prefix=prefix):
11050
5d35f7d93514 commands: refactor diff --stat and qdiff --stat
Yuya Nishihara <yuya@tcha.org>
parents: 11017
diff changeset
   729
            write(chunk, label=label)
5d35f7d93514 commands: refactor diff --stat and qdiff --stat
Yuya Nishihara <yuya@tcha.org>
parents: 11017
diff changeset
   730
12167
d2c5b0927c28 diff: recurse into subrepositories with --subrepos/-S flag
Martin Geisler <mg@lazybytes.net>
parents: 12164
diff changeset
   731
    if listsubrepos:
d2c5b0927c28 diff: recurse into subrepositories with --subrepos/-S flag
Martin Geisler <mg@lazybytes.net>
parents: 12164
diff changeset
   732
        ctx1 = repo[node1]
12175
c0a8f9dea0f6 subrepos: handle modified but uncommitted .hgsub
Martin Geisler <mg@lazybytes.net>
parents: 12167
diff changeset
   733
        ctx2 = repo[node2]
12176
ecab10820983 subrepos: add function for iterating over ctx subrepos
Martin Geisler <mg@lazybytes.net>
parents: 12175
diff changeset
   734
        for subpath, sub in subrepo.itersubrepos(ctx1, ctx2):
12167
d2c5b0927c28 diff: recurse into subrepositories with --subrepos/-S flag
Martin Geisler <mg@lazybytes.net>
parents: 12164
diff changeset
   735
            if node2 is not None:
12209
affec9fb56ef subrepos: handle diff nodeids in subrepos, not before
Patrick Mezard <pmezard@gmail.com>
parents: 12176
diff changeset
   736
                node2 = ctx2.substate[subpath][1]
12167
d2c5b0927c28 diff: recurse into subrepositories with --subrepos/-S flag
Martin Geisler <mg@lazybytes.net>
parents: 12164
diff changeset
   737
            submatch = matchmod.narrowmatcher(subpath, match)
d2c5b0927c28 diff: recurse into subrepositories with --subrepos/-S flag
Martin Geisler <mg@lazybytes.net>
parents: 12164
diff changeset
   738
            sub.diff(diffopts, node2, submatch, changes=changes,
d2c5b0927c28 diff: recurse into subrepositories with --subrepos/-S flag
Martin Geisler <mg@lazybytes.net>
parents: 12164
diff changeset
   739
                     stat=stat, fp=fp, prefix=prefix)
d2c5b0927c28 diff: recurse into subrepositories with --subrepos/-S flag
Martin Geisler <mg@lazybytes.net>
parents: 12164
diff changeset
   740
3643
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   741
class changeset_printer(object):
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   742
    '''show changeset information when templating not requested.'''
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   743
7762
fece056bf240 add --git option to commands supporting --patch (log, incoming, history, tip)
Jim Correia <jim.correia@pobox.com>
parents: 7667
diff changeset
   744
    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
   745
        self.ui = ui
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   746
        self.repo = repo
3645
b984dcb1df71 Refactor log ui buffering and patch display
Matt Mackall <mpm@selenic.com>
parents: 3643
diff changeset
   747
        self.buffered = buffered
b984dcb1df71 Refactor log ui buffering and patch display
Matt Mackall <mpm@selenic.com>
parents: 3643
diff changeset
   748
        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
   749
        self.diffopts = diffopts
3738
cb48cd27d3f4 use ui buffering in changeset printer
Matt Mackall <mpm@selenic.com>
parents: 3718
diff changeset
   750
        self.header = {}
cb48cd27d3f4 use ui buffering in changeset printer
Matt Mackall <mpm@selenic.com>
parents: 3718
diff changeset
   751
        self.hunk = {}
cb48cd27d3f4 use ui buffering in changeset printer
Matt Mackall <mpm@selenic.com>
parents: 3718
diff changeset
   752
        self.lastheader = None
10152
56284451a22c Added support for templatevar "footer" to cmdutil.py
Robert Bachmann <rbachm@gmail.com>
parents: 10111
diff changeset
   753
        self.footer = None
3645
b984dcb1df71 Refactor log ui buffering and patch display
Matt Mackall <mpm@selenic.com>
parents: 3643
diff changeset
   754
b984dcb1df71 Refactor log ui buffering and patch display
Matt Mackall <mpm@selenic.com>
parents: 3643
diff changeset
   755
    def flush(self, rev):
3738
cb48cd27d3f4 use ui buffering in changeset printer
Matt Mackall <mpm@selenic.com>
parents: 3718
diff changeset
   756
        if rev in self.header:
cb48cd27d3f4 use ui buffering in changeset printer
Matt Mackall <mpm@selenic.com>
parents: 3718
diff changeset
   757
            h = self.header[rev]
cb48cd27d3f4 use ui buffering in changeset printer
Matt Mackall <mpm@selenic.com>
parents: 3718
diff changeset
   758
            if h != self.lastheader:
cb48cd27d3f4 use ui buffering in changeset printer
Matt Mackall <mpm@selenic.com>
parents: 3718
diff changeset
   759
                self.lastheader = h
cb48cd27d3f4 use ui buffering in changeset printer
Matt Mackall <mpm@selenic.com>
parents: 3718
diff changeset
   760
                self.ui.write(h)
cb48cd27d3f4 use ui buffering in changeset printer
Matt Mackall <mpm@selenic.com>
parents: 3718
diff changeset
   761
            del self.header[rev]
cb48cd27d3f4 use ui buffering in changeset printer
Matt Mackall <mpm@selenic.com>
parents: 3718
diff changeset
   762
        if rev in self.hunk:
cb48cd27d3f4 use ui buffering in changeset printer
Matt Mackall <mpm@selenic.com>
parents: 3718
diff changeset
   763
            self.ui.write(self.hunk[rev])
cb48cd27d3f4 use ui buffering in changeset printer
Matt Mackall <mpm@selenic.com>
parents: 3718
diff changeset
   764
            del self.hunk[rev]
cb48cd27d3f4 use ui buffering in changeset printer
Matt Mackall <mpm@selenic.com>
parents: 3718
diff changeset
   765
            return 1
cb48cd27d3f4 use ui buffering in changeset printer
Matt Mackall <mpm@selenic.com>
parents: 3718
diff changeset
   766
        return 0
3643
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   767
10152
56284451a22c Added support for templatevar "footer" to cmdutil.py
Robert Bachmann <rbachm@gmail.com>
parents: 10111
diff changeset
   768
    def close(self):
56284451a22c Added support for templatevar "footer" to cmdutil.py
Robert Bachmann <rbachm@gmail.com>
parents: 10111
diff changeset
   769
        if self.footer:
56284451a22c Added support for templatevar "footer" to cmdutil.py
Robert Bachmann <rbachm@gmail.com>
parents: 10111
diff changeset
   770
            self.ui.write(self.footer)
56284451a22c Added support for templatevar "footer" to cmdutil.py
Robert Bachmann <rbachm@gmail.com>
parents: 10111
diff changeset
   771
11488
f786fc4b8764 log: follow filenames through renames (issue647)
Mads Kiilerich <mads@kiilerich.com>
parents: 11465
diff changeset
   772
    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
   773
        if self.buffered:
cb48cd27d3f4 use ui buffering in changeset printer
Matt Mackall <mpm@selenic.com>
parents: 3718
diff changeset
   774
            self.ui.pushbuffer()
11488
f786fc4b8764 log: follow filenames through renames (issue647)
Mads Kiilerich <mads@kiilerich.com>
parents: 11465
diff changeset
   775
            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
   776
            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
   777
        else:
11488
f786fc4b8764 log: follow filenames through renames (issue647)
Mads Kiilerich <mads@kiilerich.com>
parents: 11465
diff changeset
   778
            self._show(ctx, copies, matchfn, props)
3738
cb48cd27d3f4 use ui buffering in changeset printer
Matt Mackall <mpm@selenic.com>
parents: 3718
diff changeset
   779
11488
f786fc4b8764 log: follow filenames through renames (issue647)
Mads Kiilerich <mads@kiilerich.com>
parents: 11465
diff changeset
   780
    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
   781
        '''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
   782
        changenode = ctx.node()
87158be081b8 cmdutil: use change contexts for cset-printer and cset-templater
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7361
diff changeset
   783
        rev = ctx.rev()
3643
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   784
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   785
        if self.ui.quiet:
10819
36c6a667d733 cmdutil: make use of output labeling in changeset_printer
Brodie Rao <brodie@bitheap.org>
parents: 10724
diff changeset
   786
            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
   787
                          label='log.node')
3643
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   788
            return
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   789
7369
87158be081b8 cmdutil: use change contexts for cset-printer and cset-templater
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7361
diff changeset
   790
        log = self.repo.changelog
9547
f57640bf10d4 cmdutil: changeset_printer: use methods of filectx/changectx.
Greg Ward <greg-hg@gerg.ca>
parents: 9536
diff changeset
   791
        date = util.datestr(ctx.date())
3643
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   792
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   793
        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
   794
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
   795
        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
   796
                   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
   797
10819
36c6a667d733 cmdutil: make use of output labeling in changeset_printer
Brodie Rao <brodie@bitheap.org>
parents: 10724
diff changeset
   798
        self.ui.write(_("changeset:   %d:%s\n") % (rev, hexfunc(changenode)),
36c6a667d733 cmdutil: make use of output labeling in changeset_printer
Brodie Rao <brodie@bitheap.org>
parents: 10724
diff changeset
   799
                      label='log.changeset')
3643
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   800
9637
64425c5a9257 cmdutil: minor refactoring of changeset_printer._show
Adrian Buehlmann <adrian@cadifra.com>
parents: 9547
diff changeset
   801
        branch = ctx.branch()
4176
f9bbcebcacea "default" is the default branch name
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4055
diff changeset
   802
        # 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
   803
        if branch != 'default':
10819
36c6a667d733 cmdutil: make use of output labeling in changeset_printer
Brodie Rao <brodie@bitheap.org>
parents: 10724
diff changeset
   804
            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
   805
                          label='log.branch')
3643
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   806
        for tag in self.repo.nodetags(changenode):
10819
36c6a667d733 cmdutil: make use of output labeling in changeset_printer
Brodie Rao <brodie@bitheap.org>
parents: 10724
diff changeset
   807
            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
   808
                          label='log.tag')
3643
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   809
        for parent in parents:
10819
36c6a667d733 cmdutil: make use of output labeling in changeset_printer
Brodie Rao <brodie@bitheap.org>
parents: 10724
diff changeset
   810
            self.ui.write(_("parent:      %d:%s\n") % parent,
36c6a667d733 cmdutil: make use of output labeling in changeset_printer
Brodie Rao <brodie@bitheap.org>
parents: 10724
diff changeset
   811
                          label='log.parent')
3643
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   812
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   813
        if self.ui.debugflag:
9547
f57640bf10d4 cmdutil: changeset_printer: use methods of filectx/changectx.
Greg Ward <greg-hg@gerg.ca>
parents: 9536
diff changeset
   814
            mnode = ctx.manifestnode()
3643
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   815
            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
   816
                          (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
   817
                          label='ui.debug log.manifest')
36c6a667d733 cmdutil: make use of output labeling in changeset_printer
Brodie Rao <brodie@bitheap.org>
parents: 10724
diff changeset
   818
        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
   819
                      label='log.user')
36c6a667d733 cmdutil: make use of output labeling in changeset_printer
Brodie Rao <brodie@bitheap.org>
parents: 10724
diff changeset
   820
        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
   821
                      label='log.date')
3643
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   822
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   823
        if self.ui.debugflag:
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   824
            files = self.repo.status(log.parents(changenode)[0], changenode)[:3]
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   825
            for key, value in zip([_("files:"), _("files+:"), _("files-:")],
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   826
                                  files):
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   827
                if value:
10819
36c6a667d733 cmdutil: make use of output labeling in changeset_printer
Brodie Rao <brodie@bitheap.org>
parents: 10724
diff changeset
   828
                    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
   829
                                  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
   830
        elif ctx.files() and self.ui.verbose:
10819
36c6a667d733 cmdutil: make use of output labeling in changeset_printer
Brodie Rao <brodie@bitheap.org>
parents: 10724
diff changeset
   831
            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
   832
                          label='ui.note log.files')
3643
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   833
        if copies and self.ui.verbose:
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   834
            copies = ['%s (%s)' % c for c in copies]
10819
36c6a667d733 cmdutil: make use of output labeling in changeset_printer
Brodie Rao <brodie@bitheap.org>
parents: 10724
diff changeset
   835
            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
   836
                          label='ui.note log.copies')
3643
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   837
9637
64425c5a9257 cmdutil: minor refactoring of changeset_printer._show
Adrian Buehlmann <adrian@cadifra.com>
parents: 9547
diff changeset
   838
        extra = ctx.extra()
3643
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   839
        if extra and self.ui.debugflag:
8209
a1a5a57efe90 replace util.sort with sorted built-in
Matt Mackall <mpm@selenic.com>
parents: 8189
diff changeset
   840
            for key, value in sorted(extra.items()):
3643
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   841
                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
   842
                              % (key, value.encode('string_escape')),
36c6a667d733 cmdutil: make use of output labeling in changeset_printer
Brodie Rao <brodie@bitheap.org>
parents: 10724
diff changeset
   843
                              label='ui.debug log.extra')
3643
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   844
9547
f57640bf10d4 cmdutil: changeset_printer: use methods of filectx/changectx.
Greg Ward <greg-hg@gerg.ca>
parents: 9536
diff changeset
   845
        description = ctx.description().strip()
3643
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   846
        if description:
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   847
            if self.ui.verbose:
10819
36c6a667d733 cmdutil: make use of output labeling in changeset_printer
Brodie Rao <brodie@bitheap.org>
parents: 10724
diff changeset
   848
                self.ui.write(_("description:\n"),
36c6a667d733 cmdutil: make use of output labeling in changeset_printer
Brodie Rao <brodie@bitheap.org>
parents: 10724
diff changeset
   849
                              label='ui.note log.description')
36c6a667d733 cmdutil: make use of output labeling in changeset_printer
Brodie Rao <brodie@bitheap.org>
parents: 10724
diff changeset
   850
                self.ui.write(description,
36c6a667d733 cmdutil: make use of output labeling in changeset_printer
Brodie Rao <brodie@bitheap.org>
parents: 10724
diff changeset
   851
                              label='ui.note log.description')
3643
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   852
                self.ui.write("\n\n")
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   853
            else:
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   854
                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
   855
                              description.splitlines()[0],
36c6a667d733 cmdutil: make use of output labeling in changeset_printer
Brodie Rao <brodie@bitheap.org>
parents: 10724
diff changeset
   856
                              label='log.summary')
3643
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   857
        self.ui.write("\n")
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   858
11488
f786fc4b8764 log: follow filenames through renames (issue647)
Mads Kiilerich <mads@kiilerich.com>
parents: 11465
diff changeset
   859
        self.showpatch(changenode, matchfn)
3645
b984dcb1df71 Refactor log ui buffering and patch display
Matt Mackall <mpm@selenic.com>
parents: 3643
diff changeset
   860
11488
f786fc4b8764 log: follow filenames through renames (issue647)
Mads Kiilerich <mads@kiilerich.com>
parents: 11465
diff changeset
   861
    def showpatch(self, node, matchfn):
f786fc4b8764 log: follow filenames through renames (issue647)
Mads Kiilerich <mads@kiilerich.com>
parents: 11465
diff changeset
   862
        if not matchfn:
f786fc4b8764 log: follow filenames through renames (issue647)
Mads Kiilerich <mads@kiilerich.com>
parents: 11465
diff changeset
   863
            matchfn = self.patch
f786fc4b8764 log: follow filenames through renames (issue647)
Mads Kiilerich <mads@kiilerich.com>
parents: 11465
diff changeset
   864
        if matchfn:
11061
51d0387523c6 log: add --stat for diffstat output
Yuya Nishihara <yuya@tcha.org>
parents: 11059
diff changeset
   865
            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
   866
            diff = self.diffopts.get('patch')
11061
51d0387523c6 log: add --stat for diffstat output
Yuya Nishihara <yuya@tcha.org>
parents: 11059
diff changeset
   867
            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
   868
            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
   869
            if stat:
d157e040ac4c log: fix the bug 'hg log --stat -p == hg log --stat'
Alecs King <alecsk@gmail.com>
parents: 11488
diff changeset
   870
                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
   871
                               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
   872
            if diff:
d157e040ac4c log: fix the bug 'hg log --stat -p == hg log --stat'
Alecs King <alecsk@gmail.com>
parents: 11488
diff changeset
   873
                if stat:
d157e040ac4c log: fix the bug 'hg log --stat -p == hg log --stat'
Alecs King <alecsk@gmail.com>
parents: 11488
diff changeset
   874
                    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
   875
                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
   876
                               match=matchfn, stat=False)
3645
b984dcb1df71 Refactor log ui buffering and patch display
Matt Mackall <mpm@selenic.com>
parents: 3643
diff changeset
   877
            self.ui.write("\n")
b984dcb1df71 Refactor log ui buffering and patch display
Matt Mackall <mpm@selenic.com>
parents: 3643
diff changeset
   878
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
   879
    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
   880
        """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
   881
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
   882
        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
   883
        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
   884
        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
   885
        """
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
   886
        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
   887
        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
   888
            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
   889
                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
   890
            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
   891
                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
   892
        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
   893
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
   894
3645
b984dcb1df71 Refactor log ui buffering and patch display
Matt Mackall <mpm@selenic.com>
parents: 3643
diff changeset
   895
class changeset_templater(changeset_printer):
3643
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   896
    '''format changeset information.'''
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   897
7762
fece056bf240 add --git option to commands supporting --patch (log, incoming, history, tip)
Jim Correia <jim.correia@pobox.com>
parents: 7667
diff changeset
   898
    def __init__(self, ui, repo, patch, diffopts, mapfile, buffered):
fece056bf240 add --git option to commands supporting --patch (log, incoming, history, tip)
Jim Correia <jim.correia@pobox.com>
parents: 7667
diff changeset
   899
        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
   900
        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
   901
        defaulttempl = {
9e2ab10728a2 Make {file_copies} usable as a --template key
Patrick Mezard <pmezard@gmail.com>
parents: 10060
diff changeset
   902
            'parent': '{rev}:{node|formatnode} ',
9e2ab10728a2 Make {file_copies} usable as a --template key
Patrick Mezard <pmezard@gmail.com>
parents: 10060
diff changeset
   903
            'manifest': '{rev}:{node|formatnode}',
9e2ab10728a2 Make {file_copies} usable as a --template key
Patrick Mezard <pmezard@gmail.com>
parents: 10060
diff changeset
   904
            'file_copy': '{name} ({source})',
9e2ab10728a2 Make {file_copies} usable as a --template key
Patrick Mezard <pmezard@gmail.com>
parents: 10060
diff changeset
   905
            'extra': '{key}={value|stringescape}'
9e2ab10728a2 Make {file_copies} usable as a --template key
Patrick Mezard <pmezard@gmail.com>
parents: 10060
diff changeset
   906
            }
9e2ab10728a2 Make {file_copies} usable as a --template key
Patrick Mezard <pmezard@gmail.com>
parents: 10060
diff changeset
   907
        # filecopy is preserved for compatibility reasons
9e2ab10728a2 Make {file_copies} usable as a --template key
Patrick Mezard <pmezard@gmail.com>
parents: 10060
diff changeset
   908
        defaulttempl['filecopy'] = defaulttempl['file_copy']
8360
acc202b71619 templater: provide the standard template filters by default
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 8312
diff changeset
   909
        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
   910
                                     cache=defaulttempl)
10057
babc00a82c5e cmdutil: extract latest tags closures in templatekw
Patrick Mezard <pmezard@gmail.com>
parents: 10056
diff changeset
   911
        self.cache = {}
3643
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   912
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   913
    def use_template(self, t):
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   914
        '''set template string to use'''
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   915
        self.t.cache['changeset'] = t
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   916
7878
8c09952cd39a templater: use contexts consistently throughout changeset_templater
Alexander Solovyov <piranha@piranha.org.ua>
parents: 7807
diff changeset
   917
    def _meaningful_parentrevs(self, ctx):
8c09952cd39a templater: use contexts consistently throughout changeset_templater
Alexander Solovyov <piranha@piranha.org.ua>
parents: 7807
diff changeset
   918
        """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
   919
        """
8c09952cd39a templater: use contexts consistently throughout changeset_templater
Alexander Solovyov <piranha@piranha.org.ua>
parents: 7807
diff changeset
   920
        parents = ctx.parents()
8c09952cd39a templater: use contexts consistently throughout changeset_templater
Alexander Solovyov <piranha@piranha.org.ua>
parents: 7807
diff changeset
   921
        if len(parents) > 1:
8c09952cd39a templater: use contexts consistently throughout changeset_templater
Alexander Solovyov <piranha@piranha.org.ua>
parents: 7807
diff changeset
   922
            return parents
8c09952cd39a templater: use contexts consistently throughout changeset_templater
Alexander Solovyov <piranha@piranha.org.ua>
parents: 7807
diff changeset
   923
        if self.ui.debugflag:
8c09952cd39a templater: use contexts consistently throughout changeset_templater
Alexander Solovyov <piranha@piranha.org.ua>
parents: 7807
diff changeset
   924
            return [parents[0], self.repo['null']]
8c09952cd39a templater: use contexts consistently throughout changeset_templater
Alexander Solovyov <piranha@piranha.org.ua>
parents: 7807
diff changeset
   925
        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
   926
            return []
8c09952cd39a templater: use contexts consistently throughout changeset_templater
Alexander Solovyov <piranha@piranha.org.ua>
parents: 7807
diff changeset
   927
        return parents
8c09952cd39a templater: use contexts consistently throughout changeset_templater
Alexander Solovyov <piranha@piranha.org.ua>
parents: 7807
diff changeset
   928
11488
f786fc4b8764 log: follow filenames through renames (issue647)
Mads Kiilerich <mads@kiilerich.com>
parents: 11465
diff changeset
   929
    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
   930
        '''show a single changeset or file revision'''
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   931
10053
5c5c6295533d cmdutil: replace showlist() closure with a function
Patrick Mezard <pmezard@gmail.com>
parents: 10026
diff changeset
   932
        showlist = templatekw.showlist
3643
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   933
10058
c829563b3118 cmdutil: extract file copies closure into templatekw
Patrick Mezard <pmezard@gmail.com>
parents: 10057
diff changeset
   934
        # 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
   935
        # 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
   936
        # 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
   937
        # 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
   938
        def showparents(**args):
10260
fe699ca08a45 templatekw: fix extras, manifest and showlist args (issue1989)
Patrick Mezard <pmezard@gmail.com>
parents: 10250
diff changeset
   939
            ctx = args['ctx']
7878
8c09952cd39a templater: use contexts consistently throughout changeset_templater
Alexander Solovyov <piranha@piranha.org.ua>
parents: 7807
diff changeset
   940
            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
   941
                       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
   942
            return showlist('parent', parents, **args)
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   943
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   944
        props = props.copy()
10054
1a85861f59af cmdutil: extract ctx dependent closures into templatekw
Patrick Mezard <pmezard@gmail.com>
parents: 10053
diff changeset
   945
        props.update(templatekw.keywords)
10058
c829563b3118 cmdutil: extract file copies closure into templatekw
Patrick Mezard <pmezard@gmail.com>
parents: 10057
diff changeset
   946
        props['parents'] = showparents
10053
5c5c6295533d cmdutil: replace showlist() closure with a function
Patrick Mezard <pmezard@gmail.com>
parents: 10026
diff changeset
   947
        props['templ'] = self.t
10054
1a85861f59af cmdutil: extract ctx dependent closures into templatekw
Patrick Mezard <pmezard@gmail.com>
parents: 10053
diff changeset
   948
        props['ctx'] = ctx
10055
e400a511e63a cmdutil: extract repo dependent closures in templatekw
Patrick Mezard <pmezard@gmail.com>
parents: 10054
diff changeset
   949
        props['repo'] = self.repo
10058
c829563b3118 cmdutil: extract file copies closure into templatekw
Patrick Mezard <pmezard@gmail.com>
parents: 10057
diff changeset
   950
        props['revcache'] = {'copies': copies}
10057
babc00a82c5e cmdutil: extract latest tags closures in templatekw
Patrick Mezard <pmezard@gmail.com>
parents: 10056
diff changeset
   951
        props['cache'] = self.cache
3643
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   952
8013
9ec25db32b4e cmdutil: prevent code repetition by abstraction in changeset_templater
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7967
diff changeset
   953
        # 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
   954
9ec25db32b4e cmdutil: prevent code repetition by abstraction in changeset_templater
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7967
diff changeset
   955
        tmplmodes = [
9ec25db32b4e cmdutil: prevent code repetition by abstraction in changeset_templater
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7967
diff changeset
   956
            (True, None),
9ec25db32b4e cmdutil: prevent code repetition by abstraction in changeset_templater
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7967
diff changeset
   957
            (self.ui.verbose, 'verbose'),
9ec25db32b4e cmdutil: prevent code repetition by abstraction in changeset_templater
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7967
diff changeset
   958
            (self.ui.quiet, 'quiet'),
9ec25db32b4e cmdutil: prevent code repetition by abstraction in changeset_templater
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7967
diff changeset
   959
            (self.ui.debugflag, 'debug'),
9ec25db32b4e cmdutil: prevent code repetition by abstraction in changeset_templater
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7967
diff changeset
   960
        ]
9ec25db32b4e cmdutil: prevent code repetition by abstraction in changeset_templater
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7967
diff changeset
   961
10152
56284451a22c Added support for templatevar "footer" to cmdutil.py
Robert Bachmann <rbachm@gmail.com>
parents: 10111
diff changeset
   962
        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
   963
        for mode, postfix  in tmplmodes:
9ec25db32b4e cmdutil: prevent code repetition by abstraction in changeset_templater
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7967
diff changeset
   964
            for type in types:
9ec25db32b4e cmdutil: prevent code repetition by abstraction in changeset_templater
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7967
diff changeset
   965
                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
   966
                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
   967
                    types[type] = cur
9ec25db32b4e cmdutil: prevent code repetition by abstraction in changeset_templater
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7967
diff changeset
   968
3643
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   969
        try:
8013
9ec25db32b4e cmdutil: prevent code repetition by abstraction in changeset_templater
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7967
diff changeset
   970
9ec25db32b4e cmdutil: prevent code repetition by abstraction in changeset_templater
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7967
diff changeset
   971
            # write header
9ec25db32b4e cmdutil: prevent code repetition by abstraction in changeset_templater
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7967
diff changeset
   972
            if types['header']:
9ec25db32b4e cmdutil: prevent code repetition by abstraction in changeset_templater
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7967
diff changeset
   973
                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
   974
                if self.buffered:
7878
8c09952cd39a templater: use contexts consistently throughout changeset_templater
Alexander Solovyov <piranha@piranha.org.ua>
parents: 7807
diff changeset
   975
                    self.header[ctx.rev()] = h
3645
b984dcb1df71 Refactor log ui buffering and patch display
Matt Mackall <mpm@selenic.com>
parents: 3643
diff changeset
   976
                else:
11465
ace5bd98bee3 heads: fix templating of headers again (issue2130)
Simon Howkins <simonh@symbian.org>
parents: 11441
diff changeset
   977
                    if self.lastheader != h:
ace5bd98bee3 heads: fix templating of headers again (issue2130)
Simon Howkins <simonh@symbian.org>
parents: 11441
diff changeset
   978
                        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
   979
                        self.ui.write(h)
8013
9ec25db32b4e cmdutil: prevent code repetition by abstraction in changeset_templater
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7967
diff changeset
   980
9ec25db32b4e cmdutil: prevent code repetition by abstraction in changeset_templater
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7967
diff changeset
   981
            # 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
   982
            key = types['changeset']
3645
b984dcb1df71 Refactor log ui buffering and patch display
Matt Mackall <mpm@selenic.com>
parents: 3643
diff changeset
   983
            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
   984
            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
   985
10160
48653dea23dd Bugfix and test for hg log XML output
Robert Bachmann <rbachm@gmail.com>
parents: 10152
diff changeset
   986
            if types['footer']:
10152
56284451a22c Added support for templatevar "footer" to cmdutil.py
Robert Bachmann <rbachm@gmail.com>
parents: 10111
diff changeset
   987
                if not self.footer:
56284451a22c Added support for templatevar "footer" to cmdutil.py
Robert Bachmann <rbachm@gmail.com>
parents: 10111
diff changeset
   988
                    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
   989
                                                      **props))
56284451a22c Added support for templatevar "footer" to cmdutil.py
Robert Bachmann <rbachm@gmail.com>
parents: 10111
diff changeset
   990
3643
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
   991
        except KeyError, inst:
8013
9ec25db32b4e cmdutil: prevent code repetition by abstraction in changeset_templater
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7967
diff changeset
   992
            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
   993
            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
   994
        except SyntaxError, inst:
10829
56fffc9c8928 cmdutil: do not translate trivial string
Martin Geisler <mg@lazybytes.net>
parents: 10819
diff changeset
   995
            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
   996
11488
f786fc4b8764 log: follow filenames through renames (issue647)
Mads Kiilerich <mads@kiilerich.com>
parents: 11465
diff changeset
   997
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
   998
    """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
   999
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
  1000
    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
  1001
    1. option 'template'
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
  1002
    2. option 'style'
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
  1003
    3. [ui] setting 'logtemplate'
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
  1004
    4. [ui] setting 'style'
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
  1005
    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
  1006
    regular display via changeset_printer() is done.
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
  1007
    """
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
  1008
    # options
3837
7df171ea50cd Fix log regression where log -p file showed diffs for other files
Matt Mackall <mpm@selenic.com>
parents: 3827
diff changeset
  1009
    patch = False
11061
51d0387523c6 log: add --stat for diffstat output
Yuya Nishihara <yuya@tcha.org>
parents: 11059
diff changeset
  1010
    if opts.get('patch') or opts.get('stat'):
11488
f786fc4b8764 log: follow filenames through renames (issue647)
Mads Kiilerich <mads@kiilerich.com>
parents: 11465
diff changeset
  1011
        patch = 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
  1012
3643
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
  1013
    tmpl = opts.get('template')
7967
c03f42159afa cmdutil: refactor handling of templating in show_changeset()
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7948
diff changeset
  1014
    style = None
3643
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
  1015
    if tmpl:
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
  1016
        tmpl = templater.parsestring(tmpl, quoted=False)
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
  1017
    else:
7967
c03f42159afa cmdutil: refactor handling of templating in show_changeset()
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7948
diff changeset
  1018
        style = opts.get('style')
c03f42159afa cmdutil: refactor handling of templating in show_changeset()
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7948
diff changeset
  1019
c03f42159afa cmdutil: refactor handling of templating in show_changeset()
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7948
diff changeset
  1020
    # ui settings
c03f42159afa cmdutil: refactor handling of templating in show_changeset()
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7948
diff changeset
  1021
    if not (tmpl or style):
c03f42159afa cmdutil: refactor handling of templating in show_changeset()
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7948
diff changeset
  1022
        tmpl = ui.config('ui', 'logtemplate')
c03f42159afa cmdutil: refactor handling of templating in show_changeset()
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7948
diff changeset
  1023
        if tmpl:
c03f42159afa cmdutil: refactor handling of templating in show_changeset()
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7948
diff changeset
  1024
            tmpl = templater.parsestring(tmpl)
c03f42159afa cmdutil: refactor handling of templating in show_changeset()
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7948
diff changeset
  1025
        else:
10249
8ebb34b0f6f7 cmdutil: expand style paths (issue1948)
Patrick Mezard <pmezard@gmail.com>
parents: 10025
diff changeset
  1026
            style = util.expandpath(ui.config('ui', 'style', ''))
7967
c03f42159afa cmdutil: refactor handling of templating in show_changeset()
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7948
diff changeset
  1027
c03f42159afa cmdutil: refactor handling of templating in show_changeset()
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7948
diff changeset
  1028
    if not (tmpl or style):
c03f42159afa cmdutil: refactor handling of templating in show_changeset()
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7948
diff changeset
  1029
        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
  1030
7967
c03f42159afa cmdutil: refactor handling of templating in show_changeset()
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7948
diff changeset
  1031
    mapfile = None
c03f42159afa cmdutil: refactor handling of templating in show_changeset()
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7948
diff changeset
  1032
    if style and not tmpl:
c03f42159afa cmdutil: refactor handling of templating in show_changeset()
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7948
diff changeset
  1033
        mapfile = style
c03f42159afa cmdutil: refactor handling of templating in show_changeset()
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7948
diff changeset
  1034
        if not os.path.split(mapfile)[0]:
c03f42159afa cmdutil: refactor handling of templating in show_changeset()
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7948
diff changeset
  1035
            mapname = (templater.templatepath('map-cmdline.' + mapfile)
c03f42159afa cmdutil: refactor handling of templating in show_changeset()
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7948
diff changeset
  1036
                       or templater.templatepath(mapfile))
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10264
diff changeset
  1037
            if mapname:
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10264
diff changeset
  1038
                mapfile = mapname
7967
c03f42159afa cmdutil: refactor handling of templating in show_changeset()
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7948
diff changeset
  1039
c03f42159afa cmdutil: refactor handling of templating in show_changeset()
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7948
diff changeset
  1040
    try:
c03f42159afa cmdutil: refactor handling of templating in show_changeset()
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7948
diff changeset
  1041
        t = changeset_templater(ui, repo, patch, opts, mapfile, buffered)
c03f42159afa cmdutil: refactor handling of templating in show_changeset()
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7948
diff changeset
  1042
    except SyntaxError, inst:
c03f42159afa cmdutil: refactor handling of templating in show_changeset()
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7948
diff changeset
  1043
        raise util.Abort(inst.args[0])
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10264
diff changeset
  1044
    if tmpl:
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10264
diff changeset
  1045
        t.use_template(tmpl)
7967
c03f42159afa cmdutil: refactor handling of templating in show_changeset()
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7948
diff changeset
  1046
    return t
3643
b4ad640a3bcf templates: move changeset templating bits to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3531
diff changeset
  1047
3814
120be84f33de Add --date support to update and revert
Matt Mackall <mpm@selenic.com>
parents: 3738
diff changeset
  1048
def finddate(ui, repo, date):
120be84f33de Add --date support to update and revert
Matt Mackall <mpm@selenic.com>
parents: 3738
diff changeset
  1049
    """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
  1050
5836
c5c9a022bd9a Tweak finddate to pass date directly.
mark.williamson@cl.cam.ac.uk
parents: 5829
diff changeset
  1051
    df = util.matchdate(date)
9652
2cb0cab10d2e walkchangerevs: pull out matchfn
Matt Mackall <mpm@selenic.com>
parents: 9547
diff changeset
  1052
    m = matchall(repo)
3814
120be84f33de Add --date support to update and revert
Matt Mackall <mpm@selenic.com>
parents: 3738
diff changeset
  1053
    results = {}
9662
f3d60543924f walkchangerevs: move 'add' to callback
Matt Mackall <mpm@selenic.com>
parents: 9656
diff changeset
  1054
f3d60543924f walkchangerevs: move 'add' to callback
Matt Mackall <mpm@selenic.com>
parents: 9656
diff changeset
  1055
    def prep(ctx, fns):
f3d60543924f walkchangerevs: move 'add' to callback
Matt Mackall <mpm@selenic.com>
parents: 9656
diff changeset
  1056
        d = ctx.date()
f3d60543924f walkchangerevs: move 'add' to callback
Matt Mackall <mpm@selenic.com>
parents: 9656
diff changeset
  1057
        if df(d[0]):
9668
2c24471d478c cmdutil: fix bug in finddate() implementation
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 9667
diff changeset
  1058
            results[ctx.rev()] = d
9662
f3d60543924f walkchangerevs: move 'add' to callback
Matt Mackall <mpm@selenic.com>
parents: 9656
diff changeset
  1059
9667
8743f2e1bc54 merge changes from mpm
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 9666 9665
diff changeset
  1060
    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
  1061
        rev = ctx.rev()
f3d60543924f walkchangerevs: move 'add' to callback
Matt Mackall <mpm@selenic.com>
parents: 9656
diff changeset
  1062
        if rev in results:
f3d60543924f walkchangerevs: move 'add' to callback
Matt Mackall <mpm@selenic.com>
parents: 9656
diff changeset
  1063
            ui.status(_("Found revision %s from %s\n") %
f3d60543924f walkchangerevs: move 'add' to callback
Matt Mackall <mpm@selenic.com>
parents: 9656
diff changeset
  1064
                      (rev, util.datestr(results[rev])))
f3d60543924f walkchangerevs: move 'add' to callback
Matt Mackall <mpm@selenic.com>
parents: 9656
diff changeset
  1065
            return str(rev)
3814
120be84f33de Add --date support to update and revert
Matt Mackall <mpm@selenic.com>
parents: 3738
diff changeset
  1066
120be84f33de Add --date support to update and revert
Matt Mackall <mpm@selenic.com>
parents: 3738
diff changeset
  1067
    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
  1068
9665
1de5ebfa5585 walkchangerevs: drop ui arg
Matt Mackall <mpm@selenic.com>
parents: 9664
diff changeset
  1069
def walkchangerevs(repo, match, opts, prepare):
7807
bd8f44638847 help: miscellaneous language fixes
timeless <timeless@gmail.com>
parents: 7779
diff changeset
  1070
    '''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
  1071
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1072
    Callers most commonly need to iterate backwards over the history
7807
bd8f44638847 help: miscellaneous language fixes
timeless <timeless@gmail.com>
parents: 7779
diff changeset
  1073
    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
  1074
    performance, so we use iterators in a "windowed" way.
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1075
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1076
    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
  1077
    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
  1078
    order (usually backwards) to display it.
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1079
9662
f3d60543924f walkchangerevs: move 'add' to callback
Matt Mackall <mpm@selenic.com>
parents: 9656
diff changeset
  1080
    This function returns an iterator yielding contexts. Before
f3d60543924f walkchangerevs: move 'add' to callback
Matt Mackall <mpm@selenic.com>
parents: 9656
diff changeset
  1081
    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
  1082
    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
  1083
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1084
    def increasing_windows(start, end, windowsize=8, sizelimit=512):
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1085
        if start < end:
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1086
            while start < end:
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10264
diff changeset
  1087
                yield start, min(windowsize, end - start)
3650
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1088
                start += windowsize
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1089
                if windowsize < sizelimit:
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1090
                    windowsize *= 2
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1091
        else:
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1092
            while start > end:
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10264
diff changeset
  1093
                yield start, min(windowsize, start - end - 1)
3650
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1094
                start -= windowsize
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1095
                if windowsize < sizelimit:
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1096
                    windowsize *= 2
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1097
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1098
    follow = opts.get('follow') or opts.get('follow_first')
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1099
6750
fb42030d79d6 add __len__ and __iter__ methods to repo and revlog
Matt Mackall <mpm@selenic.com>
parents: 6747
diff changeset
  1100
    if not len(repo):
9652
2cb0cab10d2e walkchangerevs: pull out matchfn
Matt Mackall <mpm@selenic.com>
parents: 9547
diff changeset
  1101
        return []
3650
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1102
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1103
    if follow:
6747
f6c00b17387c use repo[changeid] to get a changectx
Matt Mackall <mpm@selenic.com>
parents: 6739
diff changeset
  1104
        defrange = '%s:0' % repo['.'].rev()
3650
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1105
    else:
6145
154f8be6272b cmdutil.walkchangerevs: use '-1:0' instead ot 'tip:0'
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 6139
diff changeset
  1106
        defrange = '-1:0'
3707
67f44b825784 Removed unused ui parameter from revpair/revrange and fix its users.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 3673
diff changeset
  1107
    revs = revrange(repo, opts['rev'] or [defrange])
11281
b724b8467b82 walkchangerevs: allow empty query sets
Matt Mackall <mpm@selenic.com>
parents: 11277
diff changeset
  1108
    if not revs:
b724b8467b82 walkchangerevs: allow empty query sets
Matt Mackall <mpm@selenic.com>
parents: 11277
diff changeset
  1109
        return []
8152
08e1baf924ca replace set-like dictionaries with real sets
Martin Geisler <mg@lazybytes.net>
parents: 8119
diff changeset
  1110
    wanted = set()
9652
2cb0cab10d2e walkchangerevs: pull out matchfn
Matt Mackall <mpm@selenic.com>
parents: 9547
diff changeset
  1111
    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
  1112
    fncache = {}
9655
6d7d3f849062 walkchangerevs: internalize ctx caching
Matt Mackall <mpm@selenic.com>
parents: 9654
diff changeset
  1113
    change = util.cachefunc(repo.changectx)
3650
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1114
11632
f418d2570920 log: document the different phases in walkchangerevs
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 11631
diff changeset
  1115
    # 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
  1116
    # 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
  1117
    # 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
  1118
    # match the file filtering conditions.
f418d2570920 log: document the different phases in walkchangerevs
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 11631
diff changeset
  1119
9652
2cb0cab10d2e walkchangerevs: pull out matchfn
Matt Mackall <mpm@selenic.com>
parents: 9547
diff changeset
  1120
    if not slowpath and not match.files():
3650
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1121
        # No files, no patterns.  Display all revs.
8152
08e1baf924ca replace set-like dictionaries with real sets
Martin Geisler <mg@lazybytes.net>
parents: 8119
diff changeset
  1122
        wanted = set(revs)
3650
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1123
    copies = []
9665
1de5ebfa5585 walkchangerevs: drop ui arg
Matt Mackall <mpm@selenic.com>
parents: 9664
diff changeset
  1124
3650
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1125
    if not slowpath:
11632
f418d2570920 log: document the different phases in walkchangerevs
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 11631
diff changeset
  1126
        # 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
  1127
11607
cc784ad8b3da log: refactor: test for ranges inside filerevgen
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 11606
diff changeset
  1128
        minrev, maxrev = min(revs), max(revs)
11606
326ab8727a93 log: refactor: compute the value of last outside of filerevgen
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 11488
diff changeset
  1129
        def filerevgen(filelog, last):
11899
99cafcae25d9 log: do not --follow file that is deleted and recreated later (issue732)
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 11821
diff changeset
  1130
            """
99cafcae25d9 log: do not --follow file that is deleted and recreated later (issue732)
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 11821
diff changeset
  1131
            Only files, no patterns.  Check the history of each file.
99cafcae25d9 log: do not --follow file that is deleted and recreated later (issue732)
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 11821
diff changeset
  1132
99cafcae25d9 log: do not --follow file that is deleted and recreated later (issue732)
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 11821
diff changeset
  1133
            Examines filelog entries within minrev, maxrev linkrev range
99cafcae25d9 log: do not --follow file that is deleted and recreated later (issue732)
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 11821
diff changeset
  1134
            Returns an iterator yielding (linkrev, parentlinkrevs, copied)
99cafcae25d9 log: do not --follow file that is deleted and recreated later (issue732)
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 11821
diff changeset
  1135
            tuples in backwards order
99cafcae25d9 log: do not --follow file that is deleted and recreated later (issue732)
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 11821
diff changeset
  1136
            """
6750
fb42030d79d6 add __len__ and __iter__ methods to repo and revlog
Matt Mackall <mpm@selenic.com>
parents: 6747
diff changeset
  1137
            cl_count = len(repo)
11608
183e63112698 log: remove increasing windows usage in fastpath
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 11607
diff changeset
  1138
            revs = []
11634
09147c065711 cmdutils: fix code style
Martin Geisler <mg@aragost.com>
parents: 11632
diff changeset
  1139
            for j in xrange(0, last + 1):
11608
183e63112698 log: remove increasing windows usage in fastpath
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 11607
diff changeset
  1140
                linkrev = filelog.linkrev(j)
183e63112698 log: remove increasing windows usage in fastpath
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 11607
diff changeset
  1141
                if linkrev < minrev:
183e63112698 log: remove increasing windows usage in fastpath
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 11607
diff changeset
  1142
                    continue
183e63112698 log: remove increasing windows usage in fastpath
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 11607
diff changeset
  1143
                # only yield rev for which we have the changelog, it can
183e63112698 log: remove increasing windows usage in fastpath
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 11607
diff changeset
  1144
                # happen while doing "hg log" during a pull or commit
12971
15390d1a3cfc cmdutil: move range check outside of filerevgen
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 12900
diff changeset
  1145
                if linkrev >= cl_count:
11608
183e63112698 log: remove increasing windows usage in fastpath
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 11607
diff changeset
  1146
                    break
11899
99cafcae25d9 log: do not --follow file that is deleted and recreated later (issue732)
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 11821
diff changeset
  1147
99cafcae25d9 log: do not --follow file that is deleted and recreated later (issue732)
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 11821
diff changeset
  1148
                parentlinkrevs = []
99cafcae25d9 log: do not --follow file that is deleted and recreated later (issue732)
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 11821
diff changeset
  1149
                for p in filelog.parentrevs(j):
99cafcae25d9 log: do not --follow file that is deleted and recreated later (issue732)
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 11821
diff changeset
  1150
                    if p != nullrev:
99cafcae25d9 log: do not --follow file that is deleted and recreated later (issue732)
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 11821
diff changeset
  1151
                        parentlinkrevs.append(filelog.linkrev(p))
11608
183e63112698 log: remove increasing windows usage in fastpath
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 11607
diff changeset
  1152
                n = filelog.node(j)
11899
99cafcae25d9 log: do not --follow file that is deleted and recreated later (issue732)
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 11821
diff changeset
  1153
                revs.append((linkrev, parentlinkrevs,
11608
183e63112698 log: remove increasing windows usage in fastpath
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 11607
diff changeset
  1154
                             follow and filelog.renamed(n)))
183e63112698 log: remove increasing windows usage in fastpath
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 11607
diff changeset
  1155
11901
a80577bfea29 cmdutil: code simplification
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 11899
diff changeset
  1156
            return reversed(revs)
3650
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1157
        def iterfiles():
9652
2cb0cab10d2e walkchangerevs: pull out matchfn
Matt Mackall <mpm@selenic.com>
parents: 9547
diff changeset
  1158
            for filename in match.files():
3650
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1159
                yield filename, None
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1160
            for filename_node in copies:
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1161
                yield filename_node
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1162
        for file_, node in iterfiles():
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1163
            filelog = repo.file(file_)
6750
fb42030d79d6 add __len__ and __iter__ methods to repo and revlog
Matt Mackall <mpm@selenic.com>
parents: 6747
diff changeset
  1164
            if not len(filelog):
6536
dfdef3d560a8 cmdutil: handle and warn about missing copy revisions
Patrick Mezard <pmezard@gmail.com>
parents: 6258
diff changeset
  1165
                if node is None:
dfdef3d560a8 cmdutil: handle and warn about missing copy revisions
Patrick Mezard <pmezard@gmail.com>
parents: 6258
diff changeset
  1166
                    # A zero count may be a directory or deleted file, so
dfdef3d560a8 cmdutil: handle and warn about missing copy revisions
Patrick Mezard <pmezard@gmail.com>
parents: 6258
diff changeset
  1167
                    # try to find matching entries on the slow path.
7404
07cb58b8c843 Improved error message for log --follow
Brendan Cully <brendan@kublai.com>
parents: 7369
diff changeset
  1168
                    if follow:
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10264
diff changeset
  1169
                        raise util.Abort(
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10264
diff changeset
  1170
                            _('cannot follow nonexistent file: "%s"') % file_)
6536
dfdef3d560a8 cmdutil: handle and warn about missing copy revisions
Patrick Mezard <pmezard@gmail.com>
parents: 6258
diff changeset
  1171
                    slowpath = True
dfdef3d560a8 cmdutil: handle and warn about missing copy revisions
Patrick Mezard <pmezard@gmail.com>
parents: 6258
diff changeset
  1172
                    break
dfdef3d560a8 cmdutil: handle and warn about missing copy revisions
Patrick Mezard <pmezard@gmail.com>
parents: 6258
diff changeset
  1173
                else:
dfdef3d560a8 cmdutil: handle and warn about missing copy revisions
Patrick Mezard <pmezard@gmail.com>
parents: 6258
diff changeset
  1174
                    continue
11606
326ab8727a93 log: refactor: compute the value of last outside of filerevgen
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 11488
diff changeset
  1175
326ab8727a93 log: refactor: compute the value of last outside of filerevgen
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 11488
diff changeset
  1176
            if node is None:
326ab8727a93 log: refactor: compute the value of last outside of filerevgen
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 11488
diff changeset
  1177
                last = len(filelog) - 1
326ab8727a93 log: refactor: compute the value of last outside of filerevgen
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 11488
diff changeset
  1178
            else:
326ab8727a93 log: refactor: compute the value of last outside of filerevgen
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 11488
diff changeset
  1179
                last = filelog.rev(node)
326ab8727a93 log: refactor: compute the value of last outside of filerevgen
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 11488
diff changeset
  1180
11899
99cafcae25d9 log: do not --follow file that is deleted and recreated later (issue732)
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 11821
diff changeset
  1181
99cafcae25d9 log: do not --follow file that is deleted and recreated later (issue732)
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 11821
diff changeset
  1182
            # keep track of all ancestors of the file
99cafcae25d9 log: do not --follow file that is deleted and recreated later (issue732)
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 11821
diff changeset
  1183
            ancestors = set([filelog.linkrev(last)])
99cafcae25d9 log: do not --follow file that is deleted and recreated later (issue732)
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 11821
diff changeset
  1184
99cafcae25d9 log: do not --follow file that is deleted and recreated later (issue732)
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 11821
diff changeset
  1185
            # iterate from latest to oldest revision
99cafcae25d9 log: do not --follow file that is deleted and recreated later (issue732)
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 11821
diff changeset
  1186
            for rev, flparentlinkrevs, copied in filerevgen(filelog, last):
12972
7916a84c0758 log: fix log -rREV FILE when REV isnt the last filerev (issue2492)
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 12971
diff changeset
  1187
                if not follow:
7916a84c0758 log: fix log -rREV FILE when REV isnt the last filerev (issue2492)
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 12971
diff changeset
  1188
                    if rev > maxrev:
7916a84c0758 log: fix log -rREV FILE when REV isnt the last filerev (issue2492)
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 12971
diff changeset
  1189
                        continue
7916a84c0758 log: fix log -rREV FILE when REV isnt the last filerev (issue2492)
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 12971
diff changeset
  1190
                else:
7916a84c0758 log: fix log -rREV FILE when REV isnt the last filerev (issue2492)
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 12971
diff changeset
  1191
                    # Note that last might not be the first interesting
7916a84c0758 log: fix log -rREV FILE when REV isnt the last filerev (issue2492)
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 12971
diff changeset
  1192
                    # rev to us:
7916a84c0758 log: fix log -rREV FILE when REV isnt the last filerev (issue2492)
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 12971
diff changeset
  1193
                    # if the file has been changed after maxrev, we'll
7916a84c0758 log: fix log -rREV FILE when REV isnt the last filerev (issue2492)
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 12971
diff changeset
  1194
                    # have linkrev(last) > maxrev, and we still need
7916a84c0758 log: fix log -rREV FILE when REV isnt the last filerev (issue2492)
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 12971
diff changeset
  1195
                    # to explore the file graph
7916a84c0758 log: fix log -rREV FILE when REV isnt the last filerev (issue2492)
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 12971
diff changeset
  1196
                    if rev not in ancestors:
7916a84c0758 log: fix log -rREV FILE when REV isnt the last filerev (issue2492)
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 12971
diff changeset
  1197
                        continue
7916a84c0758 log: fix log -rREV FILE when REV isnt the last filerev (issue2492)
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 12971
diff changeset
  1198
                    # XXX insert 1327 fix here
7916a84c0758 log: fix log -rREV FILE when REV isnt the last filerev (issue2492)
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 12971
diff changeset
  1199
                    if flparentlinkrevs:
7916a84c0758 log: fix log -rREV FILE when REV isnt the last filerev (issue2492)
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 12971
diff changeset
  1200
                        ancestors.update(flparentlinkrevs)
11899
99cafcae25d9 log: do not --follow file that is deleted and recreated later (issue732)
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 11821
diff changeset
  1201
11901
a80577bfea29 cmdutil: code simplification
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 11899
diff changeset
  1202
                fncache.setdefault(rev, []).append(file_)
11607
cc784ad8b3da log: refactor: test for ranges inside filerevgen
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 11606
diff changeset
  1203
                wanted.add(rev)
cc784ad8b3da log: refactor: test for ranges inside filerevgen
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 11606
diff changeset
  1204
                if copied:
cc784ad8b3da log: refactor: test for ranges inside filerevgen
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 11606
diff changeset
  1205
                    copies.append(copied)
3650
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1206
    if slowpath:
11632
f418d2570920 log: document the different phases in walkchangerevs
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 11631
diff changeset
  1207
        # 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
  1208
        # changed files
f418d2570920 log: document the different phases in walkchangerevs
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 11631
diff changeset
  1209
3650
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1210
        if follow:
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1211
            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
  1212
                               'filenames'))
3650
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1213
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1214
        # The slow path checks files modified in every changeset.
11631
dbb98d8fbcaf log: slowpath: only walk specified revision range during preparation
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 11613
diff changeset
  1215
        for i in sorted(revs):
11609
890ad9d6a169 log: slowpath: do not read the full changelog
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 11608
diff changeset
  1216
            ctx = change(i)
9652
2cb0cab10d2e walkchangerevs: pull out matchfn
Matt Mackall <mpm@selenic.com>
parents: 9547
diff changeset
  1217
            matches = filter(match, ctx.files())
3650
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1218
            if matches:
11609
890ad9d6a169 log: slowpath: do not read the full changelog
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 11608
diff changeset
  1219
                fncache[i] = matches
890ad9d6a169 log: slowpath: do not read the full changelog
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 11608
diff changeset
  1220
                wanted.add(i)
3650
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1221
8778
c5f36402daad use new style classes
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 8761
diff changeset
  1222
    class followfilter(object):
3650
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1223
        def __init__(self, onlyfirst=False):
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1224
            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
  1225
            self.roots = set()
3650
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1226
            self.onlyfirst = onlyfirst
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1227
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1228
        def match(self, rev):
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1229
            def realparents(rev):
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1230
                if self.onlyfirst:
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1231
                    return repo.changelog.parentrevs(rev)[0:1]
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1232
                else:
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1233
                    return filter(lambda x: x != nullrev,
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1234
                                  repo.changelog.parentrevs(rev))
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1235
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1236
            if self.startrev == nullrev:
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1237
                self.startrev = rev
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1238
                return True
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1239
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1240
            if rev > self.startrev:
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1241
                # forward: all descendants
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1242
                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
  1243
                    self.roots.add(self.startrev)
3650
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1244
                for parent in realparents(rev):
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1245
                    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
  1246
                        self.roots.add(rev)
3650
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1247
                        return True
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1248
            else:
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1249
                # backwards: all parents
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1250
                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
  1251
                    self.roots.update(realparents(self.startrev))
3650
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1252
                if rev in self.roots:
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1253
                    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
  1254
                    self.roots.update(realparents(rev))
3650
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1255
                    return True
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1256
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1257
            return False
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1258
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1259
    # 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
  1260
    # 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
  1261
    for rev in opts.get('prune', ()):
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1262
        rev = repo.changelog.rev(repo.lookup(rev))
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1263
        ff = followfilter()
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1264
        stop = min(revs[0], revs[-1])
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10264
diff changeset
  1265
        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
  1266
            if ff.match(x):
08e1baf924ca replace set-like dictionaries with real sets
Martin Geisler <mg@lazybytes.net>
parents: 8119
diff changeset
  1267
                wanted.discard(x)
3650
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1268
11632
f418d2570920 log: document the different phases in walkchangerevs
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 11631
diff changeset
  1269
    # 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
  1270
    # revision range, yielding only revisions in wanted.
3650
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1271
    def iterate():
9652
2cb0cab10d2e walkchangerevs: pull out matchfn
Matt Mackall <mpm@selenic.com>
parents: 9547
diff changeset
  1272
        if follow and not match.files():
3650
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1273
            ff = followfilter(onlyfirst=opts.get('follow_first'))
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1274
            def want(rev):
8119
af44d0b953c6 cmdutil: return boolean result directly in want function
Martin Geisler <mg@lazybytes.net>
parents: 8117
diff changeset
  1275
                return ff.match(rev) and rev in wanted
3650
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1276
        else:
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1277
            def want(rev):
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1278
                return rev in wanted
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1279
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1280
        for i, window in increasing_windows(0, len(revs)):
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10264
diff changeset
  1281
            nrevs = [rev for rev in revs[i:i + window] if want(rev)]
8209
a1a5a57efe90 replace util.sort with sorted built-in
Matt Mackall <mpm@selenic.com>
parents: 8189
diff changeset
  1282
            for rev in sorted(nrevs):
3650
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1283
                fns = fncache.get(rev)
9654
96fe91be9c1e walkchangerevs: yield contexts
Matt Mackall <mpm@selenic.com>
parents: 9653
diff changeset
  1284
                ctx = change(rev)
3650
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1285
                if not fns:
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1286
                    def fns_generator():
9654
96fe91be9c1e walkchangerevs: yield contexts
Matt Mackall <mpm@selenic.com>
parents: 9653
diff changeset
  1287
                        for f in ctx.files():
9652
2cb0cab10d2e walkchangerevs: pull out matchfn
Matt Mackall <mpm@selenic.com>
parents: 9547
diff changeset
  1288
                            if match(f):
3650
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1289
                                yield f
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1290
                    fns = fns_generator()
9662
f3d60543924f walkchangerevs: move 'add' to callback
Matt Mackall <mpm@selenic.com>
parents: 9656
diff changeset
  1291
                prepare(ctx, fns)
3650
731e739b8659 move walkchangerevs to cmdutils
Matt Mackall <mpm@selenic.com>
parents: 3649
diff changeset
  1292
            for rev in nrevs:
9662
f3d60543924f walkchangerevs: move 'add' to callback
Matt Mackall <mpm@selenic.com>
parents: 9656
diff changeset
  1293
                yield change(rev)
9652
2cb0cab10d2e walkchangerevs: pull out matchfn
Matt Mackall <mpm@selenic.com>
parents: 9547
diff changeset
  1294
    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
  1295
12270
166b9866580a add: recurse into subrepositories with --subrepos/-S flag
Martin Geisler <mg@lazybytes.net>
parents: 12269
diff changeset
  1296
def add(ui, repo, match, dryrun, listsubrepos, prefix):
166b9866580a add: recurse into subrepositories with --subrepos/-S flag
Martin Geisler <mg@lazybytes.net>
parents: 12269
diff changeset
  1297
    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
  1298
    bad = []
877236cdd437 add: move main part to cmdutil to make it easier to reuse
Martin Geisler <mg@lazybytes.net>
parents: 12266
diff changeset
  1299
    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
  1300
    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
  1301
    names = []
12270
166b9866580a add: recurse into subrepositories with --subrepos/-S flag
Martin Geisler <mg@lazybytes.net>
parents: 12269
diff changeset
  1302
    wctx = repo[None]
12269
877236cdd437 add: move main part to cmdutil to make it easier to reuse
Martin Geisler <mg@lazybytes.net>
parents: 12266
diff changeset
  1303
    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
  1304
        exact = match.exact(f)
877236cdd437 add: move main part to cmdutil to make it easier to reuse
Martin Geisler <mg@lazybytes.net>
parents: 12266
diff changeset
  1305
        if exact or f not in repo.dirstate:
877236cdd437 add: move main part to cmdutil to make it easier to reuse
Martin Geisler <mg@lazybytes.net>
parents: 12266
diff changeset
  1306
            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
  1307
            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
  1308
                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
  1309
166b9866580a add: recurse into subrepositories with --subrepos/-S flag
Martin Geisler <mg@lazybytes.net>
parents: 12269
diff changeset
  1310
    if listsubrepos:
166b9866580a add: recurse into subrepositories with --subrepos/-S flag
Martin Geisler <mg@lazybytes.net>
parents: 12269
diff changeset
  1311
        for subpath in wctx.substate:
166b9866580a add: recurse into subrepositories with --subrepos/-S flag
Martin Geisler <mg@lazybytes.net>
parents: 12269
diff changeset
  1312
            sub = wctx.sub(subpath)
166b9866580a add: recurse into subrepositories with --subrepos/-S flag
Martin Geisler <mg@lazybytes.net>
parents: 12269
diff changeset
  1313
            try:
166b9866580a add: recurse into subrepositories with --subrepos/-S flag
Martin Geisler <mg@lazybytes.net>
parents: 12269
diff changeset
  1314
                submatch = matchmod.narrowmatcher(subpath, match)
166b9866580a add: recurse into subrepositories with --subrepos/-S flag
Martin Geisler <mg@lazybytes.net>
parents: 12269
diff changeset
  1315
                bad.extend(sub.add(ui, submatch, dryrun, prefix))
166b9866580a add: recurse into subrepositories with --subrepos/-S flag
Martin Geisler <mg@lazybytes.net>
parents: 12269
diff changeset
  1316
            except error.LookupError:
166b9866580a add: recurse into subrepositories with --subrepos/-S flag
Martin Geisler <mg@lazybytes.net>
parents: 12269
diff changeset
  1317
                ui.status(_("skipping missing subrepository: %s\n")
166b9866580a add: recurse into subrepositories with --subrepos/-S flag
Martin Geisler <mg@lazybytes.net>
parents: 12269
diff changeset
  1318
                               % join(subpath))
166b9866580a add: recurse into subrepositories with --subrepos/-S flag
Martin Geisler <mg@lazybytes.net>
parents: 12269
diff changeset
  1319
12269
877236cdd437 add: move main part to cmdutil to make it easier to reuse
Martin Geisler <mg@lazybytes.net>
parents: 12266
diff changeset
  1320
    if not dryrun:
12270
166b9866580a add: recurse into subrepositories with --subrepos/-S flag
Martin Geisler <mg@lazybytes.net>
parents: 12269
diff changeset
  1321
        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
  1322
        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
  1323
    return bad
877236cdd437 add: move main part to cmdutil to make it easier to reuse
Martin Geisler <mg@lazybytes.net>
parents: 12266
diff changeset
  1324
5034
c0417a319e39 commands: move commit to cmdutil as wrapper for commit-like functions
Bryan O'Sullivan <bos@serpentine.com>
parents: 4965
diff changeset
  1325
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
  1326
    '''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
  1327
    date = opts.get('date')
989467e8e3a9 Fix bad behaviour when specifying an invalid date (issue700)
Thomas Arendsen Hein <thomas@intevation.de>
parents: 6112
diff changeset
  1328
    if date:
989467e8e3a9 Fix bad behaviour when specifying an invalid date (issue700)
Thomas Arendsen Hein <thomas@intevation.de>
parents: 6112
diff changeset
  1329
        opts['date'] = util.parsedate(date)
5034
c0417a319e39 commands: move commit to cmdutil as wrapper for commit-like functions
Bryan O'Sullivan <bos@serpentine.com>
parents: 4965
diff changeset
  1330
    message = logmessage(opts)
c0417a319e39 commands: move commit to cmdutil as wrapper for commit-like functions
Bryan O'Sullivan <bos@serpentine.com>
parents: 4965
diff changeset
  1331
5829
784073457a0f cmdutil.commit: extract 'addremove' from opts carefully
Kirill Smelkov <kirr@mns.spb.ru>
parents: 5797
diff changeset
  1332
    # 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
  1333
    # that doesn't support addremove
784073457a0f cmdutil.commit: extract 'addremove' from opts carefully
Kirill Smelkov <kirr@mns.spb.ru>
parents: 5797
diff changeset
  1334
    if opts.get('addremove'):
5034
c0417a319e39 commands: move commit to cmdutil as wrapper for commit-like functions
Bryan O'Sullivan <bos@serpentine.com>
parents: 4965
diff changeset
  1335
        addremove(repo, pats, opts)
5829
784073457a0f cmdutil.commit: extract 'addremove' from opts carefully
Kirill Smelkov <kirr@mns.spb.ru>
parents: 5797
diff changeset
  1336
8709
b9e0ddb04c5c commit: move explicit file checking into repo.commit
Matt Mackall <mpm@selenic.com>
parents: 8707
diff changeset
  1337
    return commitfunc(ui, repo, message, match(repo, pats, opts), opts)
8407
223000a687b0 commit: move commit editor to cmdutil, pass as function
Matt Mackall <mpm@selenic.com>
parents: 8390
diff changeset
  1338
8994
4a1187d3cb00 commit: report modified subrepos in commit editor
Matt Mackall <mpm@selenic.com>
parents: 8990
diff changeset
  1339
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
  1340
    if ctx.description():
223000a687b0 commit: move commit editor to cmdutil, pass as function
Matt Mackall <mpm@selenic.com>
parents: 8390
diff changeset
  1341
        return ctx.description()
8994
4a1187d3cb00 commit: report modified subrepos in commit editor
Matt Mackall <mpm@selenic.com>
parents: 8990
diff changeset
  1342
    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
  1343
8994
4a1187d3cb00 commit: report modified subrepos in commit editor
Matt Mackall <mpm@selenic.com>
parents: 8990
diff changeset
  1344
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
  1345
    edittext = []
8707
0550dfe4fca1 commit: editor reads file lists from provided context
Matt Mackall <mpm@selenic.com>
parents: 8680
diff changeset
  1346
    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
  1347
    if ctx.description():
223000a687b0 commit: move commit editor to cmdutil, pass as function
Matt Mackall <mpm@selenic.com>
parents: 8390
diff changeset
  1348
        edittext.append(ctx.description())
223000a687b0 commit: move commit editor to cmdutil, pass as function
Matt Mackall <mpm@selenic.com>
parents: 8390
diff changeset
  1349
    edittext.append("")
223000a687b0 commit: move commit editor to cmdutil, pass as function
Matt Mackall <mpm@selenic.com>
parents: 8390
diff changeset
  1350
    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
  1351
    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
  1352
                      "  Lines beginning with 'HG:' are removed."))
8535
5b6a6ed4f185 cmdutil: mark string for translation
Martin Geisler <mg@lazybytes.net>
parents: 8497
diff changeset
  1353
    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
  1354
    edittext.append("HG: --")
223000a687b0 commit: move commit editor to cmdutil, pass as function
Matt Mackall <mpm@selenic.com>
parents: 8390
diff changeset
  1355
    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
  1356
    if ctx.p2():
223000a687b0 commit: move commit editor to cmdutil, pass as function
Matt Mackall <mpm@selenic.com>
parents: 8390
diff changeset
  1357
        edittext.append(_("HG: branch merge"))
223000a687b0 commit: move commit editor to cmdutil, pass as function
Matt Mackall <mpm@selenic.com>
parents: 8390
diff changeset
  1358
    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
  1359
        edittext.append(_("HG: branch '%s'") % ctx.branch())
8994
4a1187d3cb00 commit: report modified subrepos in commit editor
Matt Mackall <mpm@selenic.com>
parents: 8990
diff changeset
  1360
    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
  1361
    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
  1362
    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
  1363
    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
  1364
    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
  1365
        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
  1366
    edittext.append("")
223000a687b0 commit: move commit editor to cmdutil, pass as function
Matt Mackall <mpm@selenic.com>
parents: 8390
diff changeset
  1367
    # 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
  1368
    olddir = os.getcwd()
223000a687b0 commit: move commit editor to cmdutil, pass as function
Matt Mackall <mpm@selenic.com>
parents: 8390
diff changeset
  1369
    os.chdir(repo.root)
223000a687b0 commit: move commit editor to cmdutil, pass as function
Matt Mackall <mpm@selenic.com>
parents: 8390
diff changeset
  1370
    text = repo.ui.edit("\n".join(edittext), ctx.user())
12900
4ff61287bde2 commit: handle missing newline on last commit comment
Matt Mackall <mpm@selenic.com>
parents: 12874
diff changeset
  1371
    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
  1372
    os.chdir(olddir)
223000a687b0 commit: move commit editor to cmdutil, pass as function
Matt Mackall <mpm@selenic.com>
parents: 8390
diff changeset
  1373
223000a687b0 commit: move commit editor to cmdutil, pass as function
Matt Mackall <mpm@selenic.com>
parents: 8390
diff changeset
  1374
    if not text.strip():
223000a687b0 commit: move commit editor to cmdutil, pass as function
Matt Mackall <mpm@selenic.com>
parents: 8390
diff changeset
  1375
        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
  1376
223000a687b0 commit: move commit editor to cmdutil, pass as function
Matt Mackall <mpm@selenic.com>
parents: 8390
diff changeset
  1377
    return text