1
# Copyright (C) 2006-2009 Canonical Ltd
 
 
3
# Authors: Robert Collins <robert.collins@canonical.com>
 
 
4
#          Jelmer Vernooij <jelmer@samba.org>
 
 
5
#          John Carr <john.carr@unrouted.co.uk>
 
 
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.
 
 
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.
 
 
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
 
 
22
"""Git-specific subcommands for Bazaar."""
 
 
24
from bzrlib.commands import Command
 
 
25
from bzrlib.option import Option
 
 
27
class cmd_git_serve(Command):
 
 
28
    """Provide access to a Bazaar branch using the git protocol.
 
 
30
    This command is experimental and doesn't do much yet.
 
 
34
               help='serve contents of directory',
 
 
38
    def run(self, directory=None):
 
 
40
        from dulwich.server import TCPGitServer
 
 
41
        from bzrlib.plugins.git.server import BzrBackend
 
 
42
        from bzrlib.trace import warning
 
 
45
        warning("server support in bzr-git is experimental.")
 
 
48
            directory = os.getcwd()
 
 
50
        backend = BzrBackend(directory)
 
 
52
        server = TCPGitServer(backend, 'localhost')
 
 
53
        server.serve_forever()
 
 
55
class cmd_git_import(Command):
 
 
56
    """Import all branches from a git repository.
 
 
60
    takes_args = ["src_location", "dest_location"]
 
 
62
    def run(self, src_location, dest_location):
 
 
63
        from bzrlib.bzrdir import BzrDir, format_registry
 
 
64
        from bzrlib.errors import NoRepositoryPresent, NotBranchError
 
 
65
        from bzrlib.repository import Repository
 
 
66
        source_repo = Repository.open(src_location)
 
 
67
        format = format_registry.make_bzrdir('rich-root-pack')
 
 
69
            target_bzrdir = BzrDir.open(dest_location)
 
 
70
        except NotBranchError:
 
 
71
            target_bzrdir = BzrDir.create(dest_location, format=format)
 
 
73
            target_repo = target_bzrdir.open_repository()
 
 
74
        except NoRepositoryPresent:
 
 
75
            target_repo = target_bzrdir.create_repository(shared=True)
 
 
77
        target_repo.fetch(source_repo)
 
 
78
        for name, ref in source_repo._git.heads().iteritems():
 
 
79
            head_loc = os.path.join(dest_location, name)
 
 
81
                head_bzrdir = BzrDir.open(head_loc)
 
 
82
            except NotBranchError:
 
 
83
                head_bzrdir = BzrDir.create(head_loc, format=format)
 
 
85
                head_branch = head_bzrdir.open_branch()
 
 
86
            except NotBranchError:
 
 
87
                head_branch = head_bzrdir.create_branch()
 
 
88
            head_branch.generate_revision_history(source_repo.get_mapping().revision_id_foreign_to_bzr(ref))