/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 dir.py

  • Committer: Jelmer Vernooij
  • Date: 2018-03-28 01:51:03 UTC
  • mto: (0.200.1902 work)
  • mto: This revision was merged to the branch mainline in revision 6960.
  • Revision ID: jelmer@jelmer.uk-20180328015103-1zev7bdm87yzo90m
Support reading .git files.

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
import urllib
 
23
 
 
24
from ... import (
 
25
    branch as _mod_branch,
 
26
    errors as bzr_errors,
 
27
    trace,
 
28
    osutils,
 
29
    revision as _mod_revision,
 
30
    urlutils,
 
31
    )
 
32
from ...controldir import RepositoryAcquisitionPolicy
 
33
from ...transport import (
 
34
    do_catching_redirections,
 
35
    get_transport_from_path,
 
36
    )
 
37
 
 
38
from ...controldir import (
 
39
    ControlDir,
 
40
    ControlDirFormat,
 
41
    format_registry,
 
42
    RepositoryAcquisitionPolicy,
 
43
    )
 
44
 
 
45
from .transportgit import (
 
46
    OBJECTDIR,
 
47
    TransportObjectStore,
 
48
    )
 
49
 
 
50
 
 
51
class GitDirConfig(object):
 
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
 
 
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
 
 
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
 
 
100
class GitDir(ControlDir):
 
101
    """An adapter to the '.git' dir used by git."""
 
102
 
 
103
    def is_supported(self):
 
104
        return True
 
105
 
 
106
    def can_convert_format(self):
 
107
        return False
 
108
 
 
109
    def break_lock(self):
 
110
        # There are no global locks, so nothing to break.
 
111
        raise NotImplementedError(self.break_lock)
 
112
 
 
113
    def cloning_metadir(self, stacked=False):
 
114
        return format_registry.make_controldir("git")
 
115
 
 
116
    def checkout_metadir(self, stacked=False):
 
117
        return format_registry.make_controldir("git")
 
118
 
 
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
 
124
        if branch is not None:
 
125
            from .refs import branch_name_to_ref
 
126
            return branch_name_to_ref(branch)
 
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)
 
132
        if branch is None and getattr(self, "_get_selected_branch", False):
 
133
            branch = self._get_selected_branch()
 
134
            if branch is not None:
 
135
                from .refs import branch_name_to_ref
 
136
                return branch_name_to_ref(branch)
 
137
        return b"HEAD"
 
138
 
 
139
    def get_config(self):
 
140
        return GitDirConfig()
 
141
 
 
142
    def _available_backup_name(self, base):
 
143
        return osutils.available_backup_name(base, self.root_transport.has)
 
144
 
 
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):
 
149
        from ...repository import InterRepository
 
150
        from ...transport.local import LocalTransport
 
151
        from ...transport import get_transport
 
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
 
156
        try:
 
157
            result = ControlDir.open_from_transport(target_transport)
 
158
        except bzr_errors.NotBranchError:
 
159
            result = cloning_format.initialize_on_transport(target_transport)
 
160
        source_branch = self.open_branch()
 
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:
 
170
            raise _mod_branch.UnstackableBranchFormat(self._format, self.user_url)
 
171
        interrepo = InterRepository.get(source_repository, result_repo)
 
172
 
 
173
        if revision_id is not None:
 
174
            determine_wants = interrepo.get_determine_wants_revids(
 
175
                [revision_id], include_tags=True)
 
176
        else:
 
177
            determine_wants = interrepo.determine_wants_all
 
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)
 
182
        if (create_tree_if_local
 
183
            and isinstance(target_transport, LocalTransport)
 
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)
 
187
        return result
 
188
 
 
189
    def clone_on_transport(self, transport, revision_id=None,
 
190
        force_new_repo=False, preserve_stacking=False, stacked_on=None,
 
191
        create_prefix=False, use_existing_dir=True, no_tree=False):
 
192
        """See ControlDir.clone_on_transport."""
 
193
        from ...repository import InterRepository
 
194
        from .mapping import default_mapping
 
195
        if stacked_on is not None:
 
196
            raise _mod_branch.UnstackableBranchFormat(self._format, self.user_url)
 
197
        if no_tree:
 
198
            format = BareLocalGitControlDirFormat()
 
199
        else:
 
200
            format = LocalGitControlDirFormat()
 
201
        (target_repo, target_controldir, stacking,
 
202
                repo_policy) = format.initialize_on_transport_ex(
 
203
                        transport, use_existing_dir=use_existing_dir,
 
204
                        create_prefix=create_prefix,
 
205
                        force_new_repo=force_new_repo)
 
206
        target_repo = target_controldir.find_repository()
 
207
        target_git_repo = target_repo._git
 
208
        source_repo = self.find_repository()
 
209
        source_git_repo = source_repo._git
 
210
        interrepo = InterRepository.get(source_repo, target_repo)
 
211
        if revision_id is not None:
 
212
            determine_wants = interrepo.get_determine_wants_revids([revision_id], include_tags=True)
 
213
        else:
 
214
            determine_wants = interrepo.determine_wants_all
 
215
        (pack_hint, _, refs) = interrepo.fetch_objects(determine_wants,
 
216
            mapping=default_mapping)
 
217
        for name, val in refs.iteritems():
 
218
            target_git_repo.refs[name] = val
 
219
        result_dir = self.__class__(transport, target_git_repo, format)
 
220
        if revision_id is not None:
 
221
            result_dir.open_branch().set_last_revision(revision_id)
 
222
        return result_dir
 
223
 
 
224
    def _find_commondir(self):
 
225
        try:
 
226
            commondir = self.control_transport.get_bytes('commondir')
 
227
        except bzr_errors.NoSuchFile:
 
228
            return self
 
229
        else:
 
230
            commondir = commondir.rstrip('/.git/')
 
231
            return ControlDir.open_from_transport(get_transport_from_path(commondir))
 
232
 
 
233
    def find_repository(self):
 
234
        """Find the repository that should be used.
 
235
 
 
236
        This does not require a branch as we use it to find the repo for
 
237
        new branches as well as to hook existing branches up to their
 
238
        repository.
 
239
        """
 
240
        return self._gitrepository_class(self._find_commondir())
 
241
 
 
242
    def get_refs_container(self):
 
243
        """Retrieve the refs container.
 
244
        """
 
245
        raise NotImplementedError(self.get_refs_container)
 
246
 
 
247
    def determine_repository_policy(self, force_new_repo=False, stack_on=None,
 
248
                                    stack_on_pwd=None, require_stacking=False):
 
249
        """Return an object representing a policy to use.
 
250
 
 
251
        This controls whether a new repository is created, and the format of
 
252
        that repository, or some existing shared repository used instead.
 
253
 
 
254
        If stack_on is supplied, will not seek a containing shared repo.
 
255
 
 
256
        :param force_new_repo: If True, require a new repository to be created.
 
257
        :param stack_on: If supplied, the location to stack on.  If not
 
258
            supplied, a default_stack_on location may be used.
 
259
        :param stack_on_pwd: If stack_on is relative, the location it is
 
260
            relative to.
 
261
        """
 
262
        return UseExistingRepository(self.find_repository())
 
263
 
 
264
    def get_branches(self):
 
265
        from .refs import ref_to_branch_name
 
266
        ret = {}
 
267
        for ref in self.get_refs_container().keys():
 
268
            try:
 
269
                branch_name = ref_to_branch_name(ref)
 
270
            except ValueError:
 
271
                continue
 
272
            except UnicodeDecodeError:
 
273
                trace.warning("Ignoring branch %r with unicode error ref", ref)
 
274
                continue
 
275
            ret[branch_name] = self.open_branch(ref=ref)
 
276
        return ret
 
277
 
 
278
    def list_branches(self):
 
279
        return self.get_branches().values()
 
280
 
 
281
 
 
282
class LocalGitControlDirFormat(GitControlDirFormat):
 
283
    """The .git directory control format."""
 
284
 
 
285
    bare = False
 
286
 
 
287
    @classmethod
 
288
    def _known_formats(self):
 
289
        return set([LocalGitControlDirFormat()])
 
290
 
 
291
    @property
 
292
    def repository_format(self):
 
293
        from .repository import GitRepositoryFormat
 
294
        return GitRepositoryFormat()
 
295
 
 
296
    @property
 
297
    def workingtree_format(self):
 
298
        from .workingtree import GitWorkingTreeFormat
 
299
        return GitWorkingTreeFormat()
 
300
 
 
301
    def get_branch_format(self):
 
302
        from .branch import LocalGitBranchFormat
 
303
        return LocalGitBranchFormat()
 
304
 
 
305
    def open(self, transport, _found=None):
 
306
        """Open this directory.
 
307
 
 
308
        """
 
309
        from .transportgit import TransportRepo
 
310
        gitrepo = TransportRepo(transport, self.bare,
 
311
                refs_text=getattr(self, "_refs_text", None))
 
312
        if not gitrepo._controltransport.has('HEAD'):
 
313
            raise bzr_errors.NotBranchError(path=transport.base)
 
314
        return LocalGitDir(transport, gitrepo, self)
 
315
 
 
316
    def get_format_description(self):
 
317
        return "Local Git Repository"
 
318
 
 
319
    def initialize_on_transport(self, transport):
 
320
        from .transportgit import TransportRepo
 
321
        repo = TransportRepo.init(transport, bare=self.bare)
 
322
        return self.open(transport)
 
323
 
 
324
    def initialize_on_transport_ex(self, transport, use_existing_dir=False,
 
325
        create_prefix=False, force_new_repo=False, stacked_on=None,
 
326
        stack_on_pwd=None, repo_format_name=None, make_working_trees=None,
 
327
        shared_repo=False, vfs_only=False):
 
328
        def make_directory(transport):
 
329
            transport.mkdir('.')
 
330
            return transport
 
331
        def redirected(transport, e, redirection_notice):
 
332
            trace.note(redirection_notice)
 
333
            return transport._redirected_to(e.source, e.target)
 
334
        try:
 
335
            transport = do_catching_redirections(make_directory, transport,
 
336
                redirected)
 
337
        except bzr_errors.FileExists:
 
338
            if not use_existing_dir:
 
339
                raise
 
340
        except bzr_errors.NoSuchFile:
 
341
            if not create_prefix:
 
342
                raise
 
343
            transport.create_prefix()
 
344
        controldir = self.initialize_on_transport(transport)
 
345
        if repo_format_name:
 
346
            result_repo = controldir.find_repository()
 
347
            repository_policy = UseExistingRepository(result_repo)
 
348
            result_repo.lock_write()
 
349
        else:
 
350
            result_repo = None
 
351
            repository_policy = None
 
352
        return (result_repo, controldir, False,
 
353
                repository_policy)
 
354
 
 
355
    def is_supported(self):
 
356
        return True
 
357
 
 
358
    def supports_transport(self, transport):
 
359
        try:
 
360
            external_url = transport.external_url()
 
361
        except bzr_errors.InProcessTransport:
 
362
            raise bzr_errors.NotBranchError(path=transport.base)
 
363
        return external_url.startswith("file:")
 
364
 
 
365
 
 
366
class BareLocalGitControlDirFormat(LocalGitControlDirFormat):
 
367
 
 
368
    bare = True
 
369
    supports_workingtrees = False
 
370
 
 
371
    def get_format_description(self):
 
372
        return "Local Git Repository (bare)"
 
373
 
 
374
 
 
375
class LocalGitDir(GitDir):
 
376
    """An adapter to the '.git' dir used by git."""
 
377
 
 
378
    def _get_gitrepository_class(self):
 
379
        from .repository import LocalGitRepository
 
380
        return LocalGitRepository
 
381
 
 
382
    def __repr__(self):
 
383
        return "<%s at %r>" % (
 
384
            self.__class__.__name__, self.root_transport.base)
 
385
 
 
386
    _gitrepository_class = property(_get_gitrepository_class)
 
387
 
 
388
    @property
 
389
    def user_transport(self):
 
390
        return self.root_transport
 
391
 
 
392
    @property
 
393
    def control_transport(self):
 
394
        return self._git._controltransport
 
395
 
 
396
    def __init__(self, transport, gitrepo, format):
 
397
        self._format = format
 
398
        self.root_transport = transport
 
399
        self._mode_check_done = False
 
400
        self._git = gitrepo
 
401
        if gitrepo.bare:
 
402
            self.transport = transport
 
403
        else:
 
404
            self.transport = transport.clone('.git')
 
405
        self._mode_check_done = None
 
406
 
 
407
    def is_control_filename(self, filename):
 
408
        return (filename == '.git' or
 
409
                filename.startswith('.git/') or
 
410
                filename.startswith('.git\\'))
 
411
 
 
412
    def _get_symref(self, ref):
 
413
        from dulwich.repo import SYMREF
 
414
        ref_chain, unused_sha = self._git.refs.follow(ref)
 
415
        if len(ref_chain) == 1:
 
416
            return None
 
417
        return ref_chain[1]
 
418
 
 
419
    def set_branch_reference(self, target_branch, name=None):
 
420
        ref = self._get_selected_ref(name)
 
421
        if self.control_transport.base == target_branch.controldir.control_transport.base:
 
422
            self._git.refs.set_symbolic_ref(ref, target_branch.ref)
 
423
        else:
 
424
            try:
 
425
                target_path = target_branch.controldir.control_transport.local_abspath('.')
 
426
            except bzr_errors.NotLocalUrl:
 
427
                raise bzr_errors.IncompatibleFormat(target_branch._format, self._format)
 
428
            # TODO(jelmer): Do some consistency checking across branches..
 
429
            self.control_transport.put_bytes('commondir', target_path.encode('utf-8'))
 
430
            # TODO(jelmer): Urgh, avoid mucking about with internals.
 
431
            self._git._commontransport = target_branch.repository._git._commontransport.clone()
 
432
            self._git.object_store = TransportObjectStore(self._git._commontransport.clone(OBJECTDIR))
 
433
            self._git.refs.transport = self._git._commontransport
 
434
            target_ref_chain, unused_sha = target_branch.controldir._git.refs.follow(target_branch.ref)
 
435
            for target_ref in target_ref_chain:
 
436
                if target_ref == b'HEAD':
 
437
                    continue
 
438
                break
 
439
            else:
 
440
                # Can't create a reference to something that is not a in a repository.
 
441
                raise bzr_errors.IncompatibleFormat(self.set_branch_reference, self)
 
442
            self._git.refs.set_symbolic_ref(ref, target_ref)
 
443
 
 
444
    def get_branch_reference(self, name=None):
 
445
        ref = self._get_selected_ref(name)
 
446
        target_ref = self._get_symref(ref)
 
447
        if target_ref is not None:
 
448
            from .refs import ref_to_branch_name
 
449
            try:
 
450
                branch_name = ref_to_branch_name(target_ref)
 
451
            except ValueError:
 
452
                params = {'ref': urllib.quote(target_ref, '')}
 
453
            else:
 
454
                if branch_name != b'':
 
455
                    params = {'branch': urllib.quote(branch_name.encode('utf-8'), '')}
 
456
                else:
 
457
                    params = {}
 
458
            try:
 
459
                base_url = urlutils.local_path_to_url(self.control_transport.get_bytes('commondir')).rstrip('/.git/')+'/'
 
460
            except bzr_errors.NoSuchFile:
 
461
                base_url = self.user_url.rstrip('/')
 
462
            return urlutils.join_segment_parameters(base_url, params)
 
463
        return None
 
464
 
 
465
    def find_branch_format(self, name=None):
 
466
        from .branch import (
 
467
            LocalGitBranchFormat,
 
468
            )
 
469
        ref = self._get_selected_ref(name)
 
470
        return LocalGitBranchFormat()
 
471
 
 
472
    def get_branch_transport(self, branch_format, name=None):
 
473
        if branch_format is None:
 
474
            return self.transport
 
475
        if isinstance(branch_format, LocalGitControlDirFormat):
 
476
            return self.transport
 
477
        raise bzr_errors.IncompatibleFormat(branch_format, self._format)
 
478
 
 
479
    def get_repository_transport(self, format):
 
480
        if format is None:
 
481
            return self.transport
 
482
        if isinstance(format, LocalGitControlDirFormat):
 
483
            return self.transport
 
484
        raise bzr_errors.IncompatibleFormat(format, self._format)
 
485
 
 
486
    def get_workingtree_transport(self, format):
 
487
        if format is None:
 
488
            return self.transport
 
489
        if isinstance(format, LocalGitControlDirFormat):
 
490
            return self.transport
 
491
        raise bzr_errors.IncompatibleFormat(format, self._format)
 
492
 
 
493
    def open_branch(self, name=None, unsupported=False, ignore_fallbacks=None,
 
494
            ref=None, possible_transports=None, nascent_ok=False):
 
495
        """'create' a branch for this dir."""
 
496
        repo = self.find_repository()
 
497
        from .branch import LocalGitBranch
 
498
        ref = self._get_selected_ref(name, ref)
 
499
        if not nascent_ok and ref not in self._git.refs:
 
500
            raise bzr_errors.NotBranchError(self.root_transport.base,
 
501
                    controldir=self)
 
502
        ref_chain, unused_sha = self._git.refs.follow(ref)
 
503
        if ref_chain[-1] == b'HEAD':
 
504
            controldir = self
 
505
        else:
 
506
            controldir = self._find_commondir()
 
507
        return LocalGitBranch(controldir, repo, ref_chain[-1])
 
508
 
 
509
    def destroy_branch(self, name=None):
 
510
        refname = self._get_selected_ref(name)
 
511
        if refname == b'HEAD':
 
512
            # HEAD can't be removed
 
513
            raise bzr_errors.UnsupportedOperation(
 
514
                self.destroy_branch, self)
 
515
        try:
 
516
            del self._git.refs[refname]
 
517
        except KeyError:
 
518
            raise bzr_errors.NotBranchError(self.root_transport.base,
 
519
                    controldir=self)
 
520
 
 
521
    def destroy_repository(self):
 
522
        raise bzr_errors.UnsupportedOperation(self.destroy_repository, self)
 
523
 
 
524
    def destroy_workingtree(self):
 
525
        raise bzr_errors.UnsupportedOperation(self.destroy_workingtree, self)
 
526
 
 
527
    def destroy_workingtree_metadata(self):
 
528
        raise bzr_errors.UnsupportedOperation(self.destroy_workingtree_metadata, self)
 
529
 
 
530
    def needs_format_conversion(self, format=None):
 
531
        return not isinstance(self._format, format.__class__)
 
532
 
 
533
    def open_repository(self):
 
534
        """'open' a repository for this dir."""
 
535
        if self.control_transport.has('commondir'):
 
536
            raise bzr_errors.NoRepositoryPresent(self)
 
537
        return self._gitrepository_class(self)
 
538
 
 
539
    def has_workingtree(self):
 
540
        return not self._git.bare
 
541
 
 
542
    def open_workingtree(self, recommend_upgrade=True, unsupported=False):
 
543
        if not self._git.bare:
 
544
            from dulwich.errors import NoIndexPresent
 
545
            repo = self.find_repository()
 
546
            try:
 
547
                index = repo._git.open_index()
 
548
            except NoIndexPresent:
 
549
                pass
 
550
            else:
 
551
                from .workingtree import GitWorkingTree
 
552
                branch = self.open_branch(ref=b'HEAD', nascent_ok=True)
 
553
                return GitWorkingTree(self, repo, branch, index)
 
554
        loc = urlutils.unescape_for_display(self.root_transport.base, 'ascii')
 
555
        raise bzr_errors.NoWorkingTree(loc)
 
556
 
 
557
    def create_repository(self, shared=False):
 
558
        from .repository import GitRepositoryFormat
 
559
        if shared:
 
560
            raise bzr_errors.IncompatibleFormat(GitRepositoryFormat(), self._format)
 
561
        return self.find_repository()
 
562
 
 
563
    def create_branch(self, name=None, repository=None,
 
564
                      append_revisions_only=None, ref=None):
 
565
        refname = self._get_selected_ref(name, ref)
 
566
        if refname != b'HEAD' and refname in self._git.refs:
 
567
            raise bzr_errors.AlreadyBranchError(self.user_url)
 
568
        repo = self.open_repository()
 
569
        if refname in self._git.refs:
 
570
            ref_chain, unused_sha = self._git.refs.follow(self._get_selected_ref(None))
 
571
            if ref_chain[0] == b'HEAD':
 
572
                refname = ref_chain[1]
 
573
        from .branch import LocalGitBranch
 
574
        branch = LocalGitBranch(self, repo, refname)
 
575
        if append_revisions_only:
 
576
            branch.set_append_revisions_only(append_revisions_only)
 
577
        return branch
 
578
 
 
579
    def backup_bzrdir(self):
 
580
        if not self._git.bare:
 
581
            self.root_transport.copy_tree(".git", ".git.backup")
 
582
            return (self.root_transport.abspath(".git"),
 
583
                    self.root_transport.abspath(".git.backup"))
 
584
        else:
 
585
            basename = urlutils.basename(self.root_transport.base)
 
586
            parent = self.root_transport.clone('..')
 
587
            parent.copy_tree(basename, basename + ".backup")
 
588
 
 
589
    def create_workingtree(self, revision_id=None, from_branch=None,
 
590
        accelerator_tree=None, hardlink=False):
 
591
        if self._git.bare:
 
592
            raise bzr_errors.UnsupportedOperation(self.create_workingtree, self)
 
593
        from dulwich.index import build_index_from_tree
 
594
        if from_branch is None:
 
595
            from_branch = self.open_branch(nascent_ok=True)
 
596
        if revision_id is None:
 
597
            revision_id = from_branch.last_revision()
 
598
        repo = self.find_repository()
 
599
        from .workingtree import GitWorkingTree
 
600
        index = self._git.open_index()
 
601
        wt = GitWorkingTree(self, repo, from_branch, index)
 
602
        wt.set_last_revision(revision_id)
 
603
        wt._build_checkout_with_index()
 
604
        return wt
 
605
 
 
606
    def _find_or_create_repository(self, force_new_repo=None):
 
607
        return self.create_repository(shared=False)
 
608
 
 
609
    def _find_creation_modes(self):
 
610
        """Determine the appropriate modes for files and directories.
 
611
 
 
612
        They're always set to be consistent with the base directory,
 
613
        assuming that this transport allows setting modes.
 
614
        """
 
615
        # TODO: Do we need or want an option (maybe a config setting) to turn
 
616
        # this off or override it for particular locations? -- mbp 20080512
 
617
        if self._mode_check_done:
 
618
            return
 
619
        self._mode_check_done = True
 
620
        try:
 
621
            st = self.transport.stat('.')
 
622
        except bzr_errors.TransportNotPossible:
 
623
            self._dir_mode = None
 
624
            self._file_mode = None
 
625
        else:
 
626
            # Check the directory mode, but also make sure the created
 
627
            # directories and files are read-write for this user. This is
 
628
            # mostly a workaround for filesystems which lie about being able to
 
629
            # write to a directory (cygwin & win32)
 
630
            if (st.st_mode & 07777 == 00000):
 
631
                # FTP allows stat but does not return dir/file modes
 
632
                self._dir_mode = None
 
633
                self._file_mode = None
 
634
            else:
 
635
                self._dir_mode = (st.st_mode & 07777) | 00700
 
636
                # Remove the sticky and execute bits for files
 
637
                self._file_mode = self._dir_mode & ~07111
 
638
 
 
639
    def _get_file_mode(self):
 
640
        """Return Unix mode for newly created files, or None.
 
641
        """
 
642
        if not self._mode_check_done:
 
643
            self._find_creation_modes()
 
644
        return self._file_mode
 
645
 
 
646
    def _get_dir_mode(self):
 
647
        """Return Unix mode for newly created directories, or None.
 
648
        """
 
649
        if not self._mode_check_done:
 
650
            self._find_creation_modes()
 
651
        return self._dir_mode
 
652
 
 
653
    def get_refs_container(self):
 
654
        return self._git.refs
 
655
 
 
656
    def get_peeled(self, ref):
 
657
        return self._git.get_peeled(ref)