/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to __init__.py

Fix remote fetching.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# Copyright (C) 2006 Canonical Ltd
 
2
 
2
3
# Authors: Robert Collins <robert.collins@canonical.com>
3
4
#          Jelmer Vernooij <jelmer@samba.org>
4
5
#          John Carr <john.carr@unrouted.co.uk>
20
21
 
21
22
"""A GIT branch and repository format implementation for bzr."""
22
23
 
23
 
try:
24
 
    import dulwich as git
25
 
except ImportError:
26
 
    import os, sys
27
 
    sys.path.insert(0, os.path.join(os.path.dirname(__file__), "dulwich"))
28
 
    import dulwich as git
29
 
from bzrlib import bzrdir
30
 
from bzrlib.foreign import ForeignVcs, VcsMappingRegistry, foreign_vcs_registry
31
 
from bzrlib.plugins.git.dir import LocalGitBzrDirFormat, RemoteGitBzrDirFormat
 
24
import bzrlib
 
25
import bzrlib.api
 
26
from bzrlib import bzrdir, errors as bzr_errors
 
27
from bzrlib.foreign import foreign_vcs_registry
 
28
from bzrlib.lockable_files import TransportLock
32
29
from bzrlib.transport import register_lazy_transport
33
30
from bzrlib.commands import Command, register_command
34
31
from bzrlib.option import Option
35
 
 
36
 
bzrdir.format_registry.register(
37
 
    'git', LocalGitBzrDirFormat,
38
 
    help='GIT repository.', 
39
 
    native=False, experimental=True,
 
32
from bzrlib.trace import warning
 
33
 
 
34
MINIMUM_DULWICH_VERSION = (0, 1, 0)
 
35
COMPATIBLE_BZR_VERSIONS = [(1, 11, 0), (1, 12, 0)]
 
36
 
 
37
_versions_checked = False
 
38
def lazy_check_versions():
 
39
    global _versions_checked
 
40
    if _versions_checked:
 
41
        return
 
42
    _versions_checked = True
 
43
    try:
 
44
        from dulwich import __version__ as dulwich_version
 
45
    except ImportError:
 
46
        warning("Please install dulwich, https://launchpad.net/dulwich")
 
47
        raise
 
48
    else:
 
49
        if dulwich_version < MINIMUM_DULWICH_VERSION:
 
50
            warning("Dulwich is too old; at least %d.%d.%d is required" % MINIMUM_DULWICH_VERSION)
 
51
            raise ImportError
 
52
 
 
53
bzrlib.api.require_any_api(bzrlib, COMPATIBLE_BZR_VERSIONS)
 
54
 
 
55
bzrdir.format_registry.register_lazy('git', 
 
56
    "bzrlib.plugins.git.dir", "LocalGitBzrDirFormat",
 
57
    help='GIT repository.', native=False, experimental=True,
40
58
    )
41
59
 
 
60
try:
 
61
    from bzrlib.revisionspec import revspec_registry
 
62
    revspec_registry.register_lazy("git:", "bzrlib.plugins.git.revspec", 
 
63
        "RevisionSpec_git")
 
64
except ImportError:
 
65
    lazy_check_versions()
 
66
    from bzrlib.revisionspec import SPEC_TYPES
 
67
    from bzrlib.plugins.git.revspec import RevisionSpec_git
 
68
    SPEC_TYPES.append(RevisionSpec_git)
 
69
 
 
70
class GitBzrDirFormat(bzrdir.BzrDirFormat):
 
71
    _lock_class = TransportLock
 
72
 
 
73
    def is_supported(self):
 
74
        return True
 
75
 
 
76
 
 
77
class LocalGitBzrDirFormat(GitBzrDirFormat):
 
78
    """The .git directory control format."""
 
79
 
 
80
    @classmethod
 
81
    def _known_formats(self):
 
82
        return set([LocalGitBzrDirFormat()])
 
83
 
 
84
    def open(self, transport, _found=None):
 
85
        """Open this directory.
 
86
 
 
87
        """
 
88
        import dulwich as git
 
89
        # we dont grok readonly - git isn't integrated with transport.
 
90
        url = transport.base
 
91
        if url.startswith('readonly+'):
 
92
            url = url[len('readonly+'):]
 
93
 
 
94
        try:
 
95
            gitrepo = git.repo.Repo(transport.local_abspath("."))
 
96
        except bzr_errors.NotLocalUrl:
 
97
            raise bzr_errors.NotBranchError(path=transport.base)
 
98
        from bzrlib.plugins.git.dir import LocalGitDir, GitLockableFiles, GitLock
 
99
        lockfiles = GitLockableFiles(transport, GitLock())
 
100
        return LocalGitDir(transport, lockfiles, gitrepo, self)
 
101
 
 
102
    @classmethod
 
103
    def probe_transport(klass, transport):
 
104
        """Our format is present if the transport ends in '.not/'."""
 
105
        from bzrlib.transport.local import LocalTransport
 
106
 
 
107
        if not isinstance(transport, LocalTransport):
 
108
            raise bzr_errors.NotBranchError(path=transport.base)
 
109
 
 
110
        # This should quickly filter out most things that are not 
 
111
        # git repositories, saving us the trouble from loading dulwich.
 
112
        if not transport.has(".git") and not transport.has("objects"):
 
113
            raise bzr_errors.NotBranchError(path=transport.base)
 
114
 
 
115
        import dulwich as git
 
116
        format = klass()
 
117
        try:
 
118
            format.open(transport)
 
119
            return format
 
120
        except git.errors.NotGitRepository, e:
 
121
            raise bzr_errors.NotBranchError(path=transport.base)
 
122
        raise bzr_errors.NotBranchError(path=transport.base)
 
123
 
 
124
    def get_format_description(self):
 
125
        return "Local Git Repository"
 
126
 
 
127
    def get_format_string(self):
 
128
        return "Local Git Repository"
 
129
 
 
130
    def initialize_on_transport(self, transport):
 
131
        from bzrlib.transport.local import LocalTransport
 
132
 
 
133
        if not isinstance(transport, LocalTransport):
 
134
            raise NotImplementedError(self.initialize, 
 
135
                "Can't create Git Repositories/branches on "
 
136
                "non-local transports")
 
137
 
 
138
        from dulwich.repo import Repo
 
139
        Repo.create(transport.local_abspath(".")) 
 
140
        return self.open(transport)
 
141
 
 
142
    def is_supported(self):
 
143
        return True
 
144
 
 
145
 
 
146
class RemoteGitBzrDirFormat(GitBzrDirFormat):
 
147
    """The .git directory control format."""
 
148
 
 
149
    @classmethod
 
150
    def _known_formats(self):
 
151
        return set([RemoteGitBzrDirFormat()])
 
152
 
 
153
    def open(self, transport, _found=None):
 
154
        """Open this directory.
 
155
 
 
156
        """
 
157
        from bzrlib.plugins.git.remote import RemoteGitDir, GitSmartTransport
 
158
        if not isinstance(transport, GitSmartTransport):
 
159
            raise bzr_errors.NotBranchError(transport.base)
 
160
        # we dont grok readonly - git isn't integrated with transport.
 
161
        url = transport.base
 
162
        if url.startswith('readonly+'):
 
163
            url = url[len('readonly+'):]
 
164
 
 
165
        from bzrlib.plugins.git.dir import GitLockableFiles, GitLock
 
166
        lockfiles = GitLockableFiles(transport, GitLock())
 
167
        return RemoteGitDir(transport, lockfiles, self)
 
168
 
 
169
    @classmethod
 
170
    def probe_transport(klass, transport):
 
171
        """Our format is present if the transport ends in '.not/'."""
 
172
        # little ugly, but works
 
173
        format = klass()
 
174
        from bzrlib.plugins.git.remote import GitSmartTransport
 
175
        if not isinstance(transport, GitSmartTransport):
 
176
            raise bzr_errors.NotBranchError(transport.base)
 
177
        # The only way to know a path exists and contains a valid repository 
 
178
        # is to do a request against it:
 
179
        try:
 
180
            transport.fetch_pack(lambda x: [], None, lambda x: None, 
 
181
                                 lambda x: mutter("git: %s" % x))
 
182
        except errors.git_errors.GitProtocolError:
 
183
            raise bzr_errors.NotBranchError(path=transport.base)
 
184
        else:
 
185
            return format
 
186
        raise bzr_errors.NotBranchError(path=transport.base)
 
187
 
 
188
    def get_format_description(self):
 
189
        return "Remote Git Repository"
 
190
 
 
191
    def get_format_string(self):
 
192
        return "Remote Git Repository"
 
193
 
 
194
    def initialize_on_transport(self, transport):
 
195
        raise bzr_errors.UninitializableFormat(self)
 
196
 
 
197
 
42
198
bzrdir.BzrDirFormat.register_control_format(LocalGitBzrDirFormat)
43
199
bzrdir.BzrDirFormat.register_control_format(RemoteGitBzrDirFormat)
44
200
 
45
201
register_lazy_transport("git://", 'bzrlib.plugins.git.remote',
46
202
                        'GitSmartTransport')
47
203
 
48
 
 
49
 
class ForeignGit(ForeignVcs):
50
 
    """Foreign Git."""
51
 
 
52
 
 
53
 
git_mapping_registry = VcsMappingRegistry()
54
 
git_mapping_registry.register_lazy('git-experimental', "bzrlib.plugins.git.mapping",
55
 
                                   "BzrGitMappingExperimental")
56
 
foreign_vcs_registry.register("git", ForeignGit(git_mapping_registry), 
57
 
                                      "Stupid content tracker")
 
204
foreign_vcs_registry.register_lazy("git", 
 
205
                        "bzrlib.plugins.git.mapping", 
 
206
                        "foreign_git",
 
207
                        "Stupid content tracker")
58
208
 
59
209
 
60
210
class cmd_git_serve(Command):
69
219
    ]
70
220
 
71
221
    def run(self, directory=None):
 
222
        lazy_check_versions()
72
223
        from dulwich.server import TCPGitServer
73
224
        from bzrlib.plugins.git.server import BzrBackend
74
225
        from bzrlib.trace import warning