~abentley/bzrtools/bzrtools.dev

274 by Aaron Bentley
Added force-reweave-inventory from Daniel Silverstone
1
# (C) 2005 Canonical
2
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
17
"""Plugin to force-reweave the inventory of a branch.
18
19
This makes sure that the inventory weave's DAG of ancestry is correct so that
20
attempts to fetch the branch over http, or certain merge operations cope
21
correctly.
22
23
This is most likely needed if you have used fetch-ghosts from bzrlib to
24
resolve ghosts after a baz (or otherwise) import and you get bizarre behaviour
25
when either exporting over http or when merging from other translated branches.
26
"""
27
28
from bzrlib.commands import Command
29
from bzrlib.weavefile import write_weave_v5 as w5
30
31
import bzrlib.branch
32
import bzrlib.progress
33
34
import os
35
277 by Aaron Bentley
Renamed force-reweave-inventory to fix
36
class cmd_fix(Command):
274 by Aaron Bentley
Added force-reweave-inventory from Daniel Silverstone
37
    """Force-reweave the inventory in this branch.
38
39
    The branch URL *MUST* be on the local filesystem."""
40
    takes_args = ['branch_dir?']
41
42
    def run(self, branch_dir="."):
43
        branch = bzrlib.branch.Branch.open(branch_dir)
44
        branch.lock_write()
45
        try:
46
            self._do_reweave(branch, branch_dir)
47
        finally:
48
            branch.unlock()
49
275.1.3 by Daniel Silverstone
Fix up fetch_ghosts to lock the branches, and to invoke bzr fix if it fetches any ghosts into the tree
50
    @staticmethod
51
    def _do_reweave(branch, branch_dir):
286.1.1 by Aaron Bentley
Updates to match API changes
52
        inventory_weave = branch._get_inventory_weave()
274 by Aaron Bentley
Added force-reweave-inventory from Daniel Silverstone
53
54
        new_weave = bzrlib.weave.Weave()
55
56
        pending = list(inventory_weave.iter_names())
57
        total = len(pending)
58
        progress = bzrlib.progress.ProgressBar()
59
60
        while pending:
61
            index = pending.pop(0)
62
            done = total - len(pending)
63
            progress.update('re-weaving', done, total)
64
            rev = branch.get_revision(index)
65
            parents = []
66
            for parent in rev.parent_ids:
67
                if parent in inventory_weave:
68
                    parents.append(parent)
69
            unavailable = [p for p in parents if p not in new_weave]
70
            if len(unavailable) == 0:
71
                new_weave.add(index, parents, inventory_weave.get(index))
72
            else:
73
                pending.append(index)
74
75
                
76
        progress.update('Writing weave')
77
276 by Aaron Bentley
Merged hardlink-safe force-reweave-inventory
78
        out_file_name = os.path.join(branch_dir, ".bzr", "inventory.weave")
79
        f = open(out_file_name+".tmp", "w")
274 by Aaron Bentley
Added force-reweave-inventory from Daniel Silverstone
80
        w5(new_weave, f)
81
        f.close()
276 by Aaron Bentley
Merged hardlink-safe force-reweave-inventory
82
        os.rename(out_file_name+".tmp", out_file_name)
274 by Aaron Bentley
Added force-reweave-inventory from Daniel Silverstone
83
84
        progress.clear()