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