mercurial/sparse.py
author Gregory Szorc <gregory.szorc@gmail.com>
Thu, 06 Jul 2017 16:11:56 -0700
changeset 33317 df1287268cc0
parent 33304 3e1accab7447
child 33318 526255fe7899
permissions -rw-r--r--
sparse: move config signature logic into core This is a pretty straightforward port. It will be cleaned up in a subsequent commit.
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
33317
df1287268cc0 sparse: move config signature logic into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33304
diff changeset
    10
import hashlib
df1287268cc0 sparse: move config signature logic into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33304
diff changeset
    11
33297
ba5d89774db6 sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    12
from .i18n import _
33301
ca4b78eb11e7 sparse: move active profiles function into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33300
diff changeset
    13
from .node import nullid
33297
ba5d89774db6 sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    14
from . import (
ba5d89774db6 sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    15
    error,
ba5d89774db6 sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    16
)
ba5d89774db6 sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    17
33299
41448fc51510 sparse: variable to track if sparse is enabled
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33298
diff changeset
    18
# 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
    19
# 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
    20
# 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
    21
enabled = False
41448fc51510 sparse: variable to track if sparse is enabled
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33298
diff changeset
    22
33297
ba5d89774db6 sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    23
def parseconfig(ui, raw):
ba5d89774db6 sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    24
    """Parse sparse config file content.
ba5d89774db6 sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    25
ba5d89774db6 sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    26
    Returns a tuple of includes, excludes, and profiles.
ba5d89774db6 sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    27
    """
ba5d89774db6 sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    28
    includes = set()
ba5d89774db6 sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    29
    excludes = set()
ba5d89774db6 sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    30
    current = includes
ba5d89774db6 sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    31
    profiles = []
ba5d89774db6 sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    32
    for line in raw.split('\n'):
ba5d89774db6 sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    33
        line = line.strip()
ba5d89774db6 sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    34
        if not line or line.startswith('#'):
ba5d89774db6 sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    35
            # empty or comment line, skip
ba5d89774db6 sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    36
            continue
ba5d89774db6 sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    37
        elif line.startswith('%include '):
ba5d89774db6 sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    38
            line = line[9:].strip()
ba5d89774db6 sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    39
            if line:
ba5d89774db6 sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    40
                profiles.append(line)
ba5d89774db6 sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    41
        elif line == '[include]':
ba5d89774db6 sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    42
            if current != includes:
ba5d89774db6 sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    43
                # 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
    44
                raise error.Abort(_('sparse config cannot have includes ' +
ba5d89774db6 sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    45
                                    'after excludes'))
ba5d89774db6 sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    46
            continue
ba5d89774db6 sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    47
        elif line == '[exclude]':
ba5d89774db6 sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    48
            current = excludes
ba5d89774db6 sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    49
        elif line:
ba5d89774db6 sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    50
            if line.strip().startswith('/'):
ba5d89774db6 sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    51
                ui.warn(_('warning: sparse profile cannot use' +
ba5d89774db6 sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    52
                          ' paths starting with /, ignoring %s\n') % line)
ba5d89774db6 sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    53
                continue
ba5d89774db6 sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    54
            current.add(line)
ba5d89774db6 sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    55
ba5d89774db6 sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    56
    return includes, excludes, profiles
33298
f41a99c45956 sparse: move profile reading into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33297
diff changeset
    57
f41a99c45956 sparse: move profile reading into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33297
diff changeset
    58
# Exists as separate function to facilitate monkeypatching.
f41a99c45956 sparse: move profile reading into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33297
diff changeset
    59
def readprofile(repo, profile, changeid):
f41a99c45956 sparse: move profile reading into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33297
diff changeset
    60
    """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
    61
    # 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
    62
    # resolve and can be slow.
f41a99c45956 sparse: move profile reading into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33297
diff changeset
    63
    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
    64
f7a106b3f089 sparse: move resolving of sparse patterns for rev into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33299
diff changeset
    65
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
    66
    """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
    67
f7a106b3f089 sparse: move resolving of sparse patterns for rev into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33299
diff changeset
    68
    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
    69
    patterns.
f7a106b3f089 sparse: move resolving of sparse patterns for rev into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33299
diff changeset
    70
    """
f7a106b3f089 sparse: move resolving of sparse patterns for rev into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33299
diff changeset
    71
    # 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
    72
    if not enabled:
f7a106b3f089 sparse: move resolving of sparse patterns for rev into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33299
diff changeset
    73
        return set(), set(), []
f7a106b3f089 sparse: move resolving of sparse patterns for rev into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33299
diff changeset
    74
f7a106b3f089 sparse: move resolving of sparse patterns for rev into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33299
diff changeset
    75
    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
    76
    if not raw:
f7a106b3f089 sparse: move resolving of sparse patterns for rev into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33299
diff changeset
    77
        return set(), set(), []
f7a106b3f089 sparse: move resolving of sparse patterns for rev into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33299
diff changeset
    78
f7a106b3f089 sparse: move resolving of sparse patterns for rev into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33299
diff changeset
    79
    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
    80
        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
    81
                            'directory'))
f7a106b3f089 sparse: move resolving of sparse patterns for rev into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33299
diff changeset
    82
f7a106b3f089 sparse: move resolving of sparse patterns for rev into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33299
diff changeset
    83
    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
    84
    ctx = repo[rev]
f7a106b3f089 sparse: move resolving of sparse patterns for rev into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33299
diff changeset
    85
f7a106b3f089 sparse: move resolving of sparse patterns for rev into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33299
diff changeset
    86
    if profiles:
f7a106b3f089 sparse: move resolving of sparse patterns for rev into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33299
diff changeset
    87
        visited = set()
f7a106b3f089 sparse: move resolving of sparse patterns for rev into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33299
diff changeset
    88
        while profiles:
f7a106b3f089 sparse: move resolving of sparse patterns for rev into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33299
diff changeset
    89
            profile = profiles.pop()
f7a106b3f089 sparse: move resolving of sparse patterns for rev into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33299
diff changeset
    90
            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
    91
                continue
f7a106b3f089 sparse: move resolving of sparse patterns for rev into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33299
diff changeset
    92
f7a106b3f089 sparse: move resolving of sparse patterns for rev into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33299
diff changeset
    93
            visited.add(profile)
f7a106b3f089 sparse: move resolving of sparse patterns for rev into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33299
diff changeset
    94
f7a106b3f089 sparse: move resolving of sparse patterns for rev into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33299
diff changeset
    95
            try:
f7a106b3f089 sparse: move resolving of sparse patterns for rev into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33299
diff changeset
    96
                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
    97
            except error.ManifestLookupError:
f7a106b3f089 sparse: move resolving of sparse patterns for rev into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33299
diff changeset
    98
                msg = (
f7a106b3f089 sparse: move resolving of sparse patterns for rev into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33299
diff changeset
    99
                    "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
   100
                    "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
   101
                # 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
   102
                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
   103
                        'sparse', 'missingwarning', True):
f7a106b3f089 sparse: move resolving of sparse patterns for rev into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33299
diff changeset
   104
                    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
   105
                else:
f7a106b3f089 sparse: move resolving of sparse patterns for rev into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33299
diff changeset
   106
                    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
   107
                continue
f7a106b3f089 sparse: move resolving of sparse patterns for rev into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33299
diff changeset
   108
f7a106b3f089 sparse: move resolving of sparse patterns for rev into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33299
diff changeset
   109
            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
   110
            includes.update(pincludes)
f7a106b3f089 sparse: move resolving of sparse patterns for rev into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33299
diff changeset
   111
            excludes.update(pexcludes)
f7a106b3f089 sparse: move resolving of sparse patterns for rev into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33299
diff changeset
   112
            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
   113
                profiles.append(subprofile)
f7a106b3f089 sparse: move resolving of sparse patterns for rev into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33299
diff changeset
   114
f7a106b3f089 sparse: move resolving of sparse patterns for rev into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33299
diff changeset
   115
        profiles = visited
f7a106b3f089 sparse: move resolving of sparse patterns for rev into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33299
diff changeset
   116
f7a106b3f089 sparse: move resolving of sparse patterns for rev into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33299
diff changeset
   117
    if includes:
f7a106b3f089 sparse: move resolving of sparse patterns for rev into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33299
diff changeset
   118
        includes.add('.hg*')
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
    return includes, excludes, profiles
33301
ca4b78eb11e7 sparse: move active profiles function into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33300
diff changeset
   121
ca4b78eb11e7 sparse: move active profiles function into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33300
diff changeset
   122
def activeprofiles(repo):
ca4b78eb11e7 sparse: move active profiles function into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33300
diff changeset
   123
    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
   124
            repo.dirstate.parents() if node != nullid]
ca4b78eb11e7 sparse: move active profiles function into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33300
diff changeset
   125
ca4b78eb11e7 sparse: move active profiles function into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33300
diff changeset
   126
    profiles = set()
ca4b78eb11e7 sparse: move active profiles function into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33300
diff changeset
   127
    for rev in revs:
ca4b78eb11e7 sparse: move active profiles function into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33300
diff changeset
   128
        profiles.update(patternsforrev(repo, rev)[2])
ca4b78eb11e7 sparse: move active profiles function into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33300
diff changeset
   129
ca4b78eb11e7 sparse: move active profiles function into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33300
diff changeset
   130
    return profiles
33302
36a415b5a4b2 localrepo: add sparse caches
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33301
diff changeset
   131
36a415b5a4b2 localrepo: add sparse caches
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33301
diff changeset
   132
def invalidatesignaturecache(repo):
36a415b5a4b2 localrepo: add sparse caches
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33301
diff changeset
   133
    repo._sparsesignaturecache.clear()
33303
8b571495d811 sparse: move config file writing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33302
diff changeset
   134
33317
df1287268cc0 sparse: move config signature logic into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33304
diff changeset
   135
def _checksum(repo, path):
df1287268cc0 sparse: move config signature logic into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33304
diff changeset
   136
    data = repo.vfs.read(path)
df1287268cc0 sparse: move config signature logic into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33304
diff changeset
   137
    return hashlib.sha1(data).hexdigest()
df1287268cc0 sparse: move config signature logic into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33304
diff changeset
   138
df1287268cc0 sparse: move config signature logic into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33304
diff changeset
   139
def configsignature(repo, includetemp=True):
df1287268cc0 sparse: move config signature logic into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33304
diff changeset
   140
    """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
   141
df1287268cc0 sparse: move config signature logic into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33304
diff changeset
   142
    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
   143
    """
df1287268cc0 sparse: move config signature logic into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33304
diff changeset
   144
    cache = repo._sparsesignaturecache
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
    signature = cache.get('signature')
df1287268cc0 sparse: move config signature logic into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33304
diff changeset
   147
df1287268cc0 sparse: move config signature logic into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33304
diff changeset
   148
    if includetemp:
df1287268cc0 sparse: move config signature logic into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33304
diff changeset
   149
        tempsignature = cache.get('tempsignature')
df1287268cc0 sparse: move config signature logic into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33304
diff changeset
   150
    else:
df1287268cc0 sparse: move config signature logic into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33304
diff changeset
   151
        tempsignature = 0
df1287268cc0 sparse: move config signature logic into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33304
diff changeset
   152
df1287268cc0 sparse: move config signature logic into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33304
diff changeset
   153
    if signature is None or (includetemp and tempsignature is None):
df1287268cc0 sparse: move config signature logic into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33304
diff changeset
   154
        signature = 0
df1287268cc0 sparse: move config signature logic into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33304
diff changeset
   155
        try:
df1287268cc0 sparse: move config signature logic into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33304
diff changeset
   156
            signature = _checksum(repo, 'sparse')
df1287268cc0 sparse: move config signature logic into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33304
diff changeset
   157
        except (OSError, IOError):
df1287268cc0 sparse: move config signature logic into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33304
diff changeset
   158
            pass
df1287268cc0 sparse: move config signature logic into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33304
diff changeset
   159
        cache['signature'] = signature
df1287268cc0 sparse: move config signature logic into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33304
diff changeset
   160
df1287268cc0 sparse: move config signature logic into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33304
diff changeset
   161
        tempsignature = 0
df1287268cc0 sparse: move config signature logic into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33304
diff changeset
   162
        if includetemp:
df1287268cc0 sparse: move config signature logic into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33304
diff changeset
   163
            try:
df1287268cc0 sparse: move config signature logic into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33304
diff changeset
   164
                tempsignature = _checksum(repo, 'tempsparse')
df1287268cc0 sparse: move config signature logic into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33304
diff changeset
   165
            except (OSError, IOError):
df1287268cc0 sparse: move config signature logic into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33304
diff changeset
   166
                pass
df1287268cc0 sparse: move config signature logic into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33304
diff changeset
   167
            cache['tempsignature'] = tempsignature
df1287268cc0 sparse: move config signature logic into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33304
diff changeset
   168
df1287268cc0 sparse: move config signature logic into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33304
diff changeset
   169
    return '%s %s' % (str(signature), str(tempsignature))
df1287268cc0 sparse: move config signature logic into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33304
diff changeset
   170
33303
8b571495d811 sparse: move config file writing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33302
diff changeset
   171
def writeconfig(repo, includes, excludes, profiles):
8b571495d811 sparse: move config file writing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33302
diff changeset
   172
    """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
   173
    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
   174
        for p in sorted(profiles):
8b571495d811 sparse: move config file writing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33302
diff changeset
   175
            fh.write('%%include %s\n' % p)
8b571495d811 sparse: move config file writing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33302
diff changeset
   176
8b571495d811 sparse: move config file writing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33302
diff changeset
   177
        if includes:
8b571495d811 sparse: move config file writing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33302
diff changeset
   178
            fh.write('[include]\n')
8b571495d811 sparse: move config file writing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33302
diff changeset
   179
            for i in sorted(includes):
8b571495d811 sparse: move config file writing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33302
diff changeset
   180
                fh.write(i)
8b571495d811 sparse: move config file writing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33302
diff changeset
   181
                fh.write('\n')
8b571495d811 sparse: move config file writing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33302
diff changeset
   182
8b571495d811 sparse: move config file writing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33302
diff changeset
   183
        if excludes:
8b571495d811 sparse: move config file writing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33302
diff changeset
   184
            fh.write('[exclude]\n')
8b571495d811 sparse: move config file writing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33302
diff changeset
   185
            for e in sorted(excludes):
8b571495d811 sparse: move config file writing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33302
diff changeset
   186
                fh.write(e)
8b571495d811 sparse: move config file writing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33302
diff changeset
   187
                fh.write('\n')
8b571495d811 sparse: move config file writing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33302
diff changeset
   188
8b571495d811 sparse: move config file writing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33302
diff changeset
   189
    invalidatesignaturecache(repo)
33304
3e1accab7447 sparse: move some temporary includes functions into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33303
diff changeset
   190
3e1accab7447 sparse: move some temporary includes functions into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33303
diff changeset
   191
def readtemporaryincludes(repo):
3e1accab7447 sparse: move some temporary includes functions into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33303
diff changeset
   192
    raw = repo.vfs.tryread('tempsparse')
3e1accab7447 sparse: move some temporary includes functions into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33303
diff changeset
   193
    if not raw:
3e1accab7447 sparse: move some temporary includes functions into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33303
diff changeset
   194
        return set()
3e1accab7447 sparse: move some temporary includes functions into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33303
diff changeset
   195
3e1accab7447 sparse: move some temporary includes functions into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33303
diff changeset
   196
    return set(raw.split('\n'))
3e1accab7447 sparse: move some temporary includes functions into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33303
diff changeset
   197
3e1accab7447 sparse: move some temporary includes functions into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33303
diff changeset
   198
def writetemporaryincludes(repo, includes):
3e1accab7447 sparse: move some temporary includes functions into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33303
diff changeset
   199
    repo.vfs.write('tempsparse', '\n'.join(sorted(includes)))
3e1accab7447 sparse: move some temporary includes functions into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33303
diff changeset
   200
    invalidatesignaturecache(repo)
3e1accab7447 sparse: move some temporary includes functions into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33303
diff changeset
   201
3e1accab7447 sparse: move some temporary includes functions into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33303
diff changeset
   202
def addtemporaryincludes(repo, additional):
3e1accab7447 sparse: move some temporary includes functions into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33303
diff changeset
   203
    includes = readtemporaryincludes(repo)
3e1accab7447 sparse: move some temporary includes functions into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33303
diff changeset
   204
    for i in additional:
3e1accab7447 sparse: move some temporary includes functions into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33303
diff changeset
   205
        includes.add(i)
3e1accab7447 sparse: move some temporary includes functions into core
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33303
diff changeset
   206
    writetemporaryincludes(repo, includes)