mercurial/utils/storageutil.py
author Pierre-Yves David <pierre-yves.david@octobus.net>
Thu, 18 Mar 2021 10:12:55 +0100
changeset 46823 f1f2961d2816
parent 46715 45f0d5297698
child 47012 d55b71393907
permissions -rw-r--r--
path: move handling of "default" (*) suboptions value inside __init__ With the introduction of `path://` scheme the handling of default value will need to be subtler. We do simple code movement first to clarify the future changes. Differential Revision: https://phab.mercurial-scm.org/D10260
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
39877
f8eb71f9e3bd storageutil: new module for storage primitives (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     1
# storageutil.py - Storage functionality agnostic of backend implementation.
f8eb71f9e3bd storageutil: new module for storage primitives (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     2
#
f8eb71f9e3bd storageutil: new module for storage primitives (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     3
# Copyright 2018 Gregory Szorc <gregory.szorc@gmail.com>
f8eb71f9e3bd storageutil: new module for storage primitives (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     4
#
f8eb71f9e3bd storageutil: new module for storage primitives (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     5
# This software may be used and distributed according to the terms of the
f8eb71f9e3bd storageutil: new module for storage primitives (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     6
# GNU General Public License version 2 or any later version.
f8eb71f9e3bd storageutil: new module for storage primitives (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     7
f8eb71f9e3bd storageutil: new module for storage primitives (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     8
from __future__ import absolute_import
f8eb71f9e3bd storageutil: new module for storage primitives (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     9
39878
3e896b51aa5d storageutil: move metadata parsing and packing from revlog (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39877
diff changeset
    10
import re
40325
b0fbd1792e2d storageutil: extract most of peek_censored from revlog
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40321
diff changeset
    11
import struct
39877
f8eb71f9e3bd storageutil: new module for storage primitives (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    12
40002
0e8836be9541 storageutil: implement file identifier resolution method (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39881
diff changeset
    13
from ..i18n import _
39877
f8eb71f9e3bd storageutil: new module for storage primitives (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    14
from ..node import (
40002
0e8836be9541 storageutil: implement file identifier resolution method (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39881
diff changeset
    15
    bin,
39877
f8eb71f9e3bd storageutil: new module for storage primitives (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    16
    nullid,
40004
fa3dc85a747e storageutil: extract functionality for resolving strip revisions
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40003
diff changeset
    17
    nullrev,
39877
f8eb71f9e3bd storageutil: new module for storage primitives (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    18
)
39881
d63153611ed5 storageutil: extract revision number iteration
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39880
diff changeset
    19
from .. import (
40010
f5d819d84461 storageutil: pass nodes into emitrevisions()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40009
diff changeset
    20
    dagop,
40002
0e8836be9541 storageutil: implement file identifier resolution method (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39881
diff changeset
    21
    error,
40009
631c6f5058b9 storageutil: make all callables optional
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40008
diff changeset
    22
    mdiff,
39881
d63153611ed5 storageutil: extract revision number iteration
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39880
diff changeset
    23
    pycompat,
d63153611ed5 storageutil: extract revision number iteration
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39880
diff changeset
    24
)
42813
268662aac075 interfaces: create a new folder for interfaces and move repository.py in it
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 42781
diff changeset
    25
from ..interfaces import repository
46715
45f0d5297698 changegroupv4: add sidedata helpers
Raphaël Gomès <rgomes@octobus.net>
parents: 46712
diff changeset
    26
from ..revlogutils import sidedata as sidedatamod
44060
a61287a95dc3 core: migrate uses of hashlib.sha1 to hashutil.sha1
Augie Fackler <augie@google.com>
parents: 43077
diff changeset
    27
from ..utils import hashutil
39877
f8eb71f9e3bd storageutil: new module for storage primitives (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    28
44060
a61287a95dc3 core: migrate uses of hashlib.sha1 to hashutil.sha1
Augie Fackler <augie@google.com>
parents: 43077
diff changeset
    29
_nullhash = hashutil.sha1(nullid)
39877
f8eb71f9e3bd storageutil: new module for storage primitives (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    30
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42813
diff changeset
    31
39877
f8eb71f9e3bd storageutil: new module for storage primitives (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    32
def hashrevisionsha1(text, p1, p2):
f8eb71f9e3bd storageutil: new module for storage primitives (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    33
    """Compute the SHA-1 for revision data and its parents.
f8eb71f9e3bd storageutil: new module for storage primitives (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    34
f8eb71f9e3bd storageutil: new module for storage primitives (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    35
    This hash combines both the current file contents and its history
f8eb71f9e3bd storageutil: new module for storage primitives (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    36
    in a manner that makes it easy to distinguish nodes with the same
f8eb71f9e3bd storageutil: new module for storage primitives (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    37
    content in the revision graph.
f8eb71f9e3bd storageutil: new module for storage primitives (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    38
    """
f8eb71f9e3bd storageutil: new module for storage primitives (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    39
    # As of now, if one of the parent node is null, p2 is null
f8eb71f9e3bd storageutil: new module for storage primitives (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    40
    if p2 == nullid:
f8eb71f9e3bd storageutil: new module for storage primitives (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    41
        # deep copy of a hash is faster than creating one
f8eb71f9e3bd storageutil: new module for storage primitives (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    42
        s = _nullhash.copy()
f8eb71f9e3bd storageutil: new module for storage primitives (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    43
        s.update(p1)
f8eb71f9e3bd storageutil: new module for storage primitives (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    44
    else:
f8eb71f9e3bd storageutil: new module for storage primitives (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    45
        # none of the parent nodes are nullid
f8eb71f9e3bd storageutil: new module for storage primitives (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    46
        if p1 < p2:
f8eb71f9e3bd storageutil: new module for storage primitives (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    47
            a = p1
f8eb71f9e3bd storageutil: new module for storage primitives (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    48
            b = p2
f8eb71f9e3bd storageutil: new module for storage primitives (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    49
        else:
f8eb71f9e3bd storageutil: new module for storage primitives (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    50
            a = p2
f8eb71f9e3bd storageutil: new module for storage primitives (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    51
            b = p1
44060
a61287a95dc3 core: migrate uses of hashlib.sha1 to hashutil.sha1
Augie Fackler <augie@google.com>
parents: 43077
diff changeset
    52
        s = hashutil.sha1(a)
39877
f8eb71f9e3bd storageutil: new module for storage primitives (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    53
        s.update(b)
f8eb71f9e3bd storageutil: new module for storage primitives (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    54
    s.update(text)
f8eb71f9e3bd storageutil: new module for storage primitives (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    55
    return s.digest()
39878
3e896b51aa5d storageutil: move metadata parsing and packing from revlog (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39877
diff changeset
    56
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42813
diff changeset
    57
39878
3e896b51aa5d storageutil: move metadata parsing and packing from revlog (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39877
diff changeset
    58
METADATA_RE = re.compile(b'\x01\n')
3e896b51aa5d storageutil: move metadata parsing and packing from revlog (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39877
diff changeset
    59
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42813
diff changeset
    60
39878
3e896b51aa5d storageutil: move metadata parsing and packing from revlog (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39877
diff changeset
    61
def parsemeta(text):
3e896b51aa5d storageutil: move metadata parsing and packing from revlog (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39877
diff changeset
    62
    """Parse metadata header from revision data.
3e896b51aa5d storageutil: move metadata parsing and packing from revlog (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39877
diff changeset
    63
3e896b51aa5d storageutil: move metadata parsing and packing from revlog (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39877
diff changeset
    64
    Returns a 2-tuple of (metadata, offset), where both can be None if there
3e896b51aa5d storageutil: move metadata parsing and packing from revlog (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39877
diff changeset
    65
    is no metadata.
3e896b51aa5d storageutil: move metadata parsing and packing from revlog (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39877
diff changeset
    66
    """
3e896b51aa5d storageutil: move metadata parsing and packing from revlog (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39877
diff changeset
    67
    # text can be buffer, so we can't use .startswith or .index
3e896b51aa5d storageutil: move metadata parsing and packing from revlog (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39877
diff changeset
    68
    if text[:2] != b'\x01\n':
3e896b51aa5d storageutil: move metadata parsing and packing from revlog (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39877
diff changeset
    69
        return None, None
3e896b51aa5d storageutil: move metadata parsing and packing from revlog (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39877
diff changeset
    70
    s = METADATA_RE.search(text, 2).start()
3e896b51aa5d storageutil: move metadata parsing and packing from revlog (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39877
diff changeset
    71
    mtext = text[2:s]
3e896b51aa5d storageutil: move metadata parsing and packing from revlog (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39877
diff changeset
    72
    meta = {}
3e896b51aa5d storageutil: move metadata parsing and packing from revlog (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39877
diff changeset
    73
    for l in mtext.splitlines():
3e896b51aa5d storageutil: move metadata parsing and packing from revlog (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39877
diff changeset
    74
        k, v = l.split(b': ', 1)
3e896b51aa5d storageutil: move metadata parsing and packing from revlog (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39877
diff changeset
    75
        meta[k] = v
3e896b51aa5d storageutil: move metadata parsing and packing from revlog (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39877
diff changeset
    76
    return meta, s + 2
3e896b51aa5d storageutil: move metadata parsing and packing from revlog (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39877
diff changeset
    77
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42813
diff changeset
    78
39878
3e896b51aa5d storageutil: move metadata parsing and packing from revlog (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39877
diff changeset
    79
def packmeta(meta, text):
3e896b51aa5d storageutil: move metadata parsing and packing from revlog (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39877
diff changeset
    80
    """Add metadata to fulltext to produce revision text."""
3e896b51aa5d storageutil: move metadata parsing and packing from revlog (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39877
diff changeset
    81
    keys = sorted(meta)
3e896b51aa5d storageutil: move metadata parsing and packing from revlog (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39877
diff changeset
    82
    metatext = b''.join(b'%s: %s\n' % (k, meta[k]) for k in keys)
3e896b51aa5d storageutil: move metadata parsing and packing from revlog (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39877
diff changeset
    83
    return b'\x01\n%s\x01\n%s' % (metatext, text)
39879
d269ddbf54f0 storageutil: move _censoredtext() from revlog
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39878
diff changeset
    84
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42813
diff changeset
    85
39879
d269ddbf54f0 storageutil: move _censoredtext() from revlog
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39878
diff changeset
    86
def iscensoredtext(text):
d269ddbf54f0 storageutil: move _censoredtext() from revlog
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39878
diff changeset
    87
    meta = parsemeta(text)[0]
d269ddbf54f0 storageutil: move _censoredtext() from revlog
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39878
diff changeset
    88
    return meta and b'censored' in meta
39880
1b65fb4d43d6 storageutil: new function for extracting metadata-less content from text
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39879
diff changeset
    89
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42813
diff changeset
    90
39880
1b65fb4d43d6 storageutil: new function for extracting metadata-less content from text
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39879
diff changeset
    91
def filtermetadata(text):
1b65fb4d43d6 storageutil: new function for extracting metadata-less content from text
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39879
diff changeset
    92
    """Extract just the revision data from source text.
1b65fb4d43d6 storageutil: new function for extracting metadata-less content from text
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39879
diff changeset
    93
1b65fb4d43d6 storageutil: new function for extracting metadata-less content from text
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39879
diff changeset
    94
    Returns ``text`` unless it has a metadata header, in which case we return
1b65fb4d43d6 storageutil: new function for extracting metadata-less content from text
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39879
diff changeset
    95
    a new buffer without hte metadata.
1b65fb4d43d6 storageutil: new function for extracting metadata-less content from text
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39879
diff changeset
    96
    """
1b65fb4d43d6 storageutil: new function for extracting metadata-less content from text
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39879
diff changeset
    97
    if not text.startswith(b'\x01\n'):
1b65fb4d43d6 storageutil: new function for extracting metadata-less content from text
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39879
diff changeset
    98
        return text
1b65fb4d43d6 storageutil: new function for extracting metadata-less content from text
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39879
diff changeset
    99
1b65fb4d43d6 storageutil: new function for extracting metadata-less content from text
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39879
diff changeset
   100
    offset = text.index(b'\x01\n', 2)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42813
diff changeset
   101
    return text[offset + 2 :]
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42813
diff changeset
   102
39881
d63153611ed5 storageutil: extract revision number iteration
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39880
diff changeset
   103
40005
1d97a332c6d9 storageutil: extract copy metadata retrieval out of filelog
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40004
diff changeset
   104
def filerevisioncopied(store, node):
1d97a332c6d9 storageutil: extract copy metadata retrieval out of filelog
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40004
diff changeset
   105
    """Resolve file revision copy metadata.
1d97a332c6d9 storageutil: extract copy metadata retrieval out of filelog
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40004
diff changeset
   106
1d97a332c6d9 storageutil: extract copy metadata retrieval out of filelog
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40004
diff changeset
   107
    Returns ``False`` if the file has no copy metadata. Otherwise a
1d97a332c6d9 storageutil: extract copy metadata retrieval out of filelog
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40004
diff changeset
   108
    2-tuple of the source filename and node.
1d97a332c6d9 storageutil: extract copy metadata retrieval out of filelog
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40004
diff changeset
   109
    """
1d97a332c6d9 storageutil: extract copy metadata retrieval out of filelog
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40004
diff changeset
   110
    if store.parents(node)[0] != nullid:
1d97a332c6d9 storageutil: extract copy metadata retrieval out of filelog
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40004
diff changeset
   111
        return False
1d97a332c6d9 storageutil: extract copy metadata retrieval out of filelog
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40004
diff changeset
   112
1d97a332c6d9 storageutil: extract copy metadata retrieval out of filelog
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40004
diff changeset
   113
    meta = parsemeta(store.revision(node))[0]
1d97a332c6d9 storageutil: extract copy metadata retrieval out of filelog
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40004
diff changeset
   114
1d97a332c6d9 storageutil: extract copy metadata retrieval out of filelog
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40004
diff changeset
   115
    # copy and copyrev occur in pairs. In rare cases due to old bugs,
1d97a332c6d9 storageutil: extract copy metadata retrieval out of filelog
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40004
diff changeset
   116
    # one can occur without the other. So ensure both are present to flag
1d97a332c6d9 storageutil: extract copy metadata retrieval out of filelog
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40004
diff changeset
   117
    # as a copy.
1d97a332c6d9 storageutil: extract copy metadata retrieval out of filelog
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40004
diff changeset
   118
    if meta and b'copy' in meta and b'copyrev' in meta:
1d97a332c6d9 storageutil: extract copy metadata retrieval out of filelog
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40004
diff changeset
   119
        return meta[b'copy'], bin(meta[b'copyrev'])
1d97a332c6d9 storageutil: extract copy metadata retrieval out of filelog
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40004
diff changeset
   120
1d97a332c6d9 storageutil: extract copy metadata retrieval out of filelog
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40004
diff changeset
   121
    return False
1d97a332c6d9 storageutil: extract copy metadata retrieval out of filelog
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40004
diff changeset
   122
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42813
diff changeset
   123
40007
1470183068b8 storageutil: invert logic of file data comparison
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40006
diff changeset
   124
def filedataequivalent(store, node, filedata):
1470183068b8 storageutil: invert logic of file data comparison
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40006
diff changeset
   125
    """Determines whether file data is equivalent to a stored node.
1470183068b8 storageutil: invert logic of file data comparison
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40006
diff changeset
   126
1470183068b8 storageutil: invert logic of file data comparison
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40006
diff changeset
   127
    Returns True if the passed file data would hash to the same value
1470183068b8 storageutil: invert logic of file data comparison
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40006
diff changeset
   128
    as a stored revision and False otherwise.
1470183068b8 storageutil: invert logic of file data comparison
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40006
diff changeset
   129
1470183068b8 storageutil: invert logic of file data comparison
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40006
diff changeset
   130
    When a stored revision is censored, filedata must be empty to have
1470183068b8 storageutil: invert logic of file data comparison
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40006
diff changeset
   131
    equivalence.
1470183068b8 storageutil: invert logic of file data comparison
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40006
diff changeset
   132
1470183068b8 storageutil: invert logic of file data comparison
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40006
diff changeset
   133
    When a stored revision has copy metadata, it is ignored as part
1470183068b8 storageutil: invert logic of file data comparison
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40006
diff changeset
   134
    of the compare.
1470183068b8 storageutil: invert logic of file data comparison
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40006
diff changeset
   135
    """
40006
422beffd71ba storageutil: extract filelog.cmp() to a standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40005
diff changeset
   136
422beffd71ba storageutil: extract filelog.cmp() to a standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40005
diff changeset
   137
    if filedata.startswith(b'\x01\n'):
422beffd71ba storageutil: extract filelog.cmp() to a standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40005
diff changeset
   138
        revisiontext = b'\x01\n\x01\n' + filedata
422beffd71ba storageutil: extract filelog.cmp() to a standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40005
diff changeset
   139
    else:
422beffd71ba storageutil: extract filelog.cmp() to a standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40005
diff changeset
   140
        revisiontext = filedata
422beffd71ba storageutil: extract filelog.cmp() to a standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40005
diff changeset
   141
422beffd71ba storageutil: extract filelog.cmp() to a standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40005
diff changeset
   142
    p1, p2 = store.parents(node)
422beffd71ba storageutil: extract filelog.cmp() to a standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40005
diff changeset
   143
422beffd71ba storageutil: extract filelog.cmp() to a standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40005
diff changeset
   144
    computednode = hashrevisionsha1(revisiontext, p1, p2)
422beffd71ba storageutil: extract filelog.cmp() to a standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40005
diff changeset
   145
422beffd71ba storageutil: extract filelog.cmp() to a standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40005
diff changeset
   146
    if computednode == node:
40007
1470183068b8 storageutil: invert logic of file data comparison
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40006
diff changeset
   147
        return True
40006
422beffd71ba storageutil: extract filelog.cmp() to a standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40005
diff changeset
   148
422beffd71ba storageutil: extract filelog.cmp() to a standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40005
diff changeset
   149
    # Censored files compare against the empty file.
422beffd71ba storageutil: extract filelog.cmp() to a standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40005
diff changeset
   150
    if store.iscensored(store.rev(node)):
40007
1470183068b8 storageutil: invert logic of file data comparison
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40006
diff changeset
   151
        return filedata == b''
40006
422beffd71ba storageutil: extract filelog.cmp() to a standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40005
diff changeset
   152
422beffd71ba storageutil: extract filelog.cmp() to a standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40005
diff changeset
   153
    # Renaming a file produces a different hash, even if the data
422beffd71ba storageutil: extract filelog.cmp() to a standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40005
diff changeset
   154
    # remains unchanged. Check if that's the case.
422beffd71ba storageutil: extract filelog.cmp() to a standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40005
diff changeset
   155
    if store.renamed(node):
40007
1470183068b8 storageutil: invert logic of file data comparison
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40006
diff changeset
   156
        return store.read(node) == filedata
40006
422beffd71ba storageutil: extract filelog.cmp() to a standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40005
diff changeset
   157
40007
1470183068b8 storageutil: invert logic of file data comparison
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40006
diff changeset
   158
    return False
40006
422beffd71ba storageutil: extract filelog.cmp() to a standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40005
diff changeset
   159
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42813
diff changeset
   160
39881
d63153611ed5 storageutil: extract revision number iteration
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39880
diff changeset
   161
def iterrevs(storelen, start=0, stop=None):
d63153611ed5 storageutil: extract revision number iteration
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39880
diff changeset
   162
    """Iterate over revision numbers in a store."""
d63153611ed5 storageutil: extract revision number iteration
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39880
diff changeset
   163
    step = 1
d63153611ed5 storageutil: extract revision number iteration
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39880
diff changeset
   164
d63153611ed5 storageutil: extract revision number iteration
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39880
diff changeset
   165
    if stop is not None:
d63153611ed5 storageutil: extract revision number iteration
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39880
diff changeset
   166
        if start > stop:
d63153611ed5 storageutil: extract revision number iteration
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39880
diff changeset
   167
            step = -1
d63153611ed5 storageutil: extract revision number iteration
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39880
diff changeset
   168
        stop += step
d63153611ed5 storageutil: extract revision number iteration
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39880
diff changeset
   169
        if stop > storelen:
d63153611ed5 storageutil: extract revision number iteration
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39880
diff changeset
   170
            stop = storelen
d63153611ed5 storageutil: extract revision number iteration
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39880
diff changeset
   171
    else:
d63153611ed5 storageutil: extract revision number iteration
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39880
diff changeset
   172
        stop = storelen
d63153611ed5 storageutil: extract revision number iteration
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39880
diff changeset
   173
d63153611ed5 storageutil: extract revision number iteration
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39880
diff changeset
   174
    return pycompat.xrange(start, stop, step)
40002
0e8836be9541 storageutil: implement file identifier resolution method (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39881
diff changeset
   175
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42813
diff changeset
   176
40002
0e8836be9541 storageutil: implement file identifier resolution method (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39881
diff changeset
   177
def fileidlookup(store, fileid, identifier):
0e8836be9541 storageutil: implement file identifier resolution method (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39881
diff changeset
   178
    """Resolve the file node for a value.
0e8836be9541 storageutil: implement file identifier resolution method (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39881
diff changeset
   179
0e8836be9541 storageutil: implement file identifier resolution method (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39881
diff changeset
   180
    ``store`` is an object implementing the ``ifileindex`` interface.
0e8836be9541 storageutil: implement file identifier resolution method (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39881
diff changeset
   181
0e8836be9541 storageutil: implement file identifier resolution method (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39881
diff changeset
   182
    ``fileid`` can be:
0e8836be9541 storageutil: implement file identifier resolution method (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39881
diff changeset
   183
45404
c4a4a49589bf storageutil: allow modern hash sizes for fileids
Joerg Sonnenberger <joerg@bec.de>
parents: 44452
diff changeset
   184
    * A 20 or 32 byte binary node.
40002
0e8836be9541 storageutil: implement file identifier resolution method (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39881
diff changeset
   185
    * An integer revision number
45404
c4a4a49589bf storageutil: allow modern hash sizes for fileids
Joerg Sonnenberger <joerg@bec.de>
parents: 44452
diff changeset
   186
    * A 40 or 64 byte hex node.
40002
0e8836be9541 storageutil: implement file identifier resolution method (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39881
diff changeset
   187
    * A bytes that can be parsed as an integer representing a revision number.
0e8836be9541 storageutil: implement file identifier resolution method (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39881
diff changeset
   188
0e8836be9541 storageutil: implement file identifier resolution method (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39881
diff changeset
   189
    ``identifier`` is used to populate ``error.LookupError`` with an identifier
0e8836be9541 storageutil: implement file identifier resolution method (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39881
diff changeset
   190
    for the store.
0e8836be9541 storageutil: implement file identifier resolution method (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39881
diff changeset
   191
0e8836be9541 storageutil: implement file identifier resolution method (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39881
diff changeset
   192
    Raises ``error.LookupError`` on failure.
0e8836be9541 storageutil: implement file identifier resolution method (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39881
diff changeset
   193
    """
0e8836be9541 storageutil: implement file identifier resolution method (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39881
diff changeset
   194
    if isinstance(fileid, int):
40003
ad8389ecd3f5 storageutil: consistently raise LookupError (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40002
diff changeset
   195
        try:
ad8389ecd3f5 storageutil: consistently raise LookupError (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40002
diff changeset
   196
            return store.node(fileid)
ad8389ecd3f5 storageutil: consistently raise LookupError (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40002
diff changeset
   197
        except IndexError:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42813
diff changeset
   198
            raise error.LookupError(
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   199
                b'%d' % fileid, identifier, _(b'no match found')
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42813
diff changeset
   200
            )
40002
0e8836be9541 storageutil: implement file identifier resolution method (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39881
diff changeset
   201
45404
c4a4a49589bf storageutil: allow modern hash sizes for fileids
Joerg Sonnenberger <joerg@bec.de>
parents: 44452
diff changeset
   202
    if len(fileid) in (20, 32):
40002
0e8836be9541 storageutil: implement file identifier resolution method (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39881
diff changeset
   203
        try:
0e8836be9541 storageutil: implement file identifier resolution method (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39881
diff changeset
   204
            store.rev(fileid)
0e8836be9541 storageutil: implement file identifier resolution method (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39881
diff changeset
   205
            return fileid
0e8836be9541 storageutil: implement file identifier resolution method (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39881
diff changeset
   206
        except error.LookupError:
0e8836be9541 storageutil: implement file identifier resolution method (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39881
diff changeset
   207
            pass
0e8836be9541 storageutil: implement file identifier resolution method (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39881
diff changeset
   208
45404
c4a4a49589bf storageutil: allow modern hash sizes for fileids
Joerg Sonnenberger <joerg@bec.de>
parents: 44452
diff changeset
   209
    if len(fileid) in (40, 64):
40002
0e8836be9541 storageutil: implement file identifier resolution method (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39881
diff changeset
   210
        try:
0e8836be9541 storageutil: implement file identifier resolution method (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39881
diff changeset
   211
            rawnode = bin(fileid)
0e8836be9541 storageutil: implement file identifier resolution method (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39881
diff changeset
   212
            store.rev(rawnode)
0e8836be9541 storageutil: implement file identifier resolution method (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39881
diff changeset
   213
            return rawnode
0e8836be9541 storageutil: implement file identifier resolution method (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39881
diff changeset
   214
        except TypeError:
0e8836be9541 storageutil: implement file identifier resolution method (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39881
diff changeset
   215
            pass
0e8836be9541 storageutil: implement file identifier resolution method (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39881
diff changeset
   216
0e8836be9541 storageutil: implement file identifier resolution method (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39881
diff changeset
   217
    try:
0e8836be9541 storageutil: implement file identifier resolution method (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39881
diff changeset
   218
        rev = int(fileid)
0e8836be9541 storageutil: implement file identifier resolution method (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39881
diff changeset
   219
0e8836be9541 storageutil: implement file identifier resolution method (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39881
diff changeset
   220
        if b'%d' % rev != fileid:
0e8836be9541 storageutil: implement file identifier resolution method (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39881
diff changeset
   221
            raise ValueError
0e8836be9541 storageutil: implement file identifier resolution method (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39881
diff changeset
   222
0e8836be9541 storageutil: implement file identifier resolution method (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39881
diff changeset
   223
        try:
0e8836be9541 storageutil: implement file identifier resolution method (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39881
diff changeset
   224
            return store.node(rev)
0e8836be9541 storageutil: implement file identifier resolution method (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39881
diff changeset
   225
        except (IndexError, TypeError):
0e8836be9541 storageutil: implement file identifier resolution method (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39881
diff changeset
   226
            pass
0e8836be9541 storageutil: implement file identifier resolution method (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39881
diff changeset
   227
    except (ValueError, OverflowError):
0e8836be9541 storageutil: implement file identifier resolution method (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39881
diff changeset
   228
        pass
0e8836be9541 storageutil: implement file identifier resolution method (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39881
diff changeset
   229
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   230
    raise error.LookupError(fileid, identifier, _(b'no match found'))
40004
fa3dc85a747e storageutil: extract functionality for resolving strip revisions
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40003
diff changeset
   231
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42813
diff changeset
   232
40004
fa3dc85a747e storageutil: extract functionality for resolving strip revisions
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40003
diff changeset
   233
def resolvestripinfo(minlinkrev, tiprev, headrevs, linkrevfn, parentrevsfn):
fa3dc85a747e storageutil: extract functionality for resolving strip revisions
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40003
diff changeset
   234
    """Resolve information needed to strip revisions.
fa3dc85a747e storageutil: extract functionality for resolving strip revisions
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40003
diff changeset
   235
fa3dc85a747e storageutil: extract functionality for resolving strip revisions
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40003
diff changeset
   236
    Finds the minimum revision number that must be stripped in order to
fa3dc85a747e storageutil: extract functionality for resolving strip revisions
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40003
diff changeset
   237
    strip ``minlinkrev``.
fa3dc85a747e storageutil: extract functionality for resolving strip revisions
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40003
diff changeset
   238
fa3dc85a747e storageutil: extract functionality for resolving strip revisions
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40003
diff changeset
   239
    Returns a 2-tuple of the minimum revision number to do that and a set
fa3dc85a747e storageutil: extract functionality for resolving strip revisions
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40003
diff changeset
   240
    of all revision numbers that have linkrevs that would be broken
fa3dc85a747e storageutil: extract functionality for resolving strip revisions
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40003
diff changeset
   241
    by that strip.
fa3dc85a747e storageutil: extract functionality for resolving strip revisions
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40003
diff changeset
   242
fa3dc85a747e storageutil: extract functionality for resolving strip revisions
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40003
diff changeset
   243
    ``tiprev`` is the current tip-most revision. It is ``len(store) - 1``.
fa3dc85a747e storageutil: extract functionality for resolving strip revisions
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40003
diff changeset
   244
    ``headrevs`` is an iterable of head revisions.
fa3dc85a747e storageutil: extract functionality for resolving strip revisions
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40003
diff changeset
   245
    ``linkrevfn`` is a callable that receives a revision and returns a linked
fa3dc85a747e storageutil: extract functionality for resolving strip revisions
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40003
diff changeset
   246
    revision.
fa3dc85a747e storageutil: extract functionality for resolving strip revisions
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40003
diff changeset
   247
    ``parentrevsfn`` is a callable that receives a revision number and returns
fa3dc85a747e storageutil: extract functionality for resolving strip revisions
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40003
diff changeset
   248
    an iterable of its parent revision numbers.
fa3dc85a747e storageutil: extract functionality for resolving strip revisions
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40003
diff changeset
   249
    """
fa3dc85a747e storageutil: extract functionality for resolving strip revisions
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40003
diff changeset
   250
    brokenrevs = set()
fa3dc85a747e storageutil: extract functionality for resolving strip revisions
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40003
diff changeset
   251
    strippoint = tiprev + 1
fa3dc85a747e storageutil: extract functionality for resolving strip revisions
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40003
diff changeset
   252
fa3dc85a747e storageutil: extract functionality for resolving strip revisions
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40003
diff changeset
   253
    heads = {}
fa3dc85a747e storageutil: extract functionality for resolving strip revisions
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40003
diff changeset
   254
    futurelargelinkrevs = set()
fa3dc85a747e storageutil: extract functionality for resolving strip revisions
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40003
diff changeset
   255
    for head in headrevs:
fa3dc85a747e storageutil: extract functionality for resolving strip revisions
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40003
diff changeset
   256
        headlinkrev = linkrevfn(head)
fa3dc85a747e storageutil: extract functionality for resolving strip revisions
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40003
diff changeset
   257
        heads[head] = headlinkrev
fa3dc85a747e storageutil: extract functionality for resolving strip revisions
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40003
diff changeset
   258
        if headlinkrev >= minlinkrev:
fa3dc85a747e storageutil: extract functionality for resolving strip revisions
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40003
diff changeset
   259
            futurelargelinkrevs.add(headlinkrev)
fa3dc85a747e storageutil: extract functionality for resolving strip revisions
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40003
diff changeset
   260
fa3dc85a747e storageutil: extract functionality for resolving strip revisions
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40003
diff changeset
   261
    # This algorithm involves walking down the rev graph, starting at the
fa3dc85a747e storageutil: extract functionality for resolving strip revisions
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40003
diff changeset
   262
    # heads. Since the revs are topologically sorted according to linkrev,
fa3dc85a747e storageutil: extract functionality for resolving strip revisions
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40003
diff changeset
   263
    # once all head linkrevs are below the minlink, we know there are
fa3dc85a747e storageutil: extract functionality for resolving strip revisions
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40003
diff changeset
   264
    # no more revs that could have a linkrev greater than minlink.
fa3dc85a747e storageutil: extract functionality for resolving strip revisions
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40003
diff changeset
   265
    # So we can stop walking.
fa3dc85a747e storageutil: extract functionality for resolving strip revisions
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40003
diff changeset
   266
    while futurelargelinkrevs:
fa3dc85a747e storageutil: extract functionality for resolving strip revisions
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40003
diff changeset
   267
        strippoint -= 1
fa3dc85a747e storageutil: extract functionality for resolving strip revisions
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40003
diff changeset
   268
        linkrev = heads.pop(strippoint)
fa3dc85a747e storageutil: extract functionality for resolving strip revisions
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40003
diff changeset
   269
fa3dc85a747e storageutil: extract functionality for resolving strip revisions
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40003
diff changeset
   270
        if linkrev < minlinkrev:
fa3dc85a747e storageutil: extract functionality for resolving strip revisions
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40003
diff changeset
   271
            brokenrevs.add(strippoint)
fa3dc85a747e storageutil: extract functionality for resolving strip revisions
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40003
diff changeset
   272
        else:
fa3dc85a747e storageutil: extract functionality for resolving strip revisions
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40003
diff changeset
   273
            futurelargelinkrevs.remove(linkrev)
fa3dc85a747e storageutil: extract functionality for resolving strip revisions
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40003
diff changeset
   274
fa3dc85a747e storageutil: extract functionality for resolving strip revisions
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40003
diff changeset
   275
        for p in parentrevsfn(strippoint):
fa3dc85a747e storageutil: extract functionality for resolving strip revisions
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40003
diff changeset
   276
            if p != nullrev:
fa3dc85a747e storageutil: extract functionality for resolving strip revisions
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40003
diff changeset
   277
                plinkrev = linkrevfn(p)
fa3dc85a747e storageutil: extract functionality for resolving strip revisions
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40003
diff changeset
   278
                heads[p] = plinkrev
fa3dc85a747e storageutil: extract functionality for resolving strip revisions
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40003
diff changeset
   279
                if plinkrev >= minlinkrev:
fa3dc85a747e storageutil: extract functionality for resolving strip revisions
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40003
diff changeset
   280
                    futurelargelinkrevs.add(plinkrev)
fa3dc85a747e storageutil: extract functionality for resolving strip revisions
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40003
diff changeset
   281
fa3dc85a747e storageutil: extract functionality for resolving strip revisions
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40003
diff changeset
   282
    return strippoint, brokenrevs
40008
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   283
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42813
diff changeset
   284
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42813
diff changeset
   285
def emitrevisions(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42813
diff changeset
   286
    store,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42813
diff changeset
   287
    nodes,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42813
diff changeset
   288
    nodesorder,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42813
diff changeset
   289
    resultcls,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42813
diff changeset
   290
    deltaparentfn=None,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42813
diff changeset
   291
    candeltafn=None,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42813
diff changeset
   292
    rawsizefn=None,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42813
diff changeset
   293
    revdifffn=None,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42813
diff changeset
   294
    flagsfn=None,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42813
diff changeset
   295
    deltamode=repository.CG_DELTAMODE_STD,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42813
diff changeset
   296
    revisiondata=False,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42813
diff changeset
   297
    assumehaveparentrevisions=False,
46715
45f0d5297698 changegroupv4: add sidedata helpers
Raphaël Gomès <rgomes@octobus.net>
parents: 46712
diff changeset
   298
    sidedata_helpers=None,
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42813
diff changeset
   299
):
40008
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   300
    """Generic implementation of ifiledata.emitrevisions().
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   301
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   302
    Emitting revision data is subtly complex. This function attempts to
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   303
    encapsulate all the logic for doing so in a backend-agnostic way.
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   304
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   305
    ``store``
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   306
       Object conforming to ``ifilestorage`` interface.
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   307
40010
f5d819d84461 storageutil: pass nodes into emitrevisions()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40009
diff changeset
   308
    ``nodes``
f5d819d84461 storageutil: pass nodes into emitrevisions()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40009
diff changeset
   309
       List of revision nodes whose data to emit.
40008
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   310
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   311
    ``resultcls``
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   312
       A type implementing the ``irevisiondelta`` interface that will be
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   313
       constructed and returned.
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   314
40009
631c6f5058b9 storageutil: make all callables optional
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40008
diff changeset
   315
    ``deltaparentfn`` (optional)
40008
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   316
       Callable receiving a revision number and returning the revision number
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   317
       of a revision that the internal delta is stored against. This delta
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   318
       will be preferred over computing a new arbitrary delta.
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   319
40009
631c6f5058b9 storageutil: make all callables optional
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40008
diff changeset
   320
       If not defined, a delta will always be computed from raw revision
631c6f5058b9 storageutil: make all callables optional
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40008
diff changeset
   321
       data.
631c6f5058b9 storageutil: make all callables optional
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40008
diff changeset
   322
631c6f5058b9 storageutil: make all callables optional
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40008
diff changeset
   323
    ``candeltafn`` (optional)
40008
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   324
       Callable receiving a pair of revision numbers that returns a bool
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   325
       indicating whether a delta between them can be produced.
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   326
40009
631c6f5058b9 storageutil: make all callables optional
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40008
diff changeset
   327
       If not defined, it is assumed that any two revisions can delta with
631c6f5058b9 storageutil: make all callables optional
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40008
diff changeset
   328
       each other.
631c6f5058b9 storageutil: make all callables optional
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40008
diff changeset
   329
631c6f5058b9 storageutil: make all callables optional
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40008
diff changeset
   330
    ``rawsizefn`` (optional)
40008
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   331
       Callable receiving a revision number and returning the length of the
42781
aeb2be20b33b rawdata: update callers in storageutils
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 40460
diff changeset
   332
       ``store.rawdata(rev)``.
40008
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   333
42781
aeb2be20b33b rawdata: update callers in storageutils
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 40460
diff changeset
   334
       If not defined, ``len(store.rawdata(rev))`` will be called.
40009
631c6f5058b9 storageutil: make all callables optional
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40008
diff changeset
   335
631c6f5058b9 storageutil: make all callables optional
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40008
diff changeset
   336
    ``revdifffn`` (optional)
40008
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   337
       Callable receiving a pair of revision numbers that returns a delta
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   338
       between them.
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   339
40009
631c6f5058b9 storageutil: make all callables optional
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40008
diff changeset
   340
       If not defined, a delta will be computed by invoking mdiff code
631c6f5058b9 storageutil: make all callables optional
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40008
diff changeset
   341
       on ``store.revision()`` results.
631c6f5058b9 storageutil: make all callables optional
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40008
diff changeset
   342
631c6f5058b9 storageutil: make all callables optional
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40008
diff changeset
   343
       Defining this function allows a precomputed or stored delta to be
631c6f5058b9 storageutil: make all callables optional
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40008
diff changeset
   344
       used without having to compute on.
631c6f5058b9 storageutil: make all callables optional
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40008
diff changeset
   345
631c6f5058b9 storageutil: make all callables optional
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40008
diff changeset
   346
    ``flagsfn`` (optional)
40008
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   347
       Callable receiving a revision number and returns the integer flags
40009
631c6f5058b9 storageutil: make all callables optional
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40008
diff changeset
   348
       value for it. If not defined, flags value will be 0.
40008
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   349
40427
59a870a4ad6e changegroup: refactor emitrevision to use a `deltamode` argument
Boris Feld <boris.feld@octobus.net>
parents: 40325
diff changeset
   350
    ``deltamode``
59a870a4ad6e changegroup: refactor emitrevision to use a `deltamode` argument
Boris Feld <boris.feld@octobus.net>
parents: 40325
diff changeset
   351
       constaint on delta to be sent:
59a870a4ad6e changegroup: refactor emitrevision to use a `deltamode` argument
Boris Feld <boris.feld@octobus.net>
parents: 40325
diff changeset
   352
       * CG_DELTAMODE_STD  - normal mode, try to reuse storage deltas,
59a870a4ad6e changegroup: refactor emitrevision to use a `deltamode` argument
Boris Feld <boris.feld@octobus.net>
parents: 40325
diff changeset
   353
       * CG_DELTAMODE_PREV - only delta against "prev",
59a870a4ad6e changegroup: refactor emitrevision to use a `deltamode` argument
Boris Feld <boris.feld@octobus.net>
parents: 40325
diff changeset
   354
       * CG_DELTAMODE_FULL - only issue full snapshot.
59a870a4ad6e changegroup: refactor emitrevision to use a `deltamode` argument
Boris Feld <boris.feld@octobus.net>
parents: 40325
diff changeset
   355
40008
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   356
       Whether to send fulltext revisions instead of deltas, if allowed.
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   357
40010
f5d819d84461 storageutil: pass nodes into emitrevisions()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40009
diff changeset
   358
    ``nodesorder``
40008
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   359
    ``revisiondata``
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   360
    ``assumehaveparentrevisions``
46715
45f0d5297698 changegroupv4: add sidedata helpers
Raphaël Gomès <rgomes@octobus.net>
parents: 46712
diff changeset
   361
    ``sidedata_helpers`` (optional)
45f0d5297698 changegroupv4: add sidedata helpers
Raphaël Gomès <rgomes@octobus.net>
parents: 46712
diff changeset
   362
        If not None, means that sidedata should be included.
45f0d5297698 changegroupv4: add sidedata helpers
Raphaël Gomès <rgomes@octobus.net>
parents: 46712
diff changeset
   363
        A dictionary of revlog type to tuples of `(repo, computers, removers)`:
45f0d5297698 changegroupv4: add sidedata helpers
Raphaël Gomès <rgomes@octobus.net>
parents: 46712
diff changeset
   364
            * `repo` is used as an argument for computers
45f0d5297698 changegroupv4: add sidedata helpers
Raphaël Gomès <rgomes@octobus.net>
parents: 46712
diff changeset
   365
            * `computers` is a list of `(category, (keys, computer)` that
45f0d5297698 changegroupv4: add sidedata helpers
Raphaël Gomès <rgomes@octobus.net>
parents: 46712
diff changeset
   366
               compute the missing sidedata categories that were asked:
45f0d5297698 changegroupv4: add sidedata helpers
Raphaël Gomès <rgomes@octobus.net>
parents: 46712
diff changeset
   367
               * `category` is the sidedata category
45f0d5297698 changegroupv4: add sidedata helpers
Raphaël Gomès <rgomes@octobus.net>
parents: 46712
diff changeset
   368
               * `keys` are the sidedata keys to be affected
45f0d5297698 changegroupv4: add sidedata helpers
Raphaël Gomès <rgomes@octobus.net>
parents: 46712
diff changeset
   369
               * `computer` is the function `(repo, store, rev, sidedata)` that
45f0d5297698 changegroupv4: add sidedata helpers
Raphaël Gomès <rgomes@octobus.net>
parents: 46712
diff changeset
   370
                 returns a new sidedata dict.
45f0d5297698 changegroupv4: add sidedata helpers
Raphaël Gomès <rgomes@octobus.net>
parents: 46712
diff changeset
   371
            * `removers` will remove the keys corresponding to the categories
45f0d5297698 changegroupv4: add sidedata helpers
Raphaël Gomès <rgomes@octobus.net>
parents: 46712
diff changeset
   372
              that are present, but not needed.
45f0d5297698 changegroupv4: add sidedata helpers
Raphaël Gomès <rgomes@octobus.net>
parents: 46712
diff changeset
   373
        If both `computers` and `removers` are empty, sidedata are simply not
45f0d5297698 changegroupv4: add sidedata helpers
Raphaël Gomès <rgomes@octobus.net>
parents: 46712
diff changeset
   374
        transformed.
45f0d5297698 changegroupv4: add sidedata helpers
Raphaël Gomès <rgomes@octobus.net>
parents: 46712
diff changeset
   375
        Revlog types are `changelog`, `manifest` or `filelog`.
40008
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   376
    """
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   377
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   378
    fnode = store.node
40010
f5d819d84461 storageutil: pass nodes into emitrevisions()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40009
diff changeset
   379
    frev = store.rev
f5d819d84461 storageutil: pass nodes into emitrevisions()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40009
diff changeset
   380
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   381
    if nodesorder == b'nodes':
40010
f5d819d84461 storageutil: pass nodes into emitrevisions()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40009
diff changeset
   382
        revs = [frev(n) for n in nodes]
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   383
    elif nodesorder == b'linear':
44452
9d2b2df2c2ba cleanup: run pyupgrade on our source tree to clean up varying things
Augie Fackler <augie@google.com>
parents: 44060
diff changeset
   384
        revs = {frev(n) for n in nodes}
40010
f5d819d84461 storageutil: pass nodes into emitrevisions()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40009
diff changeset
   385
        revs = dagop.linearize(revs, store.parentrevs)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42813
diff changeset
   386
    else:  # storage and default
40445
634b45317459 changegroup: restore default node ordering (issue6001)
Boris Feld <boris.feld@octobus.net>
parents: 40325
diff changeset
   387
        revs = sorted(frev(n) for n in nodes)
40008
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   388
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   389
    prevrev = None
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   390
40427
59a870a4ad6e changegroup: refactor emitrevision to use a `deltamode` argument
Boris Feld <boris.feld@octobus.net>
parents: 40325
diff changeset
   391
    if deltamode == repository.CG_DELTAMODE_PREV or assumehaveparentrevisions:
40008
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   392
        prevrev = store.parentrevs(revs[0])[0]
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   393
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   394
    # Set of revs available to delta against.
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   395
    available = set()
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   396
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   397
    for rev in revs:
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   398
        if rev == nullrev:
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   399
            continue
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   400
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   401
        node = fnode(rev)
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   402
        p1rev, p2rev = store.parentrevs(rev)
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   403
40009
631c6f5058b9 storageutil: make all callables optional
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40008
diff changeset
   404
        if deltaparentfn:
631c6f5058b9 storageutil: make all callables optional
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40008
diff changeset
   405
            deltaparentrev = deltaparentfn(rev)
631c6f5058b9 storageutil: make all callables optional
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40008
diff changeset
   406
        else:
631c6f5058b9 storageutil: make all callables optional
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40008
diff changeset
   407
            deltaparentrev = nullrev
631c6f5058b9 storageutil: make all callables optional
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40008
diff changeset
   408
40008
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   409
        # Forced delta against previous mode.
40427
59a870a4ad6e changegroup: refactor emitrevision to use a `deltamode` argument
Boris Feld <boris.feld@octobus.net>
parents: 40325
diff changeset
   410
        if deltamode == repository.CG_DELTAMODE_PREV:
40008
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   411
            baserev = prevrev
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   412
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   413
        # We're instructed to send fulltext. Honor that.
40427
59a870a4ad6e changegroup: refactor emitrevision to use a `deltamode` argument
Boris Feld <boris.feld@octobus.net>
parents: 40325
diff changeset
   414
        elif deltamode == repository.CG_DELTAMODE_FULL:
40008
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   415
            baserev = nullrev
40432
968dd7e02ac5 changegroup: allow to force delta to be against p1
Boris Feld <boris.feld@octobus.net>
parents: 40427
diff changeset
   416
        # We're instructed to use p1. Honor that
968dd7e02ac5 changegroup: allow to force delta to be against p1
Boris Feld <boris.feld@octobus.net>
parents: 40427
diff changeset
   417
        elif deltamode == repository.CG_DELTAMODE_P1:
968dd7e02ac5 changegroup: allow to force delta to be against p1
Boris Feld <boris.feld@octobus.net>
parents: 40427
diff changeset
   418
            baserev = p1rev
40008
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   419
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   420
        # There is a delta in storage. We try to use that because it
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   421
        # amounts to effectively copying data from storage and is
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   422
        # therefore the fastest.
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   423
        elif deltaparentrev != nullrev:
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   424
            # Base revision was already emitted in this group. We can
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   425
            # always safely use the delta.
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   426
            if deltaparentrev in available:
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   427
                baserev = deltaparentrev
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   428
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   429
            # Base revision is a parent that hasn't been emitted already.
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   430
            # Use it if we can assume the receiver has the parent revision.
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42813
diff changeset
   431
            elif assumehaveparentrevisions and deltaparentrev in (p1rev, p2rev):
40008
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   432
                baserev = deltaparentrev
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   433
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   434
            # No guarantee the receiver has the delta parent. Send delta
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   435
            # against last revision (if possible), which in the common case
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   436
            # should be similar enough to this revision that the delta is
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   437
            # reasonable.
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   438
            elif prevrev is not None:
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   439
                baserev = prevrev
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   440
            else:
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   441
                baserev = nullrev
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   442
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   443
        # Storage has a fulltext revision.
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   444
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   445
        # Let's use the previous revision, which is as good a guess as any.
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   446
        # There is definitely room to improve this logic.
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   447
        elif prevrev is not None:
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   448
            baserev = prevrev
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   449
        else:
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   450
            baserev = nullrev
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   451
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   452
        # But we can't actually use our chosen delta base for whatever
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   453
        # reason. Reset to fulltext.
40009
631c6f5058b9 storageutil: make all callables optional
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40008
diff changeset
   454
        if baserev != nullrev and (candeltafn and not candeltafn(baserev, rev)):
40008
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   455
            baserev = nullrev
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   456
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   457
        revision = None
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   458
        delta = None
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   459
        baserevisionsize = None
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   460
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   461
        if revisiondata:
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   462
            if store.iscensored(baserev) or store.iscensored(rev):
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   463
                try:
42781
aeb2be20b33b rawdata: update callers in storageutils
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 40460
diff changeset
   464
                    revision = store.rawdata(node)
40008
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   465
                except error.CensoredNodeError as e:
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   466
                    revision = e.tombstone
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   467
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   468
                if baserev != nullrev:
40009
631c6f5058b9 storageutil: make all callables optional
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40008
diff changeset
   469
                    if rawsizefn:
631c6f5058b9 storageutil: make all callables optional
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40008
diff changeset
   470
                        baserevisionsize = rawsizefn(baserev)
631c6f5058b9 storageutil: make all callables optional
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40008
diff changeset
   471
                    else:
42781
aeb2be20b33b rawdata: update callers in storageutils
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 40460
diff changeset
   472
                        baserevisionsize = len(store.rawdata(baserev))
40008
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   473
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42813
diff changeset
   474
            elif (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42813
diff changeset
   475
                baserev == nullrev and deltamode != repository.CG_DELTAMODE_PREV
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42813
diff changeset
   476
            ):
42781
aeb2be20b33b rawdata: update callers in storageutils
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 40460
diff changeset
   477
                revision = store.rawdata(node)
40008
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   478
                available.add(rev)
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   479
            else:
40009
631c6f5058b9 storageutil: make all callables optional
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40008
diff changeset
   480
                if revdifffn:
631c6f5058b9 storageutil: make all callables optional
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40008
diff changeset
   481
                    delta = revdifffn(baserev, rev)
631c6f5058b9 storageutil: make all callables optional
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40008
diff changeset
   482
                else:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42813
diff changeset
   483
                    delta = mdiff.textdiff(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42813
diff changeset
   484
                        store.rawdata(baserev), store.rawdata(rev)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42813
diff changeset
   485
                    )
40009
631c6f5058b9 storageutil: make all callables optional
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40008
diff changeset
   486
40008
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   487
                available.add(rev)
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   488
46715
45f0d5297698 changegroupv4: add sidedata helpers
Raphaël Gomès <rgomes@octobus.net>
parents: 46712
diff changeset
   489
        sidedata = None
45f0d5297698 changegroupv4: add sidedata helpers
Raphaël Gomès <rgomes@octobus.net>
parents: 46712
diff changeset
   490
        if sidedata_helpers:
45f0d5297698 changegroupv4: add sidedata helpers
Raphaël Gomès <rgomes@octobus.net>
parents: 46712
diff changeset
   491
            sidedata = store.sidedata(rev)
45f0d5297698 changegroupv4: add sidedata helpers
Raphaël Gomès <rgomes@octobus.net>
parents: 46712
diff changeset
   492
            sidedata = run_sidedata_helpers(
45f0d5297698 changegroupv4: add sidedata helpers
Raphaël Gomès <rgomes@octobus.net>
parents: 46712
diff changeset
   493
                store=store,
45f0d5297698 changegroupv4: add sidedata helpers
Raphaël Gomès <rgomes@octobus.net>
parents: 46712
diff changeset
   494
                sidedata_helpers=sidedata_helpers,
45f0d5297698 changegroupv4: add sidedata helpers
Raphaël Gomès <rgomes@octobus.net>
parents: 46712
diff changeset
   495
                sidedata=sidedata,
45f0d5297698 changegroupv4: add sidedata helpers
Raphaël Gomès <rgomes@octobus.net>
parents: 46712
diff changeset
   496
                rev=rev,
45f0d5297698 changegroupv4: add sidedata helpers
Raphaël Gomès <rgomes@octobus.net>
parents: 46712
diff changeset
   497
            )
45f0d5297698 changegroupv4: add sidedata helpers
Raphaël Gomès <rgomes@octobus.net>
parents: 46712
diff changeset
   498
            sidedata = sidedatamod.serialize_sidedata(sidedata)
45f0d5297698 changegroupv4: add sidedata helpers
Raphaël Gomès <rgomes@octobus.net>
parents: 46712
diff changeset
   499
40008
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   500
        yield resultcls(
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   501
            node=node,
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   502
            p1node=fnode(p1rev),
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   503
            p2node=fnode(p2rev),
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   504
            basenode=fnode(baserev),
40009
631c6f5058b9 storageutil: make all callables optional
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40008
diff changeset
   505
            flags=flagsfn(rev) if flagsfn else 0,
40008
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   506
            baserevisionsize=baserevisionsize,
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   507
            revision=revision,
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42813
diff changeset
   508
            delta=delta,
46712
e8c11a2c96c0 delta: add sidedata field to revision delta
Raphaël Gomès <rgomes@octobus.net>
parents: 45404
diff changeset
   509
            sidedata=sidedata,
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42813
diff changeset
   510
        )
40008
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   511
842ffcf1d42f storageutil: extract most of emitrevisions() to standalone function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40007
diff changeset
   512
        prevrev = rev
40325
b0fbd1792e2d storageutil: extract most of peek_censored from revlog
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40321
diff changeset
   513
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42813
diff changeset
   514
46715
45f0d5297698 changegroupv4: add sidedata helpers
Raphaël Gomès <rgomes@octobus.net>
parents: 46712
diff changeset
   515
def run_sidedata_helpers(store, sidedata_helpers, sidedata, rev):
45f0d5297698 changegroupv4: add sidedata helpers
Raphaël Gomès <rgomes@octobus.net>
parents: 46712
diff changeset
   516
    """Returns the sidedata for the given revision after running through
45f0d5297698 changegroupv4: add sidedata helpers
Raphaël Gomès <rgomes@octobus.net>
parents: 46712
diff changeset
   517
    the given helpers.
45f0d5297698 changegroupv4: add sidedata helpers
Raphaël Gomès <rgomes@octobus.net>
parents: 46712
diff changeset
   518
    - `store`: the revlog this applies to (changelog, manifest, or filelog
45f0d5297698 changegroupv4: add sidedata helpers
Raphaël Gomès <rgomes@octobus.net>
parents: 46712
diff changeset
   519
      instance)
45f0d5297698 changegroupv4: add sidedata helpers
Raphaël Gomès <rgomes@octobus.net>
parents: 46712
diff changeset
   520
    - `sidedata_helpers`: see `storageutil.emitrevisions`
45f0d5297698 changegroupv4: add sidedata helpers
Raphaël Gomès <rgomes@octobus.net>
parents: 46712
diff changeset
   521
    - `sidedata`: previous sidedata at the given rev, if any
45f0d5297698 changegroupv4: add sidedata helpers
Raphaël Gomès <rgomes@octobus.net>
parents: 46712
diff changeset
   522
    - `rev`: affected rev of `store`
45f0d5297698 changegroupv4: add sidedata helpers
Raphaël Gomès <rgomes@octobus.net>
parents: 46712
diff changeset
   523
    """
45f0d5297698 changegroupv4: add sidedata helpers
Raphaël Gomès <rgomes@octobus.net>
parents: 46712
diff changeset
   524
    repo, sd_computers, sd_removers = sidedata_helpers
45f0d5297698 changegroupv4: add sidedata helpers
Raphaël Gomès <rgomes@octobus.net>
parents: 46712
diff changeset
   525
    kind = store.revlog_kind
45f0d5297698 changegroupv4: add sidedata helpers
Raphaël Gomès <rgomes@octobus.net>
parents: 46712
diff changeset
   526
    for _keys, sd_computer in sd_computers.get(kind, []):
45f0d5297698 changegroupv4: add sidedata helpers
Raphaël Gomès <rgomes@octobus.net>
parents: 46712
diff changeset
   527
        sidedata = sd_computer(repo, store, rev, sidedata)
45f0d5297698 changegroupv4: add sidedata helpers
Raphaël Gomès <rgomes@octobus.net>
parents: 46712
diff changeset
   528
    for keys, _computer in sd_removers.get(kind, []):
45f0d5297698 changegroupv4: add sidedata helpers
Raphaël Gomès <rgomes@octobus.net>
parents: 46712
diff changeset
   529
        for key in keys:
45f0d5297698 changegroupv4: add sidedata helpers
Raphaël Gomès <rgomes@octobus.net>
parents: 46712
diff changeset
   530
            sidedata.pop(key, None)
45f0d5297698 changegroupv4: add sidedata helpers
Raphaël Gomès <rgomes@octobus.net>
parents: 46712
diff changeset
   531
    return sidedata
45f0d5297698 changegroupv4: add sidedata helpers
Raphaël Gomès <rgomes@octobus.net>
parents: 46712
diff changeset
   532
45f0d5297698 changegroupv4: add sidedata helpers
Raphaël Gomès <rgomes@octobus.net>
parents: 46712
diff changeset
   533
40325
b0fbd1792e2d storageutil: extract most of peek_censored from revlog
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40321
diff changeset
   534
def deltaiscensored(delta, baserev, baselenfn):
b0fbd1792e2d storageutil: extract most of peek_censored from revlog
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40321
diff changeset
   535
    """Determine if a delta represents censored revision data.
b0fbd1792e2d storageutil: extract most of peek_censored from revlog
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40321
diff changeset
   536
b0fbd1792e2d storageutil: extract most of peek_censored from revlog
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40321
diff changeset
   537
    ``baserev`` is the base revision this delta is encoded against.
b0fbd1792e2d storageutil: extract most of peek_censored from revlog
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40321
diff changeset
   538
    ``baselenfn`` is a callable receiving a revision number that resolves the
b0fbd1792e2d storageutil: extract most of peek_censored from revlog
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40321
diff changeset
   539
    length of the revision fulltext.
b0fbd1792e2d storageutil: extract most of peek_censored from revlog
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40321
diff changeset
   540
b0fbd1792e2d storageutil: extract most of peek_censored from revlog
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40321
diff changeset
   541
    Returns a bool indicating if the result of the delta represents a censored
b0fbd1792e2d storageutil: extract most of peek_censored from revlog
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40321
diff changeset
   542
    revision.
b0fbd1792e2d storageutil: extract most of peek_censored from revlog
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40321
diff changeset
   543
    """
b0fbd1792e2d storageutil: extract most of peek_censored from revlog
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40321
diff changeset
   544
    # Fragile heuristic: unless new file meta keys are added alphabetically
b0fbd1792e2d storageutil: extract most of peek_censored from revlog
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40321
diff changeset
   545
    # preceding "censored", all censored revisions are prefixed by
b0fbd1792e2d storageutil: extract most of peek_censored from revlog
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40321
diff changeset
   546
    # "\1\ncensored:". A delta producing such a censored revision must be a
b0fbd1792e2d storageutil: extract most of peek_censored from revlog
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40321
diff changeset
   547
    # full-replacement delta, so we inspect the first and only patch in the
b0fbd1792e2d storageutil: extract most of peek_censored from revlog
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40321
diff changeset
   548
    # delta for this prefix.
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   549
    hlen = struct.calcsize(b">lll")
40325
b0fbd1792e2d storageutil: extract most of peek_censored from revlog
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40321
diff changeset
   550
    if len(delta) <= hlen:
b0fbd1792e2d storageutil: extract most of peek_censored from revlog
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40321
diff changeset
   551
        return False
b0fbd1792e2d storageutil: extract most of peek_censored from revlog
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40321
diff changeset
   552
b0fbd1792e2d storageutil: extract most of peek_censored from revlog
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40321
diff changeset
   553
    oldlen = baselenfn(baserev)
b0fbd1792e2d storageutil: extract most of peek_censored from revlog
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40321
diff changeset
   554
    newlen = len(delta) - hlen
b0fbd1792e2d storageutil: extract most of peek_censored from revlog
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40321
diff changeset
   555
    if delta[:hlen] != mdiff.replacediffheader(oldlen, newlen):
b0fbd1792e2d storageutil: extract most of peek_censored from revlog
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40321
diff changeset
   556
        return False
b0fbd1792e2d storageutil: extract most of peek_censored from revlog
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40321
diff changeset
   557
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   558
    add = b"\1\ncensored:"
40325
b0fbd1792e2d storageutil: extract most of peek_censored from revlog
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40321
diff changeset
   559
    addlen = len(add)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42813
diff changeset
   560
    return newlen >= addlen and delta[hlen : hlen + addlen] == add