/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
1
# Copyright (C) 2007 Canonical Ltd
0.200.910 by Jelmer Vernooij
update copyright years
2
# Copyright (C) 2010 Jelmer Vernooij
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
3
#
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
0.200.1012 by Jelmer Vernooij
Rename BzrDir to ControlDir.
18
"""An adapter between a Git control dir and a Bazaar ControlDir."""
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
19
0.200.1311 by Jelmer Vernooij
More work on colocated branch support.
20
import urllib
21
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
22
from bzrlib import (
0.239.13 by Jelmer Vernooij
Don't break "bzr info -v" when Dulwich is not installed.
23
    errors as bzr_errors,
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
24
    lockable_files,
0.200.1137 by Jelmer Vernooij
Support BzrProber.known_formats().
25
    trace,
0.200.1172 by Jelmer Vernooij
Provide GitDir._available_backup_name.
26
    osutils,
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
27
    urlutils,
28
    )
0.200.1137 by Jelmer Vernooij
Support BzrProber.known_formats().
29
from bzrlib.bzrdir import CreateRepository
30
from bzrlib.transport import do_catching_redirections
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
31
0.200.280 by Jelmer Vernooij
Support bzr.dev.
32
LockWarner = getattr(lockable_files, "_LockWarner", None)
33
0.200.1111 by Jelmer Vernooij
Drop support for Bazaar < 2.3.
34
from bzrlib.controldir import (
35
    ControlDir,
0.200.1137 by Jelmer Vernooij
Support BzrProber.known_formats().
36
    ControlDirFormat,
0.200.1111 by Jelmer Vernooij
Drop support for Bazaar < 2.3.
37
    format_registry,
38
    )
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
39
0.200.123 by Jelmer Vernooij
Use central git module.
40
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
41
class GitLock(object):
42
    """A lock that thunks through to Git."""
43
0.200.1130 by Jelmer Vernooij
Implement GitLock.lock_name.
44
    def __init__(self):
45
        self.lock_name = "git lock"
46
0.200.84 by Jelmer Vernooij
Fix lock_write argument.
47
    def lock_write(self, token=None):
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
48
        pass
49
50
    def lock_read(self):
51
        pass
52
53
    def unlock(self):
54
        pass
55
0.200.73 by Jelmer Vernooij
Implement GitLock.peek().
56
    def peek(self):
57
        pass
58
0.200.130 by Jelmer Vernooij
Make most tree inspection tests succeed.
59
    def validate_token(self, token):
60
        pass
61
0.200.629 by Jelmer Vernooij
Add GitLock.break_lock().
62
    def break_lock(self):
0.200.1254 by Jelmer Vernooij
break_lock is not implemented for git control directories.
63
        raise NotImplementedError(self.break_lock)
0.200.629 by Jelmer Vernooij
Add GitLock.break_lock().
64
0.200.1170 by Jelmer Vernooij
Implement GitLock.leave_lock_in_place and GitLock.dont_leave_lock_in_place.
65
    def dont_leave_in_place(self):
66
        raise NotImplementedError(self.dont_leave_in_place)
67
68
    def leave_in_place(self):
69
        raise NotImplementedError(self.leave_in_place)
70
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
71
72
class GitLockableFiles(lockable_files.LockableFiles):
73
    """Git specific lockable files abstraction."""
74
0.200.129 by Jelmer Vernooij
merge dulwich.
75
    def __init__(self, transport, lock):
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
76
        self._lock = lock
77
        self._transaction = None
78
        self._lock_mode = None
0.200.129 by Jelmer Vernooij
merge dulwich.
79
        self._transport = transport
0.200.280 by Jelmer Vernooij
Support bzr.dev.
80
        if LockWarner is None:
81
            # Bzr 1.13
82
            self._lock_count = 0
83
        else:
84
            self._lock_warner = LockWarner(repr(self))
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
85
86
0.200.1026 by Jelmer Vernooij
Fix typo.
87
class GitDirConfig(object):
0.200.1025 by Jelmer Vernooij
Implement GitDir.get_config().
88
89
    def get_default_stack_on(self):
90
        return None
91
92
    def set_default_stack_on(self, value):
93
        raise bzr_errors.BzrError("Cannot set configuration")
94
95
0.200.1137 by Jelmer Vernooij
Support BzrProber.known_formats().
96
class GitControlDirFormat(ControlDirFormat):
97
98
    _lock_class = lockable_files.TransportLock
99
100
    colocated_branches = True
101
    fixed_components = True
102
103
    def __eq__(self, other):
104
        return type(self) == type(other)
105
106
    def is_supported(self):
107
        return True
108
109
    def network_name(self):
110
        return "git"
111
112
0.200.1012 by Jelmer Vernooij
Rename BzrDir to ControlDir.
113
class GitDir(ControlDir):
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
114
    """An adapter to the '.git' dir used by git."""
115
0.200.148 by Jelmer Vernooij
Share more infrastructure between LocalGitDir and RemoteGitDir.
116
    def is_supported(self):
117
        return True
118
0.200.981 by Jelmer Vernooij
Mark git directories as not convertable (for now).
119
    def can_convert_format(self):
120
        return False
121
0.200.1025 by Jelmer Vernooij
Implement GitDir.get_config().
122
    def break_lock(self):
123
        pass
124
0.200.155 by Jelmer Vernooij
Fix formatting, remove catch-all for exceptions when opening local repositories.
125
    def cloning_metadir(self, stacked=False):
0.200.1013 by Jelmer Vernooij
More renames.
126
        return format_registry.make_bzrdir("default")
0.200.155 by Jelmer Vernooij
Fix formatting, remove catch-all for exceptions when opening local repositories.
127
0.200.1165 by Jelmer Vernooij
Implement GitDir.checkout_metadir.
128
    def checkout_metadir(self, stacked=False):
129
        return format_registry.make_bzrdir("default")
130
0.200.1310 by Jelmer Vernooij
Add _get_selected_ref method.
131
    def _get_selected_ref(self, branch):
132
        if branch is None and getattr(self, "_get_selected_branch", False):
133
            branch = self._get_selected_branch()
134
        if branch is not None:
0.200.1311 by Jelmer Vernooij
More work on colocated branch support.
135
            from bzrlib.plugins.git.refs import branch_name_to_ref
136
            return branch_name_to_ref(branch, None)
0.200.1310 by Jelmer Vernooij
Add _get_selected_ref method.
137
        segment_parameters = getattr(
138
            self.user_transport, "get_segment_parameters", lambda: {})()
0.200.1311 by Jelmer Vernooij
More work on colocated branch support.
139
        ref = segment_parameters.get("ref")
140
        if ref is not None:
141
            ref = urlutils.unescape(ref)
142
        return ref
0.200.1310 by Jelmer Vernooij
Add _get_selected_ref method.
143
0.200.1025 by Jelmer Vernooij
Implement GitDir.get_config().
144
    def get_config(self):
145
        return GitDirConfig()
146
0.200.1172 by Jelmer Vernooij
Provide GitDir._available_backup_name.
147
    def _available_backup_name(self, base):
148
        return osutils.available_backup_name(base, self.root_transport.has)
149
0.259.1 by Jelmer Vernooij
Provide custom GitDir.sprout() for bzr 2.4 compatibility.
150
    def sprout(self, url, revision_id=None, force_new_repo=False,
151
               recurse='down', possible_transports=None,
152
               accelerator_tree=None, hardlink=False, stacked=False,
153
               source_branch=None, create_tree_if_local=True):
154
        from bzrlib.repository import InterRepository
155
        from bzrlib.transport.local import LocalTransport
156
        from bzrlib.transport import get_transport
157
        target_transport = get_transport(url, possible_transports)
158
        target_transport.ensure_base()
159
        cloning_format = self.cloning_metadir()
160
        # Create/update the result branch
161
        result = cloning_format.initialize_on_transport(target_transport)
162
        source_branch = self.open_branch()
163
        source_repository = self.find_repository()
164
        try:
165
            result_repo = result.find_repository()
166
        except bzr_errors.NoRepositoryPresent:
167
            result_repo = result.create_repository()
168
            target_is_empty = True
169
        else:
170
            target_is_empty = None # Unknown
171
        if stacked:
172
            raise bzr_errors.IncompatibleRepositories(source_repository, result_repo)
173
        interrepo = InterRepository.get(source_repository, result_repo)
174
175
        if revision_id is not None:
0.259.4 by Jelmer Vernooij
Put determine_wants methods on InterRepo.
176
            determine_wants = interrepo.get_determine_wants_revids(
177
                [revision_id], include_tags=True)
0.259.1 by Jelmer Vernooij
Provide custom GitDir.sprout() for bzr 2.4 compatibility.
178
        else:
0.259.4 by Jelmer Vernooij
Put determine_wants methods on InterRepo.
179
            determine_wants = interrepo.determine_wants_all
0.259.1 by Jelmer Vernooij
Provide custom GitDir.sprout() for bzr 2.4 compatibility.
180
        interrepo.fetch_objects(determine_wants=determine_wants,
181
            mapping=source_branch.mapping)
182
        result_branch = source_branch.sprout(result,
183
            revision_id=revision_id, repository=result_repo)
184
        if (create_tree_if_local and isinstance(target_transport, LocalTransport)
185
            and (result_repo is None or result_repo.make_working_trees())):
186
            wt = result.create_workingtree(accelerator_tree=accelerator_tree,
187
                hardlink=hardlink, from_branch=result_branch)
188
            wt.lock_write()
189
            try:
190
                if wt.path2id('') is None:
191
                    try:
192
                        wt.set_root_id(self.open_workingtree.get_root_id())
193
                    except bzr_errors.NoWorkingTree:
194
                        pass
195
            finally:
196
                wt.unlock()
197
        return result
198
0.200.1117 by Jelmer Vernooij
Provide basic implementation of GitDir.clone_on_transport.
199
    def clone_on_transport(self, transport, revision_id=None,
200
        force_new_repo=False, preserve_stacking=False, stacked_on=None,
201
        create_prefix=False, use_existing_dir=True, no_tree=False):
202
        """See ControlDir.clone_on_transport."""
0.200.1171 by Jelmer Vernooij
Fix some more tests.
203
        from bzrlib.repository import InterRepository
204
        from bzrlib.plugins.git.mapping import default_mapping
0.200.1119 by Jelmer Vernooij
Refactor repository initialization.
205
        if no_tree:
206
            format = BareLocalGitControlDirFormat()
207
        else:
208
            format = LocalGitControlDirFormat()
209
        (target_repo, target_controldir, stacking, repo_policy) = format.initialize_on_transport_ex(transport, use_existing_dir=use_existing_dir, create_prefix=create_prefix, force_new_repo=force_new_repo)
210
        target_git_repo = target_repo._git
0.200.1117 by Jelmer Vernooij
Provide basic implementation of GitDir.clone_on_transport.
211
        source_repo = self.open_repository()
212
        source_git_repo = source_repo._git
0.200.1171 by Jelmer Vernooij
Fix some more tests.
213
        interrepo = InterRepository.get(source_repo, target_repo)
0.200.1117 by Jelmer Vernooij
Provide basic implementation of GitDir.clone_on_transport.
214
        if revision_id is not None:
0.200.1171 by Jelmer Vernooij
Fix some more tests.
215
            determine_wants = interrepo.get_determine_wants_revids([revision_id], include_tags=True)
0.200.1117 by Jelmer Vernooij
Provide basic implementation of GitDir.clone_on_transport.
216
        else:
0.200.1171 by Jelmer Vernooij
Fix some more tests.
217
            determine_wants = interrepo.determine_wants_all
218
        (pack_hint, _, refs) = interrepo.fetch_objects(determine_wants,
219
            mapping=default_mapping)
0.200.1117 by Jelmer Vernooij
Provide basic implementation of GitDir.clone_on_transport.
220
        for name, val in refs.iteritems():
221
            target_git_repo.refs[name] = val
222
        lockfiles = GitLockableFiles(transport, GitLock())
223
        return self.__class__(transport, lockfiles, target_git_repo, format)
224
0.259.2 by Jelmer Vernooij
Make sure RemoteGitDir.find_repository works.
225
    def find_repository(self):
226
        """Find the repository that should be used.
227
228
        This does not require a branch as we use it to find the repo for
229
        new branches as well as to hook existing branches up to their
230
        repository.
231
        """
232
        return self.open_repository()
233
0.200.148 by Jelmer Vernooij
Share more infrastructure between LocalGitDir and RemoteGitDir.
234
0.200.1137 by Jelmer Vernooij
Support BzrProber.known_formats().
235
class LocalGitControlDirFormat(GitControlDirFormat):
236
    """The .git directory control format."""
237
238
    bare = False
239
240
    @classmethod
241
    def _known_formats(self):
242
        return set([LocalGitControlDirFormat()])
243
244
    @property
245
    def repository_format(self):
246
        from bzrlib.plugins.git.repository import GitRepositoryFormat
247
        return GitRepositoryFormat()
248
249
    def get_branch_format(self):
250
        from bzrlib.plugins.git.branch import GitBranchFormat
251
        return GitBranchFormat()
252
253
    def open(self, transport, _found=None):
254
        """Open this directory.
255
256
        """
257
        from bzrlib.plugins.git.transportgit import TransportRepo
258
        gitrepo = TransportRepo(transport)
259
        lockfiles = GitLockableFiles(transport, GitLock())
260
        return LocalGitDir(transport, lockfiles, gitrepo, self)
261
262
    def get_format_description(self):
263
        return "Local Git Repository"
264
265
    def initialize_on_transport(self, transport):
266
        from bzrlib.plugins.git.transportgit import TransportRepo
267
        TransportRepo.init(transport, bare=self.bare)
268
        return self.open(transport)
269
270
    def initialize_on_transport_ex(self, transport, use_existing_dir=False,
271
        create_prefix=False, force_new_repo=False, stacked_on=None,
272
        stack_on_pwd=None, repo_format_name=None, make_working_trees=None,
273
        shared_repo=False, vfs_only=False):
274
        def make_directory(transport):
275
            transport.mkdir('.')
276
            return transport
277
        def redirected(transport, e, redirection_notice):
278
            trace.note(redirection_notice)
279
            return transport._redirected_to(e.source, e.target)
280
        try:
281
            transport = do_catching_redirections(make_directory, transport,
282
                redirected)
283
        except bzr_errors.FileExists:
284
            if not use_existing_dir:
285
                raise
286
        except bzr_errors.NoSuchFile:
287
            if not create_prefix:
288
                raise
289
            transport.create_prefix()
290
        controldir = self.initialize_on_transport(transport)
291
        repository = controldir.open_repository()
292
        repository.lock_write()
293
        return (repository, controldir, False, CreateRepository(controldir))
294
295
    def is_supported(self):
296
        return True
297
298
299
class BareLocalGitControlDirFormat(LocalGitControlDirFormat):
300
301
    bare = True
302
    supports_workingtrees = False
303
304
    def get_format_description(self):
305
        return "Local Git Repository (bare)"
306
307
0.200.148 by Jelmer Vernooij
Share more infrastructure between LocalGitDir and RemoteGitDir.
308
class LocalGitDir(GitDir):
309
    """An adapter to the '.git' dir used by git."""
310
0.239.13 by Jelmer Vernooij
Don't break "bzr info -v" when Dulwich is not installed.
311
    def _get_gitrepository_class(self):
312
        from bzrlib.plugins.git.repository import LocalGitRepository
313
        return LocalGitRepository
314
315
    _gitrepository_class = property(_get_gitrepository_class)
0.202.2 by David Allouche
GitRepository.get_inventory and .revision_tree work for the null revision. Support for testing GitRepository without disk data.
316
0.200.1014 by Jelmer Vernooij
Fix tests.
317
    @property
318
    def user_transport(self):
319
        return self.root_transport
320
321
    @property
322
    def control_transport(self):
323
        return self.transport
324
0.200.90 by Jelmer Vernooij
Basic support for opening working trees.
325
    def __init__(self, transport, lockfiles, gitrepo, format):
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
326
        self._format = format
327
        self.root_transport = transport
0.200.1018 by Jelmer Vernooij
Fix use with new control dir API.
328
        self._mode_check_done = False
0.200.90 by Jelmer Vernooij
Basic support for opening working trees.
329
        self._git = gitrepo
330
        if gitrepo.bare:
331
            self.transport = transport
332
        else:
333
            self.transport = transport.clone('.git')
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
334
        self._lockfiles = lockfiles
0.200.381 by Jelmer Vernooij
Support working trees properly, status and ls.
335
        self._mode_check_done = None
336
337
    def is_control_filename(self, filename):
0.200.1126 by Jelmer Vernooij
Fix GitDir.is_control_filename.
338
        return (filename == '.git' or filename.startswith('.git/'))
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
339
0.200.1311 by Jelmer Vernooij
More work on colocated branch support.
340
    def _get_symref(self, ref):
341
        from dulwich.repo import SYMREF
342
        refcontents = self._git.refs.read_ref(ref)
343
        if refcontents is None: # no such ref
344
            return None
345
        if refcontents.startswith(SYMREF):
346
            return refcontents[len(SYMREF):].rstrip("\n")
347
        return None
348
349
    def set_branch_reference(self, name, target):
350
        ref = self._get_selected_ref(name)
351
        if ref is None:
352
            ref = "HEAD"
353
        if not getattr(target, "ref", None):
354
            raise bzr_errors.BzrError("Can only set symrefs to Git refs")
355
        self._git.refs.set_symbolic_ref(ref, target.ref)
356
357
    def get_branch_reference(self, name=None):
358
        ref = self._get_selected_ref(name)
359
        if ref is None:
360
            ref = "HEAD"
361
        target_ref = self._get_symref(ref)
362
        if target_ref is not None:
363
            return ",ref=%s" % urllib.quote(target_ref)
364
        return None
365
366
    def find_branch_format(self, name=None):
367
        from bzrlib.plugins.git.branch import (
368
            GitBranchFormat,
369
            GitSymrefBranchFormat,
370
            )
371
        ref = self._get_selected_ref(name)
372
        if ref is None:
373
            ref = "HEAD"
374
        if self._get_symref(ref) is not None:
375
            return GitSymrefBranchFormat()
376
        else:
377
            return GitBranchFormat()
378
0.200.978 by Jelmer Vernooij
Allow name argument to get_branch_transport to be missing.
379
    def get_branch_transport(self, branch_format, name=None):
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
380
        if branch_format is None:
381
            return self.transport
0.200.1012 by Jelmer Vernooij
Rename BzrDir to ControlDir.
382
        if isinstance(branch_format, LocalGitControlDirFormat):
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
383
            return self.transport
0.239.13 by Jelmer Vernooij
Don't break "bzr info -v" when Dulwich is not installed.
384
        raise bzr_errors.IncompatibleFormat(branch_format, self._format)
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
385
0.200.887 by Jelmer Vernooij
get_branch_transport takes a name argument.
386
    def get_repository_transport(self, format):
387
        if format is None:
388
            return self.transport
0.200.1012 by Jelmer Vernooij
Rename BzrDir to ControlDir.
389
        if isinstance(format, LocalGitControlDirFormat):
0.200.887 by Jelmer Vernooij
get_branch_transport takes a name argument.
390
            return self.transport
391
        raise bzr_errors.IncompatibleFormat(format, self._format)
392
393
    def get_workingtree_transport(self, format):
394
        if format is None:
395
            return self.transport
0.200.1012 by Jelmer Vernooij
Rename BzrDir to ControlDir.
396
        if isinstance(format, LocalGitControlDirFormat):
0.200.887 by Jelmer Vernooij
get_branch_transport takes a name argument.
397
            return self.transport
398
        raise bzr_errors.IncompatibleFormat(format, self._format)
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
399
0.200.1148 by Jelmer Vernooij
Remove no longer necessary compatibility code for open_branch.
400
    def open_branch(self, name=None, unsupported=False, ignore_fallbacks=None):
0.200.57 by Jelmer Vernooij
Fix more tests.
401
        """'create' a branch for this dir."""
402
        repo = self.open_repository()
0.239.13 by Jelmer Vernooij
Don't break "bzr info -v" when Dulwich is not installed.
403
        from bzrlib.plugins.git.branch import LocalGitBranch
0.200.1310 by Jelmer Vernooij
Add _get_selected_ref method.
404
        ref = self._get_selected_ref(name)
0.200.1311 by Jelmer Vernooij
More work on colocated branch support.
405
        if ref is None:
406
            ref = "HEAD"
407
        try:
408
            ref, sha = self._git.refs._follow(ref)
409
        except KeyError:
410
            raise bzr_errors.NotBranchError(self.root_transport.base,
411
                    bzrdir=self)
0.200.1310 by Jelmer Vernooij
Add _get_selected_ref method.
412
        return LocalGitBranch(self, repo, ref, self._lockfiles)
0.200.722 by Jelmer Vernooij
Implement GitDir.list_branches() and support name argument to open_branch.
413
0.200.724 by Jelmer Vernooij
support destroy_branch
414
    def destroy_branch(self, name=None):
0.200.1310 by Jelmer Vernooij
Add _get_selected_ref method.
415
        refname = self._get_selected_ref(name)
0.200.997 by Jelmer Vernooij
Implement BzrDir.needs_format_conversion.
416
        if not refname in self._git.refs:
417
            raise bzr_errors.NotBranchError(self.root_transport.base,
418
                    bzrdir=self)
419
        del self._git.refs[refname]
0.200.724 by Jelmer Vernooij
support destroy_branch
420
0.200.980 by Jelmer Vernooij
Implement LocalGitBzrDir.destroy_repository().
421
    def destroy_repository(self):
422
        raise bzr_errors.UnsupportedOperation(self.destroy_repository, self)
423
0.200.986 by Jelmer Vernooij
Implement GitDir.destroy_workingtree.
424
    def destroy_workingtree(self):
425
        raise bzr_errors.UnsupportedOperation(self.destroy_workingtree, self)
426
0.200.997 by Jelmer Vernooij
Implement BzrDir.needs_format_conversion.
427
    def needs_format_conversion(self, format=None):
428
        return not isinstance(self._format, format.__class__)
429
0.200.722 by Jelmer Vernooij
Implement GitDir.list_branches() and support name argument to open_branch.
430
    def list_branches(self):
431
        ret = []
432
        for name in self._git.get_refs():
0.200.832 by Jelmer Vernooij
Update to newer version of Dulwich, saner branch names.
433
            if name.startswith("refs/heads/"):
0.200.766 by Jelmer Vernooij
Only list actual branches, not tags.
434
                ret.append(self.open_branch(name=name))
0.200.722 by Jelmer Vernooij
Implement GitDir.list_branches() and support name argument to open_branch.
435
        return ret
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
436
0.200.1114 by Jelmer Vernooij
Properly raise exception when create_repository is called with shared=True
437
    def open_repository(self):
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
438
        """'open' a repository for this dir."""
0.202.2 by David Allouche
GitRepository.get_inventory and .revision_tree work for the null revision. Support for testing GitRepository without disk data.
439
        return self._gitrepository_class(self, self._lockfiles)
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
440
0.203.1 by Aaron Bentley
Make checkouts work
441
    def open_workingtree(self, recommend_upgrade=True):
0.246.5 by Jelmer Vernooij
Cope with has_index not existing.
442
        if not self._git.bare:
443
            from dulwich.errors import NoIndexPresent
0.200.803 by Jelmer Vernooij
Default to non-bare repositories when initializing a control directory.
444
            repo = self.open_repository()
0.246.5 by Jelmer Vernooij
Cope with has_index not existing.
445
            try:
0.200.803 by Jelmer Vernooij
Default to non-bare repositories when initializing a control directory.
446
                index = repo._git.open_index()
0.246.5 by Jelmer Vernooij
Cope with has_index not existing.
447
            except NoIndexPresent:
448
                pass
0.200.803 by Jelmer Vernooij
Default to non-bare repositories when initializing a control directory.
449
            else:
450
                from bzrlib.plugins.git.workingtree import GitWorkingTree
0.200.921 by Jelmer Vernooij
fix init tests.
451
                try:
452
                    branch = self.open_branch()
453
                except bzr_errors.NotBranchError:
454
                    pass
455
                else:
456
                    return GitWorkingTree(self, repo, branch, index)
0.200.392 by Jelmer Vernooij
Fix some tests now that working trees are supported.
457
        loc = urlutils.unescape_for_display(self.root_transport.base, 'ascii')
0.239.13 by Jelmer Vernooij
Don't break "bzr info -v" when Dulwich is not installed.
458
        raise bzr_errors.NoWorkingTree(loc)
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
459
0.200.108 by Jelmer Vernooij
Support bzr init --git.
460
    def create_repository(self, shared=False):
0.200.1114 by Jelmer Vernooij
Properly raise exception when create_repository is called with shared=True
461
        from bzrlib.plugins.git.repository import GitRepositoryFormat
462
        if shared:
463
            raise bzr_errors.IncompatibleFormat(GitRepositoryFormat(), self._format)
0.200.108 by Jelmer Vernooij
Support bzr init --git.
464
        return self.open_repository()
0.200.288 by Jelmer Vernooij
Add test for init-repo.
465
0.200.1132 by Jelmer Vernooij
Support repository argument to LocalGitDir.create_branch.
466
    def create_branch(self, name=None, repository=None):
0.200.1310 by Jelmer Vernooij
Add _get_selected_ref method.
467
        refname = self._get_selected_ref(name)
0.200.891 by Jelmer Vernooij
Use ZERO_SHA constant where possible.
468
        from dulwich.protocol import ZERO_SHA
0.200.1311 by Jelmer Vernooij
More work on colocated branch support.
469
        # FIXME: This is a bit awkward. Perhaps we should have a
470
        # a separate method for changing the default branch?
471
        if refname is None:
472
            refname = "refs/heads/master"
473
            self._git.refs.set_symbolic_ref("HEAD", refname)
474
        self._git.refs[refname] = ZERO_SHA
0.200.731 by Jelmer Vernooij
Handle unsupported flag to open_branch().
475
        return self.open_branch(name)
0.200.535 by Jelmer Vernooij
use standard version to check for index.
476
477
    def backup_bzrdir(self):
478
        if self._git.bare:
479
            self.root_transport.copy_tree(".git", ".git.backup")
480
            return (self.root_transport.abspath(".git"),
481
                    self.root_transport.abspath(".git.backup"))
482
        else:
0.239.13 by Jelmer Vernooij
Don't break "bzr info -v" when Dulwich is not installed.
483
            raise bzr_errors.BzrError("Unable to backup bare repositories")
0.200.535 by Jelmer Vernooij
use standard version to check for index.
484
485
    def create_workingtree(self, revision_id=None, from_branch=None,
486
        accelerator_tree=None, hardlink=False):
487
        if self._git.bare:
0.200.1038 by Jelmer Vernooij
Raise UnsupportedOperation on create_workingtree.
488
            raise bzr_errors.UnsupportedOperation(self.create_workingtree, self)
0.200.535 by Jelmer Vernooij
use standard version to check for index.
489
        from dulwich.index import write_index
0.200.613 by Jelmer Vernooij
Support creating working tree for existing git repo.
490
        from dulwich.pack import SHA1Writer
491
        f = open(self.transport.local_abspath("index"), 'w+')
492
        try:
493
            f = SHA1Writer(f)
494
            write_index(f, [])
495
        finally:
496
            f.close()
0.200.535 by Jelmer Vernooij
use standard version to check for index.
497
        return self.open_workingtree()
0.200.1015 by Jelmer Vernooij
Fix GitControlDir.find_repository().
498
0.200.1114 by Jelmer Vernooij
Properly raise exception when create_repository is called with shared=True
499
    def _find_or_create_repository(self, force_new_repo=None):
500
        return self.create_repository(shared=False)
501
0.200.1018 by Jelmer Vernooij
Fix use with new control dir API.
502
    def _find_creation_modes(self):
503
        """Determine the appropriate modes for files and directories.
504
505
        They're always set to be consistent with the base directory,
506
        assuming that this transport allows setting modes.
507
        """
508
        # TODO: Do we need or want an option (maybe a config setting) to turn
509
        # this off or override it for particular locations? -- mbp 20080512
510
        if self._mode_check_done:
511
            return
512
        self._mode_check_done = True
513
        try:
514
            st = self.transport.stat('.')
0.200.1116 by Jelmer Vernooij
Fix missing import.
515
        except bzr_errors.TransportNotPossible:
0.200.1018 by Jelmer Vernooij
Fix use with new control dir API.
516
            self._dir_mode = None
517
            self._file_mode = None
518
        else:
519
            # Check the directory mode, but also make sure the created
520
            # directories and files are read-write for this user. This is
521
            # mostly a workaround for filesystems which lie about being able to
522
            # write to a directory (cygwin & win32)
523
            if (st.st_mode & 07777 == 00000):
524
                # FTP allows stat but does not return dir/file modes
525
                self._dir_mode = None
526
                self._file_mode = None
527
            else:
528
                self._dir_mode = (st.st_mode & 07777) | 00700
529
                # Remove the sticky and execute bits for files
530
                self._file_mode = self._dir_mode & ~07111
531
532
    def _get_file_mode(self):
533
        """Return Unix mode for newly created files, or None.
534
        """
535
        if not self._mode_check_done:
536
            self._find_creation_modes()
537
        return self._file_mode
538
539
    def _get_dir_mode(self):
540
        """Return Unix mode for newly created directories, or None.
541
        """
542
        if not self._mode_check_done:
543
            self._find_creation_modes()
544
        return self._dir_mode
545