/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: Jelmer Vernooij
  • Date: 2019-06-03 23:48:08 UTC
  • mfrom: (7316 work)
  • mto: This revision was merged to the branch mainline in revision 7328.
  • Revision ID: jelmer@jelmer.uk-20190603234808-15yk5c7054tj8e2b
Merge trunk.

Show diffs side-by-side

added added

removed removed

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