File: historypack.py

package info (click to toggle)
mercurial 7.1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 45,080 kB
  • sloc: python: 208,589; ansic: 56,460; tcl: 3,715; sh: 1,839; lisp: 1,483; cpp: 864; makefile: 769; javascript: 649; xml: 36
file content (574 lines) | stat: -rw-r--r-- 19,081 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
from __future__ import annotations

import struct

from mercurial.node import (
    hex,
    sha1nodeconstants,
)
from mercurial import (
    util,
)
from mercurial.utils import hashutil
from . import (
    basepack,
    constants,
    shallowutil,
)

# (filename hash, offset, size)
INDEXFORMAT2 = b'!20sQQII'
INDEXENTRYLENGTH2 = struct.calcsize(INDEXFORMAT2)
NODELENGTH = 20

NODEINDEXFORMAT = b'!20sQ'
NODEINDEXENTRYLENGTH = struct.calcsize(NODEINDEXFORMAT)

# (node, p1, p2, linknode)
PACKFORMAT = b"!20s20s20s20sH"
PACKENTRYLENGTH = 82

ENTRYCOUNTSIZE = 4

INDEXSUFFIX = b'.histidx'
PACKSUFFIX = b'.histpack'

ANC_NODE = 0
ANC_P1NODE = 1
ANC_P2NODE = 2
ANC_LINKNODE = 3
ANC_COPYFROM = 4


class historypackstore(basepack.basepackstore):
    INDEXSUFFIX = INDEXSUFFIX
    PACKSUFFIX = PACKSUFFIX

    def getpack(self, path):
        return historypack(path)

    def getancestors(self, name, node, known=None):
        for pack in self.packs:
            try:
                return pack.getancestors(name, node, known=known)
            except KeyError:
                pass

        for pack in self.refresh():
            try:
                return pack.getancestors(name, node, known=known)
            except KeyError:
                pass

        raise KeyError((name, node))

    def getnodeinfo(self, name, node):
        for pack in self.packs:
            try:
                return pack.getnodeinfo(name, node)
            except KeyError:
                pass

        for pack in self.refresh():
            try:
                return pack.getnodeinfo(name, node)
            except KeyError:
                pass

        raise KeyError((name, node))

    def add(self, filename, node, p1, p2, linknode, copyfrom):
        raise RuntimeError(
            b"cannot add to historypackstore (%s:%s)" % (filename, hex(node))
        )


class historypack(basepack.basepack):
    INDEXSUFFIX = INDEXSUFFIX
    PACKSUFFIX = PACKSUFFIX

    SUPPORTED_VERSIONS = [2]

    def __init__(self, path):
        super().__init__(path)
        self.INDEXFORMAT = INDEXFORMAT2
        self.INDEXENTRYLENGTH = INDEXENTRYLENGTH2

    def getmissing(self, keys):
        missing = []
        for name, node in keys:
            try:
                self._findnode(name, node)
            except KeyError:
                missing.append((name, node))

        return missing

    def getancestors(self, name, node, known=None):
        """Returns as many ancestors as we're aware of.

        return value: {
           node: (p1, p2, linknode, copyfrom),
           ...
        }
        """
        if known and node in known:
            return []

        ancestors = self._getancestors(name, node, known=known)
        results = {}
        for ancnode, p1, p2, linknode, copyfrom in ancestors:
            results[ancnode] = (p1, p2, linknode, copyfrom)

        if not results:
            raise KeyError((name, node))
        return results

    def getnodeinfo(self, name, node):
        # Drop the node from the tuple before returning, since the result should
        # just be (p1, p2, linknode, copyfrom)
        return self._findnode(name, node)[1:]

    def _getancestors(self, name, node, known=None):
        if known is None:
            known = set()
        section = self._findsection(name)
        filename, offset, size, nodeindexoffset, nodeindexsize = section
        pending = {node}
        o = 0
        while o < size:
            if not pending:
                break
            entry, copyfrom = self._readentry(offset + o)
            o += PACKENTRYLENGTH
            if copyfrom:
                o += len(copyfrom)

            ancnode = entry[ANC_NODE]
            if ancnode in pending:
                pending.remove(ancnode)
                p1node = entry[ANC_P1NODE]
                p2node = entry[ANC_P2NODE]
                if p1node != sha1nodeconstants.nullid and p1node not in known:
                    pending.add(p1node)
                if p2node != sha1nodeconstants.nullid and p2node not in known:
                    pending.add(p2node)

                yield (ancnode, p1node, p2node, entry[ANC_LINKNODE], copyfrom)

    def _readentry(self, offset):
        data = self._data
        entry = struct.unpack(
            PACKFORMAT, data[offset : offset + PACKENTRYLENGTH]
        )
        copyfrom = None
        copyfromlen = entry[ANC_COPYFROM]
        if copyfromlen != 0:
            offset += PACKENTRYLENGTH
            copyfrom = data[offset : offset + copyfromlen]
        return entry, copyfrom

    def add(self, filename, node, p1, p2, linknode, copyfrom):
        raise RuntimeError(
            b"cannot add to historypack (%s:%s)" % (filename, hex(node))
        )

    def _findnode(self, name, node):
        if self.VERSION == 0:
            ancestors = self._getancestors(name, node)
            for ancnode, p1node, p2node, linknode, copyfrom in ancestors:
                if ancnode == node:
                    return (ancnode, p1node, p2node, linknode, copyfrom)
        else:
            section = self._findsection(name)
            nodeindexoffset, nodeindexsize = section[3:]
            entry = self._bisect(
                node,
                nodeindexoffset,
                nodeindexoffset + nodeindexsize,
                NODEINDEXENTRYLENGTH,
            )
            if entry is not None:
                node, offset = struct.unpack(NODEINDEXFORMAT, entry)
                entry, copyfrom = self._readentry(offset)
                # Drop the copyfromlen from the end of entry, and replace it
                # with the copyfrom string.
                return entry[:4] + (copyfrom,)

        raise KeyError(b"unable to find history for %s:%s" % (name, hex(node)))

    def _findsection(self, name):
        params = self.params
        namehash = hashutil.sha1(name).digest()
        fanoutkey = struct.unpack(
            params.fanoutstruct, namehash[: params.fanoutprefix]
        )[0]
        fanout = self._fanouttable

        start = fanout[fanoutkey] + params.indexstart
        indexend = self._indexend

        for i in range(fanoutkey + 1, params.fanoutcount):
            end = fanout[i] + params.indexstart
            if end != start:
                break
        else:
            end = indexend

        entry = self._bisect(namehash, start, end, self.INDEXENTRYLENGTH)
        if not entry:
            raise KeyError(name)

        rawentry = struct.unpack(self.INDEXFORMAT, entry)
        x, offset, size, nodeindexoffset, nodeindexsize = rawentry
        rawnamelen = self._index[
            nodeindexoffset : nodeindexoffset + constants.FILENAMESIZE
        ]
        actualnamelen = struct.unpack(b'!H', rawnamelen)[0]
        nodeindexoffset += constants.FILENAMESIZE
        actualname = self._index[
            nodeindexoffset : nodeindexoffset + actualnamelen
        ]
        if actualname != name:
            raise KeyError(
                b"found file name %s when looking for %s" % (actualname, name)
            )
        nodeindexoffset += actualnamelen

        filenamelength = struct.unpack(
            b'!H', self._data[offset : offset + constants.FILENAMESIZE]
        )[0]
        offset += constants.FILENAMESIZE

        actualname = self._data[offset : offset + filenamelength]
        offset += filenamelength

        if name != actualname:
            raise KeyError(
                b"found file name %s when looking for %s" % (actualname, name)
            )

        # Skip entry list size
        offset += ENTRYCOUNTSIZE

        nodelistoffset = offset
        nodelistsize = (
            size - constants.FILENAMESIZE - filenamelength - ENTRYCOUNTSIZE
        )
        return (
            name,
            nodelistoffset,
            nodelistsize,
            nodeindexoffset,
            nodeindexsize,
        )

    def _bisect(self, node, start, end, entrylen):
        # Bisect between start and end to find node
        origstart = start
        startnode = self._index[start : start + NODELENGTH]
        endnode = self._index[end : end + NODELENGTH]

        if startnode == node:
            return self._index[start : start + entrylen]
        elif endnode == node:
            return self._index[end : end + entrylen]
        else:
            while start < end - entrylen:
                mid = start + (end - start) // 2
                mid = mid - ((mid - origstart) % entrylen)
                midnode = self._index[mid : mid + NODELENGTH]
                if midnode == node:
                    return self._index[mid : mid + entrylen]
                if node > midnode:
                    start = mid
                elif node < midnode:
                    end = mid
        return None

    def markledger(self, ledger, options=None):
        for filename, node in self:
            ledger.markhistoryentry(self, filename, node)

    def cleanup(self, ledger):
        entries = ledger.sources.get(self, [])
        allkeys = set(self)
        repackedkeys = {
            (e.filename, e.node) for e in entries if e.historyrepacked
        }

        if len(allkeys - repackedkeys) == 0:
            if self.path not in ledger.created:
                util.unlinkpath(self.indexpath, ignoremissing=True)
                util.unlinkpath(self.packpath, ignoremissing=True)

    def __iter__(self):
        for f, n, x, x, x, x in self.iterentries():
            yield f, n

    def iterentries(self):
        # Start at 1 to skip the header
        offset = 1
        while offset < self.datasize:
            data = self._data
            # <2 byte len> + <filename>
            filenamelen = struct.unpack(
                b'!H', data[offset : offset + constants.FILENAMESIZE]
            )[0]
            offset += constants.FILENAMESIZE
            filename = data[offset : offset + filenamelen]
            offset += filenamelen

            revcount = struct.unpack(
                b'!I', data[offset : offset + ENTRYCOUNTSIZE]
            )[0]
            offset += ENTRYCOUNTSIZE

            for i in range(revcount):
                entry = struct.unpack(
                    PACKFORMAT, data[offset : offset + PACKENTRYLENGTH]
                )
                offset += PACKENTRYLENGTH

                copyfrom = data[offset : offset + entry[ANC_COPYFROM]]
                offset += entry[ANC_COPYFROM]

                yield (
                    filename,
                    entry[ANC_NODE],
                    entry[ANC_P1NODE],
                    entry[ANC_P2NODE],
                    entry[ANC_LINKNODE],
                    copyfrom,
                )

                self._pagedin += PACKENTRYLENGTH

            # If we've read a lot of data from the mmap, free some memory.
            self.freememory()


class mutablehistorypack(basepack.mutablebasepack):
    """A class for constructing and serializing a histpack file and index.

    A history pack is a pair of files that contain the revision history for
    various file revisions in Mercurial. It contains only revision history (like
    parent pointers and linknodes), not any revision content information.

    It consists of two files, with the following format:

    .histpack
        The pack itself is a series of file revisions with some basic header
        information on each.

        datapack = <version: 1 byte>
                   [<filesection>,...]
        filesection = <filename len: 2 byte unsigned int>
                      <filename>
                      <revision count: 4 byte unsigned int>
                      [<revision>,...]
        revision = <node: 20 byte>
                   <p1node: 20 byte>
                   <p2node: 20 byte>
                   <linknode: 20 byte>
                   <copyfromlen: 2 byte>
                   <copyfrom>

        The revisions within each filesection are stored in topological order
        (newest first). If a given entry has a parent from another file (a copy)
        then p1node is the node from the other file, and copyfrom is the
        filepath of the other file.

    .histidx
        The index file provides a mapping from filename to the file section in
        the histpack. In V1 it also contains sub-indexes for specific nodes
        within each file. It consists of three parts, the fanout, the file index
        and the node indexes.

        The file index is a list of index entries, sorted by filename hash (one
        per file section in the pack). Each entry has:

        - node (The 20 byte hash of the filename)
        - pack entry offset (The location of this file section in the histpack)
        - pack content size (The on-disk length of this file section's pack
                             data)
        - node index offset (The location of the file's node index in the index
                             file) [1]
        - node index size (the on-disk length of this file's node index) [1]

        The fanout is a quick lookup table to reduce the number of steps for
        bisecting the index. It is a series of 4 byte pointers to positions
        within the index. It has 2^16 entries, which corresponds to hash
        prefixes [00, 01, 02,..., FD, FE, FF]. Example: the pointer in slot 4F
        points to the index position of the first revision whose node starts
        with 4F. This saves log(2^16) bisect steps.

        dataidx = <fanouttable>
                  <file count: 8 byte unsigned> [1]
                  <fileindex>
                  <node count: 8 byte unsigned> [1]
                  [<nodeindex>,...] [1]
        fanouttable = [<index offset: 4 byte unsigned int>,...] (2^16 entries)

        fileindex = [<file index entry>,...]
        fileindexentry = <node: 20 byte>
                         <pack file section offset: 8 byte unsigned int>
                         <pack file section size: 8 byte unsigned int>
                         <node index offset: 4 byte unsigned int> [1]
                         <node index size: 4 byte unsigned int>   [1]
        nodeindex = <filename>[<node index entry>,...] [1]
        filename = <filename len : 2 byte unsigned int><filename value> [1]
        nodeindexentry = <node: 20 byte> [1]
                         <pack file node offset: 8 byte unsigned int> [1]

    [1]: new in version 1.
    """

    INDEXSUFFIX = INDEXSUFFIX
    PACKSUFFIX = PACKSUFFIX

    SUPPORTED_VERSIONS = [2]

    def __init__(self, ui, packpath, version=2):
        super().__init__(ui, packpath, version=version)
        self.files = {}
        self.entrylocations = {}
        self.fileentries = {}

        self.INDEXFORMAT = INDEXFORMAT2
        self.INDEXENTRYLENGTH = INDEXENTRYLENGTH2

        self.NODEINDEXFORMAT = NODEINDEXFORMAT
        self.NODEINDEXENTRYLENGTH = NODEINDEXENTRYLENGTH

    def add(self, filename, node, p1, p2, linknode, copyfrom):
        copyfrom = copyfrom or b''
        copyfromlen = struct.pack(b'!H', len(copyfrom))
        self.fileentries.setdefault(filename, []).append(
            (node, p1, p2, linknode, copyfromlen, copyfrom)
        )

    def _write(self):
        for filename in sorted(self.fileentries):
            entries = self.fileentries[filename]
            sectionstart = self.packfp.tell()

            # Write the file section content
            entrymap = {e[0]: e for e in entries}

            def parentfunc(node):
                x, p1, p2, x, x, x = entrymap[node]
                parents = []
                if p1 != sha1nodeconstants.nullid:
                    parents.append(p1)
                if p2 != sha1nodeconstants.nullid:
                    parents.append(p2)
                return parents

            sortednodes = list(
                reversed(
                    shallowutil.sortnodes((e[0] for e in entries), parentfunc)
                )
            )

            # Write the file section header
            self.writeraw(
                b"%s%s%s"
                % (
                    struct.pack(b'!H', len(filename)),
                    filename,
                    struct.pack(b'!I', len(sortednodes)),
                )
            )

            sectionlen = constants.FILENAMESIZE + len(filename) + 4

            rawstrings = []

            # Record the node locations for the index
            locations = self.entrylocations.setdefault(filename, {})
            offset = sectionstart + sectionlen
            for node in sortednodes:
                locations[node] = offset
                raw = b'%s%s%s%s%s%s' % entrymap[node]
                rawstrings.append(raw)
                offset += len(raw)

            rawdata = b''.join(rawstrings)
            sectionlen += len(rawdata)

            self.writeraw(rawdata)

            # Record metadata for the index
            self.files[filename] = (sectionstart, sectionlen)
            node = hashutil.sha1(filename).digest()
            self.entries[node] = node

    def close(self, ledger=None):
        if self._closed:
            return

        self._write()

        return super().close(ledger=ledger)

    def createindex(self, nodelocations, indexoffset):
        fileindexformat = self.INDEXFORMAT
        fileindexlength = self.INDEXENTRYLENGTH
        nodeindexformat = self.NODEINDEXFORMAT
        nodeindexlength = self.NODEINDEXENTRYLENGTH

        files = (
            (hashutil.sha1(filename).digest(), filename, offset, size)
            for filename, (offset, size) in self.files.items()
        )
        files = sorted(files)

        # node index is after file index size, file index, and node index size
        indexlensize = struct.calcsize(b'!Q')
        nodeindexoffset = (
            indexoffset
            + indexlensize
            + (len(files) * fileindexlength)
            + indexlensize
        )

        fileindexentries = []
        nodeindexentries = []
        nodecount = 0
        for namehash, filename, offset, size in files:
            # File section index
            nodelocations = self.entrylocations[filename]

            nodeindexsize = len(nodelocations) * nodeindexlength

            rawentry = struct.pack(
                fileindexformat,
                namehash,
                offset,
                size,
                nodeindexoffset,
                nodeindexsize,
            )
            # Node index
            nodeindexentries.append(
                struct.pack(constants.FILENAMESTRUCT, len(filename)) + filename
            )
            nodeindexoffset += constants.FILENAMESIZE + len(filename)

            for node, location in sorted(nodelocations.items()):
                nodeindexentries.append(
                    struct.pack(nodeindexformat, node, location)
                )
                nodecount += 1

            nodeindexoffset += len(nodelocations) * nodeindexlength

            fileindexentries.append(rawentry)

        nodecountraw = struct.pack(b'!Q', nodecount)
        return (
            b''.join(fileindexentries)
            + nodecountraw
            + b''.join(nodeindexentries)
        )