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