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