mercurial/utils/storageutil.py
author Gregory Szorc <gregory.szorc@gmail.com>
Fri, 28 Sep 2018 11:03:17 -0700
changeset 40002 0e8836be9541
parent 39881 d63153611ed5
child 40003 ad8389ecd3f5
permissions -rw-r--r--
storageutil: implement file identifier resolution method (BC) revlog.lookup() has a number of advanced features, including partial node matching. These advanced features aren't needed for file id lookup because file identifiers are almost always from internal sources. (An exception to this is hgweb, which appears to have some code paths that attempt to resolve a user-supplied string to a node.) This commit implements a new function for resolving file identifiers to nodes. It behaves like revlog.lookup() but without the advanced features. Tests reveal behavior changes: * Partial hex nodes are no longer resolved to nodes. * "-1" now returns nullid instead of raising LookupError. * "0" on an empty store now raises LookupError instead of returning nullid. I'm not sure why "-1" wasn't recognized before. But it seems reasonable to accept it as a value since integer -1 resolves to nullid. These changes all seem reasonable to me. And with the exception of partial hex node matching, we may want to consider changing revlog.lookup() as well. Differential Revision: https://phab.mercurial-scm.org/D4797
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
f8eb71f9e3bd storageutil: new module for storage primitives (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    10
import hashlib
39878
3e896b51aa5d storageutil: move metadata parsing and packing from revlog (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39877
diff changeset
    11
import re
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,
f8eb71f9e3bd storageutil: new module for storage primitives (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    17
)
39881
d63153611ed5 storageutil: extract revision number iteration
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39880
diff changeset
    18
from .. import (
40002
0e8836be9541 storageutil: implement file identifier resolution method (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39881
diff changeset
    19
    error,
39881
d63153611ed5 storageutil: extract revision number iteration
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39880
diff changeset
    20
    pycompat,
d63153611ed5 storageutil: extract revision number iteration
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39880
diff changeset
    21
)
39877
f8eb71f9e3bd storageutil: new module for storage primitives (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    22
f8eb71f9e3bd storageutil: new module for storage primitives (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    23
_nullhash = hashlib.sha1(nullid)
f8eb71f9e3bd storageutil: new module for storage primitives (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    24
f8eb71f9e3bd storageutil: new module for storage primitives (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    25
def hashrevisionsha1(text, p1, p2):
f8eb71f9e3bd storageutil: new module for storage primitives (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    26
    """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
    27
f8eb71f9e3bd storageutil: new module for storage primitives (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    28
    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
    29
    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
    30
    content in the revision graph.
f8eb71f9e3bd storageutil: new module for storage primitives (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    31
    """
f8eb71f9e3bd storageutil: new module for storage primitives (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    32
    # 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
    33
    if p2 == nullid:
f8eb71f9e3bd storageutil: new module for storage primitives (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    34
        # 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
    35
        s = _nullhash.copy()
f8eb71f9e3bd storageutil: new module for storage primitives (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    36
        s.update(p1)
f8eb71f9e3bd storageutil: new module for storage primitives (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    37
    else:
f8eb71f9e3bd storageutil: new module for storage primitives (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    38
        # none of the parent nodes are nullid
f8eb71f9e3bd storageutil: new module for storage primitives (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    39
        if p1 < p2:
f8eb71f9e3bd storageutil: new module for storage primitives (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    40
            a = p1
f8eb71f9e3bd storageutil: new module for storage primitives (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    41
            b = p2
f8eb71f9e3bd storageutil: new module for storage primitives (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    42
        else:
f8eb71f9e3bd storageutil: new module for storage primitives (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    43
            a = p2
f8eb71f9e3bd storageutil: new module for storage primitives (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    44
            b = p1
f8eb71f9e3bd storageutil: new module for storage primitives (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    45
        s = hashlib.sha1(a)
f8eb71f9e3bd storageutil: new module for storage primitives (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    46
        s.update(b)
f8eb71f9e3bd storageutil: new module for storage primitives (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    47
    s.update(text)
f8eb71f9e3bd storageutil: new module for storage primitives (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    48
    return s.digest()
39878
3e896b51aa5d storageutil: move metadata parsing and packing from revlog (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39877
diff changeset
    49
3e896b51aa5d storageutil: move metadata parsing and packing from revlog (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39877
diff changeset
    50
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
    51
3e896b51aa5d storageutil: move metadata parsing and packing from revlog (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39877
diff changeset
    52
def parsemeta(text):
3e896b51aa5d storageutil: move metadata parsing and packing from revlog (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39877
diff changeset
    53
    """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
    54
3e896b51aa5d storageutil: move metadata parsing and packing from revlog (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39877
diff changeset
    55
    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
    56
    is no metadata.
3e896b51aa5d storageutil: move metadata parsing and packing from revlog (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39877
diff changeset
    57
    """
3e896b51aa5d storageutil: move metadata parsing and packing from revlog (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39877
diff changeset
    58
    # 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
    59
    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
    60
        return None, None
3e896b51aa5d storageutil: move metadata parsing and packing from revlog (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39877
diff changeset
    61
    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
    62
    mtext = text[2:s]
3e896b51aa5d storageutil: move metadata parsing and packing from revlog (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39877
diff changeset
    63
    meta = {}
3e896b51aa5d storageutil: move metadata parsing and packing from revlog (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39877
diff changeset
    64
    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
    65
        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
    66
        meta[k] = v
3e896b51aa5d storageutil: move metadata parsing and packing from revlog (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39877
diff changeset
    67
    return meta, s + 2
3e896b51aa5d storageutil: move metadata parsing and packing from revlog (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39877
diff changeset
    68
3e896b51aa5d storageutil: move metadata parsing and packing from revlog (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39877
diff changeset
    69
def packmeta(meta, text):
3e896b51aa5d storageutil: move metadata parsing and packing from revlog (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39877
diff changeset
    70
    """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
    71
    keys = sorted(meta)
3e896b51aa5d storageutil: move metadata parsing and packing from revlog (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39877
diff changeset
    72
    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
    73
    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
    74
d269ddbf54f0 storageutil: move _censoredtext() from revlog
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39878
diff changeset
    75
def iscensoredtext(text):
d269ddbf54f0 storageutil: move _censoredtext() from revlog
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39878
diff changeset
    76
    meta = parsemeta(text)[0]
d269ddbf54f0 storageutil: move _censoredtext() from revlog
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39878
diff changeset
    77
    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
    78
1b65fb4d43d6 storageutil: new function for extracting metadata-less content from text
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39879
diff changeset
    79
def filtermetadata(text):
1b65fb4d43d6 storageutil: new function for extracting metadata-less content from text
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39879
diff changeset
    80
    """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
    81
1b65fb4d43d6 storageutil: new function for extracting metadata-less content from text
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39879
diff changeset
    82
    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
    83
    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
    84
    """
1b65fb4d43d6 storageutil: new function for extracting metadata-less content from text
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39879
diff changeset
    85
    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
    86
        return text
1b65fb4d43d6 storageutil: new function for extracting metadata-less content from text
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39879
diff changeset
    87
1b65fb4d43d6 storageutil: new function for extracting metadata-less content from text
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39879
diff changeset
    88
    offset = text.index(b'\x01\n', 2)
1b65fb4d43d6 storageutil: new function for extracting metadata-less content from text
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39879
diff changeset
    89
    return text[offset + 2:]
39881
d63153611ed5 storageutil: extract revision number iteration
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39880
diff changeset
    90
d63153611ed5 storageutil: extract revision number iteration
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39880
diff changeset
    91
def iterrevs(storelen, start=0, stop=None):
d63153611ed5 storageutil: extract revision number iteration
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39880
diff changeset
    92
    """Iterate over revision numbers in a store."""
d63153611ed5 storageutil: extract revision number iteration
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39880
diff changeset
    93
    step = 1
d63153611ed5 storageutil: extract revision number iteration
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39880
diff changeset
    94
d63153611ed5 storageutil: extract revision number iteration
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39880
diff changeset
    95
    if stop is not None:
d63153611ed5 storageutil: extract revision number iteration
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39880
diff changeset
    96
        if start > stop:
d63153611ed5 storageutil: extract revision number iteration
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39880
diff changeset
    97
            step = -1
d63153611ed5 storageutil: extract revision number iteration
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39880
diff changeset
    98
        stop += step
d63153611ed5 storageutil: extract revision number iteration
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39880
diff changeset
    99
        if stop > storelen:
d63153611ed5 storageutil: extract revision number iteration
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39880
diff changeset
   100
            stop = storelen
d63153611ed5 storageutil: extract revision number iteration
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39880
diff changeset
   101
    else:
d63153611ed5 storageutil: extract revision number iteration
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39880
diff changeset
   102
        stop = storelen
d63153611ed5 storageutil: extract revision number iteration
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39880
diff changeset
   103
d63153611ed5 storageutil: extract revision number iteration
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39880
diff changeset
   104
    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
   105
0e8836be9541 storageutil: implement file identifier resolution method (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39881
diff changeset
   106
def fileidlookup(store, fileid, identifier):
0e8836be9541 storageutil: implement file identifier resolution method (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39881
diff changeset
   107
    """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
   108
0e8836be9541 storageutil: implement file identifier resolution method (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39881
diff changeset
   109
    ``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
   110
0e8836be9541 storageutil: implement file identifier resolution method (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39881
diff changeset
   111
    ``fileid`` can be:
0e8836be9541 storageutil: implement file identifier resolution method (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39881
diff changeset
   112
0e8836be9541 storageutil: implement file identifier resolution method (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39881
diff changeset
   113
    * A 20 byte binary node.
0e8836be9541 storageutil: implement file identifier resolution method (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39881
diff changeset
   114
    * An integer revision number
0e8836be9541 storageutil: implement file identifier resolution method (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39881
diff changeset
   115
    * A 40 byte hex node.
0e8836be9541 storageutil: implement file identifier resolution method (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39881
diff changeset
   116
    * 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
   117
0e8836be9541 storageutil: implement file identifier resolution method (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39881
diff changeset
   118
    ``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
   119
    for the store.
0e8836be9541 storageutil: implement file identifier resolution method (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39881
diff changeset
   120
0e8836be9541 storageutil: implement file identifier resolution method (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39881
diff changeset
   121
    Raises ``error.LookupError`` on failure.
0e8836be9541 storageutil: implement file identifier resolution method (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39881
diff changeset
   122
    """
0e8836be9541 storageutil: implement file identifier resolution method (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39881
diff changeset
   123
    if isinstance(fileid, int):
0e8836be9541 storageutil: implement file identifier resolution method (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39881
diff changeset
   124
        return store.node(fileid)
0e8836be9541 storageutil: implement file identifier resolution method (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39881
diff changeset
   125
0e8836be9541 storageutil: implement file identifier resolution method (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39881
diff changeset
   126
    if len(fileid) == 20:
0e8836be9541 storageutil: implement file identifier resolution method (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39881
diff changeset
   127
        try:
0e8836be9541 storageutil: implement file identifier resolution method (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39881
diff changeset
   128
            store.rev(fileid)
0e8836be9541 storageutil: implement file identifier resolution method (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39881
diff changeset
   129
            return fileid
0e8836be9541 storageutil: implement file identifier resolution method (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39881
diff changeset
   130
        except error.LookupError:
0e8836be9541 storageutil: implement file identifier resolution method (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39881
diff changeset
   131
            pass
0e8836be9541 storageutil: implement file identifier resolution method (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39881
diff changeset
   132
0e8836be9541 storageutil: implement file identifier resolution method (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39881
diff changeset
   133
    if len(fileid) == 40:
0e8836be9541 storageutil: implement file identifier resolution method (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39881
diff changeset
   134
        try:
0e8836be9541 storageutil: implement file identifier resolution method (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39881
diff changeset
   135
            rawnode = bin(fileid)
0e8836be9541 storageutil: implement file identifier resolution method (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39881
diff changeset
   136
            store.rev(rawnode)
0e8836be9541 storageutil: implement file identifier resolution method (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39881
diff changeset
   137
            return rawnode
0e8836be9541 storageutil: implement file identifier resolution method (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39881
diff changeset
   138
        except TypeError:
0e8836be9541 storageutil: implement file identifier resolution method (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39881
diff changeset
   139
            pass
0e8836be9541 storageutil: implement file identifier resolution method (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39881
diff changeset
   140
0e8836be9541 storageutil: implement file identifier resolution method (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39881
diff changeset
   141
    try:
0e8836be9541 storageutil: implement file identifier resolution method (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39881
diff changeset
   142
        rev = int(fileid)
0e8836be9541 storageutil: implement file identifier resolution method (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39881
diff changeset
   143
0e8836be9541 storageutil: implement file identifier resolution method (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39881
diff changeset
   144
        if b'%d' % rev != fileid:
0e8836be9541 storageutil: implement file identifier resolution method (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39881
diff changeset
   145
            raise ValueError
0e8836be9541 storageutil: implement file identifier resolution method (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39881
diff changeset
   146
0e8836be9541 storageutil: implement file identifier resolution method (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39881
diff changeset
   147
        try:
0e8836be9541 storageutil: implement file identifier resolution method (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39881
diff changeset
   148
            return store.node(rev)
0e8836be9541 storageutil: implement file identifier resolution method (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39881
diff changeset
   149
        except (IndexError, TypeError):
0e8836be9541 storageutil: implement file identifier resolution method (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39881
diff changeset
   150
            pass
0e8836be9541 storageutil: implement file identifier resolution method (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39881
diff changeset
   151
    except (ValueError, OverflowError):
0e8836be9541 storageutil: implement file identifier resolution method (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39881
diff changeset
   152
        pass
0e8836be9541 storageutil: implement file identifier resolution method (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39881
diff changeset
   153
0e8836be9541 storageutil: implement file identifier resolution method (BC)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39881
diff changeset
   154
    raise error.LookupError(fileid, identifier, _('no match found'))