contrib/shrink-revlog.py
author Brendan Cully <brendan@kublai.com>
Sun, 07 Feb 2010 18:06:52 +0100
changeset 10384 832f35386067
parent 10282 08a0f04b56bd
child 10440 b39b32c33269
permissions -rwxr-xr-x
import: import each patch in a file or stream as a separate change Supports hg export <revrange>, mail messages, and mailboxes. Does not support multiple patches in a single MIME attachment. Closes issue167.
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
"""\
10236
49a8625b8cac shrink-revlog: help/doc tweaks
Greg Ward <greg-hg@gerg.ca>
parents: 10234
diff changeset
     4
reorder a revlog (the manifest by default) to save space
49a8625b8cac shrink-revlog: help/doc tweaks
Greg Ward <greg-hg@gerg.ca>
parents: 10234
diff changeset
     5
49a8625b8cac shrink-revlog: help/doc tweaks
Greg Ward <greg-hg@gerg.ca>
parents: 10234
diff changeset
     6
Specifically, this topologically sorts the revisions in the revlog so that
49a8625b8cac shrink-revlog: help/doc tweaks
Greg Ward <greg-hg@gerg.ca>
parents: 10234
diff changeset
     7
revisions on the same branch are adjacent as much as possible. This is a
49a8625b8cac shrink-revlog: help/doc tweaks
Greg Ward <greg-hg@gerg.ca>
parents: 10234
diff changeset
     8
workaround for the fact that Mercurial computes deltas relative to the
10216
843f6ee6d14b contrib: small documentation fixes in shrink-revlog.py
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10215
diff changeset
     9
previous revision rather than relative to a parent revision.
843f6ee6d14b contrib: small documentation fixes in shrink-revlog.py
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10215
diff changeset
    10
843f6ee6d14b contrib: small documentation fixes in shrink-revlog.py
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10215
diff changeset
    11
This is *not* safe to run on a changelog.
9515
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    12
"""
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    13
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    14
# Originally written by Benoit Boissinot <benoit.boissinot at ens-lyon.org>
10216
843f6ee6d14b contrib: small documentation fixes in shrink-revlog.py
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10215
diff changeset
    15
# as a patch to rewrite-log. Cleaned up, refactored, documented, and
9515
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    16
# 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
    17
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    18
# 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
    19
# 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
    20
# (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
    21
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    22
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
    23
import optparse
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    24
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
    25
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
    26
10213
9e6848f352b0 contrib: use ui to write in shrink-revlog.py
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10009
diff changeset
    27
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
    28
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    29
    children = {}
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    30
    root = []
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    31
    # build children and roots
10213
9e6848f352b0 contrib: use ui to write in shrink-revlog.py
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10009
diff changeset
    32
    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
    33
    try:
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    34
        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
    35
            children[i] = []
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    36
            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
    37
            # 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
    38
            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
    39
                del parents[1]
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    40
            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
    41
                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
    42
                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
    43
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    44
            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
    45
                root.append(i)
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    46
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    47
            if i % 1000 == 0:
10213
9e6848f352b0 contrib: use ui to write in shrink-revlog.py
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10009
diff changeset
    48
                ui.write('.')
9515
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    49
    finally:
10213
9e6848f352b0 contrib: use ui to write in shrink-revlog.py
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10009
diff changeset
    50
        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
    51
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    52
    # 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
    53
    # 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
    54
    # the algorithm
10213
9e6848f352b0 contrib: use ui to write in shrink-revlog.py
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10009
diff changeset
    55
    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
    56
    visit = root
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    57
    ret = []
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    58
    while visit:
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    59
        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
    60
        ret.append(i)
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    61
        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
    62
            # 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
    63
            # 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
    64
            continue
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    65
        next = []
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    66
        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
    67
            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
    68
                              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
    69
            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
    70
                next.append(c)
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    71
        visit = next + visit
10213
9e6848f352b0 contrib: use ui to write in shrink-revlog.py
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10009
diff changeset
    72
    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
    73
    return ret
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    74
10213
9e6848f352b0 contrib: use ui to write in shrink-revlog.py
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10009
diff changeset
    75
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
    76
10213
9e6848f352b0 contrib: use ui to write in shrink-revlog.py
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10009
diff changeset
    77
    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
    78
    count = [0]
69dca8574a6a shrink-revlog: improve performance: use changegroup instead of revisions
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 9712
diff changeset
    79
    def progress(*args):
69dca8574a6a shrink-revlog: improve performance: use changegroup instead of revisions
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 9712
diff changeset
    80
        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
    81
            ui.write('.')
10009
69dca8574a6a shrink-revlog: improve performance: use changegroup instead of revisions
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 9712
diff changeset
    82
        count[0] += 1
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
    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
    85
69dca8574a6a shrink-revlog: improve performance: use changegroup instead of revisions
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 9712
diff changeset
    86
    # 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
    87
    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
    88
    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
    89
9515
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    90
    try:
10009
69dca8574a6a shrink-revlog: improve performance: use changegroup instead of revisions
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 9712
diff changeset
    91
        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
    92
        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
    93
        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
    94
    finally:
10213
9e6848f352b0 contrib: use ui to write in shrink-revlog.py
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10009
diff changeset
    95
        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
    96
10213
9e6848f352b0 contrib: use ui to write in shrink-revlog.py
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10009
diff changeset
    97
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
    98
    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
    99
    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
   100
9712
18b134ef294c kill trailing whitespace
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 9515
diff changeset
   101
    # 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
   102
    # 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
   103
    ui.write('old file size: %12d bytes (%6.1f MiB)\n'
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10241
diff changeset
   104
             % (int(oldsize), oldsize / 1024 / 1024))
10213
9e6848f352b0 contrib: use ui to write in shrink-revlog.py
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10009
diff changeset
   105
    ui.write('new file size: %12d bytes (%6.1f MiB)\n'
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10241
diff changeset
   106
             % (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
   107
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   108
    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
   109
    shrink_factor = oldsize / newsize
10213
9e6848f352b0 contrib: use ui to write in shrink-revlog.py
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10009
diff changeset
   110
    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
   111
10215
9d79b8f58bea contrib: turn shrink-revlog.py into an extension
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10214
diff changeset
   112
def shrink(ui, repo, **opts):
9d79b8f58bea contrib: turn shrink-revlog.py into an extension
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10214
diff changeset
   113
    """
9d79b8f58bea contrib: turn shrink-revlog.py into an extension
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10214
diff changeset
   114
    Shrink revlog by re-ordering revisions. Will operate on manifest for
9d79b8f58bea contrib: turn shrink-revlog.py into an extension
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10214
diff changeset
   115
    the given repository if no other revlog is specified."""
9515
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   116
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   117
    # 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
   118
    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
   119
10215
9d79b8f58bea contrib: turn shrink-revlog.py into an extension
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10214
diff changeset
   120
    if not repo.local():
9d79b8f58bea contrib: turn shrink-revlog.py into an extension
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10214
diff changeset
   121
        raise util.Abort('not a local repository: %s' % repo.root)
9515
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   122
10215
9d79b8f58bea contrib: turn shrink-revlog.py into an extension
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10214
diff changeset
   123
    fn = opts.get('revlog')
9d79b8f58bea contrib: turn shrink-revlog.py into an extension
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10214
diff changeset
   124
    if not fn:
9515
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   125
        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
   126
    else:
10215
9d79b8f58bea contrib: turn shrink-revlog.py into an extension
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10214
diff changeset
   127
        if not fn.endswith('.i'):
10214
8111f9988c9d contrib: fix error handling in shrink-revlog.py to be more hg-like
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10213
diff changeset
   128
            raise util.Abort('--revlog option must specify the revlog index '
10215
9d79b8f58bea contrib: turn shrink-revlog.py into an extension
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10214
diff changeset
   129
                             'file (*.i), not %s' % opts.get('revlog'))
9515
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   130
10215
9d79b8f58bea contrib: turn shrink-revlog.py into an extension
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10214
diff changeset
   131
        indexfn = os.path.realpath(fn)
9515
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   132
        store = repo.sjoin('')
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   133
        if not indexfn.startswith(store):
10214
8111f9988c9d contrib: fix error handling in shrink-revlog.py to be more hg-like
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10213
diff changeset
   134
            raise util.Abort('--revlog option must specify a revlog in %s, '
8111f9988c9d contrib: fix error handling in shrink-revlog.py to be more hg-like
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10213
diff changeset
   135
                             'not %s' % (store, indexfn))
9515
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   136
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   137
    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
   138
    if not os.path.exists(indexfn):
10214
8111f9988c9d contrib: fix error handling in shrink-revlog.py to be more hg-like
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10213
diff changeset
   139
        raise util.Abort('no such file: %s' % indexfn)
9515
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   140
    if '00changelog' in indexfn:
10214
8111f9988c9d contrib: fix error handling in shrink-revlog.py to be more hg-like
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10213
diff changeset
   141
        raise util.Abort('shrinking the changelog will corrupt your repository')
9515
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   142
    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
   143
        # 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
   144
        # handle all the special cases that entail from no .d file.
10214
8111f9988c9d contrib: fix error handling in shrink-revlog.py to be more hg-like
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10213
diff changeset
   145
        raise util.Abort('%s does not exist: revlog not big enough '
8111f9988c9d contrib: fix error handling in shrink-revlog.py to be more hg-like
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10213
diff changeset
   146
                         'to be worth shrinking' % datafn)
9515
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   147
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   148
    oldindexfn = indexfn + '.old'
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   149
    olddatafn = datafn + '.old'
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   150
    if os.path.exists(oldindexfn) or os.path.exists(olddatafn):
10214
8111f9988c9d contrib: fix error handling in shrink-revlog.py to be more hg-like
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10213
diff changeset
   151
        raise util.Abort('one or both of\n'
8111f9988c9d contrib: fix error handling in shrink-revlog.py to be more hg-like
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10213
diff changeset
   152
                         '  %s\n'
8111f9988c9d contrib: fix error handling in shrink-revlog.py to be more hg-like
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10213
diff changeset
   153
                         '  %s\n'
8111f9988c9d contrib: fix error handling in shrink-revlog.py to be more hg-like
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10213
diff changeset
   154
                         'exists from a previous run; please clean up before '
8111f9988c9d contrib: fix error handling in shrink-revlog.py to be more hg-like
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10213
diff changeset
   155
                         'running again' % (oldindexfn, olddatafn))
9515
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   156
10213
9e6848f352b0 contrib: use ui to write in shrink-revlog.py
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10009
diff changeset
   157
    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
   158
    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
   159
    (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
   160
                                           prefix=prefix,
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   161
                                           suffix='.i')
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   162
    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
   163
    os.close(tmpfd)
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   164
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   165
    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
   166
    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
   167
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   168
    # 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
   169
    # paths: some need to be relative to .hg, and some need to be
10216
843f6ee6d14b contrib: small documentation fixes in shrink-revlog.py
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10215
diff changeset
   170
    # absolute. Doing it this way keeps things simple: everything is an
9515
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   171
    # absolute path.
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   172
    lock = repo.lock(wait=False)
10234
c8d6f339bbd7 shrink-revlog: make it work on windows (issue1976)
Patrick Mezard <pmezard@gmail.com>
parents: 10230
diff changeset
   173
    tr = transaction.transaction(ui.warn,
9515
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   174
                                 open,
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   175
                                 repo.sjoin('journal'))
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   176
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   177
    try:
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   178
        try:
10213
9e6848f352b0 contrib: use ui to write in shrink-revlog.py
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10009
diff changeset
   179
            order = toposort(ui, r1)
9e6848f352b0 contrib: use ui to write in shrink-revlog.py
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10009
diff changeset
   180
            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
   181
            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
   182
            tr.close()
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   183
        except:
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   184
            # 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
   185
            # deleting them.
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   186
            tr.abort()
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   187
            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
   188
                os.unlink(tmpindexfn)
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   189
            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
   190
                os.unlink(tmpdatafn)
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   191
            raise
10241
4b2a086bee31 shrink-revlog: add --dry-run option
Patrick Mezard <pmezard@gmail.com>
parents: 10236
diff changeset
   192
        if not opts.get('dry_run'):
4b2a086bee31 shrink-revlog: add --dry-run option
Patrick Mezard <pmezard@gmail.com>
parents: 10236
diff changeset
   193
            # Racy since both files cannot be renamed atomically
4b2a086bee31 shrink-revlog: add --dry-run option
Patrick Mezard <pmezard@gmail.com>
parents: 10236
diff changeset
   194
            util.os_link(indexfn, oldindexfn)
4b2a086bee31 shrink-revlog: add --dry-run option
Patrick Mezard <pmezard@gmail.com>
parents: 10236
diff changeset
   195
            util.os_link(datafn, olddatafn)
4b2a086bee31 shrink-revlog: add --dry-run option
Patrick Mezard <pmezard@gmail.com>
parents: 10236
diff changeset
   196
            util.rename(tmpindexfn, indexfn)
4b2a086bee31 shrink-revlog: add --dry-run option
Patrick Mezard <pmezard@gmail.com>
parents: 10236
diff changeset
   197
            util.rename(tmpdatafn, datafn)
4b2a086bee31 shrink-revlog: add --dry-run option
Patrick Mezard <pmezard@gmail.com>
parents: 10236
diff changeset
   198
        else:
4b2a086bee31 shrink-revlog: add --dry-run option
Patrick Mezard <pmezard@gmail.com>
parents: 10236
diff changeset
   199
            os.unlink(tmpindexfn)
4b2a086bee31 shrink-revlog: add --dry-run option
Patrick Mezard <pmezard@gmail.com>
parents: 10236
diff changeset
   200
            os.unlink(tmpdatafn)
9515
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   201
    finally:
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   202
        lock.release()
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   203
10241
4b2a086bee31 shrink-revlog: add --dry-run option
Patrick Mezard <pmezard@gmail.com>
parents: 10236
diff changeset
   204
    if not opts.get('dry_run'):
4b2a086bee31 shrink-revlog: add --dry-run option
Patrick Mezard <pmezard@gmail.com>
parents: 10236
diff changeset
   205
        ui.write('note: old revlog saved in:\n'
4b2a086bee31 shrink-revlog: add --dry-run option
Patrick Mezard <pmezard@gmail.com>
parents: 10236
diff changeset
   206
                 '  %s\n'
4b2a086bee31 shrink-revlog: add --dry-run option
Patrick Mezard <pmezard@gmail.com>
parents: 10236
diff changeset
   207
                 '  %s\n'
4b2a086bee31 shrink-revlog: add --dry-run option
Patrick Mezard <pmezard@gmail.com>
parents: 10236
diff changeset
   208
                 '(You can delete those files when you are satisfied that your\n'
4b2a086bee31 shrink-revlog: add --dry-run option
Patrick Mezard <pmezard@gmail.com>
parents: 10236
diff changeset
   209
                 'repository is still sane.  '
4b2a086bee31 shrink-revlog: add --dry-run option
Patrick Mezard <pmezard@gmail.com>
parents: 10236
diff changeset
   210
                 'Running \'hg verify\' is strongly recommended.)\n'
4b2a086bee31 shrink-revlog: add --dry-run option
Patrick Mezard <pmezard@gmail.com>
parents: 10236
diff changeset
   211
                 % (oldindexfn, olddatafn))
9515
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   212
10215
9d79b8f58bea contrib: turn shrink-revlog.py into an extension
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10214
diff changeset
   213
cmdtable = {
9d79b8f58bea contrib: turn shrink-revlog.py into an extension
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10214
diff changeset
   214
    'shrink': (shrink,
10241
4b2a086bee31 shrink-revlog: add --dry-run option
Patrick Mezard <pmezard@gmail.com>
parents: 10236
diff changeset
   215
               [('', 'revlog', '', 'index (.i) file of the revlog to shrink'),
4b2a086bee31 shrink-revlog: add --dry-run option
Patrick Mezard <pmezard@gmail.com>
parents: 10236
diff changeset
   216
                ('n', 'dry-run', None, 'do not shrink, simulate only'),
4b2a086bee31 shrink-revlog: add --dry-run option
Patrick Mezard <pmezard@gmail.com>
parents: 10236
diff changeset
   217
                ],
10215
9d79b8f58bea contrib: turn shrink-revlog.py into an extension
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10214
diff changeset
   218
               'hg shrink [--revlog PATH]')
9d79b8f58bea contrib: turn shrink-revlog.py into an extension
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10214
diff changeset
   219
}
10236
49a8625b8cac shrink-revlog: help/doc tweaks
Greg Ward <greg-hg@gerg.ca>
parents: 10234
diff changeset
   220
49a8625b8cac shrink-revlog: help/doc tweaks
Greg Ward <greg-hg@gerg.ca>
parents: 10234
diff changeset
   221
if __name__ == "__main__":
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10241
diff changeset
   222
    print "shrink-revlog.py is now an extension (see hg help extensions)"