/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.200.1 by Robert Collins
Commit initial content.
1
# Copyright (C) 2006 Canonical Ltd
0.200.201 by Jelmer Vernooij
Try to import nothing other than __init__ when not opening git repositories.
2
0.200.1 by Robert Collins
Commit initial content.
3
# Authors: Robert Collins <robert.collins@canonical.com>
0.200.184 by Jelmer Vernooij
Update authors: line.
4
#          Jelmer Vernooij <jelmer@samba.org>
5
#          John Carr <john.carr@unrouted.co.uk>
0.200.1 by Robert Collins
Commit initial content.
6
#
7
# This program is free software; you can redistribute it and/or modify
8
# it under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 2 of the License, or
10
# (at your option) any later version.
11
#
12
# This program is distributed in the hope that it will be useful,
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with this program; if not, write to the Free Software
19
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20
21
22
"""A GIT branch and repository format implementation for bzr."""
23
0.200.199 by Jelmer Vernooij
Check for bzrlib API version.
24
import bzrlib
25
import bzrlib.api
0.200.203 by Jelmer Vernooij
Fix imports.
26
from bzrlib import bzrdir, errors as bzr_errors
0.200.195 by Jelmer Vernooij
Return mapping in revision_id_bzr_to_foreign() as required by the interface.
27
from bzrlib.foreign import foreign_vcs_registry
0.200.201 by Jelmer Vernooij
Try to import nothing other than __init__ when not opening git repositories.
28
from bzrlib.lockable_files import TransportLock
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
29
from bzrlib.transport import register_lazy_transport
0.217.2 by John Carr
Fix missing imports. Update TCPGitServer instantiation to latest. BzrBackend needs to know which directory its repo is in.
30
from bzrlib.commands import Command, register_command
31
from bzrlib.option import Option
0.200.192 by Jelmer Vernooij
use system-provided dulwich, remove own copy.
32
from bzrlib.trace import warning
33
34
MINIMUM_DULWICH_VERSION = (0, 1, 0)
0.200.204 by Jelmer Vernooij
Support bzr 1.11.
35
COMPATIBLE_BZR_VERSIONS = [(1, 11, 0), (1, 12, 0)]
0.200.192 by Jelmer Vernooij
use system-provided dulwich, remove own copy.
36
0.200.200 by Jelmer Vernooij
Register lazily where possible.
37
_versions_checked = False
38
def lazy_check_versions():
39
    global _versions_checked
40
    if _versions_checked:
41
        return
42
    _versions_checked = True
43
    try:
44
        from dulwich import __version__ as dulwich_version
45
    except ImportError:
46
        warning("Please install dulwich, https://launchpad.net/dulwich")
47
        raise
48
    else:
49
        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
0.200.199 by Jelmer Vernooij
Check for bzrlib API version.
52
53
bzrlib.api.require_any_api(bzrlib, COMPATIBLE_BZR_VERSIONS)
54
0.200.200 by Jelmer Vernooij
Register lazily where possible.
55
bzrdir.format_registry.register_lazy('git', 
56
    "bzrlib.plugins.git.dir", "LocalGitBzrDirFormat",
57
    help='GIT repository.', native=False, experimental=True,
58
    )
0.200.204 by Jelmer Vernooij
Support bzr 1.11.
59
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)
0.200.200 by Jelmer Vernooij
Register lazily where possible.
69
0.200.201 by Jelmer Vernooij
Try to import nothing other than __init__ when not opening git repositories.
70
class GitBzrDirFormat(bzrdir.BzrDirFormat):
71
    _lock_class = TransportLock
72
73
    def is_supported(self):
74
        return True
75
76
77
class LocalGitBzrDirFormat(GitBzrDirFormat):
78
    """The .git directory control format."""
79
80
    @classmethod
81
    def _known_formats(self):
82
        return set([LocalGitBzrDirFormat()])
83
84
    def open(self, transport, _found=None):
85
        """Open this directory.
86
87
        """
88
        import dulwich as git
89
        # we dont grok readonly - git isn't integrated with transport.
90
        url = transport.base
91
        if url.startswith('readonly+'):
92
            url = url[len('readonly+'):]
93
94
        try:
95
            gitrepo = git.repo.Repo(transport.local_abspath("."))
0.200.203 by Jelmer Vernooij
Fix imports.
96
        except bzr_errors.NotLocalUrl:
97
            raise bzr_errors.NotBranchError(path=transport.base)
0.200.201 by Jelmer Vernooij
Try to import nothing other than __init__ when not opening git repositories.
98
        from bzrlib.plugins.git.dir import LocalGitDir, GitLockableFiles, GitLock
99
        lockfiles = GitLockableFiles(transport, GitLock())
100
        return LocalGitDir(transport, lockfiles, gitrepo, self)
101
102
    @classmethod
103
    def probe_transport(klass, transport):
104
        """Our format is present if the transport ends in '.not/'."""
105
        from bzrlib.transport.local import LocalTransport
106
107
        if not isinstance(transport, LocalTransport):
0.200.203 by Jelmer Vernooij
Fix imports.
108
            raise bzr_errors.NotBranchError(path=transport.base)
0.200.201 by Jelmer Vernooij
Try to import nothing other than __init__ when not opening git repositories.
109
110
        # This should quickly filter out most things that are not 
111
        # git repositories, saving us the trouble from loading dulwich.
112
        if not transport.has(".git") and not transport.has("objects"):
0.200.203 by Jelmer Vernooij
Fix imports.
113
            raise bzr_errors.NotBranchError(path=transport.base)
0.200.201 by Jelmer Vernooij
Try to import nothing other than __init__ when not opening git repositories.
114
115
        import dulwich as git
116
        format = klass()
117
        try:
118
            format.open(transport)
119
            return format
120
        except git.errors.NotGitRepository, e:
0.200.203 by Jelmer Vernooij
Fix imports.
121
            raise bzr_errors.NotBranchError(path=transport.base)
122
        raise bzr_errors.NotBranchError(path=transport.base)
0.200.201 by Jelmer Vernooij
Try to import nothing other than __init__ when not opening git repositories.
123
124
    def get_format_description(self):
125
        return "Local Git Repository"
126
127
    def get_format_string(self):
128
        return "Local Git Repository"
129
130
    def initialize_on_transport(self, transport):
131
        from bzrlib.transport.local import LocalTransport
132
133
        if not isinstance(transport, LocalTransport):
134
            raise NotImplementedError(self.initialize, 
135
                "Can't create Git Repositories/branches on "
136
                "non-local transports")
137
138
        from dulwich.repo import Repo
139
        Repo.create(transport.local_abspath(".")) 
140
        return self.open(transport)
141
142
    def is_supported(self):
143
        return True
144
145
146
class RemoteGitBzrDirFormat(GitBzrDirFormat):
147
    """The .git directory control format."""
148
149
    @classmethod
150
    def _known_formats(self):
151
        return set([RemoteGitBzrDirFormat()])
152
153
    def open(self, transport, _found=None):
154
        """Open this directory.
155
156
        """
157
        from bzrlib.plugins.git.remote import RemoteGitDir, GitSmartTransport
158
        if not isinstance(transport, GitSmartTransport):
0.200.203 by Jelmer Vernooij
Fix imports.
159
            raise bzr_errors.NotBranchError(transport.base)
0.200.201 by Jelmer Vernooij
Try to import nothing other than __init__ when not opening git repositories.
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
        from bzrlib.plugins.git.dir import GitLockableFiles, GitLock
166
        lockfiles = GitLockableFiles(transport, GitLock())
167
        return RemoteGitDir(transport, lockfiles, self)
168
169
    @classmethod
170
    def probe_transport(klass, transport):
171
        """Our format is present if the transport ends in '.not/'."""
172
        # little ugly, but works
173
        format = klass()
174
        from bzrlib.plugins.git.remote import GitSmartTransport
175
        if not isinstance(transport, GitSmartTransport):
0.200.203 by Jelmer Vernooij
Fix imports.
176
            raise bzr_errors.NotBranchError(transport.base)
0.200.201 by Jelmer Vernooij
Try to import nothing other than __init__ when not opening git repositories.
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:
0.200.203 by Jelmer Vernooij
Fix imports.
183
            raise bzr_errors.NotBranchError(path=transport.base)
0.200.201 by Jelmer Vernooij
Try to import nothing other than __init__ when not opening git repositories.
184
        else:
185
            return format
0.200.203 by Jelmer Vernooij
Fix imports.
186
        raise bzr_errors.NotBranchError(path=transport.base)
0.200.201 by Jelmer Vernooij
Try to import nothing other than __init__ when not opening git repositories.
187
188
    def get_format_description(self):
189
        return "Remote Git Repository"
190
191
    def get_format_string(self):
192
        return "Remote Git Repository"
193
194
    def initialize_on_transport(self, transport):
0.200.203 by Jelmer Vernooij
Fix imports.
195
        raise bzr_errors.UninitializableFormat(self)
0.200.201 by Jelmer Vernooij
Try to import nothing other than __init__ when not opening git repositories.
196
197
198
bzrdir.BzrDirFormat.register_control_format(LocalGitBzrDirFormat)
199
bzrdir.BzrDirFormat.register_control_format(RemoteGitBzrDirFormat)
0.200.138 by Jelmer Vernooij
Add initial infrastructure for accessing remote git repositories.
200
201
register_lazy_transport("git://", 'bzrlib.plugins.git.remote',
202
                        'GitSmartTransport')
0.200.19 by John Arbash Meinel
More refactoring. Add some direct tests for GitModel.
203
0.200.195 by Jelmer Vernooij
Return mapping in revision_id_bzr_to_foreign() as required by the interface.
204
foreign_vcs_registry.register_lazy("git", 
205
                        "bzrlib.plugins.git.mapping", 
206
                        "foreign_git",
207
                        "Stupid content tracker")
0.208.5 by Jelmer Vernooij
Add log show function for git.
208
0.200.114 by Jelmer Vernooij
Fix reporting of git commits in 'bzr log'.
209
0.217.1 by John Carr
Start stubbing out rewritten git-serve
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
0.217.9 by John Carr
Handle git+ssh:// seperately to our git:// server
221
    def run(self, directory=None):
0.200.200 by Jelmer Vernooij
Register lazily where possible.
222
        lazy_check_versions()
0.217.1 by John Carr
Start stubbing out rewritten git-serve
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
0.217.9 by John Carr
Handle git+ssh:// seperately to our git:// server
235
        server = TCPGitServer(backend, 'localhost')
236
        server.serve_forever()
0.217.1 by John Carr
Start stubbing out rewritten git-serve
237
238
register_command(cmd_git_serve)
239
240
0.200.177 by Jelmer Vernooij
Add git-import command.
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
279
0.201.1 by Jelmer Vernooij
Add very small initial testsuite.
280
def test_suite():
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
281
    from bzrlib.plugins.git import tests
282
    return tests.test_suite()