/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:
21
21
 
22
22
"""A GIT branch and repository format implementation for bzr."""
23
23
 
24
 
import os
25
 
import sys
26
 
 
27
24
import bzrlib
28
25
import bzrlib.api
29
 
from bzrlib import bzrdir, errors as bzr_errors
 
26
from bzrlib import bzrdir
30
27
from bzrlib.foreign import foreign_vcs_registry
31
28
from bzrlib.lockable_files import TransportLock
 
29
from bzrlib.revisionspec import revspec_registry
32
30
from bzrlib.transport import register_lazy_transport
33
 
from bzrlib.commands import plugin_cmds
 
31
from bzrlib.commands import Command, register_command
 
32
from bzrlib.option import Option
34
33
from bzrlib.trace import warning
35
34
 
36
35
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')))
 
36
COMPATIBLE_BZR_VERSIONS = [(1, 12, 0)]
42
37
 
43
38
_versions_checked = False
44
39
def lazy_check_versions():
49
44
    try:
50
45
        from dulwich import __version__ as dulwich_version
51
46
    except ImportError:
52
 
        raise ImportError("bzr-git: Please install dulwich, https://launchpad.net/dulwich")
 
47
        warning("Please install dulwich, https://launchpad.net/dulwich")
 
48
        raise
53
49
    else:
54
50
        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)
 
51
            warning("Dulwich is too old; at least %d.%d.%d is required" % MINIMUM_DULWICH_VERSION)
 
52
            raise ImportError
56
53
 
57
54
bzrlib.api.require_any_api(bzrlib, COMPATIBLE_BZR_VERSIONS)
58
55
 
60
57
    "bzrlib.plugins.git.dir", "LocalGitBzrDirFormat",
61
58
    help='GIT repository.', native=False, experimental=True,
62
59
    )
63
 
 
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)
 
60
revspec_registry.register_lazy("git:", "bzrlib.plugins.git.revspec", 
 
61
    "RevisionSpec_git")
73
62
 
74
63
class GitBzrDirFormat(bzrdir.BzrDirFormat):
75
64
    _lock_class = TransportLock
97
86
 
98
87
        try:
99
88
            gitrepo = git.repo.Repo(transport.local_abspath("."))
100
 
        except bzr_errors.NotLocalUrl:
101
 
            raise bzr_errors.NotBranchError(path=transport.base)
 
89
        except errors.bzr_errors.NotLocalUrl:
 
90
            raise errors.bzr_errors.NotBranchError(path=transport.base)
102
91
        from bzrlib.plugins.git.dir import LocalGitDir, GitLockableFiles, GitLock
103
92
        lockfiles = GitLockableFiles(transport, GitLock())
104
93
        return LocalGitDir(transport, lockfiles, gitrepo, self)
109
98
        from bzrlib.transport.local import LocalTransport
110
99
 
111
100
        if not isinstance(transport, LocalTransport):
112
 
            raise bzr_errors.NotBranchError(path=transport.base)
 
101
            raise errors.bzr_errors.NotBranchError(path=transport.base)
113
102
 
114
103
        # This should quickly filter out most things that are not 
115
104
        # git repositories, saving us the trouble from loading dulwich.
116
105
        if not transport.has(".git") and not transport.has("objects"):
117
 
            raise bzr_errors.NotBranchError(path=transport.base)
 
106
            raise errors.bzr_errors.NotBranchError(path=transport.base)
118
107
 
119
108
        import dulwich as git
120
109
        format = klass()
122
111
            format.open(transport)
123
112
            return format
124
113
        except git.errors.NotGitRepository, e:
125
 
            raise bzr_errors.NotBranchError(path=transport.base)
126
 
        raise bzr_errors.NotBranchError(path=transport.base)
 
114
            raise errors.bzr_errors.NotBranchError(path=transport.base)
 
115
        raise errors.bzr_errors.NotBranchError(path=transport.base)
127
116
 
128
117
    def get_format_description(self):
129
118
        return "Local Git Repository"
160
149
        """
161
150
        from bzrlib.plugins.git.remote import RemoteGitDir, GitSmartTransport
162
151
        if not isinstance(transport, GitSmartTransport):
163
 
            raise bzr_errors.NotBranchError(transport.base)
 
152
            raise errors.bzr_errors.NotBranchError(transport.base)
164
153
        # we dont grok readonly - git isn't integrated with transport.
165
154
        url = transport.base
166
155
        if url.startswith('readonly+'):
177
166
        format = klass()
178
167
        from bzrlib.plugins.git.remote import GitSmartTransport
179
168
        if not isinstance(transport, GitSmartTransport):
180
 
            raise bzr_errors.NotBranchError(transport.base)
 
169
            raise errors.bzr_errors.NotBranchError(transport.base)
181
170
        # The only way to know a path exists and contains a valid repository 
182
171
        # is to do a request against it:
183
172
        try:
184
173
            transport.fetch_pack(lambda x: [], None, lambda x: None, 
185
174
                                 lambda x: mutter("git: %s" % x))
186
175
        except errors.git_errors.GitProtocolError:
187
 
            raise bzr_errors.NotBranchError(path=transport.base)
 
176
            raise errors.bzr_errors.NotBranchError(path=transport.base)
188
177
        else:
189
178
            return format
190
 
        raise bzr_errors.NotBranchError(path=transport.base)
 
179
        raise errors.bzr_errors.NotBranchError(path=transport.base)
191
180
 
192
181
    def get_format_description(self):
193
182
        return "Remote Git Repository"
196
185
        return "Remote Git Repository"
197
186
 
198
187
    def initialize_on_transport(self, transport):
199
 
        raise bzr_errors.UninitializableFormat(self)
 
188
        raise errors.bzr_errors.UninitializableFormat(self)
200
189
 
201
190
 
202
191
bzrdir.BzrDirFormat.register_control_format(LocalGitBzrDirFormat)
210
199
                        "foreign_git",
211
200
                        "Stupid content tracker")
212
201
 
213
 
plugin_cmds.register_lazy("cmd_git_serve", [], "bzrlib.plugins.git.commands")
214
 
plugin_cmds.register_lazy("cmd_git_import", [], "bzrlib.plugins.git.commands")
 
202
 
 
203
class cmd_git_serve(Command):
 
204
    """Provide access to a Bazaar branch using the git protocol.
 
205
 
 
206
    This command is experimental and doesn't do much yet.
 
207
    """
 
208
    takes_options = [
 
209
        Option('directory',
 
210
               help='serve contents of directory',
 
211
               type=unicode)
 
212
    ]
 
213
 
 
214
    def run(self, directory=None):
 
215
        lazy_check_versions()
 
216
        from dulwich.server import TCPGitServer
 
217
        from bzrlib.plugins.git.server import BzrBackend
 
218
        from bzrlib.trace import warning
 
219
        import os
 
220
 
 
221
        warning("server support in bzr-git is experimental.")
 
222
 
 
223
        if directory is None:
 
224
            directory = os.getcwd()
 
225
 
 
226
        backend = BzrBackend(directory)
 
227
 
 
228
        server = TCPGitServer(backend, 'localhost')
 
229
        server.serve_forever()
 
230
 
 
231
register_command(cmd_git_serve)
 
232
 
 
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
 
215
272
 
216
273
def test_suite():
217
274
    from bzrlib.plugins.git import tests