1
# (C) 2005, 2006 Canonical Limited.
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.
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.
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
17
"""Reconcilers are able to fix some potential data errors in a branch."""
21
import bzrlib.errors as errors
22
import bzrlib.progress
23
from bzrlib.trace import mutter
24
from bzrlib.tsort import TopoSorter
25
import bzrlib.ui as ui
29
"""Reconcile the data in dir.
31
Currently this is limited to a inventory 'reweave'.
33
This is a convenience method, for using a Reconciler object.
35
Directly using Reconciler is recommended for library users that
36
desire fine grained control or analysis of the found issues.
38
reconciler = Reconciler(dir)
39
reconciler.reconcile()
42
class Reconciler(object):
43
"""Reconcilers are used to reconcile existing data.
45
Currently this is limited to a single repository, and consists
46
of an inventory reweave with revision cross-checks.
49
def __init__(self, dir):
53
"""Perform reconciliation.
55
After reconciliation the following attributes document found issues:
56
inconsistent_parents: The number of revisions in the repository whose
57
ancestry was being reported incorrectly.
58
garbage_inventories: The number of inventory objects without revisions
59
that were garbage collected.
61
self.pb = ui.ui_factory.progress_bar()
62
self.repo = self.bzrdir.open_repository()
63
self.repo.lock_write()
65
self.pb.note('Reconciling repository %s',
66
self.repo.bzrdir.root_transport.base)
67
self._reweave_inventory()
70
self.pb.note('Reconciliation complete.')
72
def _reweave_inventory(self):
73
"""Regenerate the inventory weave for the repository from scratch."""
74
self.pb.update('Reading inventory data.')
75
self.inventory = self.repo.get_inventory_weave()
76
# the total set of revisions to process
77
self.pending = set([file_id for file_id in self.repo.revision_store])
79
# total steps = 1 read per revision + one insert into the inventory
80
self.total = len(self.pending) * 2
83
# mapping from revision_id to parents
85
# errors that we detect
86
self.inconsistent_parents = 0
87
# we need the revision id of each revision and its available parents list
88
for rev_id in self.pending:
89
# put a revision into the graph.
90
self._graph_revision(rev_id)
91
# we gc unreferenced inventories too
92
self.garbage_inventories = len(self.inventory.names()) \
93
- len(self._rev_graph)
95
if not self.inconsistent_parents and not self.garbage_inventories:
96
self.pb.note('Inventory ok.')
98
self.repo.control_weaves.put_weave('inventory.backup',
100
self.repo.get_transaction())
101
self.pb.note('Backup Inventory created.')
102
# asking for '' should never return a non-empty weave
103
new_inventory = self.repo.control_weaves.get_weave_or_empty('',
104
self.repo.get_transaction())
106
# we have topological order of revisions and non ghost parents ready.
107
for rev_id in TopoSorter(self._rev_graph.items()).iter_topo_order():
108
parents = self._rev_graph[rev_id]
109
# double check this really is in topological order.
110
unavailable = [p for p in parents if p not in new_inventory]
111
assert len(unavailable) == 0
112
# this entry has all the non ghost parents in the inventory
114
self._reweave_step('adding inventories')
115
new_inventory.add(rev_id, parents, self.inventory.get(rev_id))
117
# if this worked, the set of new_inventory.names should equal
119
assert set(new_inventory.names()) == self.pending
120
self.pb.update('Writing weave')
121
self.repo.control_weaves.put_weave('inventory',
123
self.repo.get_transaction())
124
self.inventory = None
125
self.pb.note('Inventory regenerated.')
127
def _graph_revision(self, rev_id):
128
"""Load a revision into the revision graph."""
129
# pick a random revision
130
# analyse revision id rev_id and put it in the stack.
131
self._reweave_step('loading revisions')
132
rev = self.repo.get_revision(rev_id)
133
assert rev.revision_id == rev_id
135
for parent in rev.parent_ids:
136
if parent in self.inventory:
137
parents.append(parent)
139
mutter('found ghost %s', parent)
140
self._rev_graph[rev_id] = parents
141
if set(self.inventory.parent_names(rev_id)) != set(parents):
142
self.inconsistent_parents += 1
144
def _reweave_step(self, message):
145
"""Mark a single step of regeneration complete."""
146
self.pb.update(message, self.count, self.total)