~bzr-pqm/bzr/bzr.dev

6517.1.2 by Jelmer Vernooij
Move BzrBranchFormat5 into a separate file.
1
# Copyright (C) 2006-2012 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
"""Full history branch formats."""
18
6517.1.6 by Jelmer Vernooij
Fix remaining tests.
19
from __future__ import absolute_import
20
6517.1.2 by Jelmer Vernooij
Move BzrBranchFormat5 into a separate file.
21
from bzrlib import (
22
    debug,
23
    errors,
24
    revision as _mod_revision,
25
    )
26
27
from bzrlib.branch import (
28
    Branch,
29
    BranchFormatMetadir,
30
    BzrBranch,
31
    )
32
33
from bzrlib.decorators import (
34
    needs_write_lock,
35
    )
36
from bzrlib.trace import mutter_callsite
37
38
39
class FullHistoryBzrBranch(BzrBranch):
40
    """Bzr branch which contains the full revision history."""
41
42
    @needs_write_lock
43
    def set_last_revision_info(self, revno, revision_id):
44
        if not revision_id or not isinstance(revision_id, basestring):
45
            raise errors.InvalidRevisionId(revision_id=revision_id, branch=self)
46
        revision_id = _mod_revision.ensure_null(revision_id)
47
        # this old format stores the full history, but this api doesn't
48
        # provide it, so we must generate, and might as well check it's
49
        # correct
50
        history = self._lefthand_history(revision_id)
51
        if len(history) != revno:
52
            raise AssertionError('%d != %d' % (len(history), revno))
53
        self._set_revision_history(history)
54
55
    def _read_last_revision_info(self):
56
        rh = self._revision_history()
57
        revno = len(rh)
58
        if revno:
59
            return (revno, rh[-1])
60
        else:
61
            return (0, _mod_revision.NULL_REVISION)
62
63
    def _set_revision_history(self, rev_history):
64
        if 'evil' in debug.debug_flags:
65
            mutter_callsite(3, "set_revision_history scales with history.")
66
        check_not_reserved_id = _mod_revision.check_not_reserved_id
67
        for rev_id in rev_history:
68
            check_not_reserved_id(rev_id)
69
        if Branch.hooks['post_change_branch_tip']:
70
            # Don't calculate the last_revision_info() if there are no hooks
71
            # that will use it.
72
            old_revno, old_revid = self.last_revision_info()
73
        if len(rev_history) == 0:
74
            revid = _mod_revision.NULL_REVISION
75
        else:
76
            revid = rev_history[-1]
77
        self._run_pre_change_branch_tip_hooks(len(rev_history), revid)
78
        self._write_revision_history(rev_history)
79
        self._clear_cached_state()
80
        self._cache_revision_history(rev_history)
81
        if Branch.hooks['post_change_branch_tip']:
82
            self._run_post_change_branch_tip_hooks(old_revno, old_revid)
83
84
    def _write_revision_history(self, history):
85
        """Factored out of set_revision_history.
86
87
        This performs the actual writing to disk.
88
        It is intended to be called by set_revision_history."""
89
        self._transport.put_bytes(
90
            'revision-history', '\n'.join(history),
91
            mode=self.bzrdir._get_file_mode())
92
93
    def _gen_revision_history(self):
94
        history = self._transport.get_bytes('revision-history').split('\n')
95
        if history[-1:] == ['']:
96
            # There shouldn't be a trailing newline, but just in case.
97
            history.pop()
98
        return history
99
100
    def _synchronize_history(self, destination, revision_id):
101
        if not isinstance(destination, FullHistoryBzrBranch):
102
            super(BzrBranch, self)._synchronize_history(
103
                destination, revision_id)
104
            return
105
        if revision_id == _mod_revision.NULL_REVISION:
106
            new_history = []
107
        else:
108
            new_history = self._revision_history()
109
        if revision_id is not None and new_history != []:
110
            try:
111
                new_history = new_history[:new_history.index(revision_id) + 1]
112
            except ValueError:
113
                rev = self.repository.get_revision(revision_id)
114
                new_history = rev.get_history(self.repository)[1:]
115
        destination._set_revision_history(new_history)
116
117
    @needs_write_lock
118
    def generate_revision_history(self, revision_id, last_rev=None,
119
        other_branch=None):
120
        """Create a new revision history that will finish with revision_id.
121
122
        :param revision_id: the new tip to use.
123
        :param last_rev: The previous last_revision. If not None, then this
124
            must be a ancestory of revision_id, or DivergedBranches is raised.
125
        :param other_branch: The other branch that DivergedBranches should
126
            raise with respect to.
127
        """
128
        self._set_revision_history(self._lefthand_history(revision_id,
129
            last_rev, other_branch))
130
131
132
class BzrBranch5(FullHistoryBzrBranch):
133
    """A format 5 branch. This supports new features over plain branches.
134
135
    It has support for a master_branch which is the data for bound branches.
136
    """
137
138
139
class BzrBranchFormat5(BranchFormatMetadir):
140
    """Bzr branch format 5.
141
142
    This format has:
143
     - a revision-history file.
144
     - a format string
145
     - a lock dir guarding the branch itself
146
     - all of this stored in a branch/ subdirectory
147
     - works with shared repositories.
148
149
    This format is new in bzr 0.8.
150
    """
151
152
    def _branch_class(self):
153
        return BzrBranch5
154
155
    @classmethod
156
    def get_format_string(cls):
157
        """See BranchFormat.get_format_string()."""
158
        return "Bazaar-NG branch format 5\n"
159
160
    def get_format_description(self):
161
        """See BranchFormat.get_format_description()."""
162
        return "Branch format 5"
163
164
    def initialize(self, a_bzrdir, name=None, repository=None,
165
                   append_revisions_only=None):
166
        """Create a branch of this format in a_bzrdir."""
167
        if append_revisions_only:
168
            raise errors.UpgradeRequired(a_bzrdir.user_url)
169
        utf8_files = [('revision-history', ''),
170
                      ('branch-name', ''),
171
                      ]
172
        return self._initialize_helper(a_bzrdir, utf8_files, name, repository)
173
174
    def supports_tags(self):
175
        return False
176
177
178