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."""
20
__all__ = ['reconcile', 'Reconciler', 'RepoReconciler']
24
import bzrlib.errors as errors
25
import bzrlib.progress
26
from bzrlib.trace import mutter
27
from bzrlib.tsort import TopoSorter
28
import bzrlib.ui as ui
32
"""Reconcile the data in dir.
34
Currently this is limited to a inventory 'reweave'.
36
This is a convenience method, for using a Reconciler object.
38
Directly using Reconciler is recommended for library users that
39
desire fine grained control or analysis of the found issues.
41
reconciler = Reconciler(dir)
42
reconciler.reconcile()
45
class Reconciler(object):
46
"""Reconcilers are used to reconcile existing data."""
48
def __init__(self, dir):
52
"""Perform reconciliation.
54
After reconciliation the following attributes document found issues:
55
inconsistent_parents: The number of revisions in the repository whose
56
ancestry was being reported incorrectly.
57
garbage_inventories: The number of inventory objects without revisions
58
that were garbage collected.
60
self.pb = ui.ui_factory.progress_bar()
61
self.repo = self.bzrdir.find_repository()
62
self.pb.note('Reconciling repository %s',
63
self.repo.bzrdir.root_transport.base)
64
repo_reconciler = RepoReconciler(self.repo)
65
repo_reconciler.reconcile()
66
self.inconsistent_parents = repo_reconciler.inconsistent_parents
67
self.garbage_inventories = repo_reconciler.garbage_inventories
68
self.pb.note('Reconciliation complete.')
71
class RepoReconciler(object):
72
"""Reconciler that reconciles a repository.
74
Currently this consists of an inventory reweave with revision cross-checks.
77
def __init__(self, repo):
81
"""Perform reconciliation.
83
After reconciliation the following attributes document found issues:
84
inconsistent_parents: The number of revisions in the repository whose
85
ancestry was being reported incorrectly.
86
garbage_inventories: The number of inventory objects without revisions
87
that were garbage collected.
89
self.pb = ui.ui_factory.progress_bar()
90
self.repo.lock_write()
92
self._reweave_inventory()
96
def _reweave_inventory(self):
97
"""Regenerate the inventory weave for the repository from scratch."""
98
transaction = self.repo.get_transaction()
99
self.pb.update('Reading inventory data.')
100
self.inventory = self.repo.get_inventory_weave()
101
# the total set of revisions to process
102
self.pending = set([rev_id for rev_id in self.repo._revision_store.all_revision_ids(transaction)])
104
# mapping from revision_id to parents
106
# errors that we detect
107
self.inconsistent_parents = 0
108
# we need the revision id of each revision and its available parents list
109
self._setup_steps(len(self.pending))
110
for rev_id in self.pending:
111
# put a revision into the graph.
112
self._graph_revision(rev_id)
113
# we gc unreferenced inventories too
114
self.garbage_inventories = len(self.inventory.versions()) \
115
- len(self._rev_graph)
117
if not self.inconsistent_parents and not self.garbage_inventories:
118
self.pb.note('Inventory ok.')
120
self.pb.update('Backing up inventory...', 0, 0)
121
self.repo.control_weaves.copy(self.inventory, 'inventory.backup', self.repo.get_transaction())
122
self.pb.note('Backup Inventory created.')
123
# asking for '' should never return a non-empty weave
124
new_inventory = self.repo.control_weaves.get_empty('inventory.new',
125
self.repo.get_transaction())
127
# we have topological order of revisions and non ghost parents ready.
128
self._setup_steps(len(self._rev_graph))
129
for rev_id in TopoSorter(self._rev_graph.items()).iter_topo_order():
130
parents = self._rev_graph[rev_id]
131
# double check this really is in topological order.
132
unavailable = [p for p in parents if p not in new_inventory]
133
assert len(unavailable) == 0
134
# this entry has all the non ghost parents in the inventory
136
self._reweave_step('adding inventories')
137
new_inventory.add_lines(rev_id, parents, self.inventory.get_lines(rev_id))
139
# if this worked, the set of new_inventory.names should equal
141
assert set(new_inventory.versions()) == self.pending
142
self.pb.update('Writing weave')
143
self.repo.control_weaves.copy(new_inventory, 'inventory', self.repo.get_transaction())
144
self.repo.control_weaves.delete('inventory.new', self.repo.get_transaction())
145
self.inventory = None
146
self.pb.note('Inventory regenerated.')
148
def _setup_steps(self, new_total):
149
"""Setup the markers we need to control the progress bar."""
150
self.total = new_total
153
def _graph_revision(self, rev_id):
154
"""Load a revision into the revision graph."""
155
# pick a random revision
156
# analyse revision id rev_id and put it in the stack.
157
self._reweave_step('loading revisions')
158
rev = self.repo.get_revision_reconcile(rev_id)
159
assert rev.revision_id == rev_id
161
for parent in rev.parent_ids:
162
if self._parent_is_available(parent):
163
parents.append(parent)
165
mutter('found ghost %s', parent)
166
self._rev_graph[rev_id] = parents
167
if set(self.inventory.get_parents(rev_id)) != set(parents):
168
self.inconsistent_parents += 1
170
def _parent_is_available(self, parent):
171
"""True if parent is a fully available revision
173
A fully available revision has a inventory and a revision object in the
176
return (parent in self._rev_graph or
177
(parent in self.inventory and self.repo.has_revision(parent)))
179
def _reweave_step(self, message):
180
"""Mark a single step of regeneration complete."""
181
self.pb.update(message, self.count, self.total)