/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

More work on roundtrip push support.

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