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