/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
1
# Copyright (C) 2007 Canonical Ltd
0.200.910 by Jelmer Vernooij
update copyright years
2
# Copyright (C) 2010 Jelmer Vernooij
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
3
#
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
0.200.1012 by Jelmer Vernooij
Rename BzrDir to ControlDir.
18
"""An adapter between a Git control dir and a Bazaar ControlDir."""
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
19
0.200.1594 by Jelmer Vernooij
Use absolute_import everywhere.
20
from __future__ import absolute_import
21
0.200.1311 by Jelmer Vernooij
More work on colocated branch support.
22
import urllib
23
0.200.1641 by Jelmer Vernooij
Use relative imports where possible.
24
from ... import (
0.200.1688 by Jelmer Vernooij
Fix stacking tests.
25
    branch as _mod_branch,
0.239.13 by Jelmer Vernooij
Don't break "bzr info -v" when Dulwich is not installed.
26
    errors as bzr_errors,
0.200.1137 by Jelmer Vernooij
Support BzrProber.known_formats().
27
    trace,
0.200.1172 by Jelmer Vernooij
Provide GitDir._available_backup_name.
28
    osutils,
0.200.1566 by Jelmer Vernooij
Basic implementation of LocalGitDir.destroy_workingtree.
29
    revision as _mod_revision,
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
30
    urlutils,
31
    )
0.200.1702 by Jelmer Vernooij
Implement GitDir.acquire_repository.
32
from ...controldir import RepositoryAcquisitionPolicy
0.310.14 by Jelmer Vernooij
merge lightweight checkout-in-git support.
33
from ...transport import (
34
    do_catching_redirections,
35
    get_transport_from_path,
36
    )
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
37
0.200.1641 by Jelmer Vernooij
Use relative imports where possible.
38
from ...controldir import (
0.200.1111 by Jelmer Vernooij
Drop support for Bazaar < 2.3.
39
    ControlDir,
0.200.1137 by Jelmer Vernooij
Support BzrProber.known_formats().
40
    ControlDirFormat,
0.200.1111 by Jelmer Vernooij
Drop support for Bazaar < 2.3.
41
    format_registry,
0.200.1702 by Jelmer Vernooij
Implement GitDir.acquire_repository.
42
    RepositoryAcquisitionPolicy,
0.200.1111 by Jelmer Vernooij
Drop support for Bazaar < 2.3.
43
    )
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
44
0.311.3 by Jelmer Vernooij
Fix handling of lightweight checkouts.
45
from .transportgit import (
46
    OBJECTDIR,
47
    TransportObjectStore,
48
    )
49
0.200.123 by Jelmer Vernooij
Use central git module.
50
0.200.1026 by Jelmer Vernooij
Fix typo.
51
class GitDirConfig(object):
0.200.1025 by Jelmer Vernooij
Implement GitDir.get_config().
52
53
    def get_default_stack_on(self):
54
        return None
55
56
    def set_default_stack_on(self, value):
57
        raise bzr_errors.BzrError("Cannot set configuration")
58
59
0.200.1137 by Jelmer Vernooij
Support BzrProber.known_formats().
60
class GitControlDirFormat(ControlDirFormat):
61
62
    colocated_branches = True
63
    fixed_components = True
64
65
    def __eq__(self, other):
66
        return type(self) == type(other)
67
68
    def is_supported(self):
69
        return True
70
71
    def network_name(self):
72
        return "git"
73
74
0.200.1702 by Jelmer Vernooij
Implement GitDir.acquire_repository.
75
class UseExistingRepository(RepositoryAcquisitionPolicy):
76
    """A policy of reusing an existing repository"""
77
78
    def __init__(self, repository, stack_on=None, stack_on_pwd=None,
79
                 require_stacking=False):
80
        """Constructor.
81
82
        :param repository: The repository to use.
83
        :param stack_on: A location to stack on
84
        :param stack_on_pwd: If stack_on is relative, the location it is
85
            relative to.
86
        """
87
        super(UseExistingRepository, self).__init__(
88
                stack_on, stack_on_pwd, require_stacking)
89
        self._repository = repository
90
91
    def acquire_repository(self, make_working_trees=None, shared=False,
92
            possible_transports=None):
93
        """Implementation of RepositoryAcquisitionPolicy.acquire_repository
94
95
        Returns an existing repository to use.
96
        """
97
        return self._repository, False
98
99
0.200.1012 by Jelmer Vernooij
Rename BzrDir to ControlDir.
100
class GitDir(ControlDir):
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
101
    """An adapter to the '.git' dir used by git."""
102
0.200.148 by Jelmer Vernooij
Share more infrastructure between LocalGitDir and RemoteGitDir.
103
    def is_supported(self):
104
        return True
105
0.200.981 by Jelmer Vernooij
Mark git directories as not convertable (for now).
106
    def can_convert_format(self):
107
        return False
108
0.200.1025 by Jelmer Vernooij
Implement GitDir.get_config().
109
    def break_lock(self):
0.314.1 by Jelmer Vernooij
Mark ControlDir.break_lock as not implemented.
110
        # There are no global locks, so nothing to break.
111
        raise NotImplementedError(self.break_lock)
0.200.1025 by Jelmer Vernooij
Implement GitDir.get_config().
112
0.200.155 by Jelmer Vernooij
Fix formatting, remove catch-all for exceptions when opening local repositories.
113
    def cloning_metadir(self, stacked=False):
0.288.1 by Jelmer Vernooij
Use git format as default for clones.
114
        return format_registry.make_controldir("git")
0.200.155 by Jelmer Vernooij
Fix formatting, remove catch-all for exceptions when opening local repositories.
115
0.200.1165 by Jelmer Vernooij
Implement GitDir.checkout_metadir.
116
    def checkout_metadir(self, stacked=False):
0.288.1 by Jelmer Vernooij
Use git format as default for clones.
117
        return format_registry.make_controldir("git")
0.200.1165 by Jelmer Vernooij
Implement GitDir.checkout_metadir.
118
0.269.8 by Jelmer Vernooij
Support push in git-remote-bzr.
119
    def _get_selected_ref(self, branch, ref=None):
120
        if ref is not None and branch is not None:
121
            raise bzr_errors.BzrError("can't specify both ref and branch")
122
        if ref is not None:
123
            return ref
0.303.2 by Jelmer Vernooij
Fix resolution order for segment parameters.
124
        if branch is not None:
125
            from .refs import branch_name_to_ref
126
            return branch_name_to_ref(branch)
0.200.1559 by Jelmer Vernooij
Fix compatibility with bzr 2.5.
127
        segment_parameters = getattr(
128
            self.user_transport, "get_segment_parameters", lambda: {})()
129
        ref = segment_parameters.get("ref")
130
        if ref is not None:
131
            return urlutils.unescape(ref)
0.200.1310 by Jelmer Vernooij
Add _get_selected_ref method.
132
        if branch is None and getattr(self, "_get_selected_branch", False):
133
            branch = self._get_selected_branch()
0.303.2 by Jelmer Vernooij
Fix resolution order for segment parameters.
134
            if branch is not None:
135
                from .refs import branch_name_to_ref
136
                return branch_name_to_ref(branch)
0.303.1 by Jelmer Vernooij
Remove _get_default_ref.
137
        return b"HEAD"
0.200.1310 by Jelmer Vernooij
Add _get_selected_ref method.
138
0.200.1025 by Jelmer Vernooij
Implement GitDir.get_config().
139
    def get_config(self):
140
        return GitDirConfig()
141
0.200.1172 by Jelmer Vernooij
Provide GitDir._available_backup_name.
142
    def _available_backup_name(self, base):
143
        return osutils.available_backup_name(base, self.root_transport.has)
144
0.259.1 by Jelmer Vernooij
Provide custom GitDir.sprout() for bzr 2.4 compatibility.
145
    def sprout(self, url, revision_id=None, force_new_repo=False,
146
               recurse='down', possible_transports=None,
147
               accelerator_tree=None, hardlink=False, stacked=False,
148
               source_branch=None, create_tree_if_local=True):
0.200.1644 by Jelmer Vernooij
More relative imports.
149
        from ...repository import InterRepository
150
        from ...transport.local import LocalTransport
151
        from ...transport import get_transport
0.259.1 by Jelmer Vernooij
Provide custom GitDir.sprout() for bzr 2.4 compatibility.
152
        target_transport = get_transport(url, possible_transports)
153
        target_transport.ensure_base()
154
        cloning_format = self.cloning_metadir()
155
        # Create/update the result branch
0.310.15 by Jelmer Vernooij
Handle destroy_workingtree.
156
        try:
157
            result = ControlDir.open_from_transport(target_transport)
158
        except bzr_errors.NotBranchError:
159
            result = cloning_format.initialize_on_transport(target_transport)
0.200.1373 by Jelmer Vernooij
Prevent accidentally removing branch.
160
        source_branch = self.open_branch()
0.259.1 by Jelmer Vernooij
Provide custom GitDir.sprout() for bzr 2.4 compatibility.
161
        source_repository = self.find_repository()
162
        try:
163
            result_repo = result.find_repository()
164
        except bzr_errors.NoRepositoryPresent:
165
            result_repo = result.create_repository()
166
            target_is_empty = True
167
        else:
168
            target_is_empty = None # Unknown
169
        if stacked:
0.200.1688 by Jelmer Vernooij
Fix stacking tests.
170
            raise _mod_branch.UnstackableBranchFormat(self._format, self.user_url)
0.259.1 by Jelmer Vernooij
Provide custom GitDir.sprout() for bzr 2.4 compatibility.
171
        interrepo = InterRepository.get(source_repository, result_repo)
172
173
        if revision_id is not None:
0.259.4 by Jelmer Vernooij
Put determine_wants methods on InterRepo.
174
            determine_wants = interrepo.get_determine_wants_revids(
0.200.1777 by Jelmer Vernooij
Copy tags.
175
                [revision_id], include_tags=True)
0.259.1 by Jelmer Vernooij
Provide custom GitDir.sprout() for bzr 2.4 compatibility.
176
        else:
0.259.4 by Jelmer Vernooij
Put determine_wants methods on InterRepo.
177
            determine_wants = interrepo.determine_wants_all
0.259.1 by Jelmer Vernooij
Provide custom GitDir.sprout() for bzr 2.4 compatibility.
178
        interrepo.fetch_objects(determine_wants=determine_wants,
179
            mapping=source_branch.mapping)
180
        result_branch = source_branch.sprout(result,
181
            revision_id=revision_id, repository=result_repo)
0.200.1372 by Jelmer Vernooij
Fix formatting.
182
        if (create_tree_if_local
183
            and isinstance(target_transport, LocalTransport)
0.259.1 by Jelmer Vernooij
Provide custom GitDir.sprout() for bzr 2.4 compatibility.
184
            and (result_repo is None or result_repo.make_working_trees())):
185
            wt = result.create_workingtree(accelerator_tree=accelerator_tree,
186
                hardlink=hardlink, from_branch=result_branch)
0.200.1788 by Jelmer Vernooij
Use context managers.
187
            with wt.lock_write():
0.259.1 by Jelmer Vernooij
Provide custom GitDir.sprout() for bzr 2.4 compatibility.
188
                if wt.path2id('') is None:
189
                    try:
190
                        wt.set_root_id(self.open_workingtree.get_root_id())
191
                    except bzr_errors.NoWorkingTree:
192
                        pass
193
        return result
194
0.200.1117 by Jelmer Vernooij
Provide basic implementation of GitDir.clone_on_transport.
195
    def clone_on_transport(self, transport, revision_id=None,
196
        force_new_repo=False, preserve_stacking=False, stacked_on=None,
197
        create_prefix=False, use_existing_dir=True, no_tree=False):
198
        """See ControlDir.clone_on_transport."""
0.200.1644 by Jelmer Vernooij
More relative imports.
199
        from ...repository import InterRepository
200
        from .mapping import default_mapping
0.285.3 by Jelmer Vernooij
Fix handling of stacking requests.
201
        if stacked_on is not None:
202
            raise _mod_branch.UnstackableBranchFormat(self._format, self.user_url)
0.200.1119 by Jelmer Vernooij
Refactor repository initialization.
203
        if no_tree:
204
            format = BareLocalGitControlDirFormat()
205
        else:
206
            format = LocalGitControlDirFormat()
0.200.1778 by Jelmer Vernooij
Fix cloning to non-last revision.
207
        (target_repo, target_controldir, stacking,
208
                repo_policy) = format.initialize_on_transport_ex(
209
                        transport, use_existing_dir=use_existing_dir,
210
                        create_prefix=create_prefix,
211
                        force_new_repo=force_new_repo)
0.200.1119 by Jelmer Vernooij
Refactor repository initialization.
212
        target_git_repo = target_repo._git
0.302.1 by Jelmer Vernooij
Implement .find_repository.
213
        source_repo = self.find_repository()
0.200.1117 by Jelmer Vernooij
Provide basic implementation of GitDir.clone_on_transport.
214
        source_git_repo = source_repo._git
0.200.1171 by Jelmer Vernooij
Fix some more tests.
215
        interrepo = InterRepository.get(source_repo, target_repo)
0.200.1117 by Jelmer Vernooij
Provide basic implementation of GitDir.clone_on_transport.
216
        if revision_id is not None:
0.200.1171 by Jelmer Vernooij
Fix some more tests.
217
            determine_wants = interrepo.get_determine_wants_revids([revision_id], include_tags=True)
0.200.1117 by Jelmer Vernooij
Provide basic implementation of GitDir.clone_on_transport.
218
        else:
0.200.1171 by Jelmer Vernooij
Fix some more tests.
219
            determine_wants = interrepo.determine_wants_all
220
        (pack_hint, _, refs) = interrepo.fetch_objects(determine_wants,
221
            mapping=default_mapping)
0.200.1778 by Jelmer Vernooij
Fix cloning to non-last revision.
222
        if revision_id is not None:
223
            ref_chain, unused_sha = self._git.refs.follow(self._get_selected_ref(None))
224
            foreign_revid = source_repo.lookup_bzr_revision_id(revision_id)[0]
225
            refs[ref_chain[-1]] = foreign_revid
0.200.1117 by Jelmer Vernooij
Provide basic implementation of GitDir.clone_on_transport.
226
        for name, val in refs.iteritems():
227
            target_git_repo.refs[name] = val
0.200.1411 by Jelmer Vernooij
Fix control files.
228
        return self.__class__(transport, target_git_repo, format)
0.200.1117 by Jelmer Vernooij
Provide basic implementation of GitDir.clone_on_transport.
229
0.310.14 by Jelmer Vernooij
merge lightweight checkout-in-git support.
230
    def _find_commondir(self):
231
        try:
232
            commondir = self.control_transport.get_bytes('commondir')
233
        except bzr_errors.NoSuchFile:
234
            return self
235
        else:
236
            commondir = commondir.rstrip('/.git/')
237
            return ControlDir.open_from_transport(get_transport_from_path(commondir))
238
0.259.2 by Jelmer Vernooij
Make sure RemoteGitDir.find_repository works.
239
    def find_repository(self):
240
        """Find the repository that should be used.
241
242
        This does not require a branch as we use it to find the repo for
243
        new branches as well as to hook existing branches up to their
244
        repository.
245
        """
0.310.14 by Jelmer Vernooij
merge lightweight checkout-in-git support.
246
        return self._gitrepository_class(self._find_commondir())
0.259.2 by Jelmer Vernooij
Make sure RemoteGitDir.find_repository works.
247
0.200.1487 by Jelmer Vernooij
Use peeling.
248
    def get_refs_container(self):
249
        """Retrieve the refs container.
0.200.1434 by Jelmer Vernooij
Move refs access to control dir.
250
        """
0.200.1487 by Jelmer Vernooij
Use peeling.
251
        raise NotImplementedError(self.get_refs_container)
0.200.1434 by Jelmer Vernooij
Move refs access to control dir.
252
0.200.1701 by Jelmer Vernooij
Fix a few tests.
253
    def determine_repository_policy(self, force_new_repo=False, stack_on=None,
254
                                    stack_on_pwd=None, require_stacking=False):
255
        """Return an object representing a policy to use.
256
257
        This controls whether a new repository is created, and the format of
258
        that repository, or some existing shared repository used instead.
259
260
        If stack_on is supplied, will not seek a containing shared repo.
261
262
        :param force_new_repo: If True, require a new repository to be created.
263
        :param stack_on: If supplied, the location to stack on.  If not
264
            supplied, a default_stack_on location may be used.
265
        :param stack_on_pwd: If stack_on is relative, the location it is
266
            relative to.
267
        """
0.302.1 by Jelmer Vernooij
Implement .find_repository.
268
        return UseExistingRepository(self.find_repository())
0.200.1701 by Jelmer Vernooij
Fix a few tests.
269
0.200.148 by Jelmer Vernooij
Share more infrastructure between LocalGitDir and RemoteGitDir.
270
0.200.1137 by Jelmer Vernooij
Support BzrProber.known_formats().
271
class LocalGitControlDirFormat(GitControlDirFormat):
272
    """The .git directory control format."""
273
274
    bare = False
275
276
    @classmethod
277
    def _known_formats(self):
278
        return set([LocalGitControlDirFormat()])
279
280
    @property
281
    def repository_format(self):
0.200.1644 by Jelmer Vernooij
More relative imports.
282
        from .repository import GitRepositoryFormat
0.200.1137 by Jelmer Vernooij
Support BzrProber.known_formats().
283
        return GitRepositoryFormat()
284
0.290.3 by Jelmer Vernooij
Set workingtree_format on LocalGitDirFormat.
285
    @property
286
    def workingtree_format(self):
287
        from .workingtree import GitWorkingTreeFormat
288
        return GitWorkingTreeFormat()
289
0.200.1137 by Jelmer Vernooij
Support BzrProber.known_formats().
290
    def get_branch_format(self):
0.295.1 by Jelmer Vernooij
Split up branch formats.
291
        from .branch import LocalGitBranchFormat
292
        return LocalGitBranchFormat()
0.200.1137 by Jelmer Vernooij
Support BzrProber.known_formats().
293
294
    def open(self, transport, _found=None):
295
        """Open this directory.
296
297
        """
0.200.1644 by Jelmer Vernooij
More relative imports.
298
        from .transportgit import TransportRepo
0.200.1485 by Jelmer Vernooij
Keep track of refs text when opening bare repository.
299
        gitrepo = TransportRepo(transport, self.bare,
300
                refs_text=getattr(self, "_refs_text", None))
0.310.10 by Jelmer Vernooij
Fix format opener fails on empty dir.
301
        if not gitrepo._controltransport.has('HEAD'):
302
            raise bzr_errors.NotBranchError(path=transport.base)
0.200.1411 by Jelmer Vernooij
Fix control files.
303
        return LocalGitDir(transport, gitrepo, self)
0.200.1137 by Jelmer Vernooij
Support BzrProber.known_formats().
304
305
    def get_format_description(self):
306
        return "Local Git Repository"
307
308
    def initialize_on_transport(self, transport):
0.200.1644 by Jelmer Vernooij
More relative imports.
309
        from .transportgit import TransportRepo
0.200.1559 by Jelmer Vernooij
Fix compatibility with bzr 2.5.
310
        repo = TransportRepo.init(transport, bare=self.bare)
0.200.1137 by Jelmer Vernooij
Support BzrProber.known_formats().
311
        return self.open(transport)
312
313
    def initialize_on_transport_ex(self, transport, use_existing_dir=False,
314
        create_prefix=False, force_new_repo=False, stacked_on=None,
315
        stack_on_pwd=None, repo_format_name=None, make_working_trees=None,
316
        shared_repo=False, vfs_only=False):
317
        def make_directory(transport):
318
            transport.mkdir('.')
319
            return transport
320
        def redirected(transport, e, redirection_notice):
321
            trace.note(redirection_notice)
322
            return transport._redirected_to(e.source, e.target)
323
        try:
324
            transport = do_catching_redirections(make_directory, transport,
325
                redirected)
326
        except bzr_errors.FileExists:
327
            if not use_existing_dir:
328
                raise
329
        except bzr_errors.NoSuchFile:
330
            if not create_prefix:
331
                raise
332
            transport.create_prefix()
333
        controldir = self.initialize_on_transport(transport)
0.302.1 by Jelmer Vernooij
Implement .find_repository.
334
        repository = controldir.find_repository()
0.200.1137 by Jelmer Vernooij
Support BzrProber.known_formats().
335
        repository.lock_write()
0.200.1703 by Jelmer Vernooij
Implement apply_inventory_delta.
336
        return (repository, controldir, False,
0.302.1 by Jelmer Vernooij
Implement .find_repository.
337
                UseExistingRepository(controldir.find_repository()))
0.200.1137 by Jelmer Vernooij
Support BzrProber.known_formats().
338
339
    def is_supported(self):
340
        return True
341
0.200.1412 by Jelmer Vernooij
Implement GitControlDirFormat.supports_transport.
342
    def supports_transport(self, transport):
343
        try:
344
            external_url = transport.external_url()
345
        except bzr_errors.InProcessTransport:
346
            raise bzr_errors.NotBranchError(path=transport.base)
347
        return (external_url.startswith("http:") or
348
                external_url.startswith("https:") or
349
                external_url.startswith("file:"))
350
0.200.1137 by Jelmer Vernooij
Support BzrProber.known_formats().
351
352
class BareLocalGitControlDirFormat(LocalGitControlDirFormat):
353
354
    bare = True
355
    supports_workingtrees = False
356
357
    def get_format_description(self):
358
        return "Local Git Repository (bare)"
359
360
0.200.148 by Jelmer Vernooij
Share more infrastructure between LocalGitDir and RemoteGitDir.
361
class LocalGitDir(GitDir):
362
    """An adapter to the '.git' dir used by git."""
363
0.239.13 by Jelmer Vernooij
Don't break "bzr info -v" when Dulwich is not installed.
364
    def _get_gitrepository_class(self):
0.200.1644 by Jelmer Vernooij
More relative imports.
365
        from .repository import LocalGitRepository
0.239.13 by Jelmer Vernooij
Don't break "bzr info -v" when Dulwich is not installed.
366
        return LocalGitRepository
367
0.200.1313 by Jelmer Vernooij
Add __repr__
368
    def __repr__(self):
369
        return "<%s at %r>" % (
370
            self.__class__.__name__, self.root_transport.base)
371
0.239.13 by Jelmer Vernooij
Don't break "bzr info -v" when Dulwich is not installed.
372
    _gitrepository_class = property(_get_gitrepository_class)
0.202.2 by David Allouche
GitRepository.get_inventory and .revision_tree work for the null revision. Support for testing GitRepository without disk data.
373
0.200.1014 by Jelmer Vernooij
Fix tests.
374
    @property
375
    def user_transport(self):
376
        return self.root_transport
377
378
    @property
379
    def control_transport(self):
380
        return self.transport
381
0.200.1411 by Jelmer Vernooij
Fix control files.
382
    def __init__(self, transport, gitrepo, format):
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
383
        self._format = format
384
        self.root_transport = transport
0.200.1018 by Jelmer Vernooij
Fix use with new control dir API.
385
        self._mode_check_done = False
0.200.90 by Jelmer Vernooij
Basic support for opening working trees.
386
        self._git = gitrepo
387
        if gitrepo.bare:
388
            self.transport = transport
389
        else:
390
            self.transport = transport.clone('.git')
0.200.381 by Jelmer Vernooij
Support working trees properly, status and ls.
391
        self._mode_check_done = None
392
393
    def is_control_filename(self, filename):
0.200.1603 by Jelmer Vernooij
Ignore control directory filenames on Windows, too.
394
        return (filename == '.git' or
395
                filename.startswith('.git/') or
396
                filename.startswith('.git\\'))
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
397
0.200.1311 by Jelmer Vernooij
More work on colocated branch support.
398
    def _get_symref(self, ref):
399
        from dulwich.repo import SYMREF
0.310.14 by Jelmer Vernooij
merge lightweight checkout-in-git support.
400
        ref_chain, unused_sha = self._git.refs.follow(ref)
401
        if len(ref_chain) == 1:
0.200.1311 by Jelmer Vernooij
More work on colocated branch support.
402
            return None
0.310.14 by Jelmer Vernooij
merge lightweight checkout-in-git support.
403
        return ref_chain[1]
0.200.1311 by Jelmer Vernooij
More work on colocated branch support.
404
0.200.1773 by Jelmer Vernooij
Fix argument name.
405
    def set_branch_reference(self, target_branch, name=None):
0.200.1311 by Jelmer Vernooij
More work on colocated branch support.
406
        ref = self._get_selected_ref(name)
0.311.1 by Jelmer Vernooij
Support git lightweight checkouts.
407
        if self.control_transport.base == target_branch.controldir.control_transport.base:
408
            self._git.refs.set_symbolic_ref(ref, target_branch.ref)
409
        else:
410
            try:
411
                target_path = target_branch.controldir.control_transport.local_abspath('.')
412
            except bzr_errors.NotLocalUrl:
413
                raise bzr_errors.IncompatibleFormat(target_branch._format, self._format)
0.310.14 by Jelmer Vernooij
merge lightweight checkout-in-git support.
414
            # TODO(jelmer): Do some consistency checking across branches..
0.311.1 by Jelmer Vernooij
Support git lightweight checkouts.
415
            self.control_transport.put_bytes('commondir', target_path.encode('utf-8'))
0.311.3 by Jelmer Vernooij
Fix handling of lightweight checkouts.
416
            # TODO(jelmer): Urgh, avoid mucking about with internals.
417
            self._git._commontransport = target_branch.repository._git._commontransport.clone()
418
            self._git.object_store = TransportObjectStore(self._git._commontransport.clone(OBJECTDIR))
419
            self._git.refs.transport = self._git._commontransport
0.310.14 by Jelmer Vernooij
merge lightweight checkout-in-git support.
420
            target_ref_chain, unused_sha = target_branch.controldir._git.refs.follow(target_branch.ref)
421
            for target_ref in target_ref_chain:
422
                if target_ref == b'HEAD':
423
                    continue
424
                break
425
            else:
426
                # Can't create a reference to something that is not a in a repository.
427
                raise bzr_errors.IncompatibleFormat(self.set_branch_reference, self)
428
            self._git.refs.set_symbolic_ref(ref, target_ref)
0.200.1311 by Jelmer Vernooij
More work on colocated branch support.
429
430
    def get_branch_reference(self, name=None):
431
        ref = self._get_selected_ref(name)
432
        target_ref = self._get_symref(ref)
433
        if target_ref is not None:
0.303.3 by Jelmer Vernooij
Prefer using branch segment parameter in reference branch URLs.
434
            from .refs import ref_to_branch_name
435
            try:
436
                branch_name = ref_to_branch_name(target_ref)
437
            except ValueError:
438
                params = {'ref': urllib.quote(target_ref, '')}
439
            else:
440
                if branch_name != b'':
441
                    params = {'branch': urllib.quote(branch_name.encode('utf-8'), '')}
442
                else:
443
                    params = {}
0.310.14 by Jelmer Vernooij
merge lightweight checkout-in-git support.
444
            try:
445
                base_url = urlutils.local_path_to_url(self.control_transport.get_bytes('commondir')).rstrip('/.git/')+'/'
446
            except bzr_errors.NoSuchFile:
447
                base_url = self.user_url.rstrip('/')
448
            return urlutils.join_segment_parameters(base_url, params)
0.200.1311 by Jelmer Vernooij
More work on colocated branch support.
449
        return None
450
451
    def find_branch_format(self, name=None):
0.200.1644 by Jelmer Vernooij
More relative imports.
452
        from .branch import (
0.200.1311 by Jelmer Vernooij
More work on colocated branch support.
453
            GitSymrefBranchFormat,
0.295.1 by Jelmer Vernooij
Split up branch formats.
454
            LocalGitBranchFormat,
0.200.1311 by Jelmer Vernooij
More work on colocated branch support.
455
            )
456
        ref = self._get_selected_ref(name)
457
        if self._get_symref(ref) is not None:
458
            return GitSymrefBranchFormat()
459
        else:
0.295.1 by Jelmer Vernooij
Split up branch formats.
460
            return LocalGitBranchFormat()
0.200.1311 by Jelmer Vernooij
More work on colocated branch support.
461
0.200.978 by Jelmer Vernooij
Allow name argument to get_branch_transport to be missing.
462
    def get_branch_transport(self, branch_format, name=None):
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
463
        if branch_format is None:
464
            return self.transport
0.200.1012 by Jelmer Vernooij
Rename BzrDir to ControlDir.
465
        if isinstance(branch_format, LocalGitControlDirFormat):
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
466
            return self.transport
0.239.13 by Jelmer Vernooij
Don't break "bzr info -v" when Dulwich is not installed.
467
        raise bzr_errors.IncompatibleFormat(branch_format, self._format)
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
468
0.200.887 by Jelmer Vernooij
get_branch_transport takes a name argument.
469
    def get_repository_transport(self, format):
470
        if format is None:
471
            return self.transport
0.200.1012 by Jelmer Vernooij
Rename BzrDir to ControlDir.
472
        if isinstance(format, LocalGitControlDirFormat):
0.200.887 by Jelmer Vernooij
get_branch_transport takes a name argument.
473
            return self.transport
474
        raise bzr_errors.IncompatibleFormat(format, self._format)
475
476
    def get_workingtree_transport(self, format):
477
        if format is None:
478
            return self.transport
0.200.1012 by Jelmer Vernooij
Rename BzrDir to ControlDir.
479
        if isinstance(format, LocalGitControlDirFormat):
0.200.887 by Jelmer Vernooij
get_branch_transport takes a name argument.
480
            return self.transport
481
        raise bzr_errors.IncompatibleFormat(format, self._format)
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
482
0.269.8 by Jelmer Vernooij
Support push in git-remote-bzr.
483
    def open_branch(self, name=None, unsupported=False, ignore_fallbacks=None,
0.310.9 by Jelmer Vernooij
Some controldir fixes.
484
            ref=None, possible_transports=None, nascent_ok=False):
0.200.57 by Jelmer Vernooij
Fix more tests.
485
        """'create' a branch for this dir."""
0.302.1 by Jelmer Vernooij
Implement .find_repository.
486
        repo = self.find_repository()
0.200.1644 by Jelmer Vernooij
More relative imports.
487
        from .branch import LocalGitBranch
0.269.8 by Jelmer Vernooij
Support push in git-remote-bzr.
488
        ref = self._get_selected_ref(name, ref)
0.310.9 by Jelmer Vernooij
Some controldir fixes.
489
        if not nascent_ok and ref not in self._git.refs:
0.200.1311 by Jelmer Vernooij
More work on colocated branch support.
490
            raise bzr_errors.NotBranchError(self.root_transport.base,
0.200.1648 by Jelmer Vernooij
Fix compatibility with newer versions of breezy.
491
                    controldir=self)
0.310.9 by Jelmer Vernooij
Some controldir fixes.
492
        ref_chain, unused_sha = self._git.refs.follow(ref)
0.310.14 by Jelmer Vernooij
merge lightweight checkout-in-git support.
493
        if ref_chain[-1] == b'HEAD':
494
            controldir = self
495
        else:
496
            controldir = self._find_commondir()
497
        return LocalGitBranch(controldir, repo, ref_chain[-1])
0.200.722 by Jelmer Vernooij
Implement GitDir.list_branches() and support name argument to open_branch.
498
0.200.724 by Jelmer Vernooij
support destroy_branch
499
    def destroy_branch(self, name=None):
0.200.1310 by Jelmer Vernooij
Add _get_selected_ref method.
500
        refname = self._get_selected_ref(name)
0.310.2 by Jelmer Vernooij
Don't allow removing HEAD.
501
        if refname == b'HEAD':
502
            # HEAD can't be removed
503
            raise bzr_errors.UnsupportedOperation(
504
                self.destroy_branch, self)
0.200.1364 by Jelmer Vernooij
Fix .destroy_branch.
505
        try:
506
            del self._git.refs[refname]
507
        except KeyError:
0.200.997 by Jelmer Vernooij
Implement BzrDir.needs_format_conversion.
508
            raise bzr_errors.NotBranchError(self.root_transport.base,
0.200.1648 by Jelmer Vernooij
Fix compatibility with newer versions of breezy.
509
                    controldir=self)
0.200.724 by Jelmer Vernooij
support destroy_branch
510
0.200.980 by Jelmer Vernooij
Implement LocalGitBzrDir.destroy_repository().
511
    def destroy_repository(self):
512
        raise bzr_errors.UnsupportedOperation(self.destroy_repository, self)
513
0.200.986 by Jelmer Vernooij
Implement GitDir.destroy_workingtree.
514
    def destroy_workingtree(self):
0.310.15 by Jelmer Vernooij
Handle destroy_workingtree.
515
        raise bzr_errors.UnsupportedOperation(self.destroy_workingtree, self)
0.200.1566 by Jelmer Vernooij
Basic implementation of LocalGitDir.destroy_workingtree.
516
517
    def destroy_workingtree_metadata(self):
0.310.15 by Jelmer Vernooij
Handle destroy_workingtree.
518
        raise bzr_errors.UnsupportedOperation(self.destroy_workingtree_metadata, self)
0.200.986 by Jelmer Vernooij
Implement GitDir.destroy_workingtree.
519
0.200.997 by Jelmer Vernooij
Implement BzrDir.needs_format_conversion.
520
    def needs_format_conversion(self, format=None):
521
        return not isinstance(self._format, format.__class__)
522
0.200.722 by Jelmer Vernooij
Implement GitDir.list_branches() and support name argument to open_branch.
523
    def list_branches(self):
0.200.1501 by Jelmer Vernooij
Provide ControlDir.get_branches.
524
        return self.get_branches().values()
525
526
    def get_branches(self):
0.200.1644 by Jelmer Vernooij
More relative imports.
527
        from .refs import ref_to_branch_name
0.200.1501 by Jelmer Vernooij
Provide ControlDir.get_branches.
528
        ret = {}
529
        for ref in self._git.refs.keys():
530
            try:
531
                branch_name = ref_to_branch_name(ref)
532
            except ValueError:
533
                continue
534
            except UnicodeDecodeError:
535
                trace.warning("Ignoring branch %r with unicode error ref", ref)
536
                continue
537
            ret[branch_name] = self.open_branch(ref=ref)
0.200.722 by Jelmer Vernooij
Implement GitDir.list_branches() and support name argument to open_branch.
538
        return ret
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
539
0.200.1114 by Jelmer Vernooij
Properly raise exception when create_repository is called with shared=True
540
    def open_repository(self):
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
541
        """'open' a repository for this dir."""
0.302.1 by Jelmer Vernooij
Implement .find_repository.
542
        if self.control_transport.has('commondir'):
543
            raise bzr_errors.NoRepositoryPresent(self)
0.200.1411 by Jelmer Vernooij
Fix control files.
544
        return self._gitrepository_class(self)
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
545
0.310.3 by Jelmer Vernooij
Implement ControlDir.has_workingtree.
546
    def has_workingtree(self):
547
        return not self._git.bare
548
0.200.1537 by Jelmer Vernooij
Support unsupported= argument.
549
    def open_workingtree(self, recommend_upgrade=True, unsupported=False):
0.246.5 by Jelmer Vernooij
Cope with has_index not existing.
550
        if not self._git.bare:
551
            from dulwich.errors import NoIndexPresent
0.302.1 by Jelmer Vernooij
Implement .find_repository.
552
            repo = self.find_repository()
0.246.5 by Jelmer Vernooij
Cope with has_index not existing.
553
            try:
0.200.803 by Jelmer Vernooij
Default to non-bare repositories when initializing a control directory.
554
                index = repo._git.open_index()
0.246.5 by Jelmer Vernooij
Cope with has_index not existing.
555
            except NoIndexPresent:
556
                pass
0.200.803 by Jelmer Vernooij
Default to non-bare repositories when initializing a control directory.
557
            else:
0.200.1644 by Jelmer Vernooij
More relative imports.
558
                from .workingtree import GitWorkingTree
0.310.9 by Jelmer Vernooij
Some controldir fixes.
559
                branch = self.open_branch(ref=b'HEAD', nascent_ok=True)
560
                return GitWorkingTree(self, repo, branch, index)
0.200.392 by Jelmer Vernooij
Fix some tests now that working trees are supported.
561
        loc = urlutils.unescape_for_display(self.root_transport.base, 'ascii')
0.239.13 by Jelmer Vernooij
Don't break "bzr info -v" when Dulwich is not installed.
562
        raise bzr_errors.NoWorkingTree(loc)
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
563
0.200.108 by Jelmer Vernooij
Support bzr init --git.
564
    def create_repository(self, shared=False):
0.200.1644 by Jelmer Vernooij
More relative imports.
565
        from .repository import GitRepositoryFormat
0.200.1114 by Jelmer Vernooij
Properly raise exception when create_repository is called with shared=True
566
        if shared:
567
            raise bzr_errors.IncompatibleFormat(GitRepositoryFormat(), self._format)
0.302.1 by Jelmer Vernooij
Implement .find_repository.
568
        return self.find_repository()
0.200.288 by Jelmer Vernooij
Add test for init-repo.
569
0.200.1377 by Jelmer Vernooij
Fix get_branch_reference.
570
    def create_branch(self, name=None, repository=None,
0.269.8 by Jelmer Vernooij
Support push in git-remote-bzr.
571
                      append_revisions_only=None, ref=None):
572
        refname = self._get_selected_ref(name, ref)
0.310.1 by Jelmer Vernooij
Support from_branch, don't raise AlreadyBranchError for HEAD.
573
        if refname != b'HEAD' and refname in self._git.refs:
0.200.1559 by Jelmer Vernooij
Fix compatibility with bzr 2.5.
574
            raise bzr_errors.AlreadyBranchError(self.user_url)
0.310.9 by Jelmer Vernooij
Some controldir fixes.
575
        repo = self.open_repository()
576
        from dulwich.objects import ZERO_SHA
577
        if refname in self._git.refs:
0.310.14 by Jelmer Vernooij
merge lightweight checkout-in-git support.
578
            ref_chain, unused_sha = self._git.refs.follow(self._get_selected_ref(None))
579
            if ref_chain[0] == b'HEAD':
580
                refname = ref_chain[1]
581
            self._git.refs[refname] = ZERO_SHA
582
        from .branch import LocalGitBranch
583
        branch = LocalGitBranch(self, repo, refname)
0.200.1378 by Jelmer Vernooij
Fix branch.
584
        if append_revisions_only:
0.200.1377 by Jelmer Vernooij
Fix get_branch_reference.
585
            branch.set_append_revisions_only(append_revisions_only)
586
        return branch
0.200.535 by Jelmer Vernooij
use standard version to check for index.
587
588
    def backup_bzrdir(self):
0.200.1549 by Jelmer Vernooij
Support backing up bare repositories.
589
        if not self._git.bare:
0.200.535 by Jelmer Vernooij
use standard version to check for index.
590
            self.root_transport.copy_tree(".git", ".git.backup")
591
            return (self.root_transport.abspath(".git"),
592
                    self.root_transport.abspath(".git.backup"))
593
        else:
0.200.1549 by Jelmer Vernooij
Support backing up bare repositories.
594
            basename = urlutils.basename(self.root_transport.base)
595
            parent = self.root_transport.clone('..')
596
            parent.copy_tree(basename, basename + ".backup")
0.200.535 by Jelmer Vernooij
use standard version to check for index.
597
598
    def create_workingtree(self, revision_id=None, from_branch=None,
599
        accelerator_tree=None, hardlink=False):
600
        if self._git.bare:
0.200.1038 by Jelmer Vernooij
Raise UnsupportedOperation on create_workingtree.
601
            raise bzr_errors.UnsupportedOperation(self.create_workingtree, self)
0.288.5 by Jelmer Vernooij
Fix working tree contruction.
602
        from dulwich.index import build_index_from_tree
603
        from dulwich.objects import ZERO_SHA
0.310.1 by Jelmer Vernooij
Support from_branch, don't raise AlreadyBranchError for HEAD.
604
        if from_branch is None:
0.310.9 by Jelmer Vernooij
Some controldir fixes.
605
            from_branch = self.open_branch(nascent_ok=True)
0.288.5 by Jelmer Vernooij
Fix working tree contruction.
606
        if revision_id is None:
0.310.1 by Jelmer Vernooij
Support from_branch, don't raise AlreadyBranchError for HEAD.
607
            revision_id = from_branch.last_revision()
0.302.1 by Jelmer Vernooij
Implement .find_repository.
608
        repo = self.find_repository()
0.288.5 by Jelmer Vernooij
Fix working tree contruction.
609
        store = repo._git.object_store
610
        (commit_id, mapping) = repo.lookup_bzr_revision_id(revision_id)
611
        build_index_from_tree(
612
            self.root_transport.local_abspath('.'),
613
            self.transport.local_abspath("index"),
614
            store,
615
            None if commit_id == ZERO_SHA else store[commit_id].tree)
0.302.2 by Jelmer Vernooij
Use from_branch.
616
        from .workingtree import GitWorkingTree
0.310.14 by Jelmer Vernooij
merge lightweight checkout-in-git support.
617
        index = self._git.open_index()
0.302.2 by Jelmer Vernooij
Use from_branch.
618
        wt = GitWorkingTree(self, repo, from_branch, index)
0.288.5 by Jelmer Vernooij
Fix working tree contruction.
619
        wt.set_last_revision(revision_id)
0.200.1721 by Jelmer Vernooij
Support passing in last revision.
620
        return wt
0.200.1015 by Jelmer Vernooij
Fix GitControlDir.find_repository().
621
0.200.1114 by Jelmer Vernooij
Properly raise exception when create_repository is called with shared=True
622
    def _find_or_create_repository(self, force_new_repo=None):
623
        return self.create_repository(shared=False)
624
0.200.1018 by Jelmer Vernooij
Fix use with new control dir API.
625
    def _find_creation_modes(self):
626
        """Determine the appropriate modes for files and directories.
627
628
        They're always set to be consistent with the base directory,
629
        assuming that this transport allows setting modes.
630
        """
631
        # TODO: Do we need or want an option (maybe a config setting) to turn
632
        # this off or override it for particular locations? -- mbp 20080512
633
        if self._mode_check_done:
634
            return
635
        self._mode_check_done = True
636
        try:
637
            st = self.transport.stat('.')
0.200.1116 by Jelmer Vernooij
Fix missing import.
638
        except bzr_errors.TransportNotPossible:
0.200.1018 by Jelmer Vernooij
Fix use with new control dir API.
639
            self._dir_mode = None
640
            self._file_mode = None
641
        else:
642
            # Check the directory mode, but also make sure the created
643
            # directories and files are read-write for this user. This is
644
            # mostly a workaround for filesystems which lie about being able to
645
            # write to a directory (cygwin & win32)
646
            if (st.st_mode & 07777 == 00000):
647
                # FTP allows stat but does not return dir/file modes
648
                self._dir_mode = None
649
                self._file_mode = None
650
            else:
651
                self._dir_mode = (st.st_mode & 07777) | 00700
652
                # Remove the sticky and execute bits for files
653
                self._file_mode = self._dir_mode & ~07111
654
655
    def _get_file_mode(self):
656
        """Return Unix mode for newly created files, or None.
657
        """
658
        if not self._mode_check_done:
659
            self._find_creation_modes()
660
        return self._file_mode
661
662
    def _get_dir_mode(self):
663
        """Return Unix mode for newly created directories, or None.
664
        """
665
        if not self._mode_check_done:
666
            self._find_creation_modes()
667
        return self._dir_mode
668
0.200.1487 by Jelmer Vernooij
Use peeling.
669
    def get_refs_container(self):
670
        return self._git.refs
0.200.1489 by Jelmer Vernooij
More fixes to peel handling.
671
672
    def get_peeled(self, ref):
673
        return self._git.get_peeled(ref)