/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/reconcile.py

  • Committer: Robert Collins
  • Date: 2010-05-06 11:08:10 UTC
  • mto: This revision was merged to the branch mainline in revision 5223.
  • Revision ID: robertc@robertcollins.net-20100506110810-h3j07fh5gmw54s25
Cleaner matcher matching revised unlocking protocol.

Show diffs side-by-side

added added

removed removed

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