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