/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

Fix access to native git repositories.

Show diffs side-by-side

added added

removed removed

Lines of Context:
36
36
 
37
37
    takes_args = ["src_location", "dest_location?"]
38
38
 
 
39
    def _get_colocated_branch(self, target_bzrdir, name):
 
40
        from bzrlib.errors import NotBranchError
 
41
        try:
 
42
            return target_bzrdir.open_branch(name=name)
 
43
        except NotBranchError:
 
44
            return target_bzrdir.create_branch(name=name)
 
45
 
 
46
    def _get_nested_branch(self, dest_transport, dest_format, name):
 
47
        from bzrlib.bzrdir import BzrDir
 
48
        from bzrlib.errors import NotBranchError
 
49
        head_transport = dest_transport.clone(name)
 
50
        try:
 
51
            head_bzrdir = BzrDir.open_from_transport(head_transport)
 
52
        except NotBranchError:
 
53
            head_bzrdir = dest_format.initialize_on_transport_ex(
 
54
                head_transport, create_prefix=True)[1]
 
55
        try:
 
56
            return head_bzrdir.open_branch()
 
57
        except NotBranchError:
 
58
            return head_bzrdir.create_branch()
 
59
 
39
60
    def run(self, src_location, dest_location=None):
40
61
        import os
 
62
        import urllib
41
63
        from bzrlib import (
 
64
            controldir,
 
65
            trace,
42
66
            ui,
43
67
            urlutils,
44
68
            )
50
74
            NoRepositoryPresent,
51
75
            NotBranchError,
52
76
            )
 
77
        from bzrlib.plugins.git import gettext
53
78
        from bzrlib.repository import (
54
79
            InterRepository,
55
80
            Repository,
56
81
            )
 
82
        from bzrlib.transport import get_transport
57
83
        from bzrlib.plugins.git.branch import (
58
84
            GitBranch,
59
 
            extract_tags,
 
85
            )
 
86
        from bzrlib.plugins.git.refs import (
 
87
            ref_to_branch_name,
 
88
            ref_to_tag_name,
60
89
            )
61
90
        from bzrlib.plugins.git.repository import GitRepository
62
91
 
 
92
        dest_format = controldir.ControlDirFormat.get_default_format()
 
93
 
63
94
        if dest_location is None:
64
95
            dest_location = os.path.basename(src_location.rstrip("/\\"))
65
96
 
 
97
        dest_transport = get_transport(dest_location)
 
98
 
66
99
        source_repo = Repository.open(src_location)
67
100
        if not isinstance(source_repo, GitRepository):
68
 
            raise BzrCommandError("%r is not a git repository" % src_location)
 
101
            raise BzrCommandError(gettext("%r is not a git repository") % src_location)
69
102
        try:
70
 
            target_bzrdir = BzrDir.open(dest_location)
 
103
            target_bzrdir = BzrDir.open_from_transport(dest_transport)
71
104
        except NotBranchError:
72
 
            target_bzrdir = BzrDir.create(dest_location)
 
105
            target_bzrdir = dest_format.initialize_on_transport_ex(
 
106
                dest_transport, shared_repo=True)[1]
73
107
        try:
74
108
            target_repo = target_bzrdir.find_repository()
75
109
        except NoRepositoryPresent:
76
110
            target_repo = target_bzrdir.create_repository(shared=True)
77
111
 
78
112
        if not target_repo.supports_rich_root():
79
 
            raise BzrCommandError("Target repository doesn't support rich roots")
 
113
            raise BzrCommandError(gettext("Target repository doesn't support rich roots"))
80
114
 
81
115
        interrepo = InterRepository.get(source_repo, target_repo)
82
116
        mapping = source_repo.get_mapping()
83
117
        refs = interrepo.fetch()
84
 
        tags = {}
85
 
        for k, v in extract_tags(refs).iteritems():
86
 
            tags[k] = mapping.revision_id_foreign_to_bzr(v)
 
118
        refs_dict = refs.as_dict()
87
119
        pb = ui.ui_factory.nested_progress_bar()
88
120
        try:
89
 
            for i, (name, ref) in enumerate(refs.iteritems()):
90
 
                if name.startswith("refs/tags/"):
 
121
            for i, (name, sha) in enumerate(refs_dict.iteritems()):
 
122
                try:
 
123
                    branch_name = ref_to_branch_name(name)
 
124
                except ValueError:
 
125
                    # Not a branch, ignore
91
126
                    continue
92
 
                pb.update("creating branches", i, len(refs))
93
 
                head_loc = os.path.join(dest_location, name)
94
 
                try:
95
 
                    head_bzrdir = BzrDir.open(head_loc)
96
 
                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)
101
 
                try:
102
 
                    head_branch = head_bzrdir.open_branch()
103
 
                except NotBranchError:
104
 
                    head_branch = head_bzrdir.create_branch()
105
 
                revid = mapping.revision_id_foreign_to_bzr(ref)
 
127
                pb.update(gettext("creating branches"), i, len(refs_dict))
 
128
                if getattr(target_bzrdir._format, "colocated_branches", False):
 
129
                    if name == "HEAD":
 
130
                        branch_name = None
 
131
                    head_branch = self._get_colocated_branch(target_bzrdir, branch_name)
 
132
                else:
 
133
                    head_branch = self._get_nested_branch(dest_transport, dest_format, branch_name)
 
134
                revid = mapping.revision_id_foreign_to_bzr(sha)
106
135
                source_branch = GitBranch(source_repo.bzrdir, source_repo,
107
 
                    name, None, tags)
108
 
                source_branch.head = ref
 
136
                    sha)
 
137
                source_branch.head = sha
109
138
                if head_branch.last_revision() != revid:
110
139
                    head_branch.generate_revision_history(revid)
111
140
                source_branch.tags.merge_to(head_branch.tags)
 
141
                if not head_branch.get_parent():
 
142
                    url = urlutils.join_segment_parameters(
 
143
                        source_branch.base, {"ref": urllib.quote(name, '')})
 
144
                    head_branch.set_parent(url)
112
145
        finally:
113
146
            pb.finished()
 
147
        trace.note(gettext(
 
148
            "Use 'bzr checkout' to create a working tree in "
 
149
            "the newly created branches."))
114
150
 
115
151
 
116
152
class cmd_git_object(Command):
138
174
        from bzrlib.bzrdir import (
139
175
            BzrDir,
140
176
            )
 
177
        from bzrlib.plugins.git.object_store import (
 
178
            get_object_store,
 
179
            )
 
180
        from bzrlib.plugins.git import gettext
141
181
        bzrdir, _ = BzrDir.open_containing(directory)
142
182
        repo = bzrdir.find_repository()
143
 
        from bzrlib.plugins.git.object_store import (
144
 
            get_object_store,
145
 
            )
146
183
        object_store = get_object_store(repo)
147
 
        repo.lock_read()
 
184
        object_store.lock_read()
148
185
        try:
149
186
            if sha1 is not None:
150
187
                try:
151
188
                    obj = object_store[str(sha1)]
152
189
                except KeyError:
153
 
                    raise BzrCommandError("Object not found: %s" % sha1)
 
190
                    raise BzrCommandError(gettext("Object not found: %s") % sha1)
154
191
                if pretty:
155
192
                    text = obj.as_pretty_string()
156
193
                else:
160
197
                for sha1 in object_store:
161
198
                    self.outf.write("%s\n" % sha1)
162
199
        finally:
163
 
            repo.unlock()
 
200
            object_store.unlock()
164
201
 
165
202
 
166
203
class cmd_git_refs(Command):
180
217
            BzrDir,
181
218
            )
182
219
        from bzrlib.plugins.git.refs import (
183
 
            BazaarRefsContainer,
 
220
            get_refs_container,
184
221
            )
185
222
        from bzrlib.plugins.git.object_store import (
186
223
            get_object_store,
187
224
            )
188
225
        bzrdir, _ = BzrDir.open_containing(directory)
189
226
        repo = bzrdir.find_repository()
190
 
        repo.lock_read()
 
227
        object_store = get_object_store(repo)
 
228
        object_store.lock_read()
191
229
        try:
192
 
            object_store = get_object_store(repo)
193
 
            refs = BazaarRefsContainer(bzrdir, object_store)
 
230
            refs = get_refs_container(bzrdir)
194
231
            for k, v in refs.as_dict().iteritems():
195
232
                self.outf.write("%s -> %s\n" % (k, v))
196
233
        finally:
197
 
            repo.unlock()
 
234
            object_store.unlock()
198
235
 
199
236
 
200
237
class cmd_git_apply(Command):
204
241
    "bzr pull".
205
242
    """
206
243
 
 
244
    takes_options = [
 
245
        Option('signoff', short_name='s', help='Add a Signed-off-by line.'),
 
246
        Option('force',
 
247
            help='Apply patches even if tree has uncommitted changes.')
 
248
        ]
207
249
    takes_args = ["patches*"]
208
250
 
209
 
    def _apply_patch(self, wt, f):
 
251
    def _apply_patch(self, wt, f, signoff):
 
252
        """Apply a patch.
 
253
 
 
254
        :param wt: A Bazaar working tree object.
 
255
        :param f: Patch file to read.
 
256
        :param signoff: Add Signed-Off-By flag.
 
257
        """
 
258
        from bzrlib.errors import BzrCommandError
210
259
        from dulwich.patch import git_am_patch_split
 
260
        import subprocess
211
261
        (c, diff, version) = git_am_patch_split(f)
212
 
        # FIXME: Process diff
213
 
        wt.commit(committer=c.committer,
214
 
                  message=c.message)
 
262
        # FIXME: Cope with git-specific bits in patch
 
263
        # FIXME: Add new files to working tree
 
264
        p = subprocess.Popen(["patch", "-p1"], stdin=subprocess.PIPE,
 
265
            cwd=wt.basedir)
 
266
        p.communicate(diff)
 
267
        exitcode = p.wait()
 
268
        if exitcode != 0:
 
269
            raise BzrCommandError(gettext("error running patch"))
 
270
        message = c.message
 
271
        if signoff:
 
272
            signed_off_by = wt.branch.get_config().username()
 
273
            message += "Signed-off-by: %s\n" % signed_off_by.encode('utf-8')
 
274
        wt.commit(authors=[c.author], message=message)
215
275
 
216
 
    def run(self, patches_list=None):
 
276
    def run(self, patches_list=None, signoff=False, force=False):
 
277
        from bzrlib.errors import UncommittedChanges
217
278
        from bzrlib.workingtree import WorkingTree
218
279
        if patches_list is None:
219
280
            patches_list = []
220
 
        
 
281
 
221
282
        tree, _ = WorkingTree.open_containing(".")
 
283
        if tree.basis_tree().changes_from(tree).has_changed() and not force:
 
284
            raise UncommittedChanges(tree)
222
285
        tree.lock_write()
223
286
        try:
224
287
            for patch in patches_list:
225
288
                f = open(patch, 'r')
226
289
                try:
227
 
                    self._apply_patch(tree, f)
 
290
                    self._apply_patch(tree, f, signoff=signoff)
228
291
                finally:
229
292
                    f.close()
230
293
        finally: