/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

Add 'github:' directory service.

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