contrib/shrink-revlog.py
author Brodie Rao <brodie@sf.io>
Fri, 11 May 2012 15:45:37 +0200
branchstable
changeset 16631 369741ef7253
parent 16306 d76b9abd1509
child 16705 c2d9ef43ff6c
permissions -rw-r--r--
pager: preserve Hg's exit code (and fix Windows support) (issue3225) This changes how the pager extension invokes the pager. Prior to this change, the extension would fork Hg and exec the pager in the parent process. This loses Hg exit code, and it doesn't work on Windows. Now the pager is invoked using the subprocess library, and an atexit handler is registered that makes Hg wait for the pager to exit before it exits itself. Note that if you exit the pager before Hg is done running, you'll get an exit code of 255, which is caused by Python blowing up due to a broken pipe. If you set pager.quiet=True, you'll get the OS-level return code of 141.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14028
7e453770b364 shrink-revlog: remove \ from docstring
Augie Fackler <durin42@gmail.com>
parents: 13783
diff changeset
     1
"""reorder a revlog (the manifest by default) to save space
10236
49a8625b8cac shrink-revlog: help/doc tweaks
Greg Ward <greg-hg@gerg.ca>
parents: 10234
diff changeset
     2
49a8625b8cac shrink-revlog: help/doc tweaks
Greg Ward <greg-hg@gerg.ca>
parents: 10234
diff changeset
     3
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
     4
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
     5
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
     6
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
     7
843f6ee6d14b contrib: small documentation fixes in shrink-revlog.py
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10215
diff changeset
     8
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
     9
"""
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
# 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
    12
# 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
    13
# 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
    14
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    15
# 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
    16
# 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
    17
# (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
    18
16305
90ca62bb9e78 shrink-revlog: make pyflakes happy
Greg Ward <greg@gerg.ca>
parents: 15381
diff changeset
    19
import os, errno
14029
83d3f87c059e shrink-revlog: update util.opener to scmutil.opener after d13913355390
Augie Fackler <durin42@gmail.com>
parents: 14028
diff changeset
    20
from mercurial import revlog, transaction, node, util, scmutil
10009
69dca8574a6a shrink-revlog: improve performance: use changegroup instead of revisions
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 9712
diff changeset
    21
from mercurial import changegroup
10508
cc35ad583e66 shrink-revlog: add strings for translation / import _ before using it
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10496
diff changeset
    22
from mercurial.i18n import _
9515
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    23
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    24
10627
adcd5bcb37ab shrink-revlog: factor out postorder algorithm
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10626
diff changeset
    25
def postorder(start, edges):
adcd5bcb37ab shrink-revlog: factor out postorder algorithm
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10626
diff changeset
    26
    result = []
adcd5bcb37ab shrink-revlog: factor out postorder algorithm
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10626
diff changeset
    27
    visit = list(start)
adcd5bcb37ab shrink-revlog: factor out postorder algorithm
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10626
diff changeset
    28
    finished = set()
9515
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    29
10627
adcd5bcb37ab shrink-revlog: factor out postorder algorithm
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10626
diff changeset
    30
    while visit:
adcd5bcb37ab shrink-revlog: factor out postorder algorithm
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10626
diff changeset
    31
        cur = visit[-1]
adcd5bcb37ab shrink-revlog: factor out postorder algorithm
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10626
diff changeset
    32
        for p in edges[cur]:
14034
1f667030b139 shrink-revlog: defend against null first parents
Augie Fackler <durin42@gmail.com>
parents: 14030
diff changeset
    33
            # defend against node.nullrev because it's occasionally
1f667030b139 shrink-revlog: defend against null first parents
Augie Fackler <durin42@gmail.com>
parents: 14030
diff changeset
    34
            # possible for a node to have parents (null, something)
1f667030b139 shrink-revlog: defend against null first parents
Augie Fackler <durin42@gmail.com>
parents: 14030
diff changeset
    35
            # rather than (something, null)
1f667030b139 shrink-revlog: defend against null first parents
Augie Fackler <durin42@gmail.com>
parents: 14030
diff changeset
    36
            if p not in finished and p != node.nullrev:
10627
adcd5bcb37ab shrink-revlog: factor out postorder algorithm
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10626
diff changeset
    37
                visit.append(p)
adcd5bcb37ab shrink-revlog: factor out postorder algorithm
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10626
diff changeset
    38
                break
adcd5bcb37ab shrink-revlog: factor out postorder algorithm
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10626
diff changeset
    39
        else:
adcd5bcb37ab shrink-revlog: factor out postorder algorithm
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10626
diff changeset
    40
            result.append(cur)
adcd5bcb37ab shrink-revlog: factor out postorder algorithm
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10626
diff changeset
    41
            finished.add(cur)
adcd5bcb37ab shrink-revlog: factor out postorder algorithm
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10626
diff changeset
    42
            visit.pop()
adcd5bcb37ab shrink-revlog: factor out postorder algorithm
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10626
diff changeset
    43
adcd5bcb37ab shrink-revlog: factor out postorder algorithm
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10626
diff changeset
    44
    return result
9515
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    45
10623
64e286c22f29 shrink-revlog: add "reverse postorder" and "postorder reverse" toposorts.
Greg Ward <greg-hg@gerg.ca>
parents: 10622
diff changeset
    46
def toposort_reversepostorder(ui, rl):
64e286c22f29 shrink-revlog: add "reverse postorder" and "postorder reverse" toposorts.
Greg Ward <greg-hg@gerg.ca>
parents: 10622
diff changeset
    47
    # postorder of the reverse directed graph
64e286c22f29 shrink-revlog: add "reverse postorder" and "postorder reverse" toposorts.
Greg Ward <greg-hg@gerg.ca>
parents: 10622
diff changeset
    48
64e286c22f29 shrink-revlog: add "reverse postorder" and "postorder reverse" toposorts.
Greg Ward <greg-hg@gerg.ca>
parents: 10622
diff changeset
    49
    # map rev to list of parent revs (p2 first)
64e286c22f29 shrink-revlog: add "reverse postorder" and "postorder reverse" toposorts.
Greg Ward <greg-hg@gerg.ca>
parents: 10622
diff changeset
    50
    parents = {}
64e286c22f29 shrink-revlog: add "reverse postorder" and "postorder reverse" toposorts.
Greg Ward <greg-hg@gerg.ca>
parents: 10622
diff changeset
    51
    heads = set()
10508
cc35ad583e66 shrink-revlog: add strings for translation / import _ before using it
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10496
diff changeset
    52
    ui.status(_('reading revs\n'))
9515
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    53
    try:
10623
64e286c22f29 shrink-revlog: add "reverse postorder" and "postorder reverse" toposorts.
Greg Ward <greg-hg@gerg.ca>
parents: 10622
diff changeset
    54
        for rev in rl:
64e286c22f29 shrink-revlog: add "reverse postorder" and "postorder reverse" toposorts.
Greg Ward <greg-hg@gerg.ca>
parents: 10622
diff changeset
    55
            ui.progress(_('reading'), rev, total=len(rl))
64e286c22f29 shrink-revlog: add "reverse postorder" and "postorder reverse" toposorts.
Greg Ward <greg-hg@gerg.ca>
parents: 10622
diff changeset
    56
            (p1, p2) = rl.parentrevs(rev)
64e286c22f29 shrink-revlog: add "reverse postorder" and "postorder reverse" toposorts.
Greg Ward <greg-hg@gerg.ca>
parents: 10622
diff changeset
    57
            if p1 == p2 == node.nullrev:
64e286c22f29 shrink-revlog: add "reverse postorder" and "postorder reverse" toposorts.
Greg Ward <greg-hg@gerg.ca>
parents: 10622
diff changeset
    58
                parents[rev] = ()       # root node
64e286c22f29 shrink-revlog: add "reverse postorder" and "postorder reverse" toposorts.
Greg Ward <greg-hg@gerg.ca>
parents: 10622
diff changeset
    59
            elif p1 == p2 or p2 == node.nullrev:
64e286c22f29 shrink-revlog: add "reverse postorder" and "postorder reverse" toposorts.
Greg Ward <greg-hg@gerg.ca>
parents: 10622
diff changeset
    60
                parents[rev] = (p1,)    # normal node
64e286c22f29 shrink-revlog: add "reverse postorder" and "postorder reverse" toposorts.
Greg Ward <greg-hg@gerg.ca>
parents: 10622
diff changeset
    61
            else:
64e286c22f29 shrink-revlog: add "reverse postorder" and "postorder reverse" toposorts.
Greg Ward <greg-hg@gerg.ca>
parents: 10622
diff changeset
    62
                parents[rev] = (p2, p1) # merge node
64e286c22f29 shrink-revlog: add "reverse postorder" and "postorder reverse" toposorts.
Greg Ward <greg-hg@gerg.ca>
parents: 10622
diff changeset
    63
            heads.add(rev)
64e286c22f29 shrink-revlog: add "reverse postorder" and "postorder reverse" toposorts.
Greg Ward <greg-hg@gerg.ca>
parents: 10622
diff changeset
    64
            for p in parents[rev]:
64e286c22f29 shrink-revlog: add "reverse postorder" and "postorder reverse" toposorts.
Greg Ward <greg-hg@gerg.ca>
parents: 10622
diff changeset
    65
                heads.discard(p)
64e286c22f29 shrink-revlog: add "reverse postorder" and "postorder reverse" toposorts.
Greg Ward <greg-hg@gerg.ca>
parents: 10622
diff changeset
    66
    finally:
10724
cb0a7faa29ea progress: drop extra args for pos=None calls (issue2087)
Matt Mackall <mpm@selenic.com>
parents: 10655
diff changeset
    67
        ui.progress(_('reading'), None)
9515
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    68
10627
adcd5bcb37ab shrink-revlog: factor out postorder algorithm
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10626
diff changeset
    69
    heads = list(heads)
adcd5bcb37ab shrink-revlog: factor out postorder algorithm
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10626
diff changeset
    70
    heads.sort(reverse=True)
10623
64e286c22f29 shrink-revlog: add "reverse postorder" and "postorder reverse" toposorts.
Greg Ward <greg-hg@gerg.ca>
parents: 10622
diff changeset
    71
10627
adcd5bcb37ab shrink-revlog: factor out postorder algorithm
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10626
diff changeset
    72
    ui.status(_('sorting revs\n'))
adcd5bcb37ab shrink-revlog: factor out postorder algorithm
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10626
diff changeset
    73
    return postorder(heads, parents)
10623
64e286c22f29 shrink-revlog: add "reverse postorder" and "postorder reverse" toposorts.
Greg Ward <greg-hg@gerg.ca>
parents: 10622
diff changeset
    74
64e286c22f29 shrink-revlog: add "reverse postorder" and "postorder reverse" toposorts.
Greg Ward <greg-hg@gerg.ca>
parents: 10622
diff changeset
    75
def toposort_postorderreverse(ui, rl):
64e286c22f29 shrink-revlog: add "reverse postorder" and "postorder reverse" toposorts.
Greg Ward <greg-hg@gerg.ca>
parents: 10622
diff changeset
    76
    # reverse-postorder of the reverse directed graph
9515
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    77
10623
64e286c22f29 shrink-revlog: add "reverse postorder" and "postorder reverse" toposorts.
Greg Ward <greg-hg@gerg.ca>
parents: 10622
diff changeset
    78
    children = {}
64e286c22f29 shrink-revlog: add "reverse postorder" and "postorder reverse" toposorts.
Greg Ward <greg-hg@gerg.ca>
parents: 10622
diff changeset
    79
    roots = set()
64e286c22f29 shrink-revlog: add "reverse postorder" and "postorder reverse" toposorts.
Greg Ward <greg-hg@gerg.ca>
parents: 10622
diff changeset
    80
    ui.status(_('reading revs\n'))
64e286c22f29 shrink-revlog: add "reverse postorder" and "postorder reverse" toposorts.
Greg Ward <greg-hg@gerg.ca>
parents: 10622
diff changeset
    81
    try:
64e286c22f29 shrink-revlog: add "reverse postorder" and "postorder reverse" toposorts.
Greg Ward <greg-hg@gerg.ca>
parents: 10622
diff changeset
    82
        for rev in rl:
64e286c22f29 shrink-revlog: add "reverse postorder" and "postorder reverse" toposorts.
Greg Ward <greg-hg@gerg.ca>
parents: 10622
diff changeset
    83
            ui.progress(_('reading'), rev, total=len(rl))
64e286c22f29 shrink-revlog: add "reverse postorder" and "postorder reverse" toposorts.
Greg Ward <greg-hg@gerg.ca>
parents: 10622
diff changeset
    84
            (p1, p2) = rl.parentrevs(rev)
64e286c22f29 shrink-revlog: add "reverse postorder" and "postorder reverse" toposorts.
Greg Ward <greg-hg@gerg.ca>
parents: 10622
diff changeset
    85
            if p1 == p2 == node.nullrev:
64e286c22f29 shrink-revlog: add "reverse postorder" and "postorder reverse" toposorts.
Greg Ward <greg-hg@gerg.ca>
parents: 10622
diff changeset
    86
                roots.add(rev)
64e286c22f29 shrink-revlog: add "reverse postorder" and "postorder reverse" toposorts.
Greg Ward <greg-hg@gerg.ca>
parents: 10622
diff changeset
    87
            children[rev] = []
64e286c22f29 shrink-revlog: add "reverse postorder" and "postorder reverse" toposorts.
Greg Ward <greg-hg@gerg.ca>
parents: 10622
diff changeset
    88
            if p1 != node.nullrev:
64e286c22f29 shrink-revlog: add "reverse postorder" and "postorder reverse" toposorts.
Greg Ward <greg-hg@gerg.ca>
parents: 10622
diff changeset
    89
                children[p1].append(rev)
64e286c22f29 shrink-revlog: add "reverse postorder" and "postorder reverse" toposorts.
Greg Ward <greg-hg@gerg.ca>
parents: 10622
diff changeset
    90
            if p2 != node.nullrev:
64e286c22f29 shrink-revlog: add "reverse postorder" and "postorder reverse" toposorts.
Greg Ward <greg-hg@gerg.ca>
parents: 10622
diff changeset
    91
                children[p2].append(rev)
64e286c22f29 shrink-revlog: add "reverse postorder" and "postorder reverse" toposorts.
Greg Ward <greg-hg@gerg.ca>
parents: 10622
diff changeset
    92
    finally:
10724
cb0a7faa29ea progress: drop extra args for pos=None calls (issue2087)
Matt Mackall <mpm@selenic.com>
parents: 10655
diff changeset
    93
        ui.progress(_('reading'), None)
10623
64e286c22f29 shrink-revlog: add "reverse postorder" and "postorder reverse" toposorts.
Greg Ward <greg-hg@gerg.ca>
parents: 10622
diff changeset
    94
11298
3e46d76eaabf shrink-repo: wrong variable name
Pradeepkumar Gayam <in3xes@gmail.com>
parents: 11294
diff changeset
    95
    roots = list(roots)
10627
adcd5bcb37ab shrink-revlog: factor out postorder algorithm
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10626
diff changeset
    96
    roots.sort()
10624
432eb853a2c6 shrink-revlog: add accounting of suboptimal nodes to the new algorithms.
Greg Ward <greg-hg@gerg.ca>
parents: 10623
diff changeset
    97
10508
cc35ad583e66 shrink-revlog: add strings for translation / import _ before using it
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10496
diff changeset
    98
    ui.status(_('sorting revs\n'))
10627
adcd5bcb37ab shrink-revlog: factor out postorder algorithm
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10626
diff changeset
    99
    result = postorder(roots, children)
10623
64e286c22f29 shrink-revlog: add "reverse postorder" and "postorder reverse" toposorts.
Greg Ward <greg-hg@gerg.ca>
parents: 10622
diff changeset
   100
    result.reverse()
64e286c22f29 shrink-revlog: add "reverse postorder" and "postorder reverse" toposorts.
Greg Ward <greg-hg@gerg.ca>
parents: 10622
diff changeset
   101
    return result
9515
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   102
10213
9e6848f352b0 contrib: use ui to write in shrink-revlog.py
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10009
diff changeset
   103
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
   104
10508
cc35ad583e66 shrink-revlog: add strings for translation / import _ before using it
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10496
diff changeset
   105
    ui.status(_('writing revs\n'))
10440
b39b32c33269 shrink: use progress API
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10282
diff changeset
   106
10009
69dca8574a6a shrink-revlog: improve performance: use changegroup instead of revisions
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 9712
diff changeset
   107
69dca8574a6a shrink-revlog: improve performance: use changegroup instead of revisions
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 9712
diff changeset
   108
    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
   109
69dca8574a6a shrink-revlog: improve performance: use changegroup instead of revisions
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 9712
diff changeset
   110
    # this is a bit ugly, but it works
13783
c196352d935b changegroup: fold progress meter into callbacks
Matt Mackall <mpm@selenic.com>
parents: 13782
diff changeset
   111
    count = [0]
14030
e5dd974a99fa shrink-revlog: use a bundler object (see d69c9510d648)
Augie Fackler <durin42@gmail.com>
parents: 14029
diff changeset
   112
    def lookup(revl, x):
13783
c196352d935b changegroup: fold progress meter into callbacks
Matt Mackall <mpm@selenic.com>
parents: 13782
diff changeset
   113
        count[0] += 1
c196352d935b changegroup: fold progress meter into callbacks
Matt Mackall <mpm@selenic.com>
parents: 13782
diff changeset
   114
        ui.progress(_('writing'), count[0], total=len(order))
14030
e5dd974a99fa shrink-revlog: use a bundler object (see d69c9510d648)
Augie Fackler <durin42@gmail.com>
parents: 14029
diff changeset
   115
        return "%020d" % revl.linkrev(revl.rev(x))
13782
9131724c3f4b changegroup: combine infocollect and lookup callbacks
Matt Mackall <mpm@selenic.com>
parents: 12865
diff changeset
   116
10009
69dca8574a6a shrink-revlog: improve performance: use changegroup instead of revisions
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 9712
diff changeset
   117
    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
   118
9515
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   119
    try:
14030
e5dd974a99fa shrink-revlog: use a bundler object (see d69c9510d648)
Augie Fackler <durin42@gmail.com>
parents: 14029
diff changeset
   120
        bundler = changegroup.bundle10(lookup)
e5dd974a99fa shrink-revlog: use a bundler object (see d69c9510d648)
Augie Fackler <durin42@gmail.com>
parents: 14029
diff changeset
   121
        group = util.chunkbuffer(r1.group(order, bundler))
12348
7f97b4841ee7 bundle: fix shrink-revlog bundle usage
Matt Mackall <mpm@selenic.com>
parents: 12335
diff changeset
   122
        group = changegroup.unbundle10(group, "UN")
12335
e21fe9c5fb25 bundle: get rid of chunkiter
Matt Mackall <mpm@selenic.com>
parents: 12333
diff changeset
   123
        r2.addgroup(group, unlookup, tr)
9515
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   124
    finally:
10724
cb0a7faa29ea progress: drop extra args for pos=None calls (issue2087)
Matt Mackall <mpm@selenic.com>
parents: 10655
diff changeset
   125
        ui.progress(_('writing'), None)
9515
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   126
10542
989b2a5eaaba shrink: handle all combinations of inline/non-inline revlogs
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10509
diff changeset
   127
def report(ui, r1, r2):
989b2a5eaaba shrink: handle all combinations of inline/non-inline revlogs
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10509
diff changeset
   128
    def getsize(r):
989b2a5eaaba shrink: handle all combinations of inline/non-inline revlogs
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10509
diff changeset
   129
        s = 0
989b2a5eaaba shrink: handle all combinations of inline/non-inline revlogs
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10509
diff changeset
   130
        for fn in (r.indexfile, r.datafile):
989b2a5eaaba shrink: handle all combinations of inline/non-inline revlogs
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10509
diff changeset
   131
            try:
989b2a5eaaba shrink: handle all combinations of inline/non-inline revlogs
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10509
diff changeset
   132
                s += os.stat(fn).st_size
989b2a5eaaba shrink: handle all combinations of inline/non-inline revlogs
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10509
diff changeset
   133
            except OSError, inst:
989b2a5eaaba shrink: handle all combinations of inline/non-inline revlogs
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10509
diff changeset
   134
                if inst.errno != errno.ENOENT:
989b2a5eaaba shrink: handle all combinations of inline/non-inline revlogs
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10509
diff changeset
   135
                    raise
989b2a5eaaba shrink: handle all combinations of inline/non-inline revlogs
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10509
diff changeset
   136
        return s
989b2a5eaaba shrink: handle all combinations of inline/non-inline revlogs
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10509
diff changeset
   137
989b2a5eaaba shrink: handle all combinations of inline/non-inline revlogs
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10509
diff changeset
   138
    oldsize = float(getsize(r1))
989b2a5eaaba shrink: handle all combinations of inline/non-inline revlogs
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10509
diff changeset
   139
    newsize = float(getsize(r2))
9515
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   140
9712
18b134ef294c kill trailing whitespace
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 9515
diff changeset
   141
    # 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
   142
    # blows up under Python 2.5 or earlier
10508
cc35ad583e66 shrink-revlog: add strings for translation / import _ before using it
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10496
diff changeset
   143
    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
   144
             % (int(oldsize), oldsize / 1024 / 1024))
10508
cc35ad583e66 shrink-revlog: add strings for translation / import _ before using it
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10496
diff changeset
   145
    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
   146
             % (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
   147
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   148
    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
   149
    shrink_factor = oldsize / newsize
10508
cc35ad583e66 shrink-revlog: add strings for translation / import _ before using it
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10496
diff changeset
   150
    ui.write(_('shrinkage: %.1f%% (%.1fx)\n')
cc35ad583e66 shrink-revlog: add strings for translation / import _ before using it
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10496
diff changeset
   151
             % (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
   152
10215
9d79b8f58bea contrib: turn shrink-revlog.py into an extension
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10214
diff changeset
   153
def shrink(ui, repo, **opts):
10622
bc81f126139f shrink-revlog: add --sort option for user-selectable toposort algorithm.
Greg Ward <greg-hg@gerg.ca>
parents: 10621
diff changeset
   154
    """shrink a revlog by reordering revisions
bc81f126139f shrink-revlog: add --sort option for user-selectable toposort algorithm.
Greg Ward <greg-hg@gerg.ca>
parents: 10621
diff changeset
   155
bc81f126139f shrink-revlog: add --sort option for user-selectable toposort algorithm.
Greg Ward <greg-hg@gerg.ca>
parents: 10621
diff changeset
   156
    Rewrites all the entries in some revlog of the current repository
bc81f126139f shrink-revlog: add --sort option for user-selectable toposort algorithm.
Greg Ward <greg-hg@gerg.ca>
parents: 10621
diff changeset
   157
    (by default, the manifest log) to save space.
bc81f126139f shrink-revlog: add --sort option for user-selectable toposort algorithm.
Greg Ward <greg-hg@gerg.ca>
parents: 10621
diff changeset
   158
bc81f126139f shrink-revlog: add --sort option for user-selectable toposort algorithm.
Greg Ward <greg-hg@gerg.ca>
parents: 10621
diff changeset
   159
    Different sort algorithms have different performance
bc81f126139f shrink-revlog: add --sort option for user-selectable toposort algorithm.
Greg Ward <greg-hg@gerg.ca>
parents: 10621
diff changeset
   160
    characteristics.  Use ``--sort`` to select a sort algorithm so you
10625
add562abc28a shrink-revlog: remove branchsort algorithm (it behaves poorly)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10624
diff changeset
   161
    can determine which works best for your data.
10215
9d79b8f58bea contrib: turn shrink-revlog.py into an extension
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10214
diff changeset
   162
    """
9515
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   163
10215
9d79b8f58bea contrib: turn shrink-revlog.py into an extension
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10214
diff changeset
   164
    if not repo.local():
10508
cc35ad583e66 shrink-revlog: add strings for translation / import _ before using it
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10496
diff changeset
   165
        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
   166
10215
9d79b8f58bea contrib: turn shrink-revlog.py into an extension
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10214
diff changeset
   167
    fn = opts.get('revlog')
9d79b8f58bea contrib: turn shrink-revlog.py into an extension
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10214
diff changeset
   168
    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
   169
        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
   170
    else:
10215
9d79b8f58bea contrib: turn shrink-revlog.py into an extension
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10214
diff changeset
   171
        if not fn.endswith('.i'):
10508
cc35ad583e66 shrink-revlog: add strings for translation / import _ before using it
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10496
diff changeset
   172
            raise util.Abort(_('--revlog option must specify the revlog index '
cc35ad583e66 shrink-revlog: add strings for translation / import _ before using it
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10496
diff changeset
   173
                               '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
   174
15381
c519cd8f0169 backout dbdb777502dc (issue3077) (issue3071)
Matt Mackall <mpm@selenic.com>
parents: 15355
diff changeset
   175
        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
   176
        store = repo.sjoin('')
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   177
        if not indexfn.startswith(store):
10508
cc35ad583e66 shrink-revlog: add strings for translation / import _ before using it
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10496
diff changeset
   178
            raise util.Abort(_('--revlog option must specify a revlog in %s, '
cc35ad583e66 shrink-revlog: add strings for translation / import _ before using it
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10496
diff changeset
   179
                               '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
   180
10622
bc81f126139f shrink-revlog: add --sort option for user-selectable toposort algorithm.
Greg Ward <greg-hg@gerg.ca>
parents: 10621
diff changeset
   181
    sortname = opts['sort']
bc81f126139f shrink-revlog: add --sort option for user-selectable toposort algorithm.
Greg Ward <greg-hg@gerg.ca>
parents: 10621
diff changeset
   182
    try:
bc81f126139f shrink-revlog: add --sort option for user-selectable toposort algorithm.
Greg Ward <greg-hg@gerg.ca>
parents: 10621
diff changeset
   183
        toposort = globals()['toposort_' + sortname]
bc81f126139f shrink-revlog: add --sort option for user-selectable toposort algorithm.
Greg Ward <greg-hg@gerg.ca>
parents: 10621
diff changeset
   184
    except KeyError:
bc81f126139f shrink-revlog: add --sort option for user-selectable toposort algorithm.
Greg Ward <greg-hg@gerg.ca>
parents: 10621
diff changeset
   185
        raise util.Abort(_('no such toposort algorithm: %s') % sortname)
bc81f126139f shrink-revlog: add --sort option for user-selectable toposort algorithm.
Greg Ward <greg-hg@gerg.ca>
parents: 10621
diff changeset
   186
9515
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   187
    if not os.path.exists(indexfn):
10508
cc35ad583e66 shrink-revlog: add strings for translation / import _ before using it
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10496
diff changeset
   188
        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
   189
    if '00changelog' in indexfn:
10508
cc35ad583e66 shrink-revlog: add strings for translation / import _ before using it
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10496
diff changeset
   190
        raise util.Abort(_('shrinking the changelog '
cc35ad583e66 shrink-revlog: add strings for translation / import _ before using it
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10496
diff changeset
   191
                           'will corrupt your repository'))
10542
989b2a5eaaba shrink: handle all combinations of inline/non-inline revlogs
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10509
diff changeset
   192
989b2a5eaaba shrink: handle all combinations of inline/non-inline revlogs
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10509
diff changeset
   193
    ui.write(_('shrinking %s\n') % indexfn)
11294
7b5d05e0fb1e shrink-revlog: use util.mktempcopy() to preserve mode of index file.
Greg Ward <greg-hg@gerg.ca>
parents: 11268
diff changeset
   194
    tmpindexfn = util.mktempcopy(indexfn, emptyok=True)
10542
989b2a5eaaba shrink: handle all combinations of inline/non-inline revlogs
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10509
diff changeset
   195
14029
83d3f87c059e shrink-revlog: update util.opener to scmutil.opener after d13913355390
Augie Fackler <durin42@gmail.com>
parents: 14028
diff changeset
   196
    r1 = revlog.revlog(scmutil.opener(os.getcwd(), audit=False), indexfn)
83d3f87c059e shrink-revlog: update util.opener to scmutil.opener after d13913355390
Augie Fackler <durin42@gmail.com>
parents: 14028
diff changeset
   197
    r2 = revlog.revlog(scmutil.opener(os.getcwd(), audit=False), tmpindexfn)
10542
989b2a5eaaba shrink: handle all combinations of inline/non-inline revlogs
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10509
diff changeset
   198
989b2a5eaaba shrink: handle all combinations of inline/non-inline revlogs
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10509
diff changeset
   199
    datafn, tmpdatafn = r1.datafile, r2.datafile
9515
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   200
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   201
    oldindexfn = indexfn + '.old'
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   202
    olddatafn = datafn + '.old'
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   203
    if os.path.exists(oldindexfn) or os.path.exists(olddatafn):
10508
cc35ad583e66 shrink-revlog: add strings for translation / import _ before using it
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10496
diff changeset
   204
        raise util.Abort(_('one or both of\n'
cc35ad583e66 shrink-revlog: add strings for translation / import _ before using it
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10496
diff changeset
   205
                           '  %s\n'
cc35ad583e66 shrink-revlog: add strings for translation / import _ before using it
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10496
diff changeset
   206
                           '  %s\n'
cc35ad583e66 shrink-revlog: add strings for translation / import _ before using it
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10496
diff changeset
   207
                           'exists from a previous run; please clean up '
cc35ad583e66 shrink-revlog: add strings for translation / import _ before using it
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10496
diff changeset
   208
                           'before 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
   209
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   210
    # 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
   211
    # 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
   212
    # 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
   213
    # absolute path.
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   214
    lock = repo.lock(wait=False)
10234
c8d6f339bbd7 shrink-revlog: make it work on windows (issue1976)
Patrick Mezard <pmezard@gmail.com>
parents: 10230
diff changeset
   215
    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
   216
                                 open,
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   217
                                 repo.sjoin('journal'))
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   218
10542
989b2a5eaaba shrink: handle all combinations of inline/non-inline revlogs
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10509
diff changeset
   219
    def ignoremissing(func):
989b2a5eaaba shrink: handle all combinations of inline/non-inline revlogs
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10509
diff changeset
   220
        def f(*args, **kw):
989b2a5eaaba shrink: handle all combinations of inline/non-inline revlogs
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10509
diff changeset
   221
            try:
989b2a5eaaba shrink: handle all combinations of inline/non-inline revlogs
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10509
diff changeset
   222
                return func(*args, **kw)
989b2a5eaaba shrink: handle all combinations of inline/non-inline revlogs
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10509
diff changeset
   223
            except OSError, inst:
989b2a5eaaba shrink: handle all combinations of inline/non-inline revlogs
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10509
diff changeset
   224
                if inst.errno != errno.ENOENT:
989b2a5eaaba shrink: handle all combinations of inline/non-inline revlogs
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10509
diff changeset
   225
                    raise
989b2a5eaaba shrink: handle all combinations of inline/non-inline revlogs
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10509
diff changeset
   226
        return f
989b2a5eaaba shrink: handle all combinations of inline/non-inline revlogs
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10509
diff changeset
   227
9515
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   228
    try:
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   229
        try:
10213
9e6848f352b0 contrib: use ui to write in shrink-revlog.py
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10009
diff changeset
   230
            order = toposort(ui, r1)
10626
3fc95c3bc3ba shrink-revlog: factor out suboptimal computation
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10625
diff changeset
   231
3fc95c3bc3ba shrink-revlog: factor out suboptimal computation
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10625
diff changeset
   232
            suboptimal = 0
3fc95c3bc3ba shrink-revlog: factor out suboptimal computation
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10625
diff changeset
   233
            for i in xrange(1, len(order)):
3fc95c3bc3ba shrink-revlog: factor out suboptimal computation
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10625
diff changeset
   234
                parents = [p for p in r1.parentrevs(order[i])
3fc95c3bc3ba shrink-revlog: factor out suboptimal computation
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10625
diff changeset
   235
                           if p != node.nullrev]
10655
f63fb5c0cf67 shrink-revlog: add missing whitespace in expression
Martin Geisler <mg@lazybytes.net>
parents: 10627
diff changeset
   236
                if parents and order[i - 1] not in parents:
10626
3fc95c3bc3ba shrink-revlog: factor out suboptimal computation
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10625
diff changeset
   237
                    suboptimal += 1
3fc95c3bc3ba shrink-revlog: factor out suboptimal computation
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10625
diff changeset
   238
            ui.note(_('%d suboptimal nodes\n') % suboptimal)
3fc95c3bc3ba shrink-revlog: factor out suboptimal computation
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10625
diff changeset
   239
10213
9e6848f352b0 contrib: use ui to write in shrink-revlog.py
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10009
diff changeset
   240
            writerevs(ui, r1, r2, order, tr)
10542
989b2a5eaaba shrink: handle all combinations of inline/non-inline revlogs
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10509
diff changeset
   241
            report(ui, r1, r2)
9515
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   242
            tr.close()
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   243
        except:
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   244
            # 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
   245
            # deleting them.
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   246
            tr.abort()
10542
989b2a5eaaba shrink: handle all combinations of inline/non-inline revlogs
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10509
diff changeset
   247
            for fn in (tmpindexfn, tmpdatafn):
989b2a5eaaba shrink: handle all combinations of inline/non-inline revlogs
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10509
diff changeset
   248
                ignoremissing(os.unlink)(fn)
9515
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   249
            raise
10241
4b2a086bee31 shrink-revlog: add --dry-run option
Patrick Mezard <pmezard@gmail.com>
parents: 10236
diff changeset
   250
        if not opts.get('dry_run'):
10542
989b2a5eaaba shrink: handle all combinations of inline/non-inline revlogs
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10509
diff changeset
   251
            # racy, both files cannot be renamed atomically
989b2a5eaaba shrink: handle all combinations of inline/non-inline revlogs
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10509
diff changeset
   252
            # copy files
14235
b9e1b041744f rename util.os_link to oslink
Adrian Buehlmann <adrian@cadifra.com>
parents: 14034
diff changeset
   253
            util.oslink(indexfn, oldindexfn)
b9e1b041744f rename util.os_link to oslink
Adrian Buehlmann <adrian@cadifra.com>
parents: 14034
diff changeset
   254
            ignoremissing(util.oslink)(datafn, olddatafn)
11267
d3ebb1a0bc49 shrink-revlog: preserve mode of the shrunken index and data file.
Greg Ward <greg-hg@gerg.ca>
parents: 10542
diff changeset
   255
10542
989b2a5eaaba shrink: handle all combinations of inline/non-inline revlogs
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10509
diff changeset
   256
            # rename
10241
4b2a086bee31 shrink-revlog: add --dry-run option
Patrick Mezard <pmezard@gmail.com>
parents: 10236
diff changeset
   257
            util.rename(tmpindexfn, indexfn)
10542
989b2a5eaaba shrink: handle all combinations of inline/non-inline revlogs
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10509
diff changeset
   258
            try:
11267
d3ebb1a0bc49 shrink-revlog: preserve mode of the shrunken index and data file.
Greg Ward <greg-hg@gerg.ca>
parents: 10542
diff changeset
   259
                os.chmod(tmpdatafn, os.stat(datafn).st_mode)
10542
989b2a5eaaba shrink: handle all combinations of inline/non-inline revlogs
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10509
diff changeset
   260
                util.rename(tmpdatafn, datafn)
989b2a5eaaba shrink: handle all combinations of inline/non-inline revlogs
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10509
diff changeset
   261
            except OSError, inst:
989b2a5eaaba shrink: handle all combinations of inline/non-inline revlogs
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10509
diff changeset
   262
                if inst.errno != errno.ENOENT:
989b2a5eaaba shrink: handle all combinations of inline/non-inline revlogs
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10509
diff changeset
   263
                    raise
989b2a5eaaba shrink: handle all combinations of inline/non-inline revlogs
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10509
diff changeset
   264
                ignoremissing(os.unlink)(datafn)
10241
4b2a086bee31 shrink-revlog: add --dry-run option
Patrick Mezard <pmezard@gmail.com>
parents: 10236
diff changeset
   265
        else:
10542
989b2a5eaaba shrink: handle all combinations of inline/non-inline revlogs
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10509
diff changeset
   266
            for fn in (tmpindexfn, tmpdatafn):
989b2a5eaaba shrink: handle all combinations of inline/non-inline revlogs
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10509
diff changeset
   267
                ignoremissing(os.unlink)(fn)
9515
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   268
    finally:
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   269
        lock.release()
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   270
10241
4b2a086bee31 shrink-revlog: add --dry-run option
Patrick Mezard <pmezard@gmail.com>
parents: 10236
diff changeset
   271
    if not opts.get('dry_run'):
16306
d76b9abd1509 shrink-revlog: make check-code happier
Greg Ward <greg@gerg.ca>
parents: 16305
diff changeset
   272
        ui.write(
d76b9abd1509 shrink-revlog: make check-code happier
Greg Ward <greg@gerg.ca>
parents: 16305
diff changeset
   273
            _('note: old revlog saved in:\n'
d76b9abd1509 shrink-revlog: make check-code happier
Greg Ward <greg@gerg.ca>
parents: 16305
diff changeset
   274
              '  %s\n'
d76b9abd1509 shrink-revlog: make check-code happier
Greg Ward <greg@gerg.ca>
parents: 16305
diff changeset
   275
              '  %s\n'
d76b9abd1509 shrink-revlog: make check-code happier
Greg Ward <greg@gerg.ca>
parents: 16305
diff changeset
   276
              '(You can delete those files when you are satisfied that your\n'
d76b9abd1509 shrink-revlog: make check-code happier
Greg Ward <greg@gerg.ca>
parents: 16305
diff changeset
   277
              'repository is still sane.  '
d76b9abd1509 shrink-revlog: make check-code happier
Greg Ward <greg@gerg.ca>
parents: 16305
diff changeset
   278
              'Running \'hg verify\' is strongly recommended.)\n')
d76b9abd1509 shrink-revlog: make check-code happier
Greg Ward <greg@gerg.ca>
parents: 16305
diff changeset
   279
            % (oldindexfn, olddatafn))
9515
f7d85980261c Add script to rewrite revlog to workaround lack of parent deltas.
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   280
10215
9d79b8f58bea contrib: turn shrink-revlog.py into an extension
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10214
diff changeset
   281
cmdtable = {
9d79b8f58bea contrib: turn shrink-revlog.py into an extension
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10214
diff changeset
   282
    'shrink': (shrink,
16306
d76b9abd1509 shrink-revlog: make check-code happier
Greg Ward <greg@gerg.ca>
parents: 16305
diff changeset
   283
               [('', 'revlog', '',
d76b9abd1509 shrink-revlog: make check-code happier
Greg Ward <greg@gerg.ca>
parents: 16305
diff changeset
   284
                 _('the revlog to shrink (.i)')),
d76b9abd1509 shrink-revlog: make check-code happier
Greg Ward <greg@gerg.ca>
parents: 16305
diff changeset
   285
                ('n', 'dry-run', None,
d76b9abd1509 shrink-revlog: make check-code happier
Greg Ward <greg@gerg.ca>
parents: 16305
diff changeset
   286
                 _('do not shrink, simulate only')),
d76b9abd1509 shrink-revlog: make check-code happier
Greg Ward <greg@gerg.ca>
parents: 16305
diff changeset
   287
                ('', 'sort', 'reversepostorder',
d76b9abd1509 shrink-revlog: make check-code happier
Greg Ward <greg@gerg.ca>
parents: 16305
diff changeset
   288
                 _('name of sort algorithm to use')),
10241
4b2a086bee31 shrink-revlog: add --dry-run option
Patrick Mezard <pmezard@gmail.com>
parents: 10236
diff changeset
   289
                ],
10508
cc35ad583e66 shrink-revlog: add strings for translation / import _ before using it
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10496
diff changeset
   290
               _('hg shrink [--revlog PATH]'))
10215
9d79b8f58bea contrib: turn shrink-revlog.py into an extension
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10214
diff changeset
   291
}
10236
49a8625b8cac shrink-revlog: help/doc tweaks
Greg Ward <greg-hg@gerg.ca>
parents: 10234
diff changeset
   292
49a8625b8cac shrink-revlog: help/doc tweaks
Greg Ward <greg-hg@gerg.ca>
parents: 10234
diff changeset
   293
if __name__ == "__main__":
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10241
diff changeset
   294
    print "shrink-revlog.py is now an extension (see hg help extensions)"