/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 commands.py

  • Committer: Invité
  • Date: 2009-05-05 20:43:26 UTC
  • mto: (0.200.441 trunk)
  • mto: This revision was merged to the branch mainline in revision 6960.
  • Revision ID: guest@mirexpress-20090505204326-n0vcprylu2hyzq4v
Ensure git plugin is loaded in bzr-*-pack

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
# along with this program; if not, write to the Free Software
19
19
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20
20
 
21
 
 
22
21
"""Git-specific subcommands for Bazaar."""
23
22
 
24
 
from bzrlib.commands import Command
25
 
from bzrlib.option import Option
 
23
from bzrlib.commands import (
 
24
    Command,
 
25
    display_command,
 
26
    )
 
27
from bzrlib.option import (
 
28
    Option,
 
29
    )
 
30
 
 
31
from bzrlib.plugins.git import (
 
32
    get_rich_root_format,
 
33
    )
26
34
 
27
35
class cmd_git_serve(Command):
28
36
    """Provide access to a Bazaar branch using the git protocol.
52
60
        server = TCPGitServer(backend, 'localhost')
53
61
        server.serve_forever()
54
62
 
 
63
 
55
64
class cmd_git_import(Command):
56
65
    """Import all branches from a git repository.
57
66
 
58
67
    """
59
68
 
60
 
    takes_args = ["src_location", "dest_location"]
61
 
 
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
 
69
    takes_args = ["src_location", "dest_location?"]
 
70
 
 
71
    def run(self, src_location, dest_location=None):
 
72
        import os
 
73
        from bzrlib import (
 
74
            ui,
 
75
            urlutils,
 
76
            )
 
77
        from bzrlib.bzrdir import (
 
78
            BzrDir,
 
79
            format_registry,
 
80
            )
 
81
        from bzrlib.errors import (
 
82
            BzrCommandError,
 
83
            NoRepositoryPresent,
 
84
            NotBranchError,
 
85
            )
 
86
        from bzrlib.repository import (
 
87
            InterRepository,
 
88
            Repository,
 
89
            )
 
90
        from bzrlib.plugins.git.branch import GitBranch
 
91
        from bzrlib.plugins.git.repository import GitRepository
 
92
 
 
93
        if dest_location is None:
 
94
            dest_location = os.path.basename(src_location.rstrip("/\\"))
 
95
 
66
96
        source_repo = Repository.open(src_location)
67
 
        format = format_registry.make_bzrdir('rich-root-pack')
 
97
        if not isinstance(source_repo, GitRepository):
 
98
            raise BzrCommandError("%r is not a git repository" % src_location)
 
99
        format = get_rich_root_format()
68
100
        try:
69
101
            target_bzrdir = BzrDir.open(dest_location)
70
102
        except NotBranchError:
74
106
        except NoRepositoryPresent:
75
107
            target_repo = target_bzrdir.create_repository(shared=True)
76
108
 
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)
80
 
            try:
81
 
                head_bzrdir = BzrDir.open(head_loc)
82
 
            except NotBranchError:
83
 
                head_bzrdir = BzrDir.create(head_loc, format=format)
84
 
            try:
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))
89
 
 
 
109
        interrepo = InterRepository.get(source_repo, target_repo)
 
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()):
 
115
                if name.startswith("refs/tags/"):
 
116
                    continue
 
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()
 
130
                revid = mapping.revision_id_foreign_to_bzr(ref)
 
131
                source_branch = GitBranch(source_repo.bzrdir, source_repo, 
 
132
                    name, ref, None)
 
133
                head_branch.generate_revision_history(revid)
 
134
                source_branch.tags.merge_to(head_branch.tags)
 
135
        finally:
 
136
            pb.finished()
 
137
 
 
138
 
 
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
    """
 
161
 
 
162
    hidden = True
 
163
 
 
164
    aliases = ["git-objects", "git-cat"]
 
165
    takes_args = ["sha1?"]
 
166
    takes_options = [Option('directory', 
 
167
        help='Location of repository.', type=unicode),
 
168
        Option('pretty', help='Pretty-print objects.')]
 
169
    encoding_type = 'exact'
 
170
 
 
171
    @display_command
 
172
    def run(self, sha1=None, directory=".", pretty=False):
 
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()
 
181
        object_store = repo_get_object_store(repo)
 
182
        repo.lock_read()
 
183
        try:
 
184
            if sha1 is not None:
 
185
                try:
 
186
                    obj = object_store[sha1]
 
187
                except KeyError:
 
188
                    raise BzrCommandError("Object not found: %s" % sha1)
 
189
                if pretty:
 
190
                    text = obj.as_pretty_string()
 
191
                else:
 
192
                    text = obj.as_raw_string()
 
193
                self.outf.write(text)
 
194
            else:
 
195
                for sha1 in object_store:
 
196
                    self.outf.write("%s\n" % sha1)
 
197
        finally:
 
198
            repo.unlock()