mercurial/repair.py
author Anton Shestakov <av6@dwimlabs.net>
Fri, 12 Jun 2015 18:34:10 +0800
changeset 25526 32f76eccbced
parent 25404 ff955e7b1085
child 25652 2882d6886919
permissions -rw-r--r--
hgweb: don't point file links at tip hash where it doesn't make sense Some pages, e.g. bookmarks, help and summary don't have a meaningful revision context: they always either show information about tip or about the whole repo (and not about any specific changeset). And error pages can just show hgweb error messages, not related to any repo or changeset. Having a hash in the links worked (even when '{node|short}' resolved to an empty string on error pages), but seeing pages without revision context provide links with hashes is a bit confusing (unless you keep current tip hash in your head at all times) and not consistent with other template styles and other links on the same page: they don't have a hash. Let's just link to '/file', which is equal to '/file/tip'.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4702
18e91c9def0c strip: move strip code to a new repair module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
     1
# repair.py - functions for repository repair for mercurial
18e91c9def0c strip: move strip code to a new repair module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
     2
#
18e91c9def0c strip: move strip code to a new repair module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
     3
# Copyright 2005, 2006 Chris Mason <mason@suse.com>
18e91c9def0c strip: move strip code to a new repair module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
     4
# Copyright 2007 Matt Mackall
18e91c9def0c strip: move strip code to a new repair module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
     5
#
8225
46293a0c7e9f updated license to be explicit about GPL version 2
Martin Geisler <mg@lazybytes.net>
parents: 8073
diff changeset
     6
# This software may be used and distributed according to the terms of the
10263
25e572394f5c Update license to GPLv2+
Matt Mackall <mpm@selenic.com>
parents: 9150
diff changeset
     7
# GNU General Public License version 2 or any later version.
4702
18e91c9def0c strip: move strip code to a new repair module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
     8
23898
b21c2e0ee8a3 repair: add experimental option to write bundle2 files
Eric Sumner <ericsumner@fb.com>
parents: 23895
diff changeset
     9
from mercurial import changegroup, exchange, util, bundle2
25340
28800ab40395 repair: use _hexlist() to build revset expression from binary nodes
Yuya Nishihara <yuya@tcha.org>
parents: 25300
diff changeset
    10
from mercurial.node import short
14064
e4bfb9c337f3 remove unused imports and variables
Alexander Solovyov <alexander@solovyov.net>
parents: 13747
diff changeset
    11
from mercurial.i18n import _
16440
692bf06bb1af repair: fix missing import
Alain Leufroy <alain.leufroy@logilab.fr>
parents: 16388
diff changeset
    12
import errno
4702
18e91c9def0c strip: move strip code to a new repair module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    13
15386
6051d8e7e133 strip: backout 73307643a09f (issue3077)
Matt Mackall <mpm@selenic.com>
parents: 15068
diff changeset
    14
def _bundle(repo, bases, heads, node, suffix, compress=True):
5905
3afbd82a6c82 repair.py: don't use nested functions.
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5904
diff changeset
    15
    """create a bundle with the specified revisions as a backup"""
25404
ff955e7b1085 bundle2: use bundle2 by default
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25340
diff changeset
    16
    usebundle2 = (repo.ui.config('experimental', 'bundle2-exp', True) and
23898
b21c2e0ee8a3 repair: add experimental option to write bundle2 files
Eric Sumner <ericsumner@fb.com>
parents: 23895
diff changeset
    17
                  repo.ui.config('experimental', 'strip-bundle2-version'))
b21c2e0ee8a3 repair: add experimental option to write bundle2 files
Eric Sumner <ericsumner@fb.com>
parents: 23895
diff changeset
    18
    if usebundle2:
b21c2e0ee8a3 repair: add experimental option to write bundle2 files
Eric Sumner <ericsumner@fb.com>
parents: 23895
diff changeset
    19
        cgversion = repo.ui.config('experimental', 'strip-bundle2-version')
23939
33d1b81c6ef0 repair._bundle: fix traceback for bad config value
Eric Sumner <ericsumner@fb.com>
parents: 23898
diff changeset
    20
        if cgversion not in changegroup.packermap:
24863
f3558829471b repair: avoid string concatenation by + operator
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24686
diff changeset
    21
            repo.ui.warn(_('unknown strip-bundle2-version value %r; '
23939
33d1b81c6ef0 repair._bundle: fix traceback for bad config value
Eric Sumner <ericsumner@fb.com>
parents: 23898
diff changeset
    22
                            'should be one of %r\n') %
33d1b81c6ef0 repair._bundle: fix traceback for bad config value
Eric Sumner <ericsumner@fb.com>
parents: 23898
diff changeset
    23
                         (cgversion, sorted(changegroup.packermap.keys()),))
33d1b81c6ef0 repair._bundle: fix traceback for bad config value
Eric Sumner <ericsumner@fb.com>
parents: 23898
diff changeset
    24
            cgversion = '01'
33d1b81c6ef0 repair._bundle: fix traceback for bad config value
Eric Sumner <ericsumner@fb.com>
parents: 23898
diff changeset
    25
            usebundle2 = False
23898
b21c2e0ee8a3 repair: add experimental option to write bundle2 files
Eric Sumner <ericsumner@fb.com>
parents: 23895
diff changeset
    26
    else:
b21c2e0ee8a3 repair: add experimental option to write bundle2 files
Eric Sumner <ericsumner@fb.com>
parents: 23895
diff changeset
    27
        cgversion = '01'
b21c2e0ee8a3 repair: add experimental option to write bundle2 files
Eric Sumner <ericsumner@fb.com>
parents: 23895
diff changeset
    28
b21c2e0ee8a3 repair: add experimental option to write bundle2 files
Eric Sumner <ericsumner@fb.com>
parents: 23895
diff changeset
    29
    cg = changegroup.changegroupsubset(repo, bases, heads, 'strip',
b21c2e0ee8a3 repair: add experimental option to write bundle2 files
Eric Sumner <ericsumner@fb.com>
parents: 23895
diff changeset
    30
                                       version=cgversion)
20977
a57dcd11be34 repair: make paths in "_bundle()" relative to ".hg"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20975
diff changeset
    31
    backupdir = "strip-backup"
a57dcd11be34 repair: make paths in "_bundle()" relative to ".hg"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20975
diff changeset
    32
    vfs = repo.vfs
a57dcd11be34 repair: make paths in "_bundle()" relative to ".hg"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20975
diff changeset
    33
    if not vfs.isdir(backupdir):
a57dcd11be34 repair: make paths in "_bundle()" relative to ".hg"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20975
diff changeset
    34
        vfs.mkdir(backupdir)
23835
aa4a1672583e bundles: do not overwrite existing backup bundles (BC)
Durham Goode <durham@fb.com>
parents: 22818
diff changeset
    35
aa4a1672583e bundles: do not overwrite existing backup bundles (BC)
Durham Goode <durham@fb.com>
parents: 22818
diff changeset
    36
    # Include a hash of all the nodes in the filename for uniqueness
25340
28800ab40395 repair: use _hexlist() to build revset expression from binary nodes
Yuya Nishihara <yuya@tcha.org>
parents: 25300
diff changeset
    37
    allcommits = repo.set('%ln::%ln', bases, heads)
23835
aa4a1672583e bundles: do not overwrite existing backup bundles (BC)
Durham Goode <durham@fb.com>
parents: 22818
diff changeset
    38
    allhashes = sorted(c.hex() for c in allcommits)
aa4a1672583e bundles: do not overwrite existing backup bundles (BC)
Durham Goode <durham@fb.com>
parents: 22818
diff changeset
    39
    totalhash = util.sha1(''.join(allhashes)).hexdigest()
aa4a1672583e bundles: do not overwrite existing backup bundles (BC)
Durham Goode <durham@fb.com>
parents: 22818
diff changeset
    40
    name = "%s/%s-%s-%s.hg" % (backupdir, short(node), totalhash[:8], suffix)
aa4a1672583e bundles: do not overwrite existing backup bundles (BC)
Durham Goode <durham@fb.com>
parents: 22818
diff changeset
    41
23898
b21c2e0ee8a3 repair: add experimental option to write bundle2 files
Eric Sumner <ericsumner@fb.com>
parents: 23895
diff changeset
    42
    if usebundle2:
24686
e0e28e910fa3 bundle2: rename format, parts and config to final names
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24252
diff changeset
    43
        bundletype = "HG20"
23898
b21c2e0ee8a3 repair: add experimental option to write bundle2 files
Eric Sumner <ericsumner@fb.com>
parents: 23895
diff changeset
    44
    elif compress:
11791
00cde9bddbe4 repair: do not compress partial bundle if we do not keep it on disk
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 11600
diff changeset
    45
        bundletype = "HG10BZ"
00cde9bddbe4 repair: do not compress partial bundle if we do not keep it on disk
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 11600
diff changeset
    46
    else:
00cde9bddbe4 repair: do not compress partial bundle if we do not keep it on disk
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 11600
diff changeset
    47
        bundletype = "HG10UN"
23895
cda18ded2c48 changegroup.writebundle: provide ui
Eric Sumner <ericsumner@fb.com>
parents: 23878
diff changeset
    48
    return changegroup.writebundle(repo.ui, cg, name, bundletype, vfs)
4702
18e91c9def0c strip: move strip code to a new repair module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    49
5910
b9a830fa10f6 simplify revlog.strip interface and callers; add docstring
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5909
diff changeset
    50
def _collectfiles(repo, striprev):
b9a830fa10f6 simplify revlog.strip interface and callers; add docstring
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5909
diff changeset
    51
    """find out the filelogs affected by the strip"""
8462
e7e4e41b3bbc repair: use set instead of dict
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 8363
diff changeset
    52
    files = set()
4702
18e91c9def0c strip: move strip code to a new repair module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    53
6750
fb42030d79d6 add __len__ and __iter__ methods to repo and revlog
Matt Mackall <mpm@selenic.com>
parents: 6747
diff changeset
    54
    for x in xrange(striprev, len(repo)):
8479
3e16c0fc2241 repair: bulk update sets
Martin Geisler <mg@lazybytes.net>
parents: 8462
diff changeset
    55
        files.update(repo[x].files())
5902
98f8dec8f437 repair.py: split stripall into two functions; clean it up a bit
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5901
diff changeset
    56
8462
e7e4e41b3bbc repair: use set instead of dict
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 8363
diff changeset
    57
    return sorted(files)
5902
98f8dec8f437 repair.py: split stripall into two functions; clean it up a bit
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5901
diff changeset
    58
13702
ffd370aa050b strip: remove usage of extranodes
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 13362
diff changeset
    59
def _collectbrokencsets(repo, files, striprev):
ffd370aa050b strip: remove usage of extranodes
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 13362
diff changeset
    60
    """return the changesets which will be broken by the truncation"""
13705
73cfb7a5aa56 strip: simplify collectone
Matt Mackall <mpm@selenic.com>
parents: 13702
diff changeset
    61
    s = set()
13702
ffd370aa050b strip: remove usage of extranodes
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 13362
diff changeset
    62
    def collectone(revlog):
20074
5fc2ae1c631b strip: add faster revlog strip computation
Durham Goode <durham@fb.com>
parents: 18766
diff changeset
    63
        _, brokenset = revlog.getstrippoint(striprev)
5fc2ae1c631b strip: add faster revlog strip computation
Durham Goode <durham@fb.com>
parents: 18766
diff changeset
    64
        s.update([revlog.linkrev(r) for r in brokenset])
5909
f45f7390c1c5 strip: calculate list of extra nodes to save and pass it to changegroupsubset
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5905
diff changeset
    65
13705
73cfb7a5aa56 strip: simplify collectone
Matt Mackall <mpm@selenic.com>
parents: 13702
diff changeset
    66
    collectone(repo.manifest)
73cfb7a5aa56 strip: simplify collectone
Matt Mackall <mpm@selenic.com>
parents: 13702
diff changeset
    67
    for fname in files:
73cfb7a5aa56 strip: simplify collectone
Matt Mackall <mpm@selenic.com>
parents: 13702
diff changeset
    68
        collectone(repo.file(fname))
5909
f45f7390c1c5 strip: calculate list of extra nodes to save and pass it to changegroupsubset
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5905
diff changeset
    69
13705
73cfb7a5aa56 strip: simplify collectone
Matt Mackall <mpm@selenic.com>
parents: 13702
diff changeset
    70
    return s
5909
f45f7390c1c5 strip: calculate list of extra nodes to save and pass it to changegroupsubset
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5905
diff changeset
    71
22057
445472225ccd strip: remove -b/--backup codepaths
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 21064
diff changeset
    72
def strip(ui, repo, nodelist, backup=True, topic='backup'):
445472225ccd strip: remove -b/--backup codepaths
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 21064
diff changeset
    73
445472225ccd strip: remove -b/--backup codepaths
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 21064
diff changeset
    74
    # Simple way to maintain backwards compatibility for this
445472225ccd strip: remove -b/--backup codepaths
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 21064
diff changeset
    75
    # argument.
445472225ccd strip: remove -b/--backup codepaths
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 21064
diff changeset
    76
    if backup in ['none', 'strip']:
445472225ccd strip: remove -b/--backup codepaths
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 21064
diff changeset
    77
        backup = False
445472225ccd strip: remove -b/--backup codepaths
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 21064
diff changeset
    78
18004
747a2f43d5d9 clfilter: strip logic should be unfiltered
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17922
diff changeset
    79
    repo = repo.unfiltered()
18310
4499ba5ac35c localrepo: introduce destroying function
Idan Kamara <idankk86@gmail.com>
parents: 18121
diff changeset
    80
    repo.destroying()
17013
c8eda7bbdcab strip: incrementally update the branchheads cache after a strip
Joshua Redstone <joshua.redstone@fb.com>
parents: 16867
diff changeset
    81
5901
16f4129c19ac repair.py: rename chlog to cl
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5900
diff changeset
    82
    cl = repo.changelog
16237
b5c0c7d0f83f repair: remove undo files after strip
Idan Kamara <idankk86@gmail.com>
parents: 15901
diff changeset
    83
    # TODO handle undo of merge sets
16252
cf17e76be4dd strip: enhance repair.strip to receive a list of nodes (issue3299)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 15901
diff changeset
    84
    if isinstance(nodelist, str):
cf17e76be4dd strip: enhance repair.strip to receive a list of nodes (issue3299)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 15901
diff changeset
    85
        nodelist = [nodelist]
cf17e76be4dd strip: enhance repair.strip to receive a list of nodes (issue3299)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 15901
diff changeset
    86
    striplist = [cl.rev(node) for node in nodelist]
cf17e76be4dd strip: enhance repair.strip to receive a list of nodes (issue3299)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 15901
diff changeset
    87
    striprev = min(striplist)
4702
18e91c9def0c strip: move strip code to a new repair module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    88
6147
53ae5af55db3 repair.py: rewrite a loop, making it cleaner and faster
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5910
diff changeset
    89
    # Some revisions with rev > striprev may not be descendants of striprev.
53ae5af55db3 repair.py: rewrite a loop, making it cleaner and faster
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5910
diff changeset
    90
    # We have to find these revisions and put them in a bundle, so that
53ae5af55db3 repair.py: rewrite a loop, making it cleaner and faster
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5910
diff changeset
    91
    # we can restore them after the truncations.
53ae5af55db3 repair.py: rewrite a loop, making it cleaner and faster
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5910
diff changeset
    92
    # To create the bundle we use repo.changegroupsubset which requires
53ae5af55db3 repair.py: rewrite a loop, making it cleaner and faster
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5910
diff changeset
    93
    # the list of heads and bases of the set of interesting revisions.
53ae5af55db3 repair.py: rewrite a loop, making it cleaner and faster
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5910
diff changeset
    94
    # (head = revision in the set that has no descendant in the set;
53ae5af55db3 repair.py: rewrite a loop, making it cleaner and faster
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5910
diff changeset
    95
    #  base = revision in the set that has no ancestor in the set)
16252
cf17e76be4dd strip: enhance repair.strip to receive a list of nodes (issue3299)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 15901
diff changeset
    96
    tostrip = set(striplist)
cf17e76be4dd strip: enhance repair.strip to receive a list of nodes (issue3299)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 15901
diff changeset
    97
    for rev in striplist:
16867
1093ad1e8903 revlog: descendants(*revs) becomes descendants(revs) (API)
Bryan O'Sullivan <bryano@fb.com>
parents: 16745
diff changeset
    98
        for desc in cl.descendants([rev]):
16252
cf17e76be4dd strip: enhance repair.strip to receive a list of nodes (issue3299)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 15901
diff changeset
    99
            tostrip.add(desc)
13702
ffd370aa050b strip: remove usage of extranodes
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 13362
diff changeset
   100
ffd370aa050b strip: remove usage of extranodes
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 13362
diff changeset
   101
    files = _collectfiles(repo, striprev)
13705
73cfb7a5aa56 strip: simplify collectone
Matt Mackall <mpm@selenic.com>
parents: 13702
diff changeset
   102
    saverevs = _collectbrokencsets(repo, files, striprev)
13702
ffd370aa050b strip: remove usage of extranodes
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 13362
diff changeset
   103
ffd370aa050b strip: remove usage of extranodes
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 13362
diff changeset
   104
    # compute heads
ffd370aa050b strip: remove usage of extranodes
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 13362
diff changeset
   105
    saveheads = set(saverevs)
6750
fb42030d79d6 add __len__ and __iter__ methods to repo and revlog
Matt Mackall <mpm@selenic.com>
parents: 6747
diff changeset
   106
    for r in xrange(striprev + 1, len(cl)):
13702
ffd370aa050b strip: remove usage of extranodes
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 13362
diff changeset
   107
        if r not in tostrip:
ffd370aa050b strip: remove usage of extranodes
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 13362
diff changeset
   108
            saverevs.add(r)
ffd370aa050b strip: remove usage of extranodes
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 13362
diff changeset
   109
            saveheads.difference_update(cl.parentrevs(r))
ffd370aa050b strip: remove usage of extranodes
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 13362
diff changeset
   110
            saveheads.add(r)
ffd370aa050b strip: remove usage of extranodes
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 13362
diff changeset
   111
    saveheads = [cl.node(r) for r in saveheads]
4702
18e91c9def0c strip: move strip code to a new repair module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   112
15386
6051d8e7e133 strip: backout 73307643a09f (issue3077)
Matt Mackall <mpm@selenic.com>
parents: 15068
diff changeset
   113
    # compute base nodes
6051d8e7e133 strip: backout 73307643a09f (issue3077)
Matt Mackall <mpm@selenic.com>
parents: 15068
diff changeset
   114
    if saverevs:
16867
1093ad1e8903 revlog: descendants(*revs) becomes descendants(revs) (API)
Bryan O'Sullivan <bryano@fb.com>
parents: 16745
diff changeset
   115
        descendants = set(cl.descendants(saverevs))
15386
6051d8e7e133 strip: backout 73307643a09f (issue3077)
Matt Mackall <mpm@selenic.com>
parents: 15068
diff changeset
   116
        saverevs.difference_update(descendants)
6051d8e7e133 strip: backout 73307643a09f (issue3077)
Matt Mackall <mpm@selenic.com>
parents: 15068
diff changeset
   117
    savebases = [cl.node(r) for r in saverevs]
16252
cf17e76be4dd strip: enhance repair.strip to receive a list of nodes (issue3299)
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 15901
diff changeset
   118
    stripbases = [cl.node(r) for r in tostrip]
18040
fe8caf28d580 strip: make query to get new bookmark target cheaper
Siddharth Agarwal <sid0@fb.com>
parents: 18004
diff changeset
   119
fe8caf28d580 strip: make query to get new bookmark target cheaper
Siddharth Agarwal <sid0@fb.com>
parents: 18004
diff changeset
   120
    # For a set s, max(parents(s) - s) is the same as max(heads(::s - s)), but
fe8caf28d580 strip: make query to get new bookmark target cheaper
Siddharth Agarwal <sid0@fb.com>
parents: 18004
diff changeset
   121
    # is much faster
fe8caf28d580 strip: make query to get new bookmark target cheaper
Siddharth Agarwal <sid0@fb.com>
parents: 18004
diff changeset
   122
    newbmtarget = repo.revs('max(parents(%ld) - (%ld))', tostrip, tostrip)
17264
ec7b9bec19c9 strip: move bookmarks to nearest ancestor rather than '.'
Augie Fackler <raf@durin42.com>
parents: 17013
diff changeset
   123
    if newbmtarget:
22818
d7b114493315 repair: use `first` instead of direct indexing
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22057
diff changeset
   124
        newbmtarget = repo[newbmtarget.first()].node()
17264
ec7b9bec19c9 strip: move bookmarks to nearest ancestor rather than '.'
Augie Fackler <raf@durin42.com>
parents: 17013
diff changeset
   125
    else:
ec7b9bec19c9 strip: move bookmarks to nearest ancestor rather than '.'
Augie Fackler <raf@durin42.com>
parents: 17013
diff changeset
   126
        newbmtarget = '.'
4702
18e91c9def0c strip: move strip code to a new repair module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   127
13362
ee01d9d84115 bookmarks: move strip support to repair
Matt Mackall <mpm@selenic.com>
parents: 12057
diff changeset
   128
    bm = repo._bookmarks
ee01d9d84115 bookmarks: move strip support to repair
Matt Mackall <mpm@selenic.com>
parents: 12057
diff changeset
   129
    updatebm = []
ee01d9d84115 bookmarks: move strip support to repair
Matt Mackall <mpm@selenic.com>
parents: 12057
diff changeset
   130
    for m in bm:
ee01d9d84115 bookmarks: move strip support to repair
Matt Mackall <mpm@selenic.com>
parents: 12057
diff changeset
   131
        rev = repo[bm[m]].rev()
ee01d9d84115 bookmarks: move strip support to repair
Matt Mackall <mpm@selenic.com>
parents: 12057
diff changeset
   132
        if rev in tostrip:
ee01d9d84115 bookmarks: move strip support to repair
Matt Mackall <mpm@selenic.com>
parents: 12057
diff changeset
   133
            updatebm.append(m)
ee01d9d84115 bookmarks: move strip support to repair
Matt Mackall <mpm@selenic.com>
parents: 12057
diff changeset
   134
4702
18e91c9def0c strip: move strip code to a new repair module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   135
    # create a changegroup for all the branches we need to keep
11197
4bb4895e1693 strip: be quiet about temporary internal bundle
Matt Mackall <mpm@selenic.com>
parents: 10881
diff changeset
   136
    backupfile = None
20979
ad5b61370514 repair: make "strip()" treat bundle files via vfs
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20977
diff changeset
   137
    vfs = repo.vfs
24252
f962692853c0 repair: define explicit local variable, don't reuse a comprehension variable
Mike Edgar <adgar@google.com>
parents: 24170
diff changeset
   138
    node = nodelist[-1]
22057
445472225ccd strip: remove -b/--backup codepaths
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 21064
diff changeset
   139
    if backup:
16388
e03d8a40521f repair: allow giving strip backup a different name
Idan Kamara <idankk86@gmail.com>
parents: 16253
diff changeset
   140
        backupfile = _bundle(repo, stripbases, cl.heads(), node, topic)
20979
ad5b61370514 repair: make "strip()" treat bundle files via vfs
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20977
diff changeset
   141
        repo.ui.status(_("saved backup bundle to %s\n") %
ad5b61370514 repair: make "strip()" treat bundle files via vfs
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20977
diff changeset
   142
                       vfs.join(backupfile))
ad5b61370514 repair: make "strip()" treat bundle files via vfs
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20977
diff changeset
   143
        repo.ui.log("backupbundle", "saved backup bundle to %s\n",
ad5b61370514 repair: make "strip()" treat bundle files via vfs
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20977
diff changeset
   144
                    vfs.join(backupfile))
15386
6051d8e7e133 strip: backout 73307643a09f (issue3077)
Matt Mackall <mpm@selenic.com>
parents: 15068
diff changeset
   145
    if saveheads or savebases:
11791
00cde9bddbe4 repair: do not compress partial bundle if we do not keep it on disk
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 11600
diff changeset
   146
        # do not compress partial bundle if we remove it from disk later
15386
6051d8e7e133 strip: backout 73307643a09f (issue3077)
Matt Mackall <mpm@selenic.com>
parents: 15068
diff changeset
   147
        chgrpfile = _bundle(repo, savebases, saveheads, node, 'temp',
22057
445472225ccd strip: remove -b/--backup codepaths
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 21064
diff changeset
   148
                            compress=False)
4702
18e91c9def0c strip: move strip code to a new repair module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   149
8073
e8a28556a0a8 strip: make repair.strip transactional to avoid repository corruption
Henrik Stuart <henrik.stuart@edlund.dk>
parents: 7361
diff changeset
   150
    mfst = repo.manifest
e8a28556a0a8 strip: make repair.strip transactional to avoid repository corruption
Henrik Stuart <henrik.stuart@edlund.dk>
parents: 7361
diff changeset
   151
25300
678d0bfdd31a repair: forbid strip from inside a transaction
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24863
diff changeset
   152
    curtr = repo.currenttransaction()
678d0bfdd31a repair: forbid strip from inside a transaction
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24863
diff changeset
   153
    if curtr is not None:
678d0bfdd31a repair: forbid strip from inside a transaction
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24863
diff changeset
   154
        del curtr  # avoid carrying reference to transaction for nothing
678d0bfdd31a repair: forbid strip from inside a transaction
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24863
diff changeset
   155
        msg = _('programming error: cannot strip from inside a transaction')
678d0bfdd31a repair: forbid strip from inside a transaction
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24863
diff changeset
   156
        raise util.Abort(msg, hint=_('contact your extension maintainer'))
678d0bfdd31a repair: forbid strip from inside a transaction
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24863
diff changeset
   157
10881
a685011ed38e localrepo: add desc parameter to transaction
Steve Borho <steve@borho.org>
parents: 10263
diff changeset
   158
    tr = repo.transaction("strip")
8073
e8a28556a0a8 strip: make repair.strip transactional to avoid repository corruption
Henrik Stuart <henrik.stuart@edlund.dk>
parents: 7361
diff changeset
   159
    offset = len(tr.entries)
e8a28556a0a8 strip: make repair.strip transactional to avoid repository corruption
Henrik Stuart <henrik.stuart@edlund.dk>
parents: 7361
diff changeset
   160
11197
4bb4895e1693 strip: be quiet about temporary internal bundle
Matt Mackall <mpm@selenic.com>
parents: 10881
diff changeset
   161
    try:
4bb4895e1693 strip: be quiet about temporary internal bundle
Matt Mackall <mpm@selenic.com>
parents: 10881
diff changeset
   162
        tr.startgroup()
4bb4895e1693 strip: be quiet about temporary internal bundle
Matt Mackall <mpm@selenic.com>
parents: 10881
diff changeset
   163
        cl.strip(striprev, tr)
4bb4895e1693 strip: be quiet about temporary internal bundle
Matt Mackall <mpm@selenic.com>
parents: 10881
diff changeset
   164
        mfst.strip(striprev, tr)
4bb4895e1693 strip: be quiet about temporary internal bundle
Matt Mackall <mpm@selenic.com>
parents: 10881
diff changeset
   165
        for fn in files:
4bb4895e1693 strip: be quiet about temporary internal bundle
Matt Mackall <mpm@selenic.com>
parents: 10881
diff changeset
   166
            repo.file(fn).strip(striprev, tr)
4bb4895e1693 strip: be quiet about temporary internal bundle
Matt Mackall <mpm@selenic.com>
parents: 10881
diff changeset
   167
        tr.endgroup()
8073
e8a28556a0a8 strip: make repair.strip transactional to avoid repository corruption
Henrik Stuart <henrik.stuart@edlund.dk>
parents: 7361
diff changeset
   168
11197
4bb4895e1693 strip: be quiet about temporary internal bundle
Matt Mackall <mpm@selenic.com>
parents: 10881
diff changeset
   169
        try:
4bb4895e1693 strip: be quiet about temporary internal bundle
Matt Mackall <mpm@selenic.com>
parents: 10881
diff changeset
   170
            for i in xrange(offset, len(tr.entries)):
4bb4895e1693 strip: be quiet about temporary internal bundle
Matt Mackall <mpm@selenic.com>
parents: 10881
diff changeset
   171
                file, troffset, ignore = tr.entries[i]
23878
37a92908a382 localrepo: remove all external users of localrepo.sopener
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 23835
diff changeset
   172
                repo.svfs(file, 'a').truncate(troffset)
20885
f49d60fa40a5 fncache: clean up fncache during strips
Durham Goode <durham@fb.com>
parents: 20074
diff changeset
   173
                if troffset == 0:
f49d60fa40a5 fncache: clean up fncache during strips
Durham Goode <durham@fb.com>
parents: 20074
diff changeset
   174
                    repo.store.markremoved(file)
11197
4bb4895e1693 strip: be quiet about temporary internal bundle
Matt Mackall <mpm@selenic.com>
parents: 10881
diff changeset
   175
            tr.close()
16705
c2d9ef43ff6c check-code: ignore naked excepts with a "re-raise" comment
Brodie Rao <brodie@sf.io>
parents: 16628
diff changeset
   176
        except: # re-raises
11197
4bb4895e1693 strip: be quiet about temporary internal bundle
Matt Mackall <mpm@selenic.com>
parents: 10881
diff changeset
   177
            tr.abort()
4bb4895e1693 strip: be quiet about temporary internal bundle
Matt Mackall <mpm@selenic.com>
parents: 10881
diff changeset
   178
            raise
4bb4895e1693 strip: be quiet about temporary internal bundle
Matt Mackall <mpm@selenic.com>
parents: 10881
diff changeset
   179
15386
6051d8e7e133 strip: backout 73307643a09f (issue3077)
Matt Mackall <mpm@selenic.com>
parents: 15068
diff changeset
   180
        if saveheads or savebases:
11202
f974fe896921 strip: hide unbundle messages by default
Matt Mackall <mpm@selenic.com>
parents: 11200
diff changeset
   181
            ui.note(_("adding branch\n"))
20979
ad5b61370514 repair: make "strip()" treat bundle files via vfs
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20977
diff changeset
   182
            f = vfs.open(chgrpfile, "rb")
21064
4d9d490d7bbe bundle2: add a ui argument to readbundle
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 21063
diff changeset
   183
            gen = exchange.readbundle(ui, f, chgrpfile, vfs)
11202
f974fe896921 strip: hide unbundle messages by default
Matt Mackall <mpm@selenic.com>
parents: 11200
diff changeset
   184
            if not repo.ui.verbose:
f974fe896921 strip: hide unbundle messages by default
Matt Mackall <mpm@selenic.com>
parents: 11200
diff changeset
   185
                # silence internal shuffling chatter
f974fe896921 strip: hide unbundle messages by default
Matt Mackall <mpm@selenic.com>
parents: 11200
diff changeset
   186
                repo.ui.pushbuffer()
23898
b21c2e0ee8a3 repair: add experimental option to write bundle2 files
Eric Sumner <ericsumner@fb.com>
parents: 23895
diff changeset
   187
            if isinstance(gen, bundle2.unbundle20):
b21c2e0ee8a3 repair: add experimental option to write bundle2 files
Eric Sumner <ericsumner@fb.com>
parents: 23895
diff changeset
   188
                tr = repo.transaction('strip')
24170
fbc4d550a6ab repair: setup hookargs when processing bundle2s
Eric Sumner <ericsumner@fb.com>
parents: 23939
diff changeset
   189
                tr.hookargs = {'source': 'strip',
fbc4d550a6ab repair: setup hookargs when processing bundle2s
Eric Sumner <ericsumner@fb.com>
parents: 23939
diff changeset
   190
                               'url': 'bundle:' + vfs.join(chgrpfile)}
23898
b21c2e0ee8a3 repair: add experimental option to write bundle2 files
Eric Sumner <ericsumner@fb.com>
parents: 23895
diff changeset
   191
                try:
b21c2e0ee8a3 repair: add experimental option to write bundle2 files
Eric Sumner <ericsumner@fb.com>
parents: 23895
diff changeset
   192
                    bundle2.processbundle(repo, gen, lambda: tr)
b21c2e0ee8a3 repair: add experimental option to write bundle2 files
Eric Sumner <ericsumner@fb.com>
parents: 23895
diff changeset
   193
                    tr.close()
b21c2e0ee8a3 repair: add experimental option to write bundle2 files
Eric Sumner <ericsumner@fb.com>
parents: 23895
diff changeset
   194
                finally:
b21c2e0ee8a3 repair: add experimental option to write bundle2 files
Eric Sumner <ericsumner@fb.com>
parents: 23895
diff changeset
   195
                    tr.release()
b21c2e0ee8a3 repair: add experimental option to write bundle2 files
Eric Sumner <ericsumner@fb.com>
parents: 23895
diff changeset
   196
            else:
b21c2e0ee8a3 repair: add experimental option to write bundle2 files
Eric Sumner <ericsumner@fb.com>
parents: 23895
diff changeset
   197
                changegroup.addchangegroup(repo, gen, 'strip',
b21c2e0ee8a3 repair: add experimental option to write bundle2 files
Eric Sumner <ericsumner@fb.com>
parents: 23895
diff changeset
   198
                                           'bundle:' + vfs.join(chgrpfile),
b21c2e0ee8a3 repair: add experimental option to write bundle2 files
Eric Sumner <ericsumner@fb.com>
parents: 23895
diff changeset
   199
                                           True)
11202
f974fe896921 strip: hide unbundle messages by default
Matt Mackall <mpm@selenic.com>
parents: 11200
diff changeset
   200
            if not repo.ui.verbose:
f974fe896921 strip: hide unbundle messages by default
Matt Mackall <mpm@selenic.com>
parents: 11200
diff changeset
   201
                repo.ui.popbuffer()
11197
4bb4895e1693 strip: be quiet about temporary internal bundle
Matt Mackall <mpm@selenic.com>
parents: 10881
diff changeset
   202
            f.close()
13362
ee01d9d84115 bookmarks: move strip support to repair
Matt Mackall <mpm@selenic.com>
parents: 12057
diff changeset
   203
16237
b5c0c7d0f83f repair: remove undo files after strip
Idan Kamara <idankk86@gmail.com>
parents: 15901
diff changeset
   204
        # remove undo files
20975
37cdf1fca1b2 localrepo: make "undofiles()" return list of tuples "(vfs, relative filename)"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20933
diff changeset
   205
        for undovfs, undofile in repo.undofiles():
16237
b5c0c7d0f83f repair: remove undo files after strip
Idan Kamara <idankk86@gmail.com>
parents: 15901
diff changeset
   206
            try:
20975
37cdf1fca1b2 localrepo: make "undofiles()" return list of tuples "(vfs, relative filename)"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20933
diff changeset
   207
                undovfs.unlink(undofile)
16237
b5c0c7d0f83f repair: remove undo files after strip
Idan Kamara <idankk86@gmail.com>
parents: 15901
diff changeset
   208
            except OSError, e:
b5c0c7d0f83f repair: remove undo files after strip
Idan Kamara <idankk86@gmail.com>
parents: 15901
diff changeset
   209
                if e.errno != errno.ENOENT:
20975
37cdf1fca1b2 localrepo: make "undofiles()" return list of tuples "(vfs, relative filename)"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20933
diff changeset
   210
                    ui.warn(_('error removing %s: %s\n') %
37cdf1fca1b2 localrepo: make "undofiles()" return list of tuples "(vfs, relative filename)"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20933
diff changeset
   211
                            (undovfs.join(undofile), str(e)))
16237
b5c0c7d0f83f repair: remove undo files after strip
Idan Kamara <idankk86@gmail.com>
parents: 15901
diff changeset
   212
13362
ee01d9d84115 bookmarks: move strip support to repair
Matt Mackall <mpm@selenic.com>
parents: 12057
diff changeset
   213
        for m in updatebm:
17264
ec7b9bec19c9 strip: move bookmarks to nearest ancestor rather than '.'
Augie Fackler <raf@durin42.com>
parents: 17013
diff changeset
   214
            bm[m] = repo[newbmtarget].node()
17922
7f5dab94e48c bookmarks: introduce a bmstore to manage bookmark persistence
Augie Fackler <raf@durin42.com>
parents: 17796
diff changeset
   215
        bm.write()
16705
c2d9ef43ff6c check-code: ignore naked excepts with a "re-raise" comment
Brodie Rao <brodie@sf.io>
parents: 16628
diff changeset
   216
    except: # re-raises
11197
4bb4895e1693 strip: be quiet about temporary internal bundle
Matt Mackall <mpm@selenic.com>
parents: 10881
diff changeset
   217
        if backupfile:
11600
76454cbc11e4 mark ui.warn strings for translation
Martin Geisler <mg@lazybytes.net>
parents: 11333
diff changeset
   218
            ui.warn(_("strip failed, full bundle stored in '%s'\n")
20979
ad5b61370514 repair: make "strip()" treat bundle files via vfs
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20977
diff changeset
   219
                    % vfs.join(backupfile))
11197
4bb4895e1693 strip: be quiet about temporary internal bundle
Matt Mackall <mpm@selenic.com>
parents: 10881
diff changeset
   220
        elif saveheads:
11600
76454cbc11e4 mark ui.warn strings for translation
Martin Geisler <mg@lazybytes.net>
parents: 11333
diff changeset
   221
            ui.warn(_("strip failed, partial bundle stored in '%s'\n")
20979
ad5b61370514 repair: make "strip()" treat bundle files via vfs
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 20977
diff changeset
   222
                    % vfs.join(chgrpfile))
8073
e8a28556a0a8 strip: make repair.strip transactional to avoid repository corruption
Henrik Stuart <henrik.stuart@edlund.dk>
parents: 7361
diff changeset
   223
        raise
22057
445472225ccd strip: remove -b/--backup codepaths
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 21064
diff changeset
   224
    else:
445472225ccd strip: remove -b/--backup codepaths
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 21064
diff changeset
   225
        if saveheads or savebases:
445472225ccd strip: remove -b/--backup codepaths
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 21064
diff changeset
   226
            # Remove partial backup only if there were no exceptions
445472225ccd strip: remove -b/--backup codepaths
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 21064
diff changeset
   227
            vfs.unlink(chgrpfile)
4702
18e91c9def0c strip: move strip code to a new repair module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   228
18395
904b7109938e destroyed: drop complex branchcache rebuilt logic
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18311
diff changeset
   229
    repo.destroyed()