/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

Implement GitRevisionTree.get_file_sha1.

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,
 
44
            trace,
42
45
            ui,
43
 
            urlutils,
44
46
            )
45
47
        from bzrlib.bzrdir import (
46
48
            BzrDir,
54
56
            InterRepository,
55
57
            Repository,
56
58
            )
 
59
        from bzrlib.transport import get_transport
57
60
        from bzrlib.plugins.git.branch import (
58
61
            GitBranch,
59
62
            extract_tags,
60
63
            )
 
64
        from bzrlib.plugins.git.refs import ref_to_branch_name
61
65
        from bzrlib.plugins.git.repository import GitRepository
62
66
 
 
67
        dest_format = controldir.ControlDirFormat.get_default_format()
 
68
 
63
69
        if dest_location is None:
64
70
            dest_location = os.path.basename(src_location.rstrip("/\\"))
65
71
 
 
72
        dest_transport = get_transport(dest_location)
 
73
 
66
74
        source_repo = Repository.open(src_location)
67
75
        if not isinstance(source_repo, GitRepository):
68
76
            raise BzrCommandError("%r is not a git repository" % src_location)
69
77
        try:
70
 
            target_bzrdir = BzrDir.open(dest_location)
 
78
            target_bzrdir = BzrDir.open_from_transport(dest_transport)
71
79
        except NotBranchError:
72
 
            target_bzrdir = BzrDir.create(dest_location)
 
80
            target_bzrdir = dest_format.initialize_on_transport_ex(
 
81
                dest_transport, shared_repo=True)[1]
73
82
        try:
74
83
            target_repo = target_bzrdir.find_repository()
75
84
        except NoRepositoryPresent:
81
90
        interrepo = InterRepository.get(source_repo, target_repo)
82
91
        mapping = source_repo.get_mapping()
83
92
        refs = interrepo.fetch()
 
93
        unpeeled_tags = defaultdict(set)
84
94
        tags = {}
85
 
        for k, v in extract_tags(refs).iteritems():
86
 
            tags[k] = mapping.revision_id_foreign_to_bzr(v)
 
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
87
100
        pb = ui.ui_factory.nested_progress_bar()
88
101
        try:
89
102
            for i, (name, ref) in enumerate(refs.iteritems()):
90
 
                if name.startswith("refs/tags/"):
 
103
                try:
 
104
                    ref_to_branch_name(name)
 
105
                except ValueError:
 
106
                    # Not a branch, ignore
91
107
                    continue
92
108
                pb.update("creating branches", i, len(refs))
93
 
                head_loc = os.path.join(dest_location, name)
 
109
                head_transport = dest_transport.clone(name)
94
110
                try:
95
 
                    head_bzrdir = BzrDir.open(head_loc)
 
111
                    head_bzrdir = BzrDir.open_from_transport(head_transport)
96
112
                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)
 
113
                    head_bzrdir = dest_format.initialize_on_transport_ex(
 
114
                        head_transport, create_prefix=True)[1]
101
115
                try:
102
116
                    head_branch = head_bzrdir.open_branch()
103
117
                except NotBranchError:
111
125
                source_branch.tags.merge_to(head_branch.tags)
112
126
        finally:
113
127
            pb.finished()
 
128
        trace.note("Use 'bzr checkout' to create a working tree in "
 
129
                   "the newly created branches.")
 
130
 
114
131
 
115
132
 
116
133
class cmd_git_object(Command):
144
161
            get_object_store,
145
162
            )
146
163
        object_store = get_object_store(repo)
147
 
        repo.lock_read()
 
164
        object_store.lock_read()
148
165
        try:
149
166
            if sha1 is not None:
150
167
                try:
160
177
                for sha1 in object_store:
161
178
                    self.outf.write("%s\n" % sha1)
162
179
        finally:
163
 
            repo.unlock()
 
180
            object_store.unlock()
164
181
 
165
182
 
166
183
class cmd_git_refs(Command):
187
204
            )
188
205
        bzrdir, _ = BzrDir.open_containing(directory)
189
206
        repo = bzrdir.find_repository()
190
 
        repo.lock_read()
 
207
        object_store = get_object_store(repo)
 
208
        object_store.lock_read()
191
209
        try:
192
 
            object_store = get_object_store(repo)
193
210
            refs = BazaarRefsContainer(bzrdir, object_store)
194
211
            for k, v in refs.as_dict().iteritems():
195
212
                self.outf.write("%s -> %s\n" % (k, v))
196
213
        finally:
197
 
            repo.unlock()
 
214
            object_store.unlock()
198
215
 
199
216
 
200
217
class cmd_git_apply(Command):
204
221
    "bzr pull".
205
222
    """
206
223
 
 
224
    takes_options = [
 
225
        Option('signoff', short_name='s', help='Add a Signed-off-by line.'),
 
226
        'force']
207
227
    takes_args = ["patches*"]
208
228
 
209
 
    def _apply_patch(self, wt, f):
 
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
210
237
        from dulwich.patch import git_am_patch_split
 
238
        import subprocess
211
239
        (c, diff, version) = git_am_patch_split(f)
212
 
        # FIXME: Process diff
213
 
        wt.commit(committer=c.committer,
214
 
                  message=c.message)
 
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)
215
251
 
216
 
    def run(self, patches_list=None):
 
252
    def run(self, patches_list=None, signoff=False, force=False):
 
253
        from bzrlib.errors import UncommittedChanges
217
254
        from bzrlib.workingtree import WorkingTree
218
255
        if patches_list is None:
219
256
            patches_list = []
220
 
        
 
257
 
221
258
        tree, _ = WorkingTree.open_containing(".")
 
259
        if tree.basis_tree().changes_from(tree).has_changed() and not force:
 
260
            raise UncommittedChanges(tree)
222
261
        tree.lock_write()
223
262
        try:
224
263
            for patch in patches_list:
225
264
                f = open(patch, 'r')
226
265
                try:
227
 
                    self._apply_patch(tree, f)
 
266
                    self._apply_patch(tree, f, signoff=signoff)
228
267
                finally:
229
268
                    f.close()
230
269
        finally: