/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/git/dir.py

  • Committer: Breezy landing bot
  • Author(s): Jelmer Vernooij
  • Date: 2019-10-13 17:31:55 UTC
  • mfrom: (7397.4.9 remove-unused)
  • Revision ID: breezy.the.bot@gmail.com-20191013173155-yoiokny4mknxb3um
Remove Tree.has_id.

Merged from https://code.launchpad.net/~jelmer/brz/remove-unused/+merge/373320

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2007 Canonical Ltd
 
2
# Copyright (C) 2010-2018 Jelmer Vernooij
 
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
17
 
 
18
"""An adapter between a Git control dir and a Bazaar ControlDir."""
 
19
 
 
20
from __future__ import absolute_import
 
21
 
 
22
from .. import (
 
23
    branch as _mod_branch,
 
24
    errors as brz_errors,
 
25
    trace,
 
26
    osutils,
 
27
    urlutils,
 
28
    )
 
29
from ..sixish import (
 
30
    PY3,
 
31
    viewitems,
 
32
    )
 
33
from ..transport import (
 
34
    do_catching_redirections,
 
35
    get_transport_from_path,
 
36
    )
 
37
 
 
38
from ..controldir import (
 
39
    BranchReferenceLoop,
 
40
    ControlDir,
 
41
    ControlDirFormat,
 
42
    format_registry,
 
43
    RepositoryAcquisitionPolicy,
 
44
    )
 
45
 
 
46
from .push import (
 
47
    GitPushResult,
 
48
    )
 
49
from .transportgit import (
 
50
    OBJECTDIR,
 
51
    TransportObjectStore,
 
52
    )
 
53
 
 
54
 
 
55
class GitDirConfig(object):
 
56
 
 
57
    def get_default_stack_on(self):
 
58
        return None
 
59
 
 
60
    def set_default_stack_on(self, value):
 
61
        raise brz_errors.BzrError("Cannot set configuration")
 
62
 
 
63
 
 
64
class GitControlDirFormat(ControlDirFormat):
 
65
 
 
66
    colocated_branches = True
 
67
    fixed_components = True
 
68
 
 
69
    def __eq__(self, other):
 
70
        return type(self) == type(other)
 
71
 
 
72
    def is_supported(self):
 
73
        return True
 
74
 
 
75
    def network_name(self):
 
76
        return b"git"
 
77
 
 
78
 
 
79
class UseExistingRepository(RepositoryAcquisitionPolicy):
 
80
    """A policy of reusing an existing repository"""
 
81
 
 
82
    def __init__(self, repository, stack_on=None, stack_on_pwd=None,
 
83
                 require_stacking=False):
 
84
        """Constructor.
 
85
 
 
86
        :param repository: The repository to use.
 
87
        :param stack_on: A location to stack on
 
88
        :param stack_on_pwd: If stack_on is relative, the location it is
 
89
            relative to.
 
90
        """
 
91
        super(UseExistingRepository, self).__init__(
 
92
            stack_on, stack_on_pwd, require_stacking)
 
93
        self._repository = repository
 
94
 
 
95
    def acquire_repository(self, make_working_trees=None, shared=False,
 
96
                           possible_transports=None):
 
97
        """Implementation of RepositoryAcquisitionPolicy.acquire_repository
 
98
 
 
99
        Returns an existing repository to use.
 
100
        """
 
101
        return self._repository, False
 
102
 
 
103
 
 
104
class GitDir(ControlDir):
 
105
    """An adapter to the '.git' dir used by git."""
 
106
 
 
107
    def is_supported(self):
 
108
        return True
 
109
 
 
110
    def can_convert_format(self):
 
111
        return False
 
112
 
 
113
    def break_lock(self):
 
114
        # There are no global locks, so nothing to break.
 
115
        raise NotImplementedError(self.break_lock)
 
116
 
 
117
    def cloning_metadir(self, stacked=False):
 
118
        return format_registry.make_controldir("git")
 
119
 
 
120
    def checkout_metadir(self, stacked=False):
 
121
        return format_registry.make_controldir("git")
 
122
 
 
123
    def _get_selected_ref(self, branch, ref=None):
 
124
        if ref is not None and branch is not None:
 
125
            raise brz_errors.BzrError("can't specify both ref and branch")
 
126
        if ref is not None:
 
127
            return ref
 
128
        if branch is not None:
 
129
            from .refs import branch_name_to_ref
 
130
            return branch_name_to_ref(branch)
 
131
        segment_parameters = getattr(
 
132
            self.user_transport, "get_segment_parameters", lambda: {})()
 
133
        ref = segment_parameters.get("ref")
 
134
        if ref is not None:
 
135
            return urlutils.unquote_to_bytes(ref)
 
136
        if branch is None and getattr(self, "_get_selected_branch", False):
 
137
            branch = self._get_selected_branch()
 
138
            if branch is not None:
 
139
                from .refs import branch_name_to_ref
 
140
                return branch_name_to_ref(branch)
 
141
        return b"HEAD"
 
142
 
 
143
    def get_config(self):
 
144
        return GitDirConfig()
 
145
 
 
146
    def _available_backup_name(self, base):
 
147
        return osutils.available_backup_name(base, self.root_transport.has)
 
148
 
 
149
    def sprout(self, url, revision_id=None, force_new_repo=False,
 
150
               recurse='down', possible_transports=None,
 
151
               accelerator_tree=None, hardlink=False, stacked=False,
 
152
               source_branch=None, create_tree_if_local=True):
 
153
        from ..repository import InterRepository
 
154
        from ..transport.local import LocalTransport
 
155
        from ..transport import get_transport
 
156
        target_transport = get_transport(url, possible_transports)
 
157
        target_transport.ensure_base()
 
158
        cloning_format = self.cloning_metadir()
 
159
        # Create/update the result branch
 
160
        try:
 
161
            result = ControlDir.open_from_transport(target_transport)
 
162
        except brz_errors.NotBranchError:
 
163
            result = cloning_format.initialize_on_transport(target_transport)
 
164
        source_branch = self.open_branch()
 
165
        source_repository = self.find_repository()
 
166
        try:
 
167
            result_repo = result.find_repository()
 
168
        except brz_errors.NoRepositoryPresent:
 
169
            result_repo = result.create_repository()
 
170
        if stacked:
 
171
            raise _mod_branch.UnstackableBranchFormat(
 
172
                self._format, self.user_url)
 
173
        interrepo = InterRepository.get(source_repository, result_repo)
 
174
 
 
175
        if revision_id is not None:
 
176
            determine_wants = interrepo.get_determine_wants_revids(
 
177
                [revision_id], include_tags=True)
 
178
        else:
 
179
            determine_wants = interrepo.determine_wants_all
 
180
        interrepo.fetch_objects(determine_wants=determine_wants,
 
181
                                mapping=source_branch.mapping)
 
182
        result_branch = source_branch.sprout(
 
183
            result, revision_id=revision_id, repository=result_repo)
 
184
        if (create_tree_if_local and
 
185
            result.open_branch(name="").name == result_branch.name and
 
186
            isinstance(target_transport, LocalTransport) and
 
187
                (result_repo is None or result_repo.make_working_trees())):
 
188
            result.create_workingtree(
 
189
                accelerator_tree=accelerator_tree,
 
190
                hardlink=hardlink, from_branch=result_branch)
 
191
        return result
 
192
 
 
193
    def clone_on_transport(self, transport, revision_id=None,
 
194
                           force_new_repo=False, preserve_stacking=False,
 
195
                           stacked_on=None, create_prefix=False,
 
196
                           use_existing_dir=True, no_tree=False):
 
197
        """See ControlDir.clone_on_transport."""
 
198
        from ..repository import InterRepository
 
199
        from .mapping import default_mapping
 
200
        if stacked_on is not None:
 
201
            raise _mod_branch.UnstackableBranchFormat(
 
202
                self._format, self.user_url)
 
203
        if no_tree:
 
204
            format = BareLocalGitControlDirFormat()
 
205
        else:
 
206
            format = LocalGitControlDirFormat()
 
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)
 
212
        target_repo = target_controldir.find_repository()
 
213
        target_git_repo = target_repo._git
 
214
        source_repo = self.find_repository()
 
215
        interrepo = InterRepository.get(source_repo, target_repo)
 
216
        if revision_id is not None:
 
217
            determine_wants = interrepo.get_determine_wants_revids(
 
218
                [revision_id], include_tags=True)
 
219
        else:
 
220
            determine_wants = interrepo.determine_wants_all
 
221
        (pack_hint, _, refs) = interrepo.fetch_objects(determine_wants,
 
222
                                                       mapping=default_mapping)
 
223
        for name, val in viewitems(refs):
 
224
            target_git_repo.refs[name] = val
 
225
        result_dir = self.__class__(transport, target_git_repo, format)
 
226
        if revision_id is not None:
 
227
            result_dir.open_branch().set_last_revision(revision_id)
 
228
        try:
 
229
            # Cheaper to check if the target is not local, than to try making
 
230
            # the tree and fail.
 
231
            result_dir.root_transport.local_abspath('.')
 
232
            if result_dir.open_repository().make_working_trees():
 
233
                self.open_workingtree().clone(
 
234
                    result_dir, revision_id=revision_id)
 
235
        except (brz_errors.NoWorkingTree, brz_errors.NotLocalUrl):
 
236
            pass
 
237
 
 
238
        return result_dir
 
239
 
 
240
    def find_repository(self):
 
241
        """Find the repository that should be used.
 
242
 
 
243
        This does not require a branch as we use it to find the repo for
 
244
        new branches as well as to hook existing branches up to their
 
245
        repository.
 
246
        """
 
247
        return self._gitrepository_class(self._find_commondir())
 
248
 
 
249
    def get_refs_container(self):
 
250
        """Retrieve the refs container.
 
251
        """
 
252
        raise NotImplementedError(self.get_refs_container)
 
253
 
 
254
    def determine_repository_policy(self, force_new_repo=False, stack_on=None,
 
255
                                    stack_on_pwd=None, require_stacking=False):
 
256
        """Return an object representing a policy to use.
 
257
 
 
258
        This controls whether a new repository is created, and the format of
 
259
        that repository, or some existing shared repository used instead.
 
260
 
 
261
        If stack_on is supplied, will not seek a containing shared repo.
 
262
 
 
263
        :param force_new_repo: If True, require a new repository to be created.
 
264
        :param stack_on: If supplied, the location to stack on.  If not
 
265
            supplied, a default_stack_on location may be used.
 
266
        :param stack_on_pwd: If stack_on is relative, the location it is
 
267
            relative to.
 
268
        """
 
269
        return UseExistingRepository(self.find_repository())
 
270
 
 
271
    def get_branches(self):
 
272
        from .refs import ref_to_branch_name
 
273
        ret = {}
 
274
        for ref in self.get_refs_container().keys():
 
275
            try:
 
276
                branch_name = ref_to_branch_name(ref)
 
277
            except UnicodeDecodeError:
 
278
                trace.warning("Ignoring branch %r with unicode error ref", ref)
 
279
                continue
 
280
            except ValueError:
 
281
                continue
 
282
            ret[branch_name] = self.open_branch(ref=ref)
 
283
        return ret
 
284
 
 
285
    def list_branches(self):
 
286
        return list(self.get_branches().values())
 
287
 
 
288
    def push_branch(self, source, revision_id=None, overwrite=False,
 
289
                    remember=False, create_prefix=False, lossy=False,
 
290
                    name=None):
 
291
        """Push the source branch into this ControlDir."""
 
292
        push_result = GitPushResult()
 
293
        push_result.workingtree_updated = None
 
294
        push_result.master_branch = None
 
295
        push_result.source_branch = source
 
296
        push_result.stacked_on = None
 
297
        from .branch import GitBranch
 
298
        if isinstance(source, GitBranch) and lossy:
 
299
            raise brz_errors.LossyPushToSameVCS(source.controldir, self)
 
300
        target = self.open_branch(name, nascent_ok=True)
 
301
        push_result.branch_push_result = source.push(
 
302
            target, overwrite=overwrite, stop_revision=revision_id,
 
303
            lossy=lossy)
 
304
        push_result.new_revid = push_result.branch_push_result.new_revid
 
305
        push_result.old_revid = push_result.branch_push_result.old_revid
 
306
        try:
 
307
            wt = self.open_workingtree()
 
308
        except brz_errors.NoWorkingTree:
 
309
            push_result.workingtree_updated = None
 
310
        else:
 
311
            if self.open_branch(name="").name == target.name:
 
312
                wt._update_git_tree(
 
313
                    old_revision=push_result.old_revid,
 
314
                    new_revision=push_result.new_revid)
 
315
                push_result.workingtree_updated = True
 
316
            else:
 
317
                push_result.workingtree_updated = False
 
318
        push_result.target_branch = target
 
319
        if source.get_push_location() is None or remember:
 
320
            source.set_push_location(push_result.target_branch.base)
 
321
        return push_result
 
322
 
 
323
 
 
324
class LocalGitControlDirFormat(GitControlDirFormat):
 
325
    """The .git directory control format."""
 
326
 
 
327
    bare = False
 
328
 
 
329
    @classmethod
 
330
    def _known_formats(self):
 
331
        return set([LocalGitControlDirFormat()])
 
332
 
 
333
    @property
 
334
    def repository_format(self):
 
335
        from .repository import GitRepositoryFormat
 
336
        return GitRepositoryFormat()
 
337
 
 
338
    @property
 
339
    def workingtree_format(self):
 
340
        from .workingtree import GitWorkingTreeFormat
 
341
        return GitWorkingTreeFormat()
 
342
 
 
343
    def get_branch_format(self):
 
344
        from .branch import LocalGitBranchFormat
 
345
        return LocalGitBranchFormat()
 
346
 
 
347
    def open(self, transport, _found=None):
 
348
        """Open this directory.
 
349
 
 
350
        """
 
351
        from .transportgit import TransportRepo
 
352
 
 
353
        def _open(transport):
 
354
            try:
 
355
                return TransportRepo(transport, self.bare,
 
356
                                     refs_text=getattr(self, "_refs_text", None))
 
357
            except ValueError as e:
 
358
                if e.args == ('Expected file to start with \'gitdir: \'', ):
 
359
                    raise brz_errors.NotBranchError(path=transport.base)
 
360
                raise
 
361
 
 
362
        def redirected(transport, e, redirection_notice):
 
363
            trace.note(redirection_notice)
 
364
            return transport._redirected_to(e.source, e.target)
 
365
        gitrepo = do_catching_redirections(_open, transport, redirected)
 
366
        if not gitrepo._controltransport.has('HEAD'):
 
367
            raise brz_errors.NotBranchError(path=transport.base)
 
368
        return LocalGitDir(transport, gitrepo, self)
 
369
 
 
370
    def get_format_description(self):
 
371
        return "Local Git Repository"
 
372
 
 
373
    def initialize_on_transport(self, transport):
 
374
        from .transportgit import TransportRepo
 
375
        git_repo = TransportRepo.init(transport, bare=self.bare)
 
376
        return LocalGitDir(transport, git_repo, self)
 
377
 
 
378
    def initialize_on_transport_ex(self, transport, use_existing_dir=False,
 
379
                                   create_prefix=False, force_new_repo=False,
 
380
                                   stacked_on=None,
 
381
                                   stack_on_pwd=None, repo_format_name=None,
 
382
                                   make_working_trees=None,
 
383
                                   shared_repo=False, vfs_only=False):
 
384
        def make_directory(transport):
 
385
            transport.mkdir('.')
 
386
            return transport
 
387
 
 
388
        def redirected(transport, e, redirection_notice):
 
389
            trace.note(redirection_notice)
 
390
            return transport._redirected_to(e.source, e.target)
 
391
        try:
 
392
            transport = do_catching_redirections(
 
393
                make_directory, transport, redirected)
 
394
        except brz_errors.FileExists:
 
395
            if not use_existing_dir:
 
396
                raise
 
397
        except brz_errors.NoSuchFile:
 
398
            if not create_prefix:
 
399
                raise
 
400
            transport.create_prefix()
 
401
        controldir = self.initialize_on_transport(transport)
 
402
        if repo_format_name:
 
403
            result_repo = controldir.find_repository()
 
404
            repository_policy = UseExistingRepository(result_repo)
 
405
            result_repo.lock_write()
 
406
        else:
 
407
            result_repo = None
 
408
            repository_policy = None
 
409
        return (result_repo, controldir, False,
 
410
                repository_policy)
 
411
 
 
412
    def is_supported(self):
 
413
        return True
 
414
 
 
415
    def supports_transport(self, transport):
 
416
        try:
 
417
            external_url = transport.external_url()
 
418
        except brz_errors.InProcessTransport:
 
419
            raise brz_errors.NotBranchError(path=transport.base)
 
420
        return external_url.startswith("file:")
 
421
 
 
422
    def is_control_filename(self, filename):
 
423
        return (filename == '.git'
 
424
                or filename.startswith('.git/')
 
425
                or filename.startswith('.git\\'))
 
426
 
 
427
 
 
428
class BareLocalGitControlDirFormat(LocalGitControlDirFormat):
 
429
 
 
430
    bare = True
 
431
    supports_workingtrees = False
 
432
 
 
433
    def get_format_description(self):
 
434
        return "Local Git Repository (bare)"
 
435
 
 
436
    def is_control_filename(self, filename):
 
437
        return False
 
438
 
 
439
 
 
440
class LocalGitDir(GitDir):
 
441
    """An adapter to the '.git' dir used by git."""
 
442
 
 
443
    def _get_gitrepository_class(self):
 
444
        from .repository import LocalGitRepository
 
445
        return LocalGitRepository
 
446
 
 
447
    def __repr__(self):
 
448
        return "<%s at %r>" % (
 
449
            self.__class__.__name__, self.root_transport.base)
 
450
 
 
451
    _gitrepository_class = property(_get_gitrepository_class)
 
452
 
 
453
    @property
 
454
    def user_transport(self):
 
455
        return self.root_transport
 
456
 
 
457
    @property
 
458
    def control_transport(self):
 
459
        return self._git._controltransport
 
460
 
 
461
    def __init__(self, transport, gitrepo, format):
 
462
        self._format = format
 
463
        self.root_transport = transport
 
464
        self._mode_check_done = False
 
465
        self._git = gitrepo
 
466
        if gitrepo.bare:
 
467
            self.transport = transport
 
468
        else:
 
469
            self.transport = transport.clone('.git')
 
470
        self._mode_check_done = None
 
471
 
 
472
    def _get_symref(self, ref):
 
473
        ref_chain, unused_sha = self._git.refs.follow(ref)
 
474
        if len(ref_chain) == 1:
 
475
            return None
 
476
        return ref_chain[1]
 
477
 
 
478
    def set_branch_reference(self, target_branch, name=None):
 
479
        ref = self._get_selected_ref(name)
 
480
        target_transport = target_branch.controldir.control_transport
 
481
        if self.control_transport.base == target_transport.base:
 
482
            if ref == target_branch.ref:
 
483
                raise BranchReferenceLoop(target_branch)
 
484
            self._git.refs.set_symbolic_ref(ref, target_branch.ref)
 
485
        else:
 
486
            try:
 
487
                target_path = (
 
488
                    target_branch.controldir.control_transport.local_abspath(
 
489
                        '.'))
 
490
            except brz_errors.NotLocalUrl:
 
491
                raise brz_errors.IncompatibleFormat(
 
492
                    target_branch._format, self._format)
 
493
            # TODO(jelmer): Do some consistency checking across branches..
 
494
            self.control_transport.put_bytes(
 
495
                'commondir', target_path.encode('utf-8'))
 
496
            # TODO(jelmer): Urgh, avoid mucking about with internals.
 
497
            self._git._commontransport = (
 
498
                target_branch.repository._git._commontransport.clone())
 
499
            self._git.object_store = TransportObjectStore(
 
500
                self._git._commontransport.clone(OBJECTDIR))
 
501
            self._git.refs.transport = self._git._commontransport
 
502
            target_ref_chain, unused_sha = (
 
503
                target_branch.controldir._git.refs.follow(target_branch.ref))
 
504
            for target_ref in target_ref_chain:
 
505
                if target_ref == b'HEAD':
 
506
                    continue
 
507
                break
 
508
            else:
 
509
                # Can't create a reference to something that is not a in a repository.
 
510
                raise brz_errors.IncompatibleFormat(
 
511
                    self.set_branch_reference, self)
 
512
            self._git.refs.set_symbolic_ref(ref, target_ref)
 
513
 
 
514
    def get_branch_reference(self, name=None):
 
515
        ref = self._get_selected_ref(name)
 
516
        target_ref = self._get_symref(ref)
 
517
        if target_ref is not None:
 
518
            from .refs import ref_to_branch_name
 
519
            try:
 
520
                branch_name = ref_to_branch_name(target_ref)
 
521
            except ValueError:
 
522
                params = {'ref': urlutils.quote(
 
523
                    target_ref.decode('utf-8'), '')}
 
524
            else:
 
525
                if branch_name != '':
 
526
                    params = {'branch': urlutils.quote(branch_name, '')}
 
527
                else:
 
528
                    params = {}
 
529
            try:
 
530
                commondir = self.control_transport.get_bytes('commondir')
 
531
            except brz_errors.NoSuchFile:
 
532
                base_url = self.user_url.rstrip('/')
 
533
            else:
 
534
                base_url = urlutils.local_path_to_url(
 
535
                    commondir.decode(osutils._fs_enc)).rstrip('/.git/') + '/'
 
536
            if not PY3:
 
537
                params = {k: v.encode('utf-8') for (k, v) in viewitems(params)}
 
538
            return urlutils.join_segment_parameters(base_url, params)
 
539
        return None
 
540
 
 
541
    def find_branch_format(self, name=None):
 
542
        from .branch import (
 
543
            LocalGitBranchFormat,
 
544
            )
 
545
        return LocalGitBranchFormat()
 
546
 
 
547
    def get_branch_transport(self, branch_format, name=None):
 
548
        if branch_format is None:
 
549
            return self.transport
 
550
        if isinstance(branch_format, LocalGitControlDirFormat):
 
551
            return self.transport
 
552
        raise brz_errors.IncompatibleFormat(branch_format, self._format)
 
553
 
 
554
    def get_repository_transport(self, format):
 
555
        if format is None:
 
556
            return self.transport
 
557
        if isinstance(format, LocalGitControlDirFormat):
 
558
            return self.transport
 
559
        raise brz_errors.IncompatibleFormat(format, self._format)
 
560
 
 
561
    def get_workingtree_transport(self, format):
 
562
        if format is None:
 
563
            return self.transport
 
564
        if isinstance(format, LocalGitControlDirFormat):
 
565
            return self.transport
 
566
        raise brz_errors.IncompatibleFormat(format, self._format)
 
567
 
 
568
    def open_branch(self, name=None, unsupported=False, ignore_fallbacks=None,
 
569
                    ref=None, possible_transports=None, nascent_ok=False):
 
570
        """'create' a branch for this dir."""
 
571
        repo = self.find_repository()
 
572
        from .branch import LocalGitBranch
 
573
        ref = self._get_selected_ref(name, ref)
 
574
        if not nascent_ok and ref not in self._git.refs:
 
575
            raise brz_errors.NotBranchError(
 
576
                self.root_transport.base, controldir=self)
 
577
        ref_chain, unused_sha = self._git.refs.follow(ref)
 
578
        if ref_chain[-1] == b'HEAD':
 
579
            controldir = self
 
580
        else:
 
581
            controldir = self._find_commondir()
 
582
        return LocalGitBranch(controldir, repo, ref_chain[-1])
 
583
 
 
584
    def destroy_branch(self, name=None):
 
585
        refname = self._get_selected_ref(name)
 
586
        if refname == b'HEAD':
 
587
            # HEAD can't be removed
 
588
            raise brz_errors.UnsupportedOperation(
 
589
                self.destroy_branch, self)
 
590
        try:
 
591
            del self._git.refs[refname]
 
592
        except KeyError:
 
593
            raise brz_errors.NotBranchError(
 
594
                self.root_transport.base, controldir=self)
 
595
 
 
596
    def destroy_repository(self):
 
597
        raise brz_errors.UnsupportedOperation(self.destroy_repository, self)
 
598
 
 
599
    def destroy_workingtree(self):
 
600
        raise brz_errors.UnsupportedOperation(self.destroy_workingtree, self)
 
601
 
 
602
    def destroy_workingtree_metadata(self):
 
603
        raise brz_errors.UnsupportedOperation(
 
604
            self.destroy_workingtree_metadata, self)
 
605
 
 
606
    def needs_format_conversion(self, format=None):
 
607
        return not isinstance(self._format, format.__class__)
 
608
 
 
609
    def open_repository(self):
 
610
        """'open' a repository for this dir."""
 
611
        if self.control_transport.has('commondir'):
 
612
            raise brz_errors.NoRepositoryPresent(self)
 
613
        return self._gitrepository_class(self)
 
614
 
 
615
    def has_workingtree(self):
 
616
        return not self._git.bare
 
617
 
 
618
    def open_workingtree(self, recommend_upgrade=True, unsupported=False):
 
619
        if not self._git.bare:
 
620
            repo = self.find_repository()
 
621
            from .workingtree import GitWorkingTree
 
622
            branch = self.open_branch(ref=b'HEAD', nascent_ok=True)
 
623
            return GitWorkingTree(self, repo, branch)
 
624
        loc = urlutils.unescape_for_display(self.root_transport.base, 'ascii')
 
625
        raise brz_errors.NoWorkingTree(loc)
 
626
 
 
627
    def create_repository(self, shared=False):
 
628
        from .repository import GitRepositoryFormat
 
629
        if shared:
 
630
            raise brz_errors.IncompatibleFormat(
 
631
                GitRepositoryFormat(), self._format)
 
632
        return self.find_repository()
 
633
 
 
634
    def create_branch(self, name=None, repository=None,
 
635
                      append_revisions_only=None, ref=None):
 
636
        refname = self._get_selected_ref(name, ref)
 
637
        if refname != b'HEAD' and refname in self._git.refs:
 
638
            raise brz_errors.AlreadyBranchError(self.user_url)
 
639
        repo = self.open_repository()
 
640
        if refname in self._git.refs:
 
641
            ref_chain, unused_sha = self._git.refs.follow(
 
642
                self._get_selected_ref(None))
 
643
            if ref_chain[0] == b'HEAD':
 
644
                refname = ref_chain[1]
 
645
        from .branch import LocalGitBranch
 
646
        branch = LocalGitBranch(self, repo, refname)
 
647
        if append_revisions_only:
 
648
            branch.set_append_revisions_only(append_revisions_only)
 
649
        return branch
 
650
 
 
651
    def backup_bzrdir(self):
 
652
        if not self._git.bare:
 
653
            self.root_transport.copy_tree(".git", ".git.backup")
 
654
            return (self.root_transport.abspath(".git"),
 
655
                    self.root_transport.abspath(".git.backup"))
 
656
        else:
 
657
            basename = urlutils.basename(self.root_transport.base)
 
658
            parent = self.root_transport.clone('..')
 
659
            parent.copy_tree(basename, basename + ".backup")
 
660
 
 
661
    def create_workingtree(self, revision_id=None, from_branch=None,
 
662
                           accelerator_tree=None, hardlink=False):
 
663
        if self._git.bare:
 
664
            raise brz_errors.UnsupportedOperation(
 
665
                self.create_workingtree, self)
 
666
        if from_branch is None:
 
667
            from_branch = self.open_branch(nascent_ok=True)
 
668
        if revision_id is None:
 
669
            revision_id = from_branch.last_revision()
 
670
        repo = self.find_repository()
 
671
        from .workingtree import GitWorkingTree
 
672
        wt = GitWorkingTree(self, repo, from_branch)
 
673
        wt.set_last_revision(revision_id)
 
674
        wt._build_checkout_with_index()
 
675
        return wt
 
676
 
 
677
    def _find_or_create_repository(self, force_new_repo=None):
 
678
        return self.create_repository(shared=False)
 
679
 
 
680
    def _find_creation_modes(self):
 
681
        """Determine the appropriate modes for files and directories.
 
682
 
 
683
        They're always set to be consistent with the base directory,
 
684
        assuming that this transport allows setting modes.
 
685
        """
 
686
        # TODO: Do we need or want an option (maybe a config setting) to turn
 
687
        # this off or override it for particular locations? -- mbp 20080512
 
688
        if self._mode_check_done:
 
689
            return
 
690
        self._mode_check_done = True
 
691
        try:
 
692
            st = self.transport.stat('.')
 
693
        except brz_errors.TransportNotPossible:
 
694
            self._dir_mode = None
 
695
            self._file_mode = None
 
696
        else:
 
697
            # Check the directory mode, but also make sure the created
 
698
            # directories and files are read-write for this user. This is
 
699
            # mostly a workaround for filesystems which lie about being able to
 
700
            # write to a directory (cygwin & win32)
 
701
            if (st.st_mode & 0o7777 == 0o0000):
 
702
                # FTP allows stat but does not return dir/file modes
 
703
                self._dir_mode = None
 
704
                self._file_mode = None
 
705
            else:
 
706
                self._dir_mode = (st.st_mode & 0o7777) | 0o0700
 
707
                # Remove the sticky and execute bits for files
 
708
                self._file_mode = self._dir_mode & ~0o7111
 
709
 
 
710
    def _get_file_mode(self):
 
711
        """Return Unix mode for newly created files, or None.
 
712
        """
 
713
        if not self._mode_check_done:
 
714
            self._find_creation_modes()
 
715
        return self._file_mode
 
716
 
 
717
    def _get_dir_mode(self):
 
718
        """Return Unix mode for newly created directories, or None.
 
719
        """
 
720
        if not self._mode_check_done:
 
721
            self._find_creation_modes()
 
722
        return self._dir_mode
 
723
 
 
724
    def get_refs_container(self):
 
725
        return self._git.refs
 
726
 
 
727
    def get_peeled(self, ref):
 
728
        return self._git.get_peeled(ref)
 
729
 
 
730
    def _find_commondir(self):
 
731
        try:
 
732
            commondir = self.control_transport.get_bytes('commondir')
 
733
        except brz_errors.NoSuchFile:
 
734
            return self
 
735
        else:
 
736
            commondir = commondir.rstrip(b'/.git/').decode(osutils._fs_enc)
 
737
            return ControlDir.open_from_transport(
 
738
                get_transport_from_path(commondir))