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