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
34
from bzrlib.trace import mutter
36
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.
97
97
def _reconcile_repository(self):
98
98
self.repo = self.bzrdir.find_repository()
99
99
ui.ui_factory.note('Reconciling repository %s' %
100
self.repo.bzrdir.root_transport.base)
101
101
self.pb.update("Reconciling repository", 0, 1)
102
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)
103
111
self.inconsistent_parents = repo_reconciler.inconsistent_parents
104
112
self.garbage_inventories = repo_reconciler.garbage_inventories
105
113
if repo_reconciler.aborted:
120
128
self.branch = a_branch
122
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):
123
136
self.branch.lock_write()
125
self.pb = ui.ui_factory.nested_progress_bar()
127
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()
133
142
def _reconcile_steps(self):
134
143
self._reconcile_revision_history()
192
201
garbage_inventories: The number of inventory objects without revisions
193
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):
195
209
self.repo.lock_write()
197
self.pb = ui.ui_factory.nested_progress_bar()
199
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()
205
215
def _reconcile_steps(self):
206
216
"""Perform the steps to reconcile this repository."""
493
503
# - lock the names list
494
504
# - perform a customised pack() that regenerates data as needed
495
505
# - unlock the names list
496
# 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
498
514
def _reconcile_steps(self):
499
515
"""Perform the steps to reconcile this repository."""
502
518
collection = self.repo._pack_collection
503
519
collection.ensure_loaded()
504
520
collection.lock_names()
506
packs = collection.all_packs()
507
all_revisions = self.repo.all_revision_ids()
508
total_inventories = len(list(
509
collection.inventory_index.combined_index.iter_all_entries()))
510
if len(all_revisions):
511
new_pack = self.repo._reconcile_pack(collection, packs,
512
".reconcile", all_revisions, self.pb)
513
if new_pack is not None:
514
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
516
# 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:
517
534
self._discard_and_save(packs)
518
self.garbage_inventories = total_inventories - len(list(
519
collection.inventory_index.combined_index.iter_all_entries()))
521
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()))
523
541
def _discard_and_save(self, packs):
524
542
"""Discard some packs from the repository.