/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

Use transports in git-import.

Show diffs side-by-side

added added

removed removed

Lines of Context:
37
37
    takes_args = ["src_location", "dest_location?"]
38
38
 
39
39
    def run(self, src_location, dest_location=None):
 
40
        from collections import defaultdict
40
41
        import os
41
42
        from bzrlib import (
 
43
            controldir,
42
44
            ui,
43
 
            urlutils,
44
45
            )
45
46
        from bzrlib.bzrdir import (
46
47
            BzrDir,
54
55
            InterRepository,
55
56
            Repository,
56
57
            )
 
58
        from bzrlib.transport import get_transport
57
59
        from bzrlib.plugins.git.branch import (
58
60
            GitBranch,
59
61
            extract_tags,
60
62
            )
 
63
        from bzrlib.plugins.git.refs import ref_to_branch_name
61
64
        from bzrlib.plugins.git.repository import GitRepository
62
65
 
 
66
        dest_format = controldir.ControlDirFormat.get_default_format()
 
67
 
63
68
        if dest_location is None:
64
69
            dest_location = os.path.basename(src_location.rstrip("/\\"))
65
70
 
 
71
        dest_transport = get_transport(dest_location)
 
72
 
66
73
        source_repo = Repository.open(src_location)
67
74
        if not isinstance(source_repo, GitRepository):
68
75
            raise BzrCommandError("%r is not a git repository" % src_location)
69
76
        try:
70
 
            target_bzrdir = BzrDir.open(dest_location)
 
77
            target_bzrdir = BzrDir.open_from_transport(dest_transport)
71
78
        except NotBranchError:
72
 
            target_bzrdir = BzrDir.create(dest_location)
 
79
            target_bzrdir = dest_format.initialize_on_transport_ex(
 
80
                dest_transport)
73
81
        try:
74
82
            target_repo = target_bzrdir.find_repository()
75
83
        except NoRepositoryPresent:
81
89
        interrepo = InterRepository.get(source_repo, target_repo)
82
90
        mapping = source_repo.get_mapping()
83
91
        refs = interrepo.fetch()
 
92
        unpeeled_tags = defaultdict(set)
84
93
        tags = {}
85
 
        for k, v in extract_tags(refs).iteritems():
86
 
            tags[k] = mapping.revision_id_foreign_to_bzr(v)
 
94
        for k, (peeled, unpeeled) in extract_tags(refs).iteritems():
 
95
            tags[k] = mapping.revision_id_foreign_to_bzr(peeled)
 
96
            if unpeeled is not None:
 
97
                unpeeled_tags[peeled].add(unpeeled)
 
98
        # FIXME: Store unpeeled tag map
87
99
        pb = ui.ui_factory.nested_progress_bar()
88
100
        try:
89
101
            for i, (name, ref) in enumerate(refs.iteritems()):
90
 
                if name.startswith("refs/tags/"):
 
102
                try:
 
103
                    ref_to_branch_name(name)
 
104
                except ValueError:
 
105
                    # Not a branch, ignore
91
106
                    continue
92
107
                pb.update("creating branches", i, len(refs))
93
 
                head_loc = os.path.join(dest_location, name)
 
108
                head_transport = dest_transport.clone(name)
94
109
                try:
95
 
                    head_bzrdir = BzrDir.open(head_loc)
 
110
                    head_bzrdir = BzrDir.open_from_transport(head_transport)
96
111
                except NotBranchError:
97
 
                    parent_path = urlutils.dirname(head_loc)
98
 
                    if not os.path.isdir(parent_path):
99
 
                        os.makedirs(parent_path)
100
 
                    head_bzrdir = BzrDir.create(head_loc)
 
112
                    head_transport.create_prefix()
 
113
                    head_bzrdir = dest_format.initialize_on_transport_ex(
 
114
                        head_transport, create_prefix=True)
101
115
                try:
102
116
                    head_branch = head_bzrdir.open_branch()
103
117
                except NotBranchError:
204
218
    "bzr pull".
205
219
    """
206
220
 
 
221
    takes_options = [
 
222
        Option('signoff', short_name='s', help='Add a Signed-off-by line.'),
 
223
        'force']
207
224
    takes_args = ["patches*"]
208
225
 
209
 
    def _apply_patch(self, wt, f):
 
226
    def _apply_patch(self, wt, f, signoff):
 
227
        """Apply a patch.
 
228
 
 
229
        :param wt: A Bazaar working tree object.
 
230
        :param f: Patch file to read.
 
231
        :param signoff: Add Signed-Off-By flag.
 
232
        """
 
233
        from bzrlib.errors import BzrCommandError
210
234
        from dulwich.patch import git_am_patch_split
 
235
        import subprocess
211
236
        (c, diff, version) = git_am_patch_split(f)
212
 
        # FIXME: Process diff
213
 
        wt.commit(committer=c.committer,
214
 
                  message=c.message)
 
237
        # FIXME: Cope with git-specific bits in patch
 
238
        p = subprocess.Popen(["patch", "-p1"], stdin=subprocess.PIPE, cwd=wt.basedir)
 
239
        p.communicate(diff)
 
240
        exitcode = p.wait()
 
241
        if exitcode != 0:
 
242
            raise BzrCommandError("error running patch")
 
243
        message = c.message
 
244
        if signoff:
 
245
            signed_off_by = wt.branch.get_config().username()
 
246
            message += "Signed-off-by: %s\n" % signed_off_by.encode('utf-8')
 
247
        wt.commit(authors=[c.author], message=message)
215
248
 
216
 
    def run(self, patches_list=None):
 
249
    def run(self, patches_list=None, signoff=False, force=False):
 
250
        from bzrlib.errors import UncommittedChanges
217
251
        from bzrlib.workingtree import WorkingTree
218
252
        if patches_list is None:
219
253
            patches_list = []
220
 
        
 
254
 
221
255
        tree, _ = WorkingTree.open_containing(".")
 
256
        if tree.basis_tree().changes_from(tree).has_changed() and not force:
 
257
            raise UncommittedChanges(tree)
222
258
        tree.lock_write()
223
259
        try:
224
260
            for patch in patches_list:
225
261
                f = open(patch, 'r')
226
262
                try:
227
 
                    self._apply_patch(tree, f)
 
263
                    self._apply_patch(tree, f, signoff=signoff)
228
264
                finally:
229
265
                    f.close()
230
266
        finally: