mercurial/sparse.py
author Gregory Szorc <gregory.szorc@gmail.com>
Fri, 07 Jul 2017 11:51:10 -0700
changeset 33353 160efb559f67
parent 33325 38df146d0697
child 33354 4695f1829045
permissions -rw-r--r--
sparse: move post commit actions into core Instead of wrapping committablectx.markcommitted(), we inline the call into workingctx.markcommitted(). Per smf's review, workingctx is the proper location for this code, as committablectx is the shared base class for it and memctx. Since this code touches the working directory, it belongs in workingctx.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
33297
ba5d89774db6 sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     1
# sparse.py - functionality for sparse checkouts
ba5d89774db6 sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     2
#
ba5d89774db6 sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     3
# Copyright 2014 Facebook, Inc.
ba5d89774db6 sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     4
#
ba5d89774db6 sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     5
# This software may be used and distributed according to the terms of the
ba5d89774db6 sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     6
# GNU General Public License version 2 or any later version.
ba5d89774db6 sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     7
ba5d89774db6 sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     8
from __future__ import absolute_import
ba5d89774db6 sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     9
33321
d09e948dc303 sparse: move pruning of temporary includes into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33320
diff changeset
    10
import collections
33317
df1287268cc0 sparse: move config signature logic into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33304
diff changeset
    11
import hashlib
33320
153456f02426 sparse: move function for resolving sparse matcher into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33318
diff changeset
    12
import os
33317
df1287268cc0 sparse: move config signature logic into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33304
diff changeset
    13
33297
ba5d89774db6 sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    14
from .i18n import _
33301
ca4b78eb11e7 sparse: move active profiles function into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33300
diff changeset
    15
from .node import nullid
33297
ba5d89774db6 sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    16
from . import (
ba5d89774db6 sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    17
    error,
33320
153456f02426 sparse: move function for resolving sparse matcher into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33318
diff changeset
    18
    match as matchmod,
33321
d09e948dc303 sparse: move pruning of temporary includes into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33320
diff changeset
    19
    merge as mergemod,
33320
153456f02426 sparse: move function for resolving sparse matcher into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33318
diff changeset
    20
    pycompat,
33297
ba5d89774db6 sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    21
)
ba5d89774db6 sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    22
33299
41448fc51510 sparse: variable to track if sparse is enabled
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33298
diff changeset
    23
# Whether sparse features are enabled. This variable is intended to be
41448fc51510 sparse: variable to track if sparse is enabled
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33298
diff changeset
    24
# temporary to facilitate porting sparse to core. It should eventually be
41448fc51510 sparse: variable to track if sparse is enabled
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33298
diff changeset
    25
# a per-repo option, possibly a repo requirement.
41448fc51510 sparse: variable to track if sparse is enabled
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33298
diff changeset
    26
enabled = False
41448fc51510 sparse: variable to track if sparse is enabled
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33298
diff changeset
    27
33297
ba5d89774db6 sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    28
def parseconfig(ui, raw):
ba5d89774db6 sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    29
    """Parse sparse config file content.
ba5d89774db6 sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    30
ba5d89774db6 sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    31
    Returns a tuple of includes, excludes, and profiles.
ba5d89774db6 sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    32
    """
ba5d89774db6 sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    33
    includes = set()
ba5d89774db6 sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    34
    excludes = set()
ba5d89774db6 sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    35
    current = includes
ba5d89774db6 sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    36
    profiles = []
ba5d89774db6 sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    37
    for line in raw.split('\n'):
ba5d89774db6 sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    38
        line = line.strip()
ba5d89774db6 sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    39
        if not line or line.startswith('#'):
ba5d89774db6 sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    40
            # empty or comment line, skip
ba5d89774db6 sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    41
            continue
ba5d89774db6 sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    42
        elif line.startswith('%include '):
ba5d89774db6 sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    43
            line = line[9:].strip()
ba5d89774db6 sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    44
            if line:
ba5d89774db6 sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    45
                profiles.append(line)
ba5d89774db6 sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    46
        elif line == '[include]':
ba5d89774db6 sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    47
            if current != includes:
ba5d89774db6 sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    48
                # TODO pass filename into this API so we can report it.
ba5d89774db6 sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    49
                raise error.Abort(_('sparse config cannot have includes ' +
ba5d89774db6 sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    50
                                    'after excludes'))
ba5d89774db6 sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    51
            continue
ba5d89774db6 sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    52
        elif line == '[exclude]':
ba5d89774db6 sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    53
            current = excludes
ba5d89774db6 sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    54
        elif line:
ba5d89774db6 sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    55
            if line.strip().startswith('/'):
ba5d89774db6 sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    56
                ui.warn(_('warning: sparse profile cannot use' +
ba5d89774db6 sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    57
                          ' paths starting with /, ignoring %s\n') % line)
ba5d89774db6 sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    58
                continue
ba5d89774db6 sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    59
            current.add(line)
ba5d89774db6 sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    60
ba5d89774db6 sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    61
    return includes, excludes, profiles
33298
f41a99c45956 sparse: move profile reading into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33297
diff changeset
    62
f41a99c45956 sparse: move profile reading into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33297
diff changeset
    63
# Exists as separate function to facilitate monkeypatching.
f41a99c45956 sparse: move profile reading into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33297
diff changeset
    64
def readprofile(repo, profile, changeid):
f41a99c45956 sparse: move profile reading into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33297
diff changeset
    65
    """Resolve the raw content of a sparse profile file."""
f41a99c45956 sparse: move profile reading into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33297
diff changeset
    66
    # TODO add some kind of cache here because this incurs a manifest
f41a99c45956 sparse: move profile reading into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33297
diff changeset
    67
    # resolve and can be slow.
f41a99c45956 sparse: move profile reading into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33297
diff changeset
    68
    return repo.filectx(profile, changeid=changeid).data()
33300
f7a106b3f089 sparse: move resolving of sparse patterns for rev into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33299
diff changeset
    69
f7a106b3f089 sparse: move resolving of sparse patterns for rev into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33299
diff changeset
    70
def patternsforrev(repo, rev):
f7a106b3f089 sparse: move resolving of sparse patterns for rev into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33299
diff changeset
    71
    """Obtain sparse checkout patterns for the given rev.
f7a106b3f089 sparse: move resolving of sparse patterns for rev into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33299
diff changeset
    72
f7a106b3f089 sparse: move resolving of sparse patterns for rev into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33299
diff changeset
    73
    Returns a tuple of iterables representing includes, excludes, and
f7a106b3f089 sparse: move resolving of sparse patterns for rev into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33299
diff changeset
    74
    patterns.
f7a106b3f089 sparse: move resolving of sparse patterns for rev into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33299
diff changeset
    75
    """
f7a106b3f089 sparse: move resolving of sparse patterns for rev into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33299
diff changeset
    76
    # Feature isn't enabled. No-op.
f7a106b3f089 sparse: move resolving of sparse patterns for rev into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33299
diff changeset
    77
    if not enabled:
f7a106b3f089 sparse: move resolving of sparse patterns for rev into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33299
diff changeset
    78
        return set(), set(), []
f7a106b3f089 sparse: move resolving of sparse patterns for rev into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33299
diff changeset
    79
f7a106b3f089 sparse: move resolving of sparse patterns for rev into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33299
diff changeset
    80
    raw = repo.vfs.tryread('sparse')
f7a106b3f089 sparse: move resolving of sparse patterns for rev into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33299
diff changeset
    81
    if not raw:
f7a106b3f089 sparse: move resolving of sparse patterns for rev into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33299
diff changeset
    82
        return set(), set(), []
f7a106b3f089 sparse: move resolving of sparse patterns for rev into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33299
diff changeset
    83
f7a106b3f089 sparse: move resolving of sparse patterns for rev into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33299
diff changeset
    84
    if rev is None:
f7a106b3f089 sparse: move resolving of sparse patterns for rev into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33299
diff changeset
    85
        raise error.Abort(_('cannot parse sparse patterns from working '
f7a106b3f089 sparse: move resolving of sparse patterns for rev into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33299
diff changeset
    86
                            'directory'))
f7a106b3f089 sparse: move resolving of sparse patterns for rev into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33299
diff changeset
    87
f7a106b3f089 sparse: move resolving of sparse patterns for rev into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33299
diff changeset
    88
    includes, excludes, profiles = parseconfig(repo.ui, raw)
f7a106b3f089 sparse: move resolving of sparse patterns for rev into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33299
diff changeset
    89
    ctx = repo[rev]
f7a106b3f089 sparse: move resolving of sparse patterns for rev into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33299
diff changeset
    90
f7a106b3f089 sparse: move resolving of sparse patterns for rev into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33299
diff changeset
    91
    if profiles:
f7a106b3f089 sparse: move resolving of sparse patterns for rev into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33299
diff changeset
    92
        visited = set()
f7a106b3f089 sparse: move resolving of sparse patterns for rev into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33299
diff changeset
    93
        while profiles:
f7a106b3f089 sparse: move resolving of sparse patterns for rev into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33299
diff changeset
    94
            profile = profiles.pop()
f7a106b3f089 sparse: move resolving of sparse patterns for rev into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33299
diff changeset
    95
            if profile in visited:
f7a106b3f089 sparse: move resolving of sparse patterns for rev into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33299
diff changeset
    96
                continue
f7a106b3f089 sparse: move resolving of sparse patterns for rev into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33299
diff changeset
    97
f7a106b3f089 sparse: move resolving of sparse patterns for rev into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33299
diff changeset
    98
            visited.add(profile)
f7a106b3f089 sparse: move resolving of sparse patterns for rev into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33299
diff changeset
    99
f7a106b3f089 sparse: move resolving of sparse patterns for rev into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33299
diff changeset
   100
            try:
f7a106b3f089 sparse: move resolving of sparse patterns for rev into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33299
diff changeset
   101
                raw = readprofile(repo, profile, rev)
f7a106b3f089 sparse: move resolving of sparse patterns for rev into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33299
diff changeset
   102
            except error.ManifestLookupError:
f7a106b3f089 sparse: move resolving of sparse patterns for rev into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33299
diff changeset
   103
                msg = (
f7a106b3f089 sparse: move resolving of sparse patterns for rev into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33299
diff changeset
   104
                    "warning: sparse profile '%s' not found "
f7a106b3f089 sparse: move resolving of sparse patterns for rev into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33299
diff changeset
   105
                    "in rev %s - ignoring it\n" % (profile, ctx))
f7a106b3f089 sparse: move resolving of sparse patterns for rev into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33299
diff changeset
   106
                # experimental config: sparse.missingwarning
f7a106b3f089 sparse: move resolving of sparse patterns for rev into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33299
diff changeset
   107
                if repo.ui.configbool(
f7a106b3f089 sparse: move resolving of sparse patterns for rev into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33299
diff changeset
   108
                        'sparse', 'missingwarning', True):
f7a106b3f089 sparse: move resolving of sparse patterns for rev into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33299
diff changeset
   109
                    repo.ui.warn(msg)
f7a106b3f089 sparse: move resolving of sparse patterns for rev into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33299
diff changeset
   110
                else:
f7a106b3f089 sparse: move resolving of sparse patterns for rev into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33299
diff changeset
   111
                    repo.ui.debug(msg)
f7a106b3f089 sparse: move resolving of sparse patterns for rev into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33299
diff changeset
   112
                continue
f7a106b3f089 sparse: move resolving of sparse patterns for rev into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33299
diff changeset
   113
f7a106b3f089 sparse: move resolving of sparse patterns for rev into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33299
diff changeset
   114
            pincludes, pexcludes, subprofs = parseconfig(repo.ui, raw)
f7a106b3f089 sparse: move resolving of sparse patterns for rev into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33299
diff changeset
   115
            includes.update(pincludes)
f7a106b3f089 sparse: move resolving of sparse patterns for rev into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33299
diff changeset
   116
            excludes.update(pexcludes)
f7a106b3f089 sparse: move resolving of sparse patterns for rev into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33299
diff changeset
   117
            for subprofile in subprofs:
f7a106b3f089 sparse: move resolving of sparse patterns for rev into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33299
diff changeset
   118
                profiles.append(subprofile)
f7a106b3f089 sparse: move resolving of sparse patterns for rev into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33299
diff changeset
   119
f7a106b3f089 sparse: move resolving of sparse patterns for rev into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33299
diff changeset
   120
        profiles = visited
f7a106b3f089 sparse: move resolving of sparse patterns for rev into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33299
diff changeset
   121
f7a106b3f089 sparse: move resolving of sparse patterns for rev into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33299
diff changeset
   122
    if includes:
f7a106b3f089 sparse: move resolving of sparse patterns for rev into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33299
diff changeset
   123
        includes.add('.hg*')
f7a106b3f089 sparse: move resolving of sparse patterns for rev into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33299
diff changeset
   124
f7a106b3f089 sparse: move resolving of sparse patterns for rev into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33299
diff changeset
   125
    return includes, excludes, profiles
33301
ca4b78eb11e7 sparse: move active profiles function into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33300
diff changeset
   126
ca4b78eb11e7 sparse: move active profiles function into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33300
diff changeset
   127
def activeprofiles(repo):
ca4b78eb11e7 sparse: move active profiles function into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33300
diff changeset
   128
    revs = [repo.changelog.rev(node) for node in
ca4b78eb11e7 sparse: move active profiles function into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33300
diff changeset
   129
            repo.dirstate.parents() if node != nullid]
ca4b78eb11e7 sparse: move active profiles function into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33300
diff changeset
   130
ca4b78eb11e7 sparse: move active profiles function into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33300
diff changeset
   131
    profiles = set()
ca4b78eb11e7 sparse: move active profiles function into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33300
diff changeset
   132
    for rev in revs:
ca4b78eb11e7 sparse: move active profiles function into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33300
diff changeset
   133
        profiles.update(patternsforrev(repo, rev)[2])
ca4b78eb11e7 sparse: move active profiles function into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33300
diff changeset
   134
ca4b78eb11e7 sparse: move active profiles function into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33300
diff changeset
   135
    return profiles
33302
36a415b5a4b2 localrepo: add sparse caches
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33301
diff changeset
   136
33317
df1287268cc0 sparse: move config signature logic into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33304
diff changeset
   137
def configsignature(repo, includetemp=True):
df1287268cc0 sparse: move config signature logic into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33304
diff changeset
   138
    """Obtain the signature string for the current sparse configuration.
df1287268cc0 sparse: move config signature logic into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33304
diff changeset
   139
df1287268cc0 sparse: move config signature logic into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33304
diff changeset
   140
    This is used to construct a cache key for matchers.
df1287268cc0 sparse: move config signature logic into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33304
diff changeset
   141
    """
df1287268cc0 sparse: move config signature logic into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33304
diff changeset
   142
    cache = repo._sparsesignaturecache
df1287268cc0 sparse: move config signature logic into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33304
diff changeset
   143
df1287268cc0 sparse: move config signature logic into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33304
diff changeset
   144
    signature = cache.get('signature')
df1287268cc0 sparse: move config signature logic into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33304
diff changeset
   145
df1287268cc0 sparse: move config signature logic into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33304
diff changeset
   146
    if includetemp:
df1287268cc0 sparse: move config signature logic into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33304
diff changeset
   147
        tempsignature = cache.get('tempsignature')
df1287268cc0 sparse: move config signature logic into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33304
diff changeset
   148
    else:
33318
526255fe7899 sparse: clean up config signature code
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33317
diff changeset
   149
        tempsignature = '0'
33317
df1287268cc0 sparse: move config signature logic into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33304
diff changeset
   150
df1287268cc0 sparse: move config signature logic into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33304
diff changeset
   151
    if signature is None or (includetemp and tempsignature is None):
33318
526255fe7899 sparse: clean up config signature code
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33317
diff changeset
   152
        signature = hashlib.sha1(repo.vfs.tryread('sparse')).hexdigest()
33317
df1287268cc0 sparse: move config signature logic into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33304
diff changeset
   153
        cache['signature'] = signature
df1287268cc0 sparse: move config signature logic into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33304
diff changeset
   154
df1287268cc0 sparse: move config signature logic into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33304
diff changeset
   155
        if includetemp:
33318
526255fe7899 sparse: clean up config signature code
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33317
diff changeset
   156
            raw = repo.vfs.tryread('tempsparse')
526255fe7899 sparse: clean up config signature code
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33317
diff changeset
   157
            tempsignature = hashlib.sha1(raw).hexdigest()
33317
df1287268cc0 sparse: move config signature logic into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33304
diff changeset
   158
            cache['tempsignature'] = tempsignature
df1287268cc0 sparse: move config signature logic into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33304
diff changeset
   159
33318
526255fe7899 sparse: clean up config signature code
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33317
diff changeset
   160
    return '%s %s' % (signature, tempsignature)
33317
df1287268cc0 sparse: move config signature logic into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33304
diff changeset
   161
33303
8b571495d811 sparse: move config file writing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33302
diff changeset
   162
def writeconfig(repo, includes, excludes, profiles):
8b571495d811 sparse: move config file writing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33302
diff changeset
   163
    """Write the sparse config file given a sparse configuration."""
8b571495d811 sparse: move config file writing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33302
diff changeset
   164
    with repo.vfs('sparse', 'wb') as fh:
8b571495d811 sparse: move config file writing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33302
diff changeset
   165
        for p in sorted(profiles):
8b571495d811 sparse: move config file writing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33302
diff changeset
   166
            fh.write('%%include %s\n' % p)
8b571495d811 sparse: move config file writing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33302
diff changeset
   167
8b571495d811 sparse: move config file writing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33302
diff changeset
   168
        if includes:
8b571495d811 sparse: move config file writing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33302
diff changeset
   169
            fh.write('[include]\n')
8b571495d811 sparse: move config file writing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33302
diff changeset
   170
            for i in sorted(includes):
8b571495d811 sparse: move config file writing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33302
diff changeset
   171
                fh.write(i)
8b571495d811 sparse: move config file writing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33302
diff changeset
   172
                fh.write('\n')
8b571495d811 sparse: move config file writing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33302
diff changeset
   173
8b571495d811 sparse: move config file writing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33302
diff changeset
   174
        if excludes:
8b571495d811 sparse: move config file writing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33302
diff changeset
   175
            fh.write('[exclude]\n')
8b571495d811 sparse: move config file writing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33302
diff changeset
   176
            for e in sorted(excludes):
8b571495d811 sparse: move config file writing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33302
diff changeset
   177
                fh.write(e)
8b571495d811 sparse: move config file writing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33302
diff changeset
   178
                fh.write('\n')
8b571495d811 sparse: move config file writing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33302
diff changeset
   179
33325
38df146d0697 sparse: inline signature cache clearing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33324
diff changeset
   180
    repo._sparsesignaturecache.clear()
33304
3e1accab7447 sparse: move some temporary includes functions into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33303
diff changeset
   181
3e1accab7447 sparse: move some temporary includes functions into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33303
diff changeset
   182
def readtemporaryincludes(repo):
3e1accab7447 sparse: move some temporary includes functions into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33303
diff changeset
   183
    raw = repo.vfs.tryread('tempsparse')
3e1accab7447 sparse: move some temporary includes functions into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33303
diff changeset
   184
    if not raw:
3e1accab7447 sparse: move some temporary includes functions into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33303
diff changeset
   185
        return set()
3e1accab7447 sparse: move some temporary includes functions into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33303
diff changeset
   186
3e1accab7447 sparse: move some temporary includes functions into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33303
diff changeset
   187
    return set(raw.split('\n'))
3e1accab7447 sparse: move some temporary includes functions into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33303
diff changeset
   188
3e1accab7447 sparse: move some temporary includes functions into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33303
diff changeset
   189
def writetemporaryincludes(repo, includes):
3e1accab7447 sparse: move some temporary includes functions into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33303
diff changeset
   190
    repo.vfs.write('tempsparse', '\n'.join(sorted(includes)))
33325
38df146d0697 sparse: inline signature cache clearing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33324
diff changeset
   191
    repo._sparsesignaturecache.clear()
33304
3e1accab7447 sparse: move some temporary includes functions into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33303
diff changeset
   192
3e1accab7447 sparse: move some temporary includes functions into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33303
diff changeset
   193
def addtemporaryincludes(repo, additional):
3e1accab7447 sparse: move some temporary includes functions into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33303
diff changeset
   194
    includes = readtemporaryincludes(repo)
3e1accab7447 sparse: move some temporary includes functions into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33303
diff changeset
   195
    for i in additional:
3e1accab7447 sparse: move some temporary includes functions into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33303
diff changeset
   196
        includes.add(i)
3e1accab7447 sparse: move some temporary includes functions into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33303
diff changeset
   197
    writetemporaryincludes(repo, includes)
33320
153456f02426 sparse: move function for resolving sparse matcher into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33318
diff changeset
   198
33321
d09e948dc303 sparse: move pruning of temporary includes into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33320
diff changeset
   199
def prunetemporaryincludes(repo):
d09e948dc303 sparse: move pruning of temporary includes into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33320
diff changeset
   200
    if not enabled or not repo.vfs.exists('tempsparse'):
d09e948dc303 sparse: move pruning of temporary includes into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33320
diff changeset
   201
        return
d09e948dc303 sparse: move pruning of temporary includes into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33320
diff changeset
   202
d09e948dc303 sparse: move pruning of temporary includes into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33320
diff changeset
   203
    origstatus = repo.status()
d09e948dc303 sparse: move pruning of temporary includes into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33320
diff changeset
   204
    modified, added, removed, deleted, a, b, c = origstatus
d09e948dc303 sparse: move pruning of temporary includes into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33320
diff changeset
   205
    if modified or added or removed or deleted:
d09e948dc303 sparse: move pruning of temporary includes into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33320
diff changeset
   206
        # Still have pending changes. Don't bother trying to prune.
d09e948dc303 sparse: move pruning of temporary includes into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33320
diff changeset
   207
        return
d09e948dc303 sparse: move pruning of temporary includes into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33320
diff changeset
   208
d09e948dc303 sparse: move pruning of temporary includes into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33320
diff changeset
   209
    sparsematch = matcher(repo, includetemp=False)
d09e948dc303 sparse: move pruning of temporary includes into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33320
diff changeset
   210
    dirstate = repo.dirstate
d09e948dc303 sparse: move pruning of temporary includes into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33320
diff changeset
   211
    actions = []
d09e948dc303 sparse: move pruning of temporary includes into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33320
diff changeset
   212
    dropped = []
d09e948dc303 sparse: move pruning of temporary includes into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33320
diff changeset
   213
    tempincludes = readtemporaryincludes(repo)
d09e948dc303 sparse: move pruning of temporary includes into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33320
diff changeset
   214
    for file in tempincludes:
d09e948dc303 sparse: move pruning of temporary includes into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33320
diff changeset
   215
        if file in dirstate and not sparsematch(file):
d09e948dc303 sparse: move pruning of temporary includes into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33320
diff changeset
   216
            message = _('dropping temporarily included sparse files')
d09e948dc303 sparse: move pruning of temporary includes into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33320
diff changeset
   217
            actions.append((file, None, message))
d09e948dc303 sparse: move pruning of temporary includes into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33320
diff changeset
   218
            dropped.append(file)
d09e948dc303 sparse: move pruning of temporary includes into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33320
diff changeset
   219
d09e948dc303 sparse: move pruning of temporary includes into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33320
diff changeset
   220
    typeactions = collections.defaultdict(list)
d09e948dc303 sparse: move pruning of temporary includes into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33320
diff changeset
   221
    typeactions['r'] = actions
d09e948dc303 sparse: move pruning of temporary includes into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33320
diff changeset
   222
    mergemod.applyupdates(repo, typeactions, repo[None], repo['.'], False)
d09e948dc303 sparse: move pruning of temporary includes into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33320
diff changeset
   223
d09e948dc303 sparse: move pruning of temporary includes into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33320
diff changeset
   224
    # Fix dirstate
d09e948dc303 sparse: move pruning of temporary includes into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33320
diff changeset
   225
    for file in dropped:
d09e948dc303 sparse: move pruning of temporary includes into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33320
diff changeset
   226
        dirstate.drop(file)
d09e948dc303 sparse: move pruning of temporary includes into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33320
diff changeset
   227
d09e948dc303 sparse: move pruning of temporary includes into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33320
diff changeset
   228
    repo.vfs.unlink('tempsparse')
33325
38df146d0697 sparse: inline signature cache clearing
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33324
diff changeset
   229
    repo._sparsesignaturecache.clear()
33321
d09e948dc303 sparse: move pruning of temporary includes into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33320
diff changeset
   230
    msg = _('cleaned up %d temporarily added file(s) from the '
d09e948dc303 sparse: move pruning of temporary includes into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33320
diff changeset
   231
            'sparse checkout\n')
d09e948dc303 sparse: move pruning of temporary includes into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33320
diff changeset
   232
    repo.ui.status(msg % len(tempincludes))
d09e948dc303 sparse: move pruning of temporary includes into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33320
diff changeset
   233
33320
153456f02426 sparse: move function for resolving sparse matcher into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33318
diff changeset
   234
def matcher(repo, revs=None, includetemp=True):
153456f02426 sparse: move function for resolving sparse matcher into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33318
diff changeset
   235
    """Obtain a matcher for sparse working directories for the given revs.
153456f02426 sparse: move function for resolving sparse matcher into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33318
diff changeset
   236
153456f02426 sparse: move function for resolving sparse matcher into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33318
diff changeset
   237
    If multiple revisions are specified, the matcher is the union of all
153456f02426 sparse: move function for resolving sparse matcher into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33318
diff changeset
   238
    revs.
153456f02426 sparse: move function for resolving sparse matcher into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33318
diff changeset
   239
153456f02426 sparse: move function for resolving sparse matcher into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33318
diff changeset
   240
    ``includetemp`` indicates whether to use the temporary sparse profile.
153456f02426 sparse: move function for resolving sparse matcher into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33318
diff changeset
   241
    """
153456f02426 sparse: move function for resolving sparse matcher into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33318
diff changeset
   242
    # If sparse isn't enabled, sparse matcher matches everything.
153456f02426 sparse: move function for resolving sparse matcher into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33318
diff changeset
   243
    if not enabled:
153456f02426 sparse: move function for resolving sparse matcher into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33318
diff changeset
   244
        return matchmod.always(repo.root, '')
153456f02426 sparse: move function for resolving sparse matcher into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33318
diff changeset
   245
153456f02426 sparse: move function for resolving sparse matcher into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33318
diff changeset
   246
    if not revs or revs == [None]:
153456f02426 sparse: move function for resolving sparse matcher into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33318
diff changeset
   247
        revs = [repo.changelog.rev(node)
153456f02426 sparse: move function for resolving sparse matcher into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33318
diff changeset
   248
                for node in repo.dirstate.parents() if node != nullid]
153456f02426 sparse: move function for resolving sparse matcher into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33318
diff changeset
   249
153456f02426 sparse: move function for resolving sparse matcher into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33318
diff changeset
   250
    signature = configsignature(repo, includetemp=includetemp)
153456f02426 sparse: move function for resolving sparse matcher into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33318
diff changeset
   251
153456f02426 sparse: move function for resolving sparse matcher into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33318
diff changeset
   252
    key = '%s %s' % (signature, ' '.join(map(pycompat.bytestr, revs)))
153456f02426 sparse: move function for resolving sparse matcher into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33318
diff changeset
   253
153456f02426 sparse: move function for resolving sparse matcher into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33318
diff changeset
   254
    result = repo._sparsematchercache.get(key)
153456f02426 sparse: move function for resolving sparse matcher into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33318
diff changeset
   255
    if result:
153456f02426 sparse: move function for resolving sparse matcher into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33318
diff changeset
   256
        return result
153456f02426 sparse: move function for resolving sparse matcher into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33318
diff changeset
   257
153456f02426 sparse: move function for resolving sparse matcher into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33318
diff changeset
   258
    matchers = []
153456f02426 sparse: move function for resolving sparse matcher into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33318
diff changeset
   259
    for rev in revs:
153456f02426 sparse: move function for resolving sparse matcher into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33318
diff changeset
   260
        try:
153456f02426 sparse: move function for resolving sparse matcher into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33318
diff changeset
   261
            includes, excludes, profiles = patternsforrev(repo, rev)
153456f02426 sparse: move function for resolving sparse matcher into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33318
diff changeset
   262
153456f02426 sparse: move function for resolving sparse matcher into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33318
diff changeset
   263
            if includes or excludes:
153456f02426 sparse: move function for resolving sparse matcher into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33318
diff changeset
   264
                # Explicitly include subdirectories of includes so
153456f02426 sparse: move function for resolving sparse matcher into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33318
diff changeset
   265
                # status will walk them down to the actual include.
153456f02426 sparse: move function for resolving sparse matcher into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33318
diff changeset
   266
                subdirs = set()
153456f02426 sparse: move function for resolving sparse matcher into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33318
diff changeset
   267
                for include in includes:
153456f02426 sparse: move function for resolving sparse matcher into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33318
diff changeset
   268
                    # TODO consider using posix path functions here so Windows
153456f02426 sparse: move function for resolving sparse matcher into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33318
diff changeset
   269
                    # \ directory separators don't come into play.
153456f02426 sparse: move function for resolving sparse matcher into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33318
diff changeset
   270
                    dirname = os.path.dirname(include)
153456f02426 sparse: move function for resolving sparse matcher into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33318
diff changeset
   271
                    # basename is used to avoid issues with absolute
153456f02426 sparse: move function for resolving sparse matcher into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33318
diff changeset
   272
                    # paths (which on Windows can include the drive).
153456f02426 sparse: move function for resolving sparse matcher into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33318
diff changeset
   273
                    while os.path.basename(dirname):
153456f02426 sparse: move function for resolving sparse matcher into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33318
diff changeset
   274
                        subdirs.add(dirname)
153456f02426 sparse: move function for resolving sparse matcher into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33318
diff changeset
   275
                        dirname = os.path.dirname(dirname)
153456f02426 sparse: move function for resolving sparse matcher into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33318
diff changeset
   276
153456f02426 sparse: move function for resolving sparse matcher into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33318
diff changeset
   277
                matcher = matchmod.match(repo.root, '', [],
153456f02426 sparse: move function for resolving sparse matcher into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33318
diff changeset
   278
                                         include=includes, exclude=excludes,
153456f02426 sparse: move function for resolving sparse matcher into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33318
diff changeset
   279
                                         default='relpath')
153456f02426 sparse: move function for resolving sparse matcher into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33318
diff changeset
   280
                if subdirs:
153456f02426 sparse: move function for resolving sparse matcher into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33318
diff changeset
   281
                    matcher = matchmod.forceincludematcher(matcher, subdirs)
153456f02426 sparse: move function for resolving sparse matcher into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33318
diff changeset
   282
                matchers.append(matcher)
153456f02426 sparse: move function for resolving sparse matcher into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33318
diff changeset
   283
        except IOError:
153456f02426 sparse: move function for resolving sparse matcher into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33318
diff changeset
   284
            pass
153456f02426 sparse: move function for resolving sparse matcher into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33318
diff changeset
   285
153456f02426 sparse: move function for resolving sparse matcher into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33318
diff changeset
   286
    if not matchers:
153456f02426 sparse: move function for resolving sparse matcher into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33318
diff changeset
   287
        result = matchmod.always(repo.root, '')
153456f02426 sparse: move function for resolving sparse matcher into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33318
diff changeset
   288
    elif len(matchers) == 1:
153456f02426 sparse: move function for resolving sparse matcher into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33318
diff changeset
   289
        result = matchers[0]
153456f02426 sparse: move function for resolving sparse matcher into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33318
diff changeset
   290
    else:
153456f02426 sparse: move function for resolving sparse matcher into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33318
diff changeset
   291
        result = matchmod.unionmatcher(matchers)
153456f02426 sparse: move function for resolving sparse matcher into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33318
diff changeset
   292
153456f02426 sparse: move function for resolving sparse matcher into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33318
diff changeset
   293
    if includetemp:
153456f02426 sparse: move function for resolving sparse matcher into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33318
diff changeset
   294
        tempincludes = readtemporaryincludes(repo)
153456f02426 sparse: move function for resolving sparse matcher into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33318
diff changeset
   295
        result = matchmod.forceincludematcher(result, tempincludes)
153456f02426 sparse: move function for resolving sparse matcher into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33318
diff changeset
   296
153456f02426 sparse: move function for resolving sparse matcher into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33318
diff changeset
   297
    repo._sparsematchercache[key] = result
153456f02426 sparse: move function for resolving sparse matcher into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33318
diff changeset
   298
153456f02426 sparse: move function for resolving sparse matcher into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33318
diff changeset
   299
    return result
33322
fa6c2c3064fd sparse: move update action filtering into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33321
diff changeset
   300
33323
252500520d60 sparse: refactor update actions filtering and call from core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33322
diff changeset
   301
def filterupdatesactions(repo, wctx, mctx, branchmerge, actions):
252500520d60 sparse: refactor update actions filtering and call from core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33322
diff changeset
   302
    """Filter updates to only lay out files that match the sparse rules."""
252500520d60 sparse: refactor update actions filtering and call from core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33322
diff changeset
   303
    if not enabled:
252500520d60 sparse: refactor update actions filtering and call from core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33322
diff changeset
   304
        return actions
33322
fa6c2c3064fd sparse: move update action filtering into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33321
diff changeset
   305
fa6c2c3064fd sparse: move update action filtering into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33321
diff changeset
   306
    oldrevs = [pctx.rev() for pctx in wctx.parents()]
fa6c2c3064fd sparse: move update action filtering into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33321
diff changeset
   307
    oldsparsematch = matcher(repo, oldrevs)
fa6c2c3064fd sparse: move update action filtering into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33321
diff changeset
   308
fa6c2c3064fd sparse: move update action filtering into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33321
diff changeset
   309
    if oldsparsematch.always():
33323
252500520d60 sparse: refactor update actions filtering and call from core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33322
diff changeset
   310
        return actions
33322
fa6c2c3064fd sparse: move update action filtering into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33321
diff changeset
   311
fa6c2c3064fd sparse: move update action filtering into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33321
diff changeset
   312
    files = set()
fa6c2c3064fd sparse: move update action filtering into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33321
diff changeset
   313
    prunedactions = {}
fa6c2c3064fd sparse: move update action filtering into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33321
diff changeset
   314
fa6c2c3064fd sparse: move update action filtering into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33321
diff changeset
   315
    if branchmerge:
fa6c2c3064fd sparse: move update action filtering into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33321
diff changeset
   316
        # If we're merging, use the wctx filter, since we're merging into
fa6c2c3064fd sparse: move update action filtering into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33321
diff changeset
   317
        # the wctx.
fa6c2c3064fd sparse: move update action filtering into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33321
diff changeset
   318
        sparsematch = matcher(repo, [wctx.parents()[0].rev()])
fa6c2c3064fd sparse: move update action filtering into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33321
diff changeset
   319
    else:
fa6c2c3064fd sparse: move update action filtering into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33321
diff changeset
   320
        # If we're updating, use the target context's filter, since we're
fa6c2c3064fd sparse: move update action filtering into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33321
diff changeset
   321
        # moving to the target context.
fa6c2c3064fd sparse: move update action filtering into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33321
diff changeset
   322
        sparsematch = matcher(repo, [mctx.rev()])
fa6c2c3064fd sparse: move update action filtering into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33321
diff changeset
   323
fa6c2c3064fd sparse: move update action filtering into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33321
diff changeset
   324
    temporaryfiles = []
fa6c2c3064fd sparse: move update action filtering into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33321
diff changeset
   325
    for file, action in actions.iteritems():
fa6c2c3064fd sparse: move update action filtering into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33321
diff changeset
   326
        type, args, msg = action
fa6c2c3064fd sparse: move update action filtering into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33321
diff changeset
   327
        files.add(file)
fa6c2c3064fd sparse: move update action filtering into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33321
diff changeset
   328
        if sparsematch(file):
fa6c2c3064fd sparse: move update action filtering into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33321
diff changeset
   329
            prunedactions[file] = action
fa6c2c3064fd sparse: move update action filtering into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33321
diff changeset
   330
        elif type == 'm':
fa6c2c3064fd sparse: move update action filtering into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33321
diff changeset
   331
            temporaryfiles.append(file)
fa6c2c3064fd sparse: move update action filtering into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33321
diff changeset
   332
            prunedactions[file] = action
fa6c2c3064fd sparse: move update action filtering into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33321
diff changeset
   333
        elif branchmerge:
fa6c2c3064fd sparse: move update action filtering into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33321
diff changeset
   334
            if type != 'k':
fa6c2c3064fd sparse: move update action filtering into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33321
diff changeset
   335
                temporaryfiles.append(file)
fa6c2c3064fd sparse: move update action filtering into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33321
diff changeset
   336
                prunedactions[file] = action
fa6c2c3064fd sparse: move update action filtering into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33321
diff changeset
   337
        elif type == 'f':
fa6c2c3064fd sparse: move update action filtering into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33321
diff changeset
   338
            prunedactions[file] = action
fa6c2c3064fd sparse: move update action filtering into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33321
diff changeset
   339
        elif file in wctx:
fa6c2c3064fd sparse: move update action filtering into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33321
diff changeset
   340
            prunedactions[file] = ('r', args, msg)
fa6c2c3064fd sparse: move update action filtering into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33321
diff changeset
   341
fa6c2c3064fd sparse: move update action filtering into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33321
diff changeset
   342
    if len(temporaryfiles) > 0:
fa6c2c3064fd sparse: move update action filtering into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33321
diff changeset
   343
        repo.ui.status(_('temporarily included %d file(s) in the sparse '
fa6c2c3064fd sparse: move update action filtering into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33321
diff changeset
   344
                         'checkout for merging\n') % len(temporaryfiles))
fa6c2c3064fd sparse: move update action filtering into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33321
diff changeset
   345
        addtemporaryincludes(repo, temporaryfiles)
fa6c2c3064fd sparse: move update action filtering into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33321
diff changeset
   346
fa6c2c3064fd sparse: move update action filtering into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33321
diff changeset
   347
        # Add the new files to the working copy so they can be merged, etc
fa6c2c3064fd sparse: move update action filtering into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33321
diff changeset
   348
        actions = []
fa6c2c3064fd sparse: move update action filtering into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33321
diff changeset
   349
        message = 'temporarily adding to sparse checkout'
fa6c2c3064fd sparse: move update action filtering into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33321
diff changeset
   350
        wctxmanifest = repo[None].manifest()
fa6c2c3064fd sparse: move update action filtering into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33321
diff changeset
   351
        for file in temporaryfiles:
fa6c2c3064fd sparse: move update action filtering into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33321
diff changeset
   352
            if file in wctxmanifest:
fa6c2c3064fd sparse: move update action filtering into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33321
diff changeset
   353
                fctx = repo[None][file]
fa6c2c3064fd sparse: move update action filtering into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33321
diff changeset
   354
                actions.append((file, (fctx.flags(), False), message))
fa6c2c3064fd sparse: move update action filtering into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33321
diff changeset
   355
fa6c2c3064fd sparse: move update action filtering into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33321
diff changeset
   356
        typeactions = collections.defaultdict(list)
fa6c2c3064fd sparse: move update action filtering into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33321
diff changeset
   357
        typeactions['g'] = actions
fa6c2c3064fd sparse: move update action filtering into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33321
diff changeset
   358
        mergemod.applyupdates(repo, typeactions, repo[None], repo['.'],
fa6c2c3064fd sparse: move update action filtering into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33321
diff changeset
   359
                              False)
fa6c2c3064fd sparse: move update action filtering into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33321
diff changeset
   360
fa6c2c3064fd sparse: move update action filtering into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33321
diff changeset
   361
        dirstate = repo.dirstate
fa6c2c3064fd sparse: move update action filtering into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33321
diff changeset
   362
        for file, flags, msg in actions:
fa6c2c3064fd sparse: move update action filtering into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33321
diff changeset
   363
            dirstate.normal(file)
fa6c2c3064fd sparse: move update action filtering into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33321
diff changeset
   364
fa6c2c3064fd sparse: move update action filtering into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33321
diff changeset
   365
    profiles = activeprofiles(repo)
fa6c2c3064fd sparse: move update action filtering into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33321
diff changeset
   366
    changedprofiles = profiles & files
fa6c2c3064fd sparse: move update action filtering into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33321
diff changeset
   367
    # If an active profile changed during the update, refresh the checkout.
fa6c2c3064fd sparse: move update action filtering into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33321
diff changeset
   368
    # Don't do this during a branch merge, since all incoming changes should
fa6c2c3064fd sparse: move update action filtering into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33321
diff changeset
   369
    # have been handled by the temporary includes above.
fa6c2c3064fd sparse: move update action filtering into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33321
diff changeset
   370
    if changedprofiles and not branchmerge:
fa6c2c3064fd sparse: move update action filtering into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33321
diff changeset
   371
        mf = mctx.manifest()
fa6c2c3064fd sparse: move update action filtering into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33321
diff changeset
   372
        for file in mf:
fa6c2c3064fd sparse: move update action filtering into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33321
diff changeset
   373
            old = oldsparsematch(file)
fa6c2c3064fd sparse: move update action filtering into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33321
diff changeset
   374
            new = sparsematch(file)
fa6c2c3064fd sparse: move update action filtering into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33321
diff changeset
   375
            if not old and new:
fa6c2c3064fd sparse: move update action filtering into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33321
diff changeset
   376
                flags = mf.flags(file)
fa6c2c3064fd sparse: move update action filtering into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33321
diff changeset
   377
                prunedactions[file] = ('g', (flags, False), '')
fa6c2c3064fd sparse: move update action filtering into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33321
diff changeset
   378
            elif old and not new:
fa6c2c3064fd sparse: move update action filtering into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33321
diff changeset
   379
                prunedactions[file] = ('r', [], '')
fa6c2c3064fd sparse: move update action filtering into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33321
diff changeset
   380
33323
252500520d60 sparse: refactor update actions filtering and call from core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33322
diff changeset
   381
    return prunedactions
33324
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   382
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   383
def refreshwdir(repo, origstatus, origsparsematch, force=False):
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   384
    """Refreshes working directory by taking sparse config into account.
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   385
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   386
    The old status and sparse matcher is compared against the current sparse
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   387
    matcher.
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   388
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   389
    Will abort if a file with pending changes is being excluded or included
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   390
    unless ``force`` is True.
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   391
    """
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   392
    modified, added, removed, deleted, unknown, ignored, clean = origstatus
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   393
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   394
    # Verify there are no pending changes
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   395
    pending = set()
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   396
    pending.update(modified)
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   397
    pending.update(added)
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   398
    pending.update(removed)
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   399
    sparsematch = matcher(repo)
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   400
    abort = False
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   401
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   402
    for f in pending:
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   403
        if not sparsematch(f):
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   404
            repo.ui.warn(_("pending changes to '%s'\n") % f)
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   405
            abort = not force
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   406
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   407
    if abort:
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   408
        raise error.Abort(_('could not update sparseness due to pending '
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   409
                            'changes'))
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   410
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   411
    # Calculate actions
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   412
    dirstate = repo.dirstate
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   413
    ctx = repo['.']
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   414
    added = []
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   415
    lookup = []
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   416
    dropped = []
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   417
    mf = ctx.manifest()
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   418
    files = set(mf)
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   419
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   420
    actions = {}
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   421
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   422
    for file in files:
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   423
        old = origsparsematch(file)
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   424
        new = sparsematch(file)
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   425
        # Add files that are newly included, or that don't exist in
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   426
        # the dirstate yet.
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   427
        if (new and not old) or (old and new and not file in dirstate):
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   428
            fl = mf.flags(file)
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   429
            if repo.wvfs.exists(file):
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   430
                actions[file] = ('e', (fl,), '')
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   431
                lookup.append(file)
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   432
            else:
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   433
                actions[file] = ('g', (fl, False), '')
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   434
                added.append(file)
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   435
        # Drop files that are newly excluded, or that still exist in
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   436
        # the dirstate.
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   437
        elif (old and not new) or (not old and not new and file in dirstate):
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   438
            dropped.append(file)
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   439
            if file not in pending:
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   440
                actions[file] = ('r', [], '')
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   441
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   442
    # Verify there are no pending changes in newly included files
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   443
    abort = False
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   444
    for file in lookup:
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   445
        repo.ui.warn(_("pending changes to '%s'\n") % file)
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   446
        abort = not force
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   447
    if abort:
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   448
        raise error.Abort(_('cannot change sparseness due to pending '
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   449
                            'changes (delete the files or use '
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   450
                            '--force to bring them back dirty)'))
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   451
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   452
    # Check for files that were only in the dirstate.
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   453
    for file, state in dirstate.iteritems():
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   454
        if not file in files:
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   455
            old = origsparsematch(file)
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   456
            new = sparsematch(file)
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   457
            if old and not new:
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   458
                dropped.append(file)
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   459
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   460
    # Apply changes to disk
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   461
    typeactions = dict((m, []) for m in 'a f g am cd dc r dm dg m e k'.split())
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   462
    for f, (m, args, msg) in actions.iteritems():
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   463
        if m not in typeactions:
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   464
            typeactions[m] = []
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   465
        typeactions[m].append((f, args, msg))
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   466
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   467
    mergemod.applyupdates(repo, typeactions, repo[None], repo['.'], False)
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   468
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   469
    # Fix dirstate
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   470
    for file in added:
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   471
        dirstate.normal(file)
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   472
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   473
    for file in dropped:
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   474
        dirstate.drop(file)
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   475
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   476
    for file in lookup:
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   477
        # File exists on disk, and we're bringing it back in an unknown state.
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   478
        dirstate.normallookup(file)
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   479
33d0859c37bd sparse: move working directory refreshing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33323
diff changeset
   480
    return added, dropped, lookup
33353
160efb559f67 sparse: move post commit actions into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33325
diff changeset
   481
160efb559f67 sparse: move post commit actions into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33325
diff changeset
   482
def aftercommit(repo, node):
160efb559f67 sparse: move post commit actions into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33325
diff changeset
   483
    """Perform actions after a working directory commit."""
160efb559f67 sparse: move post commit actions into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33325
diff changeset
   484
    # This function is called unconditionally, even if sparse isn't
160efb559f67 sparse: move post commit actions into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33325
diff changeset
   485
    # enabled.
160efb559f67 sparse: move post commit actions into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33325
diff changeset
   486
    ctx = repo[node]
160efb559f67 sparse: move post commit actions into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33325
diff changeset
   487
160efb559f67 sparse: move post commit actions into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33325
diff changeset
   488
    profiles = patternsforrev(repo, ctx.rev())[2]
160efb559f67 sparse: move post commit actions into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33325
diff changeset
   489
160efb559f67 sparse: move post commit actions into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33325
diff changeset
   490
    # profiles will only have data if sparse is enabled.
160efb559f67 sparse: move post commit actions into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33325
diff changeset
   491
    if set(profiles) & set(ctx.files()):
160efb559f67 sparse: move post commit actions into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33325
diff changeset
   492
        origstatus = repo.status()
160efb559f67 sparse: move post commit actions into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33325
diff changeset
   493
        origsparsematch = matcher(repo)
160efb559f67 sparse: move post commit actions into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33325
diff changeset
   494
        refreshwdir(repo, origstatus, origsparsematch, force=True)
160efb559f67 sparse: move post commit actions into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33325
diff changeset
   495
160efb559f67 sparse: move post commit actions into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33325
diff changeset
   496
    prunetemporaryincludes(repo)