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