~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/branch.py

  • Committer: Robert Collins
  • Date: 2005-10-06 05:13:21 UTC
  • mfrom: (1393.3.3)
  • Revision ID: robertc@robertcollins.net-20051006051321-88f1053c3bf1ca4a
merge in an adjusted version of Jelmer's empty-log detection patch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2011 Canonical Ltd
2
 
#
 
1
# Copyright (C) 2005 Canonical Ltd
 
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
5
5
# the Free Software Foundation; either version 2 of the License, or
6
6
# (at your option) any later version.
7
 
#
 
7
 
8
8
# This program is distributed in the hope that it will be useful,
9
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
11
# GNU General Public License for more details.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
 
 
17
 
 
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
 
 
17
 
 
18
import sys
 
19
import os
 
20
import errno
 
21
from warnings import warn
18
22
from cStringIO import StringIO
19
 
import sys
20
 
 
21
 
from bzrlib.lazy_import import lazy_import
22
 
lazy_import(globals(), """
23
 
from itertools import chain
24
 
from bzrlib import (
25
 
        bzrdir,
26
 
        cache_utf8,
27
 
        cleanup,
28
 
        config as _mod_config,
29
 
        debug,
30
 
        errors,
31
 
        fetch,
32
 
        graph as _mod_graph,
33
 
        lockdir,
34
 
        lockable_files,
35
 
        remote,
36
 
        repository,
37
 
        revision as _mod_revision,
38
 
        rio,
39
 
        transport,
40
 
        ui,
41
 
        urlutils,
42
 
        )
43
 
from bzrlib.config import BranchConfig, TransportConfig
44
 
from bzrlib.tag import (
45
 
    BasicTags,
46
 
    DisabledTags,
47
 
    )
48
 
""")
49
 
 
50
 
from bzrlib import (
51
 
    controldir,
52
 
    )
53
 
from bzrlib.decorators import (
54
 
    needs_read_lock,
55
 
    needs_write_lock,
56
 
    only_raises,
57
 
    )
58
 
from bzrlib.hooks import Hooks
59
 
from bzrlib.inter import InterObject
60
 
from bzrlib.lock import _RelockDebugMixin, LogicalLockResult
61
 
from bzrlib import registry
62
 
from bzrlib.symbol_versioning import (
63
 
    deprecated_in,
64
 
    deprecated_method,
65
 
    )
66
 
from bzrlib.trace import mutter, mutter_callsite, note, is_quiet
67
 
 
68
 
 
69
 
class Branch(controldir.ControlComponent):
 
23
 
 
24
 
 
25
import bzrlib
 
26
from bzrlib.inventory import InventoryEntry
 
27
import bzrlib.inventory as inventory
 
28
from bzrlib.trace import mutter, note
 
29
from bzrlib.osutils import (isdir, quotefn, compact_date, rand_bytes, 
 
30
                            rename, splitpath, sha_file, appendpath, 
 
31
                            file_kind)
 
32
from bzrlib.errors import (BzrError, InvalidRevisionNumber, InvalidRevisionId,
 
33
                           NoSuchRevision, HistoryMissing, NotBranchError,
 
34
                           DivergedBranches, LockError, UnlistableStore,
 
35
                           UnlistableBranch, NoSuchFile)
 
36
from bzrlib.textui import show_status
 
37
from bzrlib.revision import Revision, is_ancestor
 
38
from bzrlib.delta import compare_trees
 
39
from bzrlib.tree import EmptyTree, RevisionTree
 
40
from bzrlib.inventory import Inventory
 
41
from bzrlib.store import copy_all
 
42
from bzrlib.store.compressed_text import CompressedTextStore
 
43
from bzrlib.store.text import TextStore
 
44
from bzrlib.store.weave import WeaveStore
 
45
from bzrlib.transport import Transport, get_transport
 
46
import bzrlib.xml5
 
47
import bzrlib.ui
 
48
 
 
49
 
 
50
BZR_BRANCH_FORMAT_4 = "Bazaar-NG branch, format 0.0.4\n"
 
51
BZR_BRANCH_FORMAT_5 = "Bazaar-NG branch, format 5\n"
 
52
## TODO: Maybe include checks for common corruption of newlines, etc?
 
53
 
 
54
 
 
55
# TODO: Some operations like log might retrieve the same revisions
 
56
# repeatedly to calculate deltas.  We could perhaps have a weakref
 
57
# cache in memory to make this faster.  In general anything can be
 
58
# cached in memory between lock and unlock operations.
 
59
 
 
60
def find_branch(*ignored, **ignored_too):
 
61
    # XXX: leave this here for about one release, then remove it
 
62
    raise NotImplementedError('find_branch() is not supported anymore, '
 
63
                              'please use one of the new branch constructors')
 
64
def _relpath(base, path):
 
65
    """Return path relative to base, or raise exception.
 
66
 
 
67
    The path may be either an absolute path or a path relative to the
 
68
    current working directory.
 
69
 
 
70
    Lifted out of Branch.relpath for ease of testing.
 
71
 
 
72
    os.path.commonprefix (python2.4) has a bad bug that it works just
 
73
    on string prefixes, assuming that '/u' is a prefix of '/u2'.  This
 
74
    avoids that problem."""
 
75
    rp = os.path.abspath(path)
 
76
 
 
77
    s = []
 
78
    head = rp
 
79
    while len(head) >= len(base):
 
80
        if head == base:
 
81
            break
 
82
        head, tail = os.path.split(head)
 
83
        if tail:
 
84
            s.insert(0, tail)
 
85
    else:
 
86
        raise NotBranchError("path %r is not within branch %r" % (rp, base))
 
87
 
 
88
    return os.sep.join(s)
 
89
        
 
90
 
 
91
def find_branch_root(t):
 
92
    """Find the branch root enclosing the transport's base.
 
93
 
 
94
    t is a Transport object.
 
95
 
 
96
    It is not necessary that the base of t exists.
 
97
 
 
98
    Basically we keep looking up until we find the control directory or
 
99
    run into the root.  If there isn't one, raises NotBranchError.
 
100
    """
 
101
    orig_base = t.base
 
102
    while True:
 
103
        if t.has(bzrlib.BZRDIR):
 
104
            return t
 
105
        new_t = t.clone('..')
 
106
        if new_t.base == t.base:
 
107
            # reached the root, whatever that may be
 
108
            raise NotBranchError('%s is not in a branch' % orig_base)
 
109
        t = new_t
 
110
 
 
111
 
 
112
######################################################################
 
113
# branch objects
 
114
 
 
115
class Branch(object):
70
116
    """Branch holding a history of revisions.
71
117
 
72
 
    :ivar base:
73
 
        Base directory/url of the branch; using control_url and
74
 
        control_transport is more standardized.
75
 
    :ivar hooks: An instance of BranchHooks.
76
 
    :ivar _master_branch_cache: cached result of get_master_branch, see
77
 
        _clear_cached_state.
 
118
    base
 
119
        Base directory/url of the branch.
78
120
    """
79
 
    # this is really an instance variable - FIXME move it there
80
 
    # - RBC 20060112
81
121
    base = None
82
122
 
83
 
    @property
84
 
    def control_transport(self):
85
 
        return self._transport
86
 
 
87
 
    @property
88
 
    def user_transport(self):
89
 
        return self.bzrdir.user_transport
90
 
 
91
123
    def __init__(self, *ignored, **ignored_too):
92
 
        self.tags = self._format.make_tags(self)
93
 
        self._revision_history_cache = None
94
 
        self._revision_id_to_revno_cache = None
95
 
        self._partial_revision_id_to_revno_cache = {}
96
 
        self._partial_revision_history_cache = []
97
 
        self._tags_bytes = None
98
 
        self._last_revision_info_cache = None
99
 
        self._master_branch_cache = None
100
 
        self._merge_sorted_revisions_cache = None
101
 
        self._open_hook()
102
 
        hooks = Branch.hooks['open']
103
 
        for hook in hooks:
104
 
            hook(self)
105
 
 
106
 
    def _open_hook(self):
107
 
        """Called by init to allow simpler extension of the base class."""
108
 
 
109
 
    def _activate_fallback_location(self, url):
110
 
        """Activate the branch/repository from url as a fallback repository."""
111
 
        for existing_fallback_repo in self.repository._fallback_repositories:
112
 
            if existing_fallback_repo.user_url == url:
113
 
                # This fallback is already configured.  This probably only
114
 
                # happens because BzrDir.sprout is a horrible mess.  To avoid
115
 
                # confusing _unstack we don't add this a second time.
116
 
                mutter('duplicate activation of fallback %r on %r', url, self)
117
 
                return
118
 
        repo = self._get_fallback_repository(url)
119
 
        if repo.has_same_location(self.repository):
120
 
            raise errors.UnstackableLocationError(self.user_url, url)
121
 
        self.repository.add_fallback_repository(repo)
122
 
 
123
 
    def break_lock(self):
124
 
        """Break a lock if one is present from another instance.
125
 
 
126
 
        Uses the ui factory to ask for confirmation if the lock may be from
127
 
        an active process.
128
 
 
129
 
        This will probe the repository for its lock as well.
130
 
        """
131
 
        self.control_files.break_lock()
132
 
        self.repository.break_lock()
133
 
        master = self.get_master_branch()
134
 
        if master is not None:
135
 
            master.break_lock()
136
 
 
137
 
    def _check_stackable_repo(self):
138
 
        if not self.repository._format.supports_external_lookups:
139
 
            raise errors.UnstackableRepositoryFormat(self.repository._format,
140
 
                self.repository.base)
141
 
 
142
 
    def _extend_partial_history(self, stop_index=None, stop_revision=None):
143
 
        """Extend the partial history to include a given index
144
 
 
145
 
        If a stop_index is supplied, stop when that index has been reached.
146
 
        If a stop_revision is supplied, stop when that revision is
147
 
        encountered.  Otherwise, stop when the beginning of history is
148
 
        reached.
149
 
 
150
 
        :param stop_index: The index which should be present.  When it is
151
 
            present, history extension will stop.
152
 
        :param stop_revision: The revision id which should be present.  When
153
 
            it is encountered, history extension will stop.
154
 
        """
155
 
        if len(self._partial_revision_history_cache) == 0:
156
 
            self._partial_revision_history_cache = [self.last_revision()]
157
 
        repository._iter_for_revno(
158
 
            self.repository, self._partial_revision_history_cache,
159
 
            stop_index=stop_index, stop_revision=stop_revision)
160
 
        if self._partial_revision_history_cache[-1] == _mod_revision.NULL_REVISION:
161
 
            self._partial_revision_history_cache.pop()
162
 
 
163
 
    def _get_check_refs(self):
164
 
        """Get the references needed for check().
165
 
 
166
 
        See bzrlib.check.
167
 
        """
168
 
        revid = self.last_revision()
169
 
        return [('revision-existence', revid), ('lefthand-distance', revid)]
170
 
 
171
 
    @staticmethod
172
 
    def open(base, _unsupported=False, possible_transports=None):
173
 
        """Open the branch rooted at base.
174
 
 
175
 
        For instance, if the branch is at URL/.bzr/branch,
176
 
        Branch.open(URL) -> a Branch instance.
177
 
        """
178
 
        control = bzrdir.BzrDir.open(base, _unsupported,
179
 
                                     possible_transports=possible_transports)
180
 
        return control.open_branch(unsupported=_unsupported)
181
 
 
182
 
    @staticmethod
183
 
    def open_from_transport(transport, name=None, _unsupported=False):
184
 
        """Open the branch rooted at transport"""
185
 
        control = bzrdir.BzrDir.open_from_transport(transport, _unsupported)
186
 
        return control.open_branch(name=name, unsupported=_unsupported)
187
 
 
188
 
    @staticmethod
189
 
    def open_containing(url, possible_transports=None):
 
124
        raise NotImplementedError('The Branch class is abstract')
 
125
 
 
126
    @staticmethod
 
127
    def open_downlevel(base):
 
128
        """Open a branch which may be of an old format.
 
129
        
 
130
        Only local branches are supported."""
 
131
        return _Branch(get_transport(base), relax_version_check=True)
 
132
        
 
133
    @staticmethod
 
134
    def open(base):
 
135
        """Open an existing branch, rooted at 'base' (url)"""
 
136
        t = get_transport(base)
 
137
        return _Branch(t)
 
138
 
 
139
    @staticmethod
 
140
    def open_containing(url):
190
141
        """Open an existing branch which contains url.
191
 
 
 
142
        
192
143
        This probes for a branch at url, and searches upwards from there.
193
 
 
194
 
        Basically we keep looking up until we find the control directory or
195
 
        run into the root.  If there isn't one, raises NotBranchError.
196
 
        If there is one and it is either an unrecognised format or an unsupported
197
 
        format, UnknownFormatError or UnsupportedFormatError are raised.
198
 
        If there is one, it is returned, along with the unused portion of url.
199
 
        """
200
 
        control, relpath = bzrdir.BzrDir.open_containing(url,
201
 
                                                         possible_transports)
202
 
        return control.open_branch(), relpath
203
 
 
204
 
    def _push_should_merge_tags(self):
205
 
        """Should _basic_push merge this branch's tags into the target?
206
 
 
207
 
        The default implementation returns False if this branch has no tags,
208
 
        and True the rest of the time.  Subclasses may override this.
209
 
        """
210
 
        return self.supports_tags() and self.tags.get_tag_dict()
211
 
 
212
 
    def get_config(self):
213
 
        """Get a bzrlib.config.BranchConfig for this Branch.
214
 
 
215
 
        This can then be used to get and set configuration options for the
216
 
        branch.
217
 
 
218
 
        :return: A bzrlib.config.BranchConfig.
219
 
        """
220
 
        return BranchConfig(self)
221
 
 
222
 
    def _get_config(self):
223
 
        """Get the concrete config for just the config in this branch.
224
 
 
225
 
        This is not intended for client use; see Branch.get_config for the
226
 
        public API.
227
 
 
228
 
        Added in 1.14.
229
 
 
230
 
        :return: An object supporting get_option and set_option.
231
 
        """
232
 
        raise NotImplementedError(self._get_config)
233
 
 
234
 
    def _get_fallback_repository(self, url):
235
 
        """Get the repository we fallback to at url."""
236
 
        url = urlutils.join(self.base, url)
237
 
        a_branch = Branch.open(url,
238
 
            possible_transports=[self.bzrdir.root_transport])
239
 
        return a_branch.repository
240
 
 
241
 
    @needs_read_lock
242
 
    def _get_tags_bytes(self):
243
 
        """Get the bytes of a serialised tags dict.
244
 
 
245
 
        Note that not all branches support tags, nor do all use the same tags
246
 
        logic: this method is specific to BasicTags. Other tag implementations
247
 
        may use the same method name and behave differently, safely, because
248
 
        of the double-dispatch via
249
 
        format.make_tags->tags_instance->get_tags_dict.
250
 
 
251
 
        :return: The bytes of the tags file.
252
 
        :seealso: Branch._set_tags_bytes.
253
 
        """
254
 
        if self._tags_bytes is None:
255
 
            self._tags_bytes = self._transport.get_bytes('tags')
256
 
        return self._tags_bytes
257
 
 
258
 
    def _get_nick(self, local=False, possible_transports=None):
259
 
        config = self.get_config()
260
 
        # explicit overrides master, but don't look for master if local is True
261
 
        if not local and not config.has_explicit_nickname():
 
144
        """
 
145
        t = get_transport(url)
 
146
        t = find_branch_root(t)
 
147
        return _Branch(t)
 
148
 
 
149
    @staticmethod
 
150
    def initialize(base):
 
151
        """Create a new branch, rooted at 'base' (url)"""
 
152
        t = get_transport(base)
 
153
        return _Branch(t, init=True)
 
154
 
 
155
    def setup_caching(self, cache_root):
 
156
        """Subclasses that care about caching should override this, and set
 
157
        up cached stores located under cache_root.
 
158
        """
 
159
        self.cache_root = cache_root
 
160
 
 
161
 
 
162
class _Branch(Branch):
 
163
    """A branch stored in the actual filesystem.
 
164
 
 
165
    Note that it's "local" in the context of the filesystem; it doesn't
 
166
    really matter if it's on an nfs/smb/afs/coda/... share, as long as
 
167
    it's writable, and can be accessed via the normal filesystem API.
 
168
 
 
169
    _lock_mode
 
170
        None, or 'r' or 'w'
 
171
 
 
172
    _lock_count
 
173
        If _lock_mode is true, a positive count of the number of times the
 
174
        lock has been taken.
 
175
 
 
176
    _lock
 
177
        Lock object from bzrlib.lock.
 
178
    """
 
179
    # We actually expect this class to be somewhat short-lived; part of its
 
180
    # purpose is to try to isolate what bits of the branch logic are tied to
 
181
    # filesystem access, so that in a later step, we can extricate them to
 
182
    # a separarte ("storage") class.
 
183
    _lock_mode = None
 
184
    _lock_count = None
 
185
    _lock = None
 
186
    _inventory_weave = None
 
187
    
 
188
    # Map some sort of prefix into a namespace
 
189
    # stuff like "revno:10", "revid:", etc.
 
190
    # This should match a prefix with a function which accepts
 
191
    REVISION_NAMESPACES = {}
 
192
 
 
193
    def push_stores(self, branch_to):
 
194
        """Copy the content of this branches store to branch_to."""
 
195
        if (self._branch_format != branch_to._branch_format
 
196
            or self._branch_format != 4):
 
197
            from bzrlib.fetch import greedy_fetch
 
198
            mutter("falling back to fetch logic to push between %s(%s) and %s(%s)",
 
199
                   self, self._branch_format, branch_to, branch_to._branch_format)
 
200
            greedy_fetch(to_branch=branch_to, from_branch=self,
 
201
                         revision=self.last_revision())
 
202
            return
 
203
 
 
204
        store_pairs = ((self.text_store,      branch_to.text_store),
 
205
                       (self.inventory_store, branch_to.inventory_store),
 
206
                       (self.revision_store,  branch_to.revision_store))
 
207
        try:
 
208
            for from_store, to_store in store_pairs: 
 
209
                copy_all(from_store, to_store)
 
210
        except UnlistableStore:
 
211
            raise UnlistableBranch(from_store)
 
212
 
 
213
    def __init__(self, transport, init=False,
 
214
                 relax_version_check=False):
 
215
        """Create new branch object at a particular location.
 
216
 
 
217
        transport -- A Transport object, defining how to access files.
 
218
                (If a string, transport.transport() will be used to
 
219
                create a Transport object)
 
220
        
 
221
        init -- If True, create new control files in a previously
 
222
             unversioned directory.  If False, the branch must already
 
223
             be versioned.
 
224
 
 
225
        relax_version_check -- If true, the usual check for the branch
 
226
            version is not applied.  This is intended only for
 
227
            upgrade/recovery type use; it's not guaranteed that
 
228
            all operations will work on old format branches.
 
229
 
 
230
        In the test suite, creation of new trees is tested using the
 
231
        `ScratchBranch` class.
 
232
        """
 
233
        assert isinstance(transport, Transport), \
 
234
            "%r is not a Transport" % transport
 
235
        self._transport = transport
 
236
        if init:
 
237
            self._make_control()
 
238
        self._check_format(relax_version_check)
 
239
 
 
240
        def get_store(name, compressed=True):
 
241
            # FIXME: This approach of assuming stores are all entirely compressed
 
242
            # or entirely uncompressed is tidy, but breaks upgrade from 
 
243
            # some existing branches where there's a mixture; we probably 
 
244
            # still want the option to look for both.
 
245
            relpath = self._rel_controlfilename(name)
 
246
            if compressed:
 
247
                store = CompressedTextStore(self._transport.clone(relpath))
 
248
            else:
 
249
                store = TextStore(self._transport.clone(relpath))
 
250
            #if self._transport.should_cache():
 
251
            #    cache_path = os.path.join(self.cache_root, name)
 
252
            #    os.mkdir(cache_path)
 
253
            #    store = bzrlib.store.CachedStore(store, cache_path)
 
254
            return store
 
255
        def get_weave(name):
 
256
            relpath = self._rel_controlfilename(name)
 
257
            ws = WeaveStore(self._transport.clone(relpath))
 
258
            if self._transport.should_cache():
 
259
                ws.enable_cache = True
 
260
            return ws
 
261
 
 
262
        if self._branch_format == 4:
 
263
            self.inventory_store = get_store('inventory-store')
 
264
            self.text_store = get_store('text-store')
 
265
            self.revision_store = get_store('revision-store')
 
266
        elif self._branch_format == 5:
 
267
            self.control_weaves = get_weave([])
 
268
            self.weave_store = get_weave('weaves')
 
269
            self.revision_store = get_store('revision-store', compressed=False)
 
270
 
 
271
    def __str__(self):
 
272
        return '%s(%r)' % (self.__class__.__name__, self._transport.base)
 
273
 
 
274
 
 
275
    __repr__ = __str__
 
276
 
 
277
 
 
278
    def __del__(self):
 
279
        if self._lock_mode or self._lock:
 
280
            # XXX: This should show something every time, and be suitable for
 
281
            # headless operation and embedding
 
282
            warn("branch %r was not explicitly unlocked" % self)
 
283
            self._lock.unlock()
 
284
 
 
285
        # TODO: It might be best to do this somewhere else,
 
286
        # but it is nice for a Branch object to automatically
 
287
        # cache it's information.
 
288
        # Alternatively, we could have the Transport objects cache requests
 
289
        # See the earlier discussion about how major objects (like Branch)
 
290
        # should never expect their __del__ function to run.
 
291
        if hasattr(self, 'cache_root') and self.cache_root is not None:
262
292
            try:
263
 
                master = self.get_master_branch(possible_transports)
264
 
                if master and self.user_url == master.user_url:
265
 
                    raise errors.RecursiveBind(self.user_url)
266
 
                if master is not None:
267
 
                    # return the master branch value
268
 
                    return master.nick
269
 
            except errors.RecursiveBind, e:
270
 
                raise e
271
 
            except errors.BzrError, e:
272
 
                # Silently fall back to local implicit nick if the master is
273
 
                # unavailable
274
 
                mutter("Could not connect to bound branch, "
275
 
                    "falling back to local nick.\n " + str(e))
276
 
        return config.get_nickname()
277
 
 
278
 
    def _set_nick(self, nick):
279
 
        self.get_config().set_user_option('nickname', nick, warn_masked=True)
280
 
 
281
 
    nick = property(_get_nick, _set_nick)
282
 
 
283
 
    def is_locked(self):
284
 
        raise NotImplementedError(self.is_locked)
285
 
 
286
 
    def _lefthand_history(self, revision_id, last_rev=None,
287
 
                          other_branch=None):
288
 
        if 'evil' in debug.debug_flags:
289
 
            mutter_callsite(4, "_lefthand_history scales with history.")
290
 
        # stop_revision must be a descendant of last_revision
291
 
        graph = self.repository.get_graph()
292
 
        if last_rev is not None:
293
 
            if not graph.is_ancestor(last_rev, revision_id):
294
 
                # our previous tip is not merged into stop_revision
295
 
                raise errors.DivergedBranches(self, other_branch)
296
 
        # make a new revision history from the graph
297
 
        parents_map = graph.get_parent_map([revision_id])
298
 
        if revision_id not in parents_map:
299
 
            raise errors.NoSuchRevision(self, revision_id)
300
 
        current_rev_id = revision_id
301
 
        new_history = []
302
 
        check_not_reserved_id = _mod_revision.check_not_reserved_id
303
 
        # Do not include ghosts or graph origin in revision_history
304
 
        while (current_rev_id in parents_map and
305
 
               len(parents_map[current_rev_id]) > 0):
306
 
            check_not_reserved_id(current_rev_id)
307
 
            new_history.append(current_rev_id)
308
 
            current_rev_id = parents_map[current_rev_id][0]
309
 
            parents_map = graph.get_parent_map([current_rev_id])
310
 
        new_history.reverse()
311
 
        return new_history
312
 
 
313
 
    def lock_write(self, token=None):
314
 
        """Lock the branch for write operations.
315
 
 
316
 
        :param token: A token to permit reacquiring a previously held and
317
 
            preserved lock.
318
 
        :return: A BranchWriteLockResult.
319
 
        """
320
 
        raise NotImplementedError(self.lock_write)
 
293
                import shutil
 
294
                shutil.rmtree(self.cache_root)
 
295
            except:
 
296
                pass
 
297
            self.cache_root = None
 
298
 
 
299
    def _get_base(self):
 
300
        if self._transport:
 
301
            return self._transport.base
 
302
        return None
 
303
 
 
304
    base = property(_get_base)
 
305
 
 
306
 
 
307
    def lock_write(self):
 
308
        # TODO: Upgrade locking to support using a Transport,
 
309
        # and potentially a remote locking protocol
 
310
        if self._lock_mode:
 
311
            if self._lock_mode != 'w':
 
312
                raise LockError("can't upgrade to a write lock from %r" %
 
313
                                self._lock_mode)
 
314
            self._lock_count += 1
 
315
        else:
 
316
            self._lock = self._transport.lock_write(
 
317
                    self._rel_controlfilename('branch-lock'))
 
318
            self._lock_mode = 'w'
 
319
            self._lock_count = 1
 
320
 
321
321
 
322
322
    def lock_read(self):
323
 
        """Lock the branch for read operations.
324
 
 
325
 
        :return: A bzrlib.lock.LogicalLockResult.
326
 
        """
327
 
        raise NotImplementedError(self.lock_read)
328
 
 
 
323
        if self._lock_mode:
 
324
            assert self._lock_mode in ('r', 'w'), \
 
325
                   "invalid lock mode %r" % self._lock_mode
 
326
            self._lock_count += 1
 
327
        else:
 
328
            self._lock = self._transport.lock_read(
 
329
                    self._rel_controlfilename('branch-lock'))
 
330
            self._lock_mode = 'r'
 
331
            self._lock_count = 1
 
332
                        
329
333
    def unlock(self):
330
 
        raise NotImplementedError(self.unlock)
331
 
 
332
 
    def peek_lock_mode(self):
333
 
        """Return lock mode for the Branch: 'r', 'w' or None"""
334
 
        raise NotImplementedError(self.peek_lock_mode)
335
 
 
336
 
    def get_physical_lock_status(self):
337
 
        raise NotImplementedError(self.get_physical_lock_status)
338
 
 
339
 
    @needs_read_lock
340
 
    def dotted_revno_to_revision_id(self, revno, _cache_reverse=False):
341
 
        """Return the revision_id for a dotted revno.
342
 
 
343
 
        :param revno: a tuple like (1,) or (1,1,2)
344
 
        :param _cache_reverse: a private parameter enabling storage
345
 
           of the reverse mapping in a top level cache. (This should
346
 
           only be done in selective circumstances as we want to
347
 
           avoid having the mapping cached multiple times.)
348
 
        :return: the revision_id
349
 
        :raises errors.NoSuchRevision: if the revno doesn't exist
350
 
        """
351
 
        rev_id = self._do_dotted_revno_to_revision_id(revno)
352
 
        if _cache_reverse:
353
 
            self._partial_revision_id_to_revno_cache[rev_id] = revno
354
 
        return rev_id
355
 
 
356
 
    def _do_dotted_revno_to_revision_id(self, revno):
357
 
        """Worker function for dotted_revno_to_revision_id.
358
 
 
359
 
        Subclasses should override this if they wish to
360
 
        provide a more efficient implementation.
361
 
        """
362
 
        if len(revno) == 1:
363
 
            return self.get_rev_id(revno[0])
364
 
        revision_id_to_revno = self.get_revision_id_to_revno_map()
365
 
        revision_ids = [revision_id for revision_id, this_revno
366
 
                        in revision_id_to_revno.iteritems()
367
 
                        if revno == this_revno]
368
 
        if len(revision_ids) == 1:
369
 
            return revision_ids[0]
370
 
        else:
371
 
            revno_str = '.'.join(map(str, revno))
372
 
            raise errors.NoSuchRevision(self, revno_str)
373
 
 
374
 
    @needs_read_lock
375
 
    def revision_id_to_dotted_revno(self, revision_id):
376
 
        """Given a revision id, return its dotted revno.
377
 
 
378
 
        :return: a tuple like (1,) or (400,1,3).
379
 
        """
380
 
        return self._do_revision_id_to_dotted_revno(revision_id)
381
 
 
382
 
    def _do_revision_id_to_dotted_revno(self, revision_id):
383
 
        """Worker function for revision_id_to_revno."""
384
 
        # Try the caches if they are loaded
385
 
        result = self._partial_revision_id_to_revno_cache.get(revision_id)
386
 
        if result is not None:
387
 
            return result
388
 
        if self._revision_id_to_revno_cache:
389
 
            result = self._revision_id_to_revno_cache.get(revision_id)
390
 
            if result is None:
391
 
                raise errors.NoSuchRevision(self, revision_id)
392
 
        # Try the mainline as it's optimised
393
 
        try:
394
 
            revno = self.revision_id_to_revno(revision_id)
395
 
            return (revno,)
396
 
        except errors.NoSuchRevision:
397
 
            # We need to load and use the full revno map after all
398
 
            result = self.get_revision_id_to_revno_map().get(revision_id)
399
 
            if result is None:
400
 
                raise errors.NoSuchRevision(self, revision_id)
401
 
        return result
402
 
 
403
 
    @needs_read_lock
404
 
    def get_revision_id_to_revno_map(self):
405
 
        """Return the revision_id => dotted revno map.
406
 
 
407
 
        This will be regenerated on demand, but will be cached.
408
 
 
409
 
        :return: A dictionary mapping revision_id => dotted revno.
410
 
            This dictionary should not be modified by the caller.
411
 
        """
412
 
        if self._revision_id_to_revno_cache is not None:
413
 
            mapping = self._revision_id_to_revno_cache
414
 
        else:
415
 
            mapping = self._gen_revno_map()
416
 
            self._cache_revision_id_to_revno(mapping)
417
 
        # TODO: jam 20070417 Since this is being cached, should we be returning
418
 
        #       a copy?
419
 
        # I would rather not, and instead just declare that users should not
420
 
        # modify the return value.
421
 
        return mapping
422
 
 
423
 
    def _gen_revno_map(self):
424
 
        """Create a new mapping from revision ids to dotted revnos.
425
 
 
426
 
        Dotted revnos are generated based on the current tip in the revision
427
 
        history.
428
 
        This is the worker function for get_revision_id_to_revno_map, which
429
 
        just caches the return value.
430
 
 
431
 
        :return: A dictionary mapping revision_id => dotted revno.
432
 
        """
433
 
        revision_id_to_revno = dict((rev_id, revno)
434
 
            for rev_id, depth, revno, end_of_merge
435
 
             in self.iter_merge_sorted_revisions())
436
 
        return revision_id_to_revno
437
 
 
438
 
    @needs_read_lock
439
 
    def iter_merge_sorted_revisions(self, start_revision_id=None,
440
 
            stop_revision_id=None, stop_rule='exclude', direction='reverse'):
441
 
        """Walk the revisions for a branch in merge sorted order.
442
 
 
443
 
        Merge sorted order is the output from a merge-aware,
444
 
        topological sort, i.e. all parents come before their
445
 
        children going forward; the opposite for reverse.
446
 
 
447
 
        :param start_revision_id: the revision_id to begin walking from.
448
 
            If None, the branch tip is used.
449
 
        :param stop_revision_id: the revision_id to terminate the walk
450
 
            after. If None, the rest of history is included.
451
 
        :param stop_rule: if stop_revision_id is not None, the precise rule
452
 
            to use for termination:
453
 
 
454
 
            * 'exclude' - leave the stop revision out of the result (default)
455
 
            * 'include' - the stop revision is the last item in the result
456
 
            * 'with-merges' - include the stop revision and all of its
457
 
              merged revisions in the result
458
 
            * 'with-merges-without-common-ancestry' - filter out revisions 
459
 
              that are in both ancestries
460
 
        :param direction: either 'reverse' or 'forward':
461
 
 
462
 
            * reverse means return the start_revision_id first, i.e.
463
 
              start at the most recent revision and go backwards in history
464
 
            * forward returns tuples in the opposite order to reverse.
465
 
              Note in particular that forward does *not* do any intelligent
466
 
              ordering w.r.t. depth as some clients of this API may like.
467
 
              (If required, that ought to be done at higher layers.)
468
 
 
469
 
        :return: an iterator over (revision_id, depth, revno, end_of_merge)
470
 
            tuples where:
471
 
 
472
 
            * revision_id: the unique id of the revision
473
 
            * depth: How many levels of merging deep this node has been
474
 
              found.
475
 
            * revno_sequence: This field provides a sequence of
476
 
              revision numbers for all revisions. The format is:
477
 
              (REVNO, BRANCHNUM, BRANCHREVNO). BRANCHNUM is the number of the
478
 
              branch that the revno is on. From left to right the REVNO numbers
479
 
              are the sequence numbers within that branch of the revision.
480
 
            * end_of_merge: When True the next node (earlier in history) is
481
 
              part of a different merge.
482
 
        """
483
 
        # Note: depth and revno values are in the context of the branch so
484
 
        # we need the full graph to get stable numbers, regardless of the
485
 
        # start_revision_id.
486
 
        if self._merge_sorted_revisions_cache is None:
487
 
            last_revision = self.last_revision()
488
 
            known_graph = self.repository.get_known_graph_ancestry(
489
 
                [last_revision])
490
 
            self._merge_sorted_revisions_cache = known_graph.merge_sort(
491
 
                last_revision)
492
 
        filtered = self._filter_merge_sorted_revisions(
493
 
            self._merge_sorted_revisions_cache, start_revision_id,
494
 
            stop_revision_id, stop_rule)
495
 
        # Make sure we don't return revisions that are not part of the
496
 
        # start_revision_id ancestry.
497
 
        filtered = self._filter_start_non_ancestors(filtered)
498
 
        if direction == 'reverse':
499
 
            return filtered
500
 
        if direction == 'forward':
501
 
            return reversed(list(filtered))
502
 
        else:
503
 
            raise ValueError('invalid direction %r' % direction)
504
 
 
505
 
    def _filter_merge_sorted_revisions(self, merge_sorted_revisions,
506
 
        start_revision_id, stop_revision_id, stop_rule):
507
 
        """Iterate over an inclusive range of sorted revisions."""
508
 
        rev_iter = iter(merge_sorted_revisions)
509
 
        if start_revision_id is not None:
510
 
            for node in rev_iter:
511
 
                rev_id = node.key
512
 
                if rev_id != start_revision_id:
513
 
                    continue
 
334
        if not self._lock_mode:
 
335
            raise LockError('branch %r is not locked' % (self))
 
336
 
 
337
        if self._lock_count > 1:
 
338
            self._lock_count -= 1
 
339
        else:
 
340
            self._lock.unlock()
 
341
            self._lock = None
 
342
            self._lock_mode = self._lock_count = None
 
343
 
 
344
    def abspath(self, name):
 
345
        """Return absolute filename for something in the branch"""
 
346
        return self._transport.abspath(name)
 
347
 
 
348
    def relpath(self, path):
 
349
        """Return path relative to this branch of something inside it.
 
350
 
 
351
        Raises an error if path is not in this branch."""
 
352
        return self._transport.relpath(path)
 
353
 
 
354
 
 
355
    def _rel_controlfilename(self, file_or_path):
 
356
        if isinstance(file_or_path, basestring):
 
357
            file_or_path = [file_or_path]
 
358
        return [bzrlib.BZRDIR] + file_or_path
 
359
 
 
360
    def controlfilename(self, file_or_path):
 
361
        """Return location relative to branch."""
 
362
        return self._transport.abspath(self._rel_controlfilename(file_or_path))
 
363
 
 
364
 
 
365
    def controlfile(self, file_or_path, mode='r'):
 
366
        """Open a control file for this branch.
 
367
 
 
368
        There are two classes of file in the control directory: text
 
369
        and binary.  binary files are untranslated byte streams.  Text
 
370
        control files are stored with Unix newlines and in UTF-8, even
 
371
        if the platform or locale defaults are different.
 
372
 
 
373
        Controlfiles should almost never be opened in write mode but
 
374
        rather should be atomically copied and replaced using atomicfile.
 
375
        """
 
376
        import codecs
 
377
 
 
378
        relpath = self._rel_controlfilename(file_or_path)
 
379
        #TODO: codecs.open() buffers linewise, so it was overloaded with
 
380
        # a much larger buffer, do we need to do the same for getreader/getwriter?
 
381
        if mode == 'rb': 
 
382
            return self._transport.get(relpath)
 
383
        elif mode == 'wb':
 
384
            raise BzrError("Branch.controlfile(mode='wb') is not supported, use put_controlfiles")
 
385
        elif mode == 'r':
 
386
            return codecs.getreader('utf-8')(self._transport.get(relpath), errors='replace')
 
387
        elif mode == 'w':
 
388
            raise BzrError("Branch.controlfile(mode='w') is not supported, use put_controlfiles")
 
389
        else:
 
390
            raise BzrError("invalid controlfile mode %r" % mode)
 
391
 
 
392
    def put_controlfile(self, path, f, encode=True):
 
393
        """Write an entry as a controlfile.
 
394
 
 
395
        :param path: The path to put the file, relative to the .bzr control
 
396
                     directory
 
397
        :param f: A file-like or string object whose contents should be copied.
 
398
        :param encode:  If true, encode the contents as utf-8
 
399
        """
 
400
        self.put_controlfiles([(path, f)], encode=encode)
 
401
 
 
402
    def put_controlfiles(self, files, encode=True):
 
403
        """Write several entries as controlfiles.
 
404
 
 
405
        :param files: A list of [(path, file)] pairs, where the path is the directory
 
406
                      underneath the bzr control directory
 
407
        :param encode:  If true, encode the contents as utf-8
 
408
        """
 
409
        import codecs
 
410
        ctrl_files = []
 
411
        for path, f in files:
 
412
            if encode:
 
413
                if isinstance(f, basestring):
 
414
                    f = f.encode('utf-8', 'replace')
514
415
                else:
515
 
                    # The decision to include the start or not
516
 
                    # depends on the stop_rule if a stop is provided
517
 
                    # so pop this node back into the iterator
518
 
                    rev_iter = chain(iter([node]), rev_iter)
519
 
                    break
520
 
        if stop_revision_id is None:
521
 
            # Yield everything
522
 
            for node in rev_iter:
523
 
                rev_id = node.key
524
 
                yield (rev_id, node.merge_depth, node.revno,
525
 
                       node.end_of_merge)
526
 
        elif stop_rule == 'exclude':
527
 
            for node in rev_iter:
528
 
                rev_id = node.key
529
 
                if rev_id == stop_revision_id:
530
 
                    return
531
 
                yield (rev_id, node.merge_depth, node.revno,
532
 
                       node.end_of_merge)
533
 
        elif stop_rule == 'include':
534
 
            for node in rev_iter:
535
 
                rev_id = node.key
536
 
                yield (rev_id, node.merge_depth, node.revno,
537
 
                       node.end_of_merge)
538
 
                if rev_id == stop_revision_id:
539
 
                    return
540
 
        elif stop_rule == 'with-merges-without-common-ancestry':
541
 
            # We want to exclude all revisions that are already part of the
542
 
            # stop_revision_id ancestry.
543
 
            graph = self.repository.get_graph()
544
 
            ancestors = graph.find_unique_ancestors(start_revision_id,
545
 
                                                    [stop_revision_id])
546
 
            for node in rev_iter:
547
 
                rev_id = node.key
548
 
                if rev_id not in ancestors:
549
 
                    continue
550
 
                yield (rev_id, node.merge_depth, node.revno,
551
 
                       node.end_of_merge)
552
 
        elif stop_rule == 'with-merges':
553
 
            stop_rev = self.repository.get_revision(stop_revision_id)
554
 
            if stop_rev.parent_ids:
555
 
                left_parent = stop_rev.parent_ids[0]
 
416
                    f = codecs.getwriter('utf-8')(f, errors='replace')
 
417
            path = self._rel_controlfilename(path)
 
418
            ctrl_files.append((path, f))
 
419
        self._transport.put_multi(ctrl_files)
 
420
 
 
421
    def _make_control(self):
 
422
        from bzrlib.inventory import Inventory
 
423
        from bzrlib.weavefile import write_weave_v5
 
424
        from bzrlib.weave import Weave
 
425
        
 
426
        # Create an empty inventory
 
427
        sio = StringIO()
 
428
        # if we want per-tree root ids then this is the place to set
 
429
        # them; they're not needed for now and so ommitted for
 
430
        # simplicity.
 
431
        bzrlib.xml5.serializer_v5.write_inventory(Inventory(), sio)
 
432
        empty_inv = sio.getvalue()
 
433
        sio = StringIO()
 
434
        bzrlib.weavefile.write_weave_v5(Weave(), sio)
 
435
        empty_weave = sio.getvalue()
 
436
 
 
437
        dirs = [[], 'revision-store', 'weaves']
 
438
        files = [('README', 
 
439
            "This is a Bazaar-NG control directory.\n"
 
440
            "Do not change any files in this directory.\n"),
 
441
            ('branch-format', BZR_BRANCH_FORMAT_5),
 
442
            ('revision-history', ''),
 
443
            ('branch-name', ''),
 
444
            ('branch-lock', ''),
 
445
            ('pending-merges', ''),
 
446
            ('inventory', empty_inv),
 
447
            ('inventory.weave', empty_weave),
 
448
            ('ancestry.weave', empty_weave)
 
449
        ]
 
450
        cfn = self._rel_controlfilename
 
451
        self._transport.mkdir_multi([cfn(d) for d in dirs])
 
452
        self.put_controlfiles(files)
 
453
        mutter('created control directory in ' + self._transport.base)
 
454
 
 
455
    def _check_format(self, relax_version_check):
 
456
        """Check this branch format is supported.
 
457
 
 
458
        The format level is stored, as an integer, in
 
459
        self._branch_format for code that needs to check it later.
 
460
 
 
461
        In the future, we might need different in-memory Branch
 
462
        classes to support downlevel branches.  But not yet.
 
463
        """
 
464
        try:
 
465
            fmt = self.controlfile('branch-format', 'r').read()
 
466
        except NoSuchFile:
 
467
            raise NotBranchError(self.base)
 
468
 
 
469
        if fmt == BZR_BRANCH_FORMAT_5:
 
470
            self._branch_format = 5
 
471
        elif fmt == BZR_BRANCH_FORMAT_4:
 
472
            self._branch_format = 4
 
473
 
 
474
        if (not relax_version_check
 
475
            and self._branch_format != 5):
 
476
            raise BzrError('sorry, branch format %r not supported' % fmt,
 
477
                           ['use a different bzr version',
 
478
                            'or remove the .bzr directory'
 
479
                            ' and "bzr init" again'])
 
480
 
 
481
    def get_root_id(self):
 
482
        """Return the id of this branches root"""
 
483
        inv = self.read_working_inventory()
 
484
        return inv.root.file_id
 
485
 
 
486
    def set_root_id(self, file_id):
 
487
        inv = self.read_working_inventory()
 
488
        orig_root_id = inv.root.file_id
 
489
        del inv._byid[inv.root.file_id]
 
490
        inv.root.file_id = file_id
 
491
        inv._byid[inv.root.file_id] = inv.root
 
492
        for fid in inv:
 
493
            entry = inv[fid]
 
494
            if entry.parent_id in (None, orig_root_id):
 
495
                entry.parent_id = inv.root.file_id
 
496
        self._write_inventory(inv)
 
497
 
 
498
    def read_working_inventory(self):
 
499
        """Read the working inventory."""
 
500
        self.lock_read()
 
501
        try:
 
502
            # ElementTree does its own conversion from UTF-8, so open in
 
503
            # binary.
 
504
            f = self.controlfile('inventory', 'rb')
 
505
            return bzrlib.xml5.serializer_v5.read_inventory(f)
 
506
        finally:
 
507
            self.unlock()
 
508
            
 
509
 
 
510
    def _write_inventory(self, inv):
 
511
        """Update the working inventory.
 
512
 
 
513
        That is to say, the inventory describing changes underway, that
 
514
        will be committed to the next revision.
 
515
        """
 
516
        from cStringIO import StringIO
 
517
        self.lock_write()
 
518
        try:
 
519
            sio = StringIO()
 
520
            bzrlib.xml5.serializer_v5.write_inventory(inv, sio)
 
521
            sio.seek(0)
 
522
            # Transport handles atomicity
 
523
            self.put_controlfile('inventory', sio)
 
524
        finally:
 
525
            self.unlock()
 
526
        
 
527
        mutter('wrote working inventory')
 
528
            
 
529
    inventory = property(read_working_inventory, _write_inventory, None,
 
530
                         """Inventory for the working copy.""")
 
531
 
 
532
    def add(self, files, ids=None):
 
533
        """Make files versioned.
 
534
 
 
535
        Note that the command line normally calls smart_add instead,
 
536
        which can automatically recurse.
 
537
 
 
538
        This puts the files in the Added state, so that they will be
 
539
        recorded by the next commit.
 
540
 
 
541
        files
 
542
            List of paths to add, relative to the base of the tree.
 
543
 
 
544
        ids
 
545
            If set, use these instead of automatically generated ids.
 
546
            Must be the same length as the list of files, but may
 
547
            contain None for ids that are to be autogenerated.
 
548
 
 
549
        TODO: Perhaps have an option to add the ids even if the files do
 
550
              not (yet) exist.
 
551
 
 
552
        TODO: Perhaps yield the ids and paths as they're added.
 
553
        """
 
554
        # TODO: Re-adding a file that is removed in the working copy
 
555
        # should probably put it back with the previous ID.
 
556
        if isinstance(files, basestring):
 
557
            assert(ids is None or isinstance(ids, basestring))
 
558
            files = [files]
 
559
            if ids is not None:
 
560
                ids = [ids]
 
561
 
 
562
        if ids is None:
 
563
            ids = [None] * len(files)
 
564
        else:
 
565
            assert(len(ids) == len(files))
 
566
 
 
567
        self.lock_write()
 
568
        try:
 
569
            inv = self.read_working_inventory()
 
570
            for f,file_id in zip(files, ids):
 
571
                if is_control_file(f):
 
572
                    raise BzrError("cannot add control file %s" % quotefn(f))
 
573
 
 
574
                fp = splitpath(f)
 
575
 
 
576
                if len(fp) == 0:
 
577
                    raise BzrError("cannot add top-level %r" % f)
 
578
 
 
579
                fullpath = os.path.normpath(self.abspath(f))
 
580
 
 
581
                try:
 
582
                    kind = file_kind(fullpath)
 
583
                except OSError:
 
584
                    # maybe something better?
 
585
                    raise BzrError('cannot add: not a regular file, symlink or directory: %s' % quotefn(f))
 
586
 
 
587
                if not InventoryEntry.versionable_kind(kind):
 
588
                    raise BzrError('cannot add: not a versionable file ('
 
589
                                   'i.e. regular file, symlink or directory): %s' % quotefn(f))
 
590
 
 
591
                if file_id is None:
 
592
                    file_id = gen_file_id(f)
 
593
                inv.add_path(f, kind=kind, file_id=file_id)
 
594
 
 
595
                mutter("add file %s file_id:{%s} kind=%r" % (f, file_id, kind))
 
596
 
 
597
            self._write_inventory(inv)
 
598
        finally:
 
599
            self.unlock()
 
600
            
 
601
 
 
602
    def print_file(self, file, revno):
 
603
        """Print `file` to stdout."""
 
604
        self.lock_read()
 
605
        try:
 
606
            tree = self.revision_tree(self.get_rev_id(revno))
 
607
            # use inventory as it was in that revision
 
608
            file_id = tree.inventory.path2id(file)
 
609
            if not file_id:
 
610
                raise BzrError("%r is not present in revision %s" % (file, revno))
 
611
            tree.print_file(file_id)
 
612
        finally:
 
613
            self.unlock()
 
614
 
 
615
 
 
616
    def remove(self, files, verbose=False):
 
617
        """Mark nominated files for removal from the inventory.
 
618
 
 
619
        This does not remove their text.  This does not run on 
 
620
 
 
621
        TODO: Refuse to remove modified files unless --force is given?
 
622
 
 
623
        TODO: Do something useful with directories.
 
624
 
 
625
        TODO: Should this remove the text or not?  Tough call; not
 
626
        removing may be useful and the user can just use use rm, and
 
627
        is the opposite of add.  Removing it is consistent with most
 
628
        other tools.  Maybe an option.
 
629
        """
 
630
        ## TODO: Normalize names
 
631
        ## TODO: Remove nested loops; better scalability
 
632
        if isinstance(files, basestring):
 
633
            files = [files]
 
634
 
 
635
        self.lock_write()
 
636
 
 
637
        try:
 
638
            tree = self.working_tree()
 
639
            inv = tree.inventory
 
640
 
 
641
            # do this before any modifications
 
642
            for f in files:
 
643
                fid = inv.path2id(f)
 
644
                if not fid:
 
645
                    raise BzrError("cannot remove unversioned file %s" % quotefn(f))
 
646
                mutter("remove inventory entry %s {%s}" % (quotefn(f), fid))
 
647
                if verbose:
 
648
                    # having remove it, it must be either ignored or unknown
 
649
                    if tree.is_ignored(f):
 
650
                        new_status = 'I'
 
651
                    else:
 
652
                        new_status = '?'
 
653
                    show_status(new_status, inv[fid].kind, quotefn(f))
 
654
                del inv[fid]
 
655
 
 
656
            self._write_inventory(inv)
 
657
        finally:
 
658
            self.unlock()
 
659
 
 
660
    # FIXME: this doesn't need to be a branch method
 
661
    def set_inventory(self, new_inventory_list):
 
662
        from bzrlib.inventory import Inventory, InventoryEntry
 
663
        inv = Inventory(self.get_root_id())
 
664
        for path, file_id, parent, kind in new_inventory_list:
 
665
            name = os.path.basename(path)
 
666
            if name == "":
 
667
                continue
 
668
            # fixme, there should be a factory function inv,add_?? 
 
669
            if kind == 'directory':
 
670
                inv.add(inventory.InventoryDirectory(file_id, name, parent))
 
671
            elif kind == 'file':
 
672
                inv.add(inventory.InventoryFile(file_id, name, parent))
 
673
            elif kind == 'symlink':
 
674
                inv.add(inventory.InventoryLink(file_id, name, parent))
556
675
            else:
557
 
                left_parent = _mod_revision.NULL_REVISION
558
 
            # left_parent is the actual revision we want to stop logging at,
559
 
            # since we want to show the merged revisions after the stop_rev too
560
 
            reached_stop_revision_id = False
561
 
            revision_id_whitelist = []
562
 
            for node in rev_iter:
563
 
                rev_id = node.key
564
 
                if rev_id == left_parent:
565
 
                    # reached the left parent after the stop_revision
566
 
                    return
567
 
                if (not reached_stop_revision_id or
568
 
                        rev_id in revision_id_whitelist):
569
 
                    yield (rev_id, node.merge_depth, node.revno,
570
 
                       node.end_of_merge)
571
 
                    if reached_stop_revision_id or rev_id == stop_revision_id:
572
 
                        # only do the merged revs of rev_id from now on
573
 
                        rev = self.repository.get_revision(rev_id)
574
 
                        if rev.parent_ids:
575
 
                            reached_stop_revision_id = True
576
 
                            revision_id_whitelist.extend(rev.parent_ids)
577
 
        else:
578
 
            raise ValueError('invalid stop_rule %r' % stop_rule)
579
 
 
580
 
    def _filter_start_non_ancestors(self, rev_iter):
581
 
        # If we started from a dotted revno, we want to consider it as a tip
582
 
        # and don't want to yield revisions that are not part of its
583
 
        # ancestry. Given the order guaranteed by the merge sort, we will see
584
 
        # uninteresting descendants of the first parent of our tip before the
585
 
        # tip itself.
586
 
        first = rev_iter.next()
587
 
        (rev_id, merge_depth, revno, end_of_merge) = first
588
 
        yield first
589
 
        if not merge_depth:
590
 
            # We start at a mainline revision so by definition, all others
591
 
            # revisions in rev_iter are ancestors
592
 
            for node in rev_iter:
593
 
                yield node
594
 
 
595
 
        clean = False
596
 
        whitelist = set()
597
 
        pmap = self.repository.get_parent_map([rev_id])
598
 
        parents = pmap.get(rev_id, [])
599
 
        if parents:
600
 
            whitelist.update(parents)
601
 
        else:
602
 
            # If there is no parents, there is nothing of interest left
603
 
 
604
 
            # FIXME: It's hard to test this scenario here as this code is never
605
 
            # called in that case. -- vila 20100322
606
 
            return
607
 
 
608
 
        for (rev_id, merge_depth, revno, end_of_merge) in rev_iter:
609
 
            if not clean:
610
 
                if rev_id in whitelist:
611
 
                    pmap = self.repository.get_parent_map([rev_id])
612
 
                    parents = pmap.get(rev_id, [])
613
 
                    whitelist.remove(rev_id)
614
 
                    whitelist.update(parents)
615
 
                    if merge_depth == 0:
616
 
                        # We've reached the mainline, there is nothing left to
617
 
                        # filter
618
 
                        clean = True
619
 
                else:
620
 
                    # A revision that is not part of the ancestry of our
621
 
                    # starting revision.
622
 
                    continue
623
 
            yield (rev_id, merge_depth, revno, end_of_merge)
624
 
 
625
 
    def leave_lock_in_place(self):
626
 
        """Tell this branch object not to release the physical lock when this
627
 
        object is unlocked.
628
 
 
629
 
        If lock_write doesn't return a token, then this method is not supported.
630
 
        """
631
 
        self.control_files.leave_in_place()
632
 
 
633
 
    def dont_leave_lock_in_place(self):
634
 
        """Tell this branch object to release the physical lock when this
635
 
        object is unlocked, even if it didn't originally acquire it.
636
 
 
637
 
        If lock_write doesn't return a token, then this method is not supported.
638
 
        """
639
 
        self.control_files.dont_leave_in_place()
640
 
 
641
 
    def bind(self, other):
642
 
        """Bind the local branch the other branch.
643
 
 
644
 
        :param other: The branch to bind to
645
 
        :type other: Branch
646
 
        """
647
 
        raise errors.UpgradeRequired(self.user_url)
648
 
 
649
 
    def set_append_revisions_only(self, enabled):
650
 
        if not self._format.supports_set_append_revisions_only():
651
 
            raise errors.UpgradeRequired(self.user_url)
652
 
        if enabled:
653
 
            value = 'True'
654
 
        else:
655
 
            value = 'False'
656
 
        self.get_config().set_user_option('append_revisions_only', value,
657
 
            warn_masked=True)
658
 
 
659
 
    def set_reference_info(self, file_id, tree_path, branch_location):
660
 
        """Set the branch location to use for a tree reference."""
661
 
        raise errors.UnsupportedOperation(self.set_reference_info, self)
662
 
 
663
 
    def get_reference_info(self, file_id):
664
 
        """Get the tree_path and branch_location for a tree reference."""
665
 
        raise errors.UnsupportedOperation(self.get_reference_info, self)
666
 
 
667
 
    @needs_write_lock
668
 
    def fetch(self, from_branch, last_revision=None, limit=None):
669
 
        """Copy revisions from from_branch into this branch.
670
 
 
671
 
        :param from_branch: Where to copy from.
672
 
        :param last_revision: What revision to stop at (None for at the end
673
 
                              of the branch.
674
 
        :param limit: Optional rough limit of revisions to fetch
675
 
        :return: None
676
 
        """
677
 
        return InterBranch.get(from_branch, self).fetch(last_revision, limit=limit)
678
 
 
679
 
    def get_bound_location(self):
680
 
        """Return the URL of the branch we are bound to.
681
 
 
682
 
        Older format branches cannot bind, please be sure to use a metadir
683
 
        branch.
684
 
        """
685
 
        return None
686
 
 
687
 
    def get_old_bound_location(self):
688
 
        """Return the URL of the branch we used to be bound to
689
 
        """
690
 
        raise errors.UpgradeRequired(self.user_url)
691
 
 
692
 
    def get_commit_builder(self, parents, config=None, timestamp=None,
693
 
                           timezone=None, committer=None, revprops=None,
694
 
                           revision_id=None, lossy=False):
695
 
        """Obtain a CommitBuilder for this branch.
696
 
 
697
 
        :param parents: Revision ids of the parents of the new revision.
698
 
        :param config: Optional configuration to use.
699
 
        :param timestamp: Optional timestamp recorded for commit.
700
 
        :param timezone: Optional timezone for timestamp.
701
 
        :param committer: Optional committer to set for commit.
702
 
        :param revprops: Optional dictionary of revision properties.
703
 
        :param revision_id: Optional revision id.
704
 
        :param lossy: Whether to discard data that can not be natively
705
 
            represented, when pushing to a foreign VCS 
706
 
        """
707
 
 
708
 
        if config is None:
709
 
            config = self.get_config()
710
 
 
711
 
        return self.repository.get_commit_builder(self, parents, config,
712
 
            timestamp, timezone, committer, revprops, revision_id,
713
 
            lossy)
714
 
 
715
 
    def get_master_branch(self, possible_transports=None):
716
 
        """Return the branch we are bound to.
717
 
 
718
 
        :return: Either a Branch, or None
719
 
        """
720
 
        return None
 
676
                raise BzrError("unknown kind %r" % kind)
 
677
        self._write_inventory(inv)
 
678
 
 
679
    def unknowns(self):
 
680
        """Return all unknown files.
 
681
 
 
682
        These are files in the working directory that are not versioned or
 
683
        control files or ignored.
 
684
        
 
685
        >>> b = ScratchBranch(files=['foo', 'foo~'])
 
686
        >>> list(b.unknowns())
 
687
        ['foo']
 
688
        >>> b.add('foo')
 
689
        >>> list(b.unknowns())
 
690
        []
 
691
        >>> b.remove('foo')
 
692
        >>> list(b.unknowns())
 
693
        ['foo']
 
694
        """
 
695
        return self.working_tree().unknowns()
 
696
 
 
697
 
 
698
    def append_revision(self, *revision_ids):
 
699
        for revision_id in revision_ids:
 
700
            mutter("add {%s} to revision-history" % revision_id)
 
701
        self.lock_write()
 
702
        try:
 
703
            rev_history = self.revision_history()
 
704
            rev_history.extend(revision_ids)
 
705
            self.put_controlfile('revision-history', '\n'.join(rev_history))
 
706
        finally:
 
707
            self.unlock()
 
708
 
 
709
    def has_revision(self, revision_id):
 
710
        """True if this branch has a copy of the revision.
 
711
 
 
712
        This does not necessarily imply the revision is merge
 
713
        or on the mainline."""
 
714
        return (revision_id is None
 
715
                or revision_id in self.revision_store)
 
716
 
 
717
    def get_revision_xml_file(self, revision_id):
 
718
        """Return XML file object for revision object."""
 
719
        if not revision_id or not isinstance(revision_id, basestring):
 
720
            raise InvalidRevisionId(revision_id)
 
721
 
 
722
        self.lock_read()
 
723
        try:
 
724
            try:
 
725
                return self.revision_store[revision_id]
 
726
            except (IndexError, KeyError):
 
727
                raise bzrlib.errors.NoSuchRevision(self, revision_id)
 
728
        finally:
 
729
            self.unlock()
 
730
 
 
731
    #deprecated
 
732
    get_revision_xml = get_revision_xml_file
 
733
 
 
734
    def get_revision_xml(self, revision_id):
 
735
        return self.get_revision_xml_file(revision_id).read()
 
736
 
 
737
 
 
738
    def get_revision(self, revision_id):
 
739
        """Return the Revision object for a named revision"""
 
740
        xml_file = self.get_revision_xml_file(revision_id)
 
741
 
 
742
        try:
 
743
            r = bzrlib.xml5.serializer_v5.read_revision(xml_file)
 
744
        except SyntaxError, e:
 
745
            raise bzrlib.errors.BzrError('failed to unpack revision_xml',
 
746
                                         [revision_id,
 
747
                                          str(e)])
 
748
            
 
749
        assert r.revision_id == revision_id
 
750
        return r
721
751
 
722
752
    def get_revision_delta(self, revno):
723
753
        """Return the delta for one revision.
725
755
        The delta is relative to its mainline predecessor, or the
726
756
        empty tree for revision 1.
727
757
        """
 
758
        assert isinstance(revno, int)
728
759
        rh = self.revision_history()
729
760
        if not (1 <= revno <= len(rh)):
730
 
            raise errors.InvalidRevisionNumber(revno)
731
 
        return self.repository.get_revision_delta(rh[revno-1])
732
 
 
733
 
    def get_stacked_on_url(self):
734
 
        """Get the URL this branch is stacked against.
735
 
 
736
 
        :raises NotStacked: If the branch is not stacked.
737
 
        :raises UnstackableBranchFormat: If the branch does not support
738
 
            stacking.
739
 
        """
740
 
        raise NotImplementedError(self.get_stacked_on_url)
741
 
 
742
 
    def print_file(self, file, revision_id):
743
 
        """Print `file` to stdout."""
744
 
        raise NotImplementedError(self.print_file)
745
 
 
746
 
    @deprecated_method(deprecated_in((2, 4, 0)))
747
 
    def set_revision_history(self, rev_history):
748
 
        """See Branch.set_revision_history."""
749
 
        self._set_revision_history(rev_history)
750
 
 
751
 
    @needs_write_lock
752
 
    def _set_revision_history(self, rev_history):
753
 
        if len(rev_history) == 0:
754
 
            revid = _mod_revision.NULL_REVISION
755
 
        else:
756
 
            revid = rev_history[-1]
757
 
        if rev_history != self._lefthand_history(revid):
758
 
            raise errors.NotLefthandHistory(rev_history)
759
 
        self.set_last_revision_info(len(rev_history), revid)
760
 
        self._cache_revision_history(rev_history)
761
 
        for hook in Branch.hooks['set_rh']:
762
 
            hook(self, rev_history)
763
 
 
764
 
    @needs_write_lock
765
 
    def set_last_revision_info(self, revno, revision_id):
766
 
        """Set the last revision of this branch.
767
 
 
768
 
        The caller is responsible for checking that the revno is correct
769
 
        for this revision id.
770
 
 
771
 
        It may be possible to set the branch last revision to an id not
772
 
        present in the repository.  However, branches can also be
773
 
        configured to check constraints on history, in which case this may not
774
 
        be permitted.
775
 
        """
776
 
        raise NotImplementedError(self.set_last_revision_info)
777
 
 
778
 
    @needs_write_lock
779
 
    def generate_revision_history(self, revision_id, last_rev=None,
780
 
                                  other_branch=None):
781
 
        """See Branch.generate_revision_history"""
782
 
        graph = self.repository.get_graph()
783
 
        (last_revno, last_revid) = self.last_revision_info()
784
 
        known_revision_ids = [
785
 
            (last_revid, last_revno),
786
 
            (_mod_revision.NULL_REVISION, 0),
787
 
            ]
788
 
        if last_rev is not None:
789
 
            if not graph.is_ancestor(last_rev, revision_id):
790
 
                # our previous tip is not merged into stop_revision
791
 
                raise errors.DivergedBranches(self, other_branch)
792
 
        revno = graph.find_distance_to_null(revision_id, known_revision_ids)
793
 
        self.set_last_revision_info(revno, revision_id)
794
 
 
795
 
    @needs_write_lock
796
 
    def set_parent(self, url):
797
 
        """See Branch.set_parent."""
798
 
        # TODO: Maybe delete old location files?
799
 
        # URLs should never be unicode, even on the local fs,
800
 
        # FIXUP this and get_parent in a future branch format bump:
801
 
        # read and rewrite the file. RBC 20060125
802
 
        if url is not None:
803
 
            if isinstance(url, unicode):
804
 
                try:
805
 
                    url = url.encode('ascii')
806
 
                except UnicodeEncodeError:
807
 
                    raise errors.InvalidURL(url,
808
 
                        "Urls must be 7-bit ascii, "
809
 
                        "use bzrlib.urlutils.escape")
810
 
            url = urlutils.relative_url(self.base, url)
811
 
        self._set_parent_location(url)
812
 
 
813
 
    @needs_write_lock
814
 
    def set_stacked_on_url(self, url):
815
 
        """Set the URL this branch is stacked against.
816
 
 
817
 
        :raises UnstackableBranchFormat: If the branch does not support
818
 
            stacking.
819
 
        :raises UnstackableRepositoryFormat: If the repository does not support
820
 
            stacking.
821
 
        """
822
 
        if not self._format.supports_stacking():
823
 
            raise errors.UnstackableBranchFormat(self._format, self.user_url)
824
 
        # XXX: Changing from one fallback repository to another does not check
825
 
        # that all the data you need is present in the new fallback.
826
 
        # Possibly it should.
827
 
        self._check_stackable_repo()
828
 
        if not url:
829
 
            try:
830
 
                old_url = self.get_stacked_on_url()
831
 
            except (errors.NotStacked, errors.UnstackableBranchFormat,
832
 
                errors.UnstackableRepositoryFormat):
833
 
                return
834
 
            self._unstack()
835
 
        else:
836
 
            self._activate_fallback_location(url)
837
 
        # write this out after the repository is stacked to avoid setting a
838
 
        # stacked config that doesn't work.
839
 
        self._set_config_location('stacked_on_location', url)
840
 
 
841
 
    def _unstack(self):
842
 
        """Change a branch to be unstacked, copying data as needed.
843
 
 
844
 
        Don't call this directly, use set_stacked_on_url(None).
845
 
        """
846
 
        pb = ui.ui_factory.nested_progress_bar()
 
761
            raise InvalidRevisionNumber(revno)
 
762
 
 
763
        # revno is 1-based; list is 0-based
 
764
 
 
765
        new_tree = self.revision_tree(rh[revno-1])
 
766
        if revno == 1:
 
767
            old_tree = EmptyTree()
 
768
        else:
 
769
            old_tree = self.revision_tree(rh[revno-2])
 
770
 
 
771
        return compare_trees(old_tree, new_tree)
 
772
 
 
773
    def get_revision_sha1(self, revision_id):
 
774
        """Hash the stored value of a revision, and return it."""
 
775
        # In the future, revision entries will be signed. At that
 
776
        # point, it is probably best *not* to include the signature
 
777
        # in the revision hash. Because that lets you re-sign
 
778
        # the revision, (add signatures/remove signatures) and still
 
779
        # have all hash pointers stay consistent.
 
780
        # But for now, just hash the contents.
 
781
        return bzrlib.osutils.sha_file(self.get_revision_xml_file(revision_id))
 
782
 
 
783
    def _get_ancestry_weave(self):
 
784
        return self.control_weaves.get_weave('ancestry')
 
785
 
 
786
    def get_ancestry(self, revision_id):
 
787
        """Return a list of revision-ids integrated by a revision."""
 
788
        if revision_id is None:
 
789
            return [None]
 
790
        w = self._get_ancestry_weave()
 
791
        return [None] + [l[:-1] for l in w.get_iter(w.lookup(revision_id))]
 
792
 
 
793
    def get_inventory_weave(self):
 
794
        return self.control_weaves.get_weave('inventory')
 
795
 
 
796
    def get_inventory(self, revision_id):
 
797
        """Get Inventory object by hash."""
 
798
        xml = self.get_inventory_xml(revision_id)
 
799
        return bzrlib.xml5.serializer_v5.read_inventory_from_string(xml)
 
800
 
 
801
    def get_inventory_xml(self, revision_id):
 
802
        """Get inventory XML as a file object."""
847
803
        try:
848
 
            pb.update("Unstacking")
849
 
            # The basic approach here is to fetch the tip of the branch,
850
 
            # including all available ghosts, from the existing stacked
851
 
            # repository into a new repository object without the fallbacks. 
852
 
            #
853
 
            # XXX: See <https://launchpad.net/bugs/397286> - this may not be
854
 
            # correct for CHKMap repostiories
855
 
            old_repository = self.repository
856
 
            if len(old_repository._fallback_repositories) != 1:
857
 
                raise AssertionError("can't cope with fallback repositories "
858
 
                    "of %r (fallbacks: %r)" % (old_repository,
859
 
                        old_repository._fallback_repositories))
860
 
            # Open the new repository object.
861
 
            # Repositories don't offer an interface to remove fallback
862
 
            # repositories today; take the conceptually simpler option and just
863
 
            # reopen it.  We reopen it starting from the URL so that we
864
 
            # get a separate connection for RemoteRepositories and can
865
 
            # stream from one of them to the other.  This does mean doing
866
 
            # separate SSH connection setup, but unstacking is not a
867
 
            # common operation so it's tolerable.
868
 
            new_bzrdir = bzrdir.BzrDir.open(self.bzrdir.root_transport.base)
869
 
            new_repository = new_bzrdir.find_repository()
870
 
            if new_repository._fallback_repositories:
871
 
                raise AssertionError("didn't expect %r to have "
872
 
                    "fallback_repositories"
873
 
                    % (self.repository,))
874
 
            # Replace self.repository with the new repository.
875
 
            # Do our best to transfer the lock state (i.e. lock-tokens and
876
 
            # lock count) of self.repository to the new repository.
877
 
            lock_token = old_repository.lock_write().repository_token
878
 
            self.repository = new_repository
879
 
            if isinstance(self, remote.RemoteBranch):
880
 
                # Remote branches can have a second reference to the old
881
 
                # repository that need to be replaced.
882
 
                if self._real_branch is not None:
883
 
                    self._real_branch.repository = new_repository
884
 
            self.repository.lock_write(token=lock_token)
885
 
            if lock_token is not None:
886
 
                old_repository.leave_lock_in_place()
887
 
            old_repository.unlock()
888
 
            if lock_token is not None:
889
 
                # XXX: self.repository.leave_lock_in_place() before this
890
 
                # function will not be preserved.  Fortunately that doesn't
891
 
                # affect the current default format (2a), and would be a
892
 
                # corner-case anyway.
893
 
                #  - Andrew Bennetts, 2010/06/30
894
 
                self.repository.dont_leave_lock_in_place()
895
 
            old_lock_count = 0
896
 
            while True:
897
 
                try:
898
 
                    old_repository.unlock()
899
 
                except errors.LockNotHeld:
900
 
                    break
901
 
                old_lock_count += 1
902
 
            if old_lock_count == 0:
903
 
                raise AssertionError(
904
 
                    'old_repository should have been locked at least once.')
905
 
            for i in range(old_lock_count-1):
906
 
                self.repository.lock_write()
907
 
            # Fetch from the old repository into the new.
908
 
            old_repository.lock_read()
909
 
            try:
910
 
                # XXX: If you unstack a branch while it has a working tree
911
 
                # with a pending merge, the pending-merged revisions will no
912
 
                # longer be present.  You can (probably) revert and remerge.
913
 
                try:
914
 
                    tags_to_fetch = set(self.tags.get_reverse_tag_dict())
915
 
                except errors.TagsNotSupported:
916
 
                    tags_to_fetch = set()
917
 
                fetch_spec = _mod_graph.NotInOtherForRevs(self.repository,
918
 
                    old_repository, required_ids=[self.last_revision()],
919
 
                    if_present_ids=tags_to_fetch, find_ghosts=True).execute()
920
 
                self.repository.fetch(old_repository, fetch_spec=fetch_spec)
921
 
            finally:
922
 
                old_repository.unlock()
923
 
        finally:
924
 
            pb.finished()
925
 
 
926
 
    def _set_tags_bytes(self, bytes):
927
 
        """Mirror method for _get_tags_bytes.
928
 
 
929
 
        :seealso: Branch._get_tags_bytes.
930
 
        """
931
 
        op = cleanup.OperationWithCleanups(self._set_tags_bytes_locked)
932
 
        op.add_cleanup(self.lock_write().unlock)
933
 
        return op.run_simple(bytes)
934
 
 
935
 
    def _set_tags_bytes_locked(self, bytes):
936
 
        self._tags_bytes = bytes
937
 
        return self._transport.put_bytes('tags', bytes)
938
 
 
939
 
    def _cache_revision_history(self, rev_history):
940
 
        """Set the cached revision history to rev_history.
941
 
 
942
 
        The revision_history method will use this cache to avoid regenerating
943
 
        the revision history.
944
 
 
945
 
        This API is semi-public; it only for use by subclasses, all other code
946
 
        should consider it to be private.
947
 
        """
948
 
        self._revision_history_cache = rev_history
949
 
 
950
 
    def _cache_revision_id_to_revno(self, revision_id_to_revno):
951
 
        """Set the cached revision_id => revno map to revision_id_to_revno.
952
 
 
953
 
        This API is semi-public; it only for use by subclasses, all other code
954
 
        should consider it to be private.
955
 
        """
956
 
        self._revision_id_to_revno_cache = revision_id_to_revno
957
 
 
958
 
    def _clear_cached_state(self):
959
 
        """Clear any cached data on this branch, e.g. cached revision history.
960
 
 
961
 
        This means the next call to revision_history will need to call
962
 
        _gen_revision_history.
963
 
 
964
 
        This API is semi-public; it only for use by subclasses, all other code
965
 
        should consider it to be private.
966
 
        """
967
 
        self._revision_history_cache = None
968
 
        self._revision_id_to_revno_cache = None
969
 
        self._last_revision_info_cache = None
970
 
        self._master_branch_cache = None
971
 
        self._merge_sorted_revisions_cache = None
972
 
        self._partial_revision_history_cache = []
973
 
        self._partial_revision_id_to_revno_cache = {}
974
 
        self._tags_bytes = None
975
 
 
976
 
    def _gen_revision_history(self):
977
 
        """Return sequence of revision hashes on to this branch.
978
 
 
979
 
        Unlike revision_history, this method always regenerates or rereads the
980
 
        revision history, i.e. it does not cache the result, so repeated calls
981
 
        may be expensive.
982
 
 
983
 
        Concrete subclasses should override this instead of revision_history so
984
 
        that subclasses do not need to deal with caching logic.
985
 
 
986
 
        This API is semi-public; it only for use by subclasses, all other code
987
 
        should consider it to be private.
988
 
        """
989
 
        raise NotImplementedError(self._gen_revision_history)
990
 
 
991
 
    @needs_read_lock
 
804
            assert isinstance(revision_id, basestring), type(revision_id)
 
805
            iw = self.get_inventory_weave()
 
806
            return iw.get_text(iw.lookup(revision_id))
 
807
        except IndexError:
 
808
            raise bzrlib.errors.HistoryMissing(self, 'inventory', revision_id)
 
809
 
 
810
    def get_inventory_sha1(self, revision_id):
 
811
        """Return the sha1 hash of the inventory entry
 
812
        """
 
813
        return self.get_revision(revision_id).inventory_sha1
 
814
 
 
815
    def get_revision_inventory(self, revision_id):
 
816
        """Return inventory of a past revision."""
 
817
        # TODO: Unify this with get_inventory()
 
818
        # bzr 0.0.6 and later imposes the constraint that the inventory_id
 
819
        # must be the same as its revision, so this is trivial.
 
820
        if revision_id == None:
 
821
            return Inventory(self.get_root_id())
 
822
        else:
 
823
            return self.get_inventory(revision_id)
 
824
 
992
825
    def revision_history(self):
993
 
        """Return sequence of revision ids on this branch.
994
 
 
995
 
        This method will cache the revision history for as long as it is safe to
996
 
        do so.
997
 
        """
998
 
        if 'evil' in debug.debug_flags:
999
 
            mutter_callsite(3, "revision_history scales with history.")
1000
 
        if self._revision_history_cache is not None:
1001
 
            history = self._revision_history_cache
1002
 
        else:
1003
 
            history = self._gen_revision_history()
1004
 
            self._cache_revision_history(history)
1005
 
        return list(history)
 
826
        """Return sequence of revision hashes on to this branch."""
 
827
        self.lock_read()
 
828
        try:
 
829
            return [l.rstrip('\r\n') for l in
 
830
                    self.controlfile('revision-history', 'r').readlines()]
 
831
        finally:
 
832
            self.unlock()
 
833
 
 
834
    def common_ancestor(self, other, self_revno=None, other_revno=None):
 
835
        """
 
836
        >>> from bzrlib.commit import commit
 
837
        >>> sb = ScratchBranch(files=['foo', 'foo~'])
 
838
        >>> sb.common_ancestor(sb) == (None, None)
 
839
        True
 
840
        >>> commit(sb, "Committing first revision", verbose=False)
 
841
        >>> sb.common_ancestor(sb)[0]
 
842
        1
 
843
        >>> clone = sb.clone()
 
844
        >>> commit(sb, "Committing second revision", verbose=False)
 
845
        >>> sb.common_ancestor(sb)[0]
 
846
        2
 
847
        >>> sb.common_ancestor(clone)[0]
 
848
        1
 
849
        >>> commit(clone, "Committing divergent second revision", 
 
850
        ...               verbose=False)
 
851
        >>> sb.common_ancestor(clone)[0]
 
852
        1
 
853
        >>> sb.common_ancestor(clone) == clone.common_ancestor(sb)
 
854
        True
 
855
        >>> sb.common_ancestor(sb) != clone.common_ancestor(clone)
 
856
        True
 
857
        >>> clone2 = sb.clone()
 
858
        >>> sb.common_ancestor(clone2)[0]
 
859
        2
 
860
        >>> sb.common_ancestor(clone2, self_revno=1)[0]
 
861
        1
 
862
        >>> sb.common_ancestor(clone2, other_revno=1)[0]
 
863
        1
 
864
        """
 
865
        my_history = self.revision_history()
 
866
        other_history = other.revision_history()
 
867
        if self_revno is None:
 
868
            self_revno = len(my_history)
 
869
        if other_revno is None:
 
870
            other_revno = len(other_history)
 
871
        indices = range(min((self_revno, other_revno)))
 
872
        indices.reverse()
 
873
        for r in indices:
 
874
            if my_history[r] == other_history[r]:
 
875
                return r+1, my_history[r]
 
876
        return None, None
 
877
 
1006
878
 
1007
879
    def revno(self):
1008
880
        """Return current revision number for this branch.
1010
882
        That is equivalent to the number of revisions committed to
1011
883
        this branch.
1012
884
        """
1013
 
        return self.last_revision_info()[0]
 
885
        return len(self.revision_history())
1014
886
 
1015
 
    def unbind(self):
1016
 
        """Older format branches cannot bind or unbind."""
1017
 
        raise errors.UpgradeRequired(self.user_url)
1018
887
 
1019
888
    def last_revision(self):
1020
 
        """Return last revision id, or NULL_REVISION."""
1021
 
        return self.last_revision_info()[1]
1022
 
 
1023
 
    @needs_read_lock
1024
 
    def last_revision_info(self):
1025
 
        """Return information about the last revision.
1026
 
 
1027
 
        :return: A tuple (revno, revision_id).
1028
 
        """
1029
 
        if self._last_revision_info_cache is None:
1030
 
            self._last_revision_info_cache = self._read_last_revision_info()
1031
 
        return self._last_revision_info_cache
1032
 
 
1033
 
    def _read_last_revision_info(self):
1034
 
        raise NotImplementedError(self._read_last_revision_info)
1035
 
 
1036
 
    @deprecated_method(deprecated_in((2, 4, 0)))
1037
 
    def import_last_revision_info(self, source_repo, revno, revid):
1038
 
        """Set the last revision info, importing from another repo if necessary.
1039
 
 
1040
 
        :param source_repo: Source repository to optionally fetch from
1041
 
        :param revno: Revision number of the new tip
1042
 
        :param revid: Revision id of the new tip
1043
 
        """
1044
 
        if not self.repository.has_same_location(source_repo):
1045
 
            self.repository.fetch(source_repo, revision_id=revid)
1046
 
        self.set_last_revision_info(revno, revid)
1047
 
 
1048
 
    def import_last_revision_info_and_tags(self, source, revno, revid,
1049
 
                                           lossy=False):
1050
 
        """Set the last revision info, importing from another repo if necessary.
1051
 
 
1052
 
        This is used by the bound branch code to upload a revision to
1053
 
        the master branch first before updating the tip of the local branch.
1054
 
        Revisions referenced by source's tags are also transferred.
1055
 
 
1056
 
        :param source: Source branch to optionally fetch from
1057
 
        :param revno: Revision number of the new tip
1058
 
        :param revid: Revision id of the new tip
1059
 
        :param lossy: Whether to discard metadata that can not be
1060
 
            natively represented
1061
 
        :return: Tuple with the new revision number and revision id
1062
 
            (should only be different from the arguments when lossy=True)
1063
 
        """
1064
 
        if not self.repository.has_same_location(source.repository):
1065
 
            self.fetch(source, revid)
1066
 
        self.set_last_revision_info(revno, revid)
1067
 
        return (revno, revid)
1068
 
 
 
889
        """Return last patch hash, or None if no history.
 
890
        """
 
891
        ph = self.revision_history()
 
892
        if ph:
 
893
            return ph[-1]
 
894
        else:
 
895
            return None
 
896
 
 
897
 
 
898
    def missing_revisions(self, other, stop_revision=None, diverged_ok=False):
 
899
        """Return a list of new revisions that would perfectly fit.
 
900
        
 
901
        If self and other have not diverged, return a list of the revisions
 
902
        present in other, but missing from self.
 
903
 
 
904
        >>> from bzrlib.commit import commit
 
905
        >>> bzrlib.trace.silent = True
 
906
        >>> br1 = ScratchBranch()
 
907
        >>> br2 = ScratchBranch()
 
908
        >>> br1.missing_revisions(br2)
 
909
        []
 
910
        >>> commit(br2, "lala!", rev_id="REVISION-ID-1")
 
911
        >>> br1.missing_revisions(br2)
 
912
        [u'REVISION-ID-1']
 
913
        >>> br2.missing_revisions(br1)
 
914
        []
 
915
        >>> commit(br1, "lala!", rev_id="REVISION-ID-1")
 
916
        >>> br1.missing_revisions(br2)
 
917
        []
 
918
        >>> commit(br2, "lala!", rev_id="REVISION-ID-2A")
 
919
        >>> br1.missing_revisions(br2)
 
920
        [u'REVISION-ID-2A']
 
921
        >>> commit(br1, "lala!", rev_id="REVISION-ID-2B")
 
922
        >>> br1.missing_revisions(br2)
 
923
        Traceback (most recent call last):
 
924
        DivergedBranches: These branches have diverged.
 
925
        """
 
926
        # FIXME: If the branches have diverged, but the latest
 
927
        # revision in this branch is completely merged into the other,
 
928
        # then we should still be able to pull.
 
929
        self_history = self.revision_history()
 
930
        self_len = len(self_history)
 
931
        other_history = other.revision_history()
 
932
        other_len = len(other_history)
 
933
        common_index = min(self_len, other_len) -1
 
934
        if common_index >= 0 and \
 
935
            self_history[common_index] != other_history[common_index]:
 
936
            raise DivergedBranches(self, other)
 
937
 
 
938
        if stop_revision is None:
 
939
            stop_revision = other_len
 
940
        else:
 
941
            assert isinstance(stop_revision, int)
 
942
            if stop_revision > other_len:
 
943
                raise bzrlib.errors.NoSuchRevision(self, stop_revision)
 
944
        return other_history[self_len:stop_revision]
 
945
 
 
946
    def update_revisions(self, other, stop_revision=None):
 
947
        """Pull in new perfect-fit revisions."""
 
948
        from bzrlib.fetch import greedy_fetch
 
949
        from bzrlib.revision import get_intervening_revisions
 
950
        if stop_revision is None:
 
951
            stop_revision = other.last_revision()
 
952
        greedy_fetch(to_branch=self, from_branch=other,
 
953
                     revision=stop_revision)
 
954
        pullable_revs = self.missing_revisions(
 
955
            other, other.revision_id_to_revno(stop_revision))
 
956
        if pullable_revs:
 
957
            greedy_fetch(to_branch=self,
 
958
                         from_branch=other,
 
959
                         revision=pullable_revs[-1])
 
960
            self.append_revision(*pullable_revs)
 
961
    
 
962
 
 
963
    def commit(self, *args, **kw):
 
964
        from bzrlib.commit import Commit
 
965
        Commit().commit(self, *args, **kw)
 
966
    
1069
967
    def revision_id_to_revno(self, revision_id):
1070
968
        """Given a revision id, return its revno"""
1071
 
        if _mod_revision.is_null(revision_id):
 
969
        if revision_id is None:
1072
970
            return 0
1073
971
        history = self.revision_history()
1074
972
        try:
1075
973
            return history.index(revision_id) + 1
1076
974
        except ValueError:
1077
 
            raise errors.NoSuchRevision(self, revision_id)
 
975
            raise bzrlib.errors.NoSuchRevision(self, revision_id)
1078
976
 
1079
 
    @needs_read_lock
1080
977
    def get_rev_id(self, revno, history=None):
1081
978
        """Find the revision id of the specified revno."""
1082
979
        if revno == 0:
1083
 
            return _mod_revision.NULL_REVISION
1084
 
        last_revno, last_revid = self.last_revision_info()
1085
 
        if revno == last_revno:
1086
 
            return last_revid
1087
 
        if revno <= 0 or revno > last_revno:
1088
 
            raise errors.NoSuchRevision(self, revno)
1089
 
        distance_from_last = last_revno - revno
1090
 
        if len(self._partial_revision_history_cache) <= distance_from_last:
1091
 
            self._extend_partial_history(distance_from_last)
1092
 
        return self._partial_revision_history_cache[distance_from_last]
1093
 
 
1094
 
    def pull(self, source, overwrite=False, stop_revision=None,
1095
 
             possible_transports=None, *args, **kwargs):
1096
 
        """Mirror source into this branch.
1097
 
 
1098
 
        This branch is considered to be 'local', having low latency.
1099
 
 
1100
 
        :returns: PullResult instance
1101
 
        """
1102
 
        return InterBranch.get(source, self).pull(overwrite=overwrite,
1103
 
            stop_revision=stop_revision,
1104
 
            possible_transports=possible_transports, *args, **kwargs)
1105
 
 
1106
 
    def push(self, target, overwrite=False, stop_revision=None, lossy=False,
1107
 
            *args, **kwargs):
1108
 
        """Mirror this branch into target.
1109
 
 
1110
 
        This branch is considered to be 'local', having low latency.
1111
 
        """
1112
 
        return InterBranch.get(self, target).push(overwrite, stop_revision,
1113
 
            lossy, *args, **kwargs)
 
980
            return None
 
981
        if history is None:
 
982
            history = self.revision_history()
 
983
        elif revno <= 0 or revno > len(history):
 
984
            raise bzrlib.errors.NoSuchRevision(self, revno)
 
985
        return history[revno - 1]
 
986
 
 
987
    def revision_tree(self, revision_id):
 
988
        """Return Tree for a revision on this branch.
 
989
 
 
990
        `revision_id` may be None for the null revision, in which case
 
991
        an `EmptyTree` is returned."""
 
992
        # TODO: refactor this to use an existing revision object
 
993
        # so we don't need to read it in twice.
 
994
        if revision_id == None:
 
995
            return EmptyTree()
 
996
        else:
 
997
            inv = self.get_revision_inventory(revision_id)
 
998
            return RevisionTree(self.weave_store, inv, revision_id)
 
999
 
 
1000
 
 
1001
    def working_tree(self):
 
1002
        """Return a `Tree` for the working copy."""
 
1003
        from bzrlib.workingtree import WorkingTree
 
1004
        # TODO: In the future, WorkingTree should utilize Transport
 
1005
        # RobertCollins 20051003 - I don't think it should - working trees are
 
1006
        # much more complex to keep consistent than our careful .bzr subset.
 
1007
        # instead, we should say that working trees are local only, and optimise
 
1008
        # for that.
 
1009
        return WorkingTree(self._transport.base, self.read_working_inventory())
 
1010
 
1114
1011
 
1115
1012
    def basis_tree(self):
1116
 
        """Return `Tree` object for last revision."""
1117
 
        return self.repository.revision_tree(self.last_revision())
 
1013
        """Return `Tree` object for last revision.
 
1014
 
 
1015
        If there are no revisions yet, return an `EmptyTree`.
 
1016
        """
 
1017
        return self.revision_tree(self.last_revision())
 
1018
 
 
1019
 
 
1020
    def rename_one(self, from_rel, to_rel):
 
1021
        """Rename one file.
 
1022
 
 
1023
        This can change the directory or the filename or both.
 
1024
        """
 
1025
        self.lock_write()
 
1026
        try:
 
1027
            tree = self.working_tree()
 
1028
            inv = tree.inventory
 
1029
            if not tree.has_filename(from_rel):
 
1030
                raise BzrError("can't rename: old working file %r does not exist" % from_rel)
 
1031
            if tree.has_filename(to_rel):
 
1032
                raise BzrError("can't rename: new working file %r already exists" % to_rel)
 
1033
 
 
1034
            file_id = inv.path2id(from_rel)
 
1035
            if file_id == None:
 
1036
                raise BzrError("can't rename: old name %r is not versioned" % from_rel)
 
1037
 
 
1038
            if inv.path2id(to_rel):
 
1039
                raise BzrError("can't rename: new name %r is already versioned" % to_rel)
 
1040
 
 
1041
            to_dir, to_tail = os.path.split(to_rel)
 
1042
            to_dir_id = inv.path2id(to_dir)
 
1043
            if to_dir_id == None and to_dir != '':
 
1044
                raise BzrError("can't determine destination directory id for %r" % to_dir)
 
1045
 
 
1046
            mutter("rename_one:")
 
1047
            mutter("  file_id    {%s}" % file_id)
 
1048
            mutter("  from_rel   %r" % from_rel)
 
1049
            mutter("  to_rel     %r" % to_rel)
 
1050
            mutter("  to_dir     %r" % to_dir)
 
1051
            mutter("  to_dir_id  {%s}" % to_dir_id)
 
1052
 
 
1053
            inv.rename(file_id, to_dir_id, to_tail)
 
1054
 
 
1055
            from_abs = self.abspath(from_rel)
 
1056
            to_abs = self.abspath(to_rel)
 
1057
            try:
 
1058
                rename(from_abs, to_abs)
 
1059
            except OSError, e:
 
1060
                raise BzrError("failed to rename %r to %r: %s"
 
1061
                        % (from_abs, to_abs, e[1]),
 
1062
                        ["rename rolled back"])
 
1063
 
 
1064
            self._write_inventory(inv)
 
1065
        finally:
 
1066
            self.unlock()
 
1067
 
 
1068
 
 
1069
    def move(self, from_paths, to_name):
 
1070
        """Rename files.
 
1071
 
 
1072
        to_name must exist as a versioned directory.
 
1073
 
 
1074
        If to_name exists and is a directory, the files are moved into
 
1075
        it, keeping their old names.  If it is a directory, 
 
1076
 
 
1077
        Note that to_name is only the last component of the new name;
 
1078
        this doesn't change the directory.
 
1079
 
 
1080
        This returns a list of (from_path, to_path) pairs for each
 
1081
        entry that is moved.
 
1082
        """
 
1083
        result = []
 
1084
        self.lock_write()
 
1085
        try:
 
1086
            ## TODO: Option to move IDs only
 
1087
            assert not isinstance(from_paths, basestring)
 
1088
            tree = self.working_tree()
 
1089
            inv = tree.inventory
 
1090
            to_abs = self.abspath(to_name)
 
1091
            if not isdir(to_abs):
 
1092
                raise BzrError("destination %r is not a directory" % to_abs)
 
1093
            if not tree.has_filename(to_name):
 
1094
                raise BzrError("destination %r not in working directory" % to_abs)
 
1095
            to_dir_id = inv.path2id(to_name)
 
1096
            if to_dir_id == None and to_name != '':
 
1097
                raise BzrError("destination %r is not a versioned directory" % to_name)
 
1098
            to_dir_ie = inv[to_dir_id]
 
1099
            if to_dir_ie.kind not in ('directory', 'root_directory'):
 
1100
                raise BzrError("destination %r is not a directory" % to_abs)
 
1101
 
 
1102
            to_idpath = inv.get_idpath(to_dir_id)
 
1103
 
 
1104
            for f in from_paths:
 
1105
                if not tree.has_filename(f):
 
1106
                    raise BzrError("%r does not exist in working tree" % f)
 
1107
                f_id = inv.path2id(f)
 
1108
                if f_id == None:
 
1109
                    raise BzrError("%r is not versioned" % f)
 
1110
                name_tail = splitpath(f)[-1]
 
1111
                dest_path = appendpath(to_name, name_tail)
 
1112
                if tree.has_filename(dest_path):
 
1113
                    raise BzrError("destination %r already exists" % dest_path)
 
1114
                if f_id in to_idpath:
 
1115
                    raise BzrError("can't move %r to a subdirectory of itself" % f)
 
1116
 
 
1117
            # OK, so there's a race here, it's possible that someone will
 
1118
            # create a file in this interval and then the rename might be
 
1119
            # left half-done.  But we should have caught most problems.
 
1120
 
 
1121
            for f in from_paths:
 
1122
                name_tail = splitpath(f)[-1]
 
1123
                dest_path = appendpath(to_name, name_tail)
 
1124
                result.append((f, dest_path))
 
1125
                inv.rename(inv.path2id(f), to_dir_id, name_tail)
 
1126
                try:
 
1127
                    rename(self.abspath(f), self.abspath(dest_path))
 
1128
                except OSError, e:
 
1129
                    raise BzrError("failed to rename %r to %r: %s" % (f, dest_path, e[1]),
 
1130
                            ["rename rolled back"])
 
1131
 
 
1132
            self._write_inventory(inv)
 
1133
        finally:
 
1134
            self.unlock()
 
1135
 
 
1136
        return result
 
1137
 
 
1138
 
 
1139
    def revert(self, filenames, old_tree=None, backups=True):
 
1140
        """Restore selected files to the versions from a previous tree.
 
1141
 
 
1142
        backups
 
1143
            If true (default) backups are made of files before
 
1144
            they're renamed.
 
1145
        """
 
1146
        from bzrlib.errors import NotVersionedError, BzrError
 
1147
        from bzrlib.atomicfile import AtomicFile
 
1148
        from bzrlib.osutils import backup_file
 
1149
        
 
1150
        inv = self.read_working_inventory()
 
1151
        if old_tree is None:
 
1152
            old_tree = self.basis_tree()
 
1153
        old_inv = old_tree.inventory
 
1154
 
 
1155
        nids = []
 
1156
        for fn in filenames:
 
1157
            file_id = inv.path2id(fn)
 
1158
            if not file_id:
 
1159
                raise NotVersionedError("not a versioned file", fn)
 
1160
            if not old_inv.has_id(file_id):
 
1161
                raise BzrError("file not present in old tree", fn, file_id)
 
1162
            nids.append((fn, file_id))
 
1163
            
 
1164
        # TODO: Rename back if it was previously at a different location
 
1165
 
 
1166
        # TODO: If given a directory, restore the entire contents from
 
1167
        # the previous version.
 
1168
 
 
1169
        # TODO: Make a backup to a temporary file.
 
1170
 
 
1171
        # TODO: If the file previously didn't exist, delete it?
 
1172
        for fn, file_id in nids:
 
1173
            backup_file(fn)
 
1174
            
 
1175
            f = AtomicFile(fn, 'wb')
 
1176
            try:
 
1177
                f.write(old_tree.get_file(file_id).read())
 
1178
                f.commit()
 
1179
            finally:
 
1180
                f.close()
 
1181
 
 
1182
 
 
1183
    def pending_merges(self):
 
1184
        """Return a list of pending merges.
 
1185
 
 
1186
        These are revisions that have been merged into the working
 
1187
        directory but not yet committed.
 
1188
        """
 
1189
        cfn = self._rel_controlfilename('pending-merges')
 
1190
        if not self._transport.has(cfn):
 
1191
            return []
 
1192
        p = []
 
1193
        for l in self.controlfile('pending-merges', 'r').readlines():
 
1194
            p.append(l.rstrip('\n'))
 
1195
        return p
 
1196
 
 
1197
 
 
1198
    def add_pending_merge(self, *revision_ids):
 
1199
        # TODO: Perhaps should check at this point that the
 
1200
        # history of the revision is actually present?
 
1201
        p = self.pending_merges()
 
1202
        updated = False
 
1203
        for rev_id in revision_ids:
 
1204
            if rev_id in p:
 
1205
                continue
 
1206
            p.append(rev_id)
 
1207
            updated = True
 
1208
        if updated:
 
1209
            self.set_pending_merges(p)
 
1210
 
 
1211
    def set_pending_merges(self, rev_list):
 
1212
        self.lock_write()
 
1213
        try:
 
1214
            self.put_controlfile('pending-merges', '\n'.join(rev_list))
 
1215
        finally:
 
1216
            self.unlock()
 
1217
 
1118
1218
 
1119
1219
    def get_parent(self):
1120
1220
        """Return the parent location of the branch.
1121
1221
 
1122
 
        This is the default location for pull/missing.  The usual
 
1222
        This is the default location for push/pull/missing.  The usual
1123
1223
        pattern is that the user can override it by specifying a
1124
1224
        location.
1125
1225
        """
1126
 
        parent = self._get_parent_location()
1127
 
        if parent is None:
1128
 
            return parent
1129
 
        # This is an old-format absolute path to a local branch
1130
 
        # turn it into a url
1131
 
        if parent.startswith('/'):
1132
 
            parent = urlutils.local_path_to_url(parent.decode('utf8'))
 
1226
        import errno
 
1227
        _locs = ['parent', 'pull', 'x-pull']
 
1228
        for l in _locs:
 
1229
            try:
 
1230
                return self.controlfile(l, 'r').read().strip('\n')
 
1231
            except IOError, e:
 
1232
                if e.errno != errno.ENOENT:
 
1233
                    raise
 
1234
        return None
 
1235
 
 
1236
 
 
1237
    def set_parent(self, url):
 
1238
        # TODO: Maybe delete old location files?
 
1239
        from bzrlib.atomicfile import AtomicFile
 
1240
        self.lock_write()
1133
1241
        try:
1134
 
            return urlutils.join(self.base[:-1], parent)
1135
 
        except errors.InvalidURLJoin, e:
1136
 
            raise errors.InaccessibleParent(parent, self.user_url)
1137
 
 
1138
 
    def _get_parent_location(self):
1139
 
        raise NotImplementedError(self._get_parent_location)
1140
 
 
1141
 
    def _set_config_location(self, name, url, config=None,
1142
 
                             make_relative=False):
1143
 
        if config is None:
1144
 
            config = self.get_config()
1145
 
        if url is None:
1146
 
            url = ''
1147
 
        elif make_relative:
1148
 
            url = urlutils.relative_url(self.base, url)
1149
 
        config.set_user_option(name, url, warn_masked=True)
1150
 
 
1151
 
    def _get_config_location(self, name, config=None):
1152
 
        if config is None:
1153
 
            config = self.get_config()
1154
 
        location = config.get_user_option(name)
1155
 
        if location == '':
1156
 
            location = None
1157
 
        return location
1158
 
 
1159
 
    def get_child_submit_format(self):
1160
 
        """Return the preferred format of submissions to this branch."""
1161
 
        return self.get_config().get_user_option("child_submit_format")
1162
 
 
1163
 
    def get_submit_branch(self):
1164
 
        """Return the submit location of the branch.
1165
 
 
1166
 
        This is the default location for bundle.  The usual
1167
 
        pattern is that the user can override it by specifying a
1168
 
        location.
1169
 
        """
1170
 
        return self.get_config().get_user_option('submit_branch')
1171
 
 
1172
 
    def set_submit_branch(self, location):
1173
 
        """Return the submit location of the branch.
1174
 
 
1175
 
        This is the default location for bundle.  The usual
1176
 
        pattern is that the user can override it by specifying a
1177
 
        location.
1178
 
        """
1179
 
        self.get_config().set_user_option('submit_branch', location,
1180
 
            warn_masked=True)
1181
 
 
1182
 
    def get_public_branch(self):
1183
 
        """Return the public location of the branch.
1184
 
 
1185
 
        This is used by merge directives.
1186
 
        """
1187
 
        return self._get_config_location('public_branch')
1188
 
 
1189
 
    def set_public_branch(self, location):
1190
 
        """Return the submit location of the branch.
1191
 
 
1192
 
        This is the default location for bundle.  The usual
1193
 
        pattern is that the user can override it by specifying a
1194
 
        location.
1195
 
        """
1196
 
        self._set_config_location('public_branch', location)
1197
 
 
1198
 
    def get_push_location(self):
1199
 
        """Return the None or the location to push this branch to."""
1200
 
        push_loc = self.get_config().get_user_option('push_location')
1201
 
        return push_loc
1202
 
 
1203
 
    def set_push_location(self, location):
1204
 
        """Set a new push location for this branch."""
1205
 
        raise NotImplementedError(self.set_push_location)
1206
 
 
1207
 
    def _run_post_change_branch_tip_hooks(self, old_revno, old_revid):
1208
 
        """Run the post_change_branch_tip hooks."""
1209
 
        hooks = Branch.hooks['post_change_branch_tip']
1210
 
        if not hooks:
1211
 
            return
1212
 
        new_revno, new_revid = self.last_revision_info()
1213
 
        params = ChangeBranchTipParams(
1214
 
            self, old_revno, new_revno, old_revid, new_revid)
1215
 
        for hook in hooks:
1216
 
            hook(params)
1217
 
 
1218
 
    def _run_pre_change_branch_tip_hooks(self, new_revno, new_revid):
1219
 
        """Run the pre_change_branch_tip hooks."""
1220
 
        hooks = Branch.hooks['pre_change_branch_tip']
1221
 
        if not hooks:
1222
 
            return
1223
 
        old_revno, old_revid = self.last_revision_info()
1224
 
        params = ChangeBranchTipParams(
1225
 
            self, old_revno, new_revno, old_revid, new_revid)
1226
 
        for hook in hooks:
1227
 
            hook(params)
1228
 
 
1229
 
    @needs_write_lock
1230
 
    def update(self):
1231
 
        """Synchronise this branch with the master branch if any.
1232
 
 
1233
 
        :return: None or the last_revision pivoted out during the update.
1234
 
        """
1235
 
        return None
 
1242
            f = AtomicFile(self.controlfilename('parent'))
 
1243
            try:
 
1244
                f.write(url + '\n')
 
1245
                f.commit()
 
1246
            finally:
 
1247
                f.close()
 
1248
        finally:
 
1249
            self.unlock()
1236
1250
 
1237
1251
    def check_revno(self, revno):
1238
1252
        """\
1241
1255
        """
1242
1256
        if revno != 0:
1243
1257
            self.check_real_revno(revno)
1244
 
 
 
1258
            
1245
1259
    def check_real_revno(self, revno):
1246
1260
        """\
1247
1261
        Check whether a revno corresponds to a real revision.
1248
1262
        Zero (the NULL revision) is considered invalid
1249
1263
        """
1250
1264
        if revno < 1 or revno > self.revno():
1251
 
            raise errors.InvalidRevisionNumber(revno)
1252
 
 
1253
 
    @needs_read_lock
1254
 
    def clone(self, to_bzrdir, revision_id=None, repository_policy=None):
1255
 
        """Clone this branch into to_bzrdir preserving all semantic values.
1256
 
 
1257
 
        Most API users will want 'create_clone_on_transport', which creates a
1258
 
        new bzrdir and branch on the fly.
1259
 
 
1260
 
        revision_id: if not None, the revision history in the new branch will
1261
 
                     be truncated to end with revision_id.
1262
 
        """
1263
 
        result = to_bzrdir.create_branch()
1264
 
        result.lock_write()
1265
 
        try:
1266
 
            if repository_policy is not None:
1267
 
                repository_policy.configure_branch(result)
1268
 
            self.copy_content_into(result, revision_id=revision_id)
1269
 
        finally:
1270
 
            result.unlock()
1271
 
        return result
1272
 
 
1273
 
    @needs_read_lock
1274
 
    def sprout(self, to_bzrdir, revision_id=None, repository_policy=None,
1275
 
            repository=None):
1276
 
        """Create a new line of development from the branch, into to_bzrdir.
1277
 
 
1278
 
        to_bzrdir controls the branch format.
1279
 
 
1280
 
        revision_id: if not None, the revision history in the new branch will
1281
 
                     be truncated to end with revision_id.
1282
 
        """
1283
 
        if (repository_policy is not None and
1284
 
            repository_policy.requires_stacking()):
1285
 
            to_bzrdir._format.require_stacking(_skip_repo=True)
1286
 
        result = to_bzrdir.create_branch(repository=repository)
1287
 
        result.lock_write()
1288
 
        try:
1289
 
            if repository_policy is not None:
1290
 
                repository_policy.configure_branch(result)
1291
 
            self.copy_content_into(result, revision_id=revision_id)
1292
 
            master_url = self.get_bound_location()
1293
 
            if master_url is None:
1294
 
                result.set_parent(self.bzrdir.root_transport.base)
1295
 
            else:
1296
 
                result.set_parent(master_url)
1297
 
        finally:
1298
 
            result.unlock()
1299
 
        return result
1300
 
 
1301
 
    def _synchronize_history(self, destination, revision_id):
1302
 
        """Synchronize last revision and revision history between branches.
1303
 
 
1304
 
        This version is most efficient when the destination is also a
1305
 
        BzrBranch6, but works for BzrBranch5, as long as the destination's
1306
 
        repository contains all the lefthand ancestors of the intended
1307
 
        last_revision.  If not, set_last_revision_info will fail.
1308
 
 
1309
 
        :param destination: The branch to copy the history into
1310
 
        :param revision_id: The revision-id to truncate history at.  May
1311
 
          be None to copy complete history.
1312
 
        """
1313
 
        source_revno, source_revision_id = self.last_revision_info()
1314
 
        if revision_id is None:
1315
 
            revno, revision_id = source_revno, source_revision_id
1316
 
        else:
1317
 
            graph = self.repository.get_graph()
1318
 
            try:
1319
 
                revno = graph.find_distance_to_null(revision_id, 
1320
 
                    [(source_revision_id, source_revno)])
1321
 
            except errors.GhostRevisionsHaveNoRevno:
1322
 
                # Default to 1, if we can't find anything else
1323
 
                revno = 1
1324
 
        destination.set_last_revision_info(revno, revision_id)
1325
 
 
1326
 
    def copy_content_into(self, destination, revision_id=None):
1327
 
        """Copy the content of self into destination.
1328
 
 
1329
 
        revision_id: if not None, the revision history in the new branch will
1330
 
                     be truncated to end with revision_id.
1331
 
        """
1332
 
        return InterBranch.get(self, destination).copy_content_into(
1333
 
            revision_id=revision_id)
1334
 
 
1335
 
    def update_references(self, target):
1336
 
        if not getattr(self._format, 'supports_reference_locations', False):
1337
 
            return
1338
 
        reference_dict = self._get_all_reference_info()
1339
 
        if len(reference_dict) == 0:
1340
 
            return
1341
 
        old_base = self.base
1342
 
        new_base = target.base
1343
 
        target_reference_dict = target._get_all_reference_info()
1344
 
        for file_id, (tree_path, branch_location) in (
1345
 
            reference_dict.items()):
1346
 
            branch_location = urlutils.rebase_url(branch_location,
1347
 
                                                  old_base, new_base)
1348
 
            target_reference_dict.setdefault(
1349
 
                file_id, (tree_path, branch_location))
1350
 
        target._set_all_reference_info(target_reference_dict)
1351
 
 
1352
 
    @needs_read_lock
1353
 
    def check(self, refs):
1354
 
        """Check consistency of the branch.
1355
 
 
1356
 
        In particular this checks that revisions given in the revision-history
1357
 
        do actually match up in the revision graph, and that they're all
1358
 
        present in the repository.
1359
 
 
1360
 
        Callers will typically also want to check the repository.
1361
 
 
1362
 
        :param refs: Calculated refs for this branch as specified by
1363
 
            branch._get_check_refs()
1364
 
        :return: A BranchCheckResult.
1365
 
        """
1366
 
        result = BranchCheckResult(self)
1367
 
        last_revno, last_revision_id = self.last_revision_info()
1368
 
        actual_revno = refs[('lefthand-distance', last_revision_id)]
1369
 
        if actual_revno != last_revno:
1370
 
            result.errors.append(errors.BzrCheckError(
1371
 
                'revno does not match len(mainline) %s != %s' % (
1372
 
                last_revno, actual_revno)))
1373
 
        # TODO: We should probably also check that self.revision_history
1374
 
        # matches the repository for older branch formats.
1375
 
        # If looking for the code that cross-checks repository parents against
1376
 
        # the iter_reverse_revision_history output, that is now a repository
1377
 
        # specific check.
1378
 
        return result
1379
 
 
1380
 
    def _get_checkout_format(self):
1381
 
        """Return the most suitable metadir for a checkout of this branch.
1382
 
        Weaves are used if this branch's repository uses weaves.
1383
 
        """
1384
 
        format = self.repository.bzrdir.checkout_metadir()
1385
 
        format.set_branch_format(self._format)
1386
 
        return format
1387
 
 
1388
 
    def create_clone_on_transport(self, to_transport, revision_id=None,
1389
 
        stacked_on=None, create_prefix=False, use_existing_dir=False,
1390
 
        no_tree=None):
1391
 
        """Create a clone of this branch and its bzrdir.
1392
 
 
1393
 
        :param to_transport: The transport to clone onto.
1394
 
        :param revision_id: The revision id to use as tip in the new branch.
1395
 
            If None the tip is obtained from this branch.
1396
 
        :param stacked_on: An optional URL to stack the clone on.
1397
 
        :param create_prefix: Create any missing directories leading up to
1398
 
            to_transport.
1399
 
        :param use_existing_dir: Use an existing directory if one exists.
1400
 
        """
1401
 
        # XXX: Fix the bzrdir API to allow getting the branch back from the
1402
 
        # clone call. Or something. 20090224 RBC/spiv.
1403
 
        # XXX: Should this perhaps clone colocated branches as well, 
1404
 
        # rather than just the default branch? 20100319 JRV
1405
 
        if revision_id is None:
1406
 
            revision_id = self.last_revision()
1407
 
        dir_to = self.bzrdir.clone_on_transport(to_transport,
1408
 
            revision_id=revision_id, stacked_on=stacked_on,
1409
 
            create_prefix=create_prefix, use_existing_dir=use_existing_dir,
1410
 
            no_tree=no_tree)
1411
 
        return dir_to.open_branch()
1412
 
 
1413
 
    def create_checkout(self, to_location, revision_id=None,
1414
 
                        lightweight=False, accelerator_tree=None,
1415
 
                        hardlink=False):
1416
 
        """Create a checkout of a branch.
1417
 
 
1418
 
        :param to_location: The url to produce the checkout at
1419
 
        :param revision_id: The revision to check out
1420
 
        :param lightweight: If True, produce a lightweight checkout, otherwise,
1421
 
            produce a bound branch (heavyweight checkout)
1422
 
        :param accelerator_tree: A tree which can be used for retrieving file
1423
 
            contents more quickly than the revision tree, i.e. a workingtree.
1424
 
            The revision tree will be used for cases where accelerator_tree's
1425
 
            content is different.
1426
 
        :param hardlink: If true, hard-link files from accelerator_tree,
1427
 
            where possible.
1428
 
        :return: The tree of the created checkout
1429
 
        """
1430
 
        t = transport.get_transport(to_location)
1431
 
        t.ensure_base()
1432
 
        if lightweight:
1433
 
            format = self._get_checkout_format()
1434
 
            checkout = format.initialize_on_transport(t)
1435
 
            from_branch = BranchReferenceFormat().initialize(checkout, 
1436
 
                target_branch=self)
1437
 
        else:
1438
 
            format = self._get_checkout_format()
1439
 
            checkout_branch = bzrdir.BzrDir.create_branch_convenience(
1440
 
                to_location, force_new_tree=False, format=format)
1441
 
            checkout = checkout_branch.bzrdir
1442
 
            checkout_branch.bind(self)
1443
 
            # pull up to the specified revision_id to set the initial
1444
 
            # branch tip correctly, and seed it with history.
1445
 
            checkout_branch.pull(self, stop_revision=revision_id)
1446
 
            from_branch=None
1447
 
        tree = checkout.create_workingtree(revision_id,
1448
 
                                           from_branch=from_branch,
1449
 
                                           accelerator_tree=accelerator_tree,
1450
 
                                           hardlink=hardlink)
1451
 
        basis_tree = tree.basis_tree()
1452
 
        basis_tree.lock_read()
1453
 
        try:
1454
 
            for path, file_id in basis_tree.iter_references():
1455
 
                reference_parent = self.reference_parent(file_id, path)
1456
 
                reference_parent.create_checkout(tree.abspath(path),
1457
 
                    basis_tree.get_reference_revision(file_id, path),
1458
 
                    lightweight)
1459
 
        finally:
1460
 
            basis_tree.unlock()
1461
 
        return tree
1462
 
 
1463
 
    @needs_write_lock
1464
 
    def reconcile(self, thorough=True):
1465
 
        """Make sure the data stored in this branch is consistent."""
1466
 
        from bzrlib.reconcile import BranchReconciler
1467
 
        reconciler = BranchReconciler(self, thorough=thorough)
1468
 
        reconciler.reconcile()
1469
 
        return reconciler
1470
 
 
1471
 
    def reference_parent(self, file_id, path, possible_transports=None):
1472
 
        """Return the parent branch for a tree-reference file_id
1473
 
 
1474
 
        :param file_id: The file_id of the tree reference
1475
 
        :param path: The path of the file_id in the tree
1476
 
        :return: A branch associated with the file_id
1477
 
        """
1478
 
        # FIXME should provide multiple branches, based on config
1479
 
        return Branch.open(self.bzrdir.root_transport.clone(path).base,
1480
 
                           possible_transports=possible_transports)
1481
 
 
1482
 
    def supports_tags(self):
1483
 
        return self._format.supports_tags()
1484
 
 
1485
 
    def automatic_tag_name(self, revision_id):
1486
 
        """Try to automatically find the tag name for a revision.
1487
 
 
1488
 
        :param revision_id: Revision id of the revision.
1489
 
        :return: A tag name or None if no tag name could be determined.
1490
 
        """
1491
 
        for hook in Branch.hooks['automatic_tag_name']:
1492
 
            ret = hook(self, revision_id)
1493
 
            if ret is not None:
1494
 
                return ret
1495
 
        return None
1496
 
 
1497
 
    def _check_if_descendant_or_diverged(self, revision_a, revision_b, graph,
1498
 
                                         other_branch):
1499
 
        """Ensure that revision_b is a descendant of revision_a.
1500
 
 
1501
 
        This is a helper function for update_revisions.
1502
 
 
1503
 
        :raises: DivergedBranches if revision_b has diverged from revision_a.
1504
 
        :returns: True if revision_b is a descendant of revision_a.
1505
 
        """
1506
 
        relation = self._revision_relations(revision_a, revision_b, graph)
1507
 
        if relation == 'b_descends_from_a':
1508
 
            return True
1509
 
        elif relation == 'diverged':
1510
 
            raise errors.DivergedBranches(self, other_branch)
1511
 
        elif relation == 'a_descends_from_b':
1512
 
            return False
1513
 
        else:
1514
 
            raise AssertionError("invalid relation: %r" % (relation,))
1515
 
 
1516
 
    def _revision_relations(self, revision_a, revision_b, graph):
1517
 
        """Determine the relationship between two revisions.
1518
 
 
1519
 
        :returns: One of: 'a_descends_from_b', 'b_descends_from_a', 'diverged'
1520
 
        """
1521
 
        heads = graph.heads([revision_a, revision_b])
1522
 
        if heads == set([revision_b]):
1523
 
            return 'b_descends_from_a'
1524
 
        elif heads == set([revision_a, revision_b]):
1525
 
            # These branches have diverged
1526
 
            return 'diverged'
1527
 
        elif heads == set([revision_a]):
1528
 
            return 'a_descends_from_b'
1529
 
        else:
1530
 
            raise AssertionError("invalid heads: %r" % (heads,))
1531
 
 
1532
 
    def heads_to_fetch(self):
1533
 
        """Return the heads that must and that should be fetched to copy this
1534
 
        branch into another repo.
1535
 
 
1536
 
        :returns: a 2-tuple of (must_fetch, if_present_fetch).  must_fetch is a
1537
 
            set of heads that must be fetched.  if_present_fetch is a set of
1538
 
            heads that must be fetched if present, but no error is necessary if
1539
 
            they are not present.
1540
 
        """
1541
 
        # For bzr native formats must_fetch is just the tip, and if_present_fetch
1542
 
        # are the tags.
1543
 
        must_fetch = set([self.last_revision()])
1544
 
        if_present_fetch = set()
1545
 
        c = self.get_config()
1546
 
        include_tags = c.get_user_option_as_bool('branch.fetch_tags',
1547
 
                                                 default=False)
1548
 
        if include_tags:
1549
 
            try:
1550
 
                if_present_fetch = set(self.tags.get_reverse_tag_dict())
1551
 
            except errors.TagsNotSupported:
1552
 
                pass
1553
 
        must_fetch.discard(_mod_revision.NULL_REVISION)
1554
 
        if_present_fetch.discard(_mod_revision.NULL_REVISION)
1555
 
        return must_fetch, if_present_fetch
1556
 
 
1557
 
 
1558
 
class BranchFormat(controldir.ControlComponentFormat):
1559
 
    """An encapsulation of the initialization and open routines for a format.
1560
 
 
1561
 
    Formats provide three things:
1562
 
     * An initialization routine,
1563
 
     * a format string,
1564
 
     * an open routine.
1565
 
 
1566
 
    Formats are placed in an dict by their format string for reference
1567
 
    during branch opening. It's not required that these be instances, they
1568
 
    can be classes themselves with class methods - it simply depends on
1569
 
    whether state is needed for a given format or not.
1570
 
 
1571
 
    Once a format is deprecated, just deprecate the initialize and open
1572
 
    methods on the format class. Do not deprecate the object, as the
1573
 
    object will be created every time regardless.
 
1265
            raise InvalidRevisionNumber(revno)
 
1266
        
 
1267
        
 
1268
        
 
1269
 
 
1270
 
 
1271
class ScratchBranch(_Branch):
 
1272
    """Special test class: a branch that cleans up after itself.
 
1273
 
 
1274
    >>> b = ScratchBranch()
 
1275
    >>> isdir(b.base)
 
1276
    True
 
1277
    >>> bd = b.base
 
1278
    >>> b.destroy()
 
1279
    >>> isdir(bd)
 
1280
    False
1574
1281
    """
1575
 
 
1576
 
    can_set_append_revisions_only = True
1577
 
 
1578
 
    def __eq__(self, other):
1579
 
        return self.__class__ is other.__class__
1580
 
 
1581
 
    def __ne__(self, other):
1582
 
        return not (self == other)
1583
 
 
1584
 
    @classmethod
1585
 
    def find_format(klass, a_bzrdir, name=None):
1586
 
        """Return the format for the branch object in a_bzrdir."""
 
1282
    def __init__(self, files=[], dirs=[], base=None):
 
1283
        """Make a test branch.
 
1284
 
 
1285
        This creates a temporary directory and runs init-tree in it.
 
1286
 
 
1287
        If any files are listed, they are created in the working copy.
 
1288
        """
 
1289
        from tempfile import mkdtemp
 
1290
        init = False
 
1291
        if base is None:
 
1292
            base = mkdtemp()
 
1293
            init = True
 
1294
        if isinstance(base, basestring):
 
1295
            base = get_transport(base)
 
1296
        _Branch.__init__(self, base, init=init)
 
1297
        for d in dirs:
 
1298
            self._transport.mkdir(d)
 
1299
            
 
1300
        for f in files:
 
1301
            self._transport.put(f, 'content of %s' % f)
 
1302
 
 
1303
 
 
1304
    def clone(self):
 
1305
        """
 
1306
        >>> orig = ScratchBranch(files=["file1", "file2"])
 
1307
        >>> clone = orig.clone()
 
1308
        >>> if os.name != 'nt':
 
1309
        ...   os.path.samefile(orig.base, clone.base)
 
1310
        ... else:
 
1311
        ...   orig.base == clone.base
 
1312
        ...
 
1313
        False
 
1314
        >>> os.path.isfile(os.path.join(clone.base, "file1"))
 
1315
        True
 
1316
        """
 
1317
        from shutil import copytree
 
1318
        from tempfile import mkdtemp
 
1319
        base = mkdtemp()
 
1320
        os.rmdir(base)
 
1321
        copytree(self.base, base, symlinks=True)
 
1322
        return ScratchBranch(base=base)
 
1323
 
 
1324
    def __del__(self):
 
1325
        self.destroy()
 
1326
 
 
1327
    def destroy(self):
 
1328
        """Destroy the test branch, removing the scratch directory."""
 
1329
        from shutil import rmtree
1587
1330
        try:
1588
 
            transport = a_bzrdir.get_branch_transport(None, name=name)
1589
 
            format_string = transport.get_bytes("format")
1590
 
            return format_registry.get(format_string)
1591
 
        except errors.NoSuchFile:
1592
 
            raise errors.NotBranchError(path=transport.base, bzrdir=a_bzrdir)
1593
 
        except KeyError:
1594
 
            raise errors.UnknownFormatError(format=format_string, kind='branch')
1595
 
 
1596
 
    @classmethod
1597
 
    @deprecated_method(deprecated_in((2, 4, 0)))
1598
 
    def get_default_format(klass):
1599
 
        """Return the current default format."""
1600
 
        return format_registry.get_default()
1601
 
 
1602
 
    @classmethod
1603
 
    @deprecated_method(deprecated_in((2, 4, 0)))
1604
 
    def get_formats(klass):
1605
 
        """Get all the known formats.
1606
 
 
1607
 
        Warning: This triggers a load of all lazy registered formats: do not
1608
 
        use except when that is desireed.
1609
 
        """
1610
 
        return format_registry._get_all()
1611
 
 
1612
 
    def get_reference(self, a_bzrdir, name=None):
1613
 
        """Get the target reference of the branch in a_bzrdir.
1614
 
 
1615
 
        format probing must have been completed before calling
1616
 
        this method - it is assumed that the format of the branch
1617
 
        in a_bzrdir is correct.
1618
 
 
1619
 
        :param a_bzrdir: The bzrdir to get the branch data from.
1620
 
        :param name: Name of the colocated branch to fetch
1621
 
        :return: None if the branch is not a reference branch.
1622
 
        """
1623
 
        return None
1624
 
 
1625
 
    @classmethod
1626
 
    def set_reference(self, a_bzrdir, name, to_branch):
1627
 
        """Set the target reference of the branch in a_bzrdir.
1628
 
 
1629
 
        format probing must have been completed before calling
1630
 
        this method - it is assumed that the format of the branch
1631
 
        in a_bzrdir is correct.
1632
 
 
1633
 
        :param a_bzrdir: The bzrdir to set the branch reference for.
1634
 
        :param name: Name of colocated branch to set, None for default
1635
 
        :param to_branch: branch that the checkout is to reference
1636
 
        """
1637
 
        raise NotImplementedError(self.set_reference)
1638
 
 
1639
 
    def get_format_string(self):
1640
 
        """Return the ASCII format string that identifies this format."""
1641
 
        raise NotImplementedError(self.get_format_string)
1642
 
 
1643
 
    def get_format_description(self):
1644
 
        """Return the short format description for this format."""
1645
 
        raise NotImplementedError(self.get_format_description)
1646
 
 
1647
 
    def _run_post_branch_init_hooks(self, a_bzrdir, name, branch):
1648
 
        hooks = Branch.hooks['post_branch_init']
1649
 
        if not hooks:
1650
 
            return
1651
 
        params = BranchInitHookParams(self, a_bzrdir, name, branch)
1652
 
        for hook in hooks:
1653
 
            hook(params)
1654
 
 
1655
 
    def initialize(self, a_bzrdir, name=None, repository=None):
1656
 
        """Create a branch of this format in a_bzrdir.
1657
 
        
1658
 
        :param name: Name of the colocated branch to create.
1659
 
        """
1660
 
        raise NotImplementedError(self.initialize)
1661
 
 
1662
 
    def is_supported(self):
1663
 
        """Is this format supported?
1664
 
 
1665
 
        Supported formats can be initialized and opened.
1666
 
        Unsupported formats may not support initialization or committing or
1667
 
        some other features depending on the reason for not being supported.
1668
 
        """
1669
 
        return True
1670
 
 
1671
 
    def make_tags(self, branch):
1672
 
        """Create a tags object for branch.
1673
 
 
1674
 
        This method is on BranchFormat, because BranchFormats are reflected
1675
 
        over the wire via network_name(), whereas full Branch instances require
1676
 
        multiple VFS method calls to operate at all.
1677
 
 
1678
 
        The default implementation returns a disabled-tags instance.
1679
 
 
1680
 
        Note that it is normal for branch to be a RemoteBranch when using tags
1681
 
        on a RemoteBranch.
1682
 
        """
1683
 
        return DisabledTags(branch)
1684
 
 
1685
 
    def network_name(self):
1686
 
        """A simple byte string uniquely identifying this format for RPC calls.
1687
 
 
1688
 
        MetaDir branch formats use their disk format string to identify the
1689
 
        repository over the wire. All in one formats such as bzr < 0.8, and
1690
 
        foreign formats like svn/git and hg should use some marker which is
1691
 
        unique and immutable.
1692
 
        """
1693
 
        raise NotImplementedError(self.network_name)
1694
 
 
1695
 
    def open(self, a_bzrdir, name=None, _found=False, ignore_fallbacks=False,
1696
 
            found_repository=None):
1697
 
        """Return the branch object for a_bzrdir
1698
 
 
1699
 
        :param a_bzrdir: A BzrDir that contains a branch.
1700
 
        :param name: Name of colocated branch to open
1701
 
        :param _found: a private parameter, do not use it. It is used to
1702
 
            indicate if format probing has already be done.
1703
 
        :param ignore_fallbacks: when set, no fallback branches will be opened
1704
 
            (if there are any).  Default is to open fallbacks.
1705
 
        """
1706
 
        raise NotImplementedError(self.open)
1707
 
 
1708
 
    @classmethod
1709
 
    @deprecated_method(deprecated_in((2, 4, 0)))
1710
 
    def register_format(klass, format):
1711
 
        """Register a metadir format.
1712
 
 
1713
 
        See MetaDirBranchFormatFactory for the ability to register a format
1714
 
        without loading the code the format needs until it is actually used.
1715
 
        """
1716
 
        format_registry.register(format)
1717
 
 
1718
 
    @classmethod
1719
 
    @deprecated_method(deprecated_in((2, 4, 0)))
1720
 
    def set_default_format(klass, format):
1721
 
        format_registry.set_default(format)
1722
 
 
1723
 
    def supports_set_append_revisions_only(self):
1724
 
        """True if this format supports set_append_revisions_only."""
1725
 
        return False
1726
 
 
1727
 
    def supports_stacking(self):
1728
 
        """True if this format records a stacked-on branch."""
1729
 
        return False
1730
 
 
1731
 
    def supports_leaving_lock(self):
1732
 
        """True if this format supports leaving locks in place."""
1733
 
        return False # by default
1734
 
 
1735
 
    @classmethod
1736
 
    @deprecated_method(deprecated_in((2, 4, 0)))
1737
 
    def unregister_format(klass, format):
1738
 
        format_registry.remove(format)
1739
 
 
1740
 
    def __str__(self):
1741
 
        return self.get_format_description().rstrip()
1742
 
 
1743
 
    def supports_tags(self):
1744
 
        """True if this format supports tags stored in the branch"""
1745
 
        return False  # by default
1746
 
 
1747
 
 
1748
 
class MetaDirBranchFormatFactory(registry._LazyObjectGetter):
1749
 
    """A factory for a BranchFormat object, permitting simple lazy registration.
 
1331
            if self.base:
 
1332
                mutter("delete ScratchBranch %s" % self.base)
 
1333
                rmtree(self.base)
 
1334
        except OSError, e:
 
1335
            # Work around for shutil.rmtree failing on Windows when
 
1336
            # readonly files are encountered
 
1337
            mutter("hit exception in destroying ScratchBranch: %s" % e)
 
1338
            for root, dirs, files in os.walk(self.base, topdown=False):
 
1339
                for name in files:
 
1340
                    os.chmod(os.path.join(root, name), 0700)
 
1341
            rmtree(self.base)
 
1342
        self._transport = None
 
1343
 
1750
1344
    
1751
 
    While none of the built in BranchFormats are lazy registered yet,
1752
 
    bzrlib.tests.test_branch.TestMetaDirBranchFormatFactory demonstrates how to
1753
 
    use it, and the bzr-loom plugin uses it as well (see
1754
 
    bzrlib.plugins.loom.formats).
1755
 
    """
1756
 
 
1757
 
    def __init__(self, format_string, module_name, member_name):
1758
 
        """Create a MetaDirBranchFormatFactory.
1759
 
 
1760
 
        :param format_string: The format string the format has.
1761
 
        :param module_name: Module to load the format class from.
1762
 
        :param member_name: Attribute name within the module for the format class.
1763
 
        """
1764
 
        registry._LazyObjectGetter.__init__(self, module_name, member_name)
1765
 
        self._format_string = format_string
1766
 
        
1767
 
    def get_format_string(self):
1768
 
        """See BranchFormat.get_format_string."""
1769
 
        return self._format_string
1770
 
 
1771
 
    def __call__(self):
1772
 
        """Used for network_format_registry support."""
1773
 
        return self.get_obj()()
1774
 
 
1775
 
 
1776
 
class BranchHooks(Hooks):
1777
 
    """A dictionary mapping hook name to a list of callables for branch hooks.
1778
 
 
1779
 
    e.g. ['set_rh'] Is the list of items to be called when the
1780
 
    set_revision_history function is invoked.
1781
 
    """
1782
 
 
1783
 
    def __init__(self):
1784
 
        """Create the default hooks.
1785
 
 
1786
 
        These are all empty initially, because by default nothing should get
1787
 
        notified.
1788
 
        """
1789
 
        Hooks.__init__(self, "bzrlib.branch", "Branch.hooks")
1790
 
        self.add_hook('set_rh',
1791
 
            "Invoked whenever the revision history has been set via "
1792
 
            "set_revision_history. The api signature is (branch, "
1793
 
            "revision_history), and the branch will be write-locked. "
1794
 
            "The set_rh hook can be expensive for bzr to trigger, a better "
1795
 
            "hook to use is Branch.post_change_branch_tip.", (0, 15))
1796
 
        self.add_hook('open',
1797
 
            "Called with the Branch object that has been opened after a "
1798
 
            "branch is opened.", (1, 8))
1799
 
        self.add_hook('post_push',
1800
 
            "Called after a push operation completes. post_push is called "
1801
 
            "with a bzrlib.branch.BranchPushResult object and only runs in the "
1802
 
            "bzr client.", (0, 15))
1803
 
        self.add_hook('post_pull',
1804
 
            "Called after a pull operation completes. post_pull is called "
1805
 
            "with a bzrlib.branch.PullResult object and only runs in the "
1806
 
            "bzr client.", (0, 15))
1807
 
        self.add_hook('pre_commit',
1808
 
            "Called after a commit is calculated but before it is "
1809
 
            "completed. pre_commit is called with (local, master, old_revno, "
1810
 
            "old_revid, future_revno, future_revid, tree_delta, future_tree"
1811
 
            "). old_revid is NULL_REVISION for the first commit to a branch, "
1812
 
            "tree_delta is a TreeDelta object describing changes from the "
1813
 
            "basis revision. hooks MUST NOT modify this delta. "
1814
 
            " future_tree is an in-memory tree obtained from "
1815
 
            "CommitBuilder.revision_tree() and hooks MUST NOT modify this "
1816
 
            "tree.", (0,91))
1817
 
        self.add_hook('post_commit',
1818
 
            "Called in the bzr client after a commit has completed. "
1819
 
            "post_commit is called with (local, master, old_revno, old_revid, "
1820
 
            "new_revno, new_revid). old_revid is NULL_REVISION for the first "
1821
 
            "commit to a branch.", (0, 15))
1822
 
        self.add_hook('post_uncommit',
1823
 
            "Called in the bzr client after an uncommit completes. "
1824
 
            "post_uncommit is called with (local, master, old_revno, "
1825
 
            "old_revid, new_revno, new_revid) where local is the local branch "
1826
 
            "or None, master is the target branch, and an empty branch "
1827
 
            "receives new_revno of 0, new_revid of None.", (0, 15))
1828
 
        self.add_hook('pre_change_branch_tip',
1829
 
            "Called in bzr client and server before a change to the tip of a "
1830
 
            "branch is made. pre_change_branch_tip is called with a "
1831
 
            "bzrlib.branch.ChangeBranchTipParams. Note that push, pull, "
1832
 
            "commit, uncommit will all trigger this hook.", (1, 6))
1833
 
        self.add_hook('post_change_branch_tip',
1834
 
            "Called in bzr client and server after a change to the tip of a "
1835
 
            "branch is made. post_change_branch_tip is called with a "
1836
 
            "bzrlib.branch.ChangeBranchTipParams. Note that push, pull, "
1837
 
            "commit, uncommit will all trigger this hook.", (1, 4))
1838
 
        self.add_hook('transform_fallback_location',
1839
 
            "Called when a stacked branch is activating its fallback "
1840
 
            "locations. transform_fallback_location is called with (branch, "
1841
 
            "url), and should return a new url. Returning the same url "
1842
 
            "allows it to be used as-is, returning a different one can be "
1843
 
            "used to cause the branch to stack on a closer copy of that "
1844
 
            "fallback_location. Note that the branch cannot have history "
1845
 
            "accessing methods called on it during this hook because the "
1846
 
            "fallback locations have not been activated. When there are "
1847
 
            "multiple hooks installed for transform_fallback_location, "
1848
 
            "all are called with the url returned from the previous hook."
1849
 
            "The order is however undefined.", (1, 9))
1850
 
        self.add_hook('automatic_tag_name',
1851
 
            "Called to determine an automatic tag name for a revision. "
1852
 
            "automatic_tag_name is called with (branch, revision_id) and "
1853
 
            "should return a tag name or None if no tag name could be "
1854
 
            "determined. The first non-None tag name returned will be used.",
1855
 
            (2, 2))
1856
 
        self.add_hook('post_branch_init',
1857
 
            "Called after new branch initialization completes. "
1858
 
            "post_branch_init is called with a "
1859
 
            "bzrlib.branch.BranchInitHookParams. "
1860
 
            "Note that init, branch and checkout (both heavyweight and "
1861
 
            "lightweight) will all trigger this hook.", (2, 2))
1862
 
        self.add_hook('post_switch',
1863
 
            "Called after a checkout switches branch. "
1864
 
            "post_switch is called with a "
1865
 
            "bzrlib.branch.SwitchHookParams.", (2, 2))
1866
 
 
1867
 
 
1868
 
 
1869
 
# install the default hooks into the Branch class.
1870
 
Branch.hooks = BranchHooks()
1871
 
 
1872
 
 
1873
 
class ChangeBranchTipParams(object):
1874
 
    """Object holding parameters passed to `*_change_branch_tip` hooks.
1875
 
 
1876
 
    There are 5 fields that hooks may wish to access:
1877
 
 
1878
 
    :ivar branch: the branch being changed
1879
 
    :ivar old_revno: revision number before the change
1880
 
    :ivar new_revno: revision number after the change
1881
 
    :ivar old_revid: revision id before the change
1882
 
    :ivar new_revid: revision id after the change
1883
 
 
1884
 
    The revid fields are strings. The revno fields are integers.
1885
 
    """
1886
 
 
1887
 
    def __init__(self, branch, old_revno, new_revno, old_revid, new_revid):
1888
 
        """Create a group of ChangeBranchTip parameters.
1889
 
 
1890
 
        :param branch: The branch being changed.
1891
 
        :param old_revno: Revision number before the change.
1892
 
        :param new_revno: Revision number after the change.
1893
 
        :param old_revid: Tip revision id before the change.
1894
 
        :param new_revid: Tip revision id after the change.
1895
 
        """
1896
 
        self.branch = branch
1897
 
        self.old_revno = old_revno
1898
 
        self.new_revno = new_revno
1899
 
        self.old_revid = old_revid
1900
 
        self.new_revid = new_revid
1901
 
 
1902
 
    def __eq__(self, other):
1903
 
        return self.__dict__ == other.__dict__
1904
 
 
1905
 
    def __repr__(self):
1906
 
        return "<%s of %s from (%s, %s) to (%s, %s)>" % (
1907
 
            self.__class__.__name__, self.branch,
1908
 
            self.old_revno, self.old_revid, self.new_revno, self.new_revid)
1909
 
 
1910
 
 
1911
 
class BranchInitHookParams(object):
1912
 
    """Object holding parameters passed to `*_branch_init` hooks.
1913
 
 
1914
 
    There are 4 fields that hooks may wish to access:
1915
 
 
1916
 
    :ivar format: the branch format
1917
 
    :ivar bzrdir: the BzrDir where the branch will be/has been initialized
1918
 
    :ivar name: name of colocated branch, if any (or None)
1919
 
    :ivar branch: the branch created
1920
 
 
1921
 
    Note that for lightweight checkouts, the bzrdir and format fields refer to
1922
 
    the checkout, hence they are different from the corresponding fields in
1923
 
    branch, which refer to the original branch.
1924
 
    """
1925
 
 
1926
 
    def __init__(self, format, a_bzrdir, name, branch):
1927
 
        """Create a group of BranchInitHook parameters.
1928
 
 
1929
 
        :param format: the branch format
1930
 
        :param a_bzrdir: the BzrDir where the branch will be/has been
1931
 
            initialized
1932
 
        :param name: name of colocated branch, if any (or None)
1933
 
        :param branch: the branch created
1934
 
 
1935
 
        Note that for lightweight checkouts, the bzrdir and format fields refer
1936
 
        to the checkout, hence they are different from the corresponding fields
1937
 
        in branch, which refer to the original branch.
1938
 
        """
1939
 
        self.format = format
1940
 
        self.bzrdir = a_bzrdir
1941
 
        self.name = name
1942
 
        self.branch = branch
1943
 
 
1944
 
    def __eq__(self, other):
1945
 
        return self.__dict__ == other.__dict__
1946
 
 
1947
 
    def __repr__(self):
1948
 
        return "<%s of %s>" % (self.__class__.__name__, self.branch)
1949
 
 
1950
 
 
1951
 
class SwitchHookParams(object):
1952
 
    """Object holding parameters passed to `*_switch` hooks.
1953
 
 
1954
 
    There are 4 fields that hooks may wish to access:
1955
 
 
1956
 
    :ivar control_dir: BzrDir of the checkout to change
1957
 
    :ivar to_branch: branch that the checkout is to reference
1958
 
    :ivar force: skip the check for local commits in a heavy checkout
1959
 
    :ivar revision_id: revision ID to switch to (or None)
1960
 
    """
1961
 
 
1962
 
    def __init__(self, control_dir, to_branch, force, revision_id):
1963
 
        """Create a group of SwitchHook parameters.
1964
 
 
1965
 
        :param control_dir: BzrDir of the checkout to change
1966
 
        :param to_branch: branch that the checkout is to reference
1967
 
        :param force: skip the check for local commits in a heavy checkout
1968
 
        :param revision_id: revision ID to switch to (or None)
1969
 
        """
1970
 
        self.control_dir = control_dir
1971
 
        self.to_branch = to_branch
1972
 
        self.force = force
1973
 
        self.revision_id = revision_id
1974
 
 
1975
 
    def __eq__(self, other):
1976
 
        return self.__dict__ == other.__dict__
1977
 
 
1978
 
    def __repr__(self):
1979
 
        return "<%s for %s to (%s, %s)>" % (self.__class__.__name__,
1980
 
            self.control_dir, self.to_branch,
1981
 
            self.revision_id)
1982
 
 
1983
 
 
1984
 
class BranchFormatMetadir(BranchFormat):
1985
 
    """Common logic for meta-dir based branch formats."""
1986
 
 
1987
 
    def _branch_class(self):
1988
 
        """What class to instantiate on open calls."""
1989
 
        raise NotImplementedError(self._branch_class)
1990
 
 
1991
 
    def _initialize_helper(self, a_bzrdir, utf8_files, name=None,
1992
 
                           repository=None):
1993
 
        """Initialize a branch in a bzrdir, with specified files
1994
 
 
1995
 
        :param a_bzrdir: The bzrdir to initialize the branch in
1996
 
        :param utf8_files: The files to create as a list of
1997
 
            (filename, content) tuples
1998
 
        :param name: Name of colocated branch to create, if any
1999
 
        :return: a branch in this format
2000
 
        """
2001
 
        mutter('creating branch %r in %s', self, a_bzrdir.user_url)
2002
 
        branch_transport = a_bzrdir.get_branch_transport(self, name=name)
2003
 
        control_files = lockable_files.LockableFiles(branch_transport,
2004
 
            'lock', lockdir.LockDir)
2005
 
        control_files.create_lock()
2006
 
        control_files.lock_write()
2007
 
        try:
2008
 
            utf8_files += [('format', self.get_format_string())]
2009
 
            for (filename, content) in utf8_files:
2010
 
                branch_transport.put_bytes(
2011
 
                    filename, content,
2012
 
                    mode=a_bzrdir._get_file_mode())
2013
 
        finally:
2014
 
            control_files.unlock()
2015
 
        branch = self.open(a_bzrdir, name, _found=True,
2016
 
                found_repository=repository)
2017
 
        self._run_post_branch_init_hooks(a_bzrdir, name, branch)
2018
 
        return branch
2019
 
 
2020
 
    def network_name(self):
2021
 
        """A simple byte string uniquely identifying this format for RPC calls.
2022
 
 
2023
 
        Metadir branch formats use their format string.
2024
 
        """
2025
 
        return self.get_format_string()
2026
 
 
2027
 
    def open(self, a_bzrdir, name=None, _found=False, ignore_fallbacks=False,
2028
 
            found_repository=None):
2029
 
        """See BranchFormat.open()."""
2030
 
        if not _found:
2031
 
            format = BranchFormat.find_format(a_bzrdir, name=name)
2032
 
            if format.__class__ != self.__class__:
2033
 
                raise AssertionError("wrong format %r found for %r" %
2034
 
                    (format, self))
2035
 
        transport = a_bzrdir.get_branch_transport(None, name=name)
2036
 
        try:
2037
 
            control_files = lockable_files.LockableFiles(transport, 'lock',
2038
 
                                                         lockdir.LockDir)
2039
 
            if found_repository is None:
2040
 
                found_repository = a_bzrdir.find_repository()
2041
 
            return self._branch_class()(_format=self,
2042
 
                              _control_files=control_files,
2043
 
                              name=name,
2044
 
                              a_bzrdir=a_bzrdir,
2045
 
                              _repository=found_repository,
2046
 
                              ignore_fallbacks=ignore_fallbacks)
2047
 
        except errors.NoSuchFile:
2048
 
            raise errors.NotBranchError(path=transport.base, bzrdir=a_bzrdir)
2049
 
 
2050
 
    def __init__(self):
2051
 
        super(BranchFormatMetadir, self).__init__()
2052
 
        self._matchingbzrdir = bzrdir.BzrDirMetaFormat1()
2053
 
        self._matchingbzrdir.set_branch_format(self)
2054
 
 
2055
 
    def supports_tags(self):
2056
 
        return True
2057
 
 
2058
 
    def supports_leaving_lock(self):
2059
 
        return True
2060
 
 
2061
 
 
2062
 
class BzrBranchFormat5(BranchFormatMetadir):
2063
 
    """Bzr branch format 5.
2064
 
 
2065
 
    This format has:
2066
 
     - a revision-history file.
2067
 
     - a format string
2068
 
     - a lock dir guarding the branch itself
2069
 
     - all of this stored in a branch/ subdirectory
2070
 
     - works with shared repositories.
2071
 
 
2072
 
    This format is new in bzr 0.8.
2073
 
    """
2074
 
 
2075
 
    def _branch_class(self):
2076
 
        return BzrBranch5
2077
 
 
2078
 
    def get_format_string(self):
2079
 
        """See BranchFormat.get_format_string()."""
2080
 
        return "Bazaar-NG branch format 5\n"
2081
 
 
2082
 
    def get_format_description(self):
2083
 
        """See BranchFormat.get_format_description()."""
2084
 
        return "Branch format 5"
2085
 
 
2086
 
    def initialize(self, a_bzrdir, name=None, repository=None):
2087
 
        """Create a branch of this format in a_bzrdir."""
2088
 
        utf8_files = [('revision-history', ''),
2089
 
                      ('branch-name', ''),
2090
 
                      ]
2091
 
        return self._initialize_helper(a_bzrdir, utf8_files, name, repository)
2092
 
 
2093
 
    def supports_tags(self):
2094
 
        return False
2095
 
 
2096
 
 
2097
 
class BzrBranchFormat6(BranchFormatMetadir):
2098
 
    """Branch format with last-revision and tags.
2099
 
 
2100
 
    Unlike previous formats, this has no explicit revision history. Instead,
2101
 
    this just stores the last-revision, and the left-hand history leading
2102
 
    up to there is the history.
2103
 
 
2104
 
    This format was introduced in bzr 0.15
2105
 
    and became the default in 0.91.
2106
 
    """
2107
 
 
2108
 
    def _branch_class(self):
2109
 
        return BzrBranch6
2110
 
 
2111
 
    def get_format_string(self):
2112
 
        """See BranchFormat.get_format_string()."""
2113
 
        return "Bazaar Branch Format 6 (bzr 0.15)\n"
2114
 
 
2115
 
    def get_format_description(self):
2116
 
        """See BranchFormat.get_format_description()."""
2117
 
        return "Branch format 6"
2118
 
 
2119
 
    def initialize(self, a_bzrdir, name=None, repository=None):
2120
 
        """Create a branch of this format in a_bzrdir."""
2121
 
        utf8_files = [('last-revision', '0 null:\n'),
2122
 
                      ('branch.conf', ''),
2123
 
                      ('tags', ''),
2124
 
                      ]
2125
 
        return self._initialize_helper(a_bzrdir, utf8_files, name, repository)
2126
 
 
2127
 
    def make_tags(self, branch):
2128
 
        """See bzrlib.branch.BranchFormat.make_tags()."""
2129
 
        return BasicTags(branch)
2130
 
 
2131
 
    def supports_set_append_revisions_only(self):
2132
 
        return True
2133
 
 
2134
 
 
2135
 
class BzrBranchFormat8(BranchFormatMetadir):
2136
 
    """Metadir format supporting storing locations of subtree branches."""
2137
 
 
2138
 
    def _branch_class(self):
2139
 
        return BzrBranch8
2140
 
 
2141
 
    def get_format_string(self):
2142
 
        """See BranchFormat.get_format_string()."""
2143
 
        return "Bazaar Branch Format 8 (needs bzr 1.15)\n"
2144
 
 
2145
 
    def get_format_description(self):
2146
 
        """See BranchFormat.get_format_description()."""
2147
 
        return "Branch format 8"
2148
 
 
2149
 
    def initialize(self, a_bzrdir, name=None, repository=None):
2150
 
        """Create a branch of this format in a_bzrdir."""
2151
 
        utf8_files = [('last-revision', '0 null:\n'),
2152
 
                      ('branch.conf', ''),
2153
 
                      ('tags', ''),
2154
 
                      ('references', '')
2155
 
                      ]
2156
 
        return self._initialize_helper(a_bzrdir, utf8_files, name, repository)
2157
 
 
2158
 
    def make_tags(self, branch):
2159
 
        """See bzrlib.branch.BranchFormat.make_tags()."""
2160
 
        return BasicTags(branch)
2161
 
 
2162
 
    def supports_set_append_revisions_only(self):
2163
 
        return True
2164
 
 
2165
 
    def supports_stacking(self):
2166
 
        return True
2167
 
 
2168
 
    supports_reference_locations = True
2169
 
 
2170
 
 
2171
 
class BzrBranchFormat7(BranchFormatMetadir):
2172
 
    """Branch format with last-revision, tags, and a stacked location pointer.
2173
 
 
2174
 
    The stacked location pointer is passed down to the repository and requires
2175
 
    a repository format with supports_external_lookups = True.
2176
 
 
2177
 
    This format was introduced in bzr 1.6.
2178
 
    """
2179
 
 
2180
 
    def initialize(self, a_bzrdir, name=None, repository=None):
2181
 
        """Create a branch of this format in a_bzrdir."""
2182
 
        utf8_files = [('last-revision', '0 null:\n'),
2183
 
                      ('branch.conf', ''),
2184
 
                      ('tags', ''),
2185
 
                      ]
2186
 
        return self._initialize_helper(a_bzrdir, utf8_files, name, repository)
2187
 
 
2188
 
    def _branch_class(self):
2189
 
        return BzrBranch7
2190
 
 
2191
 
    def get_format_string(self):
2192
 
        """See BranchFormat.get_format_string()."""
2193
 
        return "Bazaar Branch Format 7 (needs bzr 1.6)\n"
2194
 
 
2195
 
    def get_format_description(self):
2196
 
        """See BranchFormat.get_format_description()."""
2197
 
        return "Branch format 7"
2198
 
 
2199
 
    def supports_set_append_revisions_only(self):
2200
 
        return True
2201
 
 
2202
 
    def supports_stacking(self):
2203
 
        return True
2204
 
 
2205
 
    def make_tags(self, branch):
2206
 
        """See bzrlib.branch.BranchFormat.make_tags()."""
2207
 
        return BasicTags(branch)
2208
 
 
2209
 
    supports_reference_locations = False
2210
 
 
2211
 
 
2212
 
class BranchReferenceFormat(BranchFormat):
2213
 
    """Bzr branch reference format.
2214
 
 
2215
 
    Branch references are used in implementing checkouts, they
2216
 
    act as an alias to the real branch which is at some other url.
2217
 
 
2218
 
    This format has:
2219
 
     - A location file
2220
 
     - a format string
2221
 
    """
2222
 
 
2223
 
    def get_format_string(self):
2224
 
        """See BranchFormat.get_format_string()."""
2225
 
        return "Bazaar-NG Branch Reference Format 1\n"
2226
 
 
2227
 
    def get_format_description(self):
2228
 
        """See BranchFormat.get_format_description()."""
2229
 
        return "Checkout reference format 1"
2230
 
 
2231
 
    def get_reference(self, a_bzrdir, name=None):
2232
 
        """See BranchFormat.get_reference()."""
2233
 
        transport = a_bzrdir.get_branch_transport(None, name=name)
2234
 
        return transport.get_bytes('location')
2235
 
 
2236
 
    def set_reference(self, a_bzrdir, name, to_branch):
2237
 
        """See BranchFormat.set_reference()."""
2238
 
        transport = a_bzrdir.get_branch_transport(None, name=name)
2239
 
        location = transport.put_bytes('location', to_branch.base)
2240
 
 
2241
 
    def initialize(self, a_bzrdir, name=None, target_branch=None,
2242
 
            repository=None):
2243
 
        """Create a branch of this format in a_bzrdir."""
2244
 
        if target_branch is None:
2245
 
            # this format does not implement branch itself, thus the implicit
2246
 
            # creation contract must see it as uninitializable
2247
 
            raise errors.UninitializableFormat(self)
2248
 
        mutter('creating branch reference in %s', a_bzrdir.user_url)
2249
 
        branch_transport = a_bzrdir.get_branch_transport(self, name=name)
2250
 
        branch_transport.put_bytes('location',
2251
 
            target_branch.bzrdir.user_url)
2252
 
        branch_transport.put_bytes('format', self.get_format_string())
2253
 
        branch = self.open(
2254
 
            a_bzrdir, name, _found=True,
2255
 
            possible_transports=[target_branch.bzrdir.root_transport])
2256
 
        self._run_post_branch_init_hooks(a_bzrdir, name, branch)
2257
 
        return branch
2258
 
 
2259
 
    def __init__(self):
2260
 
        super(BranchReferenceFormat, self).__init__()
2261
 
        self._matchingbzrdir = bzrdir.BzrDirMetaFormat1()
2262
 
        self._matchingbzrdir.set_branch_format(self)
2263
 
 
2264
 
    def _make_reference_clone_function(format, a_branch):
2265
 
        """Create a clone() routine for a branch dynamically."""
2266
 
        def clone(to_bzrdir, revision_id=None,
2267
 
            repository_policy=None):
2268
 
            """See Branch.clone()."""
2269
 
            return format.initialize(to_bzrdir, target_branch=a_branch)
2270
 
            # cannot obey revision_id limits when cloning a reference ...
2271
 
            # FIXME RBC 20060210 either nuke revision_id for clone, or
2272
 
            # emit some sort of warning/error to the caller ?!
2273
 
        return clone
2274
 
 
2275
 
    def open(self, a_bzrdir, name=None, _found=False, location=None,
2276
 
             possible_transports=None, ignore_fallbacks=False,
2277
 
             found_repository=None):
2278
 
        """Return the branch that the branch reference in a_bzrdir points at.
2279
 
 
2280
 
        :param a_bzrdir: A BzrDir that contains a branch.
2281
 
        :param name: Name of colocated branch to open, if any
2282
 
        :param _found: a private parameter, do not use it. It is used to
2283
 
            indicate if format probing has already be done.
2284
 
        :param ignore_fallbacks: when set, no fallback branches will be opened
2285
 
            (if there are any).  Default is to open fallbacks.
2286
 
        :param location: The location of the referenced branch.  If
2287
 
            unspecified, this will be determined from the branch reference in
2288
 
            a_bzrdir.
2289
 
        :param possible_transports: An optional reusable transports list.
2290
 
        """
2291
 
        if not _found:
2292
 
            format = BranchFormat.find_format(a_bzrdir, name=name)
2293
 
            if format.__class__ != self.__class__:
2294
 
                raise AssertionError("wrong format %r found for %r" %
2295
 
                    (format, self))
2296
 
        if location is None:
2297
 
            location = self.get_reference(a_bzrdir, name)
2298
 
        real_bzrdir = bzrdir.BzrDir.open(
2299
 
            location, possible_transports=possible_transports)
2300
 
        result = real_bzrdir.open_branch(name=name, 
2301
 
            ignore_fallbacks=ignore_fallbacks)
2302
 
        # this changes the behaviour of result.clone to create a new reference
2303
 
        # rather than a copy of the content of the branch.
2304
 
        # I did not use a proxy object because that needs much more extensive
2305
 
        # testing, and we are only changing one behaviour at the moment.
2306
 
        # If we decide to alter more behaviours - i.e. the implicit nickname
2307
 
        # then this should be refactored to introduce a tested proxy branch
2308
 
        # and a subclass of that for use in overriding clone() and ....
2309
 
        # - RBC 20060210
2310
 
        result.clone = self._make_reference_clone_function(result)
2311
 
        return result
2312
 
 
2313
 
 
2314
 
class BranchFormatRegistry(controldir.ControlComponentFormatRegistry):
2315
 
    """Branch format registry."""
2316
 
 
2317
 
    def __init__(self, other_registry=None):
2318
 
        super(BranchFormatRegistry, self).__init__(other_registry)
2319
 
        self._default_format = None
2320
 
 
2321
 
    def set_default(self, format):
2322
 
        self._default_format = format
2323
 
 
2324
 
    def get_default(self):
2325
 
        return self._default_format
2326
 
 
2327
 
 
2328
 
network_format_registry = registry.FormatRegistry()
2329
 
"""Registry of formats indexed by their network name.
2330
 
 
2331
 
The network name for a branch format is an identifier that can be used when
2332
 
referring to formats with smart server operations. See
2333
 
BranchFormat.network_name() for more detail.
2334
 
"""
2335
 
 
2336
 
format_registry = BranchFormatRegistry(network_format_registry)
2337
 
 
2338
 
 
2339
 
# formats which have no format string are not discoverable
2340
 
# and not independently creatable, so are not registered.
2341
 
__format5 = BzrBranchFormat5()
2342
 
__format6 = BzrBranchFormat6()
2343
 
__format7 = BzrBranchFormat7()
2344
 
__format8 = BzrBranchFormat8()
2345
 
format_registry.register(__format5)
2346
 
format_registry.register(BranchReferenceFormat())
2347
 
format_registry.register(__format6)
2348
 
format_registry.register(__format7)
2349
 
format_registry.register(__format8)
2350
 
format_registry.set_default(__format7)
2351
 
 
2352
 
 
2353
 
class BranchWriteLockResult(LogicalLockResult):
2354
 
    """The result of write locking a branch.
2355
 
 
2356
 
    :ivar branch_token: The token obtained from the underlying branch lock, or
2357
 
        None.
2358
 
    :ivar unlock: A callable which will unlock the lock.
2359
 
    """
2360
 
 
2361
 
    def __init__(self, unlock, branch_token):
2362
 
        LogicalLockResult.__init__(self, unlock)
2363
 
        self.branch_token = branch_token
2364
 
 
2365
 
    def __repr__(self):
2366
 
        return "BranchWriteLockResult(%s, %s)" % (self.branch_token,
2367
 
            self.unlock)
2368
 
 
2369
 
 
2370
 
class BzrBranch(Branch, _RelockDebugMixin):
2371
 
    """A branch stored in the actual filesystem.
2372
 
 
2373
 
    Note that it's "local" in the context of the filesystem; it doesn't
2374
 
    really matter if it's on an nfs/smb/afs/coda/... share, as long as
2375
 
    it's writable, and can be accessed via the normal filesystem API.
2376
 
 
2377
 
    :ivar _transport: Transport for file operations on this branch's
2378
 
        control files, typically pointing to the .bzr/branch directory.
2379
 
    :ivar repository: Repository for this branch.
2380
 
    :ivar base: The url of the base directory for this branch; the one
2381
 
        containing the .bzr directory.
2382
 
    :ivar name: Optional colocated branch name as it exists in the control
2383
 
        directory.
2384
 
    """
2385
 
 
2386
 
    def __init__(self, _format=None,
2387
 
                 _control_files=None, a_bzrdir=None, name=None,
2388
 
                 _repository=None, ignore_fallbacks=False):
2389
 
        """Create new branch object at a particular location."""
2390
 
        if a_bzrdir is None:
2391
 
            raise ValueError('a_bzrdir must be supplied')
2392
 
        else:
2393
 
            self.bzrdir = a_bzrdir
2394
 
        self._base = self.bzrdir.transport.clone('..').base
2395
 
        self.name = name
2396
 
        # XXX: We should be able to just do
2397
 
        #   self.base = self.bzrdir.root_transport.base
2398
 
        # but this does not quite work yet -- mbp 20080522
2399
 
        self._format = _format
2400
 
        if _control_files is None:
2401
 
            raise ValueError('BzrBranch _control_files is None')
2402
 
        self.control_files = _control_files
2403
 
        self._transport = _control_files._transport
2404
 
        self.repository = _repository
2405
 
        Branch.__init__(self)
2406
 
 
2407
 
    def __str__(self):
2408
 
        if self.name is None:
2409
 
            return '%s(%s)' % (self.__class__.__name__, self.user_url)
2410
 
        else:
2411
 
            return '%s(%s,%s)' % (self.__class__.__name__, self.user_url,
2412
 
                self.name)
2413
 
 
2414
 
    __repr__ = __str__
2415
 
 
2416
 
    def _get_base(self):
2417
 
        """Returns the directory containing the control directory."""
2418
 
        return self._base
2419
 
 
2420
 
    base = property(_get_base, doc="The URL for the root of this branch.")
2421
 
 
2422
 
    def _get_config(self):
2423
 
        return TransportConfig(self._transport, 'branch.conf')
2424
 
 
2425
 
    def is_locked(self):
2426
 
        return self.control_files.is_locked()
2427
 
 
2428
 
    def lock_write(self, token=None):
2429
 
        """Lock the branch for write operations.
2430
 
 
2431
 
        :param token: A token to permit reacquiring a previously held and
2432
 
            preserved lock.
2433
 
        :return: A BranchWriteLockResult.
2434
 
        """
2435
 
        if not self.is_locked():
2436
 
            self._note_lock('w')
2437
 
        # All-in-one needs to always unlock/lock.
2438
 
        repo_control = getattr(self.repository, 'control_files', None)
2439
 
        if self.control_files == repo_control or not self.is_locked():
2440
 
            self.repository._warn_if_deprecated(self)
2441
 
            self.repository.lock_write()
2442
 
            took_lock = True
2443
 
        else:
2444
 
            took_lock = False
2445
 
        try:
2446
 
            return BranchWriteLockResult(self.unlock,
2447
 
                self.control_files.lock_write(token=token))
2448
 
        except:
2449
 
            if took_lock:
2450
 
                self.repository.unlock()
2451
 
            raise
2452
 
 
2453
 
    def lock_read(self):
2454
 
        """Lock the branch for read operations.
2455
 
 
2456
 
        :return: A bzrlib.lock.LogicalLockResult.
2457
 
        """
2458
 
        if not self.is_locked():
2459
 
            self._note_lock('r')
2460
 
        # All-in-one needs to always unlock/lock.
2461
 
        repo_control = getattr(self.repository, 'control_files', None)
2462
 
        if self.control_files == repo_control or not self.is_locked():
2463
 
            self.repository._warn_if_deprecated(self)
2464
 
            self.repository.lock_read()
2465
 
            took_lock = True
2466
 
        else:
2467
 
            took_lock = False
2468
 
        try:
2469
 
            self.control_files.lock_read()
2470
 
            return LogicalLockResult(self.unlock)
2471
 
        except:
2472
 
            if took_lock:
2473
 
                self.repository.unlock()
2474
 
            raise
2475
 
 
2476
 
    @only_raises(errors.LockNotHeld, errors.LockBroken)
2477
 
    def unlock(self):
2478
 
        try:
2479
 
            self.control_files.unlock()
2480
 
        finally:
2481
 
            # All-in-one needs to always unlock/lock.
2482
 
            repo_control = getattr(self.repository, 'control_files', None)
2483
 
            if (self.control_files == repo_control or
2484
 
                not self.control_files.is_locked()):
2485
 
                self.repository.unlock()
2486
 
            if not self.control_files.is_locked():
2487
 
                # we just released the lock
2488
 
                self._clear_cached_state()
2489
 
 
2490
 
    def peek_lock_mode(self):
2491
 
        if self.control_files._lock_count == 0:
2492
 
            return None
2493
 
        else:
2494
 
            return self.control_files._lock_mode
2495
 
 
2496
 
    def get_physical_lock_status(self):
2497
 
        return self.control_files.get_physical_lock_status()
2498
 
 
2499
 
    @needs_read_lock
2500
 
    def print_file(self, file, revision_id):
2501
 
        """See Branch.print_file."""
2502
 
        return self.repository.print_file(file, revision_id)
2503
 
 
2504
 
    @needs_write_lock
2505
 
    def set_last_revision_info(self, revno, revision_id):
2506
 
        if not revision_id or not isinstance(revision_id, basestring):
2507
 
            raise errors.InvalidRevisionId(revision_id=revision_id, branch=self)
2508
 
        revision_id = _mod_revision.ensure_null(revision_id)
2509
 
        old_revno, old_revid = self.last_revision_info()
2510
 
        if self._get_append_revisions_only():
2511
 
            self._check_history_violation(revision_id)
2512
 
        self._run_pre_change_branch_tip_hooks(revno, revision_id)
2513
 
        self._write_last_revision_info(revno, revision_id)
2514
 
        self._clear_cached_state()
2515
 
        self._last_revision_info_cache = revno, revision_id
2516
 
        self._run_post_change_branch_tip_hooks(old_revno, old_revid)
2517
 
 
2518
 
    def basis_tree(self):
2519
 
        """See Branch.basis_tree."""
2520
 
        return self.repository.revision_tree(self.last_revision())
2521
 
 
2522
 
    def _get_parent_location(self):
2523
 
        _locs = ['parent', 'pull', 'x-pull']
2524
 
        for l in _locs:
2525
 
            try:
2526
 
                return self._transport.get_bytes(l).strip('\n')
2527
 
            except errors.NoSuchFile:
2528
 
                pass
2529
 
        return None
2530
 
 
2531
 
    def get_stacked_on_url(self):
2532
 
        raise errors.UnstackableBranchFormat(self._format, self.user_url)
2533
 
 
2534
 
    def set_push_location(self, location):
2535
 
        """See Branch.set_push_location."""
2536
 
        self.get_config().set_user_option(
2537
 
            'push_location', location,
2538
 
            store=_mod_config.STORE_LOCATION_NORECURSE)
2539
 
 
2540
 
    def _set_parent_location(self, url):
2541
 
        if url is None:
2542
 
            self._transport.delete('parent')
2543
 
        else:
2544
 
            self._transport.put_bytes('parent', url + '\n',
2545
 
                mode=self.bzrdir._get_file_mode())
2546
 
 
2547
 
    @needs_write_lock
2548
 
    def unbind(self):
2549
 
        """If bound, unbind"""
2550
 
        return self.set_bound_location(None)
2551
 
 
2552
 
    @needs_write_lock
2553
 
    def bind(self, other):
2554
 
        """Bind this branch to the branch other.
2555
 
 
2556
 
        This does not push or pull data between the branches, though it does
2557
 
        check for divergence to raise an error when the branches are not
2558
 
        either the same, or one a prefix of the other. That behaviour may not
2559
 
        be useful, so that check may be removed in future.
2560
 
 
2561
 
        :param other: The branch to bind to
2562
 
        :type other: Branch
2563
 
        """
2564
 
        # TODO: jam 20051230 Consider checking if the target is bound
2565
 
        #       It is debatable whether you should be able to bind to
2566
 
        #       a branch which is itself bound.
2567
 
        #       Committing is obviously forbidden,
2568
 
        #       but binding itself may not be.
2569
 
        #       Since we *have* to check at commit time, we don't
2570
 
        #       *need* to check here
2571
 
 
2572
 
        # we want to raise diverged if:
2573
 
        # last_rev is not in the other_last_rev history, AND
2574
 
        # other_last_rev is not in our history, and do it without pulling
2575
 
        # history around
2576
 
        self.set_bound_location(other.base)
2577
 
 
2578
 
    def get_bound_location(self):
2579
 
        try:
2580
 
            return self._transport.get_bytes('bound')[:-1]
2581
 
        except errors.NoSuchFile:
2582
 
            return None
2583
 
 
2584
 
    @needs_read_lock
2585
 
    def get_master_branch(self, possible_transports=None):
2586
 
        """Return the branch we are bound to.
2587
 
 
2588
 
        :return: Either a Branch, or None
2589
 
        """
2590
 
        if self._master_branch_cache is None:
2591
 
            self._master_branch_cache = self._get_master_branch(
2592
 
                possible_transports)
2593
 
        return self._master_branch_cache
2594
 
 
2595
 
    def _get_master_branch(self, possible_transports):
2596
 
        bound_loc = self.get_bound_location()
2597
 
        if not bound_loc:
2598
 
            return None
2599
 
        try:
2600
 
            return Branch.open(bound_loc,
2601
 
                               possible_transports=possible_transports)
2602
 
        except (errors.NotBranchError, errors.ConnectionError), e:
2603
 
            raise errors.BoundBranchConnectionFailure(
2604
 
                    self, bound_loc, e)
2605
 
 
2606
 
    @needs_write_lock
2607
 
    def set_bound_location(self, location):
2608
 
        """Set the target where this branch is bound to.
2609
 
 
2610
 
        :param location: URL to the target branch
2611
 
        """
2612
 
        self._master_branch_cache = None
2613
 
        if location:
2614
 
            self._transport.put_bytes('bound', location+'\n',
2615
 
                mode=self.bzrdir._get_file_mode())
2616
 
        else:
2617
 
            try:
2618
 
                self._transport.delete('bound')
2619
 
            except errors.NoSuchFile:
2620
 
                return False
2621
 
            return True
2622
 
 
2623
 
    @needs_write_lock
2624
 
    def update(self, possible_transports=None):
2625
 
        """Synchronise this branch with the master branch if any.
2626
 
 
2627
 
        :return: None or the last_revision that was pivoted out during the
2628
 
                 update.
2629
 
        """
2630
 
        master = self.get_master_branch(possible_transports)
2631
 
        if master is not None:
2632
 
            old_tip = _mod_revision.ensure_null(self.last_revision())
2633
 
            self.pull(master, overwrite=True)
2634
 
            if self.repository.get_graph().is_ancestor(old_tip,
2635
 
                _mod_revision.ensure_null(self.last_revision())):
2636
 
                return None
2637
 
            return old_tip
2638
 
        return None
2639
 
 
2640
 
    def _read_last_revision_info(self):
2641
 
        revision_string = self._transport.get_bytes('last-revision')
2642
 
        revno, revision_id = revision_string.rstrip('\n').split(' ', 1)
2643
 
        revision_id = cache_utf8.get_cached_utf8(revision_id)
2644
 
        revno = int(revno)
2645
 
        return revno, revision_id
2646
 
 
2647
 
    def _write_last_revision_info(self, revno, revision_id):
2648
 
        """Simply write out the revision id, with no checks.
2649
 
 
2650
 
        Use set_last_revision_info to perform this safely.
2651
 
 
2652
 
        Does not update the revision_history cache.
2653
 
        """
2654
 
        revision_id = _mod_revision.ensure_null(revision_id)
2655
 
        out_string = '%d %s\n' % (revno, revision_id)
2656
 
        self._transport.put_bytes('last-revision', out_string,
2657
 
            mode=self.bzrdir._get_file_mode())
2658
 
 
2659
 
 
2660
 
class FullHistoryBzrBranch(BzrBranch):
2661
 
    """Bzr branch which contains the full revision history."""
2662
 
 
2663
 
    @needs_write_lock
2664
 
    def set_last_revision_info(self, revno, revision_id):
2665
 
        if not revision_id or not isinstance(revision_id, basestring):
2666
 
            raise errors.InvalidRevisionId(revision_id=revision_id, branch=self)
2667
 
        revision_id = _mod_revision.ensure_null(revision_id)
2668
 
        # this old format stores the full history, but this api doesn't
2669
 
        # provide it, so we must generate, and might as well check it's
2670
 
        # correct
2671
 
        history = self._lefthand_history(revision_id)
2672
 
        if len(history) != revno:
2673
 
            raise AssertionError('%d != %d' % (len(history), revno))
2674
 
        self._set_revision_history(history)
2675
 
 
2676
 
    def _read_last_revision_info(self):
2677
 
        rh = self.revision_history()
2678
 
        revno = len(rh)
2679
 
        if revno:
2680
 
            return (revno, rh[-1])
2681
 
        else:
2682
 
            return (0, _mod_revision.NULL_REVISION)
2683
 
 
2684
 
    @deprecated_method(deprecated_in((2, 4, 0)))
2685
 
    @needs_write_lock
2686
 
    def set_revision_history(self, rev_history):
2687
 
        """See Branch.set_revision_history."""
2688
 
        self._set_revision_history(rev_history)
2689
 
 
2690
 
    def _set_revision_history(self, rev_history):
2691
 
        if 'evil' in debug.debug_flags:
2692
 
            mutter_callsite(3, "set_revision_history scales with history.")
2693
 
        check_not_reserved_id = _mod_revision.check_not_reserved_id
2694
 
        for rev_id in rev_history:
2695
 
            check_not_reserved_id(rev_id)
2696
 
        if Branch.hooks['post_change_branch_tip']:
2697
 
            # Don't calculate the last_revision_info() if there are no hooks
2698
 
            # that will use it.
2699
 
            old_revno, old_revid = self.last_revision_info()
2700
 
        if len(rev_history) == 0:
2701
 
            revid = _mod_revision.NULL_REVISION
2702
 
        else:
2703
 
            revid = rev_history[-1]
2704
 
        self._run_pre_change_branch_tip_hooks(len(rev_history), revid)
2705
 
        self._write_revision_history(rev_history)
2706
 
        self._clear_cached_state()
2707
 
        self._cache_revision_history(rev_history)
2708
 
        for hook in Branch.hooks['set_rh']:
2709
 
            hook(self, rev_history)
2710
 
        if Branch.hooks['post_change_branch_tip']:
2711
 
            self._run_post_change_branch_tip_hooks(old_revno, old_revid)
2712
 
 
2713
 
    def _write_revision_history(self, history):
2714
 
        """Factored out of set_revision_history.
2715
 
 
2716
 
        This performs the actual writing to disk.
2717
 
        It is intended to be called by set_revision_history."""
2718
 
        self._transport.put_bytes(
2719
 
            'revision-history', '\n'.join(history),
2720
 
            mode=self.bzrdir._get_file_mode())
2721
 
 
2722
 
    def _gen_revision_history(self):
2723
 
        history = self._transport.get_bytes('revision-history').split('\n')
2724
 
        if history[-1:] == ['']:
2725
 
            # There shouldn't be a trailing newline, but just in case.
2726
 
            history.pop()
2727
 
        return history
2728
 
 
2729
 
    def _synchronize_history(self, destination, revision_id):
2730
 
        if not isinstance(destination, FullHistoryBzrBranch):
2731
 
            super(BzrBranch, self)._synchronize_history(
2732
 
                destination, revision_id)
2733
 
            return
2734
 
        if revision_id == _mod_revision.NULL_REVISION:
2735
 
            new_history = []
2736
 
        else:
2737
 
            new_history = self.revision_history()
2738
 
        if revision_id is not None and new_history != []:
2739
 
            try:
2740
 
                new_history = new_history[:new_history.index(revision_id) + 1]
2741
 
            except ValueError:
2742
 
                rev = self.repository.get_revision(revision_id)
2743
 
                new_history = rev.get_history(self.repository)[1:]
2744
 
        destination._set_revision_history(new_history)
2745
 
 
2746
 
    @needs_write_lock
2747
 
    def generate_revision_history(self, revision_id, last_rev=None,
2748
 
        other_branch=None):
2749
 
        """Create a new revision history that will finish with revision_id.
2750
 
 
2751
 
        :param revision_id: the new tip to use.
2752
 
        :param last_rev: The previous last_revision. If not None, then this
2753
 
            must be a ancestory of revision_id, or DivergedBranches is raised.
2754
 
        :param other_branch: The other branch that DivergedBranches should
2755
 
            raise with respect to.
2756
 
        """
2757
 
        self._set_revision_history(self._lefthand_history(revision_id,
2758
 
            last_rev, other_branch))
2759
 
 
2760
 
 
2761
 
class BzrBranch5(FullHistoryBzrBranch):
2762
 
    """A format 5 branch. This supports new features over plain branches.
2763
 
 
2764
 
    It has support for a master_branch which is the data for bound branches.
2765
 
    """
2766
 
 
2767
 
 
2768
 
class BzrBranch8(BzrBranch):
2769
 
    """A branch that stores tree-reference locations."""
2770
 
 
2771
 
    def _open_hook(self):
2772
 
        if self._ignore_fallbacks:
2773
 
            return
2774
 
        try:
2775
 
            url = self.get_stacked_on_url()
2776
 
        except (errors.UnstackableRepositoryFormat, errors.NotStacked,
2777
 
            errors.UnstackableBranchFormat):
2778
 
            pass
2779
 
        else:
2780
 
            for hook in Branch.hooks['transform_fallback_location']:
2781
 
                url = hook(self, url)
2782
 
                if url is None:
2783
 
                    hook_name = Branch.hooks.get_hook_name(hook)
2784
 
                    raise AssertionError(
2785
 
                        "'transform_fallback_location' hook %s returned "
2786
 
                        "None, not a URL." % hook_name)
2787
 
            self._activate_fallback_location(url)
2788
 
 
2789
 
    def __init__(self, *args, **kwargs):
2790
 
        self._ignore_fallbacks = kwargs.get('ignore_fallbacks', False)
2791
 
        super(BzrBranch8, self).__init__(*args, **kwargs)
2792
 
        self._last_revision_info_cache = None
2793
 
        self._reference_info = None
2794
 
 
2795
 
    def _clear_cached_state(self):
2796
 
        super(BzrBranch8, self)._clear_cached_state()
2797
 
        self._last_revision_info_cache = None
2798
 
        self._reference_info = None
2799
 
 
2800
 
    def _check_history_violation(self, revision_id):
2801
 
        current_revid = self.last_revision()
2802
 
        last_revision = _mod_revision.ensure_null(current_revid)
2803
 
        if _mod_revision.is_null(last_revision):
2804
 
            return
2805
 
        graph = self.repository.get_graph()
2806
 
        for lh_ancestor in graph.iter_lefthand_ancestry(revision_id):
2807
 
            if lh_ancestor == current_revid:
2808
 
                return
2809
 
        raise errors.AppendRevisionsOnlyViolation(self.user_url)
2810
 
 
2811
 
    def _gen_revision_history(self):
2812
 
        """Generate the revision history from last revision
2813
 
        """
2814
 
        last_revno, last_revision = self.last_revision_info()
2815
 
        self._extend_partial_history(stop_index=last_revno-1)
2816
 
        return list(reversed(self._partial_revision_history_cache))
2817
 
 
2818
 
    @needs_write_lock
2819
 
    def _set_parent_location(self, url):
2820
 
        """Set the parent branch"""
2821
 
        self._set_config_location('parent_location', url, make_relative=True)
2822
 
 
2823
 
    @needs_read_lock
2824
 
    def _get_parent_location(self):
2825
 
        """Set the parent branch"""
2826
 
        return self._get_config_location('parent_location')
2827
 
 
2828
 
    @needs_write_lock
2829
 
    def _set_all_reference_info(self, info_dict):
2830
 
        """Replace all reference info stored in a branch.
2831
 
 
2832
 
        :param info_dict: A dict of {file_id: (tree_path, branch_location)}
2833
 
        """
2834
 
        s = StringIO()
2835
 
        writer = rio.RioWriter(s)
2836
 
        for key, (tree_path, branch_location) in info_dict.iteritems():
2837
 
            stanza = rio.Stanza(file_id=key, tree_path=tree_path,
2838
 
                                branch_location=branch_location)
2839
 
            writer.write_stanza(stanza)
2840
 
        self._transport.put_bytes('references', s.getvalue())
2841
 
        self._reference_info = info_dict
2842
 
 
2843
 
    @needs_read_lock
2844
 
    def _get_all_reference_info(self):
2845
 
        """Return all the reference info stored in a branch.
2846
 
 
2847
 
        :return: A dict of {file_id: (tree_path, branch_location)}
2848
 
        """
2849
 
        if self._reference_info is not None:
2850
 
            return self._reference_info
2851
 
        rio_file = self._transport.get('references')
2852
 
        try:
2853
 
            stanzas = rio.read_stanzas(rio_file)
2854
 
            info_dict = dict((s['file_id'], (s['tree_path'],
2855
 
                             s['branch_location'])) for s in stanzas)
2856
 
        finally:
2857
 
            rio_file.close()
2858
 
        self._reference_info = info_dict
2859
 
        return info_dict
2860
 
 
2861
 
    def set_reference_info(self, file_id, tree_path, branch_location):
2862
 
        """Set the branch location to use for a tree reference.
2863
 
 
2864
 
        :param file_id: The file-id of the tree reference.
2865
 
        :param tree_path: The path of the tree reference in the tree.
2866
 
        :param branch_location: The location of the branch to retrieve tree
2867
 
            references from.
2868
 
        """
2869
 
        info_dict = self._get_all_reference_info()
2870
 
        info_dict[file_id] = (tree_path, branch_location)
2871
 
        if None in (tree_path, branch_location):
2872
 
            if tree_path is not None:
2873
 
                raise ValueError('tree_path must be None when branch_location'
2874
 
                                 ' is None.')
2875
 
            if branch_location is not None:
2876
 
                raise ValueError('branch_location must be None when tree_path'
2877
 
                                 ' is None.')
2878
 
            del info_dict[file_id]
2879
 
        self._set_all_reference_info(info_dict)
2880
 
 
2881
 
    def get_reference_info(self, file_id):
2882
 
        """Get the tree_path and branch_location for a tree reference.
2883
 
 
2884
 
        :return: a tuple of (tree_path, branch_location)
2885
 
        """
2886
 
        return self._get_all_reference_info().get(file_id, (None, None))
2887
 
 
2888
 
    def reference_parent(self, file_id, path, possible_transports=None):
2889
 
        """Return the parent branch for a tree-reference file_id.
2890
 
 
2891
 
        :param file_id: The file_id of the tree reference
2892
 
        :param path: The path of the file_id in the tree
2893
 
        :return: A branch associated with the file_id
2894
 
        """
2895
 
        branch_location = self.get_reference_info(file_id)[1]
2896
 
        if branch_location is None:
2897
 
            return Branch.reference_parent(self, file_id, path,
2898
 
                                           possible_transports)
2899
 
        branch_location = urlutils.join(self.user_url, branch_location)
2900
 
        return Branch.open(branch_location,
2901
 
                           possible_transports=possible_transports)
2902
 
 
2903
 
    def set_push_location(self, location):
2904
 
        """See Branch.set_push_location."""
2905
 
        self._set_config_location('push_location', location)
2906
 
 
2907
 
    def set_bound_location(self, location):
2908
 
        """See Branch.set_push_location."""
2909
 
        self._master_branch_cache = None
2910
 
        result = None
2911
 
        config = self.get_config()
2912
 
        if location is None:
2913
 
            if config.get_user_option('bound') != 'True':
2914
 
                return False
2915
 
            else:
2916
 
                config.set_user_option('bound', 'False', warn_masked=True)
2917
 
                return True
2918
 
        else:
2919
 
            self._set_config_location('bound_location', location,
2920
 
                                      config=config)
2921
 
            config.set_user_option('bound', 'True', warn_masked=True)
2922
 
        return True
2923
 
 
2924
 
    def _get_bound_location(self, bound):
2925
 
        """Return the bound location in the config file.
2926
 
 
2927
 
        Return None if the bound parameter does not match"""
2928
 
        config = self.get_config()
2929
 
        config_bound = (config.get_user_option('bound') == 'True')
2930
 
        if config_bound != bound:
2931
 
            return None
2932
 
        return self._get_config_location('bound_location', config=config)
2933
 
 
2934
 
    def get_bound_location(self):
2935
 
        """See Branch.set_push_location."""
2936
 
        return self._get_bound_location(True)
2937
 
 
2938
 
    def get_old_bound_location(self):
2939
 
        """See Branch.get_old_bound_location"""
2940
 
        return self._get_bound_location(False)
2941
 
 
2942
 
    def get_stacked_on_url(self):
2943
 
        # you can always ask for the URL; but you might not be able to use it
2944
 
        # if the repo can't support stacking.
2945
 
        ## self._check_stackable_repo()
2946
 
        # stacked_on_location is only ever defined in branch.conf, so don't
2947
 
        # waste effort reading the whole stack of config files.
2948
 
        config = self.get_config()._get_branch_data_config()
2949
 
        stacked_url = self._get_config_location('stacked_on_location',
2950
 
            config=config)
2951
 
        if stacked_url is None:
2952
 
            raise errors.NotStacked(self)
2953
 
        return stacked_url
2954
 
 
2955
 
    def _get_append_revisions_only(self):
2956
 
        return self.get_config(
2957
 
            ).get_user_option_as_bool('append_revisions_only')
2958
 
 
2959
 
    @needs_read_lock
2960
 
    def get_rev_id(self, revno, history=None):
2961
 
        """Find the revision id of the specified revno."""
2962
 
        if revno == 0:
2963
 
            return _mod_revision.NULL_REVISION
2964
 
 
2965
 
        last_revno, last_revision_id = self.last_revision_info()
2966
 
        if revno <= 0 or revno > last_revno:
2967
 
            raise errors.NoSuchRevision(self, revno)
2968
 
 
2969
 
        if history is not None:
2970
 
            return history[revno - 1]
2971
 
 
2972
 
        index = last_revno - revno
2973
 
        if len(self._partial_revision_history_cache) <= index:
2974
 
            self._extend_partial_history(stop_index=index)
2975
 
        if len(self._partial_revision_history_cache) > index:
2976
 
            return self._partial_revision_history_cache[index]
2977
 
        else:
2978
 
            raise errors.NoSuchRevision(self, revno)
2979
 
 
2980
 
    @needs_read_lock
2981
 
    def revision_id_to_revno(self, revision_id):
2982
 
        """Given a revision id, return its revno"""
2983
 
        if _mod_revision.is_null(revision_id):
2984
 
            return 0
2985
 
        try:
2986
 
            index = self._partial_revision_history_cache.index(revision_id)
2987
 
        except ValueError:
2988
 
            try:
2989
 
                self._extend_partial_history(stop_revision=revision_id)
2990
 
            except errors.RevisionNotPresent, e:
2991
 
                raise errors.GhostRevisionsHaveNoRevno(revision_id, e.revision_id)
2992
 
            index = len(self._partial_revision_history_cache) - 1
2993
 
            if self._partial_revision_history_cache[index] != revision_id:
2994
 
                raise errors.NoSuchRevision(self, revision_id)
2995
 
        return self.revno() - index
2996
 
 
2997
 
 
2998
 
class BzrBranch7(BzrBranch8):
2999
 
    """A branch with support for a fallback repository."""
3000
 
 
3001
 
    def set_reference_info(self, file_id, tree_path, branch_location):
3002
 
        Branch.set_reference_info(self, file_id, tree_path, branch_location)
3003
 
 
3004
 
    def get_reference_info(self, file_id):
3005
 
        Branch.get_reference_info(self, file_id)
3006
 
 
3007
 
    def reference_parent(self, file_id, path, possible_transports=None):
3008
 
        return Branch.reference_parent(self, file_id, path,
3009
 
                                       possible_transports)
3010
 
 
3011
 
 
3012
 
class BzrBranch6(BzrBranch7):
3013
 
    """See BzrBranchFormat6 for the capabilities of this branch.
3014
 
 
3015
 
    This subclass of BzrBranch7 disables the new features BzrBranch7 added,
3016
 
    i.e. stacking.
3017
 
    """
3018
 
 
3019
 
    def get_stacked_on_url(self):
3020
 
        raise errors.UnstackableBranchFormat(self._format, self.user_url)
3021
 
 
3022
1345
 
3023
1346
######################################################################
3024
 
# results of operations
3025
 
 
3026
 
 
3027
 
class _Result(object):
3028
 
 
3029
 
    def _show_tag_conficts(self, to_file):
3030
 
        if not getattr(self, 'tag_conflicts', None):
3031
 
            return
3032
 
        to_file.write('Conflicting tags:\n')
3033
 
        for name, value1, value2 in self.tag_conflicts:
3034
 
            to_file.write('    %s\n' % (name, ))
3035
 
 
3036
 
 
3037
 
class PullResult(_Result):
3038
 
    """Result of a Branch.pull operation.
3039
 
 
3040
 
    :ivar old_revno: Revision number before pull.
3041
 
    :ivar new_revno: Revision number after pull.
3042
 
    :ivar old_revid: Tip revision id before pull.
3043
 
    :ivar new_revid: Tip revision id after pull.
3044
 
    :ivar source_branch: Source (local) branch object. (read locked)
3045
 
    :ivar master_branch: Master branch of the target, or the target if no
3046
 
        Master
3047
 
    :ivar local_branch: target branch if there is a Master, else None
3048
 
    :ivar target_branch: Target/destination branch object. (write locked)
3049
 
    :ivar tag_conflicts: A list of tag conflicts, see BasicTags.merge_to
3050
 
    """
3051
 
 
3052
 
    @deprecated_method(deprecated_in((2, 3, 0)))
3053
 
    def __int__(self):
3054
 
        """Return the relative change in revno.
3055
 
 
3056
 
        :deprecated: Use `new_revno` and `old_revno` instead.
3057
 
        """
3058
 
        return self.new_revno - self.old_revno
3059
 
 
3060
 
    def report(self, to_file):
3061
 
        if not is_quiet():
3062
 
            if self.old_revid == self.new_revid:
3063
 
                to_file.write('No revisions to pull.\n')
3064
 
            else:
3065
 
                to_file.write('Now on revision %d.\n' % self.new_revno)
3066
 
        self._show_tag_conficts(to_file)
3067
 
 
3068
 
 
3069
 
class BranchPushResult(_Result):
3070
 
    """Result of a Branch.push operation.
3071
 
 
3072
 
    :ivar old_revno: Revision number (eg 10) of the target before push.
3073
 
    :ivar new_revno: Revision number (eg 12) of the target after push.
3074
 
    :ivar old_revid: Tip revision id (eg joe@foo.com-1234234-aoeua34) of target
3075
 
        before the push.
3076
 
    :ivar new_revid: Tip revision id (eg joe@foo.com-5676566-boa234a) of target
3077
 
        after the push.
3078
 
    :ivar source_branch: Source branch object that the push was from. This is
3079
 
        read locked, and generally is a local (and thus low latency) branch.
3080
 
    :ivar master_branch: If target is a bound branch, the master branch of
3081
 
        target, or target itself. Always write locked.
3082
 
    :ivar target_branch: The direct Branch where data is being sent (write
3083
 
        locked).
3084
 
    :ivar local_branch: If the target is a bound branch this will be the
3085
 
        target, otherwise it will be None.
3086
 
    """
3087
 
 
3088
 
    @deprecated_method(deprecated_in((2, 3, 0)))
3089
 
    def __int__(self):
3090
 
        """Return the relative change in revno.
3091
 
 
3092
 
        :deprecated: Use `new_revno` and `old_revno` instead.
3093
 
        """
3094
 
        return self.new_revno - self.old_revno
3095
 
 
3096
 
    def report(self, to_file):
3097
 
        """Write a human-readable description of the result."""
3098
 
        if self.old_revid == self.new_revid:
3099
 
            note('No new revisions to push.')
3100
 
        else:
3101
 
            note('Pushed up to revision %d.' % self.new_revno)
3102
 
        self._show_tag_conficts(to_file)
3103
 
 
3104
 
 
3105
 
class BranchCheckResult(object):
3106
 
    """Results of checking branch consistency.
3107
 
 
3108
 
    :see: Branch.check
3109
 
    """
3110
 
 
3111
 
    def __init__(self, branch):
3112
 
        self.branch = branch
3113
 
        self.errors = []
3114
 
 
3115
 
    def report_results(self, verbose):
3116
 
        """Report the check results via trace.note.
3117
 
 
3118
 
        :param verbose: Requests more detailed display of what was checked,
3119
 
            if any.
3120
 
        """
3121
 
        note('checked branch %s format %s', self.branch.user_url,
3122
 
            self.branch._format)
3123
 
        for error in self.errors:
3124
 
            note('found error:%s', error)
3125
 
 
3126
 
 
3127
 
class Converter5to6(object):
3128
 
    """Perform an in-place upgrade of format 5 to format 6"""
3129
 
 
3130
 
    def convert(self, branch):
3131
 
        # Data for 5 and 6 can peacefully coexist.
3132
 
        format = BzrBranchFormat6()
3133
 
        new_branch = format.open(branch.bzrdir, _found=True)
3134
 
 
3135
 
        # Copy source data into target
3136
 
        new_branch._write_last_revision_info(*branch.last_revision_info())
3137
 
        new_branch.set_parent(branch.get_parent())
3138
 
        new_branch.set_bound_location(branch.get_bound_location())
3139
 
        new_branch.set_push_location(branch.get_push_location())
3140
 
 
3141
 
        # New branch has no tags by default
3142
 
        new_branch.tags._set_tag_dict({})
3143
 
 
3144
 
        # Copying done; now update target format
3145
 
        new_branch._transport.put_bytes('format',
3146
 
            format.get_format_string(),
3147
 
            mode=new_branch.bzrdir._get_file_mode())
3148
 
 
3149
 
        # Clean up old files
3150
 
        new_branch._transport.delete('revision-history')
3151
 
        try:
3152
 
            branch.set_parent(None)
3153
 
        except errors.NoSuchFile:
3154
 
            pass
3155
 
        branch.set_bound_location(None)
3156
 
 
3157
 
 
3158
 
class Converter6to7(object):
3159
 
    """Perform an in-place upgrade of format 6 to format 7"""
3160
 
 
3161
 
    def convert(self, branch):
3162
 
        format = BzrBranchFormat7()
3163
 
        branch._set_config_location('stacked_on_location', '')
3164
 
        # update target format
3165
 
        branch._transport.put_bytes('format', format.get_format_string())
3166
 
 
3167
 
 
3168
 
class Converter7to8(object):
3169
 
    """Perform an in-place upgrade of format 7 to format 8"""
3170
 
 
3171
 
    def convert(self, branch):
3172
 
        format = BzrBranchFormat8()
3173
 
        branch._transport.put_bytes('references', '')
3174
 
        # update target format
3175
 
        branch._transport.put_bytes('format', format.get_format_string())
3176
 
 
3177
 
 
3178
 
class InterBranch(InterObject):
3179
 
    """This class represents operations taking place between two branches.
3180
 
 
3181
 
    Its instances have methods like pull() and push() and contain
3182
 
    references to the source and target repositories these operations
3183
 
    can be carried out on.
3184
 
    """
3185
 
 
3186
 
    _optimisers = []
3187
 
    """The available optimised InterBranch types."""
3188
 
 
3189
 
    @classmethod
3190
 
    def _get_branch_formats_to_test(klass):
3191
 
        """Return an iterable of format tuples for testing.
3192
 
        
3193
 
        :return: An iterable of (from_format, to_format) to use when testing
3194
 
            this InterBranch class. Each InterBranch class should define this
3195
 
            method itself.
3196
 
        """
3197
 
        raise NotImplementedError(klass._get_branch_formats_to_test)
3198
 
 
3199
 
    @needs_write_lock
3200
 
    def pull(self, overwrite=False, stop_revision=None,
3201
 
             possible_transports=None, local=False):
3202
 
        """Mirror source into target branch.
3203
 
 
3204
 
        The target branch is considered to be 'local', having low latency.
3205
 
 
3206
 
        :returns: PullResult instance
3207
 
        """
3208
 
        raise NotImplementedError(self.pull)
3209
 
 
3210
 
    @needs_write_lock
3211
 
    def push(self, overwrite=False, stop_revision=None, lossy=False,
3212
 
             _override_hook_source_branch=None):
3213
 
        """Mirror the source branch into the target branch.
3214
 
 
3215
 
        The source branch is considered to be 'local', having low latency.
3216
 
        """
3217
 
        raise NotImplementedError(self.push)
3218
 
 
3219
 
    @needs_write_lock
3220
 
    def copy_content_into(self, revision_id=None):
3221
 
        """Copy the content of source into target
3222
 
 
3223
 
        revision_id: if not None, the revision history in the new branch will
3224
 
                     be truncated to end with revision_id.
3225
 
        """
3226
 
        raise NotImplementedError(self.copy_content_into)
3227
 
 
3228
 
    @needs_write_lock
3229
 
    def fetch(self, stop_revision=None, limit=None):
3230
 
        """Fetch revisions.
3231
 
 
3232
 
        :param stop_revision: Last revision to fetch
3233
 
        :param limit: Optional rough limit of revisions to fetch
3234
 
        """
3235
 
        raise NotImplementedError(self.fetch)
3236
 
 
3237
 
 
3238
 
class GenericInterBranch(InterBranch):
3239
 
    """InterBranch implementation that uses public Branch functions."""
3240
 
 
3241
 
    @classmethod
3242
 
    def is_compatible(klass, source, target):
3243
 
        # GenericBranch uses the public API, so always compatible
3244
 
        return True
3245
 
 
3246
 
    @classmethod
3247
 
    def _get_branch_formats_to_test(klass):
3248
 
        return [(format_registry.get_default(), format_registry.get_default())]
3249
 
 
3250
 
    @classmethod
3251
 
    def unwrap_format(klass, format):
3252
 
        if isinstance(format, remote.RemoteBranchFormat):
3253
 
            format._ensure_real()
3254
 
            return format._custom_format
3255
 
        return format
3256
 
 
3257
 
    @needs_write_lock
3258
 
    def copy_content_into(self, revision_id=None):
3259
 
        """Copy the content of source into target
3260
 
 
3261
 
        revision_id: if not None, the revision history in the new branch will
3262
 
                     be truncated to end with revision_id.
3263
 
        """
3264
 
        self.source.update_references(self.target)
3265
 
        self.source._synchronize_history(self.target, revision_id)
3266
 
        try:
3267
 
            parent = self.source.get_parent()
3268
 
        except errors.InaccessibleParent, e:
3269
 
            mutter('parent was not accessible to copy: %s', e)
3270
 
        else:
3271
 
            if parent:
3272
 
                self.target.set_parent(parent)
3273
 
        if self.source._push_should_merge_tags():
3274
 
            self.source.tags.merge_to(self.target.tags)
3275
 
 
3276
 
    @needs_write_lock
3277
 
    def fetch(self, stop_revision=None, limit=None):
3278
 
        if self.target.base == self.source.base:
3279
 
            return (0, [])
3280
 
        self.source.lock_read()
3281
 
        try:
3282
 
            fetch_spec_factory = fetch.FetchSpecFactory()
3283
 
            fetch_spec_factory.source_branch = self.source
3284
 
            fetch_spec_factory.source_branch_stop_revision_id = stop_revision
3285
 
            fetch_spec_factory.source_repo = self.source.repository
3286
 
            fetch_spec_factory.target_repo = self.target.repository
3287
 
            fetch_spec_factory.target_repo_kind = fetch.TargetRepoKinds.PREEXISTING
3288
 
            fetch_spec_factory.limit = limit
3289
 
            fetch_spec = fetch_spec_factory.make_fetch_spec()
3290
 
            return self.target.repository.fetch(self.source.repository,
3291
 
                fetch_spec=fetch_spec)
3292
 
        finally:
3293
 
            self.source.unlock()
3294
 
 
3295
 
    @needs_write_lock
3296
 
    def _update_revisions(self, stop_revision=None, overwrite=False,
3297
 
            graph=None):
3298
 
        other_revno, other_last_revision = self.source.last_revision_info()
3299
 
        stop_revno = None # unknown
3300
 
        if stop_revision is None:
3301
 
            stop_revision = other_last_revision
3302
 
            if _mod_revision.is_null(stop_revision):
3303
 
                # if there are no commits, we're done.
3304
 
                return
3305
 
            stop_revno = other_revno
3306
 
 
3307
 
        # what's the current last revision, before we fetch [and change it
3308
 
        # possibly]
3309
 
        last_rev = _mod_revision.ensure_null(self.target.last_revision())
3310
 
        # we fetch here so that we don't process data twice in the common
3311
 
        # case of having something to pull, and so that the check for
3312
 
        # already merged can operate on the just fetched graph, which will
3313
 
        # be cached in memory.
3314
 
        self.fetch(stop_revision=stop_revision)
3315
 
        # Check to see if one is an ancestor of the other
3316
 
        if not overwrite:
3317
 
            if graph is None:
3318
 
                graph = self.target.repository.get_graph()
3319
 
            if self.target._check_if_descendant_or_diverged(
3320
 
                    stop_revision, last_rev, graph, self.source):
3321
 
                # stop_revision is a descendant of last_rev, but we aren't
3322
 
                # overwriting, so we're done.
3323
 
                return
3324
 
        if stop_revno is None:
3325
 
            if graph is None:
3326
 
                graph = self.target.repository.get_graph()
3327
 
            this_revno, this_last_revision = \
3328
 
                    self.target.last_revision_info()
3329
 
            stop_revno = graph.find_distance_to_null(stop_revision,
3330
 
                            [(other_last_revision, other_revno),
3331
 
                             (this_last_revision, this_revno)])
3332
 
        self.target.set_last_revision_info(stop_revno, stop_revision)
3333
 
 
3334
 
    @needs_write_lock
3335
 
    def pull(self, overwrite=False, stop_revision=None,
3336
 
             possible_transports=None, run_hooks=True,
3337
 
             _override_hook_target=None, local=False):
3338
 
        """Pull from source into self, updating my master if any.
3339
 
 
3340
 
        :param run_hooks: Private parameter - if false, this branch
3341
 
            is being called because it's the master of the primary branch,
3342
 
            so it should not run its hooks.
3343
 
        """
3344
 
        bound_location = self.target.get_bound_location()
3345
 
        if local and not bound_location:
3346
 
            raise errors.LocalRequiresBoundBranch()
3347
 
        master_branch = None
3348
 
        source_is_master = False
3349
 
        if bound_location:
3350
 
            # bound_location comes from a config file, some care has to be
3351
 
            # taken to relate it to source.user_url
3352
 
            normalized = urlutils.normalize_url(bound_location)
3353
 
            try:
3354
 
                relpath = self.source.user_transport.relpath(normalized)
3355
 
                source_is_master = (relpath == '')
3356
 
            except (errors.PathNotChild, errors.InvalidURL):
3357
 
                source_is_master = False
3358
 
        if not local and bound_location and not source_is_master:
3359
 
            # not pulling from master, so we need to update master.
3360
 
            master_branch = self.target.get_master_branch(possible_transports)
3361
 
            master_branch.lock_write()
3362
 
        try:
3363
 
            if master_branch:
3364
 
                # pull from source into master.
3365
 
                master_branch.pull(self.source, overwrite, stop_revision,
3366
 
                    run_hooks=False)
3367
 
            return self._pull(overwrite,
3368
 
                stop_revision, _hook_master=master_branch,
3369
 
                run_hooks=run_hooks,
3370
 
                _override_hook_target=_override_hook_target,
3371
 
                merge_tags_to_master=not source_is_master)
3372
 
        finally:
3373
 
            if master_branch:
3374
 
                master_branch.unlock()
3375
 
 
3376
 
    def push(self, overwrite=False, stop_revision=None, lossy=False,
3377
 
             _override_hook_source_branch=None):
3378
 
        """See InterBranch.push.
3379
 
 
3380
 
        This is the basic concrete implementation of push()
3381
 
 
3382
 
        :param _override_hook_source_branch: If specified, run the hooks
3383
 
            passing this Branch as the source, rather than self.  This is for
3384
 
            use of RemoteBranch, where push is delegated to the underlying
3385
 
            vfs-based Branch.
3386
 
        """
3387
 
        if lossy:
3388
 
            raise errors.LossyPushToSameVCS(self.source, self.target)
3389
 
        # TODO: Public option to disable running hooks - should be trivial but
3390
 
        # needs tests.
3391
 
 
3392
 
        op = cleanup.OperationWithCleanups(self._push_with_bound_branches)
3393
 
        op.add_cleanup(self.source.lock_read().unlock)
3394
 
        op.add_cleanup(self.target.lock_write().unlock)
3395
 
        return op.run(overwrite, stop_revision,
3396
 
            _override_hook_source_branch=_override_hook_source_branch)
3397
 
 
3398
 
    def _basic_push(self, overwrite, stop_revision):
3399
 
        """Basic implementation of push without bound branches or hooks.
3400
 
 
3401
 
        Must be called with source read locked and target write locked.
3402
 
        """
3403
 
        result = BranchPushResult()
3404
 
        result.source_branch = self.source
3405
 
        result.target_branch = self.target
3406
 
        result.old_revno, result.old_revid = self.target.last_revision_info()
3407
 
        self.source.update_references(self.target)
3408
 
        if result.old_revid != stop_revision:
3409
 
            # We assume that during 'push' this repository is closer than
3410
 
            # the target.
3411
 
            graph = self.source.repository.get_graph(self.target.repository)
3412
 
            self._update_revisions(stop_revision, overwrite=overwrite,
3413
 
                    graph=graph)
3414
 
        if self.source._push_should_merge_tags():
3415
 
            result.tag_conflicts = self.source.tags.merge_to(self.target.tags,
3416
 
                overwrite)
3417
 
        result.new_revno, result.new_revid = self.target.last_revision_info()
3418
 
        return result
3419
 
 
3420
 
    def _push_with_bound_branches(self, operation, overwrite, stop_revision,
3421
 
            _override_hook_source_branch=None):
3422
 
        """Push from source into target, and into target's master if any.
3423
 
        """
3424
 
        def _run_hooks():
3425
 
            if _override_hook_source_branch:
3426
 
                result.source_branch = _override_hook_source_branch
3427
 
            for hook in Branch.hooks['post_push']:
3428
 
                hook(result)
3429
 
 
3430
 
        bound_location = self.target.get_bound_location()
3431
 
        if bound_location and self.target.base != bound_location:
3432
 
            # there is a master branch.
3433
 
            #
3434
 
            # XXX: Why the second check?  Is it even supported for a branch to
3435
 
            # be bound to itself? -- mbp 20070507
3436
 
            master_branch = self.target.get_master_branch()
3437
 
            master_branch.lock_write()
3438
 
            operation.add_cleanup(master_branch.unlock)
3439
 
            # push into the master from the source branch.
3440
 
            master_inter = InterBranch.get(self.source, master_branch)
3441
 
            master_inter._basic_push(overwrite, stop_revision)
3442
 
            # and push into the target branch from the source. Note that
3443
 
            # we push from the source branch again, because it's considered
3444
 
            # the highest bandwidth repository.
3445
 
            result = self._basic_push(overwrite, stop_revision)
3446
 
            result.master_branch = master_branch
3447
 
            result.local_branch = self.target
3448
 
        else:
3449
 
            master_branch = None
3450
 
            # no master branch
3451
 
            result = self._basic_push(overwrite, stop_revision)
3452
 
            # TODO: Why set master_branch and local_branch if there's no
3453
 
            # binding?  Maybe cleaner to just leave them unset? -- mbp
3454
 
            # 20070504
3455
 
            result.master_branch = self.target
3456
 
            result.local_branch = None
3457
 
        _run_hooks()
3458
 
        return result
3459
 
 
3460
 
    def _pull(self, overwrite=False, stop_revision=None,
3461
 
             possible_transports=None, _hook_master=None, run_hooks=True,
3462
 
             _override_hook_target=None, local=False,
3463
 
             merge_tags_to_master=True):
3464
 
        """See Branch.pull.
3465
 
 
3466
 
        This function is the core worker, used by GenericInterBranch.pull to
3467
 
        avoid duplication when pulling source->master and source->local.
3468
 
 
3469
 
        :param _hook_master: Private parameter - set the branch to
3470
 
            be supplied as the master to pull hooks.
3471
 
        :param run_hooks: Private parameter - if false, this branch
3472
 
            is being called because it's the master of the primary branch,
3473
 
            so it should not run its hooks.
3474
 
            is being called because it's the master of the primary branch,
3475
 
            so it should not run its hooks.
3476
 
        :param _override_hook_target: Private parameter - set the branch to be
3477
 
            supplied as the target_branch to pull hooks.
3478
 
        :param local: Only update the local branch, and not the bound branch.
3479
 
        """
3480
 
        # This type of branch can't be bound.
3481
 
        if local:
3482
 
            raise errors.LocalRequiresBoundBranch()
3483
 
        result = PullResult()
3484
 
        result.source_branch = self.source
3485
 
        if _override_hook_target is None:
3486
 
            result.target_branch = self.target
3487
 
        else:
3488
 
            result.target_branch = _override_hook_target
3489
 
        self.source.lock_read()
3490
 
        try:
3491
 
            # We assume that during 'pull' the target repository is closer than
3492
 
            # the source one.
3493
 
            self.source.update_references(self.target)
3494
 
            graph = self.target.repository.get_graph(self.source.repository)
3495
 
            # TODO: Branch formats should have a flag that indicates 
3496
 
            # that revno's are expensive, and pull() should honor that flag.
3497
 
            # -- JRV20090506
3498
 
            result.old_revno, result.old_revid = \
3499
 
                self.target.last_revision_info()
3500
 
            self._update_revisions(stop_revision, overwrite=overwrite,
3501
 
                graph=graph)
3502
 
            # TODO: The old revid should be specified when merging tags, 
3503
 
            # so a tags implementation that versions tags can only 
3504
 
            # pull in the most recent changes. -- JRV20090506
3505
 
            result.tag_conflicts = self.source.tags.merge_to(self.target.tags,
3506
 
                overwrite, ignore_master=not merge_tags_to_master)
3507
 
            result.new_revno, result.new_revid = self.target.last_revision_info()
3508
 
            if _hook_master:
3509
 
                result.master_branch = _hook_master
3510
 
                result.local_branch = result.target_branch
3511
 
            else:
3512
 
                result.master_branch = result.target_branch
3513
 
                result.local_branch = None
3514
 
            if run_hooks:
3515
 
                for hook in Branch.hooks['post_pull']:
3516
 
                    hook(result)
3517
 
        finally:
3518
 
            self.source.unlock()
3519
 
        return result
3520
 
 
3521
 
 
3522
 
InterBranch.register_optimiser(GenericInterBranch)
 
1347
# predicates
 
1348
 
 
1349
 
 
1350
def is_control_file(filename):
 
1351
    ## FIXME: better check
 
1352
    filename = os.path.normpath(filename)
 
1353
    while filename != '':
 
1354
        head, tail = os.path.split(filename)
 
1355
        ## mutter('check %r for control file' % ((head, tail), ))
 
1356
        if tail == bzrlib.BZRDIR:
 
1357
            return True
 
1358
        if filename == head:
 
1359
            break
 
1360
        filename = head
 
1361
    return False
 
1362
 
 
1363
 
 
1364
 
 
1365
def gen_file_id(name):
 
1366
    """Return new file id.
 
1367
 
 
1368
    This should probably generate proper UUIDs, but for the moment we
 
1369
    cope with just randomness because running uuidgen every time is
 
1370
    slow."""
 
1371
    import re
 
1372
    from binascii import hexlify
 
1373
    from time import time
 
1374
 
 
1375
    # get last component
 
1376
    idx = name.rfind('/')
 
1377
    if idx != -1:
 
1378
        name = name[idx+1 : ]
 
1379
    idx = name.rfind('\\')
 
1380
    if idx != -1:
 
1381
        name = name[idx+1 : ]
 
1382
 
 
1383
    # make it not a hidden file
 
1384
    name = name.lstrip('.')
 
1385
 
 
1386
    # remove any wierd characters; we don't escape them but rather
 
1387
    # just pull them out
 
1388
    name = re.sub(r'[^\w.]', '', name)
 
1389
 
 
1390
    s = hexlify(rand_bytes(8))
 
1391
    return '-'.join((name, compact_date(time()), s))
 
1392
 
 
1393
 
 
1394
def gen_root_id():
 
1395
    """Return a new tree-root file id."""
 
1396
    return gen_file_id('TREE_ROOT')
 
1397
 
 
1398