/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

Create cache dir if it doesn't exist yet.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006 Canonical Ltd
 
1
# Copyright (C) 2006-2009 Canonical Ltd
2
2
 
3
3
# Authors: Robert Collins <robert.collins@canonical.com>
4
4
#          Jelmer Vernooij <jelmer@samba.org>
21
21
 
22
22
"""A GIT branch and repository format implementation for bzr."""
23
23
 
 
24
import os
 
25
import sys
 
26
 
24
27
import bzrlib
25
28
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)]
 
29
 
 
30
# versions ending in 'exp' mean experimental mappings
 
31
# versions ending in 'dev' mean development version
 
32
# versions ending in 'final' mean release (well tested, etc)
 
33
version_info = (0, 3, 3, 'dev', 0)
 
34
 
 
35
if version_info[3] == 'final':
 
36
    version_string = '%d.%d.%d' % version_info[:3]
 
37
else:
 
38
    version_string = '%d.%d.%d%s%d' % version_info
 
39
__version__ = version_string
 
40
 
 
41
MINIMUM_DULWICH_VERSION = (0, 3, 1)
 
42
COMPATIBLE_BZR_VERSIONS = [(1, 14, 0), (1, 15, 0)]
 
43
 
 
44
bzrlib.api.require_any_api(bzrlib, COMPATIBLE_BZR_VERSIONS)
 
45
 
 
46
 
 
47
from bzrlib import (
 
48
    bzrdir,
 
49
    errors as bzr_errors,
 
50
    osutils,
 
51
    )
 
52
from bzrlib.foreign import (
 
53
    foreign_vcs_registry,
 
54
    )
 
55
from bzrlib.lockable_files import (
 
56
    TransportLock,
 
57
    )
 
58
from bzrlib.transport import (
 
59
    register_lazy_transport,
 
60
    register_transport_proto,
 
61
    )
 
62
from bzrlib.commands import (
 
63
    plugin_cmds,
 
64
    )
 
65
from bzrlib.trace import (
 
66
    warning,
 
67
    )
 
68
from bzrlib.version_info_formats.format_rio import (
 
69
    RioVersionInfoBuilder,
 
70
    )
 
71
 
 
72
 
 
73
if getattr(sys, "frozen", None):
 
74
    # allow import additional libs from ./_lib for bzr.exe only
 
75
    sys.path.append(os.path.normpath(os.path.join(os.path.dirname(__file__), '_lib')))
36
76
 
37
77
_versions_checked = False
38
78
def lazy_check_versions():
43
83
    try:
44
84
        from dulwich import __version__ as dulwich_version
45
85
    except ImportError:
46
 
        warning("Please install dulwich, https://launchpad.net/dulwich")
47
 
        raise
 
86
        raise ImportError("bzr-git: Please install dulwich, https://launchpad.net/dulwich")
48
87
    else:
49
88
        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)
 
89
            raise ImportError("bzr-git: Dulwich is too old; at least %d.%d.%d is required" % MINIMUM_DULWICH_VERSION)
54
90
 
55
91
bzrdir.format_registry.register_lazy('git', 
56
92
    "bzrlib.plugins.git.dir", "LocalGitBzrDirFormat",
57
93
    help='GIT repository.', native=False, experimental=True,
58
94
    )
59
95
 
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)
 
96
from bzrlib.revisionspec import revspec_registry
 
97
revspec_registry.register_lazy("git:", "bzrlib.plugins.git.revspec", 
 
98
    "RevisionSpec_git")
 
99
 
69
100
 
70
101
class GitBzrDirFormat(bzrdir.BzrDirFormat):
71
102
    _lock_class = TransportLock
73
104
    def is_supported(self):
74
105
        return True
75
106
 
 
107
    def network_name(self):
 
108
        return "git"
 
109
 
76
110
 
77
111
class LocalGitBzrDirFormat(GitBzrDirFormat):
78
112
    """The .git directory control format."""
92
126
            url = url[len('readonly+'):]
93
127
 
94
128
        try:
95
 
            gitrepo = git.repo.Repo(transport.local_abspath("."))
 
129
            gitrepo = git.repo.Repo(transport.local_abspath(".").encode(osutils._fs_enc))
96
130
        except bzr_errors.NotLocalUrl:
97
131
            raise bzr_errors.NotBranchError(path=transport.base)
98
132
        from bzrlib.plugins.git.dir import LocalGitDir, GitLockableFiles, GitLock
136
170
                "non-local transports")
137
171
 
138
172
        from dulwich.repo import Repo
139
 
        Repo.create(transport.local_abspath(".")) 
 
173
        Repo.create(transport.local_abspath(".").encode(osutils._fs_enc)) 
140
174
        return self.open(transport)
141
175
 
142
176
    def is_supported(self):
154
188
        """Open this directory.
155
189
 
156
190
        """
 
191
        # we dont grok readonly - git isn't integrated with transport.
 
192
        url = transport.base
 
193
        if url.startswith('readonly+'):
 
194
            url = url[len('readonly+'):]
 
195
        if (not url.startswith("git://") and 
 
196
            not url.startswith("git+")):
 
197
            raise bzr_errors.NotBranchError(transport.base)
157
198
        from bzrlib.plugins.git.remote import RemoteGitDir, GitSmartTransport
158
199
        if not isinstance(transport, GitSmartTransport):
159
200
            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
201
        from bzrlib.plugins.git.dir import GitLockableFiles, GitLock
166
202
        lockfiles = GitLockableFiles(transport, GitLock())
167
203
        return RemoteGitDir(transport, lockfiles, self)
169
205
    @classmethod
170
206
    def probe_transport(klass, transport):
171
207
        """Our format is present if the transport ends in '.not/'."""
 
208
        url = transport.base
 
209
        if url.startswith('readonly+'):
 
210
            url = url[len('readonly+'):]
 
211
        if (not url.startswith("git://") and 
 
212
            not url.startswith("git+")):
 
213
            raise bzr_errors.NotBranchError(transport.base)
172
214
        # little ugly, but works
173
215
        format = klass()
174
216
        from bzrlib.plugins.git.remote import GitSmartTransport
175
217
        if not isinstance(transport, GitSmartTransport):
176
218
            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)
 
219
        return format
187
220
 
188
221
    def get_format_description(self):
189
222
        return "Remote Git Repository"
198
231
bzrdir.BzrDirFormat.register_control_format(LocalGitBzrDirFormat)
199
232
bzrdir.BzrDirFormat.register_control_format(RemoteGitBzrDirFormat)
200
233
 
 
234
register_transport_proto('git://', 
 
235
        help="Access using the Git smart server protocol.")
 
236
register_transport_proto('git+ssh://', 
 
237
        help="Access using the Git smart server protocol over SSH.")
 
238
 
201
239
register_lazy_transport("git://", 'bzrlib.plugins.git.remote',
202
 
                        'GitSmartTransport')
 
240
                        'TCPGitSmartTransport')
 
241
register_lazy_transport("git+ssh://", 'bzrlib.plugins.git.remote',
 
242
                        'SSHGitSmartTransport')
203
243
 
204
244
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
 
 
 
245
    "bzrlib.plugins.git.mapping", "foreign_git", "Stupid content tracker")
 
246
 
 
247
plugin_cmds.register_lazy("cmd_git_import", [], "bzrlib.plugins.git.commands")
 
248
plugin_cmds.register_lazy("cmd_git_object", ["git-objects", "git-cat"], 
 
249
    "bzrlib.plugins.git.commands")
 
250
 
 
251
def update_stanza(rev, stanza):
 
252
    mapping = getattr(rev, "mapping", None)
 
253
    if mapping is not None and mapping.revid_prefix.startswith("git-"):
 
254
        stanza.add("git-commit", rev.foreign_revid)
 
255
 
 
256
 
 
257
rio_hooks = getattr(RioVersionInfoBuilder, "hooks", None)
 
258
if rio_hooks is not None:
 
259
    rio_hooks.install_named_hook('revision', update_stanza, None)
 
260
 
 
261
 
 
262
try:
 
263
    from bzrlib.transport import transport_server_registry
 
264
except ImportError:
 
265
    pass
 
266
else:
 
267
    transport_server_registry.register_lazy('git',
 
268
        'bzrlib.plugins.git.server', 
 
269
        'serve_git',
 
270
        'Git Smart server protocol over TCP. (default port: 9418)')
 
271
 
 
272
 
 
273
from bzrlib.repository import network_format_registry as repository_network_format_registry
 
274
repository_network_format_registry.register_lazy('git', 
 
275
    'bzrlib.plugins.git.repository', 'GitRepositoryFormat')
 
276
 
 
277
from bzrlib.bzrdir import network_format_registry as bzrdir_network_format_registry
 
278
bzrdir_network_format_registry.register('git', GitBzrDirFormat)
 
279
 
 
280
 
 
281
def get_rich_root_format(stacked=False):
 
282
    if stacked:
 
283
        return bzrdir.format_registry.make_bzrdir("1.9-rich-root")
 
284
    else:
 
285
        return bzrdir.format_registry.make_bzrdir("default-rich-root")
279
286
 
280
287
def test_suite():
281
288
    from bzrlib.plugins.git import tests