mercurial/narrowspec.py
author Martin von Zweigbergk <martinvonz@google.com>
Thu, 18 Apr 2019 03:05:42 +0530
changeset 42192 770f5f586ae5
parent 41687 0531dff73d0b
child 42213 84bd6ae2d1f6
permissions -rw-r--r--
narrow: make warning about possibly dirty files respect ui.relative-paths Differential Revision: https://phab.mercurial-scm.org/D6264
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,
42192
770f5f586ae5 narrow: make warning about possibly dirty files respect ui.relative-paths
Martin von Zweigbergk <martinvonz@google.com>
parents: 41687
diff changeset
    18
    scmutil,
38878
f64ebe7d2259 narrowspec: use sparse.parseconfig() to parse narrowspec file (BC)
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 38875
diff changeset
    19
    sparse,
36117
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    20
    util,
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    21
)
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    22
41046
ce0bc2952e2a narrow: detect if narrowspec was changed in a different share
Martin von Zweigbergk <martinvonz@google.com>
parents: 41044
diff changeset
    23
# 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
    24
FILENAME = 'narrowspec'
41046
ce0bc2952e2a narrow: detect if narrowspec was changed in a different share
Martin von Zweigbergk <martinvonz@google.com>
parents: 41044
diff changeset
    25
# 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
    26
DIRSTATE_FILENAME = 'narrowspec.dirstate'
36117
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    27
39548
0d572769046a narrowspec: limit patterns to path: and rootfilesin: (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39542
diff changeset
    28
# 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
    29
# 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
    30
# 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
    31
# 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
    32
# 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
    33
# lightly - especially removals.
39548
0d572769046a narrowspec: limit patterns to path: and rootfilesin: (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39542
diff changeset
    34
VALID_PREFIXES = (
0d572769046a narrowspec: limit patterns to path: and rootfilesin: (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39542
diff changeset
    35
    b'path:',
0d572769046a narrowspec: limit patterns to path: and rootfilesin: (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39542
diff changeset
    36
    b'rootfilesin:',
0d572769046a narrowspec: limit patterns to path: and rootfilesin: (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39542
diff changeset
    37
)
0d572769046a narrowspec: limit patterns to path: and rootfilesin: (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39542
diff changeset
    38
36117
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    39
def normalizesplitpattern(kind, pat):
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    40
    """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
    41
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    42
    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
    43
    """
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    44
    pat = pat.rstrip('/')
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    45
    _validatepattern(pat)
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    46
    return kind, pat
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    47
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    48
def _numlines(s):
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    49
    """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
    50
    # 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
    51
    # 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
    52
    # 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
    53
    return len((s + 'x').splitlines())
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    54
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    55
def _validatepattern(pat):
36136
9c55bbc29dcf narrowspec: document constraints when validating patterns
Augie Fackler <augie@google.com>
parents: 36117
diff changeset
    56
    """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
    57
9c55bbc29dcf narrowspec: document constraints when validating patterns
Augie Fackler <augie@google.com>
parents: 36117
diff changeset
    58
    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
    59
    POSIX-style bytestring paths. There's no escaping.
9c55bbc29dcf narrowspec: document constraints when validating patterns
Augie Fackler <augie@google.com>
parents: 36117
diff changeset
    60
    """
36117
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    61
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    62
    # 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
    63
    # in patterns.
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    64
    if _numlines(pat) > 1:
36198
9fd8c2a3db5a narrowspec: move module into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36197
diff changeset
    65
        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
    66
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    67
    components = pat.split('/')
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    68
    if '.' in components or '..' in components:
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    69
        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
    70
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    71
def normalizepattern(pattern, defaultkind='path'):
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    72
    """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
    73
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    74
    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
    75
    """
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    76
    kind, pat = matchmod._patsplit(pattern, defaultkind)
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    77
    return '%s:%s' % normalizesplitpattern(kind, pat)
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    78
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    79
def parsepatterns(pats):
39548
0d572769046a narrowspec: limit patterns to path: and rootfilesin: (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39542
diff changeset
    80
    """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
    81
0d572769046a narrowspec: limit patterns to path: and rootfilesin: (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39542
diff changeset
    82
    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
    83
    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
    84
    See ``validatepatterns()``.
0d572769046a narrowspec: limit patterns to path: and rootfilesin: (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39542
diff changeset
    85
0d572769046a narrowspec: limit patterns to path: and rootfilesin: (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39542
diff changeset
    86
    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
    87
    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
    88
    representing patterns.
0d572769046a narrowspec: limit patterns to path: and rootfilesin: (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39542
diff changeset
    89
    """
0d572769046a narrowspec: limit patterns to path: and rootfilesin: (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39542
diff changeset
    90
    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
    91
    validatepatterns(res)
0d572769046a narrowspec: limit patterns to path: and rootfilesin: (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39542
diff changeset
    92
    return res
0d572769046a narrowspec: limit patterns to path: and rootfilesin: (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39542
diff changeset
    93
0d572769046a narrowspec: limit patterns to path: and rootfilesin: (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39542
diff changeset
    94
def validatepatterns(pats):
0d572769046a narrowspec: limit patterns to path: and rootfilesin: (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39542
diff changeset
    95
    """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
    96
0d572769046a narrowspec: limit patterns to path: and rootfilesin: (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39542
diff changeset
    97
    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
    98
    ``rootfilesin:``.
0d572769046a narrowspec: limit patterns to path: and rootfilesin: (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39542
diff changeset
    99
0d572769046a narrowspec: limit patterns to path: and rootfilesin: (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39542
diff changeset
   100
    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
   101
    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
   102
    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
   103
    """
0d572769046a narrowspec: limit patterns to path: and rootfilesin: (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39542
diff changeset
   104
    if not isinstance(pats, set):
0d572769046a narrowspec: limit patterns to path: and rootfilesin: (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39542
diff changeset
   105
        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
   106
                                     'got %r' % pats)
0d572769046a narrowspec: limit patterns to path: and rootfilesin: (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39542
diff changeset
   107
0d572769046a narrowspec: limit patterns to path: and rootfilesin: (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39542
diff changeset
   108
    for pat in pats:
0d572769046a narrowspec: limit patterns to path: and rootfilesin: (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39542
diff changeset
   109
        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
   110
            # 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
   111
            # 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
   112
            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
   113
                              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
   114
                                     'the following: %s') %
0d572769046a narrowspec: limit patterns to path: and rootfilesin: (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39542
diff changeset
   115
                                   ', '.join(VALID_PREFIXES))
36117
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   116
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   117
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
   118
    output = '[include]\n'
36117
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   119
    for i in sorted(includes - excludes):
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   120
        output += i + '\n'
38878
f64ebe7d2259 narrowspec: use sparse.parseconfig() to parse narrowspec file (BC)
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 38875
diff changeset
   121
    output += '[exclude]\n'
36117
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   122
    for e in sorted(excludes):
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   123
        output += e + '\n'
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   124
    return output
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   125
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   126
def match(root, include=None, exclude=None):
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   127
    if not include:
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   128
        # 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
   129
        # 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
   130
        # the nevermatcher.
41687
0531dff73d0b match: delete unused root and cwd arguments from {always,never,exact}() (API)
Martin von Zweigbergk <martinvonz@google.com>
parents: 41298
diff changeset
   131
        return matchmod.never()
36117
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   132
    return matchmod.match(root, '', [], include=include or [],
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   133
                          exclude=exclude or [])
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   134
40729
efd0f79246e3 narrow: extract helper for parsing narrowspec file
Martin von Zweigbergk <martinvonz@google.com>
parents: 39816
diff changeset
   135
def parseconfig(ui, spec):
efd0f79246e3 narrow: extract helper for parsing narrowspec file
Martin von Zweigbergk <martinvonz@google.com>
parents: 39816
diff changeset
   136
    # 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
   137
    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
   138
    if profiles:
efd0f79246e3 narrow: extract helper for parsing narrowspec file
Martin von Zweigbergk <martinvonz@google.com>
parents: 39816
diff changeset
   139
        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
   140
                            " supported in narrowspec"))
efd0f79246e3 narrow: extract helper for parsing narrowspec file
Martin von Zweigbergk <martinvonz@google.com>
parents: 39816
diff changeset
   141
efd0f79246e3 narrow: extract helper for parsing narrowspec file
Martin von Zweigbergk <martinvonz@google.com>
parents: 39816
diff changeset
   142
    validatepatterns(includepats)
efd0f79246e3 narrow: extract helper for parsing narrowspec file
Martin von Zweigbergk <martinvonz@google.com>
parents: 39816
diff changeset
   143
    validatepatterns(excludepats)
efd0f79246e3 narrow: extract helper for parsing narrowspec file
Martin von Zweigbergk <martinvonz@google.com>
parents: 39816
diff changeset
   144
efd0f79246e3 narrow: extract helper for parsing narrowspec file
Martin von Zweigbergk <martinvonz@google.com>
parents: 39816
diff changeset
   145
    return includepats, excludepats
efd0f79246e3 narrow: extract helper for parsing narrowspec file
Martin von Zweigbergk <martinvonz@google.com>
parents: 39816
diff changeset
   146
36117
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   147
def load(repo):
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   148
    try:
38911
576eef1ab43d narrow: move .hg/narrowspec to .hg/store/narrowspec (BC)
Martin von Zweigbergk <martinvonz@google.com>
parents: 38910
diff changeset
   149
        spec = repo.svfs.read(FILENAME)
36117
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   150
    except IOError as e:
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   151
        # 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
   152
        # and is empty".
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   153
        if e.errno == errno.ENOENT:
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   154
            return set(), set()
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   155
        raise
39556
8d8e61df8259 narrowspec: validate patterns when loading and saving spec file
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39548
diff changeset
   156
40729
efd0f79246e3 narrow: extract helper for parsing narrowspec file
Martin von Zweigbergk <martinvonz@google.com>
parents: 39816
diff changeset
   157
    return parseconfig(repo.ui, spec)
36117
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   158
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   159
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
   160
    validatepatterns(includepats)
8d8e61df8259 narrowspec: validate patterns when loading and saving spec file
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39548
diff changeset
   161
    validatepatterns(excludepats)
36117
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   162
    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
   163
    repo.svfs.write(FILENAME, spec)
36117
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   164
41229
50ca531f1f24 narrow: copy store narrowspec to working copy immediately
Martin von Zweigbergk <martinvonz@google.com>
parents: 41227
diff changeset
   165
def copytoworkingcopy(repo):
50ca531f1f24 narrow: copy store narrowspec to working copy immediately
Martin von Zweigbergk <martinvonz@google.com>
parents: 41227
diff changeset
   166
    spec = repo.svfs.read(FILENAME)
50ca531f1f24 narrow: copy store narrowspec to working copy immediately
Martin von Zweigbergk <martinvonz@google.com>
parents: 41227
diff changeset
   167
    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
   168
38908
ad24b581e4d9 narrow: call narrowspec.{save,restore,clear}backup directly
Martin von Zweigbergk <martinvonz@google.com>
parents: 38879
diff changeset
   169
def savebackup(repo, backupname):
ad24b581e4d9 narrow: call narrowspec.{save,restore,clear}backup directly
Martin von Zweigbergk <martinvonz@google.com>
parents: 38879
diff changeset
   170
    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
   171
        return
41044
1e8d9f472ea1 narrow: keep narrowspec backup in store
Martin von Zweigbergk <martinvonz@google.com>
parents: 40729
diff changeset
   172
    svfs = repo.svfs
1e8d9f472ea1 narrow: keep narrowspec backup in store
Martin von Zweigbergk <martinvonz@google.com>
parents: 40729
diff changeset
   173
    svfs.tryunlink(backupname)
1e8d9f472ea1 narrow: keep narrowspec backup in store
Martin von Zweigbergk <martinvonz@google.com>
parents: 40729
diff changeset
   174
    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
   175
38908
ad24b581e4d9 narrow: call narrowspec.{save,restore,clear}backup directly
Martin von Zweigbergk <martinvonz@google.com>
parents: 38879
diff changeset
   176
def restorebackup(repo, backupname):
ad24b581e4d9 narrow: call narrowspec.{save,restore,clear}backup directly
Martin von Zweigbergk <martinvonz@google.com>
parents: 38879
diff changeset
   177
    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
   178
        return
41044
1e8d9f472ea1 narrow: keep narrowspec backup in store
Martin von Zweigbergk <martinvonz@google.com>
parents: 40729
diff changeset
   179
    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
   180
41227
b74481038438 narrow: make dirstateguard back up and restore working copy narrowspec instead
Martin von Zweigbergk <martinvonz@google.com>
parents: 41177
diff changeset
   181
def savewcbackup(repo, backupname):
38908
ad24b581e4d9 narrow: call narrowspec.{save,restore,clear}backup directly
Martin von Zweigbergk <martinvonz@google.com>
parents: 38879
diff changeset
   182
    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
   183
        return
41227
b74481038438 narrow: make dirstateguard back up and restore working copy narrowspec instead
Martin von Zweigbergk <martinvonz@google.com>
parents: 41177
diff changeset
   184
    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
   185
    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
   186
    # 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
   187
    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
   188
        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
   189
                      hardlink=True)
b74481038438 narrow: make dirstateguard back up and restore working copy narrowspec instead
Martin von Zweigbergk <martinvonz@google.com>
parents: 41177
diff changeset
   190
b74481038438 narrow: make dirstateguard back up and restore working copy narrowspec instead
Martin von Zweigbergk <martinvonz@google.com>
parents: 41177
diff changeset
   191
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
   192
    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
   193
        return
41298
88a7c211b21e narrow: fix crash when restoring backup in legacy repo
Martin von Zweigbergk <martinvonz@google.com>
parents: 41238
diff changeset
   194
    # It may not exist in old repos
88a7c211b21e narrow: fix crash when restoring backup in legacy repo
Martin von Zweigbergk <martinvonz@google.com>
parents: 41238
diff changeset
   195
    if repo.vfs.exists(backupname):
88a7c211b21e narrow: fix crash when restoring backup in legacy repo
Martin von Zweigbergk <martinvonz@google.com>
parents: 41238
diff changeset
   196
        util.rename(repo.vfs.join(backupname), repo.vfs.join(DIRSTATE_FILENAME))
41227
b74481038438 narrow: make dirstateguard back up and restore working copy narrowspec instead
Martin von Zweigbergk <martinvonz@google.com>
parents: 41177
diff changeset
   197
b74481038438 narrow: make dirstateguard back up and restore working copy narrowspec instead
Martin von Zweigbergk <martinvonz@google.com>
parents: 41177
diff changeset
   198
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
   199
    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
   200
        return
41298
88a7c211b21e narrow: fix crash when restoring backup in legacy repo
Martin von Zweigbergk <martinvonz@google.com>
parents: 41238
diff changeset
   201
    repo.vfs.tryunlink(backupname)
38875
fed6fe856333 narrow: extract part of narrowspec backup to core
Martin von Zweigbergk <martinvonz@google.com>
parents: 36500
diff changeset
   202
36138
8fd0a9e2d7e9 narrow: make restrictpatterns a little more idiomatic
Augie Fackler <augie@google.com>
parents: 36137
diff changeset
   203
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
   204
    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
   205
    results in a logical AND operation
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   206
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   207
    :param req_includes: requested includes
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   208
    :param req_excludes: requested excludes
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   209
    :param repo_includes: repo includes
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   210
    :param repo_excludes: repo excludes
36138
8fd0a9e2d7e9 narrow: make restrictpatterns a little more idiomatic
Augie Fackler <augie@google.com>
parents: 36137
diff changeset
   211
    :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
   212
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   213
    >>> restrictpatterns({'f1','f2'}, {}, ['f1'], [])
36138
8fd0a9e2d7e9 narrow: make restrictpatterns a little more idiomatic
Augie Fackler <augie@google.com>
parents: 36137
diff changeset
   214
    (set(['f1']), {}, [])
36117
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   215
    >>> restrictpatterns({'f1'}, {}, ['f1','f2'], [])
36138
8fd0a9e2d7e9 narrow: make restrictpatterns a little more idiomatic
Augie Fackler <augie@google.com>
parents: 36137
diff changeset
   216
    (set(['f1']), {}, [])
36117
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   217
    >>> 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
   218
    (set(['f1/fc1']), {}, [])
36117
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   219
    >>> restrictpatterns({'f1_fc1'}, {}, ['f1','f2'], [])
36138
8fd0a9e2d7e9 narrow: make restrictpatterns a little more idiomatic
Augie Fackler <augie@google.com>
parents: 36137
diff changeset
   220
    ([], set(['path:.']), [])
36117
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   221
    >>> restrictpatterns({'f1/../f2/fc2'}, {}, ['f1','f2'], [])
36138
8fd0a9e2d7e9 narrow: make restrictpatterns a little more idiomatic
Augie Fackler <augie@google.com>
parents: 36137
diff changeset
   222
    (set(['f2/fc2']), {}, [])
36117
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   223
    >>> restrictpatterns({'f1/../f3/fc3'}, {}, ['f1','f2'], [])
36138
8fd0a9e2d7e9 narrow: make restrictpatterns a little more idiomatic
Augie Fackler <augie@google.com>
parents: 36137
diff changeset
   224
    ([], set(['path:.']), [])
36117
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   225
    >>> 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
   226
    (set(['f1/$non_exitent_var']), {}, [])
36117
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   227
    """
36137
b8bbe589fd47 narrowspec: consistently use set() to copy sets
Augie Fackler <augie@google.com>
parents: 36136
diff changeset
   228
    res_excludes = set(req_excludes)
36117
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   229
    res_excludes.update(repo_excludes)
36138
8fd0a9e2d7e9 narrow: make restrictpatterns a little more idiomatic
Augie Fackler <augie@google.com>
parents: 36137
diff changeset
   230
    invalid_includes = []
36117
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   231
    if not req_includes:
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   232
        res_includes = set(repo_includes)
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   233
    elif 'path:.' not in repo_includes:
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   234
        res_includes = []
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   235
        for req_include in req_includes:
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   236
            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
   237
            if req_include in repo_includes:
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   238
                res_includes.append(req_include)
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   239
                continue
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   240
            valid = False
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   241
            for repo_include in repo_includes:
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   242
                if req_include.startswith(repo_include + '/'):
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   243
                    valid = True
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   244
                    res_includes.append(req_include)
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   245
                    break
36138
8fd0a9e2d7e9 narrow: make restrictpatterns a little more idiomatic
Augie Fackler <augie@google.com>
parents: 36137
diff changeset
   246
            if not valid:
36117
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   247
                invalid_includes.append(req_include)
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   248
        if len(res_includes) == 0:
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   249
            res_excludes = {'path:.'}
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   250
        else:
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   251
            res_includes = set(res_includes)
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   252
    else:
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
   253
        res_includes = set(req_includes)
36138
8fd0a9e2d7e9 narrow: make restrictpatterns a little more idiomatic
Augie Fackler <augie@google.com>
parents: 36137
diff changeset
   254
    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
   255
ce0bc2952e2a narrow: detect if narrowspec was changed in a different share
Martin von Zweigbergk <martinvonz@google.com>
parents: 41044
diff changeset
   256
# 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
   257
# system)
ce0bc2952e2a narrow: detect if narrowspec was changed in a different share
Martin von Zweigbergk <martinvonz@google.com>
parents: 41044
diff changeset
   258
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
   259
    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
   260
        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
   261
ce0bc2952e2a narrow: detect if narrowspec was changed in a different share
Martin von Zweigbergk <martinvonz@google.com>
parents: 41044
diff changeset
   262
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
   263
    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
   264
    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
   265
    mf = repo['.'].manifest()
ce0bc2952e2a narrow: detect if narrowspec was changed in a different share
Martin von Zweigbergk <martinvonz@google.com>
parents: 41044
diff changeset
   266
    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
   267
        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
   268
            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
   269
    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
   270
                       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
   271
ce0bc2952e2a narrow: detect if narrowspec was changed in a different share
Martin von Zweigbergk <martinvonz@google.com>
parents: 41044
diff changeset
   272
def checkworkingcopynarrowspec(repo):
ce0bc2952e2a narrow: detect if narrowspec was changed in a different share
Martin von Zweigbergk <martinvonz@google.com>
parents: 41044
diff changeset
   273
    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
   274
    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
   275
    if wcspec != storespec:
ce0bc2952e2a narrow: detect if narrowspec was changed in a different share
Martin von Zweigbergk <martinvonz@google.com>
parents: 41044
diff changeset
   276
        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
   277
                          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
   278
41238
8c366af085f4 narrow: reuse narrowspec.updateworkingcopy() when narrowing
Martin von Zweigbergk <martinvonz@google.com>
parents: 41229
diff changeset
   279
def updateworkingcopy(repo, assumeclean=False):
8c366af085f4 narrow: reuse narrowspec.updateworkingcopy() when narrowing
Martin von Zweigbergk <martinvonz@google.com>
parents: 41229
diff changeset
   280
    """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
   281
8c366af085f4 narrow: reuse narrowspec.updateworkingcopy() when narrowing
Martin von Zweigbergk <martinvonz@google.com>
parents: 41229
diff changeset
   282
    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
   283
    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
   284
    """
41046
ce0bc2952e2a narrow: detect if narrowspec was changed in a different share
Martin von Zweigbergk <martinvonz@google.com>
parents: 41044
diff changeset
   285
    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
   286
    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
   287
ce0bc2952e2a narrow: detect if narrowspec was changed in a different share
Martin von Zweigbergk <martinvonz@google.com>
parents: 41044
diff changeset
   288
    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
   289
    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
   290
    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
   291
    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
   292
    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
   293
    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
   294
ce0bc2952e2a narrow: detect if narrowspec was changed in a different share
Martin von Zweigbergk <martinvonz@google.com>
parents: 41044
diff changeset
   295
    ds = repo.dirstate
ce0bc2952e2a narrow: detect if narrowspec was changed in a different share
Martin von Zweigbergk <martinvonz@google.com>
parents: 41044
diff changeset
   296
    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
   297
                               clean=True, unknown=False)
41238
8c366af085f4 narrow: reuse narrowspec.updateworkingcopy() when narrowing
Martin von Zweigbergk <martinvonz@google.com>
parents: 41229
diff changeset
   298
    trackeddirty = status.modified + status.added
8c366af085f4 narrow: reuse narrowspec.updateworkingcopy() when narrowing
Martin von Zweigbergk <martinvonz@google.com>
parents: 41229
diff changeset
   299
    clean = status.clean
8c366af085f4 narrow: reuse narrowspec.updateworkingcopy() when narrowing
Martin von Zweigbergk <martinvonz@google.com>
parents: 41229
diff changeset
   300
    if assumeclean:
8c366af085f4 narrow: reuse narrowspec.updateworkingcopy() when narrowing
Martin von Zweigbergk <martinvonz@google.com>
parents: 41229
diff changeset
   301
        assert not trackeddirty
8c366af085f4 narrow: reuse narrowspec.updateworkingcopy() when narrowing
Martin von Zweigbergk <martinvonz@google.com>
parents: 41229
diff changeset
   302
        clean.extend(lookup)
8c366af085f4 narrow: reuse narrowspec.updateworkingcopy() when narrowing
Martin von Zweigbergk <martinvonz@google.com>
parents: 41229
diff changeset
   303
    else:
8c366af085f4 narrow: reuse narrowspec.updateworkingcopy() when narrowing
Martin von Zweigbergk <martinvonz@google.com>
parents: 41229
diff changeset
   304
        trackeddirty.extend(lookup)
8c366af085f4 narrow: reuse narrowspec.updateworkingcopy() when narrowing
Martin von Zweigbergk <martinvonz@google.com>
parents: 41229
diff changeset
   305
    _deletecleanfiles(repo, clean)
42192
770f5f586ae5 narrow: make warning about possibly dirty files respect ui.relative-paths
Martin von Zweigbergk <martinvonz@google.com>
parents: 41687
diff changeset
   306
    uipathfn = scmutil.getuipathfn(repo)
41046
ce0bc2952e2a narrow: detect if narrowspec was changed in a different share
Martin von Zweigbergk <martinvonz@google.com>
parents: 41044
diff changeset
   307
    for f in sorted(trackeddirty):
42192
770f5f586ae5 narrow: make warning about possibly dirty files respect ui.relative-paths
Martin von Zweigbergk <martinvonz@google.com>
parents: 41687
diff changeset
   308
        repo.ui.status(_('not deleting possibly dirty file %s\n') % uipathfn(f))
41238
8c366af085f4 narrow: reuse narrowspec.updateworkingcopy() when narrowing
Martin von Zweigbergk <martinvonz@google.com>
parents: 41229
diff changeset
   309
    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
   310
        ds.drop(f)
ce0bc2952e2a narrow: detect if narrowspec was changed in a different share
Martin von Zweigbergk <martinvonz@google.com>
parents: 41044
diff changeset
   311
ce0bc2952e2a narrow: detect if narrowspec was changed in a different share
Martin von Zweigbergk <martinvonz@google.com>
parents: 41044
diff changeset
   312
    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
   313
    repo._narrowmatch = newmatch
ce0bc2952e2a narrow: detect if narrowspec was changed in a different share
Martin von Zweigbergk <martinvonz@google.com>
parents: 41044
diff changeset
   314
    pctx = repo['.']
ce0bc2952e2a narrow: detect if narrowspec was changed in a different share
Martin von Zweigbergk <martinvonz@google.com>
parents: 41044
diff changeset
   315
    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
   316
    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
   317
        ds.normallookup(f)
ce0bc2952e2a narrow: detect if narrowspec was changed in a different share
Martin von Zweigbergk <martinvonz@google.com>
parents: 41044
diff changeset
   318
    _writeaddedfiles(repo, pctx, newfiles)