mercurial/scmutil.py
author Yuya Nishihara <yuya@tcha.org>
Sun, 19 Feb 2017 18:19:33 +0900
changeset 31044 0b8356705de6
parent 30644 d524c88511a7
child 31045 6cf2857526c7
permissions -rw-r--r--
revset: split language services to revsetlang module (API) New revsetlang module hosts parser, tokenizer, and miscellaneous functions working on parsed tree. It does not include functions for evaluation such as getset() and match(). 2288 mercurial/revset.py 684 mercurial/revsetlang.py 2972 total get*() functions are aliased since they are common in revset.py.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
13962
8b252e826c68 add: introduce a warning message for non-portable filenames (issue2756) (BC)
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
     1
# scmutil.py - Mercurial core utility functions
8b252e826c68 add: introduce a warning message for non-portable filenames (issue2756) (BC)
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
     2
#
8b252e826c68 add: introduce a warning message for non-portable filenames (issue2756) (BC)
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
     3
#  Copyright Matt Mackall <mpm@selenic.com>
8b252e826c68 add: introduce a warning message for non-portable filenames (issue2756) (BC)
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
     4
#
8b252e826c68 add: introduce a warning message for non-portable filenames (issue2756) (BC)
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
     5
# This software may be used and distributed according to the terms of the
8b252e826c68 add: introduce a warning message for non-portable filenames (issue2756) (BC)
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
     6
# GNU General Public License version 2 or any later version.
8b252e826c68 add: introduce a warning message for non-portable filenames (issue2756) (BC)
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
     7
27482
dde3da2246f1 scmutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27093
diff changeset
     8
from __future__ import absolute_import
dde3da2246f1 scmutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27093
diff changeset
     9
27895
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
    10
import contextlib
27482
dde3da2246f1 scmutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27093
diff changeset
    11
import errno
dde3da2246f1 scmutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27093
diff changeset
    12
import glob
29341
0d83ad967bf8 cleanup: replace uses of util.(md5|sha1|sha256|sha512) with hashlib.\1
Augie Fackler <raf@durin42.com>
parents: 29336
diff changeset
    13
import hashlib
27482
dde3da2246f1 scmutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27093
diff changeset
    14
import os
dde3da2246f1 scmutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27093
diff changeset
    15
import re
dde3da2246f1 scmutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27093
diff changeset
    16
import shutil
30529
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
    17
import socket
27482
dde3da2246f1 scmutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27093
diff changeset
    18
import stat
dde3da2246f1 scmutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27093
diff changeset
    19
import tempfile
27895
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
    20
import threading
27482
dde3da2246f1 scmutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27093
diff changeset
    21
dde3da2246f1 scmutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27093
diff changeset
    22
from .i18n import _
dde3da2246f1 scmutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27093
diff changeset
    23
from .node import wdirrev
dde3da2246f1 scmutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27093
diff changeset
    24
from . import (
dde3da2246f1 scmutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27093
diff changeset
    25
    encoding,
dde3da2246f1 scmutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27093
diff changeset
    26
    error,
dde3da2246f1 scmutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27093
diff changeset
    27
    match as matchmod,
dde3da2246f1 scmutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27093
diff changeset
    28
    osutil,
dde3da2246f1 scmutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27093
diff changeset
    29
    pathutil,
dde3da2246f1 scmutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27093
diff changeset
    30
    phases,
30318
af7c60988f6e py3: make scmutil.rcpath() return bytes
Pulkit Goyal <7895pulkit@gmail.com>
parents: 30109
diff changeset
    31
    pycompat,
27482
dde3da2246f1 scmutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27093
diff changeset
    32
    revset,
31044
0b8356705de6 revset: split language services to revsetlang module (API)
Yuya Nishihara <yuya@tcha.org>
parents: 30644
diff changeset
    33
    revsetlang,
27482
dde3da2246f1 scmutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27093
diff changeset
    34
    similar,
dde3da2246f1 scmutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27093
diff changeset
    35
    util,
dde3da2246f1 scmutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27093
diff changeset
    36
)
18690
4c6f7f0dadab scmutil: split platform-specific bits into their own modules
Kevin Bullock <kbullock@ringworld.org>
parents: 18678
diff changeset
    37
30644
d524c88511a7 py3: replace os.name with pycompat.osname (part 1 of 2)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 30529
diff changeset
    38
if pycompat.osname == 'nt':
27482
dde3da2246f1 scmutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27093
diff changeset
    39
    from . import scmwindows as scmplatform
18690
4c6f7f0dadab scmutil: split platform-specific bits into their own modules
Kevin Bullock <kbullock@ringworld.org>
parents: 18678
diff changeset
    40
else:
27482
dde3da2246f1 scmutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27093
diff changeset
    41
    from . import scmposix as scmplatform
18690
4c6f7f0dadab scmutil: split platform-specific bits into their own modules
Kevin Bullock <kbullock@ringworld.org>
parents: 18678
diff changeset
    42
4c6f7f0dadab scmutil: split platform-specific bits into their own modules
Kevin Bullock <kbullock@ringworld.org>
parents: 18678
diff changeset
    43
systemrcpath = scmplatform.systemrcpath
4c6f7f0dadab scmutil: split platform-specific bits into their own modules
Kevin Bullock <kbullock@ringworld.org>
parents: 18678
diff changeset
    44
userrcpath = scmplatform.userrcpath
30327
365812902904 scmutil: extend termwidth() to return terminal height, renamed to termsize()
Yuya Nishihara <yuya@tcha.org>
parents: 30322
diff changeset
    45
termsize = scmplatform.termsize
13962
8b252e826c68 add: introduce a warning message for non-portable filenames (issue2756) (BC)
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
    46
22913
cb4449921a1d status: create class for status lists
Martin von Zweigbergk <martinvonz@gmail.com>
parents: 22816
diff changeset
    47
class status(tuple):
cb4449921a1d status: create class for status lists
Martin von Zweigbergk <martinvonz@gmail.com>
parents: 22816
diff changeset
    48
    '''Named tuple with a list of files per status. The 'deleted', 'unknown'
cb4449921a1d status: create class for status lists
Martin von Zweigbergk <martinvonz@gmail.com>
parents: 22816
diff changeset
    49
       and 'ignored' properties are only relevant to the working copy.
cb4449921a1d status: create class for status lists
Martin von Zweigbergk <martinvonz@gmail.com>
parents: 22816
diff changeset
    50
    '''
cb4449921a1d status: create class for status lists
Martin von Zweigbergk <martinvonz@gmail.com>
parents: 22816
diff changeset
    51
cb4449921a1d status: create class for status lists
Martin von Zweigbergk <martinvonz@gmail.com>
parents: 22816
diff changeset
    52
    __slots__ = ()
cb4449921a1d status: create class for status lists
Martin von Zweigbergk <martinvonz@gmail.com>
parents: 22816
diff changeset
    53
cb4449921a1d status: create class for status lists
Martin von Zweigbergk <martinvonz@gmail.com>
parents: 22816
diff changeset
    54
    def __new__(cls, modified, added, removed, deleted, unknown, ignored,
cb4449921a1d status: create class for status lists
Martin von Zweigbergk <martinvonz@gmail.com>
parents: 22816
diff changeset
    55
                clean):
cb4449921a1d status: create class for status lists
Martin von Zweigbergk <martinvonz@gmail.com>
parents: 22816
diff changeset
    56
        return tuple.__new__(cls, (modified, added, removed, deleted, unknown,
cb4449921a1d status: create class for status lists
Martin von Zweigbergk <martinvonz@gmail.com>
parents: 22816
diff changeset
    57
                                   ignored, clean))
cb4449921a1d status: create class for status lists
Martin von Zweigbergk <martinvonz@gmail.com>
parents: 22816
diff changeset
    58
cb4449921a1d status: create class for status lists
Martin von Zweigbergk <martinvonz@gmail.com>
parents: 22816
diff changeset
    59
    @property
cb4449921a1d status: create class for status lists
Martin von Zweigbergk <martinvonz@gmail.com>
parents: 22816
diff changeset
    60
    def modified(self):
22915
4d680deb0d9e status: update and move documentation of status types to status class
Martin von Zweigbergk <martinvonz@gmail.com>
parents: 22913
diff changeset
    61
        '''files that have been modified'''
22913
cb4449921a1d status: create class for status lists
Martin von Zweigbergk <martinvonz@gmail.com>
parents: 22816
diff changeset
    62
        return self[0]
cb4449921a1d status: create class for status lists
Martin von Zweigbergk <martinvonz@gmail.com>
parents: 22816
diff changeset
    63
cb4449921a1d status: create class for status lists
Martin von Zweigbergk <martinvonz@gmail.com>
parents: 22816
diff changeset
    64
    @property
cb4449921a1d status: create class for status lists
Martin von Zweigbergk <martinvonz@gmail.com>
parents: 22816
diff changeset
    65
    def added(self):
22915
4d680deb0d9e status: update and move documentation of status types to status class
Martin von Zweigbergk <martinvonz@gmail.com>
parents: 22913
diff changeset
    66
        '''files that have been added'''
22913
cb4449921a1d status: create class for status lists
Martin von Zweigbergk <martinvonz@gmail.com>
parents: 22816
diff changeset
    67
        return self[1]
cb4449921a1d status: create class for status lists
Martin von Zweigbergk <martinvonz@gmail.com>
parents: 22816
diff changeset
    68
cb4449921a1d status: create class for status lists
Martin von Zweigbergk <martinvonz@gmail.com>
parents: 22816
diff changeset
    69
    @property
cb4449921a1d status: create class for status lists
Martin von Zweigbergk <martinvonz@gmail.com>
parents: 22816
diff changeset
    70
    def removed(self):
22915
4d680deb0d9e status: update and move documentation of status types to status class
Martin von Zweigbergk <martinvonz@gmail.com>
parents: 22913
diff changeset
    71
        '''files that have been removed'''
22913
cb4449921a1d status: create class for status lists
Martin von Zweigbergk <martinvonz@gmail.com>
parents: 22816
diff changeset
    72
        return self[2]
cb4449921a1d status: create class for status lists
Martin von Zweigbergk <martinvonz@gmail.com>
parents: 22816
diff changeset
    73
cb4449921a1d status: create class for status lists
Martin von Zweigbergk <martinvonz@gmail.com>
parents: 22816
diff changeset
    74
    @property
cb4449921a1d status: create class for status lists
Martin von Zweigbergk <martinvonz@gmail.com>
parents: 22816
diff changeset
    75
    def deleted(self):
22915
4d680deb0d9e status: update and move documentation of status types to status class
Martin von Zweigbergk <martinvonz@gmail.com>
parents: 22913
diff changeset
    76
        '''files that are in the dirstate, but have been deleted from the
4d680deb0d9e status: update and move documentation of status types to status class
Martin von Zweigbergk <martinvonz@gmail.com>
parents: 22913
diff changeset
    77
           working copy (aka "missing")
4d680deb0d9e status: update and move documentation of status types to status class
Martin von Zweigbergk <martinvonz@gmail.com>
parents: 22913
diff changeset
    78
        '''
22913
cb4449921a1d status: create class for status lists
Martin von Zweigbergk <martinvonz@gmail.com>
parents: 22816
diff changeset
    79
        return self[3]
cb4449921a1d status: create class for status lists
Martin von Zweigbergk <martinvonz@gmail.com>
parents: 22816
diff changeset
    80
cb4449921a1d status: create class for status lists
Martin von Zweigbergk <martinvonz@gmail.com>
parents: 22816
diff changeset
    81
    @property
cb4449921a1d status: create class for status lists
Martin von Zweigbergk <martinvonz@gmail.com>
parents: 22816
diff changeset
    82
    def unknown(self):
22915
4d680deb0d9e status: update and move documentation of status types to status class
Martin von Zweigbergk <martinvonz@gmail.com>
parents: 22913
diff changeset
    83
        '''files not in the dirstate that are not ignored'''
22913
cb4449921a1d status: create class for status lists
Martin von Zweigbergk <martinvonz@gmail.com>
parents: 22816
diff changeset
    84
        return self[4]
cb4449921a1d status: create class for status lists
Martin von Zweigbergk <martinvonz@gmail.com>
parents: 22816
diff changeset
    85
cb4449921a1d status: create class for status lists
Martin von Zweigbergk <martinvonz@gmail.com>
parents: 22816
diff changeset
    86
    @property
cb4449921a1d status: create class for status lists
Martin von Zweigbergk <martinvonz@gmail.com>
parents: 22816
diff changeset
    87
    def ignored(self):
22915
4d680deb0d9e status: update and move documentation of status types to status class
Martin von Zweigbergk <martinvonz@gmail.com>
parents: 22913
diff changeset
    88
        '''files not in the dirstate that are ignored (by _dirignore())'''
22913
cb4449921a1d status: create class for status lists
Martin von Zweigbergk <martinvonz@gmail.com>
parents: 22816
diff changeset
    89
        return self[5]
cb4449921a1d status: create class for status lists
Martin von Zweigbergk <martinvonz@gmail.com>
parents: 22816
diff changeset
    90
cb4449921a1d status: create class for status lists
Martin von Zweigbergk <martinvonz@gmail.com>
parents: 22816
diff changeset
    91
    @property
cb4449921a1d status: create class for status lists
Martin von Zweigbergk <martinvonz@gmail.com>
parents: 22816
diff changeset
    92
    def clean(self):
22915
4d680deb0d9e status: update and move documentation of status types to status class
Martin von Zweigbergk <martinvonz@gmail.com>
parents: 22913
diff changeset
    93
        '''files that have not been modified'''
22913
cb4449921a1d status: create class for status lists
Martin von Zweigbergk <martinvonz@gmail.com>
parents: 22816
diff changeset
    94
        return self[6]
cb4449921a1d status: create class for status lists
Martin von Zweigbergk <martinvonz@gmail.com>
parents: 22816
diff changeset
    95
cb4449921a1d status: create class for status lists
Martin von Zweigbergk <martinvonz@gmail.com>
parents: 22816
diff changeset
    96
    def __repr__(self, *args, **kwargs):
cb4449921a1d status: create class for status lists
Martin von Zweigbergk <martinvonz@gmail.com>
parents: 22816
diff changeset
    97
        return (('<status modified=%r, added=%r, removed=%r, deleted=%r, '
cb4449921a1d status: create class for status lists
Martin von Zweigbergk <martinvonz@gmail.com>
parents: 22816
diff changeset
    98
                 'unknown=%r, ignored=%r, clean=%r>') % self)
cb4449921a1d status: create class for status lists
Martin von Zweigbergk <martinvonz@gmail.com>
parents: 22816
diff changeset
    99
20392
d4f804caa0ed itersubrepos: move to scmutil to break a direct import cycle
Augie Fackler <raf@durin42.com>
parents: 20364
diff changeset
   100
def itersubrepos(ctx1, ctx2):
d4f804caa0ed itersubrepos: move to scmutil to break a direct import cycle
Augie Fackler <raf@durin42.com>
parents: 20364
diff changeset
   101
    """find subrepos in ctx1 or ctx2"""
d4f804caa0ed itersubrepos: move to scmutil to break a direct import cycle
Augie Fackler <raf@durin42.com>
parents: 20364
diff changeset
   102
    # Create a (subpath, ctx) mapping where we prefer subpaths from
d4f804caa0ed itersubrepos: move to scmutil to break a direct import cycle
Augie Fackler <raf@durin42.com>
parents: 20364
diff changeset
   103
    # ctx1. The subpaths from ctx2 are important when the .hgsub file
d4f804caa0ed itersubrepos: move to scmutil to break a direct import cycle
Augie Fackler <raf@durin42.com>
parents: 20364
diff changeset
   104
    # has been modified (in ctx2) but not yet committed (in ctx1).
d4f804caa0ed itersubrepos: move to scmutil to break a direct import cycle
Augie Fackler <raf@durin42.com>
parents: 20364
diff changeset
   105
    subpaths = dict.fromkeys(ctx2.substate, ctx2)
d4f804caa0ed itersubrepos: move to scmutil to break a direct import cycle
Augie Fackler <raf@durin42.com>
parents: 20364
diff changeset
   106
    subpaths.update(dict.fromkeys(ctx1.substate, ctx1))
25418
c0995cd8ff6f scmutil: consistently return subrepos relative to ctx1 from itersubrepos()
Matt Harbison <matt_harbison@yahoo.com>
parents: 25386
diff changeset
   107
c0995cd8ff6f scmutil: consistently return subrepos relative to ctx1 from itersubrepos()
Matt Harbison <matt_harbison@yahoo.com>
parents: 25386
diff changeset
   108
    missing = set()
c0995cd8ff6f scmutil: consistently return subrepos relative to ctx1 from itersubrepos()
Matt Harbison <matt_harbison@yahoo.com>
parents: 25386
diff changeset
   109
c0995cd8ff6f scmutil: consistently return subrepos relative to ctx1 from itersubrepos()
Matt Harbison <matt_harbison@yahoo.com>
parents: 25386
diff changeset
   110
    for subpath in ctx2.substate:
c0995cd8ff6f scmutil: consistently return subrepos relative to ctx1 from itersubrepos()
Matt Harbison <matt_harbison@yahoo.com>
parents: 25386
diff changeset
   111
        if subpath not in ctx1.substate:
c0995cd8ff6f scmutil: consistently return subrepos relative to ctx1 from itersubrepos()
Matt Harbison <matt_harbison@yahoo.com>
parents: 25386
diff changeset
   112
            del subpaths[subpath]
c0995cd8ff6f scmutil: consistently return subrepos relative to ctx1 from itersubrepos()
Matt Harbison <matt_harbison@yahoo.com>
parents: 25386
diff changeset
   113
            missing.add(subpath)
c0995cd8ff6f scmutil: consistently return subrepos relative to ctx1 from itersubrepos()
Matt Harbison <matt_harbison@yahoo.com>
parents: 25386
diff changeset
   114
20392
d4f804caa0ed itersubrepos: move to scmutil to break a direct import cycle
Augie Fackler <raf@durin42.com>
parents: 20364
diff changeset
   115
    for subpath, ctx in sorted(subpaths.iteritems()):
d4f804caa0ed itersubrepos: move to scmutil to break a direct import cycle
Augie Fackler <raf@durin42.com>
parents: 20364
diff changeset
   116
        yield subpath, ctx.sub(subpath)
d4f804caa0ed itersubrepos: move to scmutil to break a direct import cycle
Augie Fackler <raf@durin42.com>
parents: 20364
diff changeset
   117
25418
c0995cd8ff6f scmutil: consistently return subrepos relative to ctx1 from itersubrepos()
Matt Harbison <matt_harbison@yahoo.com>
parents: 25386
diff changeset
   118
    # Yield an empty subrepo based on ctx1 for anything only in ctx2.  That way,
c0995cd8ff6f scmutil: consistently return subrepos relative to ctx1 from itersubrepos()
Matt Harbison <matt_harbison@yahoo.com>
parents: 25386
diff changeset
   119
    # status and diff will have an accurate result when it does
c0995cd8ff6f scmutil: consistently return subrepos relative to ctx1 from itersubrepos()
Matt Harbison <matt_harbison@yahoo.com>
parents: 25386
diff changeset
   120
    # 'sub.{status|diff}(rev2)'.  Otherwise, the ctx2 subrepo is compared
c0995cd8ff6f scmutil: consistently return subrepos relative to ctx1 from itersubrepos()
Matt Harbison <matt_harbison@yahoo.com>
parents: 25386
diff changeset
   121
    # against itself.
c0995cd8ff6f scmutil: consistently return subrepos relative to ctx1 from itersubrepos()
Matt Harbison <matt_harbison@yahoo.com>
parents: 25386
diff changeset
   122
    for subpath in missing:
c0995cd8ff6f scmutil: consistently return subrepos relative to ctx1 from itersubrepos()
Matt Harbison <matt_harbison@yahoo.com>
parents: 25386
diff changeset
   123
        yield subpath, ctx2.nullsub(subpath, ctx1)
c0995cd8ff6f scmutil: consistently return subrepos relative to ctx1 from itersubrepos()
Matt Harbison <matt_harbison@yahoo.com>
parents: 25386
diff changeset
   124
17248
6ffb35b2284c discovery: add extinct changesets to outgoing.excluded
Patrick Mezard <patrick@mezard.eu>
parents: 17201
diff changeset
   125
def nochangesfound(ui, repo, excluded=None):
6ffb35b2284c discovery: add extinct changesets to outgoing.excluded
Patrick Mezard <patrick@mezard.eu>
parents: 17201
diff changeset
   126
    '''Report no changes for push/pull, excluded is None or a list of
6ffb35b2284c discovery: add extinct changesets to outgoing.excluded
Patrick Mezard <patrick@mezard.eu>
parents: 17201
diff changeset
   127
    nodes excluded from the push/pull.
6ffb35b2284c discovery: add extinct changesets to outgoing.excluded
Patrick Mezard <patrick@mezard.eu>
parents: 17201
diff changeset
   128
    '''
6ffb35b2284c discovery: add extinct changesets to outgoing.excluded
Patrick Mezard <patrick@mezard.eu>
parents: 17201
diff changeset
   129
    secretlist = []
6ffb35b2284c discovery: add extinct changesets to outgoing.excluded
Patrick Mezard <patrick@mezard.eu>
parents: 17201
diff changeset
   130
    if excluded:
6ffb35b2284c discovery: add extinct changesets to outgoing.excluded
Patrick Mezard <patrick@mezard.eu>
parents: 17201
diff changeset
   131
        for n in excluded:
18617
227479f61db9 outgoing: fix possible filtering crash in outgoing (issue3814)
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 18466
diff changeset
   132
            if n not in repo:
227479f61db9 outgoing: fix possible filtering crash in outgoing (issue3814)
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 18466
diff changeset
   133
                # discovery should not have included the filtered revision,
227479f61db9 outgoing: fix possible filtering crash in outgoing (issue3814)
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 18466
diff changeset
   134
                # we have to explicitly exclude it until discovery is cleanup.
227479f61db9 outgoing: fix possible filtering crash in outgoing (issue3814)
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 18466
diff changeset
   135
                continue
17248
6ffb35b2284c discovery: add extinct changesets to outgoing.excluded
Patrick Mezard <patrick@mezard.eu>
parents: 17201
diff changeset
   136
            ctx = repo[n]
6ffb35b2284c discovery: add extinct changesets to outgoing.excluded
Patrick Mezard <patrick@mezard.eu>
parents: 17201
diff changeset
   137
            if ctx.phase() >= phases.secret and not ctx.extinct():
6ffb35b2284c discovery: add extinct changesets to outgoing.excluded
Patrick Mezard <patrick@mezard.eu>
parents: 17201
diff changeset
   138
                secretlist.append(n)
6ffb35b2284c discovery: add extinct changesets to outgoing.excluded
Patrick Mezard <patrick@mezard.eu>
parents: 17201
diff changeset
   139
15993
0b05e0bfdc1c scmutil: unify some 'no changes found' messages
Matt Mackall <mpm@selenic.com>
parents: 15797
diff changeset
   140
    if secretlist:
0b05e0bfdc1c scmutil: unify some 'no changes found' messages
Matt Mackall <mpm@selenic.com>
parents: 15797
diff changeset
   141
        ui.status(_("no changes found (ignored %d secret changesets)\n")
0b05e0bfdc1c scmutil: unify some 'no changes found' messages
Matt Mackall <mpm@selenic.com>
parents: 15797
diff changeset
   142
                  % len(secretlist))
0b05e0bfdc1c scmutil: unify some 'no changes found' messages
Matt Mackall <mpm@selenic.com>
parents: 15797
diff changeset
   143
    else:
0b05e0bfdc1c scmutil: unify some 'no changes found' messages
Matt Mackall <mpm@selenic.com>
parents: 15797
diff changeset
   144
        ui.status(_("no changes found\n"))
0b05e0bfdc1c scmutil: unify some 'no changes found' messages
Matt Mackall <mpm@selenic.com>
parents: 15797
diff changeset
   145
30529
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   146
def callcatch(ui, func):
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   147
    """call func() with global exception handling
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   148
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   149
    return func() if no exception happens. otherwise do some error handling
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   150
    and return an exit code accordingly. does not handle all exceptions.
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   151
    """
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   152
    try:
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   153
        return func()
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   154
    # Global exception handling, alphabetically
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   155
    # Mercurial-specific first, followed by built-in and library exceptions
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   156
    except error.LockHeld as inst:
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   157
        if inst.errno == errno.ETIMEDOUT:
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   158
            reason = _('timed out waiting for lock held by %s') % inst.locker
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   159
        else:
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   160
            reason = _('lock held by %s') % inst.locker
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   161
        ui.warn(_("abort: %s: %s\n") % (inst.desc or inst.filename, reason))
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   162
    except error.LockUnavailable as inst:
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   163
        ui.warn(_("abort: could not lock %s: %s\n") %
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   164
               (inst.desc or inst.filename, inst.strerror))
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   165
    except error.OutOfBandError as inst:
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   166
        if inst.args:
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   167
            msg = _("abort: remote error:\n")
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   168
        else:
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   169
            msg = _("abort: remote error\n")
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   170
        ui.warn(msg)
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   171
        if inst.args:
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   172
            ui.warn(''.join(inst.args))
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   173
        if inst.hint:
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   174
            ui.warn('(%s)\n' % inst.hint)
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   175
    except error.RepoError as inst:
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   176
        ui.warn(_("abort: %s!\n") % inst)
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   177
        if inst.hint:
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   178
            ui.warn(_("(%s)\n") % inst.hint)
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   179
    except error.ResponseError as inst:
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   180
        ui.warn(_("abort: %s") % inst.args[0])
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   181
        if not isinstance(inst.args[1], basestring):
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   182
            ui.warn(" %r\n" % (inst.args[1],))
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   183
        elif not inst.args[1]:
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   184
            ui.warn(_(" empty string\n"))
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   185
        else:
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   186
            ui.warn("\n%r\n" % util.ellipsis(inst.args[1]))
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   187
    except error.CensoredNodeError as inst:
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   188
        ui.warn(_("abort: file censored %s!\n") % inst)
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   189
    except error.RevlogError as inst:
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   190
        ui.warn(_("abort: %s!\n") % inst)
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   191
    except error.SignalInterrupt:
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   192
        ui.warn(_("killed!\n"))
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   193
    except error.InterventionRequired as inst:
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   194
        ui.warn("%s\n" % inst)
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   195
        if inst.hint:
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   196
            ui.warn(_("(%s)\n") % inst.hint)
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   197
        return 1
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   198
    except error.Abort as inst:
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   199
        ui.warn(_("abort: %s\n") % inst)
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   200
        if inst.hint:
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   201
            ui.warn(_("(%s)\n") % inst.hint)
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   202
    except ImportError as inst:
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   203
        ui.warn(_("abort: %s!\n") % inst)
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   204
        m = str(inst).split()[-1]
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   205
        if m in "mpatch bdiff".split():
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   206
            ui.warn(_("(did you forget to compile extensions?)\n"))
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   207
        elif m in "zlib".split():
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   208
            ui.warn(_("(is your Python install correct?)\n"))
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   209
    except IOError as inst:
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   210
        if util.safehasattr(inst, "code"):
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   211
            ui.warn(_("abort: %s\n") % inst)
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   212
        elif util.safehasattr(inst, "reason"):
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   213
            try: # usually it is in the form (errno, strerror)
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   214
                reason = inst.reason.args[1]
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   215
            except (AttributeError, IndexError):
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   216
                # it might be anything, for example a string
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   217
                reason = inst.reason
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   218
            if isinstance(reason, unicode):
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   219
                # SSLError of Python 2.7.9 contains a unicode
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   220
                reason = reason.encode(encoding.encoding, 'replace')
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   221
            ui.warn(_("abort: error: %s\n") % reason)
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   222
        elif (util.safehasattr(inst, "args")
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   223
              and inst.args and inst.args[0] == errno.EPIPE):
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   224
            pass
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   225
        elif getattr(inst, "strerror", None):
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   226
            if getattr(inst, "filename", None):
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   227
                ui.warn(_("abort: %s: %s\n") % (inst.strerror, inst.filename))
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   228
            else:
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   229
                ui.warn(_("abort: %s\n") % inst.strerror)
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   230
        else:
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   231
            raise
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   232
    except OSError as inst:
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   233
        if getattr(inst, "filename", None) is not None:
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   234
            ui.warn(_("abort: %s: '%s'\n") % (inst.strerror, inst.filename))
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   235
        else:
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   236
            ui.warn(_("abort: %s\n") % inst.strerror)
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   237
    except MemoryError:
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   238
        ui.warn(_("abort: out of memory\n"))
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   239
    except SystemExit as inst:
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   240
        # Commands shouldn't sys.exit directly, but give a return code.
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   241
        # Just in case catch this and and pass exit code to caller.
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   242
        return inst.code
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   243
    except socket.error as inst:
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   244
        ui.warn(_("abort: %s\n") % inst.args[-1])
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   245
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   246
    return -1
4338f87dbf6f dispatch: move part of callcatch to scmutil
Jun Wu <quark@fb.com>
parents: 30427
diff changeset
   247
17821
361ab1e2086f scmutil: add bad character checking to checknewlabel
Kevin Bullock <kbullock@ringworld.org>
parents: 17817
diff changeset
   248
def checknewlabel(repo, lbl, kind):
19070
290a61833b99 translations: change label integer error to not specify the kind of label
Durham Goode <durham@fb.com>
parents: 18951
diff changeset
   249
    # Do not use the "kind" parameter in ui output.
290a61833b99 translations: change label integer error to not specify the kind of label
Durham Goode <durham@fb.com>
parents: 18951
diff changeset
   250
    # It makes strings difficult to translate.
17817
b17be267b59c scmutil: add function to validate new branch, tag, and bookmark names
Kevin Bullock <kbullock@ringworld.org>
parents: 17768
diff changeset
   251
    if lbl in ['tip', '.', 'null']:
26587
56b2bcea2529 error: get Abort from 'error' instead of 'util'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26491
diff changeset
   252
        raise error.Abort(_("the name '%s' is reserved") % lbl)
17821
361ab1e2086f scmutil: add bad character checking to checknewlabel
Kevin Bullock <kbullock@ringworld.org>
parents: 17817
diff changeset
   253
    for c in (':', '\0', '\n', '\r'):
361ab1e2086f scmutil: add bad character checking to checknewlabel
Kevin Bullock <kbullock@ringworld.org>
parents: 17817
diff changeset
   254
        if c in lbl:
26587
56b2bcea2529 error: get Abort from 'error' instead of 'util'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26491
diff changeset
   255
            raise error.Abort(_("%r cannot be used in a name") % c)
18566
341868ef0cf6 bookmark: don't allow integers as bookmark/branch/tag names
Durham Goode <durham@fb.com>
parents: 18560
diff changeset
   256
    try:
341868ef0cf6 bookmark: don't allow integers as bookmark/branch/tag names
Durham Goode <durham@fb.com>
parents: 18560
diff changeset
   257
        int(lbl)
26587
56b2bcea2529 error: get Abort from 'error' instead of 'util'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26491
diff changeset
   258
        raise error.Abort(_("cannot use an integer as a name"))
18566
341868ef0cf6 bookmark: don't allow integers as bookmark/branch/tag names
Durham Goode <durham@fb.com>
parents: 18560
diff changeset
   259
    except ValueError:
341868ef0cf6 bookmark: don't allow integers as bookmark/branch/tag names
Durham Goode <durham@fb.com>
parents: 18560
diff changeset
   260
        pass
17817
b17be267b59c scmutil: add function to validate new branch, tag, and bookmark names
Kevin Bullock <kbullock@ringworld.org>
parents: 17768
diff changeset
   261
13974
23f2736abce3 move checkfilename from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13973
diff changeset
   262
def checkfilename(f):
23f2736abce3 move checkfilename from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13973
diff changeset
   263
    '''Check that the filename f is an acceptable filename for a tracked file'''
23f2736abce3 move checkfilename from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13973
diff changeset
   264
    if '\r' in f or '\n' in f:
26587
56b2bcea2529 error: get Abort from 'error' instead of 'util'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26491
diff changeset
   265
        raise error.Abort(_("'\\n' and '\\r' disallowed in filenames: %r") % f)
13974
23f2736abce3 move checkfilename from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13973
diff changeset
   266
13962
8b252e826c68 add: introduce a warning message for non-portable filenames (issue2756) (BC)
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
   267
def checkportable(ui, f):
8b252e826c68 add: introduce a warning message for non-portable filenames (issue2756) (BC)
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
   268
    '''Check if filename f is portable and warn or abort depending on config'''
13974
23f2736abce3 move checkfilename from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13973
diff changeset
   269
    checkfilename(f)
14138
c18204fd35b0 scmutil: introduce casecollisionauditor
Adrian Buehlmann <adrian@cadifra.com>
parents: 14097
diff changeset
   270
    abort, warn = checkportabilityalert(ui)
c18204fd35b0 scmutil: introduce casecollisionauditor
Adrian Buehlmann <adrian@cadifra.com>
parents: 14097
diff changeset
   271
    if abort or warn:
13962
8b252e826c68 add: introduce a warning message for non-portable filenames (issue2756) (BC)
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
   272
        msg = util.checkwinfilename(f)
8b252e826c68 add: introduce a warning message for non-portable filenames (issue2756) (BC)
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
   273
        if msg:
14138
c18204fd35b0 scmutil: introduce casecollisionauditor
Adrian Buehlmann <adrian@cadifra.com>
parents: 14097
diff changeset
   274
            msg = "%s: %r" % (msg, f)
c18204fd35b0 scmutil: introduce casecollisionauditor
Adrian Buehlmann <adrian@cadifra.com>
parents: 14097
diff changeset
   275
            if abort:
26587
56b2bcea2529 error: get Abort from 'error' instead of 'util'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26491
diff changeset
   276
                raise error.Abort(msg)
14138
c18204fd35b0 scmutil: introduce casecollisionauditor
Adrian Buehlmann <adrian@cadifra.com>
parents: 14097
diff changeset
   277
            ui.warn(_("warning: %s\n") % msg)
14068
04ce8fa1015d add: notify when adding a file that would cause a case-folding collision
Kevin Gessner <kevin@kevingessner.com>
parents: 14067
diff changeset
   278
14067
e88a4958a6b7 scmutil: refactor ui.portablefilenames processing
Kevin Gessner <kevin@kevingessner.com>
parents: 13986
diff changeset
   279
def checkportabilityalert(ui):
e88a4958a6b7 scmutil: refactor ui.portablefilenames processing
Kevin Gessner <kevin@kevingessner.com>
parents: 13986
diff changeset
   280
    '''check if the user's config requests nothing, a warning, or abort for
e88a4958a6b7 scmutil: refactor ui.portablefilenames processing
Kevin Gessner <kevin@kevingessner.com>
parents: 13986
diff changeset
   281
    non-portable filenames'''
e88a4958a6b7 scmutil: refactor ui.portablefilenames processing
Kevin Gessner <kevin@kevingessner.com>
parents: 13986
diff changeset
   282
    val = ui.config('ui', 'portablefilenames', 'warn')
e88a4958a6b7 scmutil: refactor ui.portablefilenames processing
Kevin Gessner <kevin@kevingessner.com>
parents: 13986
diff changeset
   283
    lval = val.lower()
e88a4958a6b7 scmutil: refactor ui.portablefilenames processing
Kevin Gessner <kevin@kevingessner.com>
parents: 13986
diff changeset
   284
    bval = util.parsebool(val)
30644
d524c88511a7 py3: replace os.name with pycompat.osname (part 1 of 2)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 30529
diff changeset
   285
    abort = pycompat.osname == 'nt' or lval == 'abort'
14067
e88a4958a6b7 scmutil: refactor ui.portablefilenames processing
Kevin Gessner <kevin@kevingessner.com>
parents: 13986
diff changeset
   286
    warn = bval or lval == 'warn'
e88a4958a6b7 scmutil: refactor ui.portablefilenames processing
Kevin Gessner <kevin@kevingessner.com>
parents: 13986
diff changeset
   287
    if bval is None and not (warn or abort or lval == 'ignore'):
13962
8b252e826c68 add: introduce a warning message for non-portable filenames (issue2756) (BC)
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
   288
        raise error.ConfigError(
8b252e826c68 add: introduce a warning message for non-portable filenames (issue2756) (BC)
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
   289
            _("ui.portablefilenames value is invalid ('%s')") % val)
14067
e88a4958a6b7 scmutil: refactor ui.portablefilenames processing
Kevin Gessner <kevin@kevingessner.com>
parents: 13986
diff changeset
   290
    return abort, warn
e88a4958a6b7 scmutil: refactor ui.portablefilenames processing
Kevin Gessner <kevin@kevingessner.com>
parents: 13986
diff changeset
   291
14138
c18204fd35b0 scmutil: introduce casecollisionauditor
Adrian Buehlmann <adrian@cadifra.com>
parents: 14097
diff changeset
   292
class casecollisionauditor(object):
17201
afd75476939e scmutil: 25% speedup in casecollisionauditor
Joshua Redstone <joshua.redstone@fb.com>
parents: 17161
diff changeset
   293
    def __init__(self, ui, abort, dirstate):
14138
c18204fd35b0 scmutil: introduce casecollisionauditor
Adrian Buehlmann <adrian@cadifra.com>
parents: 14097
diff changeset
   294
        self._ui = ui
c18204fd35b0 scmutil: introduce casecollisionauditor
Adrian Buehlmann <adrian@cadifra.com>
parents: 14097
diff changeset
   295
        self._abort = abort
17201
afd75476939e scmutil: 25% speedup in casecollisionauditor
Joshua Redstone <joshua.redstone@fb.com>
parents: 17161
diff changeset
   296
        allfiles = '\0'.join(dirstate._map)
afd75476939e scmutil: 25% speedup in casecollisionauditor
Joshua Redstone <joshua.redstone@fb.com>
parents: 17161
diff changeset
   297
        self._loweredfiles = set(encoding.lower(allfiles).split('\0'))
afd75476939e scmutil: 25% speedup in casecollisionauditor
Joshua Redstone <joshua.redstone@fb.com>
parents: 17161
diff changeset
   298
        self._dirstate = dirstate
afd75476939e scmutil: 25% speedup in casecollisionauditor
Joshua Redstone <joshua.redstone@fb.com>
parents: 17161
diff changeset
   299
        # The purpose of _newfiles is so that we don't complain about
afd75476939e scmutil: 25% speedup in casecollisionauditor
Joshua Redstone <joshua.redstone@fb.com>
parents: 17161
diff changeset
   300
        # case collisions if someone were to call this object with the
afd75476939e scmutil: 25% speedup in casecollisionauditor
Joshua Redstone <joshua.redstone@fb.com>
parents: 17161
diff changeset
   301
        # same filename twice.
afd75476939e scmutil: 25% speedup in casecollisionauditor
Joshua Redstone <joshua.redstone@fb.com>
parents: 17161
diff changeset
   302
        self._newfiles = set()
14067
e88a4958a6b7 scmutil: refactor ui.portablefilenames processing
Kevin Gessner <kevin@kevingessner.com>
parents: 13986
diff changeset
   303
14138
c18204fd35b0 scmutil: introduce casecollisionauditor
Adrian Buehlmann <adrian@cadifra.com>
parents: 14097
diff changeset
   304
    def __call__(self, f):
20006
9276014db865 scmutil: skip checks in "casecollisionauditor" if filename is already checked
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 19900
diff changeset
   305
        if f in self._newfiles:
9276014db865 scmutil: skip checks in "casecollisionauditor" if filename is already checked
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 19900
diff changeset
   306
            return
14980
28e98a8b173d i18n: use UTF-8 string to lower filename for case collision check
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 14861
diff changeset
   307
        fl = encoding.lower(f)
20006
9276014db865 scmutil: skip checks in "casecollisionauditor" if filename is already checked
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 19900
diff changeset
   308
        if fl in self._loweredfiles and f not in self._dirstate:
14138
c18204fd35b0 scmutil: introduce casecollisionauditor
Adrian Buehlmann <adrian@cadifra.com>
parents: 14097
diff changeset
   309
            msg = _('possible case-folding collision for %s') % f
c18204fd35b0 scmutil: introduce casecollisionauditor
Adrian Buehlmann <adrian@cadifra.com>
parents: 14097
diff changeset
   310
            if self._abort:
26587
56b2bcea2529 error: get Abort from 'error' instead of 'util'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26491
diff changeset
   311
                raise error.Abort(msg)
14138
c18204fd35b0 scmutil: introduce casecollisionauditor
Adrian Buehlmann <adrian@cadifra.com>
parents: 14097
diff changeset
   312
            self._ui.warn(_("warning: %s\n") % msg)
17201
afd75476939e scmutil: 25% speedup in casecollisionauditor
Joshua Redstone <joshua.redstone@fb.com>
parents: 17161
diff changeset
   313
        self._loweredfiles.add(fl)
afd75476939e scmutil: 25% speedup in casecollisionauditor
Joshua Redstone <joshua.redstone@fb.com>
parents: 17161
diff changeset
   314
        self._newfiles.add(f)
13970
d13913355390 move opener from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13962
diff changeset
   315
24723
467a33142425 repoview: move function for computing filtered hash
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24693
diff changeset
   316
def filteredhash(repo, maxrev):
467a33142425 repoview: move function for computing filtered hash
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24693
diff changeset
   317
    """build hash of filtered revisions in the current repoview.
467a33142425 repoview: move function for computing filtered hash
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24693
diff changeset
   318
467a33142425 repoview: move function for computing filtered hash
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24693
diff changeset
   319
    Multiple caches perform up-to-date validation by checking that the
467a33142425 repoview: move function for computing filtered hash
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24693
diff changeset
   320
    tiprev and tipnode stored in the cache file match the current repository.
467a33142425 repoview: move function for computing filtered hash
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24693
diff changeset
   321
    However, this is not sufficient for validating repoviews because the set
467a33142425 repoview: move function for computing filtered hash
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24693
diff changeset
   322
    of revisions in the view may change without the repository tiprev and
467a33142425 repoview: move function for computing filtered hash
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24693
diff changeset
   323
    tipnode changing.
467a33142425 repoview: move function for computing filtered hash
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24693
diff changeset
   324
467a33142425 repoview: move function for computing filtered hash
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24693
diff changeset
   325
    This function hashes all the revs filtered from the view and returns
467a33142425 repoview: move function for computing filtered hash
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24693
diff changeset
   326
    that SHA-1 digest.
467a33142425 repoview: move function for computing filtered hash
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24693
diff changeset
   327
    """
467a33142425 repoview: move function for computing filtered hash
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24693
diff changeset
   328
    cl = repo.changelog
467a33142425 repoview: move function for computing filtered hash
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24693
diff changeset
   329
    if not cl.filteredrevs:
467a33142425 repoview: move function for computing filtered hash
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24693
diff changeset
   330
        return None
467a33142425 repoview: move function for computing filtered hash
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24693
diff changeset
   331
    key = None
467a33142425 repoview: move function for computing filtered hash
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24693
diff changeset
   332
    revs = sorted(r for r in cl.filteredrevs if r <= maxrev)
467a33142425 repoview: move function for computing filtered hash
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24693
diff changeset
   333
    if revs:
29341
0d83ad967bf8 cleanup: replace uses of util.(md5|sha1|sha256|sha512) with hashlib.\1
Augie Fackler <raf@durin42.com>
parents: 29336
diff changeset
   334
        s = hashlib.sha1()
24723
467a33142425 repoview: move function for computing filtered hash
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24693
diff changeset
   335
        for rev in revs:
467a33142425 repoview: move function for computing filtered hash
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24693
diff changeset
   336
            s.update('%s;' % rev)
467a33142425 repoview: move function for computing filtered hash
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24693
diff changeset
   337
        key = s.digest()
467a33142425 repoview: move function for computing filtered hash
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24693
diff changeset
   338
    return key
467a33142425 repoview: move function for computing filtered hash
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24693
diff changeset
   339
17649
f65c6a5f256c scmutil: rename classes from "opener" to "vfs"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 17561
diff changeset
   340
class abstractvfs(object):
14089
d3f7e110c3c0 opener: introduce an abstact superclass of it
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 14068
diff changeset
   341
    """Abstract base class; cannot be instantiated"""
d3f7e110c3c0 opener: introduce an abstact superclass of it
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 14068
diff changeset
   342
d3f7e110c3c0 opener: introduce an abstact superclass of it
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 14068
diff changeset
   343
    def __init__(self, *args, **kwargs):
d3f7e110c3c0 opener: introduce an abstact superclass of it
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 14068
diff changeset
   344
        '''Prevent instantiation; don't call this from subclasses.'''
d3f7e110c3c0 opener: introduce an abstact superclass of it
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 14068
diff changeset
   345
        raise NotImplementedError('attempted instantiating ' + str(type(self)))
d3f7e110c3c0 opener: introduce an abstact superclass of it
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 14068
diff changeset
   346
16455
154219f3a6a4 opener: introduce tryread helper
Matt Mackall <mpm@selenic.com>
parents: 16436
diff changeset
   347
    def tryread(self, path):
16479
fc04698fa778 opener: coding style, use triple quotes for doc string
Thomas Arendsen Hein <thomas@intevation.de>
parents: 16455
diff changeset
   348
        '''gracefully return an empty string for missing files'''
16455
154219f3a6a4 opener: introduce tryread helper
Matt Mackall <mpm@selenic.com>
parents: 16436
diff changeset
   349
        try:
154219f3a6a4 opener: introduce tryread helper
Matt Mackall <mpm@selenic.com>
parents: 16436
diff changeset
   350
            return self.read(path)
25660
328739ea70c3 global: mass rewrite to use modern exception syntax
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25658
diff changeset
   351
        except IOError as inst:
16455
154219f3a6a4 opener: introduce tryread helper
Matt Mackall <mpm@selenic.com>
parents: 16436
diff changeset
   352
            if inst.errno != errno.ENOENT:
154219f3a6a4 opener: introduce tryread helper
Matt Mackall <mpm@selenic.com>
parents: 16436
diff changeset
   353
                raise
154219f3a6a4 opener: introduce tryread helper
Matt Mackall <mpm@selenic.com>
parents: 16436
diff changeset
   354
        return ""
154219f3a6a4 opener: introduce tryread helper
Matt Mackall <mpm@selenic.com>
parents: 16436
diff changeset
   355
23368
bf8c3172255c vfs: add "readlines" and "tryreadlines"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23259
diff changeset
   356
    def tryreadlines(self, path, mode='rb'):
bf8c3172255c vfs: add "readlines" and "tryreadlines"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23259
diff changeset
   357
        '''gracefully return an empty array for missing files'''
bf8c3172255c vfs: add "readlines" and "tryreadlines"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23259
diff changeset
   358
        try:
bf8c3172255c vfs: add "readlines" and "tryreadlines"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23259
diff changeset
   359
            return self.readlines(path, mode=mode)
25660
328739ea70c3 global: mass rewrite to use modern exception syntax
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25658
diff changeset
   360
        except IOError as inst:
23368
bf8c3172255c vfs: add "readlines" and "tryreadlines"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23259
diff changeset
   361
            if inst.errno != errno.ENOENT:
bf8c3172255c vfs: add "readlines" and "tryreadlines"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23259
diff changeset
   362
                raise
bf8c3172255c vfs: add "readlines" and "tryreadlines"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23259
diff changeset
   363
        return []
bf8c3172255c vfs: add "readlines" and "tryreadlines"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23259
diff changeset
   364
29730
2dd8c225e94c vfs: use propertycache for open
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 29727
diff changeset
   365
    @util.propertycache
2dd8c225e94c vfs: use propertycache for open
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 29727
diff changeset
   366
    def open(self):
23370
46265d0f0c7b vfs: add "notindexed" argument to invoke "ensuredir" with it in write mode
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23368
diff changeset
   367
        '''Open ``path`` file, which is relative to vfs root.
46265d0f0c7b vfs: add "notindexed" argument to invoke "ensuredir" with it in write mode
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23368
diff changeset
   368
46265d0f0c7b vfs: add "notindexed" argument to invoke "ensuredir" with it in write mode
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23368
diff changeset
   369
        Newly created directories are marked as "not to be indexed by
46265d0f0c7b vfs: add "notindexed" argument to invoke "ensuredir" with it in write mode
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23368
diff changeset
   370
        the content indexing service", if ``notindexed`` is specified
46265d0f0c7b vfs: add "notindexed" argument to invoke "ensuredir" with it in write mode
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23368
diff changeset
   371
        for "write" mode access.
46265d0f0c7b vfs: add "notindexed" argument to invoke "ensuredir" with it in write mode
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23368
diff changeset
   372
        '''
29730
2dd8c225e94c vfs: use propertycache for open
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 29727
diff changeset
   373
        return self.__call__
19897
896a4568def7 vfs: add "open()" for newly added code paths which open files via vfs
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 19896
diff changeset
   374
14167
0e4753807c93 util & scmutil: adapt read/write helpers as request by mpm
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 14138
diff changeset
   375
    def read(self, path):
27706
22e362da27cf scmutil: use context managers for file handles
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27651
diff changeset
   376
        with self(path, 'rb') as fp:
14097
ca3376f044f8 opener: add read & write utility methods
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 14090
diff changeset
   377
            return fp.read()
ca3376f044f8 opener: add read & write utility methods
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 14090
diff changeset
   378
23368
bf8c3172255c vfs: add "readlines" and "tryreadlines"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23259
diff changeset
   379
    def readlines(self, path, mode='rb'):
27706
22e362da27cf scmutil: use context managers for file handles
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27651
diff changeset
   380
        with self(path, mode=mode) as fp:
23368
bf8c3172255c vfs: add "readlines" and "tryreadlines"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23259
diff changeset
   381
            return fp.readlines()
bf8c3172255c vfs: add "readlines" and "tryreadlines"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23259
diff changeset
   382
28197
2ada62388bb1 scmutil: support background closing for write()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 28017
diff changeset
   383
    def write(self, path, data, backgroundclose=False):
2ada62388bb1 scmutil: support background closing for write()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 28017
diff changeset
   384
        with self(path, 'wb', backgroundclose=backgroundclose) as fp:
14167
0e4753807c93 util & scmutil: adapt read/write helpers as request by mpm
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 14138
diff changeset
   385
            return fp.write(data)
0e4753807c93 util & scmutil: adapt read/write helpers as request by mpm
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 14138
diff changeset
   386
23371
1df6519eb3ab vfs: add "writelines"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23370
diff changeset
   387
    def writelines(self, path, data, mode='wb', notindexed=False):
27706
22e362da27cf scmutil: use context managers for file handles
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27651
diff changeset
   388
        with self(path, mode=mode, notindexed=notindexed) as fp:
23371
1df6519eb3ab vfs: add "writelines"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23370
diff changeset
   389
            return fp.writelines(data)
1df6519eb3ab vfs: add "writelines"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23370
diff changeset
   390
14167
0e4753807c93 util & scmutil: adapt read/write helpers as request by mpm
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 14138
diff changeset
   391
    def append(self, path, data):
27706
22e362da27cf scmutil: use context managers for file handles
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27651
diff changeset
   392
        with self(path, 'ab') as fp:
14097
ca3376f044f8 opener: add read & write utility methods
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 14090
diff changeset
   393
            return fp.write(data)
ca3376f044f8 opener: add read & write utility methods
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 14090
diff changeset
   394
25770
39de2e9cc6f4 vfs: add basename
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 25739
diff changeset
   395
    def basename(self, path):
39de2e9cc6f4 vfs: add basename
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 25739
diff changeset
   396
        """return base element of a path (as os.path.basename would do)
39de2e9cc6f4 vfs: add basename
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 25739
diff changeset
   397
39de2e9cc6f4 vfs: add basename
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 25739
diff changeset
   398
        This exists to allow handling of strange encoding if needed."""
39de2e9cc6f4 vfs: add basename
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 25739
diff changeset
   399
        return os.path.basename(path)
39de2e9cc6f4 vfs: add basename
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 25739
diff changeset
   400
20086
f3df2612f3c3 vfs: add "chmod()"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20085
diff changeset
   401
    def chmod(self, path, mode):
f3df2612f3c3 vfs: add "chmod()"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20085
diff changeset
   402
        return os.chmod(self.join(path), mode)
f3df2612f3c3 vfs: add "chmod()"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20085
diff changeset
   403
25772
5471965af5cb vfs: add dirname
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 25770
diff changeset
   404
    def dirname(self, path):
5471965af5cb vfs: add dirname
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 25770
diff changeset
   405
        """return dirname element of a path (as os.path.dirname would do)
5471965af5cb vfs: add dirname
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 25770
diff changeset
   406
5471965af5cb vfs: add dirname
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 25770
diff changeset
   407
        This exists to allow handling of strange encoding if needed."""
5471965af5cb vfs: add dirname
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 25770
diff changeset
   408
        return os.path.dirname(path)
5471965af5cb vfs: add dirname
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 25770
diff changeset
   409
17161
be016e96117a localrepo: use file API via vfs while ensuring repository directory
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 17157
diff changeset
   410
    def exists(self, path=None):
be016e96117a localrepo: use file API via vfs while ensuring repository directory
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 17157
diff changeset
   411
        return os.path.exists(self.join(path))
be016e96117a localrepo: use file API via vfs while ensuring repository directory
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 17157
diff changeset
   412
19899
8c3dcbbfb5de changelog: use "vfs.fstat()" instead of "util.fstat()"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 19897
diff changeset
   413
    def fstat(self, fp):
8c3dcbbfb5de changelog: use "vfs.fstat()" instead of "util.fstat()"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 19897
diff changeset
   414
        return util.fstat(fp)
8c3dcbbfb5de changelog: use "vfs.fstat()" instead of "util.fstat()"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 19897
diff changeset
   415
17161
be016e96117a localrepo: use file API via vfs while ensuring repository directory
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 17157
diff changeset
   416
    def isdir(self, path=None):
be016e96117a localrepo: use file API via vfs while ensuring repository directory
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 17157
diff changeset
   417
        return os.path.isdir(self.join(path))
be016e96117a localrepo: use file API via vfs while ensuring repository directory
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 17157
diff changeset
   418
20085
589d6bb5b18d vfs: add "isfile()"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20045
diff changeset
   419
    def isfile(self, path=None):
589d6bb5b18d vfs: add "isfile()"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20045
diff changeset
   420
        return os.path.isfile(self.join(path))
589d6bb5b18d vfs: add "isfile()"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20045
diff changeset
   421
18949
138978f20180 localrepo: use "vfs.islink()" instead of "os.path.islink()"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 18948
diff changeset
   422
    def islink(self, path=None):
138978f20180 localrepo: use "vfs.islink()" instead of "os.path.islink()"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 18948
diff changeset
   423
        return os.path.islink(self.join(path))
138978f20180 localrepo: use "vfs.islink()" instead of "os.path.islink()"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 18948
diff changeset
   424
27571
6a6e78f84cc6 merge: while checking for unknown files don't follow symlinks (issue5027)
Siddharth Agarwal <sid0@fb.com>
parents: 26836
diff changeset
   425
    def isfileorlink(self, path=None):
6a6e78f84cc6 merge: while checking for unknown files don't follow symlinks (issue5027)
Siddharth Agarwal <sid0@fb.com>
parents: 26836
diff changeset
   426
        '''return whether path is a regular file or a symlink
6a6e78f84cc6 merge: while checking for unknown files don't follow symlinks (issue5027)
Siddharth Agarwal <sid0@fb.com>
parents: 26836
diff changeset
   427
6a6e78f84cc6 merge: while checking for unknown files don't follow symlinks (issue5027)
Siddharth Agarwal <sid0@fb.com>
parents: 26836
diff changeset
   428
        Unlike isfile, this doesn't follow symlinks.'''
6a6e78f84cc6 merge: while checking for unknown files don't follow symlinks (issue5027)
Siddharth Agarwal <sid0@fb.com>
parents: 26836
diff changeset
   429
        try:
6a6e78f84cc6 merge: while checking for unknown files don't follow symlinks (issue5027)
Siddharth Agarwal <sid0@fb.com>
parents: 26836
diff changeset
   430
            st = self.lstat(path)
6a6e78f84cc6 merge: while checking for unknown files don't follow symlinks (issue5027)
Siddharth Agarwal <sid0@fb.com>
parents: 26836
diff changeset
   431
        except OSError:
6a6e78f84cc6 merge: while checking for unknown files don't follow symlinks (issue5027)
Siddharth Agarwal <sid0@fb.com>
parents: 26836
diff changeset
   432
            return False
6a6e78f84cc6 merge: while checking for unknown files don't follow symlinks (issue5027)
Siddharth Agarwal <sid0@fb.com>
parents: 26836
diff changeset
   433
        mode = st.st_mode
6a6e78f84cc6 merge: while checking for unknown files don't follow symlinks (issue5027)
Siddharth Agarwal <sid0@fb.com>
parents: 26836
diff changeset
   434
        return stat.S_ISREG(mode) or stat.S_ISLNK(mode)
6a6e78f84cc6 merge: while checking for unknown files don't follow symlinks (issue5027)
Siddharth Agarwal <sid0@fb.com>
parents: 26836
diff changeset
   435
23581
aed981c7bebf vfs: add a 'reljoin' function for joining relative paths
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23539
diff changeset
   436
    def reljoin(self, *paths):
aed981c7bebf vfs: add a 'reljoin' function for joining relative paths
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23539
diff changeset
   437
        """join various elements of a path together (as os.path.join would do)
aed981c7bebf vfs: add a 'reljoin' function for joining relative paths
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23539
diff changeset
   438
aed981c7bebf vfs: add a 'reljoin' function for joining relative paths
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23539
diff changeset
   439
        The vfs base is not injected so that path stay relative. This exists
aed981c7bebf vfs: add a 'reljoin' function for joining relative paths
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23539
diff changeset
   440
        to allow handling of strange encoding if needed."""
aed981c7bebf vfs: add a 'reljoin' function for joining relative paths
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23539
diff changeset
   441
        return os.path.join(*paths)
aed981c7bebf vfs: add a 'reljoin' function for joining relative paths
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23539
diff changeset
   442
23582
7559dc8c4238 vfs: add a 'split' method
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23581
diff changeset
   443
    def split(self, path):
7559dc8c4238 vfs: add a 'split' method
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23581
diff changeset
   444
        """split top-most element of a path (as os.path.split would do)
7559dc8c4238 vfs: add a 'split' method
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23581
diff changeset
   445
7559dc8c4238 vfs: add a 'split' method
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23581
diff changeset
   446
        This exists to allow handling of strange encoding if needed."""
7559dc8c4238 vfs: add a 'split' method
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23581
diff changeset
   447
        return os.path.split(path)
7559dc8c4238 vfs: add a 'split' method
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 23581
diff changeset
   448
21563
764b691b8bda vfs: add lexists() in current api
Chinmay Joshi <c@chinmayjoshi.com>
parents: 21111
diff changeset
   449
    def lexists(self, path=None):
764b691b8bda vfs: add lexists() in current api
Chinmay Joshi <c@chinmayjoshi.com>
parents: 21111
diff changeset
   450
        return os.path.lexists(self.join(path))
764b691b8bda vfs: add lexists() in current api
Chinmay Joshi <c@chinmayjoshi.com>
parents: 21111
diff changeset
   451
19900
7c21e3398931 context: use "vfs.lstat()" instead of "os.lstat()"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 19899
diff changeset
   452
    def lstat(self, path=None):
7c21e3398931 context: use "vfs.lstat()" instead of "os.lstat()"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 19899
diff changeset
   453
        return os.lstat(self.join(path))
7c21e3398931 context: use "vfs.lstat()" instead of "os.lstat()"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 19899
diff changeset
   454
21799
dfacdd6a111e vfs: add listdir for os.listdir in vfs
Chinmay Joshi <c@chinmayjoshi.com>
parents: 21716
diff changeset
   455
    def listdir(self, path=None):
dfacdd6a111e vfs: add listdir for os.listdir in vfs
Chinmay Joshi <c@chinmayjoshi.com>
parents: 21716
diff changeset
   456
        return os.listdir(self.join(path))
dfacdd6a111e vfs: add listdir for os.listdir in vfs
Chinmay Joshi <c@chinmayjoshi.com>
parents: 21716
diff changeset
   457
17161
be016e96117a localrepo: use file API via vfs while ensuring repository directory
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 17157
diff changeset
   458
    def makedir(self, path=None, notindexed=True):
be016e96117a localrepo: use file API via vfs while ensuring repository directory
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 17157
diff changeset
   459
        return util.makedir(self.join(path), notindexed)
be016e96117a localrepo: use file API via vfs while ensuring repository directory
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 17157
diff changeset
   460
be016e96117a localrepo: use file API via vfs while ensuring repository directory
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 17157
diff changeset
   461
    def makedirs(self, path=None, mode=None):
be016e96117a localrepo: use file API via vfs while ensuring repository directory
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 17157
diff changeset
   462
        return util.makedirs(self.join(path), mode)
be016e96117a localrepo: use file API via vfs while ensuring repository directory
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 17157
diff changeset
   463
20090
88d8e568add1 vfs: add "makelock()" and "readlock()"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20086
diff changeset
   464
    def makelock(self, info, path):
88d8e568add1 vfs: add "makelock()" and "readlock()"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20086
diff changeset
   465
        return util.makelock(info, self.join(path))
88d8e568add1 vfs: add "makelock()" and "readlock()"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20086
diff changeset
   466
17723
ab23768746fd scmutil: reorder newly added functions for vfs support in dictionary order
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 17681
diff changeset
   467
    def mkdir(self, path=None):
ab23768746fd scmutil: reorder newly added functions for vfs support in dictionary order
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 17681
diff changeset
   468
        return os.mkdir(self.join(path))
ab23768746fd scmutil: reorder newly added functions for vfs support in dictionary order
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 17681
diff changeset
   469
20980
6fb4c94ae300 vfs: add "mkstemp()"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20862
diff changeset
   470
    def mkstemp(self, suffix='', prefix='tmp', dir=None, text=False):
6fb4c94ae300 vfs: add "mkstemp()"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20862
diff changeset
   471
        fd, name = tempfile.mkstemp(suffix=suffix, prefix=prefix,
6fb4c94ae300 vfs: add "mkstemp()"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20862
diff changeset
   472
                                    dir=self.join(dir), text=text)
6fb4c94ae300 vfs: add "mkstemp()"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20862
diff changeset
   473
        dname, fname = util.split(name)
6fb4c94ae300 vfs: add "mkstemp()"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20862
diff changeset
   474
        if dir:
6fb4c94ae300 vfs: add "mkstemp()"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20862
diff changeset
   475
            return fd, os.path.join(dir, fname)
6fb4c94ae300 vfs: add "mkstemp()"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20862
diff changeset
   476
        else:
6fb4c94ae300 vfs: add "mkstemp()"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20862
diff changeset
   477
            return fd, fname
6fb4c94ae300 vfs: add "mkstemp()"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20862
diff changeset
   478
17747
aad3bce98f76 store: invoke "osutil.listdir()" via vfs
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 17726
diff changeset
   479
    def readdir(self, path=None, stat=None, skip=None):
aad3bce98f76 store: invoke "osutil.listdir()" via vfs
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 17726
diff changeset
   480
        return osutil.listdir(self.join(path), stat, skip)
aad3bce98f76 store: invoke "osutil.listdir()" via vfs
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 17726
diff changeset
   481
20090
88d8e568add1 vfs: add "makelock()" and "readlock()"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20086
diff changeset
   482
    def readlock(self, path):
88d8e568add1 vfs: add "makelock()" and "readlock()"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20086
diff changeset
   483
        return util.readlock(self.join(path))
88d8e568add1 vfs: add "makelock()" and "readlock()"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20086
diff changeset
   484
29203
731ced087a4b vfs: make rename avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29202
diff changeset
   485
    def rename(self, src, dst, checkambig=False):
29367
4e6e280e238f doc: describe detail about checkambig optional argument
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29341
diff changeset
   486
        """Rename from src to dst
4e6e280e238f doc: describe detail about checkambig optional argument
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29341
diff changeset
   487
4e6e280e238f doc: describe detail about checkambig optional argument
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29341
diff changeset
   488
        checkambig argument is used with util.filestat, and is useful
4e6e280e238f doc: describe detail about checkambig optional argument
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29341
diff changeset
   489
        only if destination file is guarded by any lock
4e6e280e238f doc: describe detail about checkambig optional argument
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29341
diff changeset
   490
        (e.g. repo.lock or repo.wlock).
4e6e280e238f doc: describe detail about checkambig optional argument
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29341
diff changeset
   491
        """
29203
731ced087a4b vfs: make rename avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29202
diff changeset
   492
        dstpath = self.join(dst)
731ced087a4b vfs: make rename avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29202
diff changeset
   493
        oldstat = checkambig and util.filestat(dstpath)
731ced087a4b vfs: make rename avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29202
diff changeset
   494
        if oldstat and oldstat.stat:
731ced087a4b vfs: make rename avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29202
diff changeset
   495
            ret = util.rename(self.join(src), dstpath)
731ced087a4b vfs: make rename avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29202
diff changeset
   496
            newstat = util.filestat(dstpath)
731ced087a4b vfs: make rename avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29202
diff changeset
   497
            if newstat.isambig(oldstat):
731ced087a4b vfs: make rename avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29202
diff changeset
   498
                # stat of renamed file is ambiguous to original one
30254
bff5ccbe5ead vfs: ignore EPERM at os.utime, which avoids ambiguity at renaming (issue5418)
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 30109
diff changeset
   499
                newstat.avoidambig(dstpath, oldstat)
29203
731ced087a4b vfs: make rename avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29202
diff changeset
   500
            return ret
731ced087a4b vfs: make rename avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29202
diff changeset
   501
        return util.rename(self.join(src), dstpath)
18948
2f05fa162316 localrepo: use "vfs.rename()" instead of "util.rename()"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 18945
diff changeset
   502
18950
647e3b0c8751 localrepo: use "vfs.readlink()" instead of "os.readlink()"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 18949
diff changeset
   503
    def readlink(self, path):
647e3b0c8751 localrepo: use "vfs.readlink()" instead of "os.readlink()"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 18949
diff changeset
   504
        return os.readlink(self.join(path))
647e3b0c8751 localrepo: use "vfs.readlink()" instead of "os.readlink()"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 18949
diff changeset
   505
24693
0d28b0df77ea vfs: add removedirs
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24689
diff changeset
   506
    def removedirs(self, path=None):
0d28b0df77ea vfs: add removedirs
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24689
diff changeset
   507
        """Remove a leaf directory and all empty intermediate ones
0d28b0df77ea vfs: add removedirs
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24689
diff changeset
   508
        """
0d28b0df77ea vfs: add removedirs
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24689
diff changeset
   509
        return util.removedirs(self.join(path))
0d28b0df77ea vfs: add removedirs
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24689
diff changeset
   510
24689
ca3a90096c95 vfs: add rmtree
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24635
diff changeset
   511
    def rmtree(self, path=None, ignore_errors=False, forcibly=False):
ca3a90096c95 vfs: add rmtree
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24635
diff changeset
   512
        """Remove a directory tree recursively
ca3a90096c95 vfs: add rmtree
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24635
diff changeset
   513
ca3a90096c95 vfs: add rmtree
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24635
diff changeset
   514
        If ``forcibly``, this tries to remove READ-ONLY files, too.
ca3a90096c95 vfs: add rmtree
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24635
diff changeset
   515
        """
ca3a90096c95 vfs: add rmtree
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24635
diff changeset
   516
        if forcibly:
ca3a90096c95 vfs: add rmtree
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24635
diff changeset
   517
            def onerror(function, path, excinfo):
ca3a90096c95 vfs: add rmtree
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24635
diff changeset
   518
                if function is not os.remove:
ca3a90096c95 vfs: add rmtree
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24635
diff changeset
   519
                    raise
ca3a90096c95 vfs: add rmtree
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24635
diff changeset
   520
                # read-only files cannot be unlinked under Windows
ca3a90096c95 vfs: add rmtree
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24635
diff changeset
   521
                s = os.stat(path)
ca3a90096c95 vfs: add rmtree
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24635
diff changeset
   522
                if (s.st_mode & stat.S_IWRITE) != 0:
ca3a90096c95 vfs: add rmtree
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24635
diff changeset
   523
                    raise
ca3a90096c95 vfs: add rmtree
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24635
diff changeset
   524
                os.chmod(path, stat.S_IMODE(s.st_mode) | stat.S_IWRITE)
ca3a90096c95 vfs: add rmtree
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24635
diff changeset
   525
                os.remove(path)
ca3a90096c95 vfs: add rmtree
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24635
diff changeset
   526
        else:
ca3a90096c95 vfs: add rmtree
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24635
diff changeset
   527
            onerror = None
ca3a90096c95 vfs: add rmtree
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24635
diff changeset
   528
        return shutil.rmtree(self.join(path),
ca3a90096c95 vfs: add rmtree
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24635
diff changeset
   529
                             ignore_errors=ignore_errors, onerror=onerror)
ca3a90096c95 vfs: add rmtree
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24635
diff changeset
   530
18951
d13916a00b7e localrepo: use "vfs.setflags()" instead of "util.setflags()"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 18950
diff changeset
   531
    def setflags(self, path, l, x):
d13916a00b7e localrepo: use "vfs.setflags()" instead of "util.setflags()"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 18950
diff changeset
   532
        return util.setflags(self.join(path), l, x)
d13916a00b7e localrepo: use "vfs.setflags()" instead of "util.setflags()"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 18950
diff changeset
   533
17726
7cb7e17c23b2 store: invoke "os.stat()" for "createmode" initialization via vfs
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 17725
diff changeset
   534
    def stat(self, path=None):
7cb7e17c23b2 store: invoke "os.stat()" for "createmode" initialization via vfs
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 17725
diff changeset
   535
        return os.stat(self.join(path))
7cb7e17c23b2 store: invoke "os.stat()" for "createmode" initialization via vfs
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 17725
diff changeset
   536
19895
37c0d93fb166 bookmarks: use "vfs.unlink()" instead of "util.unlink()"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 19655
diff changeset
   537
    def unlink(self, path=None):
37c0d93fb166 bookmarks: use "vfs.unlink()" instead of "util.unlink()"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 19655
diff changeset
   538
        return util.unlink(self.join(path))
37c0d93fb166 bookmarks: use "vfs.unlink()" instead of "util.unlink()"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 19655
diff changeset
   539
21716
90f9be5adade vfs: add unlinkpath to vfs
Chinmay Joshi <c@chinmayjoshi.com>
parents: 21563
diff changeset
   540
    def unlinkpath(self, path=None, ignoremissing=False):
90f9be5adade vfs: add unlinkpath to vfs
Chinmay Joshi <c@chinmayjoshi.com>
parents: 21563
diff changeset
   541
        return util.unlinkpath(self.join(path), ignoremissing)
90f9be5adade vfs: add unlinkpath to vfs
Chinmay Joshi <c@chinmayjoshi.com>
parents: 21563
diff changeset
   542
19896
af03279c766a bookmarks: use "vfs.utime()" instead of "os.utime()"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 19895
diff changeset
   543
    def utime(self, path=None, t=None):
af03279c766a bookmarks: use "vfs.utime()" instead of "os.utime()"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 19895
diff changeset
   544
        return os.utime(self.join(path), t)
af03279c766a bookmarks: use "vfs.utime()" instead of "os.utime()"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 19895
diff changeset
   545
24725
ee751d47cf2c vfs: add walk
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24723
diff changeset
   546
    def walk(self, path=None, onerror=None):
ee751d47cf2c vfs: add walk
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24723
diff changeset
   547
        """Yield (dirpath, dirs, files) tuple for each directories under path
ee751d47cf2c vfs: add walk
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24723
diff changeset
   548
ee751d47cf2c vfs: add walk
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24723
diff changeset
   549
        ``dirpath`` is relative one from the root of this vfs. This
ee751d47cf2c vfs: add walk
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24723
diff changeset
   550
        uses ``os.sep`` as path separator, even you specify POSIX
ee751d47cf2c vfs: add walk
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24723
diff changeset
   551
        style ``path``.
ee751d47cf2c vfs: add walk
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24723
diff changeset
   552
ee751d47cf2c vfs: add walk
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24723
diff changeset
   553
        "The root of this vfs" is represented as empty ``dirpath``.
ee751d47cf2c vfs: add walk
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24723
diff changeset
   554
        """
ee751d47cf2c vfs: add walk
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24723
diff changeset
   555
        root = os.path.normpath(self.join(None))
ee751d47cf2c vfs: add walk
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24723
diff changeset
   556
        # when dirpath == root, dirpath[prefixlen:] becomes empty
ee751d47cf2c vfs: add walk
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24723
diff changeset
   557
        # because len(dirpath) < prefixlen.
ee751d47cf2c vfs: add walk
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24723
diff changeset
   558
        prefixlen = len(pathutil.normasprefix(root))
ee751d47cf2c vfs: add walk
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24723
diff changeset
   559
        for dirpath, dirs, files in os.walk(self.join(path), onerror=onerror):
ee751d47cf2c vfs: add walk
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24723
diff changeset
   560
            yield (dirpath[prefixlen:], dirs, files)
ee751d47cf2c vfs: add walk
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24723
diff changeset
   561
27895
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
   562
    @contextlib.contextmanager
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
   563
    def backgroundclosing(self, ui, expectedcount=-1):
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
   564
        """Allow files to be closed asynchronously.
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
   565
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
   566
        When this context manager is active, ``backgroundclose`` can be passed
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
   567
        to ``__call__``/``open`` to result in the file possibly being closed
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
   568
        asynchronously, on a background thread.
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
   569
        """
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
   570
        # This is an arbitrary restriction and could be changed if we ever
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
   571
        # have a use case.
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
   572
        vfs = getattr(self, 'vfs', self)
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
   573
        if getattr(vfs, '_backgroundfilecloser', None):
29389
98e8313dcd9e i18n: translate abort messages
liscju <piotr.listkiewicz@gmail.com>
parents: 29373
diff changeset
   574
            raise error.Abort(
98e8313dcd9e i18n: translate abort messages
liscju <piotr.listkiewicz@gmail.com>
parents: 29373
diff changeset
   575
                _('can only have 1 active background file closer'))
27895
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
   576
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
   577
        with backgroundfilecloser(ui, expectedcount=expectedcount) as bfc:
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
   578
            try:
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
   579
                vfs._backgroundfilecloser = bfc
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
   580
                yield bfc
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
   581
            finally:
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
   582
                vfs._backgroundfilecloser = None
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
   583
17649
f65c6a5f256c scmutil: rename classes from "opener" to "vfs"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 17561
diff changeset
   584
class vfs(abstractvfs):
f65c6a5f256c scmutil: rename classes from "opener" to "vfs"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 17561
diff changeset
   585
    '''Operate files relative to a base directory
13970
d13913355390 move opener from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13962
diff changeset
   586
d13913355390 move opener from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13962
diff changeset
   587
    This class is used to hide the details of COW semantics and
d13913355390 move opener from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13962
diff changeset
   588
    remote file access from higher level code.
d13913355390 move opener from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13962
diff changeset
   589
    '''
18945
e75b72fffdfe vfs: split "expand" into "realpath"/"expandpath" to apply each separately
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 18900
diff changeset
   590
    def __init__(self, base, audit=True, expandpath=False, realpath=False):
e75b72fffdfe vfs: split "expand" into "realpath"/"expandpath" to apply each separately
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 18900
diff changeset
   591
        if expandpath:
e75b72fffdfe vfs: split "expand" into "realpath"/"expandpath" to apply each separately
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 18900
diff changeset
   592
            base = util.expandpath(base)
e75b72fffdfe vfs: split "expand" into "realpath"/"expandpath" to apply each separately
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 18900
diff changeset
   593
        if realpath:
e75b72fffdfe vfs: split "expand" into "realpath"/"expandpath" to apply each separately
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 18900
diff changeset
   594
            base = os.path.realpath(base)
13970
d13913355390 move opener from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13962
diff changeset
   595
        self.base = base
27879
52a4ad62b006 cleanup: use modern @property/@foo.setter property specification
Augie Fackler <augie@google.com>
parents: 27851
diff changeset
   596
        self.mustaudit = audit
17554
5450c8ad9d98 scmutil: turn opener._audit into a property, mustaudit
Bryan O'Sullivan <bryano@fb.com>
parents: 17248
diff changeset
   597
        self.createmode = None
5450c8ad9d98 scmutil: turn opener._audit into a property, mustaudit
Bryan O'Sullivan <bryano@fb.com>
parents: 17248
diff changeset
   598
        self._trustnlink = None
5450c8ad9d98 scmutil: turn opener._audit into a property, mustaudit
Bryan O'Sullivan <bryano@fb.com>
parents: 17248
diff changeset
   599
27879
52a4ad62b006 cleanup: use modern @property/@foo.setter property specification
Augie Fackler <augie@google.com>
parents: 27851
diff changeset
   600
    @property
52a4ad62b006 cleanup: use modern @property/@foo.setter property specification
Augie Fackler <augie@google.com>
parents: 27851
diff changeset
   601
    def mustaudit(self):
17554
5450c8ad9d98 scmutil: turn opener._audit into a property, mustaudit
Bryan O'Sullivan <bryano@fb.com>
parents: 17248
diff changeset
   602
        return self._audit
5450c8ad9d98 scmutil: turn opener._audit into a property, mustaudit
Bryan O'Sullivan <bryano@fb.com>
parents: 17248
diff changeset
   603
27879
52a4ad62b006 cleanup: use modern @property/@foo.setter property specification
Augie Fackler <augie@google.com>
parents: 27851
diff changeset
   604
    @mustaudit.setter
52a4ad62b006 cleanup: use modern @property/@foo.setter property specification
Augie Fackler <augie@google.com>
parents: 27851
diff changeset
   605
    def mustaudit(self, onoff):
17554
5450c8ad9d98 scmutil: turn opener._audit into a property, mustaudit
Bryan O'Sullivan <bryano@fb.com>
parents: 17248
diff changeset
   606
        self._audit = onoff
5450c8ad9d98 scmutil: turn opener._audit into a property, mustaudit
Bryan O'Sullivan <bryano@fb.com>
parents: 17248
diff changeset
   607
        if onoff:
20033
f962870712da pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents: 20006
diff changeset
   608
            self.audit = pathutil.pathauditor(self.base)
13970
d13913355390 move opener from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13962
diff changeset
   609
        else:
18327
4aecdb91443c scmutil: simplify vfs.audit - drop wrapped vfs.auditor
Mads Kiilerich <mads@kiilerich.com>
parents: 18316
diff changeset
   610
            self.audit = util.always
17554
5450c8ad9d98 scmutil: turn opener._audit into a property, mustaudit
Bryan O'Sullivan <bryano@fb.com>
parents: 17248
diff changeset
   611
13970
d13913355390 move opener from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13962
diff changeset
   612
    @util.propertycache
14261
e3649bcca3f6 opener: rename _can_symlink to _cansymlink
Adrian Buehlmann <adrian@cadifra.com>
parents: 14236
diff changeset
   613
    def _cansymlink(self):
13970
d13913355390 move opener from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13962
diff changeset
   614
        return util.checklink(self.base)
d13913355390 move opener from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13962
diff changeset
   615
18192
f9a89bdd64a6 scmutil: don't try to match modes on filesystems without modes (issue3740)
Matt Mackall <mpm@selenic.com>
parents: 17850
diff changeset
   616
    @util.propertycache
f9a89bdd64a6 scmutil: don't try to match modes on filesystems without modes (issue3740)
Matt Mackall <mpm@selenic.com>
parents: 17850
diff changeset
   617
    def _chmod(self):
f9a89bdd64a6 scmutil: don't try to match modes on filesystems without modes (issue3740)
Matt Mackall <mpm@selenic.com>
parents: 17850
diff changeset
   618
        return util.checkexec(self.base)
f9a89bdd64a6 scmutil: don't try to match modes on filesystems without modes (issue3740)
Matt Mackall <mpm@selenic.com>
parents: 17850
diff changeset
   619
17763
13070de77c86 vfs: backout fchmod change from 76b73ce0ffac
Matt Mackall <mpm@selenic.com>
parents: 17752
diff changeset
   620
    def _fixfilemode(self, name):
18192
f9a89bdd64a6 scmutil: don't try to match modes on filesystems without modes (issue3740)
Matt Mackall <mpm@selenic.com>
parents: 17850
diff changeset
   621
        if self.createmode is None or not self._chmod:
13970
d13913355390 move opener from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13962
diff changeset
   622
            return
25658
e93036747902 global: mass rewrite to use modern octal syntax
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25629
diff changeset
   623
        os.chmod(name, self.createmode & 0o666)
13970
d13913355390 move opener from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13962
diff changeset
   624
23370
46265d0f0c7b vfs: add "notindexed" argument to invoke "ensuredir" with it in write mode
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23368
diff changeset
   625
    def __call__(self, path, mode="r", text=False, atomictemp=False,
29202
76f1ea360c7e vfs: make atomictempfile avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29119
diff changeset
   626
                 notindexed=False, backgroundclose=False, checkambig=False):
23370
46265d0f0c7b vfs: add "notindexed" argument to invoke "ensuredir" with it in write mode
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23368
diff changeset
   627
        '''Open ``path`` file, which is relative to vfs root.
46265d0f0c7b vfs: add "notindexed" argument to invoke "ensuredir" with it in write mode
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23368
diff changeset
   628
46265d0f0c7b vfs: add "notindexed" argument to invoke "ensuredir" with it in write mode
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23368
diff changeset
   629
        Newly created directories are marked as "not to be indexed by
46265d0f0c7b vfs: add "notindexed" argument to invoke "ensuredir" with it in write mode
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23368
diff changeset
   630
        the content indexing service", if ``notindexed`` is specified
46265d0f0c7b vfs: add "notindexed" argument to invoke "ensuredir" with it in write mode
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23368
diff changeset
   631
        for "write" mode access.
27895
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
   632
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
   633
        If ``backgroundclose`` is passed, the file may be closed asynchronously.
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
   634
        It can only be used if the ``self.backgroundclosing()`` context manager
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
   635
        is active. This should only be specified if the following criteria hold:
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
   636
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
   637
        1. There is a potential for writing thousands of files. Unless you
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
   638
           are writing thousands of files, the performance benefits of
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
   639
           asynchronously closing files is not realized.
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
   640
        2. Files are opened exactly once for the ``backgroundclosing``
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
   641
           active duration and are therefore free of race conditions between
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
   642
           closing a file on a background thread and reopening it. (If the
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
   643
           file were opened multiple times, there could be unflushed data
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
   644
           because the original file handle hasn't been flushed/closed yet.)
29202
76f1ea360c7e vfs: make atomictempfile avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29119
diff changeset
   645
29367
4e6e280e238f doc: describe detail about checkambig optional argument
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29341
diff changeset
   646
        ``checkambig`` argument is passed to atomictemplfile (valid
4e6e280e238f doc: describe detail about checkambig optional argument
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29341
diff changeset
   647
        only for writing), and is useful only if target file is
4e6e280e238f doc: describe detail about checkambig optional argument
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29341
diff changeset
   648
        guarded by any lock (e.g. repo.lock or repo.wlock).
23370
46265d0f0c7b vfs: add "notindexed" argument to invoke "ensuredir" with it in write mode
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23368
diff changeset
   649
        '''
14720
36283a7b6856 opener: add self._audit (issue2862)
Adrian Buehlmann <adrian@cadifra.com>
parents: 14672
diff changeset
   650
        if self._audit:
36283a7b6856 opener: add self._audit (issue2862)
Adrian Buehlmann <adrian@cadifra.com>
parents: 14672
diff changeset
   651
            r = util.checkosfilename(path)
36283a7b6856 opener: add self._audit (issue2862)
Adrian Buehlmann <adrian@cadifra.com>
parents: 14672
diff changeset
   652
            if r:
26587
56b2bcea2529 error: get Abort from 'error' instead of 'util'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26491
diff changeset
   653
                raise error.Abort("%s: %r" % (r, path))
18327
4aecdb91443c scmutil: simplify vfs.audit - drop wrapped vfs.auditor
Mads Kiilerich <mads@kiilerich.com>
parents: 18316
diff changeset
   654
        self.audit(path)
16199
8181bd808dc5 scmutil: add join method to opener to construct path relative to base
Idan Kamara <idankk86@gmail.com>
parents: 16198
diff changeset
   655
        f = self.join(path)
13970
d13913355390 move opener from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13962
diff changeset
   656
d13913355390 move opener from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13962
diff changeset
   657
        if not text and "b" not in mode:
d13913355390 move opener from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13962
diff changeset
   658
            mode += "b" # for that other OS
d13913355390 move opener from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13962
diff changeset
   659
d13913355390 move opener from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13962
diff changeset
   660
        nlink = -1
17937
3cb032d50447 vfs: optimize __call__ by not calling util.split for reads
Adrian Buehlmann <adrian@cadifra.com>
parents: 17850
diff changeset
   661
        if mode not in ('r', 'rb'):
3cb032d50447 vfs: optimize __call__ by not calling util.split for reads
Adrian Buehlmann <adrian@cadifra.com>
parents: 17850
diff changeset
   662
            dirname, basename = util.split(f)
3cb032d50447 vfs: optimize __call__ by not calling util.split for reads
Adrian Buehlmann <adrian@cadifra.com>
parents: 17850
diff changeset
   663
            # If basename is empty, then the path is malformed because it points
3cb032d50447 vfs: optimize __call__ by not calling util.split for reads
Adrian Buehlmann <adrian@cadifra.com>
parents: 17850
diff changeset
   664
            # to a directory. Let the posixfile() call below raise IOError.
3cb032d50447 vfs: optimize __call__ by not calling util.split for reads
Adrian Buehlmann <adrian@cadifra.com>
parents: 17850
diff changeset
   665
            if basename:
3cb032d50447 vfs: optimize __call__ by not calling util.split for reads
Adrian Buehlmann <adrian@cadifra.com>
parents: 17850
diff changeset
   666
                if atomictemp:
29017
07be86828e79 util: fix race in makedirs()
Adam Simpkins <simpkins@fb.com>
parents: 28819
diff changeset
   667
                    util.makedirs(dirname, self.createmode, notindexed)
29202
76f1ea360c7e vfs: make atomictempfile avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29119
diff changeset
   668
                    return util.atomictempfile(f, mode, self.createmode,
76f1ea360c7e vfs: make atomictempfile avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29119
diff changeset
   669
                                               checkambig=checkambig)
17937
3cb032d50447 vfs: optimize __call__ by not calling util.split for reads
Adrian Buehlmann <adrian@cadifra.com>
parents: 17850
diff changeset
   670
                try:
3cb032d50447 vfs: optimize __call__ by not calling util.split for reads
Adrian Buehlmann <adrian@cadifra.com>
parents: 17850
diff changeset
   671
                    if 'w' in mode:
3cb032d50447 vfs: optimize __call__ by not calling util.split for reads
Adrian Buehlmann <adrian@cadifra.com>
parents: 17850
diff changeset
   672
                        util.unlink(f)
3cb032d50447 vfs: optimize __call__ by not calling util.split for reads
Adrian Buehlmann <adrian@cadifra.com>
parents: 17850
diff changeset
   673
                        nlink = 0
3cb032d50447 vfs: optimize __call__ by not calling util.split for reads
Adrian Buehlmann <adrian@cadifra.com>
parents: 17850
diff changeset
   674
                    else:
3cb032d50447 vfs: optimize __call__ by not calling util.split for reads
Adrian Buehlmann <adrian@cadifra.com>
parents: 17850
diff changeset
   675
                        # nlinks() may behave differently for files on Windows
3cb032d50447 vfs: optimize __call__ by not calling util.split for reads
Adrian Buehlmann <adrian@cadifra.com>
parents: 17850
diff changeset
   676
                        # shares if the file is open.
27706
22e362da27cf scmutil: use context managers for file handles
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27651
diff changeset
   677
                        with util.posixfile(f):
22e362da27cf scmutil: use context managers for file handles
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27651
diff changeset
   678
                            nlink = util.nlinks(f)
22e362da27cf scmutil: use context managers for file handles
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27651
diff changeset
   679
                            if nlink < 1:
22e362da27cf scmutil: use context managers for file handles
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27651
diff changeset
   680
                                nlink = 2 # force mktempcopy (issue1922)
25660
328739ea70c3 global: mass rewrite to use modern exception syntax
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25658
diff changeset
   681
                except (OSError, IOError) as e:
17937
3cb032d50447 vfs: optimize __call__ by not calling util.split for reads
Adrian Buehlmann <adrian@cadifra.com>
parents: 17850
diff changeset
   682
                    if e.errno != errno.ENOENT:
3cb032d50447 vfs: optimize __call__ by not calling util.split for reads
Adrian Buehlmann <adrian@cadifra.com>
parents: 17850
diff changeset
   683
                        raise
13970
d13913355390 move opener from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13962
diff changeset
   684
                    nlink = 0
29017
07be86828e79 util: fix race in makedirs()
Adam Simpkins <simpkins@fb.com>
parents: 28819
diff changeset
   685
                    util.makedirs(dirname, self.createmode, notindexed)
17937
3cb032d50447 vfs: optimize __call__ by not calling util.split for reads
Adrian Buehlmann <adrian@cadifra.com>
parents: 17850
diff changeset
   686
                if nlink > 0:
3cb032d50447 vfs: optimize __call__ by not calling util.split for reads
Adrian Buehlmann <adrian@cadifra.com>
parents: 17850
diff changeset
   687
                    if self._trustnlink is None:
3cb032d50447 vfs: optimize __call__ by not calling util.split for reads
Adrian Buehlmann <adrian@cadifra.com>
parents: 17850
diff changeset
   688
                        self._trustnlink = nlink > 1 or util.checknlink(f)
3cb032d50447 vfs: optimize __call__ by not calling util.split for reads
Adrian Buehlmann <adrian@cadifra.com>
parents: 17850
diff changeset
   689
                    if nlink > 1 or not self._trustnlink:
3cb032d50447 vfs: optimize __call__ by not calling util.split for reads
Adrian Buehlmann <adrian@cadifra.com>
parents: 17850
diff changeset
   690
                        util.rename(util.mktempcopy(f), f)
13970
d13913355390 move opener from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13962
diff changeset
   691
        fp = util.posixfile(f, mode)
d13913355390 move opener from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13962
diff changeset
   692
        if nlink == 0:
17763
13070de77c86 vfs: backout fchmod change from 76b73ce0ffac
Matt Mackall <mpm@selenic.com>
parents: 17752
diff changeset
   693
            self._fixfilemode(f)
27895
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
   694
30000
9766d88c2465 vfs: use checkambigatclosing in checkambig=True but atomictemp=False case
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29999
diff changeset
   695
        if checkambig:
9766d88c2465 vfs: use checkambigatclosing in checkambig=True but atomictemp=False case
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29999
diff changeset
   696
            if mode in ('r', 'rb'):
9766d88c2465 vfs: use checkambigatclosing in checkambig=True but atomictemp=False case
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29999
diff changeset
   697
                raise error.Abort(_('implementation error: mode %s is not'
9766d88c2465 vfs: use checkambigatclosing in checkambig=True but atomictemp=False case
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29999
diff changeset
   698
                                    ' valid for checkambig=True') % mode)
9766d88c2465 vfs: use checkambigatclosing in checkambig=True but atomictemp=False case
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29999
diff changeset
   699
            fp = checkambigatclosing(fp)
9766d88c2465 vfs: use checkambigatclosing in checkambig=True but atomictemp=False case
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29999
diff changeset
   700
27895
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
   701
        if backgroundclose:
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
   702
            if not self._backgroundfilecloser:
29389
98e8313dcd9e i18n: translate abort messages
liscju <piotr.listkiewicz@gmail.com>
parents: 29373
diff changeset
   703
                raise error.Abort(_('backgroundclose can only be used when a '
27895
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
   704
                                  'backgroundclosing context manager is active')
29389
98e8313dcd9e i18n: translate abort messages
liscju <piotr.listkiewicz@gmail.com>
parents: 29373
diff changeset
   705
                                  )
27895
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
   706
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
   707
            fp = delayclosedfile(fp, self._backgroundfilecloser)
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
   708
13970
d13913355390 move opener from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13962
diff changeset
   709
        return fp
d13913355390 move opener from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13962
diff changeset
   710
d13913355390 move opener from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13962
diff changeset
   711
    def symlink(self, src, dst):
18327
4aecdb91443c scmutil: simplify vfs.audit - drop wrapped vfs.auditor
Mads Kiilerich <mads@kiilerich.com>
parents: 18316
diff changeset
   712
        self.audit(dst)
16199
8181bd808dc5 scmutil: add join method to opener to construct path relative to base
Idan Kamara <idankk86@gmail.com>
parents: 16198
diff changeset
   713
        linkname = self.join(dst)
13970
d13913355390 move opener from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13962
diff changeset
   714
        try:
d13913355390 move opener from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13962
diff changeset
   715
            os.unlink(linkname)
d13913355390 move opener from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13962
diff changeset
   716
        except OSError:
d13913355390 move opener from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13962
diff changeset
   717
            pass
d13913355390 move opener from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13962
diff changeset
   718
29017
07be86828e79 util: fix race in makedirs()
Adam Simpkins <simpkins@fb.com>
parents: 28819
diff changeset
   719
        util.makedirs(os.path.dirname(linkname), self.createmode)
13970
d13913355390 move opener from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13962
diff changeset
   720
14261
e3649bcca3f6 opener: rename _can_symlink to _cansymlink
Adrian Buehlmann <adrian@cadifra.com>
parents: 14236
diff changeset
   721
        if self._cansymlink:
13970
d13913355390 move opener from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13962
diff changeset
   722
            try:
d13913355390 move opener from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13962
diff changeset
   723
                os.symlink(src, linkname)
25660
328739ea70c3 global: mass rewrite to use modern exception syntax
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25658
diff changeset
   724
            except OSError as err:
13970
d13913355390 move opener from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13962
diff changeset
   725
                raise OSError(err.errno, _('could not symlink to %r: %s') %
d13913355390 move opener from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13962
diff changeset
   726
                              (src, err.strerror), linkname)
d13913355390 move opener from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13962
diff changeset
   727
        else:
17768
9837cafc25b1 vfs: use self.write to write symlink placeholders
Matt Mackall <mpm@selenic.com>
parents: 17763
diff changeset
   728
            self.write(dst, src)
13971
bfeaa88b875d move canonpath from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13970
diff changeset
   729
24628
a0b47885a1c5 vfs: make it possible to pass multiple path elements to join
Matt Harbison <matt_harbison@yahoo.com>
parents: 24582
diff changeset
   730
    def join(self, path, *insidef):
17161
be016e96117a localrepo: use file API via vfs while ensuring repository directory
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 17157
diff changeset
   731
        if path:
24628
a0b47885a1c5 vfs: make it possible to pass multiple path elements to join
Matt Harbison <matt_harbison@yahoo.com>
parents: 24582
diff changeset
   732
            return os.path.join(self.base, path, *insidef)
17681
a41fd730f230 scmutil: backout 83785bb56062 (issue3643)
Matt Mackall <mpm@selenic.com>
parents: 17675
diff changeset
   733
        else:
a41fd730f230 scmutil: backout 83785bb56062 (issue3643)
Matt Mackall <mpm@selenic.com>
parents: 17675
diff changeset
   734
            return self.base
16199
8181bd808dc5 scmutil: add join method to opener to construct path relative to base
Idan Kamara <idankk86@gmail.com>
parents: 16198
diff changeset
   735
17649
f65c6a5f256c scmutil: rename classes from "opener" to "vfs"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 17561
diff changeset
   736
opener = vfs
f65c6a5f256c scmutil: rename classes from "opener" to "vfs"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 17561
diff changeset
   737
17845
408ded42c5ec scmutil: abstract out mustaudit delegation
Bryan O'Sullivan <bryano@fb.com>
parents: 17821
diff changeset
   738
class auditvfs(object):
408ded42c5ec scmutil: abstract out mustaudit delegation
Bryan O'Sullivan <bryano@fb.com>
parents: 17821
diff changeset
   739
    def __init__(self, vfs):
408ded42c5ec scmutil: abstract out mustaudit delegation
Bryan O'Sullivan <bryano@fb.com>
parents: 17821
diff changeset
   740
        self.vfs = vfs
408ded42c5ec scmutil: abstract out mustaudit delegation
Bryan O'Sullivan <bryano@fb.com>
parents: 17821
diff changeset
   741
27879
52a4ad62b006 cleanup: use modern @property/@foo.setter property specification
Augie Fackler <augie@google.com>
parents: 27851
diff changeset
   742
    @property
52a4ad62b006 cleanup: use modern @property/@foo.setter property specification
Augie Fackler <augie@google.com>
parents: 27851
diff changeset
   743
    def mustaudit(self):
17845
408ded42c5ec scmutil: abstract out mustaudit delegation
Bryan O'Sullivan <bryano@fb.com>
parents: 17821
diff changeset
   744
        return self.vfs.mustaudit
408ded42c5ec scmutil: abstract out mustaudit delegation
Bryan O'Sullivan <bryano@fb.com>
parents: 17821
diff changeset
   745
27879
52a4ad62b006 cleanup: use modern @property/@foo.setter property specification
Augie Fackler <augie@google.com>
parents: 27851
diff changeset
   746
    @mustaudit.setter
52a4ad62b006 cleanup: use modern @property/@foo.setter property specification
Augie Fackler <augie@google.com>
parents: 27851
diff changeset
   747
    def mustaudit(self, onoff):
17845
408ded42c5ec scmutil: abstract out mustaudit delegation
Bryan O'Sullivan <bryano@fb.com>
parents: 17821
diff changeset
   748
        self.vfs.mustaudit = onoff
408ded42c5ec scmutil: abstract out mustaudit delegation
Bryan O'Sullivan <bryano@fb.com>
parents: 17821
diff changeset
   749
29727
69109052d9ac auditvfs: forward options property from nested vfs
Augie Fackler <augie@google.com>
parents: 29417
diff changeset
   750
    @property
69109052d9ac auditvfs: forward options property from nested vfs
Augie Fackler <augie@google.com>
parents: 29417
diff changeset
   751
    def options(self):
69109052d9ac auditvfs: forward options property from nested vfs
Augie Fackler <augie@google.com>
parents: 29417
diff changeset
   752
        return self.vfs.options
69109052d9ac auditvfs: forward options property from nested vfs
Augie Fackler <augie@google.com>
parents: 29417
diff changeset
   753
69109052d9ac auditvfs: forward options property from nested vfs
Augie Fackler <augie@google.com>
parents: 29417
diff changeset
   754
    @options.setter
69109052d9ac auditvfs: forward options property from nested vfs
Augie Fackler <augie@google.com>
parents: 29417
diff changeset
   755
    def options(self, value):
69109052d9ac auditvfs: forward options property from nested vfs
Augie Fackler <augie@google.com>
parents: 29417
diff changeset
   756
        self.vfs.options = value
69109052d9ac auditvfs: forward options property from nested vfs
Augie Fackler <augie@google.com>
parents: 29417
diff changeset
   757
17846
f42cf30873dc scmutil: add mustaudit delegation to filtervfs (issue3673)
Bryan O'Sullivan <bryano@fb.com>
parents: 17845
diff changeset
   758
class filtervfs(abstractvfs, auditvfs):
17649
f65c6a5f256c scmutil: rename classes from "opener" to "vfs"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 17561
diff changeset
   759
    '''Wrapper vfs for filtering filenames with a function.'''
14090
e24b5e3c2f27 add filteropener abstraction for store openers
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 14089
diff changeset
   760
17846
f42cf30873dc scmutil: add mustaudit delegation to filtervfs (issue3673)
Bryan O'Sullivan <bryano@fb.com>
parents: 17845
diff changeset
   761
    def __init__(self, vfs, filter):
f42cf30873dc scmutil: add mustaudit delegation to filtervfs (issue3673)
Bryan O'Sullivan <bryano@fb.com>
parents: 17845
diff changeset
   762
        auditvfs.__init__(self, vfs)
14090
e24b5e3c2f27 add filteropener abstraction for store openers
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 14089
diff changeset
   763
        self._filter = filter
e24b5e3c2f27 add filteropener abstraction for store openers
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 14089
diff changeset
   764
e24b5e3c2f27 add filteropener abstraction for store openers
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 14089
diff changeset
   765
    def __call__(self, path, *args, **kwargs):
17846
f42cf30873dc scmutil: add mustaudit delegation to filtervfs (issue3673)
Bryan O'Sullivan <bryano@fb.com>
parents: 17845
diff changeset
   766
        return self.vfs(self._filter(path), *args, **kwargs)
14090
e24b5e3c2f27 add filteropener abstraction for store openers
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 14089
diff changeset
   767
24628
a0b47885a1c5 vfs: make it possible to pass multiple path elements to join
Matt Harbison <matt_harbison@yahoo.com>
parents: 24582
diff changeset
   768
    def join(self, path, *insidef):
17725
ffd589d4b785 vfs: define "join()" in each classes derived from "abstractvfs"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 17723
diff changeset
   769
        if path:
24628
a0b47885a1c5 vfs: make it possible to pass multiple path elements to join
Matt Harbison <matt_harbison@yahoo.com>
parents: 24582
diff changeset
   770
            return self.vfs.join(self._filter(self.vfs.reljoin(path, *insidef)))
17725
ffd589d4b785 vfs: define "join()" in each classes derived from "abstractvfs"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 17723
diff changeset
   771
        else:
17846
f42cf30873dc scmutil: add mustaudit delegation to filtervfs (issue3673)
Bryan O'Sullivan <bryano@fb.com>
parents: 17845
diff changeset
   772
            return self.vfs.join(path)
17725
ffd589d4b785 vfs: define "join()" in each classes derived from "abstractvfs"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 17723
diff changeset
   773
17649
f65c6a5f256c scmutil: rename classes from "opener" to "vfs"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 17561
diff changeset
   774
filteropener = filtervfs
f65c6a5f256c scmutil: rename classes from "opener" to "vfs"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 17561
diff changeset
   775
18213
c38a62af000e vfs: add a read only vfs
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 18206
diff changeset
   776
class readonlyvfs(abstractvfs, auditvfs):
c38a62af000e vfs: add a read only vfs
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 18206
diff changeset
   777
    '''Wrapper vfs preventing any writing.'''
c38a62af000e vfs: add a read only vfs
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 18206
diff changeset
   778
c38a62af000e vfs: add a read only vfs
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 18206
diff changeset
   779
    def __init__(self, vfs):
c38a62af000e vfs: add a read only vfs
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 18206
diff changeset
   780
        auditvfs.__init__(self, vfs)
c38a62af000e vfs: add a read only vfs
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 18206
diff changeset
   781
c38a62af000e vfs: add a read only vfs
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 18206
diff changeset
   782
    def __call__(self, path, mode='r', *args, **kw):
c38a62af000e vfs: add a read only vfs
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 18206
diff changeset
   783
        if mode not in ('r', 'rb'):
29389
98e8313dcd9e i18n: translate abort messages
liscju <piotr.listkiewicz@gmail.com>
parents: 29373
diff changeset
   784
            raise error.Abort(_('this vfs is read only'))
18213
c38a62af000e vfs: add a read only vfs
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 18206
diff changeset
   785
        return self.vfs(path, mode, *args, **kw)
c38a62af000e vfs: add a read only vfs
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 18206
diff changeset
   786
26156
a112fffdb632 scmutil.readonlyvfs: implement join
Siddharth Agarwal <sid0@fb.com>
parents: 26098
diff changeset
   787
    def join(self, path, *insidef):
a112fffdb632 scmutil.readonlyvfs: implement join
Siddharth Agarwal <sid0@fb.com>
parents: 26098
diff changeset
   788
        return self.vfs.join(path, *insidef)
18213
c38a62af000e vfs: add a read only vfs
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 18206
diff changeset
   789
13975
938fbeacac84 move walkrepos from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13974
diff changeset
   790
def walkrepos(path, followsym=False, seen_dirs=None, recurse=False):
17104
5a9acb0b2086 help: improve hgweb help
Mads Kiilerich <mads@kiilerich.com>
parents: 17037
diff changeset
   791
    '''yield every hg repository under path, always recursively.
5a9acb0b2086 help: improve hgweb help
Mads Kiilerich <mads@kiilerich.com>
parents: 17037
diff changeset
   792
    The recurse flag will only control recursion into repo working dirs'''
13975
938fbeacac84 move walkrepos from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13974
diff changeset
   793
    def errhandler(err):
938fbeacac84 move walkrepos from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13974
diff changeset
   794
        if err.filename == path:
938fbeacac84 move walkrepos from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13974
diff changeset
   795
            raise err
14961
5523529bd1af walkrepos: use getattr instead of hasattr for samestat
Augie Fackler <durin42@gmail.com>
parents: 14928
diff changeset
   796
    samestat = getattr(os.path, 'samestat', None)
5523529bd1af walkrepos: use getattr instead of hasattr for samestat
Augie Fackler <durin42@gmail.com>
parents: 14928
diff changeset
   797
    if followsym and samestat is not None:
14227
94985b5a8278 scmutil: rename local function _add_dir_if_not_there
Adrian Buehlmann <adrian@cadifra.com>
parents: 14226
diff changeset
   798
        def adddir(dirlst, dirname):
13975
938fbeacac84 move walkrepos from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13974
diff changeset
   799
            match = False
938fbeacac84 move walkrepos from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13974
diff changeset
   800
            dirstat = os.stat(dirname)
938fbeacac84 move walkrepos from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13974
diff changeset
   801
            for lstdirstat in dirlst:
938fbeacac84 move walkrepos from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13974
diff changeset
   802
                if samestat(dirstat, lstdirstat):
938fbeacac84 move walkrepos from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13974
diff changeset
   803
                    match = True
938fbeacac84 move walkrepos from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13974
diff changeset
   804
                    break
938fbeacac84 move walkrepos from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13974
diff changeset
   805
            if not match:
938fbeacac84 move walkrepos from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13974
diff changeset
   806
                dirlst.append(dirstat)
938fbeacac84 move walkrepos from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13974
diff changeset
   807
            return not match
938fbeacac84 move walkrepos from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13974
diff changeset
   808
    else:
938fbeacac84 move walkrepos from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13974
diff changeset
   809
        followsym = False
938fbeacac84 move walkrepos from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13974
diff changeset
   810
938fbeacac84 move walkrepos from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13974
diff changeset
   811
    if (seen_dirs is None) and followsym:
938fbeacac84 move walkrepos from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13974
diff changeset
   812
        seen_dirs = []
14227
94985b5a8278 scmutil: rename local function _add_dir_if_not_there
Adrian Buehlmann <adrian@cadifra.com>
parents: 14226
diff changeset
   813
        adddir(seen_dirs, path)
13975
938fbeacac84 move walkrepos from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13974
diff changeset
   814
    for root, dirs, files in os.walk(path, topdown=True, onerror=errhandler):
938fbeacac84 move walkrepos from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13974
diff changeset
   815
        dirs.sort()
938fbeacac84 move walkrepos from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13974
diff changeset
   816
        if '.hg' in dirs:
938fbeacac84 move walkrepos from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13974
diff changeset
   817
            yield root # found a repository
938fbeacac84 move walkrepos from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13974
diff changeset
   818
            qroot = os.path.join(root, '.hg', 'patches')
938fbeacac84 move walkrepos from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13974
diff changeset
   819
            if os.path.isdir(os.path.join(qroot, '.hg')):
938fbeacac84 move walkrepos from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13974
diff changeset
   820
                yield qroot # we have a patch queue repo here
938fbeacac84 move walkrepos from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13974
diff changeset
   821
            if recurse:
938fbeacac84 move walkrepos from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13974
diff changeset
   822
                # avoid recursing inside the .hg directory
938fbeacac84 move walkrepos from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13974
diff changeset
   823
                dirs.remove('.hg')
938fbeacac84 move walkrepos from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13974
diff changeset
   824
            else:
938fbeacac84 move walkrepos from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13974
diff changeset
   825
                dirs[:] = [] # don't descend further
938fbeacac84 move walkrepos from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13974
diff changeset
   826
        elif followsym:
938fbeacac84 move walkrepos from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13974
diff changeset
   827
            newdirs = []
938fbeacac84 move walkrepos from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13974
diff changeset
   828
            for d in dirs:
938fbeacac84 move walkrepos from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13974
diff changeset
   829
                fname = os.path.join(root, d)
14227
94985b5a8278 scmutil: rename local function _add_dir_if_not_there
Adrian Buehlmann <adrian@cadifra.com>
parents: 14226
diff changeset
   830
                if adddir(seen_dirs, fname):
13975
938fbeacac84 move walkrepos from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13974
diff changeset
   831
                    if os.path.islink(fname):
938fbeacac84 move walkrepos from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13974
diff changeset
   832
                        for hgname in walkrepos(fname, True, seen_dirs):
938fbeacac84 move walkrepos from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13974
diff changeset
   833
                            yield hgname
938fbeacac84 move walkrepos from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13974
diff changeset
   834
                    else:
938fbeacac84 move walkrepos from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13974
diff changeset
   835
                        newdirs.append(d)
938fbeacac84 move walkrepos from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13974
diff changeset
   836
            dirs[:] = newdirs
13984
af60153b5e3b move rcpath from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13975
diff changeset
   837
14224
f4189866c76c rename scmutil.os_rcpath to osrcpath
Adrian Buehlmann <adrian@cadifra.com>
parents: 14220
diff changeset
   838
def osrcpath():
13985
26335a817dd0 move os_rcpath from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13984
diff changeset
   839
    '''return default os-specific hgrc search path'''
23142
c4ce077588d0 config: introduce "built-in" default configuration settings in default.d
Mads Kiilerich <madski@unity3d.com>
parents: 22915
diff changeset
   840
    path = []
c4ce077588d0 config: introduce "built-in" default configuration settings in default.d
Mads Kiilerich <madski@unity3d.com>
parents: 22915
diff changeset
   841
    defaultpath = os.path.join(util.datapath, 'default.d')
c4ce077588d0 config: introduce "built-in" default configuration settings in default.d
Mads Kiilerich <madski@unity3d.com>
parents: 22915
diff changeset
   842
    if os.path.isdir(defaultpath):
c4ce077588d0 config: introduce "built-in" default configuration settings in default.d
Mads Kiilerich <madski@unity3d.com>
parents: 22915
diff changeset
   843
        for f, kind in osutil.listdir(defaultpath):
c4ce077588d0 config: introduce "built-in" default configuration settings in default.d
Mads Kiilerich <madski@unity3d.com>
parents: 22915
diff changeset
   844
            if f.endswith('.rc'):
c4ce077588d0 config: introduce "built-in" default configuration settings in default.d
Mads Kiilerich <madski@unity3d.com>
parents: 22915
diff changeset
   845
                path.append(os.path.join(defaultpath, f))
c4ce077588d0 config: introduce "built-in" default configuration settings in default.d
Mads Kiilerich <madski@unity3d.com>
parents: 22915
diff changeset
   846
    path.extend(systemrcpath())
14226
73cca883370d rename scmutil.user_rcpath to userrcpath
Adrian Buehlmann <adrian@cadifra.com>
parents: 14225
diff changeset
   847
    path.extend(userrcpath())
13985
26335a817dd0 move os_rcpath from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13984
diff changeset
   848
    path = [os.path.normpath(f) for f in path]
26335a817dd0 move os_rcpath from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13984
diff changeset
   849
    return path
26335a817dd0 move os_rcpath from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13984
diff changeset
   850
13984
af60153b5e3b move rcpath from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13975
diff changeset
   851
_rcpath = None
af60153b5e3b move rcpath from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13975
diff changeset
   852
af60153b5e3b move rcpath from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13975
diff changeset
   853
def rcpath():
af60153b5e3b move rcpath from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13975
diff changeset
   854
    '''return hgrc search path. if env var HGRCPATH is set, use it.
af60153b5e3b move rcpath from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13975
diff changeset
   855
    for each item in path, if directory, use files ending in .rc,
af60153b5e3b move rcpath from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13975
diff changeset
   856
    else use item.
af60153b5e3b move rcpath from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13975
diff changeset
   857
    make HGRCPATH empty to only look in .hg/hgrc of current repo.
af60153b5e3b move rcpath from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13975
diff changeset
   858
    if no HGRCPATH, use default os-specific path.'''
af60153b5e3b move rcpath from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13975
diff changeset
   859
    global _rcpath
af60153b5e3b move rcpath from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13975
diff changeset
   860
    if _rcpath is None:
30109
96a2278ee732 py3: use encoding.environ instead of os.environ
Pulkit Goyal <7895pulkit@gmail.com>
parents: 30000
diff changeset
   861
        if 'HGRCPATH' in encoding.environ:
13984
af60153b5e3b move rcpath from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13975
diff changeset
   862
            _rcpath = []
30318
af7c60988f6e py3: make scmutil.rcpath() return bytes
Pulkit Goyal <7895pulkit@gmail.com>
parents: 30109
diff changeset
   863
            for p in encoding.environ['HGRCPATH'].split(pycompat.ospathsep):
13984
af60153b5e3b move rcpath from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13975
diff changeset
   864
                if not p:
af60153b5e3b move rcpath from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13975
diff changeset
   865
                    continue
af60153b5e3b move rcpath from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13975
diff changeset
   866
                p = util.expandpath(p)
af60153b5e3b move rcpath from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13975
diff changeset
   867
                if os.path.isdir(p):
af60153b5e3b move rcpath from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13975
diff changeset
   868
                    for f, kind in osutil.listdir(p):
af60153b5e3b move rcpath from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13975
diff changeset
   869
                        if f.endswith('.rc'):
af60153b5e3b move rcpath from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13975
diff changeset
   870
                            _rcpath.append(os.path.join(p, f))
af60153b5e3b move rcpath from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13975
diff changeset
   871
                else:
af60153b5e3b move rcpath from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13975
diff changeset
   872
                    _rcpath.append(p)
af60153b5e3b move rcpath from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13975
diff changeset
   873
        else:
14224
f4189866c76c rename scmutil.os_rcpath to osrcpath
Adrian Buehlmann <adrian@cadifra.com>
parents: 14220
diff changeset
   874
            _rcpath = osrcpath()
13984
af60153b5e3b move rcpath from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13975
diff changeset
   875
    return _rcpath
13986
9c374cf76b7d move system_rcpath and user_rcpath to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13985
diff changeset
   876
25739
3dabc9b7494a changeset_printer: use node.wdirrev to calculate meaningful parentrevs
Yuya Nishihara <yuya@tcha.org>
parents: 25660
diff changeset
   877
def intrev(rev):
24582
56fff44cce98 scmutil: add function to help handling workingctx in arithmetic operation
Yuya Nishihara <yuya@tcha.org>
parents: 24447
diff changeset
   878
    """Return integer for a given revision that can be used in comparison or
56fff44cce98 scmutil: add function to help handling workingctx in arithmetic operation
Yuya Nishihara <yuya@tcha.org>
parents: 24447
diff changeset
   879
    arithmetic operation"""
56fff44cce98 scmutil: add function to help handling workingctx in arithmetic operation
Yuya Nishihara <yuya@tcha.org>
parents: 24447
diff changeset
   880
    if rev is None:
25739
3dabc9b7494a changeset_printer: use node.wdirrev to calculate meaningful parentrevs
Yuya Nishihara <yuya@tcha.org>
parents: 25660
diff changeset
   881
        return wdirrev
24582
56fff44cce98 scmutil: add function to help handling workingctx in arithmetic operation
Yuya Nishihara <yuya@tcha.org>
parents: 24447
diff changeset
   882
    return rev
56fff44cce98 scmutil: add function to help handling workingctx in arithmetic operation
Yuya Nishihara <yuya@tcha.org>
parents: 24447
diff changeset
   883
14319
b33f3e35efb0 scmutil: move revsingle/pair/range from cmdutil
Matt Mackall <mpm@selenic.com>
parents: 14261
diff changeset
   884
def revsingle(repo, revspec, default='.'):
19509
8963a706e075 revsingle: fix silly API issue (issue2992)
Matt Mackall <mpm@selenic.com>
parents: 19154
diff changeset
   885
    if not revspec and revspec != 0:
14319
b33f3e35efb0 scmutil: move revsingle/pair/range from cmdutil
Matt Mackall <mpm@selenic.com>
parents: 14261
diff changeset
   886
        return repo[default]
b33f3e35efb0 scmutil: move revsingle/pair/range from cmdutil
Matt Mackall <mpm@selenic.com>
parents: 14261
diff changeset
   887
b33f3e35efb0 scmutil: move revsingle/pair/range from cmdutil
Matt Mackall <mpm@selenic.com>
parents: 14261
diff changeset
   888
    l = revrange(repo, [revspec])
22814
8110405cf8ae revset-limit: use boolean testing instead of `len(revs) < 1`
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 21799
diff changeset
   889
    if not l:
26587
56b2bcea2529 error: get Abort from 'error' instead of 'util'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26491
diff changeset
   890
        raise error.Abort(_('empty revision set'))
22815
4f81470e83bf revsingle: use `last` instead of direct indexing
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22814
diff changeset
   891
    return repo[l.last()]
14319
b33f3e35efb0 scmutil: move revsingle/pair/range from cmdutil
Matt Mackall <mpm@selenic.com>
parents: 14261
diff changeset
   892
26020
cc3a30ff9490 revpair: restrict odd-range handling to top-level x:y expression (issue4774)
Yuya Nishihara <yuya@tcha.org>
parents: 25928
diff changeset
   893
def _pairspec(revspec):
31044
0b8356705de6 revset: split language services to revsetlang module (API)
Yuya Nishihara <yuya@tcha.org>
parents: 30644
diff changeset
   894
    tree = revsetlang.parse(revspec)
26020
cc3a30ff9490 revpair: restrict odd-range handling to top-level x:y expression (issue4774)
Yuya Nishihara <yuya@tcha.org>
parents: 25928
diff changeset
   895
    return tree and tree[0] in ('range', 'rangepre', 'rangepost', 'rangeall')
cc3a30ff9490 revpair: restrict odd-range handling to top-level x:y expression (issue4774)
Yuya Nishihara <yuya@tcha.org>
parents: 25928
diff changeset
   896
14319
b33f3e35efb0 scmutil: move revsingle/pair/range from cmdutil
Matt Mackall <mpm@selenic.com>
parents: 14261
diff changeset
   897
def revpair(repo, revs):
b33f3e35efb0 scmutil: move revsingle/pair/range from cmdutil
Matt Mackall <mpm@selenic.com>
parents: 14261
diff changeset
   898
    if not revs:
b33f3e35efb0 scmutil: move revsingle/pair/range from cmdutil
Matt Mackall <mpm@selenic.com>
parents: 14261
diff changeset
   899
        return repo.dirstate.p1(), None
b33f3e35efb0 scmutil: move revsingle/pair/range from cmdutil
Matt Mackall <mpm@selenic.com>
parents: 14261
diff changeset
   900
b33f3e35efb0 scmutil: move revsingle/pair/range from cmdutil
Matt Mackall <mpm@selenic.com>
parents: 14261
diff changeset
   901
    l = revrange(repo, revs)
b33f3e35efb0 scmutil: move revsingle/pair/range from cmdutil
Matt Mackall <mpm@selenic.com>
parents: 14261
diff changeset
   902
20862
97b2f26dfc43 revpair: smartset compatibility
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20820
diff changeset
   903
    if not l:
97b2f26dfc43 revpair: smartset compatibility
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20820
diff changeset
   904
        first = second = None
97b2f26dfc43 revpair: smartset compatibility
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20820
diff changeset
   905
    elif l.isascending():
97b2f26dfc43 revpair: smartset compatibility
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20820
diff changeset
   906
        first = l.min()
97b2f26dfc43 revpair: smartset compatibility
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20820
diff changeset
   907
        second = l.max()
97b2f26dfc43 revpair: smartset compatibility
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20820
diff changeset
   908
    elif l.isdescending():
97b2f26dfc43 revpair: smartset compatibility
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20820
diff changeset
   909
        first = l.max()
97b2f26dfc43 revpair: smartset compatibility
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20820
diff changeset
   910
        second = l.min()
97b2f26dfc43 revpair: smartset compatibility
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20820
diff changeset
   911
    else:
22816
20d998395ee7 revpair: use `first` and `last` instead of direct indexing
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22815
diff changeset
   912
        first = l.first()
20d998395ee7 revpair: use `first` and `last` instead of direct indexing
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22815
diff changeset
   913
        second = l.last()
20862
97b2f26dfc43 revpair: smartset compatibility
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20820
diff changeset
   914
97b2f26dfc43 revpair: smartset compatibility
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20820
diff changeset
   915
    if first is None:
26587
56b2bcea2529 error: get Abort from 'error' instead of 'util'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26491
diff changeset
   916
        raise error.Abort(_('empty revision range'))
26836
88c4e97b9669 scmutil: abort if an empty revision is given to revpair()
Matt Harbison <matt_harbison@yahoo.com>
parents: 26587
diff changeset
   917
    if (first == second and len(revs) >= 2
88c4e97b9669 scmutil: abort if an empty revision is given to revpair()
Matt Harbison <matt_harbison@yahoo.com>
parents: 26587
diff changeset
   918
        and not all(revrange(repo, [r]) for r in revs)):
88c4e97b9669 scmutil: abort if an empty revision is given to revpair()
Matt Harbison <matt_harbison@yahoo.com>
parents: 26587
diff changeset
   919
        raise error.Abort(_('empty revision on one side of range'))
14319
b33f3e35efb0 scmutil: move revsingle/pair/range from cmdutil
Matt Mackall <mpm@selenic.com>
parents: 14261
diff changeset
   920
26020
cc3a30ff9490 revpair: restrict odd-range handling to top-level x:y expression (issue4774)
Yuya Nishihara <yuya@tcha.org>
parents: 25928
diff changeset
   921
    # if top-level is range expression, the result must always be a pair
cc3a30ff9490 revpair: restrict odd-range handling to top-level x:y expression (issue4774)
Yuya Nishihara <yuya@tcha.org>
parents: 25928
diff changeset
   922
    if first == second and len(revs) == 1 and not _pairspec(revs[0]):
20862
97b2f26dfc43 revpair: smartset compatibility
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20820
diff changeset
   923
        return repo.lookup(first), None
14319
b33f3e35efb0 scmutil: move revsingle/pair/range from cmdutil
Matt Mackall <mpm@selenic.com>
parents: 14261
diff changeset
   924
20862
97b2f26dfc43 revpair: smartset compatibility
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 20820
diff changeset
   925
    return repo.lookup(first), repo.lookup(second)
14319
b33f3e35efb0 scmutil: move revsingle/pair/range from cmdutil
Matt Mackall <mpm@selenic.com>
parents: 14261
diff changeset
   926
29417
526b027b0130 scmutil: improve documentation of revset APIs
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29389
diff changeset
   927
def revrange(repo, specs):
526b027b0130 scmutil: improve documentation of revset APIs
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29389
diff changeset
   928
    """Execute 1 to many revsets and return the union.
526b027b0130 scmutil: improve documentation of revset APIs
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29389
diff changeset
   929
526b027b0130 scmutil: improve documentation of revset APIs
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29389
diff changeset
   930
    This is the preferred mechanism for executing revsets using user-specified
526b027b0130 scmutil: improve documentation of revset APIs
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29389
diff changeset
   931
    config options, such as revset aliases.
526b027b0130 scmutil: improve documentation of revset APIs
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29389
diff changeset
   932
526b027b0130 scmutil: improve documentation of revset APIs
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29389
diff changeset
   933
    The revsets specified by ``specs`` will be executed via a chained ``OR``
526b027b0130 scmutil: improve documentation of revset APIs
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29389
diff changeset
   934
    expression. If ``specs`` is empty, an empty result is returned.
526b027b0130 scmutil: improve documentation of revset APIs
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29389
diff changeset
   935
526b027b0130 scmutil: improve documentation of revset APIs
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29389
diff changeset
   936
    ``specs`` can contain integers, in which case they are assumed to be
526b027b0130 scmutil: improve documentation of revset APIs
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29389
diff changeset
   937
    revision numbers.
526b027b0130 scmutil: improve documentation of revset APIs
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29389
diff changeset
   938
526b027b0130 scmutil: improve documentation of revset APIs
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29389
diff changeset
   939
    It is assumed the revsets are already formatted. If you have arguments
31044
0b8356705de6 revset: split language services to revsetlang module (API)
Yuya Nishihara <yuya@tcha.org>
parents: 30644
diff changeset
   940
    that need to be expanded in the revset, call ``revsetlang.formatspec()``
29417
526b027b0130 scmutil: improve documentation of revset APIs
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29389
diff changeset
   941
    and pass the result as an element of ``specs``.
526b027b0130 scmutil: improve documentation of revset APIs
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29389
diff changeset
   942
526b027b0130 scmutil: improve documentation of revset APIs
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29389
diff changeset
   943
    Specifying a single revset is allowed.
526b027b0130 scmutil: improve documentation of revset APIs
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29389
diff changeset
   944
526b027b0130 scmutil: improve documentation of revset APIs
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29389
diff changeset
   945
    Returns a ``revset.abstractsmartset`` which is a list-like interface over
526b027b0130 scmutil: improve documentation of revset APIs
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29389
diff changeset
   946
    integer revisions.
526b027b0130 scmutil: improve documentation of revset APIs
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29389
diff changeset
   947
    """
25928
4ee4f7415095 revrange: evaluate all revset specs at once
Yuya Nishihara <yuya@tcha.org>
parents: 25904
diff changeset
   948
    allspecs = []
29417
526b027b0130 scmutil: improve documentation of revset APIs
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29389
diff changeset
   949
    for spec in specs:
25904
fbaa2de13cf6 revrange: drop old-style parser in favor of revset (API)
Yuya Nishihara <yuya@tcha.org>
parents: 25772
diff changeset
   950
        if isinstance(spec, int):
31044
0b8356705de6 revset: split language services to revsetlang module (API)
Yuya Nishihara <yuya@tcha.org>
parents: 30644
diff changeset
   951
            spec = revsetlang.formatspec('rev(%d)', spec)
25928
4ee4f7415095 revrange: evaluate all revset specs at once
Yuya Nishihara <yuya@tcha.org>
parents: 25904
diff changeset
   952
        allspecs.append(spec)
4ee4f7415095 revrange: evaluate all revset specs at once
Yuya Nishihara <yuya@tcha.org>
parents: 25904
diff changeset
   953
    m = revset.matchany(repo.ui, allspecs, repo)
4ee4f7415095 revrange: evaluate all revset specs at once
Yuya Nishihara <yuya@tcha.org>
parents: 25904
diff changeset
   954
    return m(repo)
14320
3438417a6657 scmutil: fold in wdutil
Matt Mackall <mpm@selenic.com>
parents: 14319
diff changeset
   955
26433
3ad41638b4b4 changeset_printer: move _meaningful_parentrevs() to scmutil
Yuya Nishihara <yuya@tcha.org>
parents: 26421
diff changeset
   956
def meaningfulparents(repo, ctx):
3ad41638b4b4 changeset_printer: move _meaningful_parentrevs() to scmutil
Yuya Nishihara <yuya@tcha.org>
parents: 26421
diff changeset
   957
    """Return list of meaningful (or all if debug) parentrevs for rev.
3ad41638b4b4 changeset_printer: move _meaningful_parentrevs() to scmutil
Yuya Nishihara <yuya@tcha.org>
parents: 26421
diff changeset
   958
3ad41638b4b4 changeset_printer: move _meaningful_parentrevs() to scmutil
Yuya Nishihara <yuya@tcha.org>
parents: 26421
diff changeset
   959
    For merges (two non-nullrev revisions) both parents are meaningful.
3ad41638b4b4 changeset_printer: move _meaningful_parentrevs() to scmutil
Yuya Nishihara <yuya@tcha.org>
parents: 26421
diff changeset
   960
    Otherwise the first parent revision is considered meaningful if it
3ad41638b4b4 changeset_printer: move _meaningful_parentrevs() to scmutil
Yuya Nishihara <yuya@tcha.org>
parents: 26421
diff changeset
   961
    is not the preceding revision.
3ad41638b4b4 changeset_printer: move _meaningful_parentrevs() to scmutil
Yuya Nishihara <yuya@tcha.org>
parents: 26421
diff changeset
   962
    """
3ad41638b4b4 changeset_printer: move _meaningful_parentrevs() to scmutil
Yuya Nishihara <yuya@tcha.org>
parents: 26421
diff changeset
   963
    parents = ctx.parents()
3ad41638b4b4 changeset_printer: move _meaningful_parentrevs() to scmutil
Yuya Nishihara <yuya@tcha.org>
parents: 26421
diff changeset
   964
    if len(parents) > 1:
3ad41638b4b4 changeset_printer: move _meaningful_parentrevs() to scmutil
Yuya Nishihara <yuya@tcha.org>
parents: 26421
diff changeset
   965
        return parents
3ad41638b4b4 changeset_printer: move _meaningful_parentrevs() to scmutil
Yuya Nishihara <yuya@tcha.org>
parents: 26421
diff changeset
   966
    if repo.ui.debugflag:
3ad41638b4b4 changeset_printer: move _meaningful_parentrevs() to scmutil
Yuya Nishihara <yuya@tcha.org>
parents: 26421
diff changeset
   967
        return [parents[0], repo['null']]
3ad41638b4b4 changeset_printer: move _meaningful_parentrevs() to scmutil
Yuya Nishihara <yuya@tcha.org>
parents: 26421
diff changeset
   968
    if parents[0].rev() >= intrev(ctx.rev()) - 1:
3ad41638b4b4 changeset_printer: move _meaningful_parentrevs() to scmutil
Yuya Nishihara <yuya@tcha.org>
parents: 26421
diff changeset
   969
        return []
3ad41638b4b4 changeset_printer: move _meaningful_parentrevs() to scmutil
Yuya Nishihara <yuya@tcha.org>
parents: 26421
diff changeset
   970
    return parents
3ad41638b4b4 changeset_printer: move _meaningful_parentrevs() to scmutil
Yuya Nishihara <yuya@tcha.org>
parents: 26421
diff changeset
   971
14320
3438417a6657 scmutil: fold in wdutil
Matt Mackall <mpm@selenic.com>
parents: 14319
diff changeset
   972
def expandpats(pats):
21111
9d28fd795215 match: improve documentation - docstrings and more descriptive variable naming
Mads Kiilerich <madski@unity3d.com>
parents: 20980
diff changeset
   973
    '''Expand bare globs when running on windows.
9d28fd795215 match: improve documentation - docstrings and more descriptive variable naming
Mads Kiilerich <madski@unity3d.com>
parents: 20980
diff changeset
   974
    On posix we assume it already has already been done by sh.'''
14320
3438417a6657 scmutil: fold in wdutil
Matt Mackall <mpm@selenic.com>
parents: 14319
diff changeset
   975
    if not util.expandglobs:
3438417a6657 scmutil: fold in wdutil
Matt Mackall <mpm@selenic.com>
parents: 14319
diff changeset
   976
        return list(pats)
3438417a6657 scmutil: fold in wdutil
Matt Mackall <mpm@selenic.com>
parents: 14319
diff changeset
   977
    ret = []
21111
9d28fd795215 match: improve documentation - docstrings and more descriptive variable naming
Mads Kiilerich <madski@unity3d.com>
parents: 20980
diff changeset
   978
    for kindpat in pats:
9d28fd795215 match: improve documentation - docstrings and more descriptive variable naming
Mads Kiilerich <madski@unity3d.com>
parents: 20980
diff changeset
   979
        kind, pat = matchmod._patsplit(kindpat, None)
14320
3438417a6657 scmutil: fold in wdutil
Matt Mackall <mpm@selenic.com>
parents: 14319
diff changeset
   980
        if kind is None:
3438417a6657 scmutil: fold in wdutil
Matt Mackall <mpm@selenic.com>
parents: 14319
diff changeset
   981
            try:
21111
9d28fd795215 match: improve documentation - docstrings and more descriptive variable naming
Mads Kiilerich <madski@unity3d.com>
parents: 20980
diff changeset
   982
                globbed = glob.glob(pat)
14320
3438417a6657 scmutil: fold in wdutil
Matt Mackall <mpm@selenic.com>
parents: 14319
diff changeset
   983
            except re.error:
21111
9d28fd795215 match: improve documentation - docstrings and more descriptive variable naming
Mads Kiilerich <madski@unity3d.com>
parents: 20980
diff changeset
   984
                globbed = [pat]
14320
3438417a6657 scmutil: fold in wdutil
Matt Mackall <mpm@selenic.com>
parents: 14319
diff changeset
   985
            if globbed:
3438417a6657 scmutil: fold in wdutil
Matt Mackall <mpm@selenic.com>
parents: 14319
diff changeset
   986
                ret.extend(globbed)
3438417a6657 scmutil: fold in wdutil
Matt Mackall <mpm@selenic.com>
parents: 14319
diff changeset
   987
                continue
21111
9d28fd795215 match: improve documentation - docstrings and more descriptive variable naming
Mads Kiilerich <madski@unity3d.com>
parents: 20980
diff changeset
   988
        ret.append(kindpat)
14320
3438417a6657 scmutil: fold in wdutil
Matt Mackall <mpm@selenic.com>
parents: 14319
diff changeset
   989
    return ret
3438417a6657 scmutil: fold in wdutil
Matt Mackall <mpm@selenic.com>
parents: 14319
diff changeset
   990
26326
58218b0e6005 match: remove a mutable default argument
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26325
diff changeset
   991
def matchandpats(ctx, pats=(), opts=None, globbed=False, default='relpath',
25467
f64dbe06f3d0 scmutil: add an optional parameter to matcher factories for a bad() override
Matt Harbison <matt_harbison@yahoo.com>
parents: 25466
diff changeset
   992
                 badfn=None):
21111
9d28fd795215 match: improve documentation - docstrings and more descriptive variable naming
Mads Kiilerich <madski@unity3d.com>
parents: 20980
diff changeset
   993
    '''Return a matcher and the patterns that were used.
25467
f64dbe06f3d0 scmutil: add an optional parameter to matcher factories for a bad() override
Matt Harbison <matt_harbison@yahoo.com>
parents: 25466
diff changeset
   994
    The matcher will warn about bad matches, unless an alternate badfn callback
f64dbe06f3d0 scmutil: add an optional parameter to matcher factories for a bad() override
Matt Harbison <matt_harbison@yahoo.com>
parents: 25466
diff changeset
   995
    is provided.'''
14320
3438417a6657 scmutil: fold in wdutil
Matt Mackall <mpm@selenic.com>
parents: 14319
diff changeset
   996
    if pats == ("",):
3438417a6657 scmutil: fold in wdutil
Matt Mackall <mpm@selenic.com>
parents: 14319
diff changeset
   997
        pats = []
26326
58218b0e6005 match: remove a mutable default argument
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26325
diff changeset
   998
    if opts is None:
58218b0e6005 match: remove a mutable default argument
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26325
diff changeset
   999
        opts = {}
14320
3438417a6657 scmutil: fold in wdutil
Matt Mackall <mpm@selenic.com>
parents: 14319
diff changeset
  1000
    if not globbed and default == 'relpath':
3438417a6657 scmutil: fold in wdutil
Matt Mackall <mpm@selenic.com>
parents: 14319
diff changeset
  1001
        pats = expandpats(pats or [])
14670
19197fa4c41c scmutil: match now accepts a context or a repo
Matt Mackall <mpm@selenic.com>
parents: 14669
diff changeset
  1002
25467
f64dbe06f3d0 scmutil: add an optional parameter to matcher factories for a bad() override
Matt Harbison <matt_harbison@yahoo.com>
parents: 25466
diff changeset
  1003
    def bad(f, msg):
24338
ca1365078c86 scmutil: replace 'ctx._repo' with 'ctx.repo()'
Matt Harbison <matt_harbison@yahoo.com>
parents: 24175
diff changeset
  1004
        ctx.repo().ui.warn("%s: %s\n" % (m.rel(f), msg))
25466
007a1d53f7c3 scmutil: use the optional badfn argument when building a matcher
Matt Harbison <matt_harbison@yahoo.com>
parents: 25434
diff changeset
  1005
25467
f64dbe06f3d0 scmutil: add an optional parameter to matcher factories for a bad() override
Matt Harbison <matt_harbison@yahoo.com>
parents: 25466
diff changeset
  1006
    if badfn is None:
f64dbe06f3d0 scmutil: add an optional parameter to matcher factories for a bad() override
Matt Harbison <matt_harbison@yahoo.com>
parents: 25466
diff changeset
  1007
        badfn = bad
f64dbe06f3d0 scmutil: add an optional parameter to matcher factories for a bad() override
Matt Harbison <matt_harbison@yahoo.com>
parents: 25466
diff changeset
  1008
25466
007a1d53f7c3 scmutil: use the optional badfn argument when building a matcher
Matt Harbison <matt_harbison@yahoo.com>
parents: 25434
diff changeset
  1009
    m = ctx.match(pats, opts.get('include'), opts.get('exclude'),
007a1d53f7c3 scmutil: use the optional badfn argument when building a matcher
Matt Harbison <matt_harbison@yahoo.com>
parents: 25434
diff changeset
  1010
                  default, listsubrepos=opts.get('subrepos'), badfn=badfn)
007a1d53f7c3 scmutil: use the optional badfn argument when building a matcher
Matt Harbison <matt_harbison@yahoo.com>
parents: 25434
diff changeset
  1011
24447
d44d53bc9a1e matcher: make e.g. 'relpath:.' lead to fast paths
Martin von Zweigbergk <martinvonz@google.com>
parents: 24338
diff changeset
  1012
    if m.always():
d44d53bc9a1e matcher: make e.g. 'relpath:.' lead to fast paths
Martin von Zweigbergk <martinvonz@google.com>
parents: 24338
diff changeset
  1013
        pats = []
16171
336e61875335 graphlog: restore FILE glob expansion on Windows
Patrick Mezard <patrick@mezard.eu>
parents: 16167
diff changeset
  1014
    return m, pats
336e61875335 graphlog: restore FILE glob expansion on Windows
Patrick Mezard <patrick@mezard.eu>
parents: 16167
diff changeset
  1015
26328
188c1e9506f5 match: remove a mutable default argument
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26327
diff changeset
  1016
def match(ctx, pats=(), opts=None, globbed=False, default='relpath',
188c1e9506f5 match: remove a mutable default argument
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26327
diff changeset
  1017
          badfn=None):
21111
9d28fd795215 match: improve documentation - docstrings and more descriptive variable naming
Mads Kiilerich <madski@unity3d.com>
parents: 20980
diff changeset
  1018
    '''Return a matcher that will warn about bad matches.'''
25467
f64dbe06f3d0 scmutil: add an optional parameter to matcher factories for a bad() override
Matt Harbison <matt_harbison@yahoo.com>
parents: 25466
diff changeset
  1019
    return matchandpats(ctx, pats, opts, globbed, default, badfn=badfn)[0]
14320
3438417a6657 scmutil: fold in wdutil
Matt Mackall <mpm@selenic.com>
parents: 14319
diff changeset
  1020
3438417a6657 scmutil: fold in wdutil
Matt Mackall <mpm@selenic.com>
parents: 14319
diff changeset
  1021
def matchall(repo):
21111
9d28fd795215 match: improve documentation - docstrings and more descriptive variable naming
Mads Kiilerich <madski@unity3d.com>
parents: 20980
diff changeset
  1022
    '''Return a matcher that will efficiently match everything.'''
14320
3438417a6657 scmutil: fold in wdutil
Matt Mackall <mpm@selenic.com>
parents: 14319
diff changeset
  1023
    return matchmod.always(repo.root, repo.getcwd())
3438417a6657 scmutil: fold in wdutil
Matt Mackall <mpm@selenic.com>
parents: 14319
diff changeset
  1024
25467
f64dbe06f3d0 scmutil: add an optional parameter to matcher factories for a bad() override
Matt Harbison <matt_harbison@yahoo.com>
parents: 25466
diff changeset
  1025
def matchfiles(repo, files, badfn=None):
21111
9d28fd795215 match: improve documentation - docstrings and more descriptive variable naming
Mads Kiilerich <madski@unity3d.com>
parents: 20980
diff changeset
  1026
    '''Return a matcher that will efficiently match exactly these files.'''
25467
f64dbe06f3d0 scmutil: add an optional parameter to matcher factories for a bad() override
Matt Harbison <matt_harbison@yahoo.com>
parents: 25466
diff changeset
  1027
    return matchmod.exact(repo.root, repo.getcwd(), files, badfn=badfn)
14320
3438417a6657 scmutil: fold in wdutil
Matt Mackall <mpm@selenic.com>
parents: 14319
diff changeset
  1028
27651
07fc2f2134ba origpath: move from cmdutil to scmutil
Siddharth Agarwal <sid0@fb.com>
parents: 27610
diff changeset
  1029
def origpath(ui, repo, filepath):
07fc2f2134ba origpath: move from cmdutil to scmutil
Siddharth Agarwal <sid0@fb.com>
parents: 27610
diff changeset
  1030
    '''customize where .orig files are created
07fc2f2134ba origpath: move from cmdutil to scmutil
Siddharth Agarwal <sid0@fb.com>
parents: 27610
diff changeset
  1031
07fc2f2134ba origpath: move from cmdutil to scmutil
Siddharth Agarwal <sid0@fb.com>
parents: 27610
diff changeset
  1032
    Fetch user defined path from config file: [ui] origbackuppath = <path>
07fc2f2134ba origpath: move from cmdutil to scmutil
Siddharth Agarwal <sid0@fb.com>
parents: 27610
diff changeset
  1033
    Fall back to default (filepath) if not specified
07fc2f2134ba origpath: move from cmdutil to scmutil
Siddharth Agarwal <sid0@fb.com>
parents: 27610
diff changeset
  1034
    '''
07fc2f2134ba origpath: move from cmdutil to scmutil
Siddharth Agarwal <sid0@fb.com>
parents: 27610
diff changeset
  1035
    origbackuppath = ui.config('ui', 'origbackuppath', None)
07fc2f2134ba origpath: move from cmdutil to scmutil
Siddharth Agarwal <sid0@fb.com>
parents: 27610
diff changeset
  1036
    if origbackuppath is None:
07fc2f2134ba origpath: move from cmdutil to scmutil
Siddharth Agarwal <sid0@fb.com>
parents: 27610
diff changeset
  1037
        return filepath + ".orig"
07fc2f2134ba origpath: move from cmdutil to scmutil
Siddharth Agarwal <sid0@fb.com>
parents: 27610
diff changeset
  1038
07fc2f2134ba origpath: move from cmdutil to scmutil
Siddharth Agarwal <sid0@fb.com>
parents: 27610
diff changeset
  1039
    filepathfromroot = os.path.relpath(filepath, start=repo.root)
07fc2f2134ba origpath: move from cmdutil to scmutil
Siddharth Agarwal <sid0@fb.com>
parents: 27610
diff changeset
  1040
    fullorigpath = repo.wjoin(origbackuppath, filepathfromroot)
07fc2f2134ba origpath: move from cmdutil to scmutil
Siddharth Agarwal <sid0@fb.com>
parents: 27610
diff changeset
  1041
07fc2f2134ba origpath: move from cmdutil to scmutil
Siddharth Agarwal <sid0@fb.com>
parents: 27610
diff changeset
  1042
    origbackupdir = repo.vfs.dirname(fullorigpath)
07fc2f2134ba origpath: move from cmdutil to scmutil
Siddharth Agarwal <sid0@fb.com>
parents: 27610
diff changeset
  1043
    if not repo.vfs.exists(origbackupdir):
07fc2f2134ba origpath: move from cmdutil to scmutil
Siddharth Agarwal <sid0@fb.com>
parents: 27610
diff changeset
  1044
        ui.note(_('creating directory: %s\n') % origbackupdir)
07fc2f2134ba origpath: move from cmdutil to scmutil
Siddharth Agarwal <sid0@fb.com>
parents: 27610
diff changeset
  1045
        util.makedirs(origbackupdir)
07fc2f2134ba origpath: move from cmdutil to scmutil
Siddharth Agarwal <sid0@fb.com>
parents: 27610
diff changeset
  1046
07fc2f2134ba origpath: move from cmdutil to scmutil
Siddharth Agarwal <sid0@fb.com>
parents: 27610
diff changeset
  1047
    return fullorigpath + ".orig"
07fc2f2134ba origpath: move from cmdutil to scmutil
Siddharth Agarwal <sid0@fb.com>
parents: 27610
diff changeset
  1048
26329
d9537ce64f3a addremove: remove a mutable default argument
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26328
diff changeset
  1049
def addremove(repo, matcher, prefix, opts=None, dry_run=None, similarity=None):
d9537ce64f3a addremove: remove a mutable default argument
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26328
diff changeset
  1050
    if opts is None:
d9537ce64f3a addremove: remove a mutable default argument
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26328
diff changeset
  1051
        opts = {}
23533
891aaa7c0c70 scmutil: pass a matcher to scmutil.addremove() instead of a list of patterns
Matt Harbison <matt_harbison@yahoo.com>
parents: 23481
diff changeset
  1052
    m = matcher
14320
3438417a6657 scmutil: fold in wdutil
Matt Mackall <mpm@selenic.com>
parents: 14319
diff changeset
  1053
    if dry_run is None:
3438417a6657 scmutil: fold in wdutil
Matt Mackall <mpm@selenic.com>
parents: 14319
diff changeset
  1054
        dry_run = opts.get('dry_run')
3438417a6657 scmutil: fold in wdutil
Matt Mackall <mpm@selenic.com>
parents: 14319
diff changeset
  1055
    if similarity is None:
3438417a6657 scmutil: fold in wdutil
Matt Mackall <mpm@selenic.com>
parents: 14319
diff changeset
  1056
        similarity = float(opts.get('similarity') or 0)
23533
891aaa7c0c70 scmutil: pass a matcher to scmutil.addremove() instead of a list of patterns
Matt Harbison <matt_harbison@yahoo.com>
parents: 23481
diff changeset
  1057
23537
f1b06a8aad42 commit: propagate --addremove to subrepos if -S is specified (issue3759)
Matt Harbison <matt_harbison@yahoo.com>
parents: 23534
diff changeset
  1058
    ret = 0
f1b06a8aad42 commit: propagate --addremove to subrepos if -S is specified (issue3759)
Matt Harbison <matt_harbison@yahoo.com>
parents: 23534
diff changeset
  1059
    join = lambda f: os.path.join(prefix, f)
f1b06a8aad42 commit: propagate --addremove to subrepos if -S is specified (issue3759)
Matt Harbison <matt_harbison@yahoo.com>
parents: 23534
diff changeset
  1060
f1b06a8aad42 commit: propagate --addremove to subrepos if -S is specified (issue3759)
Matt Harbison <matt_harbison@yahoo.com>
parents: 23534
diff changeset
  1061
    wctx = repo[None]
f1b06a8aad42 commit: propagate --addremove to subrepos if -S is specified (issue3759)
Matt Harbison <matt_harbison@yahoo.com>
parents: 23534
diff changeset
  1062
    for subpath in sorted(wctx.substate):
29813
35560189677c subrepo: cleanup of subrepo filematcher logic
Hannes Oldenburg <hannes.christian.oldenburg@gmail.com>
parents: 29782
diff changeset
  1063
        submatch = matchmod.subdirmatcher(subpath, m)
35560189677c subrepo: cleanup of subrepo filematcher logic
Hannes Oldenburg <hannes.christian.oldenburg@gmail.com>
parents: 29782
diff changeset
  1064
        if opts.get('subrepos') or m.exact(subpath) or any(submatch.files()):
23537
f1b06a8aad42 commit: propagate --addremove to subrepos if -S is specified (issue3759)
Matt Harbison <matt_harbison@yahoo.com>
parents: 23534
diff changeset
  1065
            sub = wctx.sub(subpath)
f1b06a8aad42 commit: propagate --addremove to subrepos if -S is specified (issue3759)
Matt Harbison <matt_harbison@yahoo.com>
parents: 23534
diff changeset
  1066
            try:
f1b06a8aad42 commit: propagate --addremove to subrepos if -S is specified (issue3759)
Matt Harbison <matt_harbison@yahoo.com>
parents: 23534
diff changeset
  1067
                if sub.addremove(submatch, prefix, opts, dry_run, similarity):
f1b06a8aad42 commit: propagate --addremove to subrepos if -S is specified (issue3759)
Matt Harbison <matt_harbison@yahoo.com>
parents: 23534
diff changeset
  1068
                    ret = 1
f1b06a8aad42 commit: propagate --addremove to subrepos if -S is specified (issue3759)
Matt Harbison <matt_harbison@yahoo.com>
parents: 23534
diff changeset
  1069
            except error.LookupError:
f1b06a8aad42 commit: propagate --addremove to subrepos if -S is specified (issue3759)
Matt Harbison <matt_harbison@yahoo.com>
parents: 23534
diff changeset
  1070
                repo.ui.status(_("skipping missing subrepository: %s\n")
f1b06a8aad42 commit: propagate --addremove to subrepos if -S is specified (issue3759)
Matt Harbison <matt_harbison@yahoo.com>
parents: 23534
diff changeset
  1071
                                 % join(subpath))
f1b06a8aad42 commit: propagate --addremove to subrepos if -S is specified (issue3759)
Matt Harbison <matt_harbison@yahoo.com>
parents: 23534
diff changeset
  1072
16167
94a8396c9305 addremove: return 1 if we failed to handle any explicit files
Matt Mackall <mpm@selenic.com>
parents: 16115
diff changeset
  1073
    rejected = []
23534
83bbedc16b3f addremove: warn when addremove fails to operate on a named path
Matt Harbison <matt_harbison@yahoo.com>
parents: 23533
diff changeset
  1074
    def badfn(f, msg):
83bbedc16b3f addremove: warn when addremove fails to operate on a named path
Matt Harbison <matt_harbison@yahoo.com>
parents: 23533
diff changeset
  1075
        if f in m.files():
25434
5984dd42e140 addremove: replace match.bad() monkey patching with match.badmatch()
Matt Harbison <matt_harbison@yahoo.com>
parents: 25418
diff changeset
  1076
            m.bad(f, msg)
23534
83bbedc16b3f addremove: warn when addremove fails to operate on a named path
Matt Harbison <matt_harbison@yahoo.com>
parents: 23533
diff changeset
  1077
        rejected.append(f)
16167
94a8396c9305 addremove: return 1 if we failed to handle any explicit files
Matt Mackall <mpm@selenic.com>
parents: 16115
diff changeset
  1078
25434
5984dd42e140 addremove: replace match.bad() monkey patching with match.badmatch()
Matt Harbison <matt_harbison@yahoo.com>
parents: 25418
diff changeset
  1079
    badmatch = matchmod.badmatch(m, badfn)
5984dd42e140 addremove: replace match.bad() monkey patching with match.badmatch()
Matt Harbison <matt_harbison@yahoo.com>
parents: 25418
diff changeset
  1080
    added, unknown, deleted, removed, forgotten = _interestingfiles(repo,
5984dd42e140 addremove: replace match.bad() monkey patching with match.badmatch()
Matt Harbison <matt_harbison@yahoo.com>
parents: 25418
diff changeset
  1081
                                                                    badmatch)
18863
1b70e5941ad7 scmutil.addremove: pull ui.status printing out of the loop
Siddharth Agarwal <sid0@fb.com>
parents: 18862
diff changeset
  1082
23259
9f4778027bc2 addremove: add back forgotten files (BC)
Martin von Zweigbergk <martinvonz@google.com>
parents: 23142
diff changeset
  1083
    unknownset = set(unknown + forgotten)
18863
1b70e5941ad7 scmutil.addremove: pull ui.status printing out of the loop
Siddharth Agarwal <sid0@fb.com>
parents: 18862
diff changeset
  1084
    toprint = unknownset.copy()
1b70e5941ad7 scmutil.addremove: pull ui.status printing out of the loop
Siddharth Agarwal <sid0@fb.com>
parents: 18862
diff changeset
  1085
    toprint.update(deleted)
1b70e5941ad7 scmutil.addremove: pull ui.status printing out of the loop
Siddharth Agarwal <sid0@fb.com>
parents: 18862
diff changeset
  1086
    for abs in sorted(toprint):
1b70e5941ad7 scmutil.addremove: pull ui.status printing out of the loop
Siddharth Agarwal <sid0@fb.com>
parents: 18862
diff changeset
  1087
        if repo.ui.verbose or not m.exact(abs):
1b70e5941ad7 scmutil.addremove: pull ui.status printing out of the loop
Siddharth Agarwal <sid0@fb.com>
parents: 18862
diff changeset
  1088
            if abs in unknownset:
23686
164915e8ef7b narrowmatcher: propagate the rel() method
Matt Harbison <matt_harbison@yahoo.com>
parents: 23582
diff changeset
  1089
                status = _('adding %s\n') % m.uipath(abs)
18863
1b70e5941ad7 scmutil.addremove: pull ui.status printing out of the loop
Siddharth Agarwal <sid0@fb.com>
parents: 18862
diff changeset
  1090
            else:
23686
164915e8ef7b narrowmatcher: propagate the rel() method
Matt Harbison <matt_harbison@yahoo.com>
parents: 23582
diff changeset
  1091
                status = _('removing %s\n') % m.uipath(abs)
18863
1b70e5941ad7 scmutil.addremove: pull ui.status printing out of the loop
Siddharth Agarwal <sid0@fb.com>
parents: 18862
diff changeset
  1092
            repo.ui.status(status)
1b70e5941ad7 scmutil.addremove: pull ui.status printing out of the loop
Siddharth Agarwal <sid0@fb.com>
parents: 18862
diff changeset
  1093
19152
7a1292523db3 scmutil.addremove: factor out code to find renames
Siddharth Agarwal <sid0@fb.com>
parents: 19151
diff changeset
  1094
    renames = _findrenames(repo, m, added + unknown, removed + deleted,
7a1292523db3 scmutil.addremove: factor out code to find renames
Siddharth Agarwal <sid0@fb.com>
parents: 19151
diff changeset
  1095
                           similarity)
14320
3438417a6657 scmutil: fold in wdutil
Matt Mackall <mpm@selenic.com>
parents: 14319
diff changeset
  1096
3438417a6657 scmutil: fold in wdutil
Matt Mackall <mpm@selenic.com>
parents: 14319
diff changeset
  1097
    if not dry_run:
23259
9f4778027bc2 addremove: add back forgotten files (BC)
Martin von Zweigbergk <martinvonz@google.com>
parents: 23142
diff changeset
  1098
        _markchanges(repo, unknown + forgotten, deleted, renames)
14320
3438417a6657 scmutil: fold in wdutil
Matt Mackall <mpm@selenic.com>
parents: 14319
diff changeset
  1099
16167
94a8396c9305 addremove: return 1 if we failed to handle any explicit files
Matt Mackall <mpm@selenic.com>
parents: 16115
diff changeset
  1100
    for f in rejected:
94a8396c9305 addremove: return 1 if we failed to handle any explicit files
Matt Mackall <mpm@selenic.com>
parents: 16115
diff changeset
  1101
        if f in m.files():
94a8396c9305 addremove: return 1 if we failed to handle any explicit files
Matt Mackall <mpm@selenic.com>
parents: 16115
diff changeset
  1102
            return 1
23537
f1b06a8aad42 commit: propagate --addremove to subrepos if -S is specified (issue3759)
Matt Harbison <matt_harbison@yahoo.com>
parents: 23534
diff changeset
  1103
    return ret
16167
94a8396c9305 addremove: return 1 if we failed to handle any explicit files
Matt Mackall <mpm@selenic.com>
parents: 16115
diff changeset
  1104
19154
0c7cf411b390 scmutil: add a function to mark that files have been operated on
Siddharth Agarwal <sid0@fb.com>
parents: 19153
diff changeset
  1105
def marktouched(repo, files, similarity=0.0):
0c7cf411b390 scmutil: add a function to mark that files have been operated on
Siddharth Agarwal <sid0@fb.com>
parents: 19153
diff changeset
  1106
    '''Assert that files have somehow been operated upon. files are relative to
0c7cf411b390 scmutil: add a function to mark that files have been operated on
Siddharth Agarwal <sid0@fb.com>
parents: 19153
diff changeset
  1107
    the repo root.'''
25467
f64dbe06f3d0 scmutil: add an optional parameter to matcher factories for a bad() override
Matt Harbison <matt_harbison@yahoo.com>
parents: 25466
diff changeset
  1108
    m = matchfiles(repo, files, badfn=lambda x, y: rejected.append(x))
19154
0c7cf411b390 scmutil: add a function to mark that files have been operated on
Siddharth Agarwal <sid0@fb.com>
parents: 19153
diff changeset
  1109
    rejected = []
0c7cf411b390 scmutil: add a function to mark that files have been operated on
Siddharth Agarwal <sid0@fb.com>
parents: 19153
diff changeset
  1110
23259
9f4778027bc2 addremove: add back forgotten files (BC)
Martin von Zweigbergk <martinvonz@google.com>
parents: 23142
diff changeset
  1111
    added, unknown, deleted, removed, forgotten = _interestingfiles(repo, m)
19154
0c7cf411b390 scmutil: add a function to mark that files have been operated on
Siddharth Agarwal <sid0@fb.com>
parents: 19153
diff changeset
  1112
0c7cf411b390 scmutil: add a function to mark that files have been operated on
Siddharth Agarwal <sid0@fb.com>
parents: 19153
diff changeset
  1113
    if repo.ui.verbose:
23259
9f4778027bc2 addremove: add back forgotten files (BC)
Martin von Zweigbergk <martinvonz@google.com>
parents: 23142
diff changeset
  1114
        unknownset = set(unknown + forgotten)
19154
0c7cf411b390 scmutil: add a function to mark that files have been operated on
Siddharth Agarwal <sid0@fb.com>
parents: 19153
diff changeset
  1115
        toprint = unknownset.copy()
0c7cf411b390 scmutil: add a function to mark that files have been operated on
Siddharth Agarwal <sid0@fb.com>
parents: 19153
diff changeset
  1116
        toprint.update(deleted)
0c7cf411b390 scmutil: add a function to mark that files have been operated on
Siddharth Agarwal <sid0@fb.com>
parents: 19153
diff changeset
  1117
        for abs in sorted(toprint):
0c7cf411b390 scmutil: add a function to mark that files have been operated on
Siddharth Agarwal <sid0@fb.com>
parents: 19153
diff changeset
  1118
            if abs in unknownset:
0c7cf411b390 scmutil: add a function to mark that files have been operated on
Siddharth Agarwal <sid0@fb.com>
parents: 19153
diff changeset
  1119
                status = _('adding %s\n') % abs
0c7cf411b390 scmutil: add a function to mark that files have been operated on
Siddharth Agarwal <sid0@fb.com>
parents: 19153
diff changeset
  1120
            else:
0c7cf411b390 scmutil: add a function to mark that files have been operated on
Siddharth Agarwal <sid0@fb.com>
parents: 19153
diff changeset
  1121
                status = _('removing %s\n') % abs
0c7cf411b390 scmutil: add a function to mark that files have been operated on
Siddharth Agarwal <sid0@fb.com>
parents: 19153
diff changeset
  1122
            repo.ui.status(status)
0c7cf411b390 scmutil: add a function to mark that files have been operated on
Siddharth Agarwal <sid0@fb.com>
parents: 19153
diff changeset
  1123
0c7cf411b390 scmutil: add a function to mark that files have been operated on
Siddharth Agarwal <sid0@fb.com>
parents: 19153
diff changeset
  1124
    renames = _findrenames(repo, m, added + unknown, removed + deleted,
0c7cf411b390 scmutil: add a function to mark that files have been operated on
Siddharth Agarwal <sid0@fb.com>
parents: 19153
diff changeset
  1125
                           similarity)
0c7cf411b390 scmutil: add a function to mark that files have been operated on
Siddharth Agarwal <sid0@fb.com>
parents: 19153
diff changeset
  1126
23259
9f4778027bc2 addremove: add back forgotten files (BC)
Martin von Zweigbergk <martinvonz@google.com>
parents: 23142
diff changeset
  1127
    _markchanges(repo, unknown + forgotten, deleted, renames)
19154
0c7cf411b390 scmutil: add a function to mark that files have been operated on
Siddharth Agarwal <sid0@fb.com>
parents: 19153
diff changeset
  1128
0c7cf411b390 scmutil: add a function to mark that files have been operated on
Siddharth Agarwal <sid0@fb.com>
parents: 19153
diff changeset
  1129
    for f in rejected:
0c7cf411b390 scmutil: add a function to mark that files have been operated on
Siddharth Agarwal <sid0@fb.com>
parents: 19153
diff changeset
  1130
        if f in m.files():
0c7cf411b390 scmutil: add a function to mark that files have been operated on
Siddharth Agarwal <sid0@fb.com>
parents: 19153
diff changeset
  1131
            return 1
0c7cf411b390 scmutil: add a function to mark that files have been operated on
Siddharth Agarwal <sid0@fb.com>
parents: 19153
diff changeset
  1132
    return 0
0c7cf411b390 scmutil: add a function to mark that files have been operated on
Siddharth Agarwal <sid0@fb.com>
parents: 19153
diff changeset
  1133
19150
7a4eab2456de scmutil.addremove: factor out dirstate walk into another function
Siddharth Agarwal <sid0@fb.com>
parents: 19070
diff changeset
  1134
def _interestingfiles(repo, matcher):
7a4eab2456de scmutil.addremove: factor out dirstate walk into another function
Siddharth Agarwal <sid0@fb.com>
parents: 19070
diff changeset
  1135
    '''Walk dirstate with matcher, looking for files that addremove would care
7a4eab2456de scmutil.addremove: factor out dirstate walk into another function
Siddharth Agarwal <sid0@fb.com>
parents: 19070
diff changeset
  1136
    about.
7a4eab2456de scmutil.addremove: factor out dirstate walk into another function
Siddharth Agarwal <sid0@fb.com>
parents: 19070
diff changeset
  1137
7a4eab2456de scmutil.addremove: factor out dirstate walk into another function
Siddharth Agarwal <sid0@fb.com>
parents: 19070
diff changeset
  1138
    This is different from dirstate.status because it doesn't care about
7a4eab2456de scmutil.addremove: factor out dirstate walk into another function
Siddharth Agarwal <sid0@fb.com>
parents: 19070
diff changeset
  1139
    whether files are modified or clean.'''
23259
9f4778027bc2 addremove: add back forgotten files (BC)
Martin von Zweigbergk <martinvonz@google.com>
parents: 23142
diff changeset
  1140
    added, unknown, deleted, removed, forgotten = [], [], [], [], []
20033
f962870712da pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents: 20006
diff changeset
  1141
    audit_path = pathutil.pathauditor(repo.root)
19150
7a4eab2456de scmutil.addremove: factor out dirstate walk into another function
Siddharth Agarwal <sid0@fb.com>
parents: 19070
diff changeset
  1142
7a4eab2456de scmutil.addremove: factor out dirstate walk into another function
Siddharth Agarwal <sid0@fb.com>
parents: 19070
diff changeset
  1143
    ctx = repo[None]
7a4eab2456de scmutil.addremove: factor out dirstate walk into another function
Siddharth Agarwal <sid0@fb.com>
parents: 19070
diff changeset
  1144
    dirstate = repo.dirstate
19655
1d07bf106c2a addremove: don't do full walks
Siddharth Agarwal <sid0@fb.com>
parents: 19509
diff changeset
  1145
    walkresults = dirstate.walk(matcher, sorted(ctx.substate), True, False,
1d07bf106c2a addremove: don't do full walks
Siddharth Agarwal <sid0@fb.com>
parents: 19509
diff changeset
  1146
                                full=False)
19150
7a4eab2456de scmutil.addremove: factor out dirstate walk into another function
Siddharth Agarwal <sid0@fb.com>
parents: 19070
diff changeset
  1147
    for abs, st in walkresults.iteritems():
7a4eab2456de scmutil.addremove: factor out dirstate walk into another function
Siddharth Agarwal <sid0@fb.com>
parents: 19070
diff changeset
  1148
        dstate = dirstate[abs]
7a4eab2456de scmutil.addremove: factor out dirstate walk into another function
Siddharth Agarwal <sid0@fb.com>
parents: 19070
diff changeset
  1149
        if dstate == '?' and audit_path.check(abs):
7a4eab2456de scmutil.addremove: factor out dirstate walk into another function
Siddharth Agarwal <sid0@fb.com>
parents: 19070
diff changeset
  1150
            unknown.append(abs)
7a4eab2456de scmutil.addremove: factor out dirstate walk into another function
Siddharth Agarwal <sid0@fb.com>
parents: 19070
diff changeset
  1151
        elif dstate != 'r' and not st:
7a4eab2456de scmutil.addremove: factor out dirstate walk into another function
Siddharth Agarwal <sid0@fb.com>
parents: 19070
diff changeset
  1152
            deleted.append(abs)
23259
9f4778027bc2 addremove: add back forgotten files (BC)
Martin von Zweigbergk <martinvonz@google.com>
parents: 23142
diff changeset
  1153
        elif dstate == 'r' and st:
9f4778027bc2 addremove: add back forgotten files (BC)
Martin von Zweigbergk <martinvonz@google.com>
parents: 23142
diff changeset
  1154
            forgotten.append(abs)
19150
7a4eab2456de scmutil.addremove: factor out dirstate walk into another function
Siddharth Agarwal <sid0@fb.com>
parents: 19070
diff changeset
  1155
        # for finding renames
23259
9f4778027bc2 addremove: add back forgotten files (BC)
Martin von Zweigbergk <martinvonz@google.com>
parents: 23142
diff changeset
  1156
        elif dstate == 'r' and not st:
19150
7a4eab2456de scmutil.addremove: factor out dirstate walk into another function
Siddharth Agarwal <sid0@fb.com>
parents: 19070
diff changeset
  1157
            removed.append(abs)
7a4eab2456de scmutil.addremove: factor out dirstate walk into another function
Siddharth Agarwal <sid0@fb.com>
parents: 19070
diff changeset
  1158
        elif dstate == 'a':
7a4eab2456de scmutil.addremove: factor out dirstate walk into another function
Siddharth Agarwal <sid0@fb.com>
parents: 19070
diff changeset
  1159
            added.append(abs)
7a4eab2456de scmutil.addremove: factor out dirstate walk into another function
Siddharth Agarwal <sid0@fb.com>
parents: 19070
diff changeset
  1160
23259
9f4778027bc2 addremove: add back forgotten files (BC)
Martin von Zweigbergk <martinvonz@google.com>
parents: 23142
diff changeset
  1161
    return added, unknown, deleted, removed, forgotten
19150
7a4eab2456de scmutil.addremove: factor out dirstate walk into another function
Siddharth Agarwal <sid0@fb.com>
parents: 19070
diff changeset
  1162
19152
7a1292523db3 scmutil.addremove: factor out code to find renames
Siddharth Agarwal <sid0@fb.com>
parents: 19151
diff changeset
  1163
def _findrenames(repo, matcher, added, removed, similarity):
7a1292523db3 scmutil.addremove: factor out code to find renames
Siddharth Agarwal <sid0@fb.com>
parents: 19151
diff changeset
  1164
    '''Find renames from removed files to added ones.'''
7a1292523db3 scmutil.addremove: factor out code to find renames
Siddharth Agarwal <sid0@fb.com>
parents: 19151
diff changeset
  1165
    renames = {}
7a1292523db3 scmutil.addremove: factor out code to find renames
Siddharth Agarwal <sid0@fb.com>
parents: 19151
diff changeset
  1166
    if similarity > 0:
7a1292523db3 scmutil.addremove: factor out code to find renames
Siddharth Agarwal <sid0@fb.com>
parents: 19151
diff changeset
  1167
        for old, new, score in similar.findrenames(repo, added, removed,
7a1292523db3 scmutil.addremove: factor out code to find renames
Siddharth Agarwal <sid0@fb.com>
parents: 19151
diff changeset
  1168
                                                   similarity):
7a1292523db3 scmutil.addremove: factor out code to find renames
Siddharth Agarwal <sid0@fb.com>
parents: 19151
diff changeset
  1169
            if (repo.ui.verbose or not matcher.exact(old)
7a1292523db3 scmutil.addremove: factor out code to find renames
Siddharth Agarwal <sid0@fb.com>
parents: 19151
diff changeset
  1170
                or not matcher.exact(new)):
7a1292523db3 scmutil.addremove: factor out code to find renames
Siddharth Agarwal <sid0@fb.com>
parents: 19151
diff changeset
  1171
                repo.ui.status(_('recording removal of %s as rename to %s '
7a1292523db3 scmutil.addremove: factor out code to find renames
Siddharth Agarwal <sid0@fb.com>
parents: 19151
diff changeset
  1172
                                 '(%d%% similar)\n') %
7a1292523db3 scmutil.addremove: factor out code to find renames
Siddharth Agarwal <sid0@fb.com>
parents: 19151
diff changeset
  1173
                               (matcher.rel(old), matcher.rel(new),
7a1292523db3 scmutil.addremove: factor out code to find renames
Siddharth Agarwal <sid0@fb.com>
parents: 19151
diff changeset
  1174
                                score * 100))
7a1292523db3 scmutil.addremove: factor out code to find renames
Siddharth Agarwal <sid0@fb.com>
parents: 19151
diff changeset
  1175
            renames[new] = old
7a1292523db3 scmutil.addremove: factor out code to find renames
Siddharth Agarwal <sid0@fb.com>
parents: 19151
diff changeset
  1176
    return renames
7a1292523db3 scmutil.addremove: factor out code to find renames
Siddharth Agarwal <sid0@fb.com>
parents: 19151
diff changeset
  1177
19153
9a4e219bda89 scmutil.addremove: factor out code to mark added/removed/renames
Siddharth Agarwal <sid0@fb.com>
parents: 19152
diff changeset
  1178
def _markchanges(repo, unknown, deleted, renames):
9a4e219bda89 scmutil.addremove: factor out code to mark added/removed/renames
Siddharth Agarwal <sid0@fb.com>
parents: 19152
diff changeset
  1179
    '''Marks the files in unknown as added, the files in deleted as removed,
9a4e219bda89 scmutil.addremove: factor out code to mark added/removed/renames
Siddharth Agarwal <sid0@fb.com>
parents: 19152
diff changeset
  1180
    and the files in renames as copied.'''
9a4e219bda89 scmutil.addremove: factor out code to mark added/removed/renames
Siddharth Agarwal <sid0@fb.com>
parents: 19152
diff changeset
  1181
    wctx = repo[None]
27851
4133a306606c with: use context manager in _markchanges
Bryan O'Sullivan <bryano@fb.com>
parents: 27706
diff changeset
  1182
    with repo.wlock():
19153
9a4e219bda89 scmutil.addremove: factor out code to mark added/removed/renames
Siddharth Agarwal <sid0@fb.com>
parents: 19152
diff changeset
  1183
        wctx.forget(deleted)
9a4e219bda89 scmutil.addremove: factor out code to mark added/removed/renames
Siddharth Agarwal <sid0@fb.com>
parents: 19152
diff changeset
  1184
        wctx.add(unknown)
9a4e219bda89 scmutil.addremove: factor out code to mark added/removed/renames
Siddharth Agarwal <sid0@fb.com>
parents: 19152
diff changeset
  1185
        for new, old in renames.iteritems():
9a4e219bda89 scmutil.addremove: factor out code to mark added/removed/renames
Siddharth Agarwal <sid0@fb.com>
parents: 19152
diff changeset
  1186
            wctx.copy(old, new)
9a4e219bda89 scmutil.addremove: factor out code to mark added/removed/renames
Siddharth Agarwal <sid0@fb.com>
parents: 19152
diff changeset
  1187
14320
3438417a6657 scmutil: fold in wdutil
Matt Mackall <mpm@selenic.com>
parents: 14319
diff changeset
  1188
def dirstatecopy(ui, repo, wctx, src, dst, dryrun=False, cwd=None):
3438417a6657 scmutil: fold in wdutil
Matt Mackall <mpm@selenic.com>
parents: 14319
diff changeset
  1189
    """Update the dirstate to reflect the intent of copying src to dst. For
3438417a6657 scmutil: fold in wdutil
Matt Mackall <mpm@selenic.com>
parents: 14319
diff changeset
  1190
    different reasons it might not end with dst being marked as copied from src.
3438417a6657 scmutil: fold in wdutil
Matt Mackall <mpm@selenic.com>
parents: 14319
diff changeset
  1191
    """
3438417a6657 scmutil: fold in wdutil
Matt Mackall <mpm@selenic.com>
parents: 14319
diff changeset
  1192
    origsrc = repo.dirstate.copied(src) or src
3438417a6657 scmutil: fold in wdutil
Matt Mackall <mpm@selenic.com>
parents: 14319
diff changeset
  1193
    if dst == origsrc: # copying back a copy?
3438417a6657 scmutil: fold in wdutil
Matt Mackall <mpm@selenic.com>
parents: 14319
diff changeset
  1194
        if repo.dirstate[dst] not in 'mn' and not dryrun:
3438417a6657 scmutil: fold in wdutil
Matt Mackall <mpm@selenic.com>
parents: 14319
diff changeset
  1195
            repo.dirstate.normallookup(dst)
3438417a6657 scmutil: fold in wdutil
Matt Mackall <mpm@selenic.com>
parents: 14319
diff changeset
  1196
    else:
3438417a6657 scmutil: fold in wdutil
Matt Mackall <mpm@selenic.com>
parents: 14319
diff changeset
  1197
        if repo.dirstate[origsrc] == 'a' and origsrc == src:
3438417a6657 scmutil: fold in wdutil
Matt Mackall <mpm@selenic.com>
parents: 14319
diff changeset
  1198
            if not ui.quiet:
3438417a6657 scmutil: fold in wdutil
Matt Mackall <mpm@selenic.com>
parents: 14319
diff changeset
  1199
                ui.warn(_("%s has not been committed yet, so no copy "
3438417a6657 scmutil: fold in wdutil
Matt Mackall <mpm@selenic.com>
parents: 14319
diff changeset
  1200
                          "data will be stored for %s.\n")
3438417a6657 scmutil: fold in wdutil
Matt Mackall <mpm@selenic.com>
parents: 14319
diff changeset
  1201
                        % (repo.pathto(origsrc, cwd), repo.pathto(dst, cwd)))
3438417a6657 scmutil: fold in wdutil
Matt Mackall <mpm@selenic.com>
parents: 14319
diff changeset
  1202
            if repo.dirstate[dst] in '?r' and not dryrun:
3438417a6657 scmutil: fold in wdutil
Matt Mackall <mpm@selenic.com>
parents: 14319
diff changeset
  1203
                wctx.add([dst])
3438417a6657 scmutil: fold in wdutil
Matt Mackall <mpm@selenic.com>
parents: 14319
diff changeset
  1204
        elif not dryrun:
3438417a6657 scmutil: fold in wdutil
Matt Mackall <mpm@selenic.com>
parents: 14319
diff changeset
  1205
            wctx.copy(origsrc, dst)
14482
58b36e9ea783 introduce new function scmutil.readrequires
Adrian Buehlmann <adrian@cadifra.com>
parents: 14435
diff changeset
  1206
58b36e9ea783 introduce new function scmutil.readrequires
Adrian Buehlmann <adrian@cadifra.com>
parents: 14435
diff changeset
  1207
def readrequires(opener, supported):
58b36e9ea783 introduce new function scmutil.readrequires
Adrian Buehlmann <adrian@cadifra.com>
parents: 14435
diff changeset
  1208
    '''Reads and parses .hg/requires and checks if all entries found
58b36e9ea783 introduce new function scmutil.readrequires
Adrian Buehlmann <adrian@cadifra.com>
parents: 14435
diff changeset
  1209
    are in the list of supported features.'''
58b36e9ea783 introduce new function scmutil.readrequires
Adrian Buehlmann <adrian@cadifra.com>
parents: 14435
diff changeset
  1210
    requirements = set(opener.read("requires").splitlines())
14746
72e4fcb43227 requirements: show all missing features in the error message.
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 14720
diff changeset
  1211
    missings = []
14482
58b36e9ea783 introduce new function scmutil.readrequires
Adrian Buehlmann <adrian@cadifra.com>
parents: 14435
diff changeset
  1212
    for r in requirements:
58b36e9ea783 introduce new function scmutil.readrequires
Adrian Buehlmann <adrian@cadifra.com>
parents: 14435
diff changeset
  1213
        if r not in supported:
14484
4582a4dd1817 requires: note apparent corruption
Matt Mackall <mpm@selenic.com>
parents: 14483
diff changeset
  1214
            if not r or not r[0].isalnum():
4582a4dd1817 requires: note apparent corruption
Matt Mackall <mpm@selenic.com>
parents: 14483
diff changeset
  1215
                raise error.RequirementError(_(".hg/requires file is corrupt"))
14746
72e4fcb43227 requirements: show all missing features in the error message.
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 14720
diff changeset
  1216
            missings.append(r)
72e4fcb43227 requirements: show all missing features in the error message.
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 14720
diff changeset
  1217
    missings.sort()
72e4fcb43227 requirements: show all missing features in the error message.
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 14720
diff changeset
  1218
    if missings:
16683
525fdb738975 cleanup: eradicate long lines
Brodie Rao <brodie@sf.io>
parents: 16479
diff changeset
  1219
        raise error.RequirementError(
20820
f8e531a3a77c repo: rephrase the "missing requirement" error message
Mads Kiilerich <madski@unity3d.com>
parents: 20819
diff changeset
  1220
            _("repository requires features unknown to this Mercurial: %s")
f8e531a3a77c repo: rephrase the "missing requirement" error message
Mads Kiilerich <madski@unity3d.com>
parents: 20819
diff changeset
  1221
            % " ".join(missings),
26421
4b0fc75f9403 urls: bulk-change primary website URLs
Matt Mackall <mpm@selenic.com>
parents: 26329
diff changeset
  1222
            hint=_("see https://mercurial-scm.org/wiki/MissingRequirement"
20820
f8e531a3a77c repo: rephrase the "missing requirement" error message
Mads Kiilerich <madski@unity3d.com>
parents: 20819
diff changeset
  1223
                   " for more information"))
14482
58b36e9ea783 introduce new function scmutil.readrequires
Adrian Buehlmann <adrian@cadifra.com>
parents: 14435
diff changeset
  1224
    return requirements
14928
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents: 14861
diff changeset
  1225
24934
5abd0a76bc8f requires: move requires file writing func from localrepo to scmutil
Drew Gottlieb <drgott@google.com>
parents: 24755
diff changeset
  1226
def writerequires(opener, requirements):
27706
22e362da27cf scmutil: use context managers for file handles
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27651
diff changeset
  1227
    with opener('requires', 'w') as fp:
22e362da27cf scmutil: use context managers for file handles
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27651
diff changeset
  1228
        for r in sorted(requirements):
22e362da27cf scmutil: use context managers for file handles
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27651
diff changeset
  1229
            fp.write("%s\n" % r)
24934
5abd0a76bc8f requires: move requires file writing func from localrepo to scmutil
Drew Gottlieb <drgott@google.com>
parents: 24755
diff changeset
  1230
20043
88bd8df008f2 scmutil: rename filecacheentry to filecachesubentry
Siddharth Agarwal <sid0@fb.com>
parents: 20042
diff changeset
  1231
class filecachesubentry(object):
20042
9a72d3886888 scmutil.filecacheentry: make stat argument to constructor mandatory
Siddharth Agarwal <sid0@fb.com>
parents: 20033
diff changeset
  1232
    def __init__(self, path, stat):
14928
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents: 14861
diff changeset
  1233
        self.path = path
18315
216230643ae2 filecache: allow filecacheentry to be created without stating in __init__
Idan Kamara <idankk86@gmail.com>
parents: 18213
diff changeset
  1234
        self.cachestat = None
216230643ae2 filecache: allow filecacheentry to be created without stating in __init__
Idan Kamara <idankk86@gmail.com>
parents: 18213
diff changeset
  1235
        self._cacheable = None
14928
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents: 14861
diff changeset
  1236
18315
216230643ae2 filecache: allow filecacheentry to be created without stating in __init__
Idan Kamara <idankk86@gmail.com>
parents: 18213
diff changeset
  1237
        if stat:
20043
88bd8df008f2 scmutil: rename filecacheentry to filecachesubentry
Siddharth Agarwal <sid0@fb.com>
parents: 20042
diff changeset
  1238
            self.cachestat = filecachesubentry.stat(self.path)
18315
216230643ae2 filecache: allow filecacheentry to be created without stating in __init__
Idan Kamara <idankk86@gmail.com>
parents: 18213
diff changeset
  1239
216230643ae2 filecache: allow filecacheentry to be created without stating in __init__
Idan Kamara <idankk86@gmail.com>
parents: 18213
diff changeset
  1240
            if self.cachestat:
216230643ae2 filecache: allow filecacheentry to be created without stating in __init__
Idan Kamara <idankk86@gmail.com>
parents: 18213
diff changeset
  1241
                self._cacheable = self.cachestat.cacheable()
216230643ae2 filecache: allow filecacheentry to be created without stating in __init__
Idan Kamara <idankk86@gmail.com>
parents: 18213
diff changeset
  1242
            else:
216230643ae2 filecache: allow filecacheentry to be created without stating in __init__
Idan Kamara <idankk86@gmail.com>
parents: 18213
diff changeset
  1243
                # None means we don't know yet
216230643ae2 filecache: allow filecacheentry to be created without stating in __init__
Idan Kamara <idankk86@gmail.com>
parents: 18213
diff changeset
  1244
                self._cacheable = None
14928
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents: 14861
diff changeset
  1245
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents: 14861
diff changeset
  1246
    def refresh(self):
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents: 14861
diff changeset
  1247
        if self.cacheable():
20043
88bd8df008f2 scmutil: rename filecacheentry to filecachesubentry
Siddharth Agarwal <sid0@fb.com>
parents: 20042
diff changeset
  1248
            self.cachestat = filecachesubentry.stat(self.path)
14928
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents: 14861
diff changeset
  1249
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents: 14861
diff changeset
  1250
    def cacheable(self):
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents: 14861
diff changeset
  1251
        if self._cacheable is not None:
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents: 14861
diff changeset
  1252
            return self._cacheable
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents: 14861
diff changeset
  1253
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents: 14861
diff changeset
  1254
        # we don't know yet, assume it is for now
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents: 14861
diff changeset
  1255
        return True
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents: 14861
diff changeset
  1256
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents: 14861
diff changeset
  1257
    def changed(self):
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents: 14861
diff changeset
  1258
        # no point in going further if we can't cache it
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents: 14861
diff changeset
  1259
        if not self.cacheable():
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents: 14861
diff changeset
  1260
            return True
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents: 14861
diff changeset
  1261
20043
88bd8df008f2 scmutil: rename filecacheentry to filecachesubentry
Siddharth Agarwal <sid0@fb.com>
parents: 20042
diff changeset
  1262
        newstat = filecachesubentry.stat(self.path)
14928
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents: 14861
diff changeset
  1263
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents: 14861
diff changeset
  1264
        # we may not know if it's cacheable yet, check again now
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents: 14861
diff changeset
  1265
        if newstat and self._cacheable is None:
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents: 14861
diff changeset
  1266
            self._cacheable = newstat.cacheable()
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents: 14861
diff changeset
  1267
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents: 14861
diff changeset
  1268
            # check again
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents: 14861
diff changeset
  1269
            if not self._cacheable:
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents: 14861
diff changeset
  1270
                return True
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents: 14861
diff changeset
  1271
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents: 14861
diff changeset
  1272
        if self.cachestat != newstat:
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents: 14861
diff changeset
  1273
            self.cachestat = newstat
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents: 14861
diff changeset
  1274
            return True
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents: 14861
diff changeset
  1275
        else:
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents: 14861
diff changeset
  1276
            return False
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents: 14861
diff changeset
  1277
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents: 14861
diff changeset
  1278
    @staticmethod
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents: 14861
diff changeset
  1279
    def stat(path):
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents: 14861
diff changeset
  1280
        try:
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents: 14861
diff changeset
  1281
            return util.cachestat(path)
25660
328739ea70c3 global: mass rewrite to use modern exception syntax
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25658
diff changeset
  1282
        except OSError as e:
14928
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents: 14861
diff changeset
  1283
            if e.errno != errno.ENOENT:
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents: 14861
diff changeset
  1284
                raise
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents: 14861
diff changeset
  1285
20044
d38de18d187a scmutil: introduce a filecacheentry that can watch multiple paths
Siddharth Agarwal <sid0@fb.com>
parents: 20043
diff changeset
  1286
class filecacheentry(object):
d38de18d187a scmutil: introduce a filecacheentry that can watch multiple paths
Siddharth Agarwal <sid0@fb.com>
parents: 20043
diff changeset
  1287
    def __init__(self, paths, stat=True):
d38de18d187a scmutil: introduce a filecacheentry that can watch multiple paths
Siddharth Agarwal <sid0@fb.com>
parents: 20043
diff changeset
  1288
        self._entries = []
d38de18d187a scmutil: introduce a filecacheentry that can watch multiple paths
Siddharth Agarwal <sid0@fb.com>
parents: 20043
diff changeset
  1289
        for path in paths:
d38de18d187a scmutil: introduce a filecacheentry that can watch multiple paths
Siddharth Agarwal <sid0@fb.com>
parents: 20043
diff changeset
  1290
            self._entries.append(filecachesubentry(path, stat))
d38de18d187a scmutil: introduce a filecacheentry that can watch multiple paths
Siddharth Agarwal <sid0@fb.com>
parents: 20043
diff changeset
  1291
d38de18d187a scmutil: introduce a filecacheentry that can watch multiple paths
Siddharth Agarwal <sid0@fb.com>
parents: 20043
diff changeset
  1292
    def changed(self):
d38de18d187a scmutil: introduce a filecacheentry that can watch multiple paths
Siddharth Agarwal <sid0@fb.com>
parents: 20043
diff changeset
  1293
        '''true if any entry has changed'''
d38de18d187a scmutil: introduce a filecacheentry that can watch multiple paths
Siddharth Agarwal <sid0@fb.com>
parents: 20043
diff changeset
  1294
        for entry in self._entries:
d38de18d187a scmutil: introduce a filecacheentry that can watch multiple paths
Siddharth Agarwal <sid0@fb.com>
parents: 20043
diff changeset
  1295
            if entry.changed():
d38de18d187a scmutil: introduce a filecacheentry that can watch multiple paths
Siddharth Agarwal <sid0@fb.com>
parents: 20043
diff changeset
  1296
                return True
d38de18d187a scmutil: introduce a filecacheentry that can watch multiple paths
Siddharth Agarwal <sid0@fb.com>
parents: 20043
diff changeset
  1297
        return False
d38de18d187a scmutil: introduce a filecacheentry that can watch multiple paths
Siddharth Agarwal <sid0@fb.com>
parents: 20043
diff changeset
  1298
d38de18d187a scmutil: introduce a filecacheentry that can watch multiple paths
Siddharth Agarwal <sid0@fb.com>
parents: 20043
diff changeset
  1299
    def refresh(self):
d38de18d187a scmutil: introduce a filecacheentry that can watch multiple paths
Siddharth Agarwal <sid0@fb.com>
parents: 20043
diff changeset
  1300
        for entry in self._entries:
d38de18d187a scmutil: introduce a filecacheentry that can watch multiple paths
Siddharth Agarwal <sid0@fb.com>
parents: 20043
diff changeset
  1301
            entry.refresh()
d38de18d187a scmutil: introduce a filecacheentry that can watch multiple paths
Siddharth Agarwal <sid0@fb.com>
parents: 20043
diff changeset
  1302
14928
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents: 14861
diff changeset
  1303
class filecache(object):
20045
b3684fd2ff1a scmutil.filecache: support watching over multiple files
Siddharth Agarwal <sid0@fb.com>
parents: 20044
diff changeset
  1304
    '''A property like decorator that tracks files under .hg/ for updates.
14928
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents: 14861
diff changeset
  1305
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents: 14861
diff changeset
  1306
    Records stat info when called in _filecache.
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents: 14861
diff changeset
  1307
20045
b3684fd2ff1a scmutil.filecache: support watching over multiple files
Siddharth Agarwal <sid0@fb.com>
parents: 20044
diff changeset
  1308
    On subsequent calls, compares old stat info with new info, and recreates the
b3684fd2ff1a scmutil.filecache: support watching over multiple files
Siddharth Agarwal <sid0@fb.com>
parents: 20044
diff changeset
  1309
    object when any of the files changes, updating the new stat info in
b3684fd2ff1a scmutil.filecache: support watching over multiple files
Siddharth Agarwal <sid0@fb.com>
parents: 20044
diff changeset
  1310
    _filecache.
14928
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents: 14861
diff changeset
  1311
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents: 14861
diff changeset
  1312
    Mercurial either atomic renames or appends for files under .hg,
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents: 14861
diff changeset
  1313
    so to ensure the cache is reliable we need the filesystem to be able
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents: 14861
diff changeset
  1314
    to tell us if a file has been replaced. If it can't, we fallback to
26098
ce26928cbe41 spelling: behaviour -> behavior
timeless@mozdev.org
parents: 26020
diff changeset
  1315
    recreating the object on every call (essentially the same behavior as
20045
b3684fd2ff1a scmutil.filecache: support watching over multiple files
Siddharth Agarwal <sid0@fb.com>
parents: 20044
diff changeset
  1316
    propertycache).
b3684fd2ff1a scmutil.filecache: support watching over multiple files
Siddharth Agarwal <sid0@fb.com>
parents: 20044
diff changeset
  1317
b3684fd2ff1a scmutil.filecache: support watching over multiple files
Siddharth Agarwal <sid0@fb.com>
parents: 20044
diff changeset
  1318
    '''
b3684fd2ff1a scmutil.filecache: support watching over multiple files
Siddharth Agarwal <sid0@fb.com>
parents: 20044
diff changeset
  1319
    def __init__(self, *paths):
b3684fd2ff1a scmutil.filecache: support watching over multiple files
Siddharth Agarwal <sid0@fb.com>
parents: 20044
diff changeset
  1320
        self.paths = paths
16198
fa8488565afd filecache: refactor path join logic to a function
Idan Kamara <idankk86@gmail.com>
parents: 16115
diff changeset
  1321
fa8488565afd filecache: refactor path join logic to a function
Idan Kamara <idankk86@gmail.com>
parents: 16115
diff changeset
  1322
    def join(self, obj, fname):
20045
b3684fd2ff1a scmutil.filecache: support watching over multiple files
Siddharth Agarwal <sid0@fb.com>
parents: 20044
diff changeset
  1323
        """Used to compute the runtime path of a cached file.
16198
fa8488565afd filecache: refactor path join logic to a function
Idan Kamara <idankk86@gmail.com>
parents: 16115
diff changeset
  1324
fa8488565afd filecache: refactor path join logic to a function
Idan Kamara <idankk86@gmail.com>
parents: 16115
diff changeset
  1325
        Users should subclass filecache and provide their own version of this
fa8488565afd filecache: refactor path join logic to a function
Idan Kamara <idankk86@gmail.com>
parents: 16115
diff changeset
  1326
        function to call the appropriate join function on 'obj' (an instance
fa8488565afd filecache: refactor path join logic to a function
Idan Kamara <idankk86@gmail.com>
parents: 16115
diff changeset
  1327
        of the class that its member function was decorated).
fa8488565afd filecache: refactor path join logic to a function
Idan Kamara <idankk86@gmail.com>
parents: 16115
diff changeset
  1328
        """
fa8488565afd filecache: refactor path join logic to a function
Idan Kamara <idankk86@gmail.com>
parents: 16115
diff changeset
  1329
        return obj.join(fname)
14928
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents: 14861
diff changeset
  1330
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents: 14861
diff changeset
  1331
    def __call__(self, func):
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents: 14861
diff changeset
  1332
        self.func = func
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents: 14861
diff changeset
  1333
        self.name = func.__name__
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents: 14861
diff changeset
  1334
        return self
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents: 14861
diff changeset
  1335
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents: 14861
diff changeset
  1336
    def __get__(self, obj, type=None):
29373
36fbd72c2f39 scmutil: allow access to filecache descriptor on class
Martijn Pieters <mjpieters@fb.com>
parents: 29367
diff changeset
  1337
        # if accessed on the class, return the descriptor itself.
36fbd72c2f39 scmutil: allow access to filecache descriptor on class
Martijn Pieters <mjpieters@fb.com>
parents: 29367
diff changeset
  1338
        if obj is None:
36fbd72c2f39 scmutil: allow access to filecache descriptor on class
Martijn Pieters <mjpieters@fb.com>
parents: 29367
diff changeset
  1339
            return self
16115
236bb604dc39 scmutil: update cached copy when filecached attribute is assigned (issue3263)
Idan Kamara <idankk86@gmail.com>
parents: 16068
diff changeset
  1340
        # do we need to check if the file changed?
236bb604dc39 scmutil: update cached copy when filecached attribute is assigned (issue3263)
Idan Kamara <idankk86@gmail.com>
parents: 16068
diff changeset
  1341
        if self.name in obj.__dict__:
18316
f36375576ed5 filecache: create an entry in _filecache when __set__ is called for a missing one
Idan Kamara <idankk86@gmail.com>
parents: 18315
diff changeset
  1342
            assert self.name in obj._filecache, self.name
16115
236bb604dc39 scmutil: update cached copy when filecached attribute is assigned (issue3263)
Idan Kamara <idankk86@gmail.com>
parents: 16068
diff changeset
  1343
            return obj.__dict__[self.name]
236bb604dc39 scmutil: update cached copy when filecached attribute is assigned (issue3263)
Idan Kamara <idankk86@gmail.com>
parents: 16068
diff changeset
  1344
14928
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents: 14861
diff changeset
  1345
        entry = obj._filecache.get(self.name)
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents: 14861
diff changeset
  1346
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents: 14861
diff changeset
  1347
        if entry:
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents: 14861
diff changeset
  1348
            if entry.changed():
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents: 14861
diff changeset
  1349
                entry.obj = self.func(obj)
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents: 14861
diff changeset
  1350
        else:
20045
b3684fd2ff1a scmutil.filecache: support watching over multiple files
Siddharth Agarwal <sid0@fb.com>
parents: 20044
diff changeset
  1351
            paths = [self.join(obj, path) for path in self.paths]
14928
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents: 14861
diff changeset
  1352
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents: 14861
diff changeset
  1353
            # We stat -before- creating the object so our cache doesn't lie if
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents: 14861
diff changeset
  1354
            # a writer modified between the time we read and stat
20045
b3684fd2ff1a scmutil.filecache: support watching over multiple files
Siddharth Agarwal <sid0@fb.com>
parents: 20044
diff changeset
  1355
            entry = filecacheentry(paths, True)
14928
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents: 14861
diff changeset
  1356
            entry.obj = self.func(obj)
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents: 14861
diff changeset
  1357
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents: 14861
diff changeset
  1358
            obj._filecache[self.name] = entry
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents: 14861
diff changeset
  1359
16115
236bb604dc39 scmutil: update cached copy when filecached attribute is assigned (issue3263)
Idan Kamara <idankk86@gmail.com>
parents: 16068
diff changeset
  1360
        obj.__dict__[self.name] = entry.obj
14928
dca59d5be12d scmutil: introduce filecache
Idan Kamara <idankk86@gmail.com>
parents: 14861
diff changeset
  1361
        return entry.obj
16115
236bb604dc39 scmutil: update cached copy when filecached attribute is assigned (issue3263)
Idan Kamara <idankk86@gmail.com>
parents: 16068
diff changeset
  1362
236bb604dc39 scmutil: update cached copy when filecached attribute is assigned (issue3263)
Idan Kamara <idankk86@gmail.com>
parents: 16068
diff changeset
  1363
    def __set__(self, obj, value):
18316
f36375576ed5 filecache: create an entry in _filecache when __set__ is called for a missing one
Idan Kamara <idankk86@gmail.com>
parents: 18315
diff changeset
  1364
        if self.name not in obj._filecache:
f36375576ed5 filecache: create an entry in _filecache when __set__ is called for a missing one
Idan Kamara <idankk86@gmail.com>
parents: 18315
diff changeset
  1365
            # we add an entry for the missing value because X in __dict__
f36375576ed5 filecache: create an entry in _filecache when __set__ is called for a missing one
Idan Kamara <idankk86@gmail.com>
parents: 18315
diff changeset
  1366
            # implies X in _filecache
20045
b3684fd2ff1a scmutil.filecache: support watching over multiple files
Siddharth Agarwal <sid0@fb.com>
parents: 20044
diff changeset
  1367
            paths = [self.join(obj, path) for path in self.paths]
b3684fd2ff1a scmutil.filecache: support watching over multiple files
Siddharth Agarwal <sid0@fb.com>
parents: 20044
diff changeset
  1368
            ce = filecacheentry(paths, False)
18316
f36375576ed5 filecache: create an entry in _filecache when __set__ is called for a missing one
Idan Kamara <idankk86@gmail.com>
parents: 18315
diff changeset
  1369
            obj._filecache[self.name] = ce
f36375576ed5 filecache: create an entry in _filecache when __set__ is called for a missing one
Idan Kamara <idankk86@gmail.com>
parents: 18315
diff changeset
  1370
        else:
f36375576ed5 filecache: create an entry in _filecache when __set__ is called for a missing one
Idan Kamara <idankk86@gmail.com>
parents: 18315
diff changeset
  1371
            ce = obj._filecache[self.name]
f36375576ed5 filecache: create an entry in _filecache when __set__ is called for a missing one
Idan Kamara <idankk86@gmail.com>
parents: 18315
diff changeset
  1372
f36375576ed5 filecache: create an entry in _filecache when __set__ is called for a missing one
Idan Kamara <idankk86@gmail.com>
parents: 18315
diff changeset
  1373
        ce.obj = value # update cached copy
16115
236bb604dc39 scmutil: update cached copy when filecached attribute is assigned (issue3263)
Idan Kamara <idankk86@gmail.com>
parents: 16068
diff changeset
  1374
        obj.__dict__[self.name] = value # update copy returned by obj.x
236bb604dc39 scmutil: update cached copy when filecached attribute is assigned (issue3263)
Idan Kamara <idankk86@gmail.com>
parents: 16068
diff changeset
  1375
236bb604dc39 scmutil: update cached copy when filecached attribute is assigned (issue3263)
Idan Kamara <idankk86@gmail.com>
parents: 16068
diff changeset
  1376
    def __delete__(self, obj):
236bb604dc39 scmutil: update cached copy when filecached attribute is assigned (issue3263)
Idan Kamara <idankk86@gmail.com>
parents: 16068
diff changeset
  1377
        try:
236bb604dc39 scmutil: update cached copy when filecached attribute is assigned (issue3263)
Idan Kamara <idankk86@gmail.com>
parents: 16068
diff changeset
  1378
            del obj.__dict__[self.name]
236bb604dc39 scmutil: update cached copy when filecached attribute is assigned (issue3263)
Idan Kamara <idankk86@gmail.com>
parents: 16068
diff changeset
  1379
        except KeyError:
18177
203b7a759218 scmutil: clean up use of two-argument raise
Augie Fackler <raf@durin42.com>
parents: 17992
diff changeset
  1380
            raise AttributeError(self.name)
26490
f0d730efb02f scmutil: add a way for a subprocess to be run with an inheritable lock
Siddharth Agarwal <sid0@fb.com>
parents: 26433
diff changeset
  1381
f0d730efb02f scmutil: add a way for a subprocess to be run with an inheritable lock
Siddharth Agarwal <sid0@fb.com>
parents: 26433
diff changeset
  1382
def _locksub(repo, lock, envvar, cmd, environ=None, *args, **kwargs):
f0d730efb02f scmutil: add a way for a subprocess to be run with an inheritable lock
Siddharth Agarwal <sid0@fb.com>
parents: 26433
diff changeset
  1383
    if lock is None:
f0d730efb02f scmutil: add a way for a subprocess to be run with an inheritable lock
Siddharth Agarwal <sid0@fb.com>
parents: 26433
diff changeset
  1384
        raise error.LockInheritanceContractViolation(
f0d730efb02f scmutil: add a way for a subprocess to be run with an inheritable lock
Siddharth Agarwal <sid0@fb.com>
parents: 26433
diff changeset
  1385
            'lock can only be inherited while held')
f0d730efb02f scmutil: add a way for a subprocess to be run with an inheritable lock
Siddharth Agarwal <sid0@fb.com>
parents: 26433
diff changeset
  1386
    if environ is None:
f0d730efb02f scmutil: add a way for a subprocess to be run with an inheritable lock
Siddharth Agarwal <sid0@fb.com>
parents: 26433
diff changeset
  1387
        environ = {}
f0d730efb02f scmutil: add a way for a subprocess to be run with an inheritable lock
Siddharth Agarwal <sid0@fb.com>
parents: 26433
diff changeset
  1388
    with lock.inherit() as locker:
f0d730efb02f scmutil: add a way for a subprocess to be run with an inheritable lock
Siddharth Agarwal <sid0@fb.com>
parents: 26433
diff changeset
  1389
        environ[envvar] = locker
f0d730efb02f scmutil: add a way for a subprocess to be run with an inheritable lock
Siddharth Agarwal <sid0@fb.com>
parents: 26433
diff changeset
  1390
        return repo.ui.system(cmd, environ=environ, *args, **kwargs)
26491
366d489295ca scmutil: add a way for a repo's wlock to be inherited by a subprocess
Siddharth Agarwal <sid0@fb.com>
parents: 26490
diff changeset
  1391
366d489295ca scmutil: add a way for a repo's wlock to be inherited by a subprocess
Siddharth Agarwal <sid0@fb.com>
parents: 26490
diff changeset
  1392
def wlocksub(repo, cmd, *args, **kwargs):
366d489295ca scmutil: add a way for a repo's wlock to be inherited by a subprocess
Siddharth Agarwal <sid0@fb.com>
parents: 26490
diff changeset
  1393
    """run cmd as a subprocess that allows inheriting repo's wlock
366d489295ca scmutil: add a way for a repo's wlock to be inherited by a subprocess
Siddharth Agarwal <sid0@fb.com>
parents: 26490
diff changeset
  1394
366d489295ca scmutil: add a way for a repo's wlock to be inherited by a subprocess
Siddharth Agarwal <sid0@fb.com>
parents: 26490
diff changeset
  1395
    This can only be called while the wlock is held. This takes all the
366d489295ca scmutil: add a way for a repo's wlock to be inherited by a subprocess
Siddharth Agarwal <sid0@fb.com>
parents: 26490
diff changeset
  1396
    arguments that ui.system does, and returns the exit code of the
366d489295ca scmutil: add a way for a repo's wlock to be inherited by a subprocess
Siddharth Agarwal <sid0@fb.com>
parents: 26490
diff changeset
  1397
    subprocess."""
366d489295ca scmutil: add a way for a repo's wlock to be inherited by a subprocess
Siddharth Agarwal <sid0@fb.com>
parents: 26490
diff changeset
  1398
    return _locksub(repo, repo.currentwlock(), 'HG_WLOCK_LOCKER', cmd, *args,
366d489295ca scmutil: add a way for a repo's wlock to be inherited by a subprocess
Siddharth Agarwal <sid0@fb.com>
parents: 26490
diff changeset
  1399
                    **kwargs)
26906
e40af07e518e scmutil: extract general delta config handling in a function
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26836
diff changeset
  1400
e40af07e518e scmutil: extract general delta config handling in a function
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26836
diff changeset
  1401
def gdinitconfig(ui):
e40af07e518e scmutil: extract general delta config handling in a function
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26836
diff changeset
  1402
    """helper function to know if a repo should be created as general delta
26907
dfab6edb98e3 format: introduce 'format.usegeneraldelta`
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26906
diff changeset
  1403
    """
dfab6edb98e3 format: introduce 'format.usegeneraldelta`
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26906
diff changeset
  1404
    # experimental config: format.generaldelta
dfab6edb98e3 format: introduce 'format.usegeneraldelta`
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26906
diff changeset
  1405
    return (ui.configbool('format', 'generaldelta', False)
27093
41d3e307a7c1 format: create new repository as 'generaldelta' by default
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26907
diff changeset
  1406
            or ui.configbool('format', 'usegeneraldelta', True))
26906
e40af07e518e scmutil: extract general delta config handling in a function
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26836
diff changeset
  1407
26907
dfab6edb98e3 format: introduce 'format.usegeneraldelta`
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26906
diff changeset
  1408
def gddeltaconfig(ui):
dfab6edb98e3 format: introduce 'format.usegeneraldelta`
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26906
diff changeset
  1409
    """helper function to know if incoming delta should be optimised
dfab6edb98e3 format: introduce 'format.usegeneraldelta`
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26906
diff changeset
  1410
    """
26906
e40af07e518e scmutil: extract general delta config handling in a function
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26836
diff changeset
  1411
    # experimental config: format.generaldelta
e40af07e518e scmutil: extract general delta config handling in a function
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26836
diff changeset
  1412
    return ui.configbool('format', 'generaldelta', False)
27895
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1413
29998
0c40e64d6154 scmutil: factor out common logic of delayclosedfile to reuse it
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29813
diff changeset
  1414
class closewrapbase(object):
0c40e64d6154 scmutil: factor out common logic of delayclosedfile to reuse it
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29813
diff changeset
  1415
    """Base class of wrapper, which hooks closing
27895
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1416
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1417
    Do not instantiate outside of the vfs layer.
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1418
    """
29998
0c40e64d6154 scmutil: factor out common logic of delayclosedfile to reuse it
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29813
diff changeset
  1419
    def __init__(self, fh):
27895
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1420
        object.__setattr__(self, '_origfh', fh)
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1421
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1422
    def __getattr__(self, attr):
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1423
        return getattr(self._origfh, attr)
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1424
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1425
    def __setattr__(self, attr, value):
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1426
        return setattr(self._origfh, attr, value)
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1427
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1428
    def __delattr__(self, attr):
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1429
        return delattr(self._origfh, attr)
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1430
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1431
    def __enter__(self):
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1432
        return self._origfh.__enter__()
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1433
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1434
    def __exit__(self, exc_type, exc_value, exc_tb):
29998
0c40e64d6154 scmutil: factor out common logic of delayclosedfile to reuse it
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29813
diff changeset
  1435
        raise NotImplementedError('attempted instantiating ' + str(type(self)))
0c40e64d6154 scmutil: factor out common logic of delayclosedfile to reuse it
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29813
diff changeset
  1436
0c40e64d6154 scmutil: factor out common logic of delayclosedfile to reuse it
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29813
diff changeset
  1437
    def close(self):
0c40e64d6154 scmutil: factor out common logic of delayclosedfile to reuse it
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29813
diff changeset
  1438
        raise NotImplementedError('attempted instantiating ' + str(type(self)))
0c40e64d6154 scmutil: factor out common logic of delayclosedfile to reuse it
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29813
diff changeset
  1439
0c40e64d6154 scmutil: factor out common logic of delayclosedfile to reuse it
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29813
diff changeset
  1440
class delayclosedfile(closewrapbase):
0c40e64d6154 scmutil: factor out common logic of delayclosedfile to reuse it
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29813
diff changeset
  1441
    """Proxy for a file object whose close is delayed.
0c40e64d6154 scmutil: factor out common logic of delayclosedfile to reuse it
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29813
diff changeset
  1442
0c40e64d6154 scmutil: factor out common logic of delayclosedfile to reuse it
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29813
diff changeset
  1443
    Do not instantiate outside of the vfs layer.
0c40e64d6154 scmutil: factor out common logic of delayclosedfile to reuse it
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29813
diff changeset
  1444
    """
0c40e64d6154 scmutil: factor out common logic of delayclosedfile to reuse it
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29813
diff changeset
  1445
    def __init__(self, fh, closer):
0c40e64d6154 scmutil: factor out common logic of delayclosedfile to reuse it
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29813
diff changeset
  1446
        super(delayclosedfile, self).__init__(fh)
0c40e64d6154 scmutil: factor out common logic of delayclosedfile to reuse it
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29813
diff changeset
  1447
        object.__setattr__(self, '_closer', closer)
0c40e64d6154 scmutil: factor out common logic of delayclosedfile to reuse it
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29813
diff changeset
  1448
0c40e64d6154 scmutil: factor out common logic of delayclosedfile to reuse it
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29813
diff changeset
  1449
    def __exit__(self, exc_type, exc_value, exc_tb):
27895
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1450
        self._closer.close(self._origfh)
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1451
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1452
    def close(self):
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1453
        self._closer.close(self._origfh)
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1454
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1455
class backgroundfilecloser(object):
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1456
    """Coordinates background closing of file handles on multiple threads."""
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1457
    def __init__(self, ui, expectedcount=-1):
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1458
        self._running = False
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1459
        self._entered = False
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1460
        self._threads = []
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1461
        self._threadexception = None
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1462
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1463
        # Only Windows/NTFS has slow file closing. So only enable by default
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1464
        # on that platform. But allow to be enabled elsewhere for testing.
30644
d524c88511a7 py3: replace os.name with pycompat.osname (part 1 of 2)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 30529
diff changeset
  1465
        defaultenabled = pycompat.osname == 'nt'
27895
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1466
        enabled = ui.configbool('worker', 'backgroundclose', defaultenabled)
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1467
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1468
        if not enabled:
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1469
            return
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1470
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1471
        # There is overhead to starting and stopping the background threads.
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1472
        # Don't do background processing unless the file count is large enough
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1473
        # to justify it.
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1474
        minfilecount = ui.configint('worker', 'backgroundcloseminfilecount',
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1475
                                    2048)
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1476
        # FUTURE dynamically start background threads after minfilecount closes.
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1477
        # (We don't currently have any callers that don't know their file count)
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1478
        if expectedcount > 0 and expectedcount < minfilecount:
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1479
            return
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1480
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1481
        # Windows defaults to a limit of 512 open files. A buffer of 128
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1482
        # should give us enough headway.
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1483
        maxqueue = ui.configint('worker', 'backgroundclosemaxqueue', 384)
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1484
        threadcount = ui.configint('worker', 'backgroundclosethreadcount', 4)
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1485
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1486
        ui.debug('starting %d threads for background file closing\n' %
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1487
                 threadcount)
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1488
28819
826d457df138 scmutil: use util.queue/util.empty for py3 compat
timeless <timeless@mozdev.org>
parents: 28197
diff changeset
  1489
        self._queue = util.queue(maxsize=maxqueue)
27895
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1490
        self._running = True
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1491
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1492
        for i in range(threadcount):
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1493
            t = threading.Thread(target=self._worker, name='backgroundcloser')
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1494
            self._threads.append(t)
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1495
            t.start()
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1496
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1497
    def __enter__(self):
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1498
        self._entered = True
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1499
        return self
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1500
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1501
    def __exit__(self, exc_type, exc_value, exc_tb):
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1502
        self._running = False
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1503
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1504
        # Wait for threads to finish closing so open files don't linger for
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1505
        # longer than lifetime of context manager.
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1506
        for t in self._threads:
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1507
            t.join()
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1508
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1509
    def _worker(self):
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1510
        """Main routine for worker thread."""
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1511
        while True:
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1512
            try:
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1513
                fh = self._queue.get(block=True, timeout=0.100)
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1514
                # Need to catch or the thread will terminate and
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1515
                # we could orphan file descriptors.
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1516
                try:
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1517
                    fh.close()
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1518
                except Exception as e:
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1519
                    # Stash so can re-raise from main thread later.
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1520
                    self._threadexception = e
28819
826d457df138 scmutil: use util.queue/util.empty for py3 compat
timeless <timeless@mozdev.org>
parents: 28197
diff changeset
  1521
            except util.empty:
27895
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1522
                if not self._running:
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1523
                    break
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1524
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1525
    def close(self, fh):
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1526
        """Schedule a file for closing."""
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1527
        if not self._entered:
29389
98e8313dcd9e i18n: translate abort messages
liscju <piotr.listkiewicz@gmail.com>
parents: 29373
diff changeset
  1528
            raise error.Abort(_('can only call close() when context manager '
98e8313dcd9e i18n: translate abort messages
liscju <piotr.listkiewicz@gmail.com>
parents: 29373
diff changeset
  1529
                              'active'))
27895
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1530
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1531
        # If a background thread encountered an exception, raise now so we fail
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1532
        # fast. Otherwise we may potentially go on for minutes until the error
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1533
        # is acted on.
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1534
        if self._threadexception:
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1535
            e = self._threadexception
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1536
            self._threadexception = None
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1537
            raise e
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1538
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1539
        # If we're not actively running, close synchronously.
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1540
        if not self._running:
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1541
            fh.close()
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1542
            return
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1543
2d6a89e79b48 scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 27879
diff changeset
  1544
        self._queue.put(fh, block=True, timeout=None)
29999
57830bd0e787 scmutil: add file object wrapper class to check ambiguity at closing
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29998
diff changeset
  1545
57830bd0e787 scmutil: add file object wrapper class to check ambiguity at closing
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29998
diff changeset
  1546
class checkambigatclosing(closewrapbase):
57830bd0e787 scmutil: add file object wrapper class to check ambiguity at closing
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29998
diff changeset
  1547
    """Proxy for a file object, to avoid ambiguity of file stat
57830bd0e787 scmutil: add file object wrapper class to check ambiguity at closing
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29998
diff changeset
  1548
57830bd0e787 scmutil: add file object wrapper class to check ambiguity at closing
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29998
diff changeset
  1549
    See also util.filestat for detail about "ambiguity of file stat".
57830bd0e787 scmutil: add file object wrapper class to check ambiguity at closing
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29998
diff changeset
  1550
57830bd0e787 scmutil: add file object wrapper class to check ambiguity at closing
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29998
diff changeset
  1551
    This proxy is useful only if the target file is guarded by any
57830bd0e787 scmutil: add file object wrapper class to check ambiguity at closing
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29998
diff changeset
  1552
    lock (e.g. repo.lock or repo.wlock)
57830bd0e787 scmutil: add file object wrapper class to check ambiguity at closing
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29998
diff changeset
  1553
57830bd0e787 scmutil: add file object wrapper class to check ambiguity at closing
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29998
diff changeset
  1554
    Do not instantiate outside of the vfs layer.
57830bd0e787 scmutil: add file object wrapper class to check ambiguity at closing
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29998
diff changeset
  1555
    """
57830bd0e787 scmutil: add file object wrapper class to check ambiguity at closing
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29998
diff changeset
  1556
    def __init__(self, fh):
57830bd0e787 scmutil: add file object wrapper class to check ambiguity at closing
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29998
diff changeset
  1557
        super(checkambigatclosing, self).__init__(fh)
57830bd0e787 scmutil: add file object wrapper class to check ambiguity at closing
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29998
diff changeset
  1558
        object.__setattr__(self, '_oldstat', util.filestat(fh.name))
57830bd0e787 scmutil: add file object wrapper class to check ambiguity at closing
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29998
diff changeset
  1559
57830bd0e787 scmutil: add file object wrapper class to check ambiguity at closing
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29998
diff changeset
  1560
    def _checkambig(self):
57830bd0e787 scmutil: add file object wrapper class to check ambiguity at closing
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29998
diff changeset
  1561
        oldstat = self._oldstat
57830bd0e787 scmutil: add file object wrapper class to check ambiguity at closing
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29998
diff changeset
  1562
        if oldstat.stat:
57830bd0e787 scmutil: add file object wrapper class to check ambiguity at closing
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29998
diff changeset
  1563
            newstat = util.filestat(self._origfh.name)
57830bd0e787 scmutil: add file object wrapper class to check ambiguity at closing
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29998
diff changeset
  1564
            if newstat.isambig(oldstat):
57830bd0e787 scmutil: add file object wrapper class to check ambiguity at closing
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29998
diff changeset
  1565
                # stat of changed file is ambiguous to original one
30255
e0ff47999b13 scmutil: ignore EPERM at os.utime, which avoids ambiguity at closing
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 30254
diff changeset
  1566
                newstat.avoidambig(self._origfh.name, oldstat)
29999
57830bd0e787 scmutil: add file object wrapper class to check ambiguity at closing
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29998
diff changeset
  1567
57830bd0e787 scmutil: add file object wrapper class to check ambiguity at closing
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29998
diff changeset
  1568
    def __exit__(self, exc_type, exc_value, exc_tb):
57830bd0e787 scmutil: add file object wrapper class to check ambiguity at closing
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29998
diff changeset
  1569
        self._origfh.__exit__(exc_type, exc_value, exc_tb)
57830bd0e787 scmutil: add file object wrapper class to check ambiguity at closing
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29998
diff changeset
  1570
        self._checkambig()
57830bd0e787 scmutil: add file object wrapper class to check ambiguity at closing
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29998
diff changeset
  1571
57830bd0e787 scmutil: add file object wrapper class to check ambiguity at closing
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29998
diff changeset
  1572
    def close(self):
57830bd0e787 scmutil: add file object wrapper class to check ambiguity at closing
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29998
diff changeset
  1573
        self._origfh.close()
57830bd0e787 scmutil: add file object wrapper class to check ambiguity at closing
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29998
diff changeset
  1574
        self._checkambig()