| 1 |
#!/usr/bin/env python3 |
| 2 |
|
| 3 |
import hashlib |
| 4 |
import os |
| 5 |
import random |
| 6 |
import shutil |
| 7 |
import stat |
| 8 |
import struct |
| 9 |
import sys |
| 10 |
import tempfile |
| 11 |
import unittest |
| 12 |
|
| 13 |
import silenttestrunner |
| 14 |
|
| 15 |
from mercurial.node import sha1nodeconstants |
| 16 |
from mercurial import ( |
| 17 |
pycompat, |
| 18 |
ui as uimod, |
| 19 |
) |
| 20 |
|
| 21 |
# Load the local remotefilelog, not the system one |
| 22 |
sys.path[0:0] = [os.path.join(os.path.dirname(__file__), '..')] |
| 23 |
from hgext.remotefilelog import ( |
| 24 |
basepack, |
| 25 |
historypack, |
| 26 |
) |
| 27 |
|
| 28 |
|
| 29 |
class histpacktests(unittest.TestCase): |
| 30 |
def setUp(self): |
| 31 |
self.tempdirs = [] |
| 32 |
|
| 33 |
def tearDown(self): |
| 34 |
for d in self.tempdirs: |
| 35 |
shutil.rmtree(d) |
| 36 |
|
| 37 |
def makeTempDir(self): |
| 38 |
tempdir = tempfile.mkdtemp() |
| 39 |
self.tempdirs.append(tempdir) |
| 40 |
return pycompat.fsencode(tempdir) |
| 41 |
|
| 42 |
def getHash(self, content): |
| 43 |
return hashlib.sha1(content).digest() |
| 44 |
|
| 45 |
def getFakeHash(self): |
| 46 |
return b''.join( |
| 47 |
pycompat.bytechr(random.randint(0, 255)) for _ in range(20) |
| 48 |
) |
| 49 |
|
| 50 |
def createPack(self, revisions=None): |
| 51 |
"""Creates and returns a historypack containing the specified revisions. |
| 52 |
|
| 53 |
`revisions` is a list of tuples, where each tuple contains a filanem, |
| 54 |
node, p1node, p2node, and linknode. |
| 55 |
""" |
| 56 |
if revisions is None: |
| 57 |
revisions = [ |
| 58 |
( |
| 59 |
b"filename", |
| 60 |
self.getFakeHash(), |
| 61 |
sha1nodeconstants.nullid, |
| 62 |
sha1nodeconstants.nullid, |
| 63 |
self.getFakeHash(), |
| 64 |
None, |
| 65 |
) |
| 66 |
] |
| 67 |
|
| 68 |
packdir = pycompat.fsencode(self.makeTempDir()) |
| 69 |
packer = historypack.mutablehistorypack(uimod.ui(), packdir, version=2) |
| 70 |
|
| 71 |
for filename, node, p1, p2, linknode, copyfrom in revisions: |
| 72 |
packer.add(filename, node, p1, p2, linknode, copyfrom) |
| 73 |
|
| 74 |
path = packer.close() |
| 75 |
return historypack.historypack(path) |
| 76 |
|
| 77 |
def testAddSingle(self): |
| 78 |
"""Test putting a single entry into a pack and reading it out.""" |
| 79 |
filename = b"foo" |
| 80 |
node = self.getFakeHash() |
| 81 |
p1 = self.getFakeHash() |
| 82 |
p2 = self.getFakeHash() |
| 83 |
linknode = self.getFakeHash() |
| 84 |
|
| 85 |
revisions = [(filename, node, p1, p2, linknode, None)] |
| 86 |
pack = self.createPack(revisions) |
| 87 |
|
| 88 |
actual = pack.getancestors(filename, node)[node] |
| 89 |
self.assertEqual(p1, actual[0]) |
| 90 |
self.assertEqual(p2, actual[1]) |
| 91 |
self.assertEqual(linknode, actual[2]) |
| 92 |
|
| 93 |
def testAddMultiple(self): |
| 94 |
"""Test putting multiple unrelated revisions into a pack and reading |
| 95 |
them out. |
| 96 |
""" |
| 97 |
revisions = [] |
| 98 |
for i in range(10): |
| 99 |
filename = b"foo-%d" % i |
| 100 |
node = self.getFakeHash() |
| 101 |
p1 = self.getFakeHash() |
| 102 |
p2 = self.getFakeHash() |
| 103 |
linknode = self.getFakeHash() |
| 104 |
revisions.append((filename, node, p1, p2, linknode, None)) |
| 105 |
|
| 106 |
pack = self.createPack(revisions) |
| 107 |
|
| 108 |
for filename, node, p1, p2, linknode, copyfrom in revisions: |
| 109 |
actual = pack.getancestors(filename, node)[node] |
| 110 |
self.assertEqual(p1, actual[0]) |
| 111 |
self.assertEqual(p2, actual[1]) |
| 112 |
self.assertEqual(linknode, actual[2]) |
| 113 |
self.assertEqual(copyfrom, actual[3]) |
| 114 |
|
| 115 |
def testAddAncestorChain(self): |
| 116 |
"""Test putting multiple revisions in into a pack and read the ancestor |
| 117 |
chain. |
| 118 |
""" |
| 119 |
revisions = [] |
| 120 |
filename = b"foo" |
| 121 |
lastnode = sha1nodeconstants.nullid |
| 122 |
for i in range(10): |
| 123 |
node = self.getFakeHash() |
| 124 |
revisions.append( |
| 125 |
( |
| 126 |
filename, |
| 127 |
node, |
| 128 |
lastnode, |
| 129 |
sha1nodeconstants.nullid, |
| 130 |
sha1nodeconstants.nullid, |
| 131 |
None, |
| 132 |
) |
| 133 |
) |
| 134 |
lastnode = node |
| 135 |
|
| 136 |
# revisions must be added in topological order, newest first |
| 137 |
revisions = list(reversed(revisions)) |
| 138 |
pack = self.createPack(revisions) |
| 139 |
|
| 140 |
# Test that the chain has all the entries |
| 141 |
ancestors = pack.getancestors(revisions[0][0], revisions[0][1]) |
| 142 |
for filename, node, p1, p2, linknode, copyfrom in revisions: |
| 143 |
ap1, ap2, alinknode, acopyfrom = ancestors[node] |
| 144 |
self.assertEqual(ap1, p1) |
| 145 |
self.assertEqual(ap2, p2) |
| 146 |
self.assertEqual(alinknode, linknode) |
| 147 |
self.assertEqual(acopyfrom, copyfrom) |
| 148 |
|
| 149 |
def testPackMany(self): |
| 150 |
"""Pack many related and unrelated ancestors.""" |
| 151 |
# Build a random pack file |
| 152 |
allentries = {} |
| 153 |
ancestorcounts = {} |
| 154 |
revisions = [] |
| 155 |
random.seed(0) |
| 156 |
for i in range(100): |
| 157 |
filename = b"filename-%d" % i |
| 158 |
entries = [] |
| 159 |
p2 = sha1nodeconstants.nullid |
| 160 |
linknode = sha1nodeconstants.nullid |
| 161 |
for j in range(random.randint(1, 100)): |
| 162 |
node = self.getFakeHash() |
| 163 |
p1 = sha1nodeconstants.nullid |
| 164 |
if len(entries) > 0: |
| 165 |
p1 = entries[random.randint(0, len(entries) - 1)] |
| 166 |
entries.append(node) |
| 167 |
revisions.append((filename, node, p1, p2, linknode, None)) |
| 168 |
allentries[(filename, node)] = (p1, p2, linknode) |
| 169 |
if p1 == sha1nodeconstants.nullid: |
| 170 |
ancestorcounts[(filename, node)] = 1 |
| 171 |
else: |
| 172 |
newcount = ancestorcounts[(filename, p1)] + 1 |
| 173 |
ancestorcounts[(filename, node)] = newcount |
| 174 |
|
| 175 |
# Must add file entries in reverse topological order |
| 176 |
revisions = list(reversed(revisions)) |
| 177 |
pack = self.createPack(revisions) |
| 178 |
|
| 179 |
# Verify the pack contents |
| 180 |
for filename, node in allentries: |
| 181 |
ancestors = pack.getancestors(filename, node) |
| 182 |
self.assertEqual(ancestorcounts[(filename, node)], len(ancestors)) |
| 183 |
for anode, (ap1, ap2, alinknode, copyfrom) in ancestors.items(): |
| 184 |
ep1, ep2, elinknode = allentries[(filename, anode)] |
| 185 |
self.assertEqual(ap1, ep1) |
| 186 |
self.assertEqual(ap2, ep2) |
| 187 |
self.assertEqual(alinknode, elinknode) |
| 188 |
self.assertEqual(copyfrom, None) |
| 189 |
|
| 190 |
def testGetNodeInfo(self): |
| 191 |
revisions = [] |
| 192 |
filename = b"foo" |
| 193 |
lastnode = sha1nodeconstants.nullid |
| 194 |
for i in range(10): |
| 195 |
node = self.getFakeHash() |
| 196 |
revisions.append( |
| 197 |
( |
| 198 |
filename, |
| 199 |
node, |
| 200 |
lastnode, |
| 201 |
sha1nodeconstants.nullid, |
| 202 |
sha1nodeconstants.nullid, |
| 203 |
None, |
| 204 |
) |
| 205 |
) |
| 206 |
lastnode = node |
| 207 |
|
| 208 |
pack = self.createPack(revisions) |
| 209 |
|
| 210 |
# Test that getnodeinfo returns the expected results |
| 211 |
for filename, node, p1, p2, linknode, copyfrom in revisions: |
| 212 |
ap1, ap2, alinknode, acopyfrom = pack.getnodeinfo(filename, node) |
| 213 |
self.assertEqual(ap1, p1) |
| 214 |
self.assertEqual(ap2, p2) |
| 215 |
self.assertEqual(alinknode, linknode) |
| 216 |
self.assertEqual(acopyfrom, copyfrom) |
| 217 |
|
| 218 |
def testGetMissing(self): |
| 219 |
"""Test the getmissing() api.""" |
| 220 |
revisions = [] |
| 221 |
filename = b"foo" |
| 222 |
for i in range(10): |
| 223 |
node = self.getFakeHash() |
| 224 |
p1 = self.getFakeHash() |
| 225 |
p2 = self.getFakeHash() |
| 226 |
linknode = self.getFakeHash() |
| 227 |
revisions.append((filename, node, p1, p2, linknode, None)) |
| 228 |
|
| 229 |
pack = self.createPack(revisions) |
| 230 |
|
| 231 |
missing = pack.getmissing([(filename, revisions[0][1])]) |
| 232 |
self.assertFalse(missing) |
| 233 |
|
| 234 |
missing = pack.getmissing( |
| 235 |
[(filename, revisions[0][1]), (filename, revisions[1][1])] |
| 236 |
) |
| 237 |
self.assertFalse(missing) |
| 238 |
|
| 239 |
fakenode = self.getFakeHash() |
| 240 |
missing = pack.getmissing( |
| 241 |
[(filename, revisions[0][1]), (filename, fakenode)] |
| 242 |
) |
| 243 |
self.assertEqual(missing, [(filename, fakenode)]) |
| 244 |
|
| 245 |
# Test getmissing on a non-existant filename |
| 246 |
missing = pack.getmissing([(b"bar", fakenode)]) |
| 247 |
self.assertEqual(missing, [(b"bar", fakenode)]) |
| 248 |
|
| 249 |
def testAddThrows(self): |
| 250 |
pack = self.createPack() |
| 251 |
|
| 252 |
try: |
| 253 |
pack.add( |
| 254 |
b'filename', |
| 255 |
sha1nodeconstants.nullid, |
| 256 |
sha1nodeconstants.nullid, |
| 257 |
sha1nodeconstants.nullid, |
| 258 |
sha1nodeconstants.nullid, |
| 259 |
None, |
| 260 |
) |
| 261 |
self.assertTrue(False, "historypack.add should throw") |
| 262 |
except RuntimeError: |
| 263 |
pass |
| 264 |
|
| 265 |
def testBadVersionThrows(self): |
| 266 |
pack = self.createPack() |
| 267 |
path = pack.path + b'.histpack' |
| 268 |
with open(path, 'rb') as f: |
| 269 |
raw = f.read() |
| 270 |
raw = struct.pack('!B', 255) + raw[1:] |
| 271 |
os.chmod(path, os.stat(path).st_mode | stat.S_IWRITE) |
| 272 |
with open(path, 'wb+') as f: |
| 273 |
f.write(raw) |
| 274 |
|
| 275 |
try: |
| 276 |
historypack.historypack(pack.path) |
| 277 |
self.assertTrue(False, "bad version number should have thrown") |
| 278 |
except RuntimeError: |
| 279 |
pass |
| 280 |
|
| 281 |
def testLargePack(self): |
| 282 |
"""Test creating and reading from a large pack with over X entries. |
| 283 |
This causes it to use a 2^16 fanout table instead.""" |
| 284 |
total = basepack.SMALLFANOUTCUTOFF + 1 |
| 285 |
revisions = [] |
| 286 |
for i in range(total): |
| 287 |
filename = b"foo-%d" % i |
| 288 |
node = self.getFakeHash() |
| 289 |
p1 = self.getFakeHash() |
| 290 |
p2 = self.getFakeHash() |
| 291 |
linknode = self.getFakeHash() |
| 292 |
revisions.append((filename, node, p1, p2, linknode, None)) |
| 293 |
|
| 294 |
pack = self.createPack(revisions) |
| 295 |
self.assertEqual(pack.params.fanoutprefix, basepack.LARGEFANOUTPREFIX) |
| 296 |
|
| 297 |
for filename, node, p1, p2, linknode, copyfrom in revisions: |
| 298 |
actual = pack.getancestors(filename, node)[node] |
| 299 |
self.assertEqual(p1, actual[0]) |
| 300 |
self.assertEqual(p2, actual[1]) |
| 301 |
self.assertEqual(linknode, actual[2]) |
| 302 |
self.assertEqual(copyfrom, actual[3]) |
| 303 |
|
| 304 |
|
| 305 |
# TODO: |
| 306 |
# histpack store: |
| 307 |
# - repack two packs into one |
| 308 |
|
| 309 |
if __name__ == '__main__': |
| 310 |
if pycompat.iswindows: |
| 311 |
sys.exit(80) # Skip on Windows |
| 312 |
silenttestrunner.main(__name__) |