contrib/shrink-revlog.py
author Dirkjan Ochtman <dirkjan@ochtman.nl>
Thu, 07 Jan 2010 15:07:18 +0100
changeset 10213 9e6848f352b0
parent 10009 69dca8574a6a
child 10214 8111f9988c9d
permissions -rwxr-xr-x
contrib: use ui to write in shrink-revlog.py
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
9515
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
     1
#!/usr/bin/env python
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
     2
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
     3
"""\
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
     4
Reorder a revlog (by default the the manifest file in the current
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
     5
repository) to save space.  Specifically, this topologically sorts the
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
     6
revisions in the revlog so that revisions on the same branch are adjacent
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
     7
as much as possible.  This is a workaround for the fact that Mercurial
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
     8
computes deltas relative to the previous revision rather than relative to a
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
     9
parent revision.  This is *not* safe to run on a changelog.
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    10
"""
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    11
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    12
# Originally written by Benoit Boissinot <benoit.boissinot at ens-lyon.org>
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    13
# as a patch to rewrite-log.  Cleaned up, refactored, documented, and
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    14
# renamed by Greg Ward <greg at gerg.ca>.
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    15
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    16
# XXX would be nice to have a way to verify the repository after shrinking,
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    17
# e.g. by comparing "before" and "after" states of random changesets
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    18
# (maybe: export before, shrink, export after, diff).
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    19
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    20
import sys, os, tempfile
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    21
import optparse
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    22
from mercurial import ui as ui_, hg, revlog, transaction, node, util
10009
69dca8574a6a shrink-revlog: improve performance: use changegroup instead of revisions
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 9712
diff changeset
    23
from mercurial import changegroup
9515
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    24
10213
9e6848f352b0 contrib: use ui to write in shrink-revlog.py
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10009
diff changeset
    25
def toposort(ui, rl):
9515
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    26
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    27
    children = {}
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    28
    root = []
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    29
    # build children and roots
10213
9e6848f352b0 contrib: use ui to write in shrink-revlog.py
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10009
diff changeset
    30
    ui.write('reading %d revs ' % len(rl))
9515
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    31
    try:
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    32
        for i in rl:
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    33
            children[i] = []
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    34
            parents = [p for p in rl.parentrevs(i) if p != node.nullrev]
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    35
            # in case of duplicate parents
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    36
            if len(parents) == 2 and parents[0] == parents[1]:
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    37
                del parents[1]
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    38
            for p in parents:
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    39
                assert p in children
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    40
                children[p].append(i)
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    41
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    42
            if len(parents) == 0:
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    43
                root.append(i)
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    44
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    45
            if i % 1000 == 0:
10213
9e6848f352b0 contrib: use ui to write in shrink-revlog.py
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10009
diff changeset
    46
                ui.write('.')
9515
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    47
    finally:
10213
9e6848f352b0 contrib: use ui to write in shrink-revlog.py
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10009
diff changeset
    48
        ui.write('\n')
9515
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    49
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    50
    # XXX this is a reimplementation of the 'branchsort' topo sort
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    51
    # algorithm in hgext.convert.convcmd... would be nice not to duplicate
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    52
    # the algorithm
10213
9e6848f352b0 contrib: use ui to write in shrink-revlog.py
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10009
diff changeset
    53
    ui.write('sorting ...')
9515
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    54
    visit = root
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    55
    ret = []
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    56
    while visit:
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    57
        i = visit.pop(0)
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    58
        ret.append(i)
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    59
        if i not in children:
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    60
            # This only happens if some node's p1 == p2, which can
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    61
            # happen in the manifest in certain circumstances.
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    62
            continue
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    63
        next = []
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    64
        for c in children.pop(i):
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    65
            parents_unseen = [p for p in rl.parentrevs(c)
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    66
                              if p != node.nullrev and p in children]
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    67
            if len(parents_unseen) == 0:
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    68
                next.append(c)
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    69
        visit = next + visit
10213
9e6848f352b0 contrib: use ui to write in shrink-revlog.py
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10009
diff changeset
    70
    ui.write('\n')
9515
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    71
    return ret
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    72
10213
9e6848f352b0 contrib: use ui to write in shrink-revlog.py
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10009
diff changeset
    73
def writerevs(ui, r1, r2, order, tr):
10009
69dca8574a6a shrink-revlog: improve performance: use changegroup instead of revisions
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 9712
diff changeset
    74
10213
9e6848f352b0 contrib: use ui to write in shrink-revlog.py
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10009
diff changeset
    75
    ui.write('writing %d revs ' % len(order))
10009
69dca8574a6a shrink-revlog: improve performance: use changegroup instead of revisions
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 9712
diff changeset
    76
    count = [0]
69dca8574a6a shrink-revlog: improve performance: use changegroup instead of revisions
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 9712
diff changeset
    77
    def progress(*args):
69dca8574a6a shrink-revlog: improve performance: use changegroup instead of revisions
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 9712
diff changeset
    78
        if count[0] % 1000 == 0:
10213
9e6848f352b0 contrib: use ui to write in shrink-revlog.py
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10009
diff changeset
    79
            ui.write('.')
10009
69dca8574a6a shrink-revlog: improve performance: use changegroup instead of revisions
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 9712
diff changeset
    80
        count[0] += 1
69dca8574a6a shrink-revlog: improve performance: use changegroup instead of revisions
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 9712
diff changeset
    81
69dca8574a6a shrink-revlog: improve performance: use changegroup instead of revisions
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 9712
diff changeset
    82
    order = [r1.node(r) for r in order]
69dca8574a6a shrink-revlog: improve performance: use changegroup instead of revisions
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 9712
diff changeset
    83
69dca8574a6a shrink-revlog: improve performance: use changegroup instead of revisions
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 9712
diff changeset
    84
    # this is a bit ugly, but it works
69dca8574a6a shrink-revlog: improve performance: use changegroup instead of revisions
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 9712
diff changeset
    85
    lookup = lambda x: "%020d" % r1.linkrev(r1.rev(x))
69dca8574a6a shrink-revlog: improve performance: use changegroup instead of revisions
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 9712
diff changeset
    86
    unlookup = lambda x: int(x, 10)
69dca8574a6a shrink-revlog: improve performance: use changegroup instead of revisions
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 9712
diff changeset
    87
9515
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    88
    try:
10009
69dca8574a6a shrink-revlog: improve performance: use changegroup instead of revisions
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 9712
diff changeset
    89
        group = util.chunkbuffer(r1.group(order, lookup, progress))
69dca8574a6a shrink-revlog: improve performance: use changegroup instead of revisions
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 9712
diff changeset
    90
        chunkiter = changegroup.chunkiter(group)
69dca8574a6a shrink-revlog: improve performance: use changegroup instead of revisions
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 9712
diff changeset
    91
        r2.addgroup(chunkiter, unlookup, tr)
9515
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    92
    finally:
10213
9e6848f352b0 contrib: use ui to write in shrink-revlog.py
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10009
diff changeset
    93
        ui.write('\n')
9712
18b134ef294c kill trailing whitespace
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 9515
diff changeset
    94
10213
9e6848f352b0 contrib: use ui to write in shrink-revlog.py
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10009
diff changeset
    95
def report(ui, olddatafn, newdatafn):
9515
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    96
    oldsize = float(os.stat(olddatafn).st_size)
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    97
    newsize = float(os.stat(newdatafn).st_size)
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    98
9712
18b134ef294c kill trailing whitespace
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 9515
diff changeset
    99
    # argh: have to pass an int to %d, because a float >= 2^32
9515
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   100
    # blows up under Python 2.5 or earlier
10213
9e6848f352b0 contrib: use ui to write in shrink-revlog.py
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10009
diff changeset
   101
    ui.write('old file size: %12d bytes (%6.1f MiB)\n'
9e6848f352b0 contrib: use ui to write in shrink-revlog.py
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10009
diff changeset
   102
             % (int(oldsize), oldsize/1024/1024))
9e6848f352b0 contrib: use ui to write in shrink-revlog.py
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10009
diff changeset
   103
    ui.write('new file size: %12d bytes (%6.1f MiB)\n'
9e6848f352b0 contrib: use ui to write in shrink-revlog.py
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10009
diff changeset
   104
             % (int(newsize), newsize/1024/1024))
9515
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   105
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   106
    shrink_percent = (oldsize - newsize) / oldsize * 100
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   107
    shrink_factor = oldsize / newsize
10213
9e6848f352b0 contrib: use ui to write in shrink-revlog.py
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10009
diff changeset
   108
    ui.write('shrinkage: %.1f%% (%.1fx)\n' % (shrink_percent, shrink_factor))
9515
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   109
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   110
def main():
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   111
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   112
    # Unbuffer stdout for nice progress output.
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   113
    sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   114
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   115
    parser = optparse.OptionParser(description=__doc__)
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   116
    parser.add_option('-R', '--repository',
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   117
                      default=os.path.curdir,
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   118
                      metavar='REPO',
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   119
                      help='repository root directory [default: current dir]')
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   120
    parser.add_option('--revlog',
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   121
                      metavar='FILE',
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   122
                      help='shrink FILE [default: REPO/hg/store/00manifest.i]')
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   123
    (options, args) = parser.parse_args()
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   124
    if args:
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   125
        parser.error('too many arguments')
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   126
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   127
    # Open the specified repository.
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   128
    ui = ui_.ui()
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   129
    repo = hg.repository(ui, options.repository)
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   130
    if not repo.local():
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   131
        parser.error('not a local repository: %s' % options.repository)
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   132
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   133
    if options.revlog is None:
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   134
        indexfn = repo.sjoin('00manifest.i')
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   135
    else:
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   136
        if not options.revlog.endswith('.i'):
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   137
            parser.error('--revlog option must specify the revlog index file '
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   138
                         '(*.i), not %s' % options.revlog)
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   139
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   140
        indexfn = os.path.realpath(options.revlog)
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   141
        store = repo.sjoin('')
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   142
        if not indexfn.startswith(store):
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   143
            parser.error('--revlog option must specify a revlog in %s, not %s'
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   144
                         % (store, indexfn))
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   145
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   146
    datafn = indexfn[:-2] + '.d'
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   147
    if not os.path.exists(indexfn):
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   148
        parser.error('no such file: %s' % indexfn)
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   149
    if '00changelog' in indexfn:
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   150
        parser.error('shrinking the changelog will corrupt your repository')
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   151
    if not os.path.exists(datafn):
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   152
        # This is just a lazy shortcut because I can't be bothered to
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   153
        # handle all the special cases that entail from no .d file.
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   154
        parser.error('%s does not exist: revlog not big enough '
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   155
                     'to be worth shrinking' % datafn)
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   156
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   157
    oldindexfn = indexfn + '.old'
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   158
    olddatafn = datafn + '.old'
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   159
    if os.path.exists(oldindexfn) or os.path.exists(olddatafn):
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   160
        parser.error('one or both of\n'
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   161
                     '  %s\n'
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   162
                     '  %s\n'
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   163
                     'exists from a previous run; please clean up before '
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   164
                     'running again'
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   165
                     % (oldindexfn, olddatafn))
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   166
10213
9e6848f352b0 contrib: use ui to write in shrink-revlog.py
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10009
diff changeset
   167
    ui.write('shrinking %s\n' % indexfn)
9515
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   168
    prefix = os.path.basename(indexfn)[:-1]
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   169
    (tmpfd, tmpindexfn) = tempfile.mkstemp(dir=os.path.dirname(indexfn),
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   170
                                           prefix=prefix,
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   171
                                           suffix='.i')
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   172
    tmpdatafn = tmpindexfn[:-2] + '.d'
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   173
    os.close(tmpfd)
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   174
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   175
    r1 = revlog.revlog(util.opener(os.getcwd(), audit=False), indexfn)
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   176
    r2 = revlog.revlog(util.opener(os.getcwd(), audit=False), tmpindexfn)
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   177
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   178
    # Don't use repo.transaction(), because then things get hairy with
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   179
    # paths: some need to be relative to .hg, and some need to be
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   180
    # absolute.  Doing it this way keeps things simple: everything is an
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   181
    # absolute path.
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   182
    lock = repo.lock(wait=False)
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   183
    tr = transaction.transaction(sys.stderr.write,
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   184
                                 open,
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   185
                                 repo.sjoin('journal'))
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   186
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   187
    try:
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   188
        try:
10213
9e6848f352b0 contrib: use ui to write in shrink-revlog.py
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10009
diff changeset
   189
            order = toposort(ui, r1)
9e6848f352b0 contrib: use ui to write in shrink-revlog.py
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10009
diff changeset
   190
            writerevs(ui, r1, r2, order, tr)
9e6848f352b0 contrib: use ui to write in shrink-revlog.py
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10009
diff changeset
   191
            report(ui, datafn, tmpdatafn)
9515
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   192
            tr.close()
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   193
        except:
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   194
            # Abort transaction first, so we truncate the files before
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   195
            # deleting them.
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   196
            tr.abort()
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   197
            if os.path.exists(tmpindexfn):
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   198
                os.unlink(tmpindexfn)
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   199
            if os.path.exists(tmpdatafn):
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   200
                os.unlink(tmpdatafn)
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   201
            raise
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   202
    finally:
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   203
        lock.release()
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   204
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   205
    os.link(indexfn, oldindexfn)
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   206
    os.link(datafn, olddatafn)
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   207
    os.rename(tmpindexfn, indexfn)
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   208
    os.rename(tmpdatafn, datafn)
10213
9e6848f352b0 contrib: use ui to write in shrink-revlog.py
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10009
diff changeset
   209
    ui.write('note: old revlog saved in:\n'
9515
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   210
          '  %s\n'
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   211
          '  %s\n'
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   212
          '(You can delete those files when you are satisfied that your\n'
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   213
          'repository is still sane.  '
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   214
          'Running \'hg verify\' is strongly recommended.)\n'
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   215
          % (oldindexfn, olddatafn))
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   216
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   217
try:
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   218
    main()
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   219
except KeyboardInterrupt:
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   220
    sys.exit("interrupted")