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