1
# Copyright (C) 2005, 2006 Canonical Ltd
1
# Copyright (C) 2006-2010 Canonical Ltd
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
29
29
from bzrlib import (
35
from bzrlib.trace import mutter, note
36
from bzrlib.tsort import TopoSorter
34
from bzrlib.trace import mutter
35
from bzrlib.tsort import topo_sort
37
36
from bzrlib.versionedfile import AdapterFactory, FulltextContentFactory
40
def reconcile(dir, other=None):
39
def reconcile(dir, canonicalize_chks=False):
41
40
"""Reconcile the data in dir.
43
42
Currently this is limited to a inventory 'reweave'.
47
46
Directly using Reconciler is recommended for library users that
48
47
desire fine grained control or analysis of the found issues.
50
:param other: another bzrdir to reconcile against.
49
:param canonicalize_chks: Make sure CHKs are in canonical form.
52
reconciler = Reconciler(dir, other=other)
51
reconciler = Reconciler(dir, canonicalize_chks=canonicalize_chks)
53
52
reconciler.reconcile()
56
55
class Reconciler(object):
57
56
"""Reconcilers are used to reconcile existing data."""
59
def __init__(self, dir, other=None):
58
def __init__(self, dir, other=None, canonicalize_chks=False):
60
59
"""Create a Reconciler."""
61
self.canonicalize_chks = canonicalize_chks
63
63
def reconcile(self):
64
64
"""Perform reconciliation.
90
90
# Nothing to check here
91
91
self.fixed_branch_history = None
93
self.pb.note('Reconciling branch %s',
93
ui.ui_factory.note('Reconciling branch %s' % self.branch.base)
95
94
branch_reconciler = self.branch.reconcile(thorough=True)
96
95
self.fixed_branch_history = branch_reconciler.fixed_history
98
97
def _reconcile_repository(self):
99
98
self.repo = self.bzrdir.find_repository()
100
self.pb.note('Reconciling repository %s',
101
self.repo.bzrdir.root_transport.base)
99
ui.ui_factory.note('Reconciling repository %s' %
102
101
self.pb.update("Reconciling repository", 0, 1)
103
repo_reconciler = self.repo.reconcile(thorough=True)
102
if self.canonicalize_chks:
104
self.repo.reconcile_canonicalize_chks
105
except AttributeError:
106
raise errors.BzrError(
107
"%s cannot canonicalize CHKs." % (self.repo,))
108
repo_reconciler = self.repo.reconcile_canonicalize_chks()
110
repo_reconciler = self.repo.reconcile(thorough=True)
104
111
self.inconsistent_parents = repo_reconciler.inconsistent_parents
105
112
self.garbage_inventories = repo_reconciler.garbage_inventories
106
113
if repo_reconciler.aborted:
108
115
'Reconcile aborted: revision index has inconsistent parents.')
110
117
'Run "bzr check" for more details.')
112
self.pb.note('Reconciliation complete.')
119
ui.ui_factory.note('Reconciliation complete.')
115
122
class BranchReconciler(object):
121
128
self.branch = a_branch
123
130
def reconcile(self):
131
operation = cleanup.OperationWithCleanups(self._reconcile)
132
self.add_cleanup = operation.add_cleanup
133
operation.run_simple()
135
def _reconcile(self):
124
136
self.branch.lock_write()
126
self.pb = ui.ui_factory.nested_progress_bar()
128
self._reconcile_steps()
137
self.add_cleanup(self.branch.unlock)
138
self.pb = ui.ui_factory.nested_progress_bar()
139
self.add_cleanup(self.pb.finished)
140
self._reconcile_steps()
134
142
def _reconcile_steps(self):
135
143
self._reconcile_revision_history()
151
159
# set_revision_history, as this will regenerate it again.
152
160
# Not really worth a whole BranchReconciler class just for this,
154
self.pb.note('Fixing last revision info %s => %s',
155
last_revno, len(real_history))
162
ui.ui_factory.note('Fixing last revision info %s => %s' % (
163
last_revno, len(real_history)))
156
164
self.branch.set_last_revision_info(len(real_history),
157
165
last_revision_id)
159
167
self.fixed_history = False
160
self.pb.note('revision_history ok.')
168
ui.ui_factory.note('revision_history ok.')
163
171
class RepoReconciler(object):
193
201
garbage_inventories: The number of inventory objects without revisions
194
202
that were garbage collected.
204
operation = cleanup.OperationWithCleanups(self._reconcile)
205
self.add_cleanup = operation.add_cleanup
206
operation.run_simple()
208
def _reconcile(self):
196
209
self.repo.lock_write()
198
self.pb = ui.ui_factory.nested_progress_bar()
200
self._reconcile_steps()
210
self.add_cleanup(self.repo.unlock)
211
self.pb = ui.ui_factory.nested_progress_bar()
212
self.add_cleanup(self.pb.finished)
213
self._reconcile_steps()
206
215
def _reconcile_steps(self):
207
216
"""Perform the steps to reconcile this repository."""
238
247
# (no garbage inventories or we are not doing a thorough check)
239
248
if (not self.inconsistent_parents and
240
249
(not self.garbage_inventories or not self.thorough)):
241
self.pb.note('Inventory ok.')
250
ui.ui_factory.note('Inventory ok.')
243
252
self.pb.update('Backing up inventory', 0, 0)
244
253
self.repo._backup_inventory()
245
self.pb.note('Backup inventory created.')
254
ui.ui_factory.note('Backup inventory created.')
246
255
new_inventories = self.repo._temp_inventories()
248
257
# we have topological order of revisions and non ghost parents ready.
249
258
self._setup_steps(len(self._rev_graph))
250
revision_keys = [(rev_id,) for rev_id in
251
TopoSorter(self._rev_graph.items()).iter_topo_order()]
259
revision_keys = [(rev_id,) for rev_id in topo_sort(self._rev_graph)]
252
260
stream = self._change_inv_parents(
253
261
self.inventory.get_record_stream(revision_keys, 'unordered', True),
254
262
self._new_inv_parents,
262
270
self.pb.update('Writing weave')
263
271
self.repo._activate_new_inventory()
264
272
self.inventory = None
265
self.pb.note('Inventory regenerated.')
273
ui.ui_factory.note('Inventory regenerated.')
267
275
def _new_inv_parents(self, revision_key):
268
276
"""Lookup ghost-filtered parents for revision_key."""
369
377
self._check_garbage_inventories()
370
378
self.pb.update('Checking unused inventories', 1, 3)
371
379
if not self.garbage_inventories:
372
self.pb.note('Inventory ok.')
380
ui.ui_factory.note('Inventory ok.')
374
382
self.pb.update('Backing up inventory', 0, 0)
375
383
self.repo._backup_inventory()
376
self.pb.note('Backup Inventory created')
384
ui.ui_factory.note('Backup Inventory created')
377
385
# asking for '' should never return a non-empty weave
378
386
new_inventories = self.repo._temp_inventories()
379
387
# we have topological order of revisions and non ghost parents ready.
380
388
graph = self.revisions.get_parent_map(self.revisions.keys())
381
revision_keys = list(TopoSorter(graph).iter_topo_order())
389
revision_keys = topo_sort(graph)
382
390
revision_ids = [key[-1] for key in revision_keys]
383
391
self._setup_steps(len(revision_keys))
384
392
stream = self._change_inv_parents(
393
401
self.pb.update('Writing weave')
394
402
self.repo._activate_new_inventory()
395
403
self.inventory = None
396
self.pb.note('Inventory regenerated.')
404
ui.ui_factory.note('Inventory regenerated.')
398
406
def _fix_text_parents(self):
399
407
"""Fix bad versionedfile parent entries.
495
503
# - lock the names list
496
504
# - perform a customised pack() that regenerates data as needed
497
505
# - unlock the names list
498
# https://bugs.edge.launchpad.net/bzr/+bug/154173
506
# https://bugs.launchpad.net/bzr/+bug/154173
508
def __init__(self, repo, other=None, thorough=False,
509
canonicalize_chks=False):
510
super(PackReconciler, self).__init__(repo, other=other,
512
self.canonicalize_chks = canonicalize_chks
500
514
def _reconcile_steps(self):
501
515
"""Perform the steps to reconcile this repository."""
504
518
collection = self.repo._pack_collection
505
519
collection.ensure_loaded()
506
520
collection.lock_names()
508
packs = collection.all_packs()
509
all_revisions = self.repo.all_revision_ids()
510
total_inventories = len(list(
511
collection.inventory_index.combined_index.iter_all_entries()))
512
if len(all_revisions):
513
new_pack = self.repo._reconcile_pack(collection, packs,
514
".reconcile", all_revisions, self.pb)
515
if new_pack is not None:
516
self._discard_and_save(packs)
521
self.add_cleanup(collection._unlock_names)
522
packs = collection.all_packs()
523
all_revisions = self.repo.all_revision_ids()
524
total_inventories = len(list(
525
collection.inventory_index.combined_index.iter_all_entries()))
526
if len(all_revisions):
527
if self.canonicalize_chks:
528
reconcile_meth = self.repo._canonicalize_chks_pack
518
# only make a new pack when there is data to copy.
530
reconcile_meth = self.repo._reconcile_pack
531
new_pack = reconcile_meth(collection, packs, ".reconcile",
532
all_revisions, self.pb)
533
if new_pack is not None:
519
534
self._discard_and_save(packs)
520
self.garbage_inventories = total_inventories - len(list(
521
collection.inventory_index.combined_index.iter_all_entries()))
523
collection._unlock_names()
536
# only make a new pack when there is data to copy.
537
self._discard_and_save(packs)
538
self.garbage_inventories = total_inventories - len(list(
539
collection.inventory_index.combined_index.iter_all_entries()))
525
541
def _discard_and_save(self, packs):
526
542
"""Discard some packs from the repository.