~bzr-pqm/bzr/bzr.dev

2052.3.1 by John Arbash Meinel
Add tests to cleanup the copyright of all source files
1
# Copyright (C) 2005, 2006 Canonical Ltd
1570.1.2 by Robert Collins
Import bzrtools' 'fix' command as 'bzr reconcile.'
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
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
16
1570.1.7 by Robert Collins
Replace the slow topo_sort routine with a much faster one for non trivial datasets.
17
"""Reconcilers are able to fix some potential data errors in a branch."""
1570.1.2 by Robert Collins
Import bzrtools' 'fix' command as 'bzr reconcile.'
18
19
2592.3.80 by Robert Collins
Make reconcile work, and pass tests.
20
__all__ = [
21
    'KnitReconciler',
22
    'PackReconciler',
23
    'reconcile',
24
    'Reconciler',
25
    'RepoReconciler',
26
    ]
1570.1.14 by Robert Collins
Enforce repository consistency during 'fetch' operations.
27
28
2745.6.11 by Aaron Bentley
Fix knit file parents to follow parentage from revision/inventory XML
29
from bzrlib import (
2745.6.16 by Aaron Bentley
Update from review
30
    errors,
2745.6.11 by Aaron Bentley
Fix knit file parents to follow parentage from revision/inventory XML
31
    ui,
32
    repository,
2951.1.3 by Robert Collins
Partial support for native reconcile with packs.
33
    repofmt,
2745.6.11 by Aaron Bentley
Fix knit file parents to follow parentage from revision/inventory XML
34
    )
2819.2.5 by Andrew Bennetts
Make reconcile abort gracefully if the revision index has bad parents.
35
from bzrlib.trace import mutter, note
1570.1.7 by Robert Collins
Replace the slow topo_sort routine with a much faster one for non trivial datasets.
36
from bzrlib.tsort import TopoSorter
1570.1.2 by Robert Collins
Import bzrtools' 'fix' command as 'bzr reconcile.'
37
38
1692.1.1 by Robert Collins
* Repository.reconcile now takes a thorough keyword parameter to allow
39
def reconcile(dir, other=None):
1570.1.2 by Robert Collins
Import bzrtools' 'fix' command as 'bzr reconcile.'
40
    """Reconcile the data in dir.
41
42
    Currently this is limited to a inventory 'reweave'.
43
1570.1.8 by Robert Collins
Only reconcile if doing so will perform gc or correct ancestry.
44
    This is a convenience method, for using a Reconciler object.
45
46
    Directly using Reconciler is recommended for library users that
47
    desire fine grained control or analysis of the found issues.
1692.1.1 by Robert Collins
* Repository.reconcile now takes a thorough keyword parameter to allow
48
49
    :param other: another bzrdir to reconcile against.
1570.1.2 by Robert Collins
Import bzrtools' 'fix' command as 'bzr reconcile.'
50
    """
1692.1.1 by Robert Collins
* Repository.reconcile now takes a thorough keyword parameter to allow
51
    reconciler = Reconciler(dir, other=other)
1570.1.2 by Robert Collins
Import bzrtools' 'fix' command as 'bzr reconcile.'
52
    reconciler.reconcile()
53
54
1570.1.6 by Robert Collins
Update fast topological_sort to be a function and to have the topo_sort tests run against it.
55
class Reconciler(object):
1570.1.14 by Robert Collins
Enforce repository consistency during 'fetch' operations.
56
    """Reconcilers are used to reconcile existing data."""
1570.1.6 by Robert Collins
Update fast topological_sort to be a function and to have the topo_sort tests run against it.
57
1692.1.1 by Robert Collins
* Repository.reconcile now takes a thorough keyword parameter to allow
58
    def __init__(self, dir, other=None):
59
        """Create a Reconciler."""
1570.1.6 by Robert Collins
Update fast topological_sort to be a function and to have the topo_sort tests run against it.
60
        self.bzrdir = dir
61
62
    def reconcile(self):
1570.1.8 by Robert Collins
Only reconcile if doing so will perform gc or correct ancestry.
63
        """Perform reconciliation.
64
        
65
        After reconciliation the following attributes document found issues:
66
        inconsistent_parents: The number of revisions in the repository whose
67
                              ancestry was being reported incorrectly.
68
        garbage_inventories: The number of inventory objects without revisions
69
                             that were garbage collected.
70
        """
1594.1.3 by Robert Collins
Fixup pb usage to use nested_progress_bar.
71
        self.pb = ui.ui_factory.nested_progress_bar()
72
        try:
73
            self._reconcile()
74
        finally:
75
            self.pb.finished()
76
77
    def _reconcile(self):
78
        """Helper function for performing reconciliation."""
1570.1.11 by Robert Collins
Make reconcile work with shared repositories.
79
        self.repo = self.bzrdir.find_repository()
1570.1.14 by Robert Collins
Enforce repository consistency during 'fetch' operations.
80
        self.pb.note('Reconciling repository %s',
81
                     self.repo.bzrdir.root_transport.base)
2960.1.1 by Robert Collins
* Reconcile now shows progress bars. (Robert Collins, #159351)
82
        self.pb.update("Reconciling repository", 0, 1)
1692.1.1 by Robert Collins
* Repository.reconcile now takes a thorough keyword parameter to allow
83
        repo_reconciler = self.repo.reconcile(thorough=True)
1570.1.14 by Robert Collins
Enforce repository consistency during 'fetch' operations.
84
        self.inconsistent_parents = repo_reconciler.inconsistent_parents
85
        self.garbage_inventories = repo_reconciler.garbage_inventories
2819.2.5 by Andrew Bennetts
Make reconcile abort gracefully if the revision index has bad parents.
86
        if repo_reconciler.aborted:
87
            self.pb.note(
88
                'Reconcile aborted: revision index has inconsistent parents.')
89
            self.pb.note(
90
                'Run "bzr check" for more details.')
91
        else:
92
            self.pb.note('Reconciliation complete.')
1570.1.14 by Robert Collins
Enforce repository consistency during 'fetch' operations.
93
94
95
class RepoReconciler(object):
96
    """Reconciler that reconciles a repository.
97
2857.1.2 by Robert Collins
Review feedback.
98
    The goal of repository reconciliation is to make any derived data
2592.3.80 by Robert Collins
Make reconcile work, and pass tests.
99
    consistent with the core data committed by a user. This can involve 
100
    reindexing, or removing unreferenced data if that can interfere with
101
    queries in a given repository.
102
1570.1.14 by Robert Collins
Enforce repository consistency during 'fetch' operations.
103
    Currently this consists of an inventory reweave with revision cross-checks.
104
    """
105
1692.1.1 by Robert Collins
* Repository.reconcile now takes a thorough keyword parameter to allow
106
    def __init__(self, repo, other=None, thorough=False):
107
        """Construct a RepoReconciler.
108
109
        :param thorough: perform a thorough check which may take longer but
110
                         will correct non-data loss issues such as incorrect
111
                         cached data.
112
        """
113
        self.garbage_inventories = 0
114
        self.inconsistent_parents = 0
2819.2.5 by Andrew Bennetts
Make reconcile abort gracefully if the revision index has bad parents.
115
        self.aborted = False
1570.1.14 by Robert Collins
Enforce repository consistency during 'fetch' operations.
116
        self.repo = repo
1692.1.1 by Robert Collins
* Repository.reconcile now takes a thorough keyword parameter to allow
117
        self.thorough = thorough
1570.1.14 by Robert Collins
Enforce repository consistency during 'fetch' operations.
118
119
    def reconcile(self):
120
        """Perform reconciliation.
121
        
122
        After reconciliation the following attributes document found issues:
123
        inconsistent_parents: The number of revisions in the repository whose
124
                              ancestry was being reported incorrectly.
125
        garbage_inventories: The number of inventory objects without revisions
126
                             that were garbage collected.
127
        """
1570.1.6 by Robert Collins
Update fast topological_sort to be a function and to have the topo_sort tests run against it.
128
        self.repo.lock_write()
129
        try:
1594.1.3 by Robert Collins
Fixup pb usage to use nested_progress_bar.
130
            self.pb = ui.ui_factory.nested_progress_bar()
131
            try:
1594.2.7 by Robert Collins
Add versionedfile.fix_parents api for correcting data post hoc.
132
                self._reconcile_steps()
1594.1.3 by Robert Collins
Fixup pb usage to use nested_progress_bar.
133
            finally:
134
                self.pb.finished()
1570.1.6 by Robert Collins
Update fast topological_sort to be a function and to have the topo_sort tests run against it.
135
        finally:
136
            self.repo.unlock()
137
1594.2.7 by Robert Collins
Add versionedfile.fix_parents api for correcting data post hoc.
138
    def _reconcile_steps(self):
139
        """Perform the steps to reconcile this repository."""
1692.1.3 by Robert Collins
Finish the reconcile tweak: filled in ghosts are a data loss issue and need to be checked during fast reconciles.
140
        self._reweave_inventory()
1594.2.7 by Robert Collins
Add versionedfile.fix_parents api for correcting data post hoc.
141
1570.1.6 by Robert Collins
Update fast topological_sort to be a function and to have the topo_sort tests run against it.
142
    def _reweave_inventory(self):
1692.1.3 by Robert Collins
Finish the reconcile tweak: filled in ghosts are a data loss issue and need to be checked during fast reconciles.
143
        """Regenerate the inventory weave for the repository from scratch.
144
        
145
        This is a smart function: it will only do the reweave if doing it 
146
        will correct data issues. The self.thorough flag controls whether
147
        only data-loss causing issues (!self.thorough) or all issues
148
        (self.thorough) are treated as requiring the reweave.
149
        """
150
        # local because needing to know about WeaveFile is a wart we want to hide
1563.2.42 by Robert Collins
Stop reconcile on weaves being quadratic.
151
        from bzrlib.weave import WeaveFile, Weave
1563.2.29 by Robert Collins
Remove all but fetch references to repository.revision_store.
152
        transaction = self.repo.get_transaction()
1570.1.6 by Robert Collins
Update fast topological_sort to be a function and to have the topo_sort tests run against it.
153
        self.pb.update('Reading inventory data.')
154
        self.inventory = self.repo.get_inventory_weave()
155
        # the total set of revisions to process
1563.2.29 by Robert Collins
Remove all but fetch references to repository.revision_store.
156
        self.pending = set([rev_id for rev_id in self.repo._revision_store.all_revision_ids(transaction)])
1570.1.6 by Robert Collins
Update fast topological_sort to be a function and to have the topo_sort tests run against it.
157
158
        # mapping from revision_id to parents
159
        self._rev_graph = {}
1570.1.8 by Robert Collins
Only reconcile if doing so will perform gc or correct ancestry.
160
        # errors that we detect
161
        self.inconsistent_parents = 0
1570.1.6 by Robert Collins
Update fast topological_sort to be a function and to have the topo_sort tests run against it.
162
        # we need the revision id of each revision and its available parents list
1570.1.10 by Robert Collins
UI tweaks to reconcile - show progress for inventory backup.
163
        self._setup_steps(len(self.pending))
1570.1.6 by Robert Collins
Update fast topological_sort to be a function and to have the topo_sort tests run against it.
164
        for rev_id in self.pending:
165
            # put a revision into the graph.
166
            self._graph_revision(rev_id)
1594.2.2 by Robert Collins
Trivial change to reconcile to mutter the cause of reconciliation to bzr.log
167
        self._check_garbage_inventories()
1692.1.3 by Robert Collins
Finish the reconcile tweak: filled in ghosts are a data loss issue and need to be checked during fast reconciles.
168
        # if there are no inconsistent_parents and 
169
        # (no garbage inventories or we are not doing a thorough check)
170
        if (not self.inconsistent_parents and 
171
            (not self.garbage_inventories or not self.thorough)):
1570.1.8 by Robert Collins
Only reconcile if doing so will perform gc or correct ancestry.
172
            self.pb.note('Inventory ok.')
173
            return
1570.1.10 by Robert Collins
UI tweaks to reconcile - show progress for inventory backup.
174
        self.pb.update('Backing up inventory...', 0, 0)
1563.2.25 by Robert Collins
Merge in upstream.
175
        self.repo.control_weaves.copy(self.inventory, 'inventory.backup', self.repo.get_transaction())
1570.1.8 by Robert Collins
Only reconcile if doing so will perform gc or correct ancestry.
176
        self.pb.note('Backup Inventory created.')
177
        # asking for '' should never return a non-empty weave
1616.1.1 by Martin Pool
[merge] robertc
178
        new_inventory_vf = self.repo.control_weaves.get_empty('inventory.new',
1570.1.8 by Robert Collins
Only reconcile if doing so will perform gc or correct ancestry.
179
            self.repo.get_transaction())
1570.1.6 by Robert Collins
Update fast topological_sort to be a function and to have the topo_sort tests run against it.
180
1570.1.4 by Robert Collins
Somewhat optimised version of reconciler.
181
        # we have topological order of revisions and non ghost parents ready.
1570.1.10 by Robert Collins
UI tweaks to reconcile - show progress for inventory backup.
182
        self._setup_steps(len(self._rev_graph))
1570.1.7 by Robert Collins
Replace the slow topo_sort routine with a much faster one for non trivial datasets.
183
        for rev_id in TopoSorter(self._rev_graph.items()).iter_topo_order():
184
            parents = self._rev_graph[rev_id]
1570.1.4 by Robert Collins
Somewhat optimised version of reconciler.
185
            # double check this really is in topological order.
1616.1.1 by Martin Pool
[merge] robertc
186
            unavailable = [p for p in parents if p not in new_inventory_vf]
1570.1.4 by Robert Collins
Somewhat optimised version of reconciler.
187
            assert len(unavailable) == 0
188
            # this entry has all the non ghost parents in the inventory
189
            # file already.
190
            self._reweave_step('adding inventories')
1616.1.1 by Martin Pool
[merge] robertc
191
            if isinstance(new_inventory_vf, WeaveFile):
192
                # It's really a WeaveFile, but we call straight into the
193
                # Weave's add method to disable the auto-write-out behaviour.
1607.1.11 by Robert Collins
Merge from bzr.dev
194
                # This is done to avoid a revision_count * time-to-write additional overhead on 
195
                # reconcile.
1616.1.1 by Martin Pool
[merge] robertc
196
                new_inventory_vf._check_write_ok()
2794.1.1 by Robert Collins
Allow knits to be instructed not to add a text based on a sha, for commit.
197
                Weave._add_lines(new_inventory_vf, rev_id, parents,
2805.6.7 by Robert Collins
Review feedback.
198
                    self.inventory.get_lines(rev_id), None, None, None, False, True)
1563.2.42 by Robert Collins
Stop reconcile on weaves being quadratic.
199
            else:
1616.1.1 by Martin Pool
[merge] robertc
200
                new_inventory_vf.add_lines(rev_id, parents, self.inventory.get_lines(rev_id))
1570.1.4 by Robert Collins
Somewhat optimised version of reconciler.
201
1616.1.1 by Martin Pool
[merge] robertc
202
        if isinstance(new_inventory_vf, WeaveFile):
203
            new_inventory_vf._save()
204
        # if this worked, the set of new_inventory_vf.names should equal
1570.1.4 by Robert Collins
Somewhat optimised version of reconciler.
205
        # self.pending
1616.1.1 by Martin Pool
[merge] robertc
206
        assert set(new_inventory_vf.versions()) == self.pending
1570.1.2 by Robert Collins
Import bzrtools' 'fix' command as 'bzr reconcile.'
207
        self.pb.update('Writing weave')
1616.1.1 by Martin Pool
[merge] robertc
208
        self.repo.control_weaves.copy(new_inventory_vf, 'inventory', self.repo.get_transaction())
1563.2.25 by Robert Collins
Merge in upstream.
209
        self.repo.control_weaves.delete('inventory.new', self.repo.get_transaction())
1570.1.3 by Robert Collins
Optimise reconcilation to only hit each revision once.
210
        self.inventory = None
1570.1.2 by Robert Collins
Import bzrtools' 'fix' command as 'bzr reconcile.'
211
        self.pb.note('Inventory regenerated.')
1570.1.3 by Robert Collins
Optimise reconcilation to only hit each revision once.
212
1570.1.10 by Robert Collins
UI tweaks to reconcile - show progress for inventory backup.
213
    def _setup_steps(self, new_total):
214
        """Setup the markers we need to control the progress bar."""
215
        self.total = new_total
216
        self.count = 0
217
1570.1.4 by Robert Collins
Somewhat optimised version of reconciler.
218
    def _graph_revision(self, rev_id):
219
        """Load a revision into the revision graph."""
220
        # pick a random revision
221
        # analyse revision id rev_id and put it in the stack.
222
        self._reweave_step('loading revisions')
1570.1.13 by Robert Collins
Check for incorrect revision parentage in the weave during revision access.
223
        rev = self.repo.get_revision_reconcile(rev_id)
1570.1.3 by Robert Collins
Optimise reconcilation to only hit each revision once.
224
        assert rev.revision_id == rev_id
225
        parents = []
226
        for parent in rev.parent_ids:
1570.1.14 by Robert Collins
Enforce repository consistency during 'fetch' operations.
227
            if self._parent_is_available(parent):
1570.1.3 by Robert Collins
Optimise reconcilation to only hit each revision once.
228
                parents.append(parent)
229
            else:
230
                mutter('found ghost %s', parent)
1570.1.4 by Robert Collins
Somewhat optimised version of reconciler.
231
        self._rev_graph[rev_id] = parents   
1692.1.3 by Robert Collins
Finish the reconcile tweak: filled in ghosts are a data loss issue and need to be checked during fast reconciles.
232
        if self._parents_are_inconsistent(rev_id, parents):
1570.1.8 by Robert Collins
Only reconcile if doing so will perform gc or correct ancestry.
233
            self.inconsistent_parents += 1
1594.2.2 by Robert Collins
Trivial change to reconcile to mutter the cause of reconciliation to bzr.log
234
            mutter('Inconsistent inventory parents: id {%s} '
235
                   'inventory claims %r, '
236
                   'available parents are %r, '
237
                   'unavailable parents are %r',
238
                   rev_id, 
1563.2.39 by Robert Collins
Merge from integration.
239
                   set(self.inventory.get_parents(rev_id)),
1594.2.2 by Robert Collins
Trivial change to reconcile to mutter the cause of reconciliation to bzr.log
240
                   set(parents),
241
                   set(rev.parent_ids).difference(set(parents)))
242
1692.1.3 by Robert Collins
Finish the reconcile tweak: filled in ghosts are a data loss issue and need to be checked during fast reconciles.
243
    def _parents_are_inconsistent(self, rev_id, parents):
244
        """Return True if the parents list of rev_id does not match the weave.
245
1759.2.2 by Jelmer Vernooij
Revert some of my spelling fixes and fix some typos after review by Aaron.
246
        This detects inconsistencies based on the self.thorough value:
1692.1.3 by Robert Collins
Finish the reconcile tweak: filled in ghosts are a data loss issue and need to be checked during fast reconciles.
247
        if thorough is on, the first parent value is checked as well as ghost
248
        differences.
249
        Otherwise only the ghost differences are evaluated.
250
        """
251
        weave_parents = self.inventory.get_parents(rev_id)
252
        weave_missing_old_ghosts = set(weave_parents) != set(parents)
253
        first_parent_is_wrong = (
254
            len(weave_parents) and len(parents) and
255
            parents[0] != weave_parents[0])
256
        if self.thorough:
257
            return weave_missing_old_ghosts or first_parent_is_wrong
258
        else:
259
            return weave_missing_old_ghosts
260
1594.2.2 by Robert Collins
Trivial change to reconcile to mutter the cause of reconciliation to bzr.log
261
    def _check_garbage_inventories(self):
262
        """Check for garbage inventories which we cannot trust
263
264
        We cant trust them because their pre-requisite file data may not
265
        be present - all we know is that their revision was not installed.
266
        """
1692.1.3 by Robert Collins
Finish the reconcile tweak: filled in ghosts are a data loss issue and need to be checked during fast reconciles.
267
        if not self.thorough:
268
            return
1563.2.39 by Robert Collins
Merge from integration.
269
        inventories = set(self.inventory.versions())
1594.2.2 by Robert Collins
Trivial change to reconcile to mutter the cause of reconciliation to bzr.log
270
        revisions = set(self._rev_graph.keys())
271
        garbage = inventories.difference(revisions)
272
        self.garbage_inventories = len(garbage)
273
        for revision_id in garbage:
274
            mutter('Garbage inventory {%s} found.', revision_id)
1570.1.4 by Robert Collins
Somewhat optimised version of reconciler.
275
1570.1.14 by Robert Collins
Enforce repository consistency during 'fetch' operations.
276
    def _parent_is_available(self, parent):
277
        """True if parent is a fully available revision
278
279
        A fully available revision has a inventory and a revision object in the
280
        repository.
281
        """
282
        return (parent in self._rev_graph or 
283
                (parent in self.inventory and self.repo.has_revision(parent)))
284
1570.1.4 by Robert Collins
Somewhat optimised version of reconciler.
285
    def _reweave_step(self, message):
286
        """Mark a single step of regeneration complete."""
287
        self.pb.update(message, self.count, self.total)
288
        self.count += 1
1594.2.7 by Robert Collins
Add versionedfile.fix_parents api for correcting data post hoc.
289
290
291
class KnitReconciler(RepoReconciler):
292
    """Reconciler that reconciles a knit format repository.
293
2592.3.80 by Robert Collins
Make reconcile work, and pass tests.
294
    This will detect garbage inventories and remove them in thorough mode.
1594.2.7 by Robert Collins
Add versionedfile.fix_parents api for correcting data post hoc.
295
    """
296
297
    def _reconcile_steps(self):
298
        """Perform the steps to reconcile this repository."""
1692.1.1 by Robert Collins
* Repository.reconcile now takes a thorough keyword parameter to allow
299
        if self.thorough:
2819.2.5 by Andrew Bennetts
Make reconcile abort gracefully if the revision index has bad parents.
300
            try:
301
                self._load_indexes()
302
            except errors.BzrCheckError:
303
                self.aborted = True
304
                return
1692.1.1 by Robert Collins
* Repository.reconcile now takes a thorough keyword parameter to allow
305
            # knits never suffer this
306
            self._gc_inventory()
2745.6.13 by Aaron Bentley
Misc cleanup
307
            self._fix_text_parents()
1594.2.7 by Robert Collins
Add versionedfile.fix_parents api for correcting data post hoc.
308
309
    def _load_indexes(self):
310
        """Load indexes for the reconciliation."""
311
        self.transaction = self.repo.get_transaction()
312
        self.pb.update('Reading indexes.', 0, 2)
313
        self.inventory = self.repo.get_inventory_weave()
314
        self.pb.update('Reading indexes.', 1, 2)
2819.2.5 by Andrew Bennetts
Make reconcile abort gracefully if the revision index has bad parents.
315
        self.repo._check_for_inconsistent_revision_parents()
1594.2.7 by Robert Collins
Add versionedfile.fix_parents api for correcting data post hoc.
316
        self.revisions = self.repo._revision_store.get_revision_file(self.transaction)
317
        self.pb.update('Reading indexes.', 2, 2)
318
319
    def _gc_inventory(self):
320
        """Remove inventories that are not referenced from the revision store."""
321
        self.pb.update('Checking unused inventories.', 0, 1)
322
        self._check_garbage_inventories()
323
        self.pb.update('Checking unused inventories.', 1, 3)
324
        if not self.garbage_inventories:
325
            self.pb.note('Inventory ok.')
326
            return
327
        self.pb.update('Backing up inventory...', 0, 0)
328
        self.repo.control_weaves.copy(self.inventory, 'inventory.backup', self.transaction)
329
        self.pb.note('Backup Inventory created.')
330
        # asking for '' should never return a non-empty weave
1616.1.1 by Martin Pool
[merge] robertc
331
        new_inventory_vf = self.repo.control_weaves.get_empty('inventory.new',
1594.2.7 by Robert Collins
Add versionedfile.fix_parents api for correcting data post hoc.
332
            self.transaction)
333
334
        # we have topological order of revisions and non ghost parents ready.
1594.2.9 by Robert Collins
Teach Knit repositories how to handle ghosts without corrupting at all.
335
        self._setup_steps(len(self.revisions))
336
        for rev_id in TopoSorter(self.revisions.get_graph().items()).iter_topo_order():
337
            parents = self.revisions.get_parents(rev_id)
1594.2.7 by Robert Collins
Add versionedfile.fix_parents api for correcting data post hoc.
338
            # double check this really is in topological order.
1616.1.1 by Martin Pool
[merge] robertc
339
            unavailable = [p for p in parents if p not in new_inventory_vf]
1594.2.7 by Robert Collins
Add versionedfile.fix_parents api for correcting data post hoc.
340
            assert len(unavailable) == 0
341
            # this entry has all the non ghost parents in the inventory
342
            # file already.
343
            self._reweave_step('adding inventories')
344
            # ugly but needed, weaves are just way tooooo slow else.
1616.1.1 by Martin Pool
[merge] robertc
345
            new_inventory_vf.add_lines(rev_id, parents, self.inventory.get_lines(rev_id))
1594.2.7 by Robert Collins
Add versionedfile.fix_parents api for correcting data post hoc.
346
1616.1.1 by Martin Pool
[merge] robertc
347
        # if this worked, the set of new_inventory_vf.names should equal
1594.2.7 by Robert Collins
Add versionedfile.fix_parents api for correcting data post hoc.
348
        # self.pending
1616.1.1 by Martin Pool
[merge] robertc
349
        assert set(new_inventory_vf.versions()) == set(self.revisions.versions())
1594.2.7 by Robert Collins
Add versionedfile.fix_parents api for correcting data post hoc.
350
        self.pb.update('Writing weave')
1616.1.1 by Martin Pool
[merge] robertc
351
        self.repo.control_weaves.copy(new_inventory_vf, 'inventory', self.transaction)
1594.2.7 by Robert Collins
Add versionedfile.fix_parents api for correcting data post hoc.
352
        self.repo.control_weaves.delete('inventory.new', self.transaction)
353
        self.inventory = None
354
        self.pb.note('Inventory regenerated.')
355
356
    def _check_garbage_inventories(self):
357
        """Check for garbage inventories which we cannot trust
358
359
        We cant trust them because their pre-requisite file data may not
360
        be present - all we know is that their revision was not installed.
361
        """
362
        inventories = set(self.inventory.versions())
363
        revisions = set(self.revisions.versions())
364
        garbage = inventories.difference(revisions)
365
        self.garbage_inventories = len(garbage)
366
        for revision_id in garbage:
367
            mutter('Garbage inventory {%s} found.', revision_id)
2745.6.11 by Aaron Bentley
Fix knit file parents to follow parentage from revision/inventory XML
368
369
    def _fix_text_parents(self):
2745.6.13 by Aaron Bentley
Misc cleanup
370
        """Fix bad versionedfile parent entries.
371
2745.6.16 by Aaron Bentley
Update from review
372
        It is possible for the parents entry in a versionedfile entry to be
2745.6.13 by Aaron Bentley
Misc cleanup
373
        inconsistent with the values in the revision and inventory.
374
375
        This method finds entries with such inconsistencies, corrects their
376
        parent lists, and replaces the versionedfile with a corrected version.
377
        """
2745.6.11 by Aaron Bentley
Fix knit file parents to follow parentage from revision/inventory XML
378
        transaction = self.repo.get_transaction()
2906.1.1 by Andrew Bennetts
Speed up reconcile by not repeatedly fetching the full inventories, by cache heads and parents queries, and by fetching revision trees in batches.
379
        versions = self.revisions.versions()
2927.2.2 by Andrew Bennetts
Only try to check versions that actually exist in the versioned file, and do a little more muttering.
380
        mutter('Prepopulating revision text cache with %d revisions',
381
                len(versions))
3036.1.3 by Robert Collins
Privatise VersionedFileChecker.
382
        vf_checker = self.repo._get_versioned_file_checker()
2988.1.8 by Robert Collins
Change check and reconcile to use the new _generate_text_key_index rather
383
        # List all weaves before altering, to avoid race conditions when we
384
        # delete unused weaves.
385
        weaves = list(enumerate(self.repo.weave_store))
386
        for num, file_id in weaves:
2745.6.12 by Aaron Bentley
Do topological sorting when adding new records to VersionedFile
387
            self.pb.update('Fixing text parents', num,
388
                           len(self.repo.weave_store))
2745.6.11 by Aaron Bentley
Fix knit file parents to follow parentage from revision/inventory XML
389
            vf = self.repo.weave_store.get_weave(file_id, transaction)
2988.1.8 by Robert Collins
Change check and reconcile to use the new _generate_text_key_index rather
390
            versions_with_bad_parents, unused_versions = \
3036.1.2 by Robert Collins
Simplify the check_file_version_parents API some more. This has already changed in this release cycle.
391
                vf_checker.check_file_version_parents(vf, file_id)
2927.2.14 by Andrew Bennetts
Tweaks suggested by review.
392
            if (len(versions_with_bad_parents) == 0 and
2988.1.8 by Robert Collins
Change check and reconcile to use the new _generate_text_key_index rather
393
                len(unused_versions) == 0):
2927.2.14 by Andrew Bennetts
Tweaks suggested by review.
394
                continue
2927.2.3 by Andrew Bennetts
Add fulltexts to avoid bug 155730.
395
            full_text_versions = set()
396
            self._fix_text_parent(file_id, vf, versions_with_bad_parents,
397
                full_text_versions, unused_versions)
2745.6.53 by Andrew Bennetts
Some more changes suggested by review.
398
2927.2.3 by Andrew Bennetts
Add fulltexts to avoid bug 155730.
399
    def _fix_text_parent(self, file_id, vf, versions_with_bad_parents,
400
            full_text_versions, unused_versions):
2745.6.53 by Andrew Bennetts
Some more changes suggested by review.
401
        """Fix bad versionedfile entries in a single versioned file."""
2927.2.2 by Andrew Bennetts
Only try to check versions that actually exist in the versioned file, and do a little more muttering.
402
        mutter('fixing text parent: %r (%d versions)', file_id,
403
                len(versions_with_bad_parents))
2927.2.3 by Andrew Bennetts
Add fulltexts to avoid bug 155730.
404
        mutter('(%d need to be full texts, %d are unused)',
405
                len(full_text_versions), len(unused_versions))
2745.6.53 by Andrew Bennetts
Some more changes suggested by review.
406
        new_vf = self.repo.weave_store.get_empty('temp:%s' % file_id,
407
            self.transaction)
408
        new_parents = {}
409
        for version in vf.versions():
2988.1.8 by Robert Collins
Change check and reconcile to use the new _generate_text_key_index rather
410
            if version in unused_versions:
411
                continue
412
            elif version in versions_with_bad_parents:
2745.6.53 by Andrew Bennetts
Some more changes suggested by review.
413
                parents = versions_with_bad_parents[version][1]
414
            else:
415
                parents = vf.get_parents(version)
416
            new_parents[version] = parents
2988.1.8 by Robert Collins
Change check and reconcile to use the new _generate_text_key_index rather
417
        if not len(new_parents):
418
            # No used versions, remove the VF.
419
            self.repo.weave_store.delete(file_id, self.transaction)
420
            return
2592.3.214 by Robert Collins
Merge bzr.dev.
421
        for version in TopoSorter(new_parents.items()).iter_topo_order():
2927.2.3 by Andrew Bennetts
Add fulltexts to avoid bug 155730.
422
            lines = vf.get_lines(version)
423
            parents = new_parents[version]
424
            if parents and (parents[0] in full_text_versions):
2927.2.10 by Andrew Bennetts
More docstrings, elaborate a comment with an XXX, and remove a little bit of cruft.
425
                # Force this record to be a fulltext, not a delta.
2927.2.3 by Andrew Bennetts
Add fulltexts to avoid bug 155730.
426
                new_vf._add(version, lines, parents, False,
427
                    None, None, None, False)
428
            else:
429
                new_vf.add_lines(version, parents, lines)
2745.6.53 by Andrew Bennetts
Some more changes suggested by review.
430
        self.repo.weave_store.copy(new_vf, file_id, self.transaction)
431
        self.repo.weave_store.delete('temp:%s' % file_id, self.transaction)
2745.6.11 by Aaron Bentley
Fix knit file parents to follow parentage from revision/inventory XML
432
2592.3.80 by Robert Collins
Make reconcile work, and pass tests.
433
434
class PackReconciler(RepoReconciler):
435
    """Reconciler that reconciles a pack based repository.
436
437
    Garbage inventories do not affect ancestry queries, and removal is
438
    considerably more expensive as there is no separate versioned file for
439
    them, so they are not cleaned. In short it is currently a no-op.
440
441
    In future this may be a good place to hook in annotation cache checking,
442
    index recreation etc.
443
    """
444
2592.3.239 by Martin Pool
doc
445
    # XXX: The index corruption that _fix_text_parents performs is needed for
446
    # packs, but not yet implemented. The basic approach is to:
447
    #  - lock the names list
448
    #  - perform a customised pack() that regenerates data as needed
449
    #  - unlock the names list
450
    # https://bugs.edge.launchpad.net/bzr/+bug/154173
451
2592.3.80 by Robert Collins
Make reconcile work, and pass tests.
452
    def _reconcile_steps(self):
453
        """Perform the steps to reconcile this repository."""
2951.1.2 by Robert Collins
Partial refactoring of pack_repo to create a Packer object for packing.
454
        if not self.thorough:
455
            return
2951.1.3 by Robert Collins
Partial support for native reconcile with packs.
456
        collection = self.repo._pack_collection
457
        collection.ensure_loaded()
458
        collection.lock_names()
2951.1.2 by Robert Collins
Partial refactoring of pack_repo to create a Packer object for packing.
459
        try:
2951.1.3 by Robert Collins
Partial support for native reconcile with packs.
460
            packs = collection.all_packs()
461
            all_revisions = self.repo.all_revision_ids()
462
            total_inventories = len(list(
463
                collection.inventory_index.combined_index.iter_all_entries()))
464
            if len(all_revisions):
465
                self._packer = repofmt.pack_repo.ReconcilePacker(
466
                    collection, packs, ".reconcile", all_revisions)
467
                new_pack = self._packer.pack(pb=self.pb)
468
                if new_pack is not None:
2951.1.10 by Robert Collins
Peer review feedback with Ian.
469
                    self._discard_and_save(packs)
2951.1.3 by Robert Collins
Partial support for native reconcile with packs.
470
            else:
471
                # only make a new pack when there is data to copy.
2951.1.10 by Robert Collins
Peer review feedback with Ian.
472
                self._discard_and_save(packs)
2951.1.3 by Robert Collins
Partial support for native reconcile with packs.
473
            self.garbage_inventories = total_inventories - len(list(
474
                collection.inventory_index.combined_index.iter_all_entries()))
2951.1.2 by Robert Collins
Partial refactoring of pack_repo to create a Packer object for packing.
475
        finally:
2951.1.3 by Robert Collins
Partial support for native reconcile with packs.
476
            collection._unlock_names()
477
2951.1.10 by Robert Collins
Peer review feedback with Ian.
478
    def _discard_and_save(self, packs):
2951.1.3 by Robert Collins
Partial support for native reconcile with packs.
479
        """Discard some packs from the repository.
480
2951.1.10 by Robert Collins
Peer review feedback with Ian.
481
        This removes them from the memory index, saves the in-memory index
482
        which makes the newly reconciled pack visible and hides the packs to be
483
        discarded, and finally renames the packs being discarded into the
2951.1.3 by Robert Collins
Partial support for native reconcile with packs.
484
        obsolete packs directory.
2951.1.10 by Robert Collins
Peer review feedback with Ian.
485
2951.1.3 by Robert Collins
Partial support for native reconcile with packs.
486
        :param packs: The packs to discard.
487
        """
488
        for pack in packs:
489
            self.repo._pack_collection._remove_pack_from_memory(pack)
490
        self.repo._pack_collection._save_pack_names()
491
        self.repo._pack_collection._obsolete_packs(packs)