hgext/lfs/pointer.py
author Martijn Pieters <mj@zopatista.com>
Wed, 11 Jul 2018 14:28:13 +0100
changeset 38661 8ac0c9cd4c48
parent 38475 67dc32d4e790
child 39777 b63dee7bd0d9
permissions -rw-r--r--
fncache: avoid loading the filename cache when not actually modifying it With time, fncache can become very large. The mozilla-central repo for example, has a 31M and growing fncache file. Loading this file takes time (280ms for the mozilla-central repository). In many scenarios, we don't need to load fncache at all. For example, when committing changes to existing files, or pushing such commits to another clone. This patch detects when a name is added via store.vfs(), and only loads the cache if a) the data metadata file doesn't already exist, or b) when opening for appending, the data or metadata file exists but has size (a transaction rollback leaves behind such files). Benchmarks (run on Macos 10.13 on a 2017-model Macbook Pro with Core i7 2.9GHz and flash drive), each test without and with patch run 5 times: * committing to an existing file, against the mozilla-central repository. Baseline real time average 2.3736, with patch 1.9884. * unbundling a large changeset consisting *only* of existing-file modifications (159 revisions, 1050 modifications, mozilla-central 4a250a0e4f29:beea9ac7d823), into a clone limited to the ancestor revision of that revset). Baseline real time average 1.5048, with patch 1.3108.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
35098
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
     1
# pointer.py - Git-LFS pointer serialization
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
     2
#
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
     3
# Copyright 2017 Facebook, Inc.
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
     4
#
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
     5
# This software may be used and distributed according to the terms of the
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
     6
# GNU General Public License version 2 or any later version.
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
     7
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
     8
from __future__ import absolute_import
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
     9
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    10
import re
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    11
35099
b8e5fb8d2389 lfs: quiesce check-module-import warnings
Matt Harbison <matt_harbison@yahoo.com>
parents: 35098
diff changeset
    12
from mercurial.i18n import _
b8e5fb8d2389 lfs: quiesce check-module-import warnings
Matt Harbison <matt_harbison@yahoo.com>
parents: 35098
diff changeset
    13
35098
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    14
from mercurial import (
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    15
    error,
36602
e30be4d2ac60 lfs: use byteskwargs() on some **kwargs for python 3 compat
Augie Fackler <augie@google.com>
parents: 36600
diff changeset
    16
    pycompat,
35098
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    17
)
37926
fb6226c15e54 lfs: stabilize error message values for Python 2 and 3
Augie Fackler <augie@google.com>
parents: 36602
diff changeset
    18
from mercurial.utils import (
fb6226c15e54 lfs: stabilize error message values for Python 2 and 3
Augie Fackler <augie@google.com>
parents: 36602
diff changeset
    19
    stringutil,
fb6226c15e54 lfs: stabilize error message values for Python 2 and 3
Augie Fackler <augie@google.com>
parents: 36602
diff changeset
    20
)
35098
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    21
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    22
class InvalidPointer(error.RevlogError):
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    23
    pass
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    24
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    25
class gitlfspointer(dict):
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    26
    VERSION = 'https://git-lfs.github.com/spec/v1'
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    27
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    28
    def __init__(self, *args, **kwargs):
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    29
        self['version'] = self.VERSION
36602
e30be4d2ac60 lfs: use byteskwargs() on some **kwargs for python 3 compat
Augie Fackler <augie@google.com>
parents: 36600
diff changeset
    30
        super(gitlfspointer, self).__init__(*args)
e30be4d2ac60 lfs: use byteskwargs() on some **kwargs for python 3 compat
Augie Fackler <augie@google.com>
parents: 36600
diff changeset
    31
        self.update(pycompat.byteskwargs(kwargs))
35098
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    32
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    33
    @classmethod
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    34
    def deserialize(cls, text):
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    35
        try:
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    36
            return cls(l.split(' ', 1) for l in text.splitlines()).validate()
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    37
        except ValueError: # l.split returns 1 item instead of 2
37942
32bc3815efae stringutil: flip the default of pprint() to bprefix=False
Yuya Nishihara <yuya@tcha.org>
parents: 37926
diff changeset
    38
            raise InvalidPointer(_('cannot parse git-lfs text: %s')
32bc3815efae stringutil: flip the default of pprint() to bprefix=False
Yuya Nishihara <yuya@tcha.org>
parents: 37926
diff changeset
    39
                                 % stringutil.pprint(text))
35098
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    40
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    41
    def serialize(self):
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    42
        sortkeyfunc = lambda x: (x[0] != 'version', x)
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    43
        items = sorted(self.validate().iteritems(), key=sortkeyfunc)
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    44
        return ''.join('%s %s\n' % (k, v) for k, v in items)
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    45
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    46
    def oid(self):
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    47
        return self['oid'].split(':')[-1]
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    48
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    49
    def size(self):
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    50
        return int(self['size'])
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    51
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    52
    # regular expressions used by _validate
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    53
    # see https://github.com/git-lfs/git-lfs/blob/master/docs/spec.md
36600
95bd9e396774 lfs: add missing b prefixes on some regular expressions
Augie Fackler <augie@google.com>
parents: 35099
diff changeset
    54
    _keyre = re.compile(br'\A[a-z0-9.-]+\Z')
95bd9e396774 lfs: add missing b prefixes on some regular expressions
Augie Fackler <augie@google.com>
parents: 35099
diff changeset
    55
    _valuere = re.compile(br'\A[^\n]*\Z')
35098
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    56
    _requiredre = {
36600
95bd9e396774 lfs: add missing b prefixes on some regular expressions
Augie Fackler <augie@google.com>
parents: 35099
diff changeset
    57
        'size': re.compile(br'\A[0-9]+\Z'),
95bd9e396774 lfs: add missing b prefixes on some regular expressions
Augie Fackler <augie@google.com>
parents: 35099
diff changeset
    58
        'oid': re.compile(br'\Asha256:[0-9a-f]{64}\Z'),
38475
67dc32d4e790 cleanup: migrate from re.escape to stringutil.reescape
Augie Fackler <augie@google.com>
parents: 38161
diff changeset
    59
        'version': re.compile(br'\A%s\Z' % stringutil.reescape(VERSION)),
35098
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    60
    }
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    61
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    62
    def validate(self):
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    63
        """raise InvalidPointer on error. return self if there is no error"""
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    64
        requiredcount = 0
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    65
        for k, v in self.iteritems():
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    66
            if k in self._requiredre:
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    67
                if not self._requiredre[k].match(v):
38161
aa10675c5dd6 lfs: clarify pointer validation error messages
Matt Harbison <matt_harbison@yahoo.com>
parents: 37942
diff changeset
    68
                    raise InvalidPointer(
aa10675c5dd6 lfs: clarify pointer validation error messages
Matt Harbison <matt_harbison@yahoo.com>
parents: 37942
diff changeset
    69
                        _('unexpected lfs pointer value: %s=%s')
aa10675c5dd6 lfs: clarify pointer validation error messages
Matt Harbison <matt_harbison@yahoo.com>
parents: 37942
diff changeset
    70
                        % (k, stringutil.pprint(v)))
35098
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    71
                requiredcount += 1
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    72
            elif not self._keyre.match(k):
38161
aa10675c5dd6 lfs: clarify pointer validation error messages
Matt Harbison <matt_harbison@yahoo.com>
parents: 37942
diff changeset
    73
                raise InvalidPointer(_('unexpected lfs pointer key: %s') % k)
35098
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    74
            if not self._valuere.match(v):
38161
aa10675c5dd6 lfs: clarify pointer validation error messages
Matt Harbison <matt_harbison@yahoo.com>
parents: 37942
diff changeset
    75
                raise InvalidPointer(_('unexpected lfs pointer value: %s=%s')
37942
32bc3815efae stringutil: flip the default of pprint() to bprefix=False
Yuya Nishihara <yuya@tcha.org>
parents: 37926
diff changeset
    76
                                     % (k, stringutil.pprint(v)))
35098
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    77
        if len(self._requiredre) != requiredcount:
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    78
            miss = sorted(set(self._requiredre.keys()).difference(self.keys()))
38161
aa10675c5dd6 lfs: clarify pointer validation error messages
Matt Harbison <matt_harbison@yahoo.com>
parents: 37942
diff changeset
    79
            raise InvalidPointer(_('missing lfs pointer keys: %s')
aa10675c5dd6 lfs: clarify pointer validation error messages
Matt Harbison <matt_harbison@yahoo.com>
parents: 37942
diff changeset
    80
                                 % ', '.join(miss))
35098
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    81
        return self
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    82
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    83
deserialize = gitlfspointer.deserialize