mercurial/narrowspec.py
author Martin von Zweigbergk <martinvonz@google.com>
Sun, 30 Dec 2018 00:15:38 -0800
changeset 41238 8c366af085f4
parent 41229 50ca531f1f24
child 41298 88a7c211b21e
permissions -rw-r--r--
narrow: reuse narrowspec.updateworkingcopy() when narrowing Similar to the previous patch for widening, but here we also need to teach updateworkingcopy() to forcefully delete files that are not recorded in the dirstate as clean. That should be safe because the narrowing command (e.g. `hg tracked --removeinclude`) has already checked that the working copy is clean. Differential Revision: https://phab.mercurial-scm.org/D5511
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36117
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
     1
# narrowspec.py - methods for working with a narrow view of a repository
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
     2
#
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
     3
# Copyright 2017 Google, Inc.
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
     4
#
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
     5
# This software may be used and distributed according to the terms of the
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
     6
# GNU General Public License version 2 or any later version.
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
     7
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
     8
from __future__ import absolute_import
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
     9
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    10
import errno
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    11
36198
9fd8c2a3db5a narrowspec: move module into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36197
diff changeset
    12
from .i18n import _
9fd8c2a3db5a narrowspec: move module into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36197
diff changeset
    13
from . import (
36117
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    14
    error,
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    15
    match as matchmod,
41046
ce0bc2952e2a narrow: detect if narrowspec was changed in a different share
Martin von Zweigbergk <martinvonz@google.com>
parents: 41044
diff changeset
    16
    merge,
38908
ad24b581e4d9 narrow: call narrowspec.{save,restore,clear}backup directly
Martin von Zweigbergk <martinvonz@google.com>
parents: 38879
diff changeset
    17
    repository,
38878
f64ebe7d2259 narrowspec: use sparse.parseconfig() to parse narrowspec file (BC)
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 38875
diff changeset
    18
    sparse,
36117
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    19
    util,
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    20
)
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    21
41046
ce0bc2952e2a narrow: detect if narrowspec was changed in a different share
Martin von Zweigbergk <martinvonz@google.com>
parents: 41044
diff changeset
    22
# The file in .hg/store/ that indicates which paths exit in the store
36117
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    23
FILENAME = 'narrowspec'
41046
ce0bc2952e2a narrow: detect if narrowspec was changed in a different share
Martin von Zweigbergk <martinvonz@google.com>
parents: 41044
diff changeset
    24
# The file in .hg/ that indicates which paths exit in the dirstate
ce0bc2952e2a narrow: detect if narrowspec was changed in a different share
Martin von Zweigbergk <martinvonz@google.com>
parents: 41044
diff changeset
    25
DIRSTATE_FILENAME = 'narrowspec.dirstate'
36117
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    26
39548
0d572769046a narrowspec: limit patterns to path: and rootfilesin: (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39542
diff changeset
    27
# Pattern prefixes that are allowed in narrow patterns. This list MUST
0d572769046a narrowspec: limit patterns to path: and rootfilesin: (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39542
diff changeset
    28
# only contain patterns that are fast and safe to evaluate. Keep in mind
0d572769046a narrowspec: limit patterns to path: and rootfilesin: (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39542
diff changeset
    29
# that patterns are supplied by clients and executed on remote servers
39816
ae20f52437e9 wireprotov2: advertise recognized path filter prefixes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39572
diff changeset
    30
# as part of wire protocol commands. That means that changes to this
ae20f52437e9 wireprotov2: advertise recognized path filter prefixes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39572
diff changeset
    31
# data structure influence the wire protocol and should not be taken
ae20f52437e9 wireprotov2: advertise recognized path filter prefixes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39572
diff changeset
    32
# lightly - especially removals.
39548
0d572769046a narrowspec: limit patterns to path: and rootfilesin: (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39542
diff changeset
    33
VALID_PREFIXES = (
0d572769046a narrowspec: limit patterns to path: and rootfilesin: (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39542
diff changeset
    34
    b'path:',
0d572769046a narrowspec: limit patterns to path: and rootfilesin: (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39542
diff changeset
    35
    b'rootfilesin:',
0d572769046a narrowspec: limit patterns to path: and rootfilesin: (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39542
diff changeset
    36
)
0d572769046a narrowspec: limit patterns to path: and rootfilesin: (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39542
diff changeset
    37
36117
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    38
def normalizesplitpattern(kind, pat):
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    39
    """Returns the normalized version of a pattern and kind.
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    40
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    41
    Returns a tuple with the normalized kind and normalized pattern.
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    42
    """
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    43
    pat = pat.rstrip('/')
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    44
    _validatepattern(pat)
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    45
    return kind, pat
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    46
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    47
def _numlines(s):
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    48
    """Returns the number of lines in s, including ending empty lines."""
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    49
    # We use splitlines because it is Unicode-friendly and thus Python 3
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    50
    # compatible. However, it does not count empty lines at the end, so trick
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    51
    # it by adding a character at the end.
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    52
    return len((s + 'x').splitlines())
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    53
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    54
def _validatepattern(pat):
36136
9c55bbc29dcf narrowspec: document constraints when validating patterns
Augie Fackler <augie@google.com>
parents: 36117
diff changeset
    55
    """Validates the pattern and aborts if it is invalid.
9c55bbc29dcf narrowspec: document constraints when validating patterns
Augie Fackler <augie@google.com>
parents: 36117
diff changeset
    56
9c55bbc29dcf narrowspec: document constraints when validating patterns
Augie Fackler <augie@google.com>
parents: 36117
diff changeset
    57
    Patterns are stored in the narrowspec as newline-separated
9c55bbc29dcf narrowspec: document constraints when validating patterns
Augie Fackler <augie@google.com>
parents: 36117
diff changeset
    58
    POSIX-style bytestring paths. There's no escaping.
9c55bbc29dcf narrowspec: document constraints when validating patterns
Augie Fackler <augie@google.com>
parents: 36117
diff changeset
    59
    """
36117
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    60
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    61
    # We use newlines as separators in the narrowspec file, so don't allow them
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    62
    # in patterns.
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    63
    if _numlines(pat) > 1:
36198
9fd8c2a3db5a narrowspec: move module into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36197
diff changeset
    64
        raise error.Abort(_('newlines are not allowed in narrowspec paths'))
36117
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    65
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    66
    components = pat.split('/')
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    67
    if '.' in components or '..' in components:
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    68
        raise error.Abort(_('"." and ".." are not allowed in narrowspec paths'))
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    69
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    70
def normalizepattern(pattern, defaultkind='path'):
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    71
    """Returns the normalized version of a text-format pattern.
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    72
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    73
    If the pattern has no kind, the default will be added.
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    74
    """
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    75
    kind, pat = matchmod._patsplit(pattern, defaultkind)
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    76
    return '%s:%s' % normalizesplitpattern(kind, pat)
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    77
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    78
def parsepatterns(pats):
39548
0d572769046a narrowspec: limit patterns to path: and rootfilesin: (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39542
diff changeset
    79
    """Parses an iterable of patterns into a typed pattern set.
0d572769046a narrowspec: limit patterns to path: and rootfilesin: (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39542
diff changeset
    80
0d572769046a narrowspec: limit patterns to path: and rootfilesin: (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39542
diff changeset
    81
    Patterns are assumed to be ``path:`` if no prefix is present.
0d572769046a narrowspec: limit patterns to path: and rootfilesin: (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39542
diff changeset
    82
    For safety and performance reasons, only some prefixes are allowed.
0d572769046a narrowspec: limit patterns to path: and rootfilesin: (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39542
diff changeset
    83
    See ``validatepatterns()``.
0d572769046a narrowspec: limit patterns to path: and rootfilesin: (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39542
diff changeset
    84
0d572769046a narrowspec: limit patterns to path: and rootfilesin: (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39542
diff changeset
    85
    This function should be used on patterns that come from the user to
0d572769046a narrowspec: limit patterns to path: and rootfilesin: (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39542
diff changeset
    86
    normalize and validate them to the internal data structure used for
0d572769046a narrowspec: limit patterns to path: and rootfilesin: (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39542
diff changeset
    87
    representing patterns.
0d572769046a narrowspec: limit patterns to path: and rootfilesin: (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39542
diff changeset
    88
    """
0d572769046a narrowspec: limit patterns to path: and rootfilesin: (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39542
diff changeset
    89
    res = {normalizepattern(orig) for orig in pats}
0d572769046a narrowspec: limit patterns to path: and rootfilesin: (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39542
diff changeset
    90
    validatepatterns(res)
0d572769046a narrowspec: limit patterns to path: and rootfilesin: (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39542
diff changeset
    91
    return res
0d572769046a narrowspec: limit patterns to path: and rootfilesin: (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39542
diff changeset
    92
0d572769046a narrowspec: limit patterns to path: and rootfilesin: (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39542
diff changeset
    93
def validatepatterns(pats):
0d572769046a narrowspec: limit patterns to path: and rootfilesin: (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39542
diff changeset
    94
    """Validate that patterns are in the expected data structure and format.
0d572769046a narrowspec: limit patterns to path: and rootfilesin: (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39542
diff changeset
    95
0d572769046a narrowspec: limit patterns to path: and rootfilesin: (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39542
diff changeset
    96
    And that is a set of normalized patterns beginning with ``path:`` or
0d572769046a narrowspec: limit patterns to path: and rootfilesin: (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39542
diff changeset
    97
    ``rootfilesin:``.
0d572769046a narrowspec: limit patterns to path: and rootfilesin: (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39542
diff changeset
    98
0d572769046a narrowspec: limit patterns to path: and rootfilesin: (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39542
diff changeset
    99
    This function should be used to validate internal data structures
0d572769046a narrowspec: limit patterns to path: and rootfilesin: (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39542
diff changeset
   100
    and patterns that are loaded from sources that use the internal,
0d572769046a narrowspec: limit patterns to path: and rootfilesin: (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39542
diff changeset
   101
    prefixed pattern representation (but can't necessarily be fully trusted).
0d572769046a narrowspec: limit patterns to path: and rootfilesin: (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39542
diff changeset
   102
    """
0d572769046a narrowspec: limit patterns to path: and rootfilesin: (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39542
diff changeset
   103
    if not isinstance(pats, set):
0d572769046a narrowspec: limit patterns to path: and rootfilesin: (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39542
diff changeset
   104
        raise error.ProgrammingError('narrow patterns should be a set; '
0d572769046a narrowspec: limit patterns to path: and rootfilesin: (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39542
diff changeset
   105
                                     'got %r' % pats)
0d572769046a narrowspec: limit patterns to path: and rootfilesin: (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39542
diff changeset
   106
0d572769046a narrowspec: limit patterns to path: and rootfilesin: (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39542
diff changeset
   107
    for pat in pats:
0d572769046a narrowspec: limit patterns to path: and rootfilesin: (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39542
diff changeset
   108
        if not pat.startswith(VALID_PREFIXES):
0d572769046a narrowspec: limit patterns to path: and rootfilesin: (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39542
diff changeset
   109
            # Use a Mercurial exception because this can happen due to user
0d572769046a narrowspec: limit patterns to path: and rootfilesin: (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39542
diff changeset
   110
            # bugs (e.g. manually updating spec file).
0d572769046a narrowspec: limit patterns to path: and rootfilesin: (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39542
diff changeset
   111
            raise error.Abort(_('invalid prefix on narrow pattern: %s') % pat,
0d572769046a narrowspec: limit patterns to path: and rootfilesin: (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39542
diff changeset
   112
                              hint=_('narrow patterns must begin with one of '
0d572769046a narrowspec: limit patterns to path: and rootfilesin: (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39542
diff changeset
   113
                                     'the following: %s') %
0d572769046a narrowspec: limit patterns to path: and rootfilesin: (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39542
diff changeset
   114
                                   ', '.join(VALID_PREFIXES))
36117
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   115
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   116
def format(includes, excludes):
38878
f64ebe7d2259 narrowspec: use sparse.parseconfig() to parse narrowspec file (BC)
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 38875
diff changeset
   117
    output = '[include]\n'
36117
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   118
    for i in sorted(includes - excludes):
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   119
        output += i + '\n'
38878
f64ebe7d2259 narrowspec: use sparse.parseconfig() to parse narrowspec file (BC)
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 38875
diff changeset
   120
    output += '[exclude]\n'
36117
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   121
    for e in sorted(excludes):
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   122
        output += e + '\n'
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   123
    return output
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   124
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   125
def match(root, include=None, exclude=None):
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   126
    if not include:
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   127
        # Passing empty include and empty exclude to matchmod.match()
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   128
        # gives a matcher that matches everything, so explicitly use
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   129
        # the nevermatcher.
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   130
        return matchmod.never(root, '')
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   131
    return matchmod.match(root, '', [], include=include or [],
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   132
                          exclude=exclude or [])
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   133
40729
efd0f79246e3 narrow: extract helper for parsing narrowspec file
Martin von Zweigbergk <martinvonz@google.com>
parents: 39816
diff changeset
   134
def parseconfig(ui, spec):
efd0f79246e3 narrow: extract helper for parsing narrowspec file
Martin von Zweigbergk <martinvonz@google.com>
parents: 39816
diff changeset
   135
    # maybe we should care about the profiles returned too
efd0f79246e3 narrow: extract helper for parsing narrowspec file
Martin von Zweigbergk <martinvonz@google.com>
parents: 39816
diff changeset
   136
    includepats, excludepats, profiles = sparse.parseconfig(ui, spec, 'narrow')
efd0f79246e3 narrow: extract helper for parsing narrowspec file
Martin von Zweigbergk <martinvonz@google.com>
parents: 39816
diff changeset
   137
    if profiles:
efd0f79246e3 narrow: extract helper for parsing narrowspec file
Martin von Zweigbergk <martinvonz@google.com>
parents: 39816
diff changeset
   138
        raise error.Abort(_("including other spec files using '%include' is not"
efd0f79246e3 narrow: extract helper for parsing narrowspec file
Martin von Zweigbergk <martinvonz@google.com>
parents: 39816
diff changeset
   139
                            " supported in narrowspec"))
efd0f79246e3 narrow: extract helper for parsing narrowspec file
Martin von Zweigbergk <martinvonz@google.com>
parents: 39816
diff changeset
   140
efd0f79246e3 narrow: extract helper for parsing narrowspec file
Martin von Zweigbergk <martinvonz@google.com>
parents: 39816
diff changeset
   141
    validatepatterns(includepats)
efd0f79246e3 narrow: extract helper for parsing narrowspec file
Martin von Zweigbergk <martinvonz@google.com>
parents: 39816
diff changeset
   142
    validatepatterns(excludepats)
efd0f79246e3 narrow: extract helper for parsing narrowspec file
Martin von Zweigbergk <martinvonz@google.com>
parents: 39816
diff changeset
   143
efd0f79246e3 narrow: extract helper for parsing narrowspec file
Martin von Zweigbergk <martinvonz@google.com>
parents: 39816
diff changeset
   144
    return includepats, excludepats
efd0f79246e3 narrow: extract helper for parsing narrowspec file
Martin von Zweigbergk <martinvonz@google.com>
parents: 39816
diff changeset
   145
36117
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   146
def load(repo):
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   147
    try:
38911
576eef1ab43d narrow: move .hg/narrowspec to .hg/store/narrowspec (BC)
Martin von Zweigbergk <martinvonz@google.com>
parents: 38910
diff changeset
   148
        spec = repo.svfs.read(FILENAME)
36117
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   149
    except IOError as e:
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   150
        # Treat "narrowspec does not exist" the same as "narrowspec file exists
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   151
        # and is empty".
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   152
        if e.errno == errno.ENOENT:
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   153
            return set(), set()
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   154
        raise
39556
8d8e61df8259 narrowspec: validate patterns when loading and saving spec file
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39548
diff changeset
   155
40729
efd0f79246e3 narrow: extract helper for parsing narrowspec file
Martin von Zweigbergk <martinvonz@google.com>
parents: 39816
diff changeset
   156
    return parseconfig(repo.ui, spec)
36117
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   157
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   158
def save(repo, includepats, excludepats):
39556
8d8e61df8259 narrowspec: validate patterns when loading and saving spec file
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39548
diff changeset
   159
    validatepatterns(includepats)
8d8e61df8259 narrowspec: validate patterns when loading and saving spec file
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39548
diff changeset
   160
    validatepatterns(excludepats)
36117
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   161
    spec = format(includepats, excludepats)
38911
576eef1ab43d narrow: move .hg/narrowspec to .hg/store/narrowspec (BC)
Martin von Zweigbergk <martinvonz@google.com>
parents: 38910
diff changeset
   162
    repo.svfs.write(FILENAME, spec)
36117
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   163
41229
50ca531f1f24 narrow: copy store narrowspec to working copy immediately
Martin von Zweigbergk <martinvonz@google.com>
parents: 41227
diff changeset
   164
def copytoworkingcopy(repo):
50ca531f1f24 narrow: copy store narrowspec to working copy immediately
Martin von Zweigbergk <martinvonz@google.com>
parents: 41227
diff changeset
   165
    spec = repo.svfs.read(FILENAME)
50ca531f1f24 narrow: copy store narrowspec to working copy immediately
Martin von Zweigbergk <martinvonz@google.com>
parents: 41227
diff changeset
   166
    repo.vfs.write(DIRSTATE_FILENAME, spec)
41046
ce0bc2952e2a narrow: detect if narrowspec was changed in a different share
Martin von Zweigbergk <martinvonz@google.com>
parents: 41044
diff changeset
   167
38908
ad24b581e4d9 narrow: call narrowspec.{save,restore,clear}backup directly
Martin von Zweigbergk <martinvonz@google.com>
parents: 38879
diff changeset
   168
def savebackup(repo, backupname):
ad24b581e4d9 narrow: call narrowspec.{save,restore,clear}backup directly
Martin von Zweigbergk <martinvonz@google.com>
parents: 38879
diff changeset
   169
    if repository.NARROW_REQUIREMENT not in repo.requirements:
ad24b581e4d9 narrow: call narrowspec.{save,restore,clear}backup directly
Martin von Zweigbergk <martinvonz@google.com>
parents: 38879
diff changeset
   170
        return
41044
1e8d9f472ea1 narrow: keep narrowspec backup in store
Martin von Zweigbergk <martinvonz@google.com>
parents: 40729
diff changeset
   171
    svfs = repo.svfs
1e8d9f472ea1 narrow: keep narrowspec backup in store
Martin von Zweigbergk <martinvonz@google.com>
parents: 40729
diff changeset
   172
    svfs.tryunlink(backupname)
1e8d9f472ea1 narrow: keep narrowspec backup in store
Martin von Zweigbergk <martinvonz@google.com>
parents: 40729
diff changeset
   173
    util.copyfile(svfs.join(FILENAME), svfs.join(backupname), hardlink=True)
38875
fed6fe856333 narrow: extract part of narrowspec backup to core
Martin von Zweigbergk <martinvonz@google.com>
parents: 36500
diff changeset
   174
38908
ad24b581e4d9 narrow: call narrowspec.{save,restore,clear}backup directly
Martin von Zweigbergk <martinvonz@google.com>
parents: 38879
diff changeset
   175
def restorebackup(repo, backupname):
ad24b581e4d9 narrow: call narrowspec.{save,restore,clear}backup directly
Martin von Zweigbergk <martinvonz@google.com>
parents: 38879
diff changeset
   176
    if repository.NARROW_REQUIREMENT not in repo.requirements:
ad24b581e4d9 narrow: call narrowspec.{save,restore,clear}backup directly
Martin von Zweigbergk <martinvonz@google.com>
parents: 38879
diff changeset
   177
        return
41044
1e8d9f472ea1 narrow: keep narrowspec backup in store
Martin von Zweigbergk <martinvonz@google.com>
parents: 40729
diff changeset
   178
    util.rename(repo.svfs.join(backupname), repo.svfs.join(FILENAME))
38875
fed6fe856333 narrow: extract part of narrowspec backup to core
Martin von Zweigbergk <martinvonz@google.com>
parents: 36500
diff changeset
   179
41227
b74481038438 narrow: make dirstateguard back up and restore working copy narrowspec instead
Martin von Zweigbergk <martinvonz@google.com>
parents: 41177
diff changeset
   180
def savewcbackup(repo, backupname):
38908
ad24b581e4d9 narrow: call narrowspec.{save,restore,clear}backup directly
Martin von Zweigbergk <martinvonz@google.com>
parents: 38879
diff changeset
   181
    if repository.NARROW_REQUIREMENT not in repo.requirements:
ad24b581e4d9 narrow: call narrowspec.{save,restore,clear}backup directly
Martin von Zweigbergk <martinvonz@google.com>
parents: 38879
diff changeset
   182
        return
41227
b74481038438 narrow: make dirstateguard back up and restore working copy narrowspec instead
Martin von Zweigbergk <martinvonz@google.com>
parents: 41177
diff changeset
   183
    vfs = repo.vfs
b74481038438 narrow: make dirstateguard back up and restore working copy narrowspec instead
Martin von Zweigbergk <martinvonz@google.com>
parents: 41177
diff changeset
   184
    vfs.tryunlink(backupname)
b74481038438 narrow: make dirstateguard back up and restore working copy narrowspec instead
Martin von Zweigbergk <martinvonz@google.com>
parents: 41177
diff changeset
   185
    # It may not exist in old repos
b74481038438 narrow: make dirstateguard back up and restore working copy narrowspec instead
Martin von Zweigbergk <martinvonz@google.com>
parents: 41177
diff changeset
   186
    if vfs.exists(DIRSTATE_FILENAME):
b74481038438 narrow: make dirstateguard back up and restore working copy narrowspec instead
Martin von Zweigbergk <martinvonz@google.com>
parents: 41177
diff changeset
   187
        util.copyfile(vfs.join(DIRSTATE_FILENAME), vfs.join(backupname),
b74481038438 narrow: make dirstateguard back up and restore working copy narrowspec instead
Martin von Zweigbergk <martinvonz@google.com>
parents: 41177
diff changeset
   188
                      hardlink=True)
b74481038438 narrow: make dirstateguard back up and restore working copy narrowspec instead
Martin von Zweigbergk <martinvonz@google.com>
parents: 41177
diff changeset
   189
b74481038438 narrow: make dirstateguard back up and restore working copy narrowspec instead
Martin von Zweigbergk <martinvonz@google.com>
parents: 41177
diff changeset
   190
def restorewcbackup(repo, backupname):
b74481038438 narrow: make dirstateguard back up and restore working copy narrowspec instead
Martin von Zweigbergk <martinvonz@google.com>
parents: 41177
diff changeset
   191
    if repository.NARROW_REQUIREMENT not in repo.requirements:
b74481038438 narrow: make dirstateguard back up and restore working copy narrowspec instead
Martin von Zweigbergk <martinvonz@google.com>
parents: 41177
diff changeset
   192
        return
b74481038438 narrow: make dirstateguard back up and restore working copy narrowspec instead
Martin von Zweigbergk <martinvonz@google.com>
parents: 41177
diff changeset
   193
    util.rename(repo.vfs.join(backupname), repo.vfs.join(DIRSTATE_FILENAME))
b74481038438 narrow: make dirstateguard back up and restore working copy narrowspec instead
Martin von Zweigbergk <martinvonz@google.com>
parents: 41177
diff changeset
   194
b74481038438 narrow: make dirstateguard back up and restore working copy narrowspec instead
Martin von Zweigbergk <martinvonz@google.com>
parents: 41177
diff changeset
   195
def clearwcbackup(repo, backupname):
b74481038438 narrow: make dirstateguard back up and restore working copy narrowspec instead
Martin von Zweigbergk <martinvonz@google.com>
parents: 41177
diff changeset
   196
    if repository.NARROW_REQUIREMENT not in repo.requirements:
b74481038438 narrow: make dirstateguard back up and restore working copy narrowspec instead
Martin von Zweigbergk <martinvonz@google.com>
parents: 41177
diff changeset
   197
        return
b74481038438 narrow: make dirstateguard back up and restore working copy narrowspec instead
Martin von Zweigbergk <martinvonz@google.com>
parents: 41177
diff changeset
   198
    repo.vfs.unlink(backupname)
38875
fed6fe856333 narrow: extract part of narrowspec backup to core
Martin von Zweigbergk <martinvonz@google.com>
parents: 36500
diff changeset
   199
36138
8fd0a9e2d7e9 narrow: make restrictpatterns a little more idiomatic
Augie Fackler <augie@google.com>
parents: 36137
diff changeset
   200
def restrictpatterns(req_includes, req_excludes, repo_includes, repo_excludes):
36117
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   201
    r""" Restricts the patterns according to repo settings,
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   202
    results in a logical AND operation
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   203
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   204
    :param req_includes: requested includes
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   205
    :param req_excludes: requested excludes
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   206
    :param repo_includes: repo includes
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   207
    :param repo_excludes: repo excludes
36138
8fd0a9e2d7e9 narrow: make restrictpatterns a little more idiomatic
Augie Fackler <augie@google.com>
parents: 36137
diff changeset
   208
    :return: include patterns, exclude patterns, and invalid include patterns.
36117
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   209
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   210
    >>> restrictpatterns({'f1','f2'}, {}, ['f1'], [])
36138
8fd0a9e2d7e9 narrow: make restrictpatterns a little more idiomatic
Augie Fackler <augie@google.com>
parents: 36137
diff changeset
   211
    (set(['f1']), {}, [])
36117
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   212
    >>> restrictpatterns({'f1'}, {}, ['f1','f2'], [])
36138
8fd0a9e2d7e9 narrow: make restrictpatterns a little more idiomatic
Augie Fackler <augie@google.com>
parents: 36137
diff changeset
   213
    (set(['f1']), {}, [])
36117
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   214
    >>> restrictpatterns({'f1/fc1', 'f3/fc3'}, {}, ['f1','f2'], [])
36138
8fd0a9e2d7e9 narrow: make restrictpatterns a little more idiomatic
Augie Fackler <augie@google.com>
parents: 36137
diff changeset
   215
    (set(['f1/fc1']), {}, [])
36117
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   216
    >>> restrictpatterns({'f1_fc1'}, {}, ['f1','f2'], [])
36138
8fd0a9e2d7e9 narrow: make restrictpatterns a little more idiomatic
Augie Fackler <augie@google.com>
parents: 36137
diff changeset
   217
    ([], set(['path:.']), [])
36117
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   218
    >>> restrictpatterns({'f1/../f2/fc2'}, {}, ['f1','f2'], [])
36138
8fd0a9e2d7e9 narrow: make restrictpatterns a little more idiomatic
Augie Fackler <augie@google.com>
parents: 36137
diff changeset
   219
    (set(['f2/fc2']), {}, [])
36117
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   220
    >>> restrictpatterns({'f1/../f3/fc3'}, {}, ['f1','f2'], [])
36138
8fd0a9e2d7e9 narrow: make restrictpatterns a little more idiomatic
Augie Fackler <augie@google.com>
parents: 36137
diff changeset
   221
    ([], set(['path:.']), [])
36117
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   222
    >>> restrictpatterns({'f1/$non_exitent_var'}, {}, ['f1','f2'], [])
36138
8fd0a9e2d7e9 narrow: make restrictpatterns a little more idiomatic
Augie Fackler <augie@google.com>
parents: 36137
diff changeset
   223
    (set(['f1/$non_exitent_var']), {}, [])
36117
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   224
    """
36137
b8bbe589fd47 narrowspec: consistently use set() to copy sets
Augie Fackler <augie@google.com>
parents: 36136
diff changeset
   225
    res_excludes = set(req_excludes)
36117
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   226
    res_excludes.update(repo_excludes)
36138
8fd0a9e2d7e9 narrow: make restrictpatterns a little more idiomatic
Augie Fackler <augie@google.com>
parents: 36137
diff changeset
   227
    invalid_includes = []
36117
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   228
    if not req_includes:
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   229
        res_includes = set(repo_includes)
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   230
    elif 'path:.' not in repo_includes:
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   231
        res_includes = []
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   232
        for req_include in req_includes:
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   233
            req_include = util.expandpath(util.normpath(req_include))
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   234
            if req_include in repo_includes:
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   235
                res_includes.append(req_include)
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   236
                continue
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   237
            valid = False
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   238
            for repo_include in repo_includes:
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   239
                if req_include.startswith(repo_include + '/'):
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   240
                    valid = True
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   241
                    res_includes.append(req_include)
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   242
                    break
36138
8fd0a9e2d7e9 narrow: make restrictpatterns a little more idiomatic
Augie Fackler <augie@google.com>
parents: 36137
diff changeset
   243
            if not valid:
36117
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   244
                invalid_includes.append(req_include)
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   245
        if len(res_includes) == 0:
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   246
            res_excludes = {'path:.'}
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   247
        else:
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   248
            res_includes = set(res_includes)
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   249
    else:
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   250
        res_includes = set(req_includes)
36138
8fd0a9e2d7e9 narrow: make restrictpatterns a little more idiomatic
Augie Fackler <augie@google.com>
parents: 36137
diff changeset
   251
    return res_includes, res_excludes, invalid_includes
41046
ce0bc2952e2a narrow: detect if narrowspec was changed in a different share
Martin von Zweigbergk <martinvonz@google.com>
parents: 41044
diff changeset
   252
ce0bc2952e2a narrow: detect if narrowspec was changed in a different share
Martin von Zweigbergk <martinvonz@google.com>
parents: 41044
diff changeset
   253
# These two are extracted for extensions (specifically for Google's CitC file
ce0bc2952e2a narrow: detect if narrowspec was changed in a different share
Martin von Zweigbergk <martinvonz@google.com>
parents: 41044
diff changeset
   254
# system)
ce0bc2952e2a narrow: detect if narrowspec was changed in a different share
Martin von Zweigbergk <martinvonz@google.com>
parents: 41044
diff changeset
   255
def _deletecleanfiles(repo, files):
ce0bc2952e2a narrow: detect if narrowspec was changed in a different share
Martin von Zweigbergk <martinvonz@google.com>
parents: 41044
diff changeset
   256
    for f in files:
ce0bc2952e2a narrow: detect if narrowspec was changed in a different share
Martin von Zweigbergk <martinvonz@google.com>
parents: 41044
diff changeset
   257
        repo.wvfs.unlinkpath(f)
ce0bc2952e2a narrow: detect if narrowspec was changed in a different share
Martin von Zweigbergk <martinvonz@google.com>
parents: 41044
diff changeset
   258
ce0bc2952e2a narrow: detect if narrowspec was changed in a different share
Martin von Zweigbergk <martinvonz@google.com>
parents: 41044
diff changeset
   259
def _writeaddedfiles(repo, pctx, files):
ce0bc2952e2a narrow: detect if narrowspec was changed in a different share
Martin von Zweigbergk <martinvonz@google.com>
parents: 41044
diff changeset
   260
    actions = merge.emptyactions()
41177
5838afea8213 narrow: use merge.ACTION_GET instead of duplicating 'g' constant
Martin von Zweigbergk <martinvonz@google.com>
parents: 41176
diff changeset
   261
    addgaction = actions[merge.ACTION_GET].append
41046
ce0bc2952e2a narrow: detect if narrowspec was changed in a different share
Martin von Zweigbergk <martinvonz@google.com>
parents: 41044
diff changeset
   262
    mf = repo['.'].manifest()
ce0bc2952e2a narrow: detect if narrowspec was changed in a different share
Martin von Zweigbergk <martinvonz@google.com>
parents: 41044
diff changeset
   263
    for f in files:
ce0bc2952e2a narrow: detect if narrowspec was changed in a different share
Martin von Zweigbergk <martinvonz@google.com>
parents: 41044
diff changeset
   264
        if not repo.wvfs.exists(f):
ce0bc2952e2a narrow: detect if narrowspec was changed in a different share
Martin von Zweigbergk <martinvonz@google.com>
parents: 41044
diff changeset
   265
            addgaction((f, (mf.flags(f), False), "narrowspec updated"))
ce0bc2952e2a narrow: detect if narrowspec was changed in a different share
Martin von Zweigbergk <martinvonz@google.com>
parents: 41044
diff changeset
   266
    merge.applyupdates(repo, actions, wctx=repo[None],
ce0bc2952e2a narrow: detect if narrowspec was changed in a different share
Martin von Zweigbergk <martinvonz@google.com>
parents: 41044
diff changeset
   267
                       mctx=repo['.'], overwrite=False)
ce0bc2952e2a narrow: detect if narrowspec was changed in a different share
Martin von Zweigbergk <martinvonz@google.com>
parents: 41044
diff changeset
   268
ce0bc2952e2a narrow: detect if narrowspec was changed in a different share
Martin von Zweigbergk <martinvonz@google.com>
parents: 41044
diff changeset
   269
def checkworkingcopynarrowspec(repo):
ce0bc2952e2a narrow: detect if narrowspec was changed in a different share
Martin von Zweigbergk <martinvonz@google.com>
parents: 41044
diff changeset
   270
    storespec = repo.svfs.tryread(FILENAME)
ce0bc2952e2a narrow: detect if narrowspec was changed in a different share
Martin von Zweigbergk <martinvonz@google.com>
parents: 41044
diff changeset
   271
    wcspec = repo.vfs.tryread(DIRSTATE_FILENAME)
ce0bc2952e2a narrow: detect if narrowspec was changed in a different share
Martin von Zweigbergk <martinvonz@google.com>
parents: 41044
diff changeset
   272
    if wcspec != storespec:
ce0bc2952e2a narrow: detect if narrowspec was changed in a different share
Martin von Zweigbergk <martinvonz@google.com>
parents: 41044
diff changeset
   273
        raise error.Abort(_("working copy's narrowspec is stale"),
ce0bc2952e2a narrow: detect if narrowspec was changed in a different share
Martin von Zweigbergk <martinvonz@google.com>
parents: 41044
diff changeset
   274
                          hint=_("run 'hg tracked --update-working-copy'"))
ce0bc2952e2a narrow: detect if narrowspec was changed in a different share
Martin von Zweigbergk <martinvonz@google.com>
parents: 41044
diff changeset
   275
41238
8c366af085f4 narrow: reuse narrowspec.updateworkingcopy() when narrowing
Martin von Zweigbergk <martinvonz@google.com>
parents: 41229
diff changeset
   276
def updateworkingcopy(repo, assumeclean=False):
8c366af085f4 narrow: reuse narrowspec.updateworkingcopy() when narrowing
Martin von Zweigbergk <martinvonz@google.com>
parents: 41229
diff changeset
   277
    """updates the working copy and dirstate from the store narrowspec
8c366af085f4 narrow: reuse narrowspec.updateworkingcopy() when narrowing
Martin von Zweigbergk <martinvonz@google.com>
parents: 41229
diff changeset
   278
8c366af085f4 narrow: reuse narrowspec.updateworkingcopy() when narrowing
Martin von Zweigbergk <martinvonz@google.com>
parents: 41229
diff changeset
   279
    When assumeclean=True, files that are not known to be clean will also
8c366af085f4 narrow: reuse narrowspec.updateworkingcopy() when narrowing
Martin von Zweigbergk <martinvonz@google.com>
parents: 41229
diff changeset
   280
    be deleted. It is then up to the caller to make sure they are clean.
8c366af085f4 narrow: reuse narrowspec.updateworkingcopy() when narrowing
Martin von Zweigbergk <martinvonz@google.com>
parents: 41229
diff changeset
   281
    """
41046
ce0bc2952e2a narrow: detect if narrowspec was changed in a different share
Martin von Zweigbergk <martinvonz@google.com>
parents: 41044
diff changeset
   282
    oldspec = repo.vfs.tryread(DIRSTATE_FILENAME)
ce0bc2952e2a narrow: detect if narrowspec was changed in a different share
Martin von Zweigbergk <martinvonz@google.com>
parents: 41044
diff changeset
   283
    newspec = repo.svfs.tryread(FILENAME)
ce0bc2952e2a narrow: detect if narrowspec was changed in a different share
Martin von Zweigbergk <martinvonz@google.com>
parents: 41044
diff changeset
   284
ce0bc2952e2a narrow: detect if narrowspec was changed in a different share
Martin von Zweigbergk <martinvonz@google.com>
parents: 41044
diff changeset
   285
    oldincludes, oldexcludes = parseconfig(repo.ui, oldspec)
ce0bc2952e2a narrow: detect if narrowspec was changed in a different share
Martin von Zweigbergk <martinvonz@google.com>
parents: 41044
diff changeset
   286
    newincludes, newexcludes = parseconfig(repo.ui, newspec)
ce0bc2952e2a narrow: detect if narrowspec was changed in a different share
Martin von Zweigbergk <martinvonz@google.com>
parents: 41044
diff changeset
   287
    oldmatch = match(repo.root, include=oldincludes, exclude=oldexcludes)
ce0bc2952e2a narrow: detect if narrowspec was changed in a different share
Martin von Zweigbergk <martinvonz@google.com>
parents: 41044
diff changeset
   288
    newmatch = match(repo.root, include=newincludes, exclude=newexcludes)
ce0bc2952e2a narrow: detect if narrowspec was changed in a different share
Martin von Zweigbergk <martinvonz@google.com>
parents: 41044
diff changeset
   289
    addedmatch = matchmod.differencematcher(newmatch, oldmatch)
ce0bc2952e2a narrow: detect if narrowspec was changed in a different share
Martin von Zweigbergk <martinvonz@google.com>
parents: 41044
diff changeset
   290
    removedmatch = matchmod.differencematcher(oldmatch, newmatch)
ce0bc2952e2a narrow: detect if narrowspec was changed in a different share
Martin von Zweigbergk <martinvonz@google.com>
parents: 41044
diff changeset
   291
ce0bc2952e2a narrow: detect if narrowspec was changed in a different share
Martin von Zweigbergk <martinvonz@google.com>
parents: 41044
diff changeset
   292
    ds = repo.dirstate
ce0bc2952e2a narrow: detect if narrowspec was changed in a different share
Martin von Zweigbergk <martinvonz@google.com>
parents: 41044
diff changeset
   293
    lookup, status = ds.status(removedmatch, subrepos=[], ignored=False,
ce0bc2952e2a narrow: detect if narrowspec was changed in a different share
Martin von Zweigbergk <martinvonz@google.com>
parents: 41044
diff changeset
   294
                               clean=True, unknown=False)
41238
8c366af085f4 narrow: reuse narrowspec.updateworkingcopy() when narrowing
Martin von Zweigbergk <martinvonz@google.com>
parents: 41229
diff changeset
   295
    trackeddirty = status.modified + status.added
8c366af085f4 narrow: reuse narrowspec.updateworkingcopy() when narrowing
Martin von Zweigbergk <martinvonz@google.com>
parents: 41229
diff changeset
   296
    clean = status.clean
8c366af085f4 narrow: reuse narrowspec.updateworkingcopy() when narrowing
Martin von Zweigbergk <martinvonz@google.com>
parents: 41229
diff changeset
   297
    if assumeclean:
8c366af085f4 narrow: reuse narrowspec.updateworkingcopy() when narrowing
Martin von Zweigbergk <martinvonz@google.com>
parents: 41229
diff changeset
   298
        assert not trackeddirty
8c366af085f4 narrow: reuse narrowspec.updateworkingcopy() when narrowing
Martin von Zweigbergk <martinvonz@google.com>
parents: 41229
diff changeset
   299
        clean.extend(lookup)
8c366af085f4 narrow: reuse narrowspec.updateworkingcopy() when narrowing
Martin von Zweigbergk <martinvonz@google.com>
parents: 41229
diff changeset
   300
    else:
8c366af085f4 narrow: reuse narrowspec.updateworkingcopy() when narrowing
Martin von Zweigbergk <martinvonz@google.com>
parents: 41229
diff changeset
   301
        trackeddirty.extend(lookup)
8c366af085f4 narrow: reuse narrowspec.updateworkingcopy() when narrowing
Martin von Zweigbergk <martinvonz@google.com>
parents: 41229
diff changeset
   302
    _deletecleanfiles(repo, clean)
41046
ce0bc2952e2a narrow: detect if narrowspec was changed in a different share
Martin von Zweigbergk <martinvonz@google.com>
parents: 41044
diff changeset
   303
    for f in sorted(trackeddirty):
ce0bc2952e2a narrow: detect if narrowspec was changed in a different share
Martin von Zweigbergk <martinvonz@google.com>
parents: 41044
diff changeset
   304
        repo.ui.status(_('not deleting possibly dirty file %s\n') % f)
41238
8c366af085f4 narrow: reuse narrowspec.updateworkingcopy() when narrowing
Martin von Zweigbergk <martinvonz@google.com>
parents: 41229
diff changeset
   305
    for f in clean + trackeddirty:
41046
ce0bc2952e2a narrow: detect if narrowspec was changed in a different share
Martin von Zweigbergk <martinvonz@google.com>
parents: 41044
diff changeset
   306
        ds.drop(f)
ce0bc2952e2a narrow: detect if narrowspec was changed in a different share
Martin von Zweigbergk <martinvonz@google.com>
parents: 41044
diff changeset
   307
ce0bc2952e2a narrow: detect if narrowspec was changed in a different share
Martin von Zweigbergk <martinvonz@google.com>
parents: 41044
diff changeset
   308
    repo.narrowpats = newincludes, newexcludes
ce0bc2952e2a narrow: detect if narrowspec was changed in a different share
Martin von Zweigbergk <martinvonz@google.com>
parents: 41044
diff changeset
   309
    repo._narrowmatch = newmatch
ce0bc2952e2a narrow: detect if narrowspec was changed in a different share
Martin von Zweigbergk <martinvonz@google.com>
parents: 41044
diff changeset
   310
    pctx = repo['.']
ce0bc2952e2a narrow: detect if narrowspec was changed in a different share
Martin von Zweigbergk <martinvonz@google.com>
parents: 41044
diff changeset
   311
    newfiles = [f for f in pctx.manifest().walk(addedmatch) if f not in ds]
ce0bc2952e2a narrow: detect if narrowspec was changed in a different share
Martin von Zweigbergk <martinvonz@google.com>
parents: 41044
diff changeset
   312
    for f in newfiles:
ce0bc2952e2a narrow: detect if narrowspec was changed in a different share
Martin von Zweigbergk <martinvonz@google.com>
parents: 41044
diff changeset
   313
        ds.normallookup(f)
ce0bc2952e2a narrow: detect if narrowspec was changed in a different share
Martin von Zweigbergk <martinvonz@google.com>
parents: 41044
diff changeset
   314
    _writeaddedfiles(repo, pctx, newfiles)