1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
|
# (C) 2005, 2006 Canonical Limited.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
"""Reconcilers are able to fix some potential data errors in a branch."""
__all__ = ['reconcile', 'Reconciler', 'RepoReconciler']
import bzrlib.branch
import bzrlib.errors as errors
import bzrlib.progress
from bzrlib.trace import mutter
from bzrlib.tsort import TopoSorter
import bzrlib.ui as ui
def reconcile(dir):
"""Reconcile the data in dir.
Currently this is limited to a inventory 'reweave'.
This is a convenience method, for using a Reconciler object.
Directly using Reconciler is recommended for library users that
desire fine grained control or analysis of the found issues.
"""
reconciler = Reconciler(dir)
reconciler.reconcile()
class Reconciler(object):
"""Reconcilers are used to reconcile existing data."""
def __init__(self, dir):
self.bzrdir = dir
def reconcile(self):
"""Perform reconciliation.
After reconciliation the following attributes document found issues:
inconsistent_parents: The number of revisions in the repository whose
ancestry was being reported incorrectly.
garbage_inventories: The number of inventory objects without revisions
that were garbage collected.
"""
self.pb = ui.ui_factory.nested_progress_bar()
try:
self._reconcile()
finally:
self.pb.finished()
def _reconcile(self):
"""Helper function for performing reconciliation."""
self.repo = self.bzrdir.find_repository()
self.pb.note('Reconciling repository %s',
self.repo.bzrdir.root_transport.base)
repo_reconciler = RepoReconciler(self.repo)
repo_reconciler.reconcile()
self.inconsistent_parents = repo_reconciler.inconsistent_parents
self.garbage_inventories = repo_reconciler.garbage_inventories
self.pb.note('Reconciliation complete.')
class RepoReconciler(object):
"""Reconciler that reconciles a repository.
Currently this consists of an inventory reweave with revision cross-checks.
"""
def __init__(self, repo):
self.repo = repo
def reconcile(self):
"""Perform reconciliation.
After reconciliation the following attributes document found issues:
inconsistent_parents: The number of revisions in the repository whose
ancestry was being reported incorrectly.
garbage_inventories: The number of inventory objects without revisions
that were garbage collected.
"""
self.repo.lock_write()
try:
self.pb = ui.ui_factory.nested_progress_bar()
try:
self._reweave_inventory()
finally:
self.pb.finished()
finally:
self.repo.unlock()
def _reweave_inventory(self):
"""Regenerate the inventory weave for the repository from scratch."""
self.pb.update('Reading inventory data.')
self.inventory = self.repo.get_inventory_weave()
# the total set of revisions to process
self.pending = set([file_id for file_id in self.repo.revision_store])
# mapping from revision_id to parents
self._rev_graph = {}
# errors that we detect
self.inconsistent_parents = 0
# we need the revision id of each revision and its available parents list
self._setup_steps(len(self.pending))
for rev_id in self.pending:
# put a revision into the graph.
self._graph_revision(rev_id)
self._check_garbage_inventories()
if not self.inconsistent_parents and not self.garbage_inventories:
self.pb.note('Inventory ok.')
return
self.pb.update('Backing up inventory...', 0, 0)
self.repo.control_weaves.put_weave('inventory.backup',
self.inventory,
self.repo.get_transaction())
self.pb.note('Backup Inventory created.')
# asking for '' should never return a non-empty weave
new_inventory = self.repo.control_weaves.get_weave_or_empty('',
self.repo.get_transaction())
# we have topological order of revisions and non ghost parents ready.
self._setup_steps(len(self._rev_graph))
for rev_id in TopoSorter(self._rev_graph.items()).iter_topo_order():
parents = self._rev_graph[rev_id]
# double check this really is in topological order.
unavailable = [p for p in parents if p not in new_inventory]
assert len(unavailable) == 0
# this entry has all the non ghost parents in the inventory
# file already.
self._reweave_step('adding inventories')
new_inventory.add(rev_id, parents, self.inventory.get(rev_id))
# if this worked, the set of new_inventory.names should equal
# self.pending
assert set(new_inventory.names()) == self.pending
self.pb.update('Writing weave')
self.repo.control_weaves.put_weave('inventory',
new_inventory,
self.repo.get_transaction())
self.inventory = None
self.pb.note('Inventory regenerated.')
def _setup_steps(self, new_total):
"""Setup the markers we need to control the progress bar."""
self.total = new_total
self.count = 0
def _graph_revision(self, rev_id):
"""Load a revision into the revision graph."""
# pick a random revision
# analyse revision id rev_id and put it in the stack.
self._reweave_step('loading revisions')
rev = self.repo.get_revision_reconcile(rev_id)
assert rev.revision_id == rev_id
parents = []
for parent in rev.parent_ids:
if self._parent_is_available(parent):
parents.append(parent)
else:
mutter('found ghost %s', parent)
self._rev_graph[rev_id] = parents
if set(self.inventory.parent_names(rev_id)) != set(parents):
self.inconsistent_parents += 1
mutter('Inconsistent inventory parents: id {%s} '
'inventory claims %r, '
'available parents are %r, '
'unavailable parents are %r',
rev_id,
set(self.inventory.parent_names(rev_id)),
set(parents),
set(rev.parent_ids).difference(set(parents)))
def _check_garbage_inventories(self):
"""Check for garbage inventories which we cannot trust
We cant trust them because their pre-requisite file data may not
be present - all we know is that their revision was not installed.
"""
inventories = set(self.inventory.names())
revisions = set(self._rev_graph.keys())
garbage = inventories.difference(revisions)
self.garbage_inventories = len(garbage)
for revision_id in garbage:
mutter('Garbage inventory {%s} found.', revision_id)
def _parent_is_available(self, parent):
"""True if parent is a fully available revision
A fully available revision has a inventory and a revision object in the
repository.
"""
return (parent in self._rev_graph or
(parent in self.inventory and self.repo.has_revision(parent)))
def _reweave_step(self, message):
"""Mark a single step of regeneration complete."""
self.pb.update(message, self.count, self.total)
self.count += 1
|