File: repack.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 (904 lines) | stat: -rw-r--r-- 30,827 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
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
from __future__ import annotations

import os
import time

from mercurial.i18n import _
from mercurial.node import short
from mercurial import (
    encoding,
    error,
    lock as lockmod,
    mdiff,
    policy,
    scmutil,
    util,
    vfs,
)
from mercurial.utils import procutil
from . import (
    constants,
    contentstore,
    datapack,
    historypack,
    metadatastore,
    shallowutil,
)

osutil = policy.importmod('osutil')


class RepackAlreadyRunning(error.Abort):
    pass


def backgroundrepack(repo, incremental=True, packsonly=False):
    cmd = [procutil.hgexecutable(), b'-R', repo.origroot, b'repack']
    msg = _(b"(running background repack)\n")
    if incremental:
        cmd.append(b'--incremental')
        msg = _(b"(running background incremental repack)\n")
    if packsonly:
        cmd.append(b'--packsonly')
    repo.ui.warn(msg)
    # We know this command will find a binary, so don't block on it starting.
    kwargs = {}
    if repo.ui.configbool(b'devel', b'remotefilelog.bg-wait'):
        kwargs['record_wait'] = repo.ui.atexit

    procutil.runbgcommand(cmd, encoding.environ, ensurestart=False, **kwargs)


def fullrepack(repo, options=None):
    """If ``packsonly`` is True, stores creating only loose objects are skipped."""
    if hasattr(repo, 'shareddatastores'):
        datasource = contentstore.unioncontentstore(*repo.shareddatastores)
        historysource = metadatastore.unionmetadatastore(
            *repo.sharedhistorystores, allowincomplete=True
        )

        packpath = shallowutil.getcachepackpath(
            repo, constants.FILEPACK_CATEGORY
        )
        _runrepack(
            repo,
            datasource,
            historysource,
            packpath,
            constants.FILEPACK_CATEGORY,
            options=options,
        )

    if hasattr(repo.manifestlog, 'datastore'):
        localdata, shareddata = _getmanifeststores(repo)
        lpackpath, ldstores, lhstores = localdata
        spackpath, sdstores, shstores = shareddata

        # Repack the shared manifest store
        datasource = contentstore.unioncontentstore(*sdstores)
        historysource = metadatastore.unionmetadatastore(
            *shstores, allowincomplete=True
        )
        _runrepack(
            repo,
            datasource,
            historysource,
            spackpath,
            constants.TREEPACK_CATEGORY,
            options=options,
        )

        # Repack the local manifest store
        datasource = contentstore.unioncontentstore(
            *ldstores, allowincomplete=True
        )
        historysource = metadatastore.unionmetadatastore(
            *lhstores, allowincomplete=True
        )
        _runrepack(
            repo,
            datasource,
            historysource,
            lpackpath,
            constants.TREEPACK_CATEGORY,
            options=options,
        )


def incrementalrepack(repo, options=None):
    """This repacks the repo by looking at the distribution of pack files in the
    repo and performing the most minimal repack to keep the repo in good shape.
    """
    if hasattr(repo, 'shareddatastores'):
        packpath = shallowutil.getcachepackpath(
            repo, constants.FILEPACK_CATEGORY
        )
        _incrementalrepack(
            repo,
            repo.shareddatastores,
            repo.sharedhistorystores,
            packpath,
            constants.FILEPACK_CATEGORY,
            options=options,
        )

    if hasattr(repo.manifestlog, 'datastore'):
        localdata, shareddata = _getmanifeststores(repo)
        lpackpath, ldstores, lhstores = localdata
        spackpath, sdstores, shstores = shareddata

        # Repack the shared manifest store
        _incrementalrepack(
            repo,
            sdstores,
            shstores,
            spackpath,
            constants.TREEPACK_CATEGORY,
            options=options,
        )

        # Repack the local manifest store
        _incrementalrepack(
            repo,
            ldstores,
            lhstores,
            lpackpath,
            constants.TREEPACK_CATEGORY,
            allowincompletedata=True,
            options=options,
        )


def _getmanifeststores(repo):
    shareddatastores = repo.manifestlog.shareddatastores
    localdatastores = repo.manifestlog.localdatastores
    sharedhistorystores = repo.manifestlog.sharedhistorystores
    localhistorystores = repo.manifestlog.localhistorystores

    sharedpackpath = shallowutil.getcachepackpath(
        repo, constants.TREEPACK_CATEGORY
    )
    localpackpath = shallowutil.getlocalpackpath(
        repo.svfs.vfs.base, constants.TREEPACK_CATEGORY
    )

    return (
        (localpackpath, localdatastores, localhistorystores),
        (sharedpackpath, shareddatastores, sharedhistorystores),
    )


def _topacks(packpath, files, constructor):
    paths = list(os.path.join(packpath, p) for p in files)
    packs = list(constructor(p) for p in paths)
    return packs


def _deletebigpacks(repo, folder, files):
    """Deletes packfiles that are bigger than ``packs.maxpacksize``.

    Returns ``files` with the removed files omitted."""
    maxsize = repo.ui.configbytes(b"packs", b"maxpacksize")
    if maxsize <= 0:
        return files

    # This only considers datapacks today, but we could broaden it to include
    # historypacks.
    VALIDEXTS = [b".datapack", b".dataidx"]

    # Either an oversize index or datapack will trigger cleanup of the whole
    # pack:
    oversized = {
        os.path.splitext(path)[0]
        for path, ftype, stat in files
        if (stat.st_size > maxsize and (os.path.splitext(path)[1] in VALIDEXTS))
    }

    for rootfname in oversized:
        rootpath = os.path.join(folder, rootfname)
        for ext in VALIDEXTS:
            path = rootpath + ext
            repo.ui.debug(
                b'removing oversize packfile %s (%s)\n'
                % (path, util.bytecount(os.stat(path).st_size))
            )
            os.unlink(path)
    return [row for row in files if os.path.basename(row[0]) not in oversized]


def _incrementalrepack(
    repo,
    datastore,
    historystore,
    packpath,
    category,
    allowincompletedata=False,
    options=None,
):
    shallowutil.mkstickygroupdir(repo.ui, packpath)

    files = osutil.listdir(packpath, stat=True)
    files = _deletebigpacks(repo, packpath, files)
    datapacks = _topacks(
        packpath, _computeincrementaldatapack(repo.ui, files), datapack.datapack
    )
    datapacks.extend(
        s for s in datastore if not isinstance(s, datapack.datapackstore)
    )

    historypacks = _topacks(
        packpath,
        _computeincrementalhistorypack(repo.ui, files),
        historypack.historypack,
    )
    historypacks.extend(
        s
        for s in historystore
        if not isinstance(s, historypack.historypackstore)
    )

    # ``allhistory{files,packs}`` contains all known history packs, even ones we
    # don't plan to repack. They are used during the datapack repack to ensure
    # good ordering of nodes.
    allhistoryfiles = _allpackfileswithsuffix(
        files, historypack.PACKSUFFIX, historypack.INDEXSUFFIX
    )
    allhistorypacks = _topacks(
        packpath,
        (f for f, mode, stat in allhistoryfiles),
        historypack.historypack,
    )
    allhistorypacks.extend(
        s
        for s in historystore
        if not isinstance(s, historypack.historypackstore)
    )
    _runrepack(
        repo,
        contentstore.unioncontentstore(
            *datapacks, allowincomplete=allowincompletedata
        ),
        metadatastore.unionmetadatastore(*historypacks, allowincomplete=True),
        packpath,
        category,
        fullhistory=metadatastore.unionmetadatastore(
            *allhistorypacks, allowincomplete=True
        ),
        options=options,
    )


def _computeincrementaldatapack(ui, files):
    opts = {
        b'gencountlimit': ui.configint(b'remotefilelog', b'data.gencountlimit'),
        b'generations': ui.configlist(b'remotefilelog', b'data.generations'),
        b'maxrepackpacks': ui.configint(
            b'remotefilelog', b'data.maxrepackpacks'
        ),
        b'repackmaxpacksize': ui.configbytes(
            b'remotefilelog', b'data.repackmaxpacksize'
        ),
        b'repacksizelimit': ui.configbytes(
            b'remotefilelog', b'data.repacksizelimit'
        ),
    }

    packfiles = _allpackfileswithsuffix(
        files, datapack.PACKSUFFIX, datapack.INDEXSUFFIX
    )
    return _computeincrementalpack(packfiles, opts)


def _computeincrementalhistorypack(ui, files):
    opts = {
        b'gencountlimit': ui.configint(
            b'remotefilelog', b'history.gencountlimit'
        ),
        b'generations': ui.configlist(
            b'remotefilelog', b'history.generations', [b'100MB']
        ),
        b'maxrepackpacks': ui.configint(
            b'remotefilelog', b'history.maxrepackpacks'
        ),
        b'repackmaxpacksize': ui.configbytes(
            b'remotefilelog', b'history.repackmaxpacksize', b'400MB'
        ),
        b'repacksizelimit': ui.configbytes(
            b'remotefilelog', b'history.repacksizelimit'
        ),
    }

    packfiles = _allpackfileswithsuffix(
        files, historypack.PACKSUFFIX, historypack.INDEXSUFFIX
    )
    return _computeincrementalpack(packfiles, opts)


def _allpackfileswithsuffix(files, packsuffix, indexsuffix):
    result = []
    fileset = {fn for fn, mode, stat in files}
    for filename, mode, stat in files:
        if not filename.endswith(packsuffix):
            continue

        prefix = filename[: -len(packsuffix)]

        # Don't process a pack if it doesn't have an index.
        if (prefix + indexsuffix) not in fileset:
            continue
        result.append((prefix, mode, stat))

    return result


def _computeincrementalpack(files, opts):
    """Given a set of pack files along with the configuration options, this
    function computes the list of files that should be packed as part of an
    incremental repack.

    It tries to strike a balance between keeping incremental repacks cheap (i.e.
    packing small things when possible, and rolling the packs up to the big ones
    over time).
    """

    limits = list(
        sorted((util.sizetoint(s) for s in opts[b'generations']), reverse=True)
    )
    limits.append(0)

    # Group the packs by generation (i.e. by size)
    generations = []
    for i in range(len(limits)):
        generations.append([])

    sizes = {}
    for prefix, mode, stat in files:
        size = stat.st_size
        if size > opts[b'repackmaxpacksize']:
            continue

        sizes[prefix] = size
        for i, limit in enumerate(limits):
            if size > limit:
                generations[i].append(prefix)
                break

    # Steps for picking what packs to repack:
    # 1. Pick the largest generation with > gencountlimit pack files.
    # 2. Take the smallest three packs.
    # 3. While total-size-of-packs < repacksizelimit: add another pack

    # Find the largest generation with more than gencountlimit packs
    genpacks = []
    for i, limit in enumerate(limits):
        if len(generations[i]) > opts[b'gencountlimit']:
            # Sort to be smallest last, for easy popping later
            genpacks.extend(
                sorted(generations[i], reverse=True, key=lambda x: sizes[x])
            )
            break

    # Take as many packs from the generation as we can
    chosenpacks = genpacks[-3:]
    genpacks = genpacks[:-3]
    repacksize = sum(sizes[n] for n in chosenpacks)
    while (
        repacksize < opts[b'repacksizelimit']
        and genpacks
        and len(chosenpacks) < opts[b'maxrepackpacks']
    ):
        chosenpacks.append(genpacks.pop())
        repacksize += sizes[chosenpacks[-1]]

    return chosenpacks


def _runrepack(
    repo, data, history, packpath, category, fullhistory=None, options=None
):
    shallowutil.mkstickygroupdir(repo.ui, packpath)

    def isold(repo, filename, node):
        """Check if the file node is older than a limit.
        Unless a limit is specified in the config the default limit is taken.
        """
        filectx = repo.filectx(filename, fileid=node)
        filetime = repo[filectx.linkrev()].date()

        ttl = repo.ui.configint(b'remotefilelog', b'nodettl')

        limit = time.time() - ttl
        return filetime[0] < limit

    garbagecollect = repo.ui.configbool(b'remotefilelog', b'gcrepack')
    if not fullhistory:
        fullhistory = history
    packer = repacker(
        repo,
        data,
        history,
        fullhistory,
        category,
        gc=garbagecollect,
        isold=isold,
        options=options,
    )

    with datapack.mutabledatapack(repo.ui, packpath) as dpack:
        with historypack.mutablehistorypack(repo.ui, packpath) as hpack:
            try:
                packer.run(dpack, hpack)
            except error.LockHeld:
                raise RepackAlreadyRunning(
                    _(
                        b"skipping repack - another repack "
                        b"is already running"
                    )
                )


def keepset(repo, keyfn, lastkeepkeys=None):
    """Computes a keepset which is not garbage collected.
    'keyfn' is a function that maps filename, node to a unique key.
    'lastkeepkeys' is an optional argument and if provided the keepset
    function updates lastkeepkeys with more keys and returns the result.
    """
    if not lastkeepkeys:
        keepkeys = set()
    else:
        keepkeys = lastkeepkeys

    # We want to keep:
    # 1. Working copy parent
    # 2. Draft commits
    # 3. Parents of draft commits
    # 4. Pullprefetch and bgprefetchrevs revsets if specified
    revs = [b'.', b'draft()', b'parents(draft())']
    prefetchrevs = repo.ui.config(b'remotefilelog', b'pullprefetch', None)
    if prefetchrevs:
        revs.append(b'(%s)' % prefetchrevs)
    prefetchrevs = repo.ui.config(b'remotefilelog', b'bgprefetchrevs', None)
    if prefetchrevs:
        revs.append(b'(%s)' % prefetchrevs)
    revs = b'+'.join(revs)

    revs = [b'sort((%s), "topo")' % revs]
    keep = scmutil.revrange(repo, revs)

    processed = set()
    lastmanifest = None

    # process the commits in toposorted order starting from the oldest
    for r in reversed(keep._list):
        delta_from, m = repo[r].manifestctx().read_any_fast_delta(processed)
        if delta_from is None and lastmanifest is not None:
            # could not find a delta, compute one.
            # XXX (is this really faster?)
            full = m
            if lastmanifest:
                m = m.diff(lastmanifest)
            lastmanifest = full
        processed.add(r)

        # populate keepkeys with keys from the current manifest
        if type(m) is dict:
            # m is a result of diff of two manifests and is a dictionary that
            # maps filename to ((newnode, newflag), (oldnode, oldflag)) tuple
            for filename, diff in m.items():
                if diff[0][0] is not None:
                    keepkeys.add(keyfn(filename, diff[0][0]))
        else:
            # m is a manifest object
            for filename, filenode in m.items():
                keepkeys.add(keyfn(filename, filenode))

    return keepkeys


class repacker:
    """Class for orchestrating the repack of data and history information into a
    new format.
    """

    def __init__(
        self,
        repo,
        data,
        history,
        fullhistory,
        category,
        gc=False,
        isold=None,
        options=None,
    ):
        self.repo = repo
        self.data = data
        self.history = history
        self.fullhistory = fullhistory
        self.unit = constants.getunits(category)
        self.garbagecollect = gc
        self.options = options
        if self.garbagecollect:
            if not isold:
                raise ValueError(b"Function 'isold' is not properly specified")
            # use (filename, node) tuple as a keepset key
            self.keepkeys = keepset(repo, lambda f, n: (f, n))
            self.isold = isold

    def run(self, targetdata, targethistory):
        ledger = repackledger()

        with lockmod.lock(
            repacklockvfs(self.repo), b"repacklock", desc=None, timeout=0
        ):
            self.repo.hook(b'prerepack')

            # Populate ledger from source
            self.data.markledger(ledger, options=self.options)
            self.history.markledger(ledger, options=self.options)

            # Run repack
            self.repackdata(ledger, targetdata)
            self.repackhistory(ledger, targethistory)

            # Call cleanup on each source
            for source in ledger.sources:
                source.cleanup(ledger)

    def _chainorphans(self, ui, filename, nodes, orphans, deltabases):
        """Reorderes ``orphans`` into a single chain inside ``nodes`` and
        ``deltabases``.

        We often have orphan entries (nodes without a base that aren't
        referenced by other nodes -- i.e., part of a chain) due to gaps in
        history. Rather than store them as individual fulltexts, we prefer to
        insert them as one chain sorted by size.
        """
        if not orphans:
            return nodes

        def getsize(node, default=0):
            meta = self.data.getmeta(filename, node)
            if constants.METAKEYSIZE in meta:
                return meta[constants.METAKEYSIZE]
            else:
                return default

        # Sort orphans by size; biggest first is preferred, since it's more
        # likely to be the newest version assuming files grow over time.
        # (Sort by node first to ensure the sort is stable.)
        orphans = sorted(orphans)
        orphans = list(sorted(orphans, key=getsize, reverse=True))
        if ui.debugflag:
            ui.debug(
                b"%s: orphan chain: %s\n"
                % (filename, b", ".join([short(s) for s in orphans]))
            )

        # Create one contiguous chain and reassign deltabases.
        for i, node in enumerate(orphans):
            if i == 0:
                deltabases[node] = (self.repo.nullid, 0)
            else:
                parent = orphans[i - 1]
                deltabases[node] = (parent, deltabases[parent][1] + 1)
        nodes = [n for n in nodes if n not in orphans]
        nodes += orphans
        return nodes

    def repackdata(self, ledger, target):
        ui = self.repo.ui
        maxchainlen = ui.configint(b'packs', b'maxchainlen', 1000)

        byfile = {}
        for entry in ledger.entries.values():
            if entry.datasource:
                byfile.setdefault(entry.filename, {})[entry.node] = entry

        count = 0
        repackprogress = ui.makeprogress(
            _(b"repacking data"), unit=self.unit, total=len(byfile)
        )
        for filename, entries in sorted(byfile.items()):
            repackprogress.update(count)

            ancestors = {}
            nodes = list(node for node in entries)
            nohistory = []
            buildprogress = ui.makeprogress(
                _(b"building history"), unit=b'nodes', total=len(nodes)
            )
            for i, node in enumerate(nodes):
                if node in ancestors:
                    continue
                buildprogress.update(i)
                try:
                    ancestors.update(
                        self.fullhistory.getancestors(
                            filename, node, known=ancestors
                        )
                    )
                except KeyError:
                    # Since we're packing data entries, we may not have the
                    # corresponding history entries for them. It's not a big
                    # deal, but the entries won't be delta'd perfectly.
                    nohistory.append(node)
            buildprogress.complete()

            # Order the nodes children first, so we can produce reverse deltas
            orderednodes = list(reversed(self._toposort(ancestors)))
            if len(nohistory) > 0:
                ui.debug(
                    b'repackdata: %d nodes without history\n' % len(nohistory)
                )
            orderednodes.extend(sorted(nohistory))

            # Filter orderednodes to just the nodes we want to serialize (it
            # currently also has the edge nodes' ancestors).
            orderednodes = list(
                filter(lambda node: node in nodes, orderednodes)
            )

            # Garbage collect old nodes:
            if self.garbagecollect:
                neworderednodes = []
                for node in orderednodes:
                    # If the node is old and is not in the keepset, we skip it,
                    # and mark as garbage collected
                    if (filename, node) not in self.keepkeys and self.isold(
                        self.repo, filename, node
                    ):
                        entries[node].gced = True
                        continue
                    neworderednodes.append(node)
                orderednodes = neworderednodes

            # Compute delta bases for nodes:
            deltabases = {}
            nobase = set()
            referenced = set()
            nodes = set(nodes)
            processprogress = ui.makeprogress(
                _(b"processing nodes"), unit=b'nodes', total=len(orderednodes)
            )
            for i, node in enumerate(orderednodes):
                processprogress.update(i)
                # Find delta base
                # TODO: allow delta'ing against most recent descendant instead
                # of immediate child
                deltatuple = deltabases.get(node, None)
                if deltatuple is None:
                    deltabase, chainlen = self.repo.nullid, 0
                    deltabases[node] = (self.repo.nullid, 0)
                    nobase.add(node)
                else:
                    deltabase, chainlen = deltatuple
                    referenced.add(deltabase)

                # Use available ancestor information to inform our delta choices
                ancestorinfo = ancestors.get(node)
                if ancestorinfo:
                    p1, p2, linknode, copyfrom = ancestorinfo

                    # The presence of copyfrom means we're at a point where the
                    # file was copied from elsewhere. So don't attempt to do any
                    # deltas with the other file.
                    if copyfrom:
                        p1 = self.repo.nullid

                    if chainlen < maxchainlen:
                        # Record this child as the delta base for its parents.
                        # This may be non optimal, since the parents may have
                        # many children, and this will only choose the last one.
                        # TODO: record all children and try all deltas to find
                        # best
                        if p1 != self.repo.nullid:
                            deltabases[p1] = (node, chainlen + 1)
                        if p2 != self.repo.nullid:
                            deltabases[p2] = (node, chainlen + 1)

            # experimental config: repack.chainorphansbysize
            if ui.configbool(b'repack', b'chainorphansbysize'):
                orphans = nobase - referenced
                orderednodes = self._chainorphans(
                    ui, filename, orderednodes, orphans, deltabases
                )

            # Compute deltas and write to the pack
            for i, node in enumerate(orderednodes):
                deltabase, chainlen = deltabases[node]
                # Compute delta
                # TODO: Optimize the deltachain fetching. Since we're
                # iterating over the different version of the file, we may
                # be fetching the same deltachain over and over again.
                if deltabase != self.repo.nullid:
                    deltaentry = self.data.getdelta(filename, node)
                    delta, deltabasename, origdeltabase, meta = deltaentry
                    size = meta.get(constants.METAKEYSIZE)
                    if (
                        deltabasename != filename
                        or origdeltabase != deltabase
                        or size is None
                    ):
                        deltabasetext = self.data.get(filename, deltabase)
                        original = self.data.get(filename, node)
                        size = len(original)
                        delta = mdiff.textdiff(deltabasetext, original)
                else:
                    delta = self.data.get(filename, node)
                    size = len(delta)
                    meta = self.data.getmeta(filename, node)

                # TODO: don't use the delta if it's larger than the fulltext
                if constants.METAKEYSIZE not in meta:
                    meta[constants.METAKEYSIZE] = size
                target.add(filename, node, deltabase, delta, meta)

                entries[node].datarepacked = True

            processprogress.complete()
            count += 1

        repackprogress.complete()
        target.close(ledger=ledger)

    def repackhistory(self, ledger, target):
        ui = self.repo.ui

        byfile = {}
        for entry in ledger.entries.values():
            if entry.historysource:
                byfile.setdefault(entry.filename, {})[entry.node] = entry

        progress = ui.makeprogress(
            _(b"repacking history"), unit=self.unit, total=len(byfile)
        )
        for filename, entries in sorted(byfile.items()):
            ancestors = {}
            nodes = list(node for node in entries)

            for node in nodes:
                if node in ancestors:
                    continue
                ancestors.update(
                    self.history.getancestors(filename, node, known=ancestors)
                )

            # Order the nodes children first
            orderednodes = reversed(self._toposort(ancestors))

            # Write to the pack
            dontprocess = set()
            for node in orderednodes:
                p1, p2, linknode, copyfrom = ancestors[node]

                # If the node is marked dontprocess, but it's also in the
                # explicit entries set, that means the node exists both in this
                # file and in another file that was copied to this file.
                # Usually this happens if the file was copied to another file,
                # then the copy was deleted, then reintroduced without copy
                # metadata. The original add and the new add have the same hash
                # since the content is identical and the parents are null.
                if node in dontprocess and node not in entries:
                    # If copyfrom == filename, it means the copy history
                    # went to come other file, then came back to this one, so we
                    # should continue processing it.
                    if p1 != self.repo.nullid and copyfrom != filename:
                        dontprocess.add(p1)
                    if p2 != self.repo.nullid:
                        dontprocess.add(p2)
                    continue

                if copyfrom:
                    dontprocess.add(p1)

                target.add(filename, node, p1, p2, linknode, copyfrom)

                if node in entries:
                    entries[node].historyrepacked = True

            progress.increment()

        progress.complete()
        target.close(ledger=ledger)

    def _toposort(self, ancestors):
        def parentfunc(node):
            p1, p2, linknode, copyfrom = ancestors[node]
            parents = []
            if p1 != self.repo.nullid:
                parents.append(p1)
            if p2 != self.repo.nullid:
                parents.append(p2)
            return parents

        sortednodes = shallowutil.sortnodes(ancestors.keys(), parentfunc)
        return sortednodes


class repackledger:
    """Storage for all the bookkeeping that happens during a repack. It contains
    the list of revisions being repacked, what happened to each revision, and
    which source store contained which revision originally (for later cleanup).
    """

    def __init__(self):
        self.entries = {}
        self.sources = {}
        self.created = set()

    def markdataentry(self, source, filename, node):
        """Mark the given filename+node revision as having a data rev in the
        given source.
        """
        entry = self._getorcreateentry(filename, node)
        entry.datasource = True
        entries = self.sources.get(source)
        if not entries:
            entries = set()
            self.sources[source] = entries
        entries.add(entry)

    def markhistoryentry(self, source, filename, node):
        """Mark the given filename+node revision as having a history rev in the
        given source.
        """
        entry = self._getorcreateentry(filename, node)
        entry.historysource = True
        entries = self.sources.get(source)
        if not entries:
            entries = set()
            self.sources[source] = entries
        entries.add(entry)

    def _getorcreateentry(self, filename, node):
        key = (filename, node)
        value = self.entries.get(key)
        if not value:
            value = repackentry(filename, node)
            self.entries[key] = value

        return value

    def addcreated(self, value):
        self.created.add(value)


class repackentry:
    """Simple class representing a single revision entry in the repackledger."""

    __slots__ = (
        'filename',
        'node',
        'datasource',
        'historysource',
        'datarepacked',
        'historyrepacked',
        'gced',
    )

    def __init__(self, filename, node):
        self.filename = filename
        self.node = node
        # If the revision has a data entry in the source
        self.datasource = False
        # If the revision has a history entry in the source
        self.historysource = False
        # If the revision's data entry was repacked into the repack target
        self.datarepacked = False
        # If the revision's history entry was repacked into the repack target
        self.historyrepacked = False
        # If garbage collected
        self.gced = False


def repacklockvfs(repo):
    if hasattr(repo, 'name'):
        # Lock in the shared cache so repacks across multiple copies of the same
        # repo are coordinated.
        sharedcachepath = shallowutil.getcachepackpath(
            repo, constants.FILEPACK_CATEGORY
        )
        return vfs.vfs(sharedcachepath)
    else:
        return repo.svfs