~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/reconcile.py

  • Committer: Daniel Watkins
  • Date: 2009-02-08 14:26:35 UTC
  • mto: (3989.1.1 bzr.ab.integration)
  • mto: This revision was merged to the branch mainline in revision 3990.
  • Revision ID: daniel@daniel-watkins.co.uk-20090208142635-zyzycxxz7i6p3dih
RevertedĀ someĀ irrelevantĀ changes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
17
"""Reconcilers are able to fix some potential data errors in a branch."""
18
18
 
27
27
 
28
28
 
29
29
from bzrlib import (
30
 
    cleanup,
31
30
    errors,
32
31
    ui,
33
32
    repository,
 
33
    repofmt,
34
34
    )
35
 
from bzrlib.trace import mutter
36
 
from bzrlib.tsort import topo_sort
 
35
from bzrlib.trace import mutter, note
 
36
from bzrlib.tsort import TopoSorter
37
37
from bzrlib.versionedfile import AdapterFactory, FulltextContentFactory
38
38
 
39
39
 
62
62
 
63
63
    def reconcile(self):
64
64
        """Perform reconciliation.
65
 
 
 
65
        
66
66
        After reconciliation the following attributes document found issues:
67
67
        inconsistent_parents: The number of revisions in the repository whose
68
68
                              ancestry was being reported incorrectly.
90
90
            # Nothing to check here
91
91
            self.fixed_branch_history = None
92
92
            return
93
 
        ui.ui_factory.note('Reconciling branch %s' % self.branch.base)
 
93
        self.pb.note('Reconciling branch %s',
 
94
                     self.branch.base)
94
95
        branch_reconciler = self.branch.reconcile(thorough=True)
95
96
        self.fixed_branch_history = branch_reconciler.fixed_history
96
97
 
97
98
    def _reconcile_repository(self):
98
99
        self.repo = self.bzrdir.find_repository()
99
 
        ui.ui_factory.note('Reconciling repository %s' %
100
 
            self.repo.bzrdir.root_transport.base)
 
100
        self.pb.note('Reconciling repository %s',
 
101
                     self.repo.bzrdir.root_transport.base)
101
102
        self.pb.update("Reconciling repository", 0, 1)
102
103
        repo_reconciler = self.repo.reconcile(thorough=True)
103
104
        self.inconsistent_parents = repo_reconciler.inconsistent_parents
104
105
        self.garbage_inventories = repo_reconciler.garbage_inventories
105
106
        if repo_reconciler.aborted:
106
 
            ui.ui_factory.note(
 
107
            self.pb.note(
107
108
                'Reconcile aborted: revision index has inconsistent parents.')
108
 
            ui.ui_factory.note(
 
109
            self.pb.note(
109
110
                'Run "bzr check" for more details.')
110
111
        else:
111
 
            ui.ui_factory.note('Reconciliation complete.')
 
112
            self.pb.note('Reconciliation complete.')
112
113
 
113
114
 
114
115
class BranchReconciler(object):
120
121
        self.branch = a_branch
121
122
 
122
123
    def reconcile(self):
123
 
        operation = cleanup.OperationWithCleanups(self._reconcile)
124
 
        self.add_cleanup = operation.add_cleanup
125
 
        operation.run_simple()
126
 
 
127
 
    def _reconcile(self):
128
124
        self.branch.lock_write()
129
 
        self.add_cleanup(self.branch.unlock)
130
 
        self.pb = ui.ui_factory.nested_progress_bar()
131
 
        self.add_cleanup(self.pb.finished)
132
 
        self._reconcile_steps()
 
125
        try:
 
126
            self.pb = ui.ui_factory.nested_progress_bar()
 
127
            try:
 
128
                self._reconcile_steps()
 
129
            finally:
 
130
                self.pb.finished()
 
131
        finally:
 
132
            self.branch.unlock()
133
133
 
134
134
    def _reconcile_steps(self):
135
135
        self._reconcile_revision_history()
137
137
    def _reconcile_revision_history(self):
138
138
        repo = self.branch.repository
139
139
        last_revno, last_revision_id = self.branch.last_revision_info()
140
 
        real_history = []
141
 
        try:
142
 
            for revid in repo.iter_reverse_revision_history(
143
 
                    last_revision_id):
144
 
                real_history.append(revid)
145
 
        except errors.RevisionNotPresent:
146
 
            pass # Hit a ghost left hand parent
 
140
        real_history = list(repo.iter_reverse_revision_history(
 
141
                                last_revision_id))
147
142
        real_history.reverse()
148
143
        if last_revno != len(real_history):
149
144
            self.fixed_history = True
151
146
            # set_revision_history, as this will regenerate it again.
152
147
            # Not really worth a whole BranchReconciler class just for this,
153
148
            # though.
154
 
            ui.ui_factory.note('Fixing last revision info %s => %s' % (
155
 
                 last_revno, len(real_history)))
 
149
            self.pb.note('Fixing last revision info %s => %s',
 
150
                         last_revno, len(real_history))
156
151
            self.branch.set_last_revision_info(len(real_history),
157
152
                                               last_revision_id)
158
153
        else:
159
154
            self.fixed_history = False
160
 
            ui.ui_factory.note('revision_history ok.')
 
155
            self.pb.note('revision_history ok.')
161
156
 
162
157
 
163
158
class RepoReconciler(object):
164
159
    """Reconciler that reconciles a repository.
165
160
 
166
161
    The goal of repository reconciliation is to make any derived data
167
 
    consistent with the core data committed by a user. This can involve
 
162
    consistent with the core data committed by a user. This can involve 
168
163
    reindexing, or removing unreferenced data if that can interfere with
169
164
    queries in a given repository.
170
165
 
186
181
 
187
182
    def reconcile(self):
188
183
        """Perform reconciliation.
189
 
 
 
184
        
190
185
        After reconciliation the following attributes document found issues:
191
186
        inconsistent_parents: The number of revisions in the repository whose
192
187
                              ancestry was being reported incorrectly.
193
188
        garbage_inventories: The number of inventory objects without revisions
194
189
                             that were garbage collected.
195
190
        """
196
 
        operation = cleanup.OperationWithCleanups(self._reconcile)
197
 
        self.add_cleanup = operation.add_cleanup
198
 
        operation.run_simple()
199
 
 
200
 
    def _reconcile(self):
201
191
        self.repo.lock_write()
202
 
        self.add_cleanup(self.repo.unlock)
203
 
        self.pb = ui.ui_factory.nested_progress_bar()
204
 
        self.add_cleanup(self.pb.finished)
205
 
        self._reconcile_steps()
 
192
        try:
 
193
            self.pb = ui.ui_factory.nested_progress_bar()
 
194
            try:
 
195
                self._reconcile_steps()
 
196
            finally:
 
197
                self.pb.finished()
 
198
        finally:
 
199
            self.repo.unlock()
206
200
 
207
201
    def _reconcile_steps(self):
208
202
        """Perform the steps to reconcile this repository."""
210
204
 
211
205
    def _reweave_inventory(self):
212
206
        """Regenerate the inventory weave for the repository from scratch.
213
 
 
214
 
        This is a smart function: it will only do the reweave if doing it
 
207
        
 
208
        This is a smart function: it will only do the reweave if doing it 
215
209
        will correct data issues. The self.thorough flag controls whether
216
210
        only data-loss causing issues (!self.thorough) or all issues
217
211
        (self.thorough) are treated as requiring the reweave.
219
213
        # local because needing to know about WeaveFile is a wart we want to hide
220
214
        from bzrlib.weave import WeaveFile, Weave
221
215
        transaction = self.repo.get_transaction()
222
 
        self.pb.update('Reading inventory data')
 
216
        self.pb.update('Reading inventory data.')
223
217
        self.inventory = self.repo.inventories
224
218
        self.revisions = self.repo.revisions
225
219
        # the total set of revisions to process
235
229
            # put a revision into the graph.
236
230
            self._graph_revision(rev_id)
237
231
        self._check_garbage_inventories()
238
 
        # if there are no inconsistent_parents and
 
232
        # if there are no inconsistent_parents and 
239
233
        # (no garbage inventories or we are not doing a thorough check)
240
 
        if (not self.inconsistent_parents and
 
234
        if (not self.inconsistent_parents and 
241
235
            (not self.garbage_inventories or not self.thorough)):
242
 
            ui.ui_factory.note('Inventory ok.')
 
236
            self.pb.note('Inventory ok.')
243
237
            return
244
 
        self.pb.update('Backing up inventory', 0, 0)
 
238
        self.pb.update('Backing up inventory...', 0, 0)
245
239
        self.repo._backup_inventory()
246
 
        ui.ui_factory.note('Backup inventory created.')
 
240
        self.pb.note('Backup Inventory created.')
247
241
        new_inventories = self.repo._temp_inventories()
248
242
 
249
243
        # we have topological order of revisions and non ghost parents ready.
250
244
        self._setup_steps(len(self._rev_graph))
251
 
        revision_keys = [(rev_id,) for rev_id in topo_sort(self._rev_graph)]
 
245
        revision_keys = [(rev_id,) for rev_id in
 
246
            TopoSorter(self._rev_graph.items()).iter_topo_order()]
252
247
        stream = self._change_inv_parents(
253
248
            self.inventory.get_record_stream(revision_keys, 'unordered', True),
254
249
            self._new_inv_parents,
262
257
        self.pb.update('Writing weave')
263
258
        self.repo._activate_new_inventory()
264
259
        self.inventory = None
265
 
        ui.ui_factory.note('Inventory regenerated.')
 
260
        self.pb.note('Inventory regenerated.')
266
261
 
267
262
    def _new_inv_parents(self, revision_key):
268
263
        """Lookup ghost-filtered parents for revision_key."""
356
351
    def _load_indexes(self):
357
352
        """Load indexes for the reconciliation."""
358
353
        self.transaction = self.repo.get_transaction()
359
 
        self.pb.update('Reading indexes', 0, 2)
 
354
        self.pb.update('Reading indexes.', 0, 2)
360
355
        self.inventory = self.repo.inventories
361
 
        self.pb.update('Reading indexes', 1, 2)
 
356
        self.pb.update('Reading indexes.', 1, 2)
362
357
        self.repo._check_for_inconsistent_revision_parents()
363
358
        self.revisions = self.repo.revisions
364
 
        self.pb.update('Reading indexes', 2, 2)
 
359
        self.pb.update('Reading indexes.', 2, 2)
365
360
 
366
361
    def _gc_inventory(self):
367
362
        """Remove inventories that are not referenced from the revision store."""
368
 
        self.pb.update('Checking unused inventories', 0, 1)
 
363
        self.pb.update('Checking unused inventories.', 0, 1)
369
364
        self._check_garbage_inventories()
370
 
        self.pb.update('Checking unused inventories', 1, 3)
 
365
        self.pb.update('Checking unused inventories.', 1, 3)
371
366
        if not self.garbage_inventories:
372
 
            ui.ui_factory.note('Inventory ok.')
 
367
            self.pb.note('Inventory ok.')
373
368
            return
374
 
        self.pb.update('Backing up inventory', 0, 0)
 
369
        self.pb.update('Backing up inventory...', 0, 0)
375
370
        self.repo._backup_inventory()
376
 
        ui.ui_factory.note('Backup Inventory created')
 
371
        self.pb.note('Backup Inventory created.')
377
372
        # asking for '' should never return a non-empty weave
378
373
        new_inventories = self.repo._temp_inventories()
379
374
        # we have topological order of revisions and non ghost parents ready.
380
375
        graph = self.revisions.get_parent_map(self.revisions.keys())
381
 
        revision_keys = topo_sort(graph)
 
376
        revision_keys = list(TopoSorter(graph).iter_topo_order())
382
377
        revision_ids = [key[-1] for key in revision_keys]
383
378
        self._setup_steps(len(revision_keys))
384
379
        stream = self._change_inv_parents(
393
388
        self.pb.update('Writing weave')
394
389
        self.repo._activate_new_inventory()
395
390
        self.inventory = None
396
 
        ui.ui_factory.note('Inventory regenerated.')
 
391
        self.pb.note('Inventory regenerated.')
397
392
 
398
393
    def _fix_text_parents(self):
399
394
        """Fix bad versionedfile parent entries.
504
499
        collection = self.repo._pack_collection
505
500
        collection.ensure_loaded()
506
501
        collection.lock_names()
507
 
        self.add_cleanup(collection._unlock_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:
 
502
        try:
 
503
            packs = collection.all_packs()
 
504
            all_revisions = self.repo.all_revision_ids()
 
505
            total_inventories = len(list(
 
506
                collection.inventory_index.combined_index.iter_all_entries()))
 
507
            if len(all_revisions):
 
508
                self._packer = repofmt.pack_repo.ReconcilePacker(
 
509
                    collection, packs, ".reconcile", all_revisions)
 
510
                new_pack = self._packer.pack(pb=self.pb)
 
511
                if new_pack is not None:
 
512
                    self._discard_and_save(packs)
 
513
            else:
 
514
                # only make a new pack when there is data to copy.
516
515
                self._discard_and_save(packs)
517
 
        else:
518
 
            # only make a new pack when there is data to copy.
519
 
            self._discard_and_save(packs)
520
 
        self.garbage_inventories = total_inventories - len(list(
521
 
            collection.inventory_index.combined_index.iter_all_entries()))
 
516
            self.garbage_inventories = total_inventories - len(list(
 
517
                collection.inventory_index.combined_index.iter_all_entries()))
 
518
        finally:
 
519
            collection._unlock_names()
522
520
 
523
521
    def _discard_and_save(self, packs):
524
522
        """Discard some packs from the repository.