/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
# Copyright (C) 2006 Canonical Ltd
 
2
# Authors: Robert Collins <robert.collins@canonical.com>
 
3
#          Jelmer Vernooij <jelmer@samba.org>
 
4
#          John Carr <john.carr@unrouted.co.uk>
 
5
#
 
6
# This program is free software; you can redistribute it and/or modify
 
7
# it under the terms of the GNU General Public License as published by
 
8
# the Free Software Foundation; either version 2 of the License, or
 
9
# (at your option) any later version.
 
10
#
 
11
# This program is distributed in the hope that it will be useful,
 
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
# GNU General Public License for more details.
 
15
#
 
16
# You should have received a copy of the GNU General Public License
 
17
# along with this program; if not, write to the Free Software
 
18
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
19
 
 
20
 
 
21
"""A GIT branch and repository format implementation for bzr."""
 
22
 
 
23
import bzrlib
 
24
import bzrlib.api
 
25
from bzrlib import bzrdir
 
26
from bzrlib.foreign import foreign_vcs_registry
 
27
from bzrlib.transport import register_lazy_transport
 
28
from bzrlib.commands import Command, register_command
 
29
from bzrlib.option import Option
 
30
from bzrlib.trace import warning
 
31
 
 
32
MINIMUM_DULWICH_VERSION = (0, 1, 0)
 
33
COMPATIBLE_BZR_VERSIONS = [(1, 12, 0)]
 
34
 
 
35
_versions_checked = False
 
36
def lazy_check_versions():
 
37
    global _versions_checked
 
38
    if _versions_checked:
 
39
        return
 
40
    _versions_checked = True
 
41
    try:
 
42
        from dulwich import __version__ as dulwich_version
 
43
    except ImportError:
 
44
        warning("Please install dulwich, https://launchpad.net/dulwich")
 
45
        raise
 
46
    else:
 
47
        if dulwich_version < MINIMUM_DULWICH_VERSION:
 
48
            warning("Dulwich is too old; at least %d.%d.%d is required" % MINIMUM_DULWICH_VERSION)
 
49
            raise ImportError
 
50
 
 
51
bzrlib.api.require_any_api(bzrlib, COMPATIBLE_BZR_VERSIONS)
 
52
 
 
53
bzrdir.format_registry.register_lazy('git', 
 
54
    "bzrlib.plugins.git.dir", "LocalGitBzrDirFormat",
 
55
    help='GIT repository.', native=False, experimental=True,
 
56
    )
 
57
 
 
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")
 
63
 
 
64
register_lazy_transport("git://", 'bzrlib.plugins.git.remote',
 
65
                        'GitSmartTransport')
 
66
 
 
67
foreign_vcs_registry.register_lazy("git", 
 
68
                        "bzrlib.plugins.git.mapping", 
 
69
                        "foreign_git",
 
70
                        "Stupid content tracker")
 
71
 
 
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
 
 
142
 
 
143
def test_suite():
 
144
    from bzrlib.plugins.git import tests
 
145
    return tests.test_suite()