~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/reconcile.py

  • Committer: John Arbash Meinel
  • Date: 2010-01-05 04:30:07 UTC
  • mfrom: (4932 +trunk)
  • mto: This revision was merged to the branch mainline in revision 4934.
  • Revision ID: john@arbash-meinel.com-20100105043007-ehgbldqd3q0gtyws
Merge bzr.dev, resolve conflicts.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2010 Canonical Ltd
 
1
# Copyright (C) 2005, 2006 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
27
27
 
28
28
 
29
29
from bzrlib import (
30
 
    cleanup,
31
30
    errors,
32
 
    revision as _mod_revision,
33
31
    ui,
 
32
    repository,
 
33
    repofmt,
34
34
    )
35
 
from bzrlib.trace import mutter
 
35
from bzrlib.trace import mutter, note
36
36
from bzrlib.tsort import topo_sort
37
37
from bzrlib.versionedfile import AdapterFactory, FulltextContentFactory
38
38
 
39
39
 
40
 
def reconcile(dir, canonicalize_chks=False):
 
40
def reconcile(dir, other=None):
41
41
    """Reconcile the data in dir.
42
42
 
43
43
    Currently this is limited to a inventory 'reweave'.
47
47
    Directly using Reconciler is recommended for library users that
48
48
    desire fine grained control or analysis of the found issues.
49
49
 
50
 
    :param canonicalize_chks: Make sure CHKs are in canonical form.
 
50
    :param other: another bzrdir to reconcile against.
51
51
    """
52
 
    reconciler = Reconciler(dir, canonicalize_chks=canonicalize_chks)
 
52
    reconciler = Reconciler(dir, other=other)
53
53
    reconciler.reconcile()
54
54
 
55
55
 
56
56
class Reconciler(object):
57
57
    """Reconcilers are used to reconcile existing data."""
58
58
 
59
 
    def __init__(self, dir, other=None, canonicalize_chks=False):
 
59
    def __init__(self, dir, other=None):
60
60
        """Create a Reconciler."""
61
61
        self.bzrdir = dir
62
 
        self.canonicalize_chks = canonicalize_chks
63
62
 
64
63
    def reconcile(self):
65
64
        """Perform reconciliation.
66
65
 
67
66
        After reconciliation the following attributes document found issues:
68
 
 
69
 
        * `inconsistent_parents`: The number of revisions in the repository
70
 
          whose ancestry was being reported incorrectly.
71
 
        * `garbage_inventories`: The number of inventory objects without
72
 
          revisions that were garbage collected.
73
 
        * `fixed_branch_history`: None if there was no branch, False if the
74
 
          branch history was correct, True if the branch history needed to be
75
 
          re-normalized.
 
67
        inconsistent_parents: The number of revisions in the repository whose
 
68
                              ancestry was being reported incorrectly.
 
69
        garbage_inventories: The number of inventory objects without revisions
 
70
                             that were garbage collected.
 
71
        fixed_branch_history: None if there was no branch, False if the branch
 
72
                              history was correct, True if the branch history
 
73
                              needed to be re-normalized.
76
74
        """
77
75
        self.pb = ui.ui_factory.nested_progress_bar()
78
76
        try:
99
97
    def _reconcile_repository(self):
100
98
        self.repo = self.bzrdir.find_repository()
101
99
        ui.ui_factory.note('Reconciling repository %s' %
102
 
            self.repo.user_url)
 
100
            self.repo.bzrdir.root_transport.base)
103
101
        self.pb.update("Reconciling repository", 0, 1)
104
 
        if self.canonicalize_chks:
105
 
            try:
106
 
                self.repo.reconcile_canonicalize_chks
107
 
            except AttributeError:
108
 
                raise errors.BzrError(
109
 
                    "%s cannot canonicalize CHKs." % (self.repo,))
110
 
            repo_reconciler = self.repo.reconcile_canonicalize_chks()
111
 
        else:
112
 
            repo_reconciler = self.repo.reconcile(thorough=True)
 
102
        repo_reconciler = self.repo.reconcile(thorough=True)
113
103
        self.inconsistent_parents = repo_reconciler.inconsistent_parents
114
104
        self.garbage_inventories = repo_reconciler.garbage_inventories
115
105
        if repo_reconciler.aborted:
130
120
        self.branch = a_branch
131
121
 
132
122
    def reconcile(self):
133
 
        operation = cleanup.OperationWithCleanups(self._reconcile)
134
 
        self.add_cleanup = operation.add_cleanup
135
 
        operation.run_simple()
136
 
 
137
 
    def _reconcile(self):
138
123
        self.branch.lock_write()
139
 
        self.add_cleanup(self.branch.unlock)
140
 
        self.pb = ui.ui_factory.nested_progress_bar()
141
 
        self.add_cleanup(self.pb.finished)
142
 
        self._reconcile_steps()
 
124
        try:
 
125
            self.pb = ui.ui_factory.nested_progress_bar()
 
126
            try:
 
127
                self._reconcile_steps()
 
128
            finally:
 
129
                self.pb.finished()
 
130
        finally:
 
131
            self.branch.unlock()
143
132
 
144
133
    def _reconcile_steps(self):
145
134
        self._reconcile_revision_history()
146
135
 
147
136
    def _reconcile_revision_history(self):
 
137
        repo = self.branch.repository
148
138
        last_revno, last_revision_id = self.branch.last_revision_info()
149
139
        real_history = []
150
 
        graph = self.branch.repository.get_graph()
151
140
        try:
152
 
            for revid in graph.iter_lefthand_ancestry(
153
 
                    last_revision_id, (_mod_revision.NULL_REVISION,)):
 
141
            for revid in repo.iter_reverse_revision_history(
 
142
                    last_revision_id):
154
143
                real_history.append(revid)
155
144
        except errors.RevisionNotPresent:
156
145
            pass # Hit a ghost left hand parent
198
187
        """Perform reconciliation.
199
188
 
200
189
        After reconciliation the following attributes document found issues:
201
 
 
202
 
        * `inconsistent_parents`: The number of revisions in the repository
203
 
          whose ancestry was being reported incorrectly.
204
 
        * `garbage_inventories`: The number of inventory objects without
205
 
          revisions that were garbage collected.
 
190
        inconsistent_parents: The number of revisions in the repository whose
 
191
                              ancestry was being reported incorrectly.
 
192
        garbage_inventories: The number of inventory objects without revisions
 
193
                             that were garbage collected.
206
194
        """
207
 
        operation = cleanup.OperationWithCleanups(self._reconcile)
208
 
        self.add_cleanup = operation.add_cleanup
209
 
        operation.run_simple()
210
 
 
211
 
    def _reconcile(self):
212
195
        self.repo.lock_write()
213
 
        self.add_cleanup(self.repo.unlock)
214
 
        self.pb = ui.ui_factory.nested_progress_bar()
215
 
        self.add_cleanup(self.pb.finished)
216
 
        self._reconcile_steps()
 
196
        try:
 
197
            self.pb = ui.ui_factory.nested_progress_bar()
 
198
            try:
 
199
                self._reconcile_steps()
 
200
            finally:
 
201
                self.pb.finished()
 
202
        finally:
 
203
            self.repo.unlock()
217
204
 
218
205
    def _reconcile_steps(self):
219
206
        """Perform the steps to reconcile this repository."""
227
214
        only data-loss causing issues (!self.thorough) or all issues
228
215
        (self.thorough) are treated as requiring the reweave.
229
216
        """
 
217
        # local because needing to know about WeaveFile is a wart we want to hide
 
218
        from bzrlib.weave import WeaveFile, Weave
230
219
        transaction = self.repo.get_transaction()
231
220
        self.pb.update('Reading inventory data')
232
221
        self.inventory = self.repo.inventories
504
493
    #  - lock the names list
505
494
    #  - perform a customised pack() that regenerates data as needed
506
495
    #  - unlock the names list
507
 
    # https://bugs.launchpad.net/bzr/+bug/154173
508
 
 
509
 
    def __init__(self, repo, other=None, thorough=False,
510
 
            canonicalize_chks=False):
511
 
        super(PackReconciler, self).__init__(repo, other=other,
512
 
            thorough=thorough)
513
 
        self.canonicalize_chks = canonicalize_chks
 
496
    # https://bugs.edge.launchpad.net/bzr/+bug/154173
514
497
 
515
498
    def _reconcile_steps(self):
516
499
        """Perform the steps to reconcile this repository."""
519
502
        collection = self.repo._pack_collection
520
503
        collection.ensure_loaded()
521
504
        collection.lock_names()
522
 
        self.add_cleanup(collection._unlock_names)
523
 
        packs = collection.all_packs()
524
 
        all_revisions = self.repo.all_revision_ids()
525
 
        total_inventories = len(list(
526
 
            collection.inventory_index.combined_index.iter_all_entries()))
527
 
        if len(all_revisions):
528
 
            if self.canonicalize_chks:
529
 
                reconcile_meth = self.repo._canonicalize_chks_pack
 
505
        try:
 
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)
530
515
            else:
531
 
                reconcile_meth = self.repo._reconcile_pack
532
 
            new_pack = reconcile_meth(collection, packs, ".reconcile",
533
 
                all_revisions, self.pb)
534
 
            if new_pack is not None:
 
516
                # only make a new pack when there is data to copy.
535
517
                self._discard_and_save(packs)
536
 
        else:
537
 
            # only make a new pack when there is data to copy.
538
 
            self._discard_and_save(packs)
539
 
        self.garbage_inventories = total_inventories - len(list(
540
 
            collection.inventory_index.combined_index.iter_all_entries()))
 
518
            self.garbage_inventories = total_inventories - len(list(
 
519
                collection.inventory_index.combined_index.iter_all_entries()))
 
520
        finally:
 
521
            collection._unlock_names()
541
522
 
542
523
    def _discard_and_save(self, packs):
543
524
        """Discard some packs from the repository.