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