/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.228.1 by Jelmer Vernooij
Add basic tests for local fetch.
31
0.200.206 by Jelmer Vernooij
Move commands to a separate python module and register them lazily.
32
class cmd_git_import(Command):
33
    """Import all branches from a git repository.
34
35
    """
36
0.200.234 by Jelmer Vernooij
Derive to_location from from_location, simialr to bzr branch.
37
    takes_args = ["src_location", "dest_location?"]
0.200.206 by Jelmer Vernooij
Move commands to a separate python module and register them lazily.
38
0.200.234 by Jelmer Vernooij
Derive to_location from from_location, simialr to bzr branch.
39
    def run(self, src_location, dest_location=None):
0.200.1061 by Jelmer Vernooij
Add support for using unpeel map.
40
        from collections import defaultdict
0.200.247 by Jelmer Vernooij
Fix git-import.
41
        import os
0.200.1390 by Jelmer Vernooij
Set parent path in git-import.
42
        import urllib
0.200.247 by Jelmer Vernooij
Fix git-import.
43
        from bzrlib import (
0.200.1141 by Jelmer Vernooij
Use transports in git-import.
44
            controldir,
0.200.1143 by Jelmer Vernooij
Add note about creating working trees.
45
            trace,
0.200.247 by Jelmer Vernooij
Fix git-import.
46
            ui,
0.200.1390 by Jelmer Vernooij
Set parent path in git-import.
47
            urlutils,
0.200.247 by Jelmer Vernooij
Fix git-import.
48
            )
49
        from bzrlib.bzrdir import (
50
            BzrDir,
51
            )
0.200.243 by Jelmer Vernooij
Error out on non-git repositories.
52
        from bzrlib.errors import (
53
            BzrCommandError,
54
            NoRepositoryPresent,
55
            NotBranchError,
56
            )
0.200.306 by Jelmer Vernooij
Fix tests, split up InterGitNonGitRepository.
57
        from bzrlib.repository import (
58
            InterRepository,
59
            Repository,
60
            )
0.200.1141 by Jelmer Vernooij
Use transports in git-import.
61
        from bzrlib.transport import get_transport
0.239.1 by Jelmer Vernooij
Avoid re-connecting to fetch tags we already know.
62
        from bzrlib.plugins.git.branch import (
63
            GitBranch,
64
            extract_tags,
65
            )
0.200.1055 by Jelmer Vernooij
Cope with unknown refs.
66
        from bzrlib.plugins.git.refs import ref_to_branch_name
0.200.243 by Jelmer Vernooij
Error out on non-git repositories.
67
        from bzrlib.plugins.git.repository import GitRepository
0.200.234 by Jelmer Vernooij
Derive to_location from from_location, simialr to bzr branch.
68
0.200.1141 by Jelmer Vernooij
Use transports in git-import.
69
        dest_format = controldir.ControlDirFormat.get_default_format()
70
0.200.234 by Jelmer Vernooij
Derive to_location from from_location, simialr to bzr branch.
71
        if dest_location is None:
72
            dest_location = os.path.basename(src_location.rstrip("/\\"))
73
0.200.1141 by Jelmer Vernooij
Use transports in git-import.
74
        dest_transport = get_transport(dest_location)
75
0.200.206 by Jelmer Vernooij
Move commands to a separate python module and register them lazily.
76
        source_repo = Repository.open(src_location)
0.200.243 by Jelmer Vernooij
Error out on non-git repositories.
77
        if not isinstance(source_repo, GitRepository):
78
            raise BzrCommandError("%r is not a git repository" % src_location)
0.200.206 by Jelmer Vernooij
Move commands to a separate python module and register them lazily.
79
        try:
0.200.1141 by Jelmer Vernooij
Use transports in git-import.
80
            target_bzrdir = BzrDir.open_from_transport(dest_transport)
0.200.206 by Jelmer Vernooij
Move commands to a separate python module and register them lazily.
81
        except NotBranchError:
0.200.1141 by Jelmer Vernooij
Use transports in git-import.
82
            target_bzrdir = dest_format.initialize_on_transport_ex(
0.200.1142 by Jelmer Vernooij
Fix use of initialize_on_transport_ex.
83
                dest_transport, shared_repo=True)[1]
0.200.206 by Jelmer Vernooij
Move commands to a separate python module and register them lazily.
84
        try:
0.200.612 by Jelmer Vernooij
Cope with Dulwich returning KeyError when a commit is not found.
85
            target_repo = target_bzrdir.find_repository()
0.200.206 by Jelmer Vernooij
Move commands to a separate python module and register them lazily.
86
        except NoRepositoryPresent:
87
            target_repo = target_bzrdir.create_repository(shared=True)
88
0.200.569 by Jelmer Vernooij
Check for rich root target repository.
89
        if not target_repo.supports_rich_root():
90
            raise BzrCommandError("Target repository doesn't support rich roots")
91
0.200.306 by Jelmer Vernooij
Fix tests, split up InterGitNonGitRepository.
92
        interrepo = InterRepository.get(source_repo, target_repo)
0.200.247 by Jelmer Vernooij
Fix git-import.
93
        mapping = source_repo.get_mapping()
0.200.1002 by Jelmer Vernooij
Fix regression in git-import.
94
        refs = interrepo.fetch()
0.200.1061 by Jelmer Vernooij
Add support for using unpeel map.
95
        unpeeled_tags = defaultdict(set)
0.200.648 by Jelmer Vernooij
Fix tag handling when encountering packed refs.
96
        tags = {}
0.200.1060 by Jelmer Vernooij
Return unpeeled tags in extract_tags.
97
        for k, (peeled, unpeeled) in extract_tags(refs).iteritems():
98
            tags[k] = mapping.revision_id_foreign_to_bzr(peeled)
99
            if unpeeled is not None:
0.200.1061 by Jelmer Vernooij
Add support for using unpeel map.
100
                unpeeled_tags[peeled].add(unpeeled)
101
        # FIXME: Store unpeeled tag map
0.200.247 by Jelmer Vernooij
Fix git-import.
102
        pb = ui.ui_factory.nested_progress_bar()
103
        try:
104
            for i, (name, ref) in enumerate(refs.iteritems()):
0.200.1055 by Jelmer Vernooij
Cope with unknown refs.
105
                try:
106
                    ref_to_branch_name(name)
107
                except ValueError:
108
                    # Not a branch, ignore
0.200.271 by Jelmer Vernooij
Stop importing tags as branches as part of git-import.
109
                    continue
0.200.247 by Jelmer Vernooij
Fix git-import.
110
                pb.update("creating branches", i, len(refs))
0.200.1141 by Jelmer Vernooij
Use transports in git-import.
111
                head_transport = dest_transport.clone(name)
0.200.247 by Jelmer Vernooij
Fix git-import.
112
                try:
0.200.1141 by Jelmer Vernooij
Use transports in git-import.
113
                    head_bzrdir = BzrDir.open_from_transport(head_transport)
0.200.247 by Jelmer Vernooij
Fix git-import.
114
                except NotBranchError:
0.200.1141 by Jelmer Vernooij
Use transports in git-import.
115
                    head_bzrdir = dest_format.initialize_on_transport_ex(
0.200.1142 by Jelmer Vernooij
Fix use of initialize_on_transport_ex.
116
                        head_transport, create_prefix=True)[1]
0.200.247 by Jelmer Vernooij
Fix git-import.
117
                try:
118
                    head_branch = head_bzrdir.open_branch()
119
                except NotBranchError:
120
                    head_branch = head_bzrdir.create_branch()
0.200.271 by Jelmer Vernooij
Stop importing tags as branches as part of git-import.
121
                revid = mapping.revision_id_foreign_to_bzr(ref)
0.200.695 by Jelmer Vernooij
Clean up trailing whitespace.
122
                source_branch = GitBranch(source_repo.bzrdir, source_repo,
0.239.1 by Jelmer Vernooij
Avoid re-connecting to fetch tags we already know.
123
                    name, None, tags)
0.200.488 by Jelmer Vernooij
Fix git-import.
124
                source_branch.head = ref
0.239.1 by Jelmer Vernooij
Avoid re-connecting to fetch tags we already know.
125
                if head_branch.last_revision() != revid:
126
                    head_branch.generate_revision_history(revid)
0.200.273 by Jelmer Vernooij
Fix import of tags in git-import.
127
                source_branch.tags.merge_to(head_branch.tags)
0.200.1390 by Jelmer Vernooij
Set parent path in git-import.
128
                if not head_branch.get_parent():
129
                    url = urlutils.join_segment_parameters(source_branch.base, {"ref": urllib.quote(name, '')})
130
                    head_branch.set_parent(url)
0.200.247 by Jelmer Vernooij
Fix git-import.
131
        finally:
132
            pb.finished()
0.200.1143 by Jelmer Vernooij
Add note about creating working trees.
133
        trace.note("Use 'bzr checkout' to create a working tree in "
134
                   "the newly created branches.")
135
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
class cmd_git_object(Command):
139
    """List or display Git objects by SHA.
0.200.695 by Jelmer Vernooij
Clean up trailing whitespace.
140
0.200.422 by Jelmer Vernooij
'bzr git-object' without arguments now prints the available git objects.
141
    Cat a particular object's Git representation if a SHA is specified.
142
    List all available SHAs otherwise.
143
    """
0.200.419 by Jelmer Vernooij
Add hidden git-cat command that can cat git objects from Bazaar repositories.
144
145
    hidden = True
146
0.200.422 by Jelmer Vernooij
'bzr git-object' without arguments now prints the available git objects.
147
    aliases = ["git-objects", "git-cat"]
148
    takes_args = ["sha1?"]
0.200.695 by Jelmer Vernooij
Clean up trailing whitespace.
149
    takes_options = [Option('directory',
0.200.440 by Jelmer Vernooij
Remove silly mapping of timezones; dulwich uses offsets now as well.
150
        short_name='d',
0.200.424 by Jelmer Vernooij
Fix formatting.
151
        help='Location of repository.', type=unicode),
152
        Option('pretty', help='Pretty-print objects.')]
0.200.423 by Jelmer Vernooij
Support pretty-printing git objects.
153
    encoding_type = 'exact'
0.200.419 by Jelmer Vernooij
Add hidden git-cat command that can cat git objects from Bazaar repositories.
154
0.200.423 by Jelmer Vernooij
Support pretty-printing git objects.
155
    @display_command
156
    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.
157
        from bzrlib.errors import (
158
            BzrCommandError,
159
            )
160
        from bzrlib.bzrdir import (
161
            BzrDir,
162
            )
163
        bzrdir, _ = BzrDir.open_containing(directory)
164
        repo = bzrdir.find_repository()
0.200.452 by Jelmer Vernooij
Rename converter -> object_store, provide utility function for getting ObjectStore's.
165
        from bzrlib.plugins.git.object_store import (
166
            get_object_store,
167
            )
168
        object_store = get_object_store(repo)
0.200.1212 by Jelmer Vernooij
Support read locking object stores.
169
        object_store.lock_read()
0.200.419 by Jelmer Vernooij
Add hidden git-cat command that can cat git objects from Bazaar repositories.
170
        try:
0.200.422 by Jelmer Vernooij
'bzr git-object' without arguments now prints the available git objects.
171
            if sha1 is not None:
172
                try:
0.200.532 by Jelmer Vernooij
The object store requires plain strings.
173
                    obj = object_store[str(sha1)]
0.200.422 by Jelmer Vernooij
'bzr git-object' without arguments now prints the available git objects.
174
                except KeyError:
175
                    raise BzrCommandError("Object not found: %s" % sha1)
0.200.423 by Jelmer Vernooij
Support pretty-printing git objects.
176
                if pretty:
177
                    text = obj.as_pretty_string()
178
                else:
179
                    text = obj.as_raw_string()
180
                self.outf.write(text)
0.200.419 by Jelmer Vernooij
Add hidden git-cat command that can cat git objects from Bazaar repositories.
181
            else:
0.200.422 by Jelmer Vernooij
'bzr git-object' without arguments now prints the available git objects.
182
                for sha1 in object_store:
183
                    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.
184
        finally:
0.200.1212 by Jelmer Vernooij
Support read locking object stores.
185
            object_store.unlock()
0.200.873 by Jelmer Vernooij
Add convenience command for accessing virtual git refs.
186
187
188
class cmd_git_refs(Command):
189
    """Output all of the virtual refs for a repository.
190
191
    """
192
193
    hidden = True
194
195
    takes_options = [Option('directory',
196
        short_name='d',
197
        help='Location of repository.', type=unicode)]
198
199
    @display_command
200
    def run(self, directory="."):
201
        from bzrlib.bzrdir import (
202
            BzrDir,
203
            )
204
        from bzrlib.plugins.git.refs import (
205
            BazaarRefsContainer,
206
            )
207
        from bzrlib.plugins.git.object_store import (
208
            get_object_store,
209
            )
210
        bzrdir, _ = BzrDir.open_containing(directory)
211
        repo = bzrdir.find_repository()
0.200.1212 by Jelmer Vernooij
Support read locking object stores.
212
        object_store = get_object_store(repo)
213
        object_store.lock_read()
0.200.873 by Jelmer Vernooij
Add convenience command for accessing virtual git refs.
214
        try:
215
            refs = BazaarRefsContainer(bzrdir, object_store)
216
            for k, v in refs.as_dict().iteritems():
217
                self.outf.write("%s -> %s\n" % (k, v))
218
        finally:
0.200.1212 by Jelmer Vernooij
Support read locking object stores.
219
            object_store.unlock()
0.200.895 by Jelmer Vernooij
Add initial work on git-apply.
220
221
222
class cmd_git_apply(Command):
223
    """Apply a series of git-am style patches.
224
225
    This command will in the future probably be integrated into 
226
    "bzr pull".
227
    """
228
0.200.1050 by Jelmer Vernooij
Add --force option to 'bzr git-apply'.
229
    takes_options = [
230
        Option('signoff', short_name='s', help='Add a Signed-off-by line.'),
231
        'force']
0.200.895 by Jelmer Vernooij
Add initial work on git-apply.
232
    takes_args = ["patches*"]
233
0.200.1049 by Jelmer Vernooij
Add --signoff option to 'bzr git-apply'.
234
    def _apply_patch(self, wt, f, signoff):
235
        """Apply a patch.
236
237
        :param wt: A Bazaar working tree object.
238
        :param f: Patch file to read.
239
        :param signoff: Add Signed-Off-By flag.
240
        """
0.200.1043 by Jelmer Vernooij
Finish git-apply command.
241
        from bzrlib.errors import BzrCommandError
0.200.895 by Jelmer Vernooij
Add initial work on git-apply.
242
        from dulwich.patch import git_am_patch_split
0.200.1043 by Jelmer Vernooij
Finish git-apply command.
243
        import subprocess
0.200.895 by Jelmer Vernooij
Add initial work on git-apply.
244
        (c, diff, version) = git_am_patch_split(f)
0.200.1043 by Jelmer Vernooij
Finish git-apply command.
245
        # FIXME: Cope with git-specific bits in patch
0.200.1298 by Jelmer Vernooij
Fix compatibility with newer versions of dulwich.
246
        # FIXME: Add new files to working tree
247
        p = subprocess.Popen(["patch", "-p1"], stdin=subprocess.PIPE,
248
            cwd=wt.basedir)
0.200.1043 by Jelmer Vernooij
Finish git-apply command.
249
        p.communicate(diff)
250
        exitcode = p.wait()
251
        if exitcode != 0:
252
            raise BzrCommandError("error running patch")
0.200.1049 by Jelmer Vernooij
Add --signoff option to 'bzr git-apply'.
253
        message = c.message
254
        if signoff:
255
            signed_off_by = wt.branch.get_config().username()
256
            message += "Signed-off-by: %s\n" % signed_off_by.encode('utf-8')
257
        wt.commit(authors=[c.author], message=message)
0.200.895 by Jelmer Vernooij
Add initial work on git-apply.
258
0.200.1050 by Jelmer Vernooij
Add --force option to 'bzr git-apply'.
259
    def run(self, patches_list=None, signoff=False, force=False):
0.200.1040 by Jelmer Vernooij
Error out about applying to a tree with changes.
260
        from bzrlib.errors import UncommittedChanges
0.200.895 by Jelmer Vernooij
Add initial work on git-apply.
261
        from bzrlib.workingtree import WorkingTree
262
        if patches_list is None:
263
            patches_list = []
0.200.1040 by Jelmer Vernooij
Error out about applying to a tree with changes.
264
0.200.895 by Jelmer Vernooij
Add initial work on git-apply.
265
        tree, _ = WorkingTree.open_containing(".")
0.200.1050 by Jelmer Vernooij
Add --force option to 'bzr git-apply'.
266
        if tree.basis_tree().changes_from(tree).has_changed() and not force:
0.200.1040 by Jelmer Vernooij
Error out about applying to a tree with changes.
267
            raise UncommittedChanges(tree)
0.200.895 by Jelmer Vernooij
Add initial work on git-apply.
268
        tree.lock_write()
269
        try:
270
            for patch in patches_list:
271
                f = open(patch, 'r')
272
                try:
0.200.1049 by Jelmer Vernooij
Add --signoff option to 'bzr git-apply'.
273
                    self._apply_patch(tree, f, signoff=signoff)
0.200.895 by Jelmer Vernooij
Add initial work on git-apply.
274
                finally:
275
                    f.close()
276
        finally:
277
            tree.unlock()