/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

Register lazily where possible.

Show diffs side-by-side

added added

removed removed

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