/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

Add git: revision specifier.

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>
 
4
#          Jelmer Vernooij <jelmer@samba.org>
 
5
#          John Carr <john.carr@unrouted.co.uk>
3
6
#
4
7
# This program is free software; you can redistribute it and/or modify
5
8
# it under the terms of the GNU General Public License as published by
18
21
 
19
22
"""A GIT branch and repository format implementation for bzr."""
20
23
 
21
 
try:
22
 
    import dulwich as git
23
 
except ImportError:
24
 
    import os, sys
25
 
    sys.path.insert(0, os.path.join(os.path.dirname(__file__), "dulwich"))
26
 
    import dulwich as git
 
24
import bzrlib
 
25
import bzrlib.api
27
26
from bzrlib import bzrdir
28
 
from bzrlib.foreign import ForeignVcs, VcsMappingRegistry, foreign_vcs_registry
29
 
from bzrlib.plugins.git.dir import LocalGitBzrDirFormat, RemoteGitBzrDirFormat
 
27
from bzrlib.foreign import foreign_vcs_registry
 
28
from bzrlib.lockable_files import TransportLock
 
29
from bzrlib.revisionspec import revspec_registry
30
30
from bzrlib.transport import register_lazy_transport
31
31
from bzrlib.commands import Command, register_command
32
32
from bzrlib.option import Option
33
 
 
34
 
bzrdir.format_registry.register(
35
 
    'git', LocalGitBzrDirFormat,
36
 
    help='GIT repository.', 
37
 
    native=False, experimental=True,
 
33
from bzrlib.trace import warning
 
34
 
 
35
MINIMUM_DULWICH_VERSION = (0, 1, 0)
 
36
COMPATIBLE_BZR_VERSIONS = [(1, 12, 0)]
 
37
 
 
38
_versions_checked = False
 
39
def lazy_check_versions():
 
40
    global _versions_checked
 
41
    if _versions_checked:
 
42
        return
 
43
    _versions_checked = True
 
44
    try:
 
45
        from dulwich import __version__ as dulwich_version
 
46
    except ImportError:
 
47
        warning("Please install dulwich, https://launchpad.net/dulwich")
 
48
        raise
 
49
    else:
 
50
        if dulwich_version < MINIMUM_DULWICH_VERSION:
 
51
            warning("Dulwich is too old; at least %d.%d.%d is required" % MINIMUM_DULWICH_VERSION)
 
52
            raise ImportError
 
53
 
 
54
bzrlib.api.require_any_api(bzrlib, COMPATIBLE_BZR_VERSIONS)
 
55
 
 
56
bzrdir.format_registry.register_lazy('git', 
 
57
    "bzrlib.plugins.git.dir", "LocalGitBzrDirFormat",
 
58
    help='GIT repository.', native=False, experimental=True,
38
59
    )
 
60
revspec_registry.register_lazy("git:", "bzrlib.plugins.git.revspec", 
 
61
    "RevisionSpec_git")
 
62
 
 
63
class GitBzrDirFormat(bzrdir.BzrDirFormat):
 
64
    _lock_class = TransportLock
 
65
 
 
66
    def is_supported(self):
 
67
        return True
 
68
 
 
69
 
 
70
class LocalGitBzrDirFormat(GitBzrDirFormat):
 
71
    """The .git directory control format."""
 
72
 
 
73
    @classmethod
 
74
    def _known_formats(self):
 
75
        return set([LocalGitBzrDirFormat()])
 
76
 
 
77
    def open(self, transport, _found=None):
 
78
        """Open this directory.
 
79
 
 
80
        """
 
81
        import dulwich as git
 
82
        # we dont grok readonly - git isn't integrated with transport.
 
83
        url = transport.base
 
84
        if url.startswith('readonly+'):
 
85
            url = url[len('readonly+'):]
 
86
 
 
87
        try:
 
88
            gitrepo = git.repo.Repo(transport.local_abspath("."))
 
89
        except errors.bzr_errors.NotLocalUrl:
 
90
            raise errors.bzr_errors.NotBranchError(path=transport.base)
 
91
        from bzrlib.plugins.git.dir import LocalGitDir, GitLockableFiles, GitLock
 
92
        lockfiles = GitLockableFiles(transport, GitLock())
 
93
        return LocalGitDir(transport, lockfiles, gitrepo, self)
 
94
 
 
95
    @classmethod
 
96
    def probe_transport(klass, transport):
 
97
        """Our format is present if the transport ends in '.not/'."""
 
98
        from bzrlib.transport.local import LocalTransport
 
99
 
 
100
        if not isinstance(transport, LocalTransport):
 
101
            raise errors.bzr_errors.NotBranchError(path=transport.base)
 
102
 
 
103
        # This should quickly filter out most things that are not 
 
104
        # git repositories, saving us the trouble from loading dulwich.
 
105
        if not transport.has(".git") and not transport.has("objects"):
 
106
            raise errors.bzr_errors.NotBranchError(path=transport.base)
 
107
 
 
108
        import dulwich as git
 
109
        format = klass()
 
110
        try:
 
111
            format.open(transport)
 
112
            return format
 
113
        except git.errors.NotGitRepository, e:
 
114
            raise errors.bzr_errors.NotBranchError(path=transport.base)
 
115
        raise errors.bzr_errors.NotBranchError(path=transport.base)
 
116
 
 
117
    def get_format_description(self):
 
118
        return "Local Git Repository"
 
119
 
 
120
    def get_format_string(self):
 
121
        return "Local Git Repository"
 
122
 
 
123
    def initialize_on_transport(self, transport):
 
124
        from bzrlib.transport.local import LocalTransport
 
125
 
 
126
        if not isinstance(transport, LocalTransport):
 
127
            raise NotImplementedError(self.initialize, 
 
128
                "Can't create Git Repositories/branches on "
 
129
                "non-local transports")
 
130
 
 
131
        from dulwich.repo import Repo
 
132
        Repo.create(transport.local_abspath(".")) 
 
133
        return self.open(transport)
 
134
 
 
135
    def is_supported(self):
 
136
        return True
 
137
 
 
138
 
 
139
class RemoteGitBzrDirFormat(GitBzrDirFormat):
 
140
    """The .git directory control format."""
 
141
 
 
142
    @classmethod
 
143
    def _known_formats(self):
 
144
        return set([RemoteGitBzrDirFormat()])
 
145
 
 
146
    def open(self, transport, _found=None):
 
147
        """Open this directory.
 
148
 
 
149
        """
 
150
        from bzrlib.plugins.git.remote import RemoteGitDir, GitSmartTransport
 
151
        if not isinstance(transport, GitSmartTransport):
 
152
            raise errors.bzr_errors.NotBranchError(transport.base)
 
153
        # we dont grok readonly - git isn't integrated with transport.
 
154
        url = transport.base
 
155
        if url.startswith('readonly+'):
 
156
            url = url[len('readonly+'):]
 
157
 
 
158
        from bzrlib.plugins.git.dir import GitLockableFiles, GitLock
 
159
        lockfiles = GitLockableFiles(transport, GitLock())
 
160
        return RemoteGitDir(transport, lockfiles, self)
 
161
 
 
162
    @classmethod
 
163
    def probe_transport(klass, transport):
 
164
        """Our format is present if the transport ends in '.not/'."""
 
165
        # little ugly, but works
 
166
        format = klass()
 
167
        from bzrlib.plugins.git.remote import GitSmartTransport
 
168
        if not isinstance(transport, GitSmartTransport):
 
169
            raise errors.bzr_errors.NotBranchError(transport.base)
 
170
        # The only way to know a path exists and contains a valid repository 
 
171
        # is to do a request against it:
 
172
        try:
 
173
            transport.fetch_pack(lambda x: [], None, lambda x: None, 
 
174
                                 lambda x: mutter("git: %s" % x))
 
175
        except errors.git_errors.GitProtocolError:
 
176
            raise errors.bzr_errors.NotBranchError(path=transport.base)
 
177
        else:
 
178
            return format
 
179
        raise errors.bzr_errors.NotBranchError(path=transport.base)
 
180
 
 
181
    def get_format_description(self):
 
182
        return "Remote Git Repository"
 
183
 
 
184
    def get_format_string(self):
 
185
        return "Remote Git Repository"
 
186
 
 
187
    def initialize_on_transport(self, transport):
 
188
        raise errors.bzr_errors.UninitializableFormat(self)
 
189
 
39
190
 
40
191
bzrdir.BzrDirFormat.register_control_format(LocalGitBzrDirFormat)
41
192
bzrdir.BzrDirFormat.register_control_format(RemoteGitBzrDirFormat)
43
194
register_lazy_transport("git://", 'bzrlib.plugins.git.remote',
44
195
                        'GitSmartTransport')
45
196
 
46
 
 
47
 
class ForeignGit(ForeignVcs):
48
 
    """Foreign Git."""
49
 
 
50
 
 
51
 
git_mapping_registry = VcsMappingRegistry()
52
 
git_mapping_registry.register_lazy('git-experimental', "bzrlib.plugins.git.mapping",
53
 
                                   "BzrGitMappingExperimental")
54
 
foreign_vcs_registry.register("git", ForeignGit(git_mapping_registry), 
55
 
                                      "Stupid content tracker")
 
197
foreign_vcs_registry.register_lazy("git", 
 
198
                        "bzrlib.plugins.git.mapping", 
 
199
                        "foreign_git",
 
200
                        "Stupid content tracker")
56
201
 
57
202
 
58
203
class cmd_git_serve(Command):
61
206
    This command is experimental and doesn't do much yet.
62
207
    """
63
208
    takes_options = [
64
 
        Option('inet',
65
 
               help='serve on stdin/out for use from inetd or sshd'),
66
209
        Option('directory',
67
210
               help='serve contents of directory',
68
211
               type=unicode)
69
212
    ]
70
213
 
71
 
    def run(self, inet=None, port=None, directory=None):
 
214
    def run(self, directory=None):
 
215
        lazy_check_versions()
72
216
        from dulwich.server import TCPGitServer
73
217
        from bzrlib.plugins.git.server import BzrBackend
74
218
        from bzrlib.trace import warning
81
225
 
82
226
        backend = BzrBackend(directory)
83
227
 
84
 
        if inet:
85
 
            #def send_fn(data):
86
 
            #    sys.stdout.write(data)
87
 
            #    sys.stdout.flush()
88
 
            #server = GitServer(sys.stdin.read, send_fn)
89
 
            raise NotImplementedError
90
 
        else:
91
 
            server = TCPGitServer(backend, 'localhost')
92
 
            server.serve_forever()
 
228
        server = TCPGitServer(backend, 'localhost')
 
229
        server.serve_forever()
93
230
 
94
231
register_command(cmd_git_serve)
95
232
 
96
233
 
 
234
class cmd_git_import(Command):
 
235
    """Import all branches from a git repository.
 
236
 
 
237
    """
 
238
 
 
239
    takes_args = ["src_location", "dest_location"]
 
240
 
 
241
    def run(self, src_location, dest_location):
 
242
        from bzrlib.bzrdir import BzrDir, format_registry
 
243
        from bzrlib.errors import NoRepositoryPresent, NotBranchError
 
244
        from bzrlib.repository import Repository
 
245
        source_repo = Repository.open(src_location)
 
246
        format = format_registry.make_bzrdir('rich-root-pack')
 
247
        try:
 
248
            target_bzrdir = BzrDir.open(dest_location)
 
249
        except NotBranchError:
 
250
            target_bzrdir = BzrDir.create(dest_location, format=format)
 
251
        try:
 
252
            target_repo = target_bzrdir.open_repository()
 
253
        except NoRepositoryPresent:
 
254
            target_repo = target_bzrdir.create_repository(shared=True)
 
255
 
 
256
        target_repo.fetch(source_repo)
 
257
        for name, ref in source_repo._git.heads().iteritems():
 
258
            head_loc = os.path.join(dest_location, name)
 
259
            try:
 
260
                head_bzrdir = BzrDir.open(head_loc)
 
261
            except NotBranchError:
 
262
                head_bzrdir = BzrDir.create(head_loc, format=format)
 
263
            try:
 
264
                head_branch = head_bzrdir.open_branch()
 
265
            except NotBranchError:
 
266
                head_branch = head_bzrdir.create_branch()
 
267
            head_branch.generate_revision_history(source_repo.get_mapping().revision_id_foreign_to_bzr(ref))
 
268
 
 
269
 
 
270
register_command(cmd_git_import)
 
271
 
 
272
 
97
273
def test_suite():
98
274
    from bzrlib.plugins.git import tests
99
275
    return tests.test_suite()