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