/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.200.206 by Jelmer Vernooij
Move commands to a separate python module and register them lazily.
1
# Copyright (C) 2006-2009 Canonical Ltd
0.358.2 by Jelmer Vernooij
Refresh copyright headers, add my email.
2
# Copyright (C) 2012-2018 Jelmer Vernooij <jelmer@jelmer.uk>
0.200.206 by Jelmer Vernooij
Move commands to a separate python module and register them lazily.
3
4
# Authors: Robert Collins <robert.collins@canonical.com>
5
#          Jelmer Vernooij <jelmer@samba.org>
6
#          John Carr <john.carr@unrouted.co.uk>
7
#
8
# This program is free software; you can redistribute it and/or modify
9
# it under the terms of the GNU General Public License as published by
10
# the Free Software Foundation; either version 2 of the License, or
11
# (at your option) any later version.
12
#
13
# This program is distributed in the hope that it will be useful,
14
# but WITHOUT ANY WARRANTY; without even the implied warranty of
15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
# GNU General Public License for more details.
17
#
18
# You should have received a copy of the GNU General Public License
19
# along with this program; if not, write to the Free Software
0.358.1 by Jelmer Vernooij
Fix FSF address.
20
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
0.200.206 by Jelmer Vernooij
Move commands to a separate python module and register them lazily.
21
22
"""Git-specific subcommands for Bazaar."""
23
0.200.1594 by Jelmer Vernooij
Use absolute_import everywhere.
24
from __future__ import absolute_import
25
7143.11.1 by Jelmer Vernooij
Remove some unused imports.
26
import breezy.bzr  # noqa: F401
7371.2.1 by Jelmer Vernooij
Fix importing from remote git repositories.
27
from breezy import controldir
6986.2.1 by Jelmer Vernooij
Move breezy.plugins.git to breezy.git.
28
from ..commands import (
0.200.292 by Jelmer Vernooij
Fix formatting.
29
    Command,
0.200.423 by Jelmer Vernooij
Support pretty-printing git objects.
30
    display_command,
0.200.292 by Jelmer Vernooij
Fix formatting.
31
    )
6986.2.1 by Jelmer Vernooij
Move breezy.plugins.git to breezy.git.
32
from ..option import (
0.200.292 by Jelmer Vernooij
Fix formatting.
33
    Option,
7371.2.1 by Jelmer Vernooij
Fix importing from remote git repositories.
34
    RegistryOption,
0.200.292 by Jelmer Vernooij
Fix formatting.
35
    )
6986.2.1 by Jelmer Vernooij
Move breezy.plugins.git to breezy.git.
36
from ..sixish import (
6964.2.4 by Jelmer Vernooij
Fix running on python2.
37
    text_type,
7018.3.7 by Jelmer Vernooij
Fix remaining git tests.
38
    viewitems,
6964.2.4 by Jelmer Vernooij
Fix running on python2.
39
    )
0.200.206 by Jelmer Vernooij
Move commands to a separate python module and register them lazily.
40
0.228.1 by Jelmer Vernooij
Add basic tests for local fetch.
41
0.200.206 by Jelmer Vernooij
Move commands to a separate python module and register them lazily.
42
class cmd_git_import(Command):
43
    """Import all branches from a git repository.
44
45
    """
46
0.200.234 by Jelmer Vernooij
Derive to_location from from_location, simialr to bzr branch.
47
    takes_args = ["src_location", "dest_location?"]
0.200.206 by Jelmer Vernooij
Move commands to a separate python module and register them lazily.
48
0.200.1609 by Jelmer Vernooij
Only create colocated branches in git-import if the --colocated option is specified.
49
    takes_options = [
7143.15.2 by Jelmer Vernooij
Run autopep8.
50
        Option('colocated', help='Create colocated branches.'),
7371.2.1 by Jelmer Vernooij
Fix importing from remote git repositories.
51
        RegistryOption('dest-format',
52
                       help='Specify a format for this branch. '
53
                       'See "help formats" for a full list.',
54
                       lazy_registry=('breezy.controldir', 'format_registry'),
55
                       converter=lambda name: controldir.format_registry.make_controldir(
56
                            name),
57
                       value_switches=True,
58
                       title="Branch format",
59
                       ),
7143.15.2 by Jelmer Vernooij
Run autopep8.
60
        ]
0.200.1609 by Jelmer Vernooij
Only create colocated branches in git-import if the --colocated option is specified.
61
0.200.1648 by Jelmer Vernooij
Fix compatibility with newer versions of breezy.
62
    def _get_colocated_branch(self, target_controldir, name):
6986.2.1 by Jelmer Vernooij
Move breezy.plugins.git to breezy.git.
63
        from ..errors import NotBranchError
0.200.1452 by Jelmer Vernooij
Support colocated branches in 'bzr git-import', flatten namespace.
64
        try:
0.200.1648 by Jelmer Vernooij
Fix compatibility with newer versions of breezy.
65
            return target_controldir.open_branch(name=name)
0.200.1452 by Jelmer Vernooij
Support colocated branches in 'bzr git-import', flatten namespace.
66
        except NotBranchError:
0.200.1648 by Jelmer Vernooij
Fix compatibility with newer versions of breezy.
67
            return target_controldir.create_branch(name=name)
0.200.1452 by Jelmer Vernooij
Support colocated branches in 'bzr git-import', flatten namespace.
68
69
    def _get_nested_branch(self, dest_transport, dest_format, name):
6986.2.1 by Jelmer Vernooij
Move breezy.plugins.git to breezy.git.
70
        from ..controldir import ControlDir
71
        from ..errors import NotBranchError
0.200.1452 by Jelmer Vernooij
Support colocated branches in 'bzr git-import', flatten namespace.
72
        head_transport = dest_transport.clone(name)
73
        try:
0.200.1648 by Jelmer Vernooij
Fix compatibility with newer versions of breezy.
74
            head_controldir = ControlDir.open_from_transport(head_transport)
0.200.1452 by Jelmer Vernooij
Support colocated branches in 'bzr git-import', flatten namespace.
75
        except NotBranchError:
0.200.1648 by Jelmer Vernooij
Fix compatibility with newer versions of breezy.
76
            head_controldir = dest_format.initialize_on_transport_ex(
0.200.1452 by Jelmer Vernooij
Support colocated branches in 'bzr git-import', flatten namespace.
77
                head_transport, create_prefix=True)[1]
78
        try:
0.200.1648 by Jelmer Vernooij
Fix compatibility with newer versions of breezy.
79
            return head_controldir.open_branch()
0.200.1452 by Jelmer Vernooij
Support colocated branches in 'bzr git-import', flatten namespace.
80
        except NotBranchError:
0.200.1648 by Jelmer Vernooij
Fix compatibility with newer versions of breezy.
81
            return head_controldir.create_branch()
0.200.1452 by Jelmer Vernooij
Support colocated branches in 'bzr git-import', flatten namespace.
82
7371.2.1 by Jelmer Vernooij
Fix importing from remote git repositories.
83
    def run(self, src_location, dest_location=None, colocated=False, dest_format=None):
0.200.247 by Jelmer Vernooij
Fix git-import.
84
        import os
6986.2.1 by Jelmer Vernooij
Move breezy.plugins.git to breezy.git.
85
        from .. import (
0.200.1141 by Jelmer Vernooij
Use transports in git-import.
86
            controldir,
0.200.1143 by Jelmer Vernooij
Add note about creating working trees.
87
            trace,
0.200.247 by Jelmer Vernooij
Fix git-import.
88
            ui,
0.200.1390 by Jelmer Vernooij
Set parent path in git-import.
89
            urlutils,
0.200.247 by Jelmer Vernooij
Fix git-import.
90
            )
6986.2.1 by Jelmer Vernooij
Move breezy.plugins.git to breezy.git.
91
        from ..controldir import (
0.200.1648 by Jelmer Vernooij
Fix compatibility with newer versions of breezy.
92
            ControlDir,
0.200.247 by Jelmer Vernooij
Fix git-import.
93
            )
6986.2.1 by Jelmer Vernooij
Move breezy.plugins.git to breezy.git.
94
        from ..errors import (
7143.15.3 by Jelmer Vernooij
Fix pep8 issues in breezy.git.
95
            BzrError,
0.200.243 by Jelmer Vernooij
Error out on non-git repositories.
96
            BzrCommandError,
97
            NoRepositoryPresent,
98
            NotBranchError,
99
            )
7143.11.5 by Jelmer Vernooij
Fix an import.
100
        from ..i18n import gettext
6986.2.1 by Jelmer Vernooij
Move breezy.plugins.git to breezy.git.
101
        from ..repository import (
0.200.306 by Jelmer Vernooij
Fix tests, split up InterGitNonGitRepository.
102
            InterRepository,
103
            Repository,
104
            )
6986.2.1 by Jelmer Vernooij
Move breezy.plugins.git to breezy.git.
105
        from ..transport import get_transport
0.200.1644 by Jelmer Vernooij
More relative imports.
106
        from .branch import (
0.295.1 by Jelmer Vernooij
Split up branch formats.
107
            LocalGitBranch,
0.200.1487 by Jelmer Vernooij
Use peeling.
108
            )
0.200.1644 by Jelmer Vernooij
More relative imports.
109
        from .refs import (
0.200.1487 by Jelmer Vernooij
Use peeling.
110
            ref_to_branch_name,
111
            )
0.200.1644 by Jelmer Vernooij
More relative imports.
112
        from .repository import GitRepository
0.200.234 by Jelmer Vernooij
Derive to_location from from_location, simialr to bzr branch.
113
0.361.1 by Jelmer Vernooij
Don't use assert.
114
        if dest_format is None:
7371.2.1 by Jelmer Vernooij
Fix importing from remote git repositories.
115
            dest_format = controldir.format_registry.make_controldir('default')
0.200.1141 by Jelmer Vernooij
Use transports in git-import.
116
0.200.234 by Jelmer Vernooij
Derive to_location from from_location, simialr to bzr branch.
117
        if dest_location is None:
118
            dest_location = os.path.basename(src_location.rstrip("/\\"))
119
0.200.1141 by Jelmer Vernooij
Use transports in git-import.
120
        dest_transport = get_transport(dest_location)
121
0.200.206 by Jelmer Vernooij
Move commands to a separate python module and register them lazily.
122
        source_repo = Repository.open(src_location)
0.200.243 by Jelmer Vernooij
Error out on non-git repositories.
123
        if not isinstance(source_repo, GitRepository):
7143.15.2 by Jelmer Vernooij
Run autopep8.
124
            raise BzrCommandError(
125
                gettext("%r is not a git repository") % src_location)
0.200.206 by Jelmer Vernooij
Move commands to a separate python module and register them lazily.
126
        try:
0.200.1648 by Jelmer Vernooij
Fix compatibility with newer versions of breezy.
127
            target_controldir = ControlDir.open_from_transport(dest_transport)
0.200.206 by Jelmer Vernooij
Move commands to a separate python module and register them lazily.
128
        except NotBranchError:
0.200.1648 by Jelmer Vernooij
Fix compatibility with newer versions of breezy.
129
            target_controldir = dest_format.initialize_on_transport_ex(
0.200.1142 by Jelmer Vernooij
Fix use of initialize_on_transport_ex.
130
                dest_transport, shared_repo=True)[1]
0.200.206 by Jelmer Vernooij
Move commands to a separate python module and register them lazily.
131
        try:
0.200.1648 by Jelmer Vernooij
Fix compatibility with newer versions of breezy.
132
            target_repo = target_controldir.find_repository()
0.200.206 by Jelmer Vernooij
Move commands to a separate python module and register them lazily.
133
        except NoRepositoryPresent:
0.200.1648 by Jelmer Vernooij
Fix compatibility with newer versions of breezy.
134
            target_repo = target_controldir.create_repository(shared=True)
0.200.206 by Jelmer Vernooij
Move commands to a separate python module and register them lazily.
135
0.200.569 by Jelmer Vernooij
Check for rich root target repository.
136
        if not target_repo.supports_rich_root():
7143.15.2 by Jelmer Vernooij
Run autopep8.
137
            raise BzrCommandError(
138
                gettext("Target repository doesn't support rich roots"))
0.200.569 by Jelmer Vernooij
Check for rich root target repository.
139
0.200.306 by Jelmer Vernooij
Fix tests, split up InterGitNonGitRepository.
140
        interrepo = InterRepository.get(source_repo, target_repo)
0.200.247 by Jelmer Vernooij
Fix git-import.
141
        mapping = source_repo.get_mapping()
7455.2.3 by Jelmer Vernooij
Fix remaining tests.
142
        result = interrepo.fetch()
7143.22.2 by Jelmer Vernooij
use more context libs for progress bars.
143
        with ui.ui_factory.nested_progress_bar() as pb:
7455.2.3 by Jelmer Vernooij
Fix remaining tests.
144
            for i, (name, sha) in enumerate(viewitems(result.refs)):
0.200.1055 by Jelmer Vernooij
Cope with unknown refs.
145
                try:
0.200.1452 by Jelmer Vernooij
Support colocated branches in 'bzr git-import', flatten namespace.
146
                    branch_name = ref_to_branch_name(name)
0.200.1055 by Jelmer Vernooij
Cope with unknown refs.
147
                except ValueError:
148
                    # Not a branch, ignore
0.200.271 by Jelmer Vernooij
Stop importing tags as branches as part of git-import.
149
                    continue
7455.2.3 by Jelmer Vernooij
Fix remaining tests.
150
                pb.update(gettext("creating branches"), i, len(result.refs))
7143.15.3 by Jelmer Vernooij
Fix pep8 issues in breezy.git.
151
                if (getattr(target_controldir._format, "colocated_branches",
152
                            False) and colocated):
0.200.1452 by Jelmer Vernooij
Support colocated branches in 'bzr git-import', flatten namespace.
153
                    if name == "HEAD":
154
                        branch_name = None
7143.15.2 by Jelmer Vernooij
Run autopep8.
155
                    head_branch = self._get_colocated_branch(
156
                        target_controldir, branch_name)
0.200.1452 by Jelmer Vernooij
Support colocated branches in 'bzr git-import', flatten namespace.
157
                else:
7143.15.2 by Jelmer Vernooij
Run autopep8.
158
                    head_branch = self._get_nested_branch(
159
                        dest_transport, dest_format, branch_name)
0.200.1487 by Jelmer Vernooij
Use peeling.
160
                revid = mapping.revision_id_foreign_to_bzr(sha)
7143.15.3 by Jelmer Vernooij
Fix pep8 issues in breezy.git.
161
                source_branch = LocalGitBranch(
162
                    source_repo.controldir, source_repo, sha)
0.239.1 by Jelmer Vernooij
Avoid re-connecting to fetch tags we already know.
163
                if head_branch.last_revision() != revid:
164
                    head_branch.generate_revision_history(revid)
0.200.273 by Jelmer Vernooij
Fix import of tags in git-import.
165
                source_branch.tags.merge_to(head_branch.tags)
0.200.1390 by Jelmer Vernooij
Set parent path in git-import.
166
                if not head_branch.get_parent():
0.200.1452 by Jelmer Vernooij
Support colocated branches in 'bzr git-import', flatten namespace.
167
                    url = urlutils.join_segment_parameters(
7143.15.3 by Jelmer Vernooij
Fix pep8 issues in breezy.git.
168
                        source_branch.base,
169
                        {"branch": urlutils.escape(branch_name)})
0.200.1390 by Jelmer Vernooij
Set parent path in git-import.
170
                    head_branch.set_parent(url)
0.200.1469 by Jelmer Vernooij
Add more strings.
171
        trace.note(gettext(
172
            "Use 'bzr checkout' to create a working tree in "
173
            "the newly created branches."))
0.200.1143 by Jelmer Vernooij
Add note about creating working trees.
174
0.200.206 by Jelmer Vernooij
Move commands to a separate python module and register them lazily.
175
0.200.422 by Jelmer Vernooij
'bzr git-object' without arguments now prints the available git objects.
176
class cmd_git_object(Command):
177
    """List or display Git objects by SHA.
0.200.695 by Jelmer Vernooij
Clean up trailing whitespace.
178
0.200.422 by Jelmer Vernooij
'bzr git-object' without arguments now prints the available git objects.
179
    Cat a particular object's Git representation if a SHA is specified.
180
    List all available SHAs otherwise.
181
    """
0.200.419 by Jelmer Vernooij
Add hidden git-cat command that can cat git objects from Bazaar repositories.
182
183
    hidden = True
184
0.200.422 by Jelmer Vernooij
'bzr git-object' without arguments now prints the available git objects.
185
    aliases = ["git-objects", "git-cat"]
186
    takes_args = ["sha1?"]
0.200.695 by Jelmer Vernooij
Clean up trailing whitespace.
187
    takes_options = [Option('directory',
7143.15.2 by Jelmer Vernooij
Run autopep8.
188
                            short_name='d',
189
                            help='Location of repository.', type=text_type),
190
                     Option('pretty', help='Pretty-print objects.')]
0.200.423 by Jelmer Vernooij
Support pretty-printing git objects.
191
    encoding_type = 'exact'
0.200.419 by Jelmer Vernooij
Add hidden git-cat command that can cat git objects from Bazaar repositories.
192
0.200.423 by Jelmer Vernooij
Support pretty-printing git objects.
193
    @display_command
194
    def run(self, sha1=None, directory=".", pretty=False):
6986.2.1 by Jelmer Vernooij
Move breezy.plugins.git to breezy.git.
195
        from ..errors import (
0.200.419 by Jelmer Vernooij
Add hidden git-cat command that can cat git objects from Bazaar repositories.
196
            BzrCommandError,
197
            )
6986.2.1 by Jelmer Vernooij
Move breezy.plugins.git to breezy.git.
198
        from ..controldir import (
0.200.1648 by Jelmer Vernooij
Fix compatibility with newer versions of breezy.
199
            ControlDir,
0.200.419 by Jelmer Vernooij
Add hidden git-cat command that can cat git objects from Bazaar repositories.
200
            )
0.200.1644 by Jelmer Vernooij
More relative imports.
201
        from .object_store import (
0.200.1484 by Jelmer Vernooij
Fix import.
202
            get_object_store,
203
            )
7233.2.1 by Jelmer Vernooij
Fix import for git-objects.
204
        from ..i18n import gettext
0.200.1648 by Jelmer Vernooij
Fix compatibility with newer versions of breezy.
205
        controldir, _ = ControlDir.open_containing(directory)
206
        repo = controldir.find_repository()
0.200.452 by Jelmer Vernooij
Rename converter -> object_store, provide utility function for getting ObjectStore's.
207
        object_store = get_object_store(repo)
0.200.1788 by Jelmer Vernooij
Use context managers.
208
        with object_store.lock_read():
0.200.422 by Jelmer Vernooij
'bzr git-object' without arguments now prints the available git objects.
209
            if sha1 is not None:
210
                try:
7233.2.2 by Jelmer Vernooij
Fix python3 compat.
211
                    obj = object_store[sha1.encode('ascii')]
0.200.422 by Jelmer Vernooij
'bzr git-object' without arguments now prints the available git objects.
212
                except KeyError:
7143.15.2 by Jelmer Vernooij
Run autopep8.
213
                    raise BzrCommandError(
214
                        gettext("Object not found: %s") % sha1)
0.200.423 by Jelmer Vernooij
Support pretty-printing git objects.
215
                if pretty:
216
                    text = obj.as_pretty_string()
217
                else:
218
                    text = obj.as_raw_string()
219
                self.outf.write(text)
0.200.419 by Jelmer Vernooij
Add hidden git-cat command that can cat git objects from Bazaar repositories.
220
            else:
0.200.422 by Jelmer Vernooij
'bzr git-object' without arguments now prints the available git objects.
221
                for sha1 in object_store:
7233.2.2 by Jelmer Vernooij
Fix python3 compat.
222
                    self.outf.write("%s\n" % sha1.decode('ascii'))
0.200.873 by Jelmer Vernooij
Add convenience command for accessing virtual git refs.
223
224
225
class cmd_git_refs(Command):
226
    """Output all of the virtual refs for a repository.
227
228
    """
229
230
    hidden = True
231
0.200.1521 by Jelmer Vernooij
Fix git-refs command.
232
    takes_args = ["location?"]
0.200.873 by Jelmer Vernooij
Add convenience command for accessing virtual git refs.
233
234
    @display_command
0.200.1521 by Jelmer Vernooij
Fix git-refs command.
235
    def run(self, location="."):
6986.2.1 by Jelmer Vernooij
Move breezy.plugins.git to breezy.git.
236
        from ..controldir import (
0.200.1648 by Jelmer Vernooij
Fix compatibility with newer versions of breezy.
237
            ControlDir,
0.200.873 by Jelmer Vernooij
Add convenience command for accessing virtual git refs.
238
            )
0.200.1644 by Jelmer Vernooij
More relative imports.
239
        from .refs import (
0.200.1487 by Jelmer Vernooij
Use peeling.
240
            get_refs_container,
0.200.873 by Jelmer Vernooij
Add convenience command for accessing virtual git refs.
241
            )
0.200.1644 by Jelmer Vernooij
More relative imports.
242
        from .object_store import (
0.200.873 by Jelmer Vernooij
Add convenience command for accessing virtual git refs.
243
            get_object_store,
244
            )
0.200.1648 by Jelmer Vernooij
Fix compatibility with newer versions of breezy.
245
        controldir, _ = ControlDir.open_containing(location)
246
        repo = controldir.find_repository()
0.200.1212 by Jelmer Vernooij
Support read locking object stores.
247
        object_store = get_object_store(repo)
0.200.1788 by Jelmer Vernooij
Use context managers.
248
        with object_store.lock_read():
0.200.1648 by Jelmer Vernooij
Fix compatibility with newer versions of breezy.
249
            refs = get_refs_container(controldir, object_store)
7018.3.7 by Jelmer Vernooij
Fix remaining git tests.
250
            for k, v in sorted(viewitems(refs.as_dict())):
7143.15.2 by Jelmer Vernooij
Run autopep8.
251
                self.outf.write("%s -> %s\n" %
252
                                (k.decode('utf-8'), v.decode('utf-8')))
0.200.895 by Jelmer Vernooij
Add initial work on git-apply.
253
254
255
class cmd_git_apply(Command):
256
    """Apply a series of git-am style patches.
257
7143.15.3 by Jelmer Vernooij
Fix pep8 issues in breezy.git.
258
    This command will in the future probably be integrated into "bzr pull".
0.200.895 by Jelmer Vernooij
Add initial work on git-apply.
259
    """
260
0.200.1050 by Jelmer Vernooij
Add --force option to 'bzr git-apply'.
261
    takes_options = [
262
        Option('signoff', short_name='s', help='Add a Signed-off-by line.'),
0.272.1 by Martin Packman
Add help for git-apply --force option
263
        Option('force',
7143.15.2 by Jelmer Vernooij
Run autopep8.
264
               help='Apply patches even if tree has uncommitted changes.')
0.272.1 by Martin Packman
Add help for git-apply --force option
265
        ]
0.200.895 by Jelmer Vernooij
Add initial work on git-apply.
266
    takes_args = ["patches*"]
267
0.200.1049 by Jelmer Vernooij
Add --signoff option to 'bzr git-apply'.
268
    def _apply_patch(self, wt, f, signoff):
269
        """Apply a patch.
270
271
        :param wt: A Bazaar working tree object.
272
        :param f: Patch file to read.
273
        :param signoff: Add Signed-Off-By flag.
274
        """
0.200.895 by Jelmer Vernooij
Add initial work on git-apply.
275
        from dulwich.patch import git_am_patch_split
7296.6.1 by Jelmer Vernooij
use standard patch functions.
276
        from breezy.patch import patch_tree
0.200.895 by Jelmer Vernooij
Add initial work on git-apply.
277
        (c, diff, version) = git_am_patch_split(f)
0.200.1043 by Jelmer Vernooij
Finish git-apply command.
278
        # FIXME: Cope with git-specific bits in patch
0.200.1298 by Jelmer Vernooij
Fix compatibility with newer versions of dulwich.
279
        # FIXME: Add new files to working tree
7296.6.1 by Jelmer Vernooij
use standard patch functions.
280
        patch_tree(wt, [diff], strip=1, out=self.outf)
7268.7.1 by Jelmer Vernooij
Fix git-apply.
281
        message = c.message.decode('utf-8')
0.200.1049 by Jelmer Vernooij
Add --signoff option to 'bzr git-apply'.
282
        if signoff:
283
            signed_off_by = wt.branch.get_config().username()
7268.7.1 by Jelmer Vernooij
Fix git-apply.
284
            message += "Signed-off-by: %s\n" % (signed_off_by, )
285
        wt.commit(authors=[c.author.decode('utf-8')], message=message)
0.200.895 by Jelmer Vernooij
Add initial work on git-apply.
286
0.200.1050 by Jelmer Vernooij
Add --force option to 'bzr git-apply'.
287
    def run(self, patches_list=None, signoff=False, force=False):
6986.2.1 by Jelmer Vernooij
Move breezy.plugins.git to breezy.git.
288
        from ..errors import UncommittedChanges
289
        from ..workingtree import WorkingTree
0.200.895 by Jelmer Vernooij
Add initial work on git-apply.
290
        if patches_list is None:
291
            patches_list = []
0.200.1040 by Jelmer Vernooij
Error out about applying to a tree with changes.
292
0.200.895 by Jelmer Vernooij
Add initial work on git-apply.
293
        tree, _ = WorkingTree.open_containing(".")
0.200.1050 by Jelmer Vernooij
Add --force option to 'bzr git-apply'.
294
        if tree.basis_tree().changes_from(tree).has_changed() and not force:
0.200.1040 by Jelmer Vernooij
Error out about applying to a tree with changes.
295
            raise UncommittedChanges(tree)
0.200.1788 by Jelmer Vernooij
Use context managers.
296
        with tree.lock_write():
0.200.895 by Jelmer Vernooij
Add initial work on git-apply.
297
            for patch in patches_list:
0.200.1788 by Jelmer Vernooij
Use context managers.
298
                with open(patch, 'r') as f:
0.200.1049 by Jelmer Vernooij
Add --signoff option to 'bzr git-apply'.
299
                    self._apply_patch(tree, f, signoff=signoff)
0.277.6 by Jelmer Vernooij
Add 'bzr git-push-pristine-tar' command.
300
301
302
class cmd_git_push_pristine_tar_deltas(Command):
303
    """Push pristine tar deltas to a git repository."""
304
305
    takes_options = [Option('directory',
7143.15.2 by Jelmer Vernooij
Run autopep8.
306
                            short_name='d',
307
                            help='Location of repository.', type=text_type)]
0.277.6 by Jelmer Vernooij
Add 'bzr git-push-pristine-tar' command.
308
    takes_args = ['target', 'package']
309
310
    def run(self, target, package, directory='.'):
6986.2.1 by Jelmer Vernooij
Move breezy.plugins.git to breezy.git.
311
        from ..branch import Branch
312
        from ..errors import (
0.277.6 by Jelmer Vernooij
Add 'bzr git-push-pristine-tar' command.
313
            BzrCommandError,
314
            NoSuchRevision,
315
            )
6986.2.1 by Jelmer Vernooij
Move breezy.plugins.git to breezy.git.
316
        from ..trace import warning
317
        from ..repository import Repository
0.200.1644 by Jelmer Vernooij
More relative imports.
318
        from .object_store import get_object_store
319
        from .pristine_tar import (
0.277.6 by Jelmer Vernooij
Add 'bzr git-push-pristine-tar' command.
320
            revision_pristine_tar_data,
321
            store_git_pristine_tar_data,
322
            )
323
        source = Branch.open_containing(directory)[0]
324
        target_bzr = Repository.open(target)
325
        target = getattr(target_bzr, '_git', None)
326
        if target is None:
327
            raise BzrCommandError("Target not a git repository")
0.200.1788 by Jelmer Vernooij
Use context managers.
328
        git_store = get_object_store(source.repository)
329
        with git_store.lock_read():
330
            tag_dict = source.tags.get_tag_dict()
331
            for name, revid in tag_dict.iteritems():
332
                try:
333
                    rev = source.repository.get_revision(revid)
334
                except NoSuchRevision:
335
                    continue
336
                try:
337
                    delta, kind = revision_pristine_tar_data(rev)
338
                except KeyError:
339
                    continue
340
                gitid = git_store._lookup_revision_sha1(revid)
7143.15.3 by Jelmer Vernooij
Fix pep8 issues in breezy.git.
341
                if (not (name.startswith('upstream/') or
342
                         name.startswith('upstream-'))):
343
                    warning(
344
                        "Unexpected pristine tar revision tagged %s. "
345
                        "Ignoring.", name)
0.200.1788 by Jelmer Vernooij
Use context managers.
346
                    continue
347
                upstream_version = name[len("upstream/"):]
7143.15.2 by Jelmer Vernooij
Run autopep8.
348
                filename = '%s_%s.orig.tar.%s' % (
349
                    package, upstream_version, kind)
7143.15.3 by Jelmer Vernooij
Fix pep8 issues in breezy.git.
350
                if gitid not in target:
351
                    warning(
352
                        "base git id %s for %s missing in target repository",
353
                        gitid, filename)
0.200.1788 by Jelmer Vernooij
Use context managers.
354
                store_git_pristine_tar_data(target, filename.encode('utf-8'),
7143.15.2 by Jelmer Vernooij
Run autopep8.
355
                                            delta, gitid)