/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

Share sha map cache connections inside threads.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2006-2009 Canonical Ltd
 
2
 
 
3
# Authors: Robert Collins <robert.collins@canonical.com>
 
4
#          Jelmer Vernooij <jelmer@samba.org>
 
5
#          John Carr <john.carr@unrouted.co.uk>
 
6
#
 
7
# This program is free software; you can redistribute it and/or modify
 
8
# it under the terms of the GNU General Public License as published by
 
9
# the Free Software Foundation; either version 2 of the License, or
 
10
# (at your option) any later version.
 
11
#
 
12
# This program is distributed in the hope that it will be useful,
 
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
# GNU General Public License for more details.
 
16
#
 
17
# You should have received a copy of the GNU General Public License
 
18
# along with this program; if not, write to the Free Software
 
19
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
20
 
 
21
 
 
22
"""A GIT branch and repository format implementation for bzr."""
 
23
 
 
24
import os
 
25
import sys
 
26
 
 
27
import bzrlib
 
28
import bzrlib.api
 
29
from bzrlib import (
 
30
    bzrdir,
 
31
    errors as bzr_errors,
 
32
    )
 
33
from bzrlib.foreign import (
 
34
    foreign_vcs_registry,
 
35
    )
 
36
from bzrlib.lockable_files import (
 
37
    TransportLock,
 
38
    )
 
39
from bzrlib.transport import (
 
40
    register_lazy_transport,
 
41
    register_transport_proto,
 
42
    )
 
43
from bzrlib.commands import (
 
44
    plugin_cmds,
 
45
    )
 
46
from bzrlib.trace import (
 
47
    warning,
 
48
    )
 
49
from bzrlib.version_info_formats.format_rio import (
 
50
    RioVersionInfoBuilder,
 
51
    )
 
52
 
 
53
 
 
54
# versions ending in 'exp' mean experimental mappings
 
55
# versions ending in 'dev' mean development version
 
56
# versions ending in 'final' mean release (well tested, etc)
 
57
version_info = (0, 2, 0, 'dev', 0)
 
58
 
 
59
if version_info[3] == 'final':
 
60
    version_string = '%d.%d.%d' % version_info[:3]
 
61
else:
 
62
    version_string = '%d.%d.%d%s%d' % version_info
 
63
__version__ = version_string
 
64
 
 
65
MINIMUM_DULWICH_VERSION = (0, 1, 1)
 
66
COMPATIBLE_BZR_VERSIONS = [(1, 15, 0)]
 
67
 
 
68
if getattr(sys, "frozen", None):
 
69
    # allow import additional libs from ./_lib for bzr.exe only
 
70
    sys.path.append(os.path.normpath(os.path.join(os.path.dirname(__file__), '_lib')))
 
71
 
 
72
_versions_checked = False
 
73
def lazy_check_versions():
 
74
    global _versions_checked
 
75
    if _versions_checked:
 
76
        return
 
77
    _versions_checked = True
 
78
    try:
 
79
        from dulwich import __version__ as dulwich_version
 
80
    except ImportError:
 
81
        raise ImportError("bzr-git: Please install dulwich, https://launchpad.net/dulwich")
 
82
    else:
 
83
        if dulwich_version < MINIMUM_DULWICH_VERSION:
 
84
            raise ImportError("bzr-git: Dulwich is too old; at least %d.%d.%d is required" % MINIMUM_DULWICH_VERSION)
 
85
 
 
86
bzrlib.api.require_any_api(bzrlib, COMPATIBLE_BZR_VERSIONS)
 
87
 
 
88
bzrdir.format_registry.register_lazy('git', 
 
89
    "bzrlib.plugins.git.dir", "LocalGitBzrDirFormat",
 
90
    help='GIT repository.', native=False, experimental=True,
 
91
    )
 
92
 
 
93
from bzrlib.revisionspec import revspec_registry
 
94
revspec_registry.register_lazy("git:", "bzrlib.plugins.git.revspec", 
 
95
    "RevisionSpec_git")
 
96
 
 
97
 
 
98
class GitBzrDirFormat(bzrdir.BzrDirFormat):
 
99
    _lock_class = TransportLock
 
100
 
 
101
    def is_supported(self):
 
102
        return True
 
103
 
 
104
 
 
105
class LocalGitBzrDirFormat(GitBzrDirFormat):
 
106
    """The .git directory control format."""
 
107
 
 
108
    @classmethod
 
109
    def _known_formats(self):
 
110
        return set([LocalGitBzrDirFormat()])
 
111
 
 
112
    def open(self, transport, _found=None):
 
113
        """Open this directory.
 
114
 
 
115
        """
 
116
        import dulwich as git
 
117
        # we dont grok readonly - git isn't integrated with transport.
 
118
        url = transport.base
 
119
        if url.startswith('readonly+'):
 
120
            url = url[len('readonly+'):]
 
121
 
 
122
        try:
 
123
            gitrepo = git.repo.Repo(transport.local_abspath("."))
 
124
        except bzr_errors.NotLocalUrl:
 
125
            raise bzr_errors.NotBranchError(path=transport.base)
 
126
        from bzrlib.plugins.git.dir import LocalGitDir, GitLockableFiles, GitLock
 
127
        lockfiles = GitLockableFiles(transport, GitLock())
 
128
        return LocalGitDir(transport, lockfiles, gitrepo, self)
 
129
 
 
130
    @classmethod
 
131
    def probe_transport(klass, transport):
 
132
        """Our format is present if the transport ends in '.not/'."""
 
133
        from bzrlib.transport.local import LocalTransport
 
134
 
 
135
        if not isinstance(transport, LocalTransport):
 
136
            raise bzr_errors.NotBranchError(path=transport.base)
 
137
 
 
138
        # This should quickly filter out most things that are not 
 
139
        # git repositories, saving us the trouble from loading dulwich.
 
140
        if not transport.has(".git") and not transport.has("objects"):
 
141
            raise bzr_errors.NotBranchError(path=transport.base)
 
142
 
 
143
        import dulwich as git
 
144
        format = klass()
 
145
        try:
 
146
            format.open(transport)
 
147
            return format
 
148
        except git.errors.NotGitRepository, e:
 
149
            raise bzr_errors.NotBranchError(path=transport.base)
 
150
        raise bzr_errors.NotBranchError(path=transport.base)
 
151
 
 
152
    def get_format_description(self):
 
153
        return "Local Git Repository"
 
154
 
 
155
    def get_format_string(self):
 
156
        return "Local Git Repository"
 
157
 
 
158
    def initialize_on_transport(self, transport):
 
159
        from bzrlib.transport.local import LocalTransport
 
160
 
 
161
        if not isinstance(transport, LocalTransport):
 
162
            raise NotImplementedError(self.initialize, 
 
163
                "Can't create Git Repositories/branches on "
 
164
                "non-local transports")
 
165
 
 
166
        from dulwich.repo import Repo
 
167
        Repo.create(transport.local_abspath(".")) 
 
168
        return self.open(transport)
 
169
 
 
170
    def is_supported(self):
 
171
        return True
 
172
 
 
173
 
 
174
class RemoteGitBzrDirFormat(GitBzrDirFormat):
 
175
    """The .git directory control format."""
 
176
 
 
177
    @classmethod
 
178
    def _known_formats(self):
 
179
        return set([RemoteGitBzrDirFormat()])
 
180
 
 
181
    def open(self, transport, _found=None):
 
182
        """Open this directory.
 
183
 
 
184
        """
 
185
        # we dont grok readonly - git isn't integrated with transport.
 
186
        url = transport.base
 
187
        if url.startswith('readonly+'):
 
188
            url = url[len('readonly+'):]
 
189
        if (not url.startswith("git://") and 
 
190
            not url.startswith("git+")):
 
191
            raise bzr_errors.NotBranchError(transport.base)
 
192
        from bzrlib.plugins.git.remote import RemoteGitDir, GitSmartTransport
 
193
        if not isinstance(transport, GitSmartTransport):
 
194
            raise bzr_errors.NotBranchError(transport.base)
 
195
        from bzrlib.plugins.git.dir import GitLockableFiles, GitLock
 
196
        lockfiles = GitLockableFiles(transport, GitLock())
 
197
        return RemoteGitDir(transport, lockfiles, self)
 
198
 
 
199
    @classmethod
 
200
    def probe_transport(klass, transport):
 
201
        """Our format is present if the transport ends in '.not/'."""
 
202
        url = transport.base
 
203
        if url.startswith('readonly+'):
 
204
            url = url[len('readonly+'):]
 
205
        if (not url.startswith("git://") and 
 
206
            not url.startswith("git+")):
 
207
            raise bzr_errors.NotBranchError(transport.base)
 
208
        # little ugly, but works
 
209
        format = klass()
 
210
        from bzrlib.plugins.git.remote import GitSmartTransport
 
211
        if not isinstance(transport, GitSmartTransport):
 
212
            raise bzr_errors.NotBranchError(transport.base)
 
213
        # The only way to know a path exists and contains a valid repository 
 
214
        # is to do a request against it:
 
215
        try:
 
216
            transport.fetch_pack(lambda x: [], None, lambda x: None, 
 
217
                                 lambda x: mutter("git: %s" % x))
 
218
        except errors.git_errors.GitProtocolError:
 
219
            raise bzr_errors.NotBranchError(path=transport.base)
 
220
        else:
 
221
            return format
 
222
        raise bzr_errors.NotBranchError(path=transport.base)
 
223
 
 
224
    def get_format_description(self):
 
225
        return "Remote Git Repository"
 
226
 
 
227
    def get_format_string(self):
 
228
        return "Remote Git Repository"
 
229
 
 
230
    def initialize_on_transport(self, transport):
 
231
        raise bzr_errors.UninitializableFormat(self)
 
232
 
 
233
 
 
234
bzrdir.BzrDirFormat.register_control_format(LocalGitBzrDirFormat)
 
235
bzrdir.BzrDirFormat.register_control_format(RemoteGitBzrDirFormat)
 
236
 
 
237
register_transport_proto('git://', 
 
238
        help="Access using the Git smart server protocol.")
 
239
register_transport_proto('git+ssh://', 
 
240
        help="Access using the Git smart server protocol over SSH.")
 
241
 
 
242
register_lazy_transport("git://", 'bzrlib.plugins.git.remote',
 
243
                        'TCPGitSmartTransport')
 
244
register_lazy_transport("git+ssh://", 'bzrlib.plugins.git.remote',
 
245
                        'SSHGitSmartTransport')
 
246
 
 
247
foreign_vcs_registry.register_lazy("git", 
 
248
    "bzrlib.plugins.git.mapping", "foreign_git", "Stupid content tracker")
 
249
 
 
250
plugin_cmds.register_lazy("cmd_git_serve", [], "bzrlib.plugins.git.commands")
 
251
plugin_cmds.register_lazy("cmd_git_import", [], "bzrlib.plugins.git.commands")
 
252
 
 
253
def update_stanza(rev, stanza):
 
254
    mapping = getattr(rev, "mapping", None)
 
255
    if mapping is not None and mapping.revid_prefix.startswith("git-"):
 
256
        stanza.add("git-commit", rev.foreign_revid)
 
257
 
 
258
 
 
259
RioVersionInfoBuilder.hooks.install_named_hook('revision',
 
260
    update_stanza, None)
 
261
 
 
262
def get_rich_root_format(stacked=False):
 
263
    if stacked:
 
264
        return bzrdir.format_registry.make_bzrdir("1.9-rich-root")
 
265
    else:
 
266
        return bzrdir.format_registry.make_bzrdir("default-rich-root")
 
267
 
 
268
def test_suite():
 
269
    from bzrlib.plugins.git import tests
 
270
    return tests.test_suite()