Mercurial > hg
diff mercurial/utils/storageutil.py @ 40002:0e8836be9541
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
author | Gregory Szorc <gregory.szorc@gmail.com> |
---|---|
date | Fri, 28 Sep 2018 11:03:17 -0700 |
parents | d63153611ed5 |
children | ad8389ecd3f5 |
line wrap: on
line diff
--- a/mercurial/utils/storageutil.py Fri Sep 28 11:00:20 2018 -0700 +++ b/mercurial/utils/storageutil.py Fri Sep 28 11:03:17 2018 -0700 @@ -10,10 +10,13 @@ import hashlib import re +from ..i18n import _ from ..node import ( + bin, nullid, ) from .. import ( + error, pycompat, ) @@ -99,3 +102,53 @@ stop = storelen return pycompat.xrange(start, stop, step) + +def fileidlookup(store, fileid, identifier): + """Resolve the file node for a value. + + ``store`` is an object implementing the ``ifileindex`` interface. + + ``fileid`` can be: + + * A 20 byte binary node. + * An integer revision number + * A 40 byte hex node. + * A bytes that can be parsed as an integer representing a revision number. + + ``identifier`` is used to populate ``error.LookupError`` with an identifier + for the store. + + Raises ``error.LookupError`` on failure. + """ + if isinstance(fileid, int): + return store.node(fileid) + + if len(fileid) == 20: + try: + store.rev(fileid) + return fileid + except error.LookupError: + pass + + if len(fileid) == 40: + try: + rawnode = bin(fileid) + store.rev(rawnode) + return rawnode + except TypeError: + pass + + try: + rev = int(fileid) + + if b'%d' % rev != fileid: + raise ValueError + + try: + return store.node(rev) + except (IndexError, TypeError): + pass + except (ValueError, OverflowError): + pass + + raise error.LookupError(fileid, identifier, _('no match found'))