/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 breezy/reconcile.py

  • Committer: Jelmer Vernooij
  • Date: 2020-05-06 02:13:25 UTC
  • mfrom: (7490.7.21 work)
  • mto: This revision was merged to the branch mainline in revision 7501.
  • Revision ID: jelmer@jelmer.uk-20200506021325-awbmmqu1zyorz7sj
Merge 3.1 branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2006-2010 Canonical Ltd
 
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
 
 
17
"""Reconcilers are able to fix some potential data errors in a branch."""
 
18
 
 
19
__all__ = [
 
20
    'reconcile',
 
21
    'Reconciler',
 
22
    ]
 
23
 
 
24
 
 
25
from . import (
 
26
    errors,
 
27
    ui,
 
28
    )
 
29
from .trace import mutter
 
30
from .i18n import gettext
 
31
 
 
32
 
 
33
def reconcile(dir, canonicalize_chks=False):
 
34
    """Reconcile the data in dir.
 
35
 
 
36
    Currently this is limited to a inventory 'reweave'.
 
37
 
 
38
    This is a convenience method, for using a Reconciler object.
 
39
 
 
40
    Directly using Reconciler is recommended for library users that
 
41
    desire fine grained control or analysis of the found issues.
 
42
 
 
43
    :param canonicalize_chks: Make sure CHKs are in canonical form.
 
44
    """
 
45
    reconciler = Reconciler(dir, canonicalize_chks=canonicalize_chks)
 
46
    return reconciler.reconcile()
 
47
 
 
48
 
 
49
class ReconcileResult(object):
 
50
    """Class describing the result of a reconcile operation."""
 
51
 
 
52
 
 
53
class Reconciler(object):
 
54
    """Reconcilers are used to reconcile existing data."""
 
55
 
 
56
    def __init__(self, dir, other=None, canonicalize_chks=False):
 
57
        """Create a Reconciler."""
 
58
        self.controldir = dir
 
59
        self.canonicalize_chks = canonicalize_chks
 
60
 
 
61
    def reconcile(self):
 
62
        """Perform reconciliation.
 
63
        """
 
64
        with ui.ui_factory.nested_progress_bar() as self.pb:
 
65
            result = ReconcileResult()
 
66
            branch_result = self._reconcile_branch()
 
67
            repo_result = self._reconcile_repository()
 
68
            # TODO(jelmer): Don't hardcode supported attributes here
 
69
            result.inconsistent_parents = getattr(
 
70
                repo_result, 'inconsistent_parents', None)
 
71
            result.aborted = getattr(repo_result, 'aborted', None)
 
72
            result.garbage_inventories = getattr(
 
73
                repo_result, 'garbage_inventories', None)
 
74
            result.fixed_branch_history = getattr(
 
75
                branch_result, 'fixed_history', None)
 
76
            return result
 
77
 
 
78
    def _reconcile_branch(self):
 
79
        try:
 
80
            self.branch = self.controldir.open_branch()
 
81
        except errors.NotBranchError:
 
82
            # Nothing to check here
 
83
            return
 
84
        ui.ui_factory.note(gettext('Reconciling branch %s') % self.branch.base)
 
85
        return self.branch.reconcile(thorough=True)
 
86
 
 
87
    def _reconcile_repository(self):
 
88
        self.repo = self.controldir.find_repository()
 
89
        ui.ui_factory.note(gettext('Reconciling repository %s') %
 
90
                           self.repo.user_url)
 
91
        self.pb.update(gettext("Reconciling repository"), 0, 1)
 
92
        if self.canonicalize_chks:
 
93
            try:
 
94
                self.repo.reconcile_canonicalize_chks
 
95
            except AttributeError:
 
96
                raise errors.BzrError(
 
97
                    gettext("%s cannot canonicalize CHKs.") % (self.repo,))
 
98
            reconcile_result = self.repo.reconcile_canonicalize_chks()
 
99
        else:
 
100
            reconcile_result = self.repo.reconcile(thorough=True)
 
101
        if reconcile_result.aborted:
 
102
            ui.ui_factory.note(gettext(
 
103
                'Reconcile aborted: revision index has inconsistent parents.'))
 
104
            ui.ui_factory.note(gettext(
 
105
                'Run "brz check" for more details.'))
 
106
        else:
 
107
            ui.ui_factory.note(gettext('Reconciliation complete.'))
 
108
        return reconcile_result