comparison mercurial/revlog.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 733db72f0f54
children 3e896b51aa5d
comparison
equal deleted inserted replaced
39876:a269fa55467e 39877:f8eb71f9e3bd
14 from __future__ import absolute_import 14 from __future__ import absolute_import
15 15
16 import collections 16 import collections
17 import contextlib 17 import contextlib
18 import errno 18 import errno
19 import hashlib
20 import os 19 import os
21 import re 20 import re
22 import struct 21 import struct
23 import zlib 22 import zlib
24 23
72 from .revlogutils import ( 71 from .revlogutils import (
73 deltas as deltautil, 72 deltas as deltautil,
74 ) 73 )
75 from .utils import ( 74 from .utils import (
76 interfaceutil, 75 interfaceutil,
76 storageutil,
77 stringutil, 77 stringutil,
78 ) 78 )
79 79
80 # blanked usage of all the name to prevent pyflakes constraints 80 # blanked usage of all the name to prevent pyflakes constraints
81 # We need these name available in the module for extensions. 81 # We need these name available in the module for extensions.
194 194
195 def offset_type(offset, type): 195 def offset_type(offset, type):
196 if (type & ~REVIDX_KNOWN_FLAGS) != 0: 196 if (type & ~REVIDX_KNOWN_FLAGS) != 0:
197 raise ValueError('unknown revlog index flags') 197 raise ValueError('unknown revlog index flags')
198 return int(int(offset) << 16 | type) 198 return int(int(offset) << 16 | type)
199
200 _nullhash = hashlib.sha1(nullid)
201
202 def hash(text, p1, p2):
203 """generate a hash from the given text and its parent hashes
204
205 This hash combines both the current file contents and its history
206 in a manner that makes it easy to distinguish nodes with the same
207 content in the revision graph.
208 """
209 # As of now, if one of the parent node is null, p2 is null
210 if p2 == nullid:
211 # deep copy of a hash is faster than creating one
212 s = _nullhash.copy()
213 s.update(p1)
214 else:
215 # none of the parent nodes are nullid
216 if p1 < p2:
217 a = p1
218 b = p2
219 else:
220 a = p2
221 b = p1
222 s = hashlib.sha1(a)
223 s.update(b)
224 s.update(text)
225 return s.digest()
226 199
227 @attr.s(slots=True, frozen=True) 200 @attr.s(slots=True, frozen=True)
228 class _revisioninfo(object): 201 class _revisioninfo(object):
229 """Information about a revision that allows building its fulltext 202 """Information about a revision that allows building its fulltext
230 node: expected hash of the revision 203 node: expected hash of the revision
1381 """compare text with a given file revision 1354 """compare text with a given file revision
1382 1355
1383 returns True if text is different than what is stored. 1356 returns True if text is different than what is stored.
1384 """ 1357 """
1385 p1, p2 = self.parents(node) 1358 p1, p2 = self.parents(node)
1386 return hash(text, p1, p2) != node 1359 return storageutil.hashrevisionsha1(text, p1, p2) != node
1387 1360
1388 def _cachesegment(self, offset, data): 1361 def _cachesegment(self, offset, data):
1389 """Add a segment to the revlog cache. 1362 """Add a segment to the revlog cache.
1390 1363
1391 Accepts an absolute offset and the data that is at that location. 1364 Accepts an absolute offset and the data that is at that location.
1670 """Compute a node hash. 1643 """Compute a node hash.
1671 1644
1672 Available as a function so that subclasses can replace the hash 1645 Available as a function so that subclasses can replace the hash
1673 as needed. 1646 as needed.
1674 """ 1647 """
1675 return hash(text, p1, p2) 1648 return storageutil.hashrevisionsha1(text, p1, p2)
1676 1649
1677 def _processflags(self, text, flags, operation, raw=False): 1650 def _processflags(self, text, flags, operation, raw=False):
1678 """Inspect revision data flags and applies transforms defined by 1651 """Inspect revision data flags and applies transforms defined by
1679 registered flag processors. 1652 registered flag processors.
1680 1653