/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.200.206 by Jelmer Vernooij
Move commands to a separate python module and register them lazily.
1
# Copyright (C) 2006-2009 Canonical Ltd
2
3
# Authors: Robert Collins <robert.collins@canonical.com>
4
#          Jelmer Vernooij <jelmer@samba.org>
5
#          John Carr <john.carr@unrouted.co.uk>
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
"""Git-specific subcommands for Bazaar."""
22
0.200.292 by Jelmer Vernooij
Fix formatting.
23
from bzrlib.commands import (
24
    Command,
25
    )
26
from bzrlib.option import (
27
    Option,
28
    )
0.200.206 by Jelmer Vernooij
Move commands to a separate python module and register them lazily.
29
0.200.292 by Jelmer Vernooij
Fix formatting.
30
from bzrlib.plugins.git import (
31
    get_rich_root_format,
32
    )
0.228.1 by Jelmer Vernooij
Add basic tests for local fetch.
33
0.200.206 by Jelmer Vernooij
Move commands to a separate python module and register them lazily.
34
class cmd_git_serve(Command):
35
    """Provide access to a Bazaar branch using the git protocol.
36
37
    This command is experimental and doesn't do much yet.
38
    """
39
    takes_options = [
40
        Option('directory',
41
               help='serve contents of directory',
42
               type=unicode)
43
    ]
44
45
    def run(self, directory=None):
46
        lazy_check_versions()
47
        from dulwich.server import TCPGitServer
48
        from bzrlib.plugins.git.server import BzrBackend
49
        from bzrlib.trace import warning
50
        import os
51
52
        warning("server support in bzr-git is experimental.")
53
54
        if directory is None:
55
            directory = os.getcwd()
56
57
        backend = BzrBackend(directory)
58
59
        server = TCPGitServer(backend, 'localhost')
60
        server.serve_forever()
61
0.200.252 by Jelmer Vernooij
Clarify history, copyright.
62
0.200.206 by Jelmer Vernooij
Move commands to a separate python module and register them lazily.
63
class cmd_git_import(Command):
64
    """Import all branches from a git repository.
65
66
    """
67
0.200.234 by Jelmer Vernooij
Derive to_location from from_location, simialr to bzr branch.
68
    takes_args = ["src_location", "dest_location?"]
0.200.206 by Jelmer Vernooij
Move commands to a separate python module and register them lazily.
69
0.200.234 by Jelmer Vernooij
Derive to_location from from_location, simialr to bzr branch.
70
    def run(self, src_location, dest_location=None):
0.200.247 by Jelmer Vernooij
Fix git-import.
71
        import os
72
        from bzrlib import (
73
            ui,
74
            urlutils,
75
            )
76
        from bzrlib.bzrdir import (
77
            BzrDir,
78
            format_registry,
79
            )
0.200.243 by Jelmer Vernooij
Error out on non-git repositories.
80
        from bzrlib.errors import (
81
            BzrCommandError,
82
            NoRepositoryPresent,
83
            NotBranchError,
84
            )
0.200.306 by Jelmer Vernooij
Fix tests, split up InterGitNonGitRepository.
85
        from bzrlib.repository import (
86
            InterRepository,
87
            Repository,
88
            )
0.200.273 by Jelmer Vernooij
Fix import of tags in git-import.
89
        from bzrlib.plugins.git.branch import GitBranch
0.200.243 by Jelmer Vernooij
Error out on non-git repositories.
90
        from bzrlib.plugins.git.repository import GitRepository
0.200.234 by Jelmer Vernooij
Derive to_location from from_location, simialr to bzr branch.
91
92
        if dest_location is None:
93
            dest_location = os.path.basename(src_location.rstrip("/\\"))
94
0.200.206 by Jelmer Vernooij
Move commands to a separate python module and register them lazily.
95
        source_repo = Repository.open(src_location)
0.200.243 by Jelmer Vernooij
Error out on non-git repositories.
96
        if not isinstance(source_repo, GitRepository):
97
            raise BzrCommandError("%r is not a git repository" % src_location)
0.228.1 by Jelmer Vernooij
Add basic tests for local fetch.
98
        format = get_rich_root_format()
0.200.206 by Jelmer Vernooij
Move commands to a separate python module and register them lazily.
99
        try:
100
            target_bzrdir = BzrDir.open(dest_location)
101
        except NotBranchError:
102
            target_bzrdir = BzrDir.create(dest_location, format=format)
103
        try:
104
            target_repo = target_bzrdir.open_repository()
105
        except NoRepositoryPresent:
106
            target_repo = target_bzrdir.create_repository(shared=True)
107
0.200.306 by Jelmer Vernooij
Fix tests, split up InterGitNonGitRepository.
108
        interrepo = InterRepository.get(source_repo, target_repo)
0.200.247 by Jelmer Vernooij
Fix git-import.
109
        mapping = source_repo.get_mapping()
110
        refs = interrepo.fetch_refs()
111
        pb = ui.ui_factory.nested_progress_bar()
112
        try:
113
            for i, (name, ref) in enumerate(refs.iteritems()):
0.200.271 by Jelmer Vernooij
Stop importing tags as branches as part of git-import.
114
                if name.startswith("refs/tags/"):
115
                    continue
0.200.247 by Jelmer Vernooij
Fix git-import.
116
                pb.update("creating branches", i, len(refs))
117
                head_loc = os.path.join(dest_location, name)
118
                try:
119
                    head_bzrdir = BzrDir.open(head_loc)
120
                except NotBranchError:
121
                    parent_path = urlutils.dirname(head_loc)
122
                    if not os.path.isdir(parent_path):
123
                        os.makedirs(parent_path)
124
                    head_bzrdir = BzrDir.create(head_loc, format=format)
125
                try:
126
                    head_branch = head_bzrdir.open_branch()
127
                except NotBranchError:
128
                    head_branch = head_bzrdir.create_branch()
0.200.271 by Jelmer Vernooij
Stop importing tags as branches as part of git-import.
129
                revid = mapping.revision_id_foreign_to_bzr(ref)
0.200.273 by Jelmer Vernooij
Fix import of tags in git-import.
130
                source_branch = GitBranch(source_repo.bzrdir, source_repo, 
131
                    name, ref, None)
0.200.247 by Jelmer Vernooij
Fix git-import.
132
                head_branch.generate_revision_history(revid)
0.200.273 by Jelmer Vernooij
Fix import of tags in git-import.
133
                source_branch.tags.merge_to(head_branch.tags)
0.200.247 by Jelmer Vernooij
Fix git-import.
134
        finally:
135
            pb.finished()
0.200.206 by Jelmer Vernooij
Move commands to a separate python module and register them lazily.
136
0.200.419 by Jelmer Vernooij
Add hidden git-cat command that can cat git objects from Bazaar repositories.
137
0.200.422 by Jelmer Vernooij
'bzr git-object' without arguments now prints the available git objects.
138
def repo_get_object_store(repo):
139
    from bzrlib.plugins.git.converter import (
140
        BazaarObjectStore,
141
        )
142
    from bzrlib.plugins.git.mapping import (
143
        default_mapping,
144
        )
145
    from bzrlib.plugins.git.repository import (
146
        GitRepository,
147
        )
148
    if isinstance(repo, GitRepository):
149
        return repo._git.object_store
150
    else:
151
        return BazaarObjectStore(repo, default_mapping)
152
153
154
class cmd_git_object(Command):
155
    """List or display Git objects by SHA.
156
    
157
    Cat a particular object's Git representation if a SHA is specified.
158
    List all available SHAs otherwise.
159
    """
0.200.419 by Jelmer Vernooij
Add hidden git-cat command that can cat git objects from Bazaar repositories.
160
161
    hidden = True
162
0.200.422 by Jelmer Vernooij
'bzr git-object' without arguments now prints the available git objects.
163
    aliases = ["git-objects", "git-cat"]
164
    takes_args = ["sha1?"]
0.200.419 by Jelmer Vernooij
Add hidden git-cat command that can cat git objects from Bazaar repositories.
165
    takes_options = [Option('directory', 
166
        help='location of repository', type=unicode)]
167
0.200.422 by Jelmer Vernooij
'bzr git-object' without arguments now prints the available git objects.
168
    def run(self, sha1=None, directory="."):
0.200.419 by Jelmer Vernooij
Add hidden git-cat command that can cat git objects from Bazaar repositories.
169
        from bzrlib.errors import (
170
            BzrCommandError,
171
            )
172
        from bzrlib.bzrdir import (
173
            BzrDir,
174
            )
175
        bzrdir, _ = BzrDir.open_containing(directory)
176
        repo = bzrdir.find_repository()
0.200.422 by Jelmer Vernooij
'bzr git-object' without arguments now prints the available git objects.
177
        object_store = repo_get_object_store(repo)
0.200.419 by Jelmer Vernooij
Add hidden git-cat command that can cat git objects from Bazaar repositories.
178
        repo.lock_read()
179
        try:
0.200.422 by Jelmer Vernooij
'bzr git-object' without arguments now prints the available git objects.
180
            if sha1 is not None:
181
                try:
182
                    obj = object_store[sha1]
183
                except KeyError:
184
                    raise BzrCommandError("Object not found: %s" % sha1)
185
                self.outf.write(obj.as_raw_string())
0.200.419 by Jelmer Vernooij
Add hidden git-cat command that can cat git objects from Bazaar repositories.
186
            else:
0.200.422 by Jelmer Vernooij
'bzr git-object' without arguments now prints the available git objects.
187
                for sha1 in object_store:
188
                    self.outf.write("%s\n" % sha1)
0.200.419 by Jelmer Vernooij
Add hidden git-cat command that can cat git objects from Bazaar repositories.
189
        finally:
190
            repo.unlock()