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