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