/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
# Copyright (C) 2006 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 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
 
29
from bzrlib.transport import register_lazy_transport
 
30
from bzrlib.commands import Command, register_command
 
31
from bzrlib.option import Option
 
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,
 
58
    )
 
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
 
 
198
bzrdir.BzrDirFormat.register_control_format(LocalGitBzrDirFormat)
 
199
bzrdir.BzrDirFormat.register_control_format(RemoteGitBzrDirFormat)
 
200
 
 
201
register_lazy_transport("git://", 'bzrlib.plugins.git.remote',
 
202
                        'GitSmartTransport')
 
203
 
 
204
foreign_vcs_registry.register_lazy("git", 
 
205
                        "bzrlib.plugins.git.mapping", 
 
206
                        "foreign_git",
 
207
                        "Stupid content tracker")
 
208
 
 
209
 
 
210
class cmd_git_serve(Command):
 
211
    """Provide access to a Bazaar branch using the git protocol.
 
212
 
 
213
    This command is experimental and doesn't do much yet.
 
214
    """
 
215
    takes_options = [
 
216
        Option('directory',
 
217
               help='serve contents of directory',
 
218
               type=unicode)
 
219
    ]
 
220
 
 
221
    def run(self, directory=None):
 
222
        lazy_check_versions()
 
223
        from dulwich.server import TCPGitServer
 
224
        from bzrlib.plugins.git.server import BzrBackend
 
225
        from bzrlib.trace import warning
 
226
        import os
 
227
 
 
228
        warning("server support in bzr-git is experimental.")
 
229
 
 
230
        if directory is None:
 
231
            directory = os.getcwd()
 
232
 
 
233
        backend = BzrBackend(directory)
 
234
 
 
235
        server = TCPGitServer(backend, 'localhost')
 
236
        server.serve_forever()
 
237
 
 
238
register_command(cmd_git_serve)
 
239
 
 
240
 
 
241
class cmd_git_import(Command):
 
242
    """Import all branches from a git repository.
 
243
 
 
244
    """
 
245
 
 
246
    takes_args = ["src_location", "dest_location"]
 
247
 
 
248
    def run(self, src_location, dest_location):
 
249
        from bzrlib.bzrdir import BzrDir, format_registry
 
250
        from bzrlib.errors import NoRepositoryPresent, NotBranchError
 
251
        from bzrlib.repository import Repository
 
252
        source_repo = Repository.open(src_location)
 
253
        format = format_registry.make_bzrdir('rich-root-pack')
 
254
        try:
 
255
            target_bzrdir = BzrDir.open(dest_location)
 
256
        except NotBranchError:
 
257
            target_bzrdir = BzrDir.create(dest_location, format=format)
 
258
        try:
 
259
            target_repo = target_bzrdir.open_repository()
 
260
        except NoRepositoryPresent:
 
261
            target_repo = target_bzrdir.create_repository(shared=True)
 
262
 
 
263
        target_repo.fetch(source_repo)
 
264
        for name, ref in source_repo._git.heads().iteritems():
 
265
            head_loc = os.path.join(dest_location, name)
 
266
            try:
 
267
                head_bzrdir = BzrDir.open(head_loc)
 
268
            except NotBranchError:
 
269
                head_bzrdir = BzrDir.create(head_loc, format=format)
 
270
            try:
 
271
                head_branch = head_bzrdir.open_branch()
 
272
            except NotBranchError:
 
273
                head_branch = head_bzrdir.create_branch()
 
274
            head_branch.generate_revision_history(source_repo.get_mapping().revision_id_foreign_to_bzr(ref))
 
275
 
 
276
 
 
277
register_command(cmd_git_import)
 
278
 
 
279
 
 
280
def test_suite():
 
281
    from bzrlib.plugins.git import tests
 
282
    return tests.test_suite()