comparison mercurial/utils/storageutil.py @ 39877:f8eb71f9e3bd

storageutil: new module for storage primitives (API) There will exist common code between storage backends. It would be nice to have a central place to put that code. This commit attempts to create that place by creating the "storageutil" module. The first thing we move is revlog.hash(), which is the function for computing the SHA-1 hash of revision fulltext and parents. Differential Revision: https://phab.mercurial-scm.org/D4753
author Gregory Szorc <gregory.szorc@gmail.com>
date Mon, 24 Sep 2018 14:23:54 -0700
parents
children 3e896b51aa5d
comparison
equal deleted inserted replaced
39876:a269fa55467e 39877:f8eb71f9e3bd
1 # storageutil.py - Storage functionality agnostic of backend implementation.
2 #
3 # Copyright 2018 Gregory Szorc <gregory.szorc@gmail.com>
4 #
5 # This software may be used and distributed according to the terms of the
6 # GNU General Public License version 2 or any later version.
7
8 from __future__ import absolute_import
9
10 import hashlib
11
12 from ..node import (
13 nullid,
14 )
15
16 _nullhash = hashlib.sha1(nullid)
17
18 def hashrevisionsha1(text, p1, p2):
19 """Compute the SHA-1 for revision data and its parents.
20
21 This hash combines both the current file contents and its history
22 in a manner that makes it easy to distinguish nodes with the same
23 content in the revision graph.
24 """
25 # As of now, if one of the parent node is null, p2 is null
26 if p2 == nullid:
27 # deep copy of a hash is faster than creating one
28 s = _nullhash.copy()
29 s.update(p1)
30 else:
31 # none of the parent nodes are nullid
32 if p1 < p2:
33 a = p1
34 b = p2
35 else:
36 a = p2
37 b = p1
38 s = hashlib.sha1(a)
39 s.update(b)
40 s.update(text)
41 return s.digest()