/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.247 by Jelmer Vernooij
Fix git-import.
127
        pb = ui.ui_factory.nested_progress_bar()
128
        try:
0.377.1 by Jelmer Vernooij
Fix some remote operations and add more tests.
129
            for i, (name, sha) in enumerate(refs.iteritems()):
0.200.1055 by Jelmer Vernooij
Cope with unknown refs.
130
                try:
0.200.1452 by Jelmer Vernooij
Support colocated branches in 'bzr git-import', flatten namespace.
131
                    branch_name = ref_to_branch_name(name)
0.200.1055 by Jelmer Vernooij
Cope with unknown refs.
132
                except ValueError:
133
                    # Not a branch, ignore
0.200.271 by Jelmer Vernooij
Stop importing tags as branches as part of git-import.
134
                    continue
0.377.1 by Jelmer Vernooij
Fix some remote operations and add more tests.
135
                pb.update(gettext("creating branches"), i, len(refs))
0.200.1648 by Jelmer Vernooij
Fix compatibility with newer versions of breezy.
136
                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.
137
                    if name == "HEAD":
138
                        branch_name = None
0.200.1648 by Jelmer Vernooij
Fix compatibility with newer versions of breezy.
139
                    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.
140
                else:
141
                    head_branch = self._get_nested_branch(dest_transport, dest_format, branch_name)
0.200.1487 by Jelmer Vernooij
Use peeling.
142
                revid = mapping.revision_id_foreign_to_bzr(sha)
0.295.1 by Jelmer Vernooij
Split up branch formats.
143
                source_branch = LocalGitBranch(source_repo.controldir, source_repo,
0.200.1487 by Jelmer Vernooij
Use peeling.
144
                    sha)
0.239.1 by Jelmer Vernooij
Avoid re-connecting to fetch tags we already know.
145
                if head_branch.last_revision() != revid:
146
                    head_branch.generate_revision_history(revid)
0.200.273 by Jelmer Vernooij
Fix import of tags in git-import.
147
                source_branch.tags.merge_to(head_branch.tags)
0.200.1390 by Jelmer Vernooij
Set parent path in git-import.
148
                if not head_branch.get_parent():
0.200.1452 by Jelmer Vernooij
Support colocated branches in 'bzr git-import', flatten namespace.
149
                    url = urlutils.join_segment_parameters(
0.408.4 by Jelmer Vernooij
Fix some tests.
150
                        source_branch.base, {"branch": urllib.quote(branch_name.encode('utf-8'), '')})
0.200.1390 by Jelmer Vernooij
Set parent path in git-import.
151
                    head_branch.set_parent(url)
0.200.247 by Jelmer Vernooij
Fix git-import.
152
        finally:
153
            pb.finished()
0.200.1469 by Jelmer Vernooij
Add more strings.
154
        trace.note(gettext(
155
            "Use 'bzr checkout' to create a working tree in "
156
            "the newly created branches."))
0.200.1143 by Jelmer Vernooij
Add note about creating working trees.
157
0.200.206 by Jelmer Vernooij
Move commands to a separate python module and register them lazily.
158
0.200.422 by Jelmer Vernooij
'bzr git-object' without arguments now prints the available git objects.
159
class cmd_git_object(Command):
160
    """List or display Git objects by SHA.
0.200.695 by Jelmer Vernooij
Clean up trailing whitespace.
161
0.200.422 by Jelmer Vernooij
'bzr git-object' without arguments now prints the available git objects.
162
    Cat a particular object's Git representation if a SHA is specified.
163
    List all available SHAs otherwise.
164
    """
0.200.419 by Jelmer Vernooij
Add hidden git-cat command that can cat git objects from Bazaar repositories.
165
166
    hidden = True
167
0.200.422 by Jelmer Vernooij
'bzr git-object' without arguments now prints the available git objects.
168
    aliases = ["git-objects", "git-cat"]
169
    takes_args = ["sha1?"]
0.200.695 by Jelmer Vernooij
Clean up trailing whitespace.
170
    takes_options = [Option('directory',
0.200.440 by Jelmer Vernooij
Remove silly mapping of timezones; dulwich uses offsets now as well.
171
        short_name='d',
0.200.424 by Jelmer Vernooij
Fix formatting.
172
        help='Location of repository.', type=unicode),
173
        Option('pretty', help='Pretty-print objects.')]
0.200.423 by Jelmer Vernooij
Support pretty-printing git objects.
174
    encoding_type = 'exact'
0.200.419 by Jelmer Vernooij
Add hidden git-cat command that can cat git objects from Bazaar repositories.
175
0.200.423 by Jelmer Vernooij
Support pretty-printing git objects.
176
    @display_command
177
    def run(self, sha1=None, directory=".", pretty=False):
0.200.1644 by Jelmer Vernooij
More relative imports.
178
        from ...errors import (
0.200.419 by Jelmer Vernooij
Add hidden git-cat command that can cat git objects from Bazaar repositories.
179
            BzrCommandError,
180
            )
0.200.1648 by Jelmer Vernooij
Fix compatibility with newer versions of breezy.
181
        from ...controldir import (
182
            ControlDir,
0.200.419 by Jelmer Vernooij
Add hidden git-cat command that can cat git objects from Bazaar repositories.
183
            )
0.200.1644 by Jelmer Vernooij
More relative imports.
184
        from .object_store import (
0.200.1484 by Jelmer Vernooij
Fix import.
185
            get_object_store,
186
            )
0.200.1644 by Jelmer Vernooij
More relative imports.
187
        from . import gettext
0.200.1648 by Jelmer Vernooij
Fix compatibility with newer versions of breezy.
188
        controldir, _ = ControlDir.open_containing(directory)
189
        repo = controldir.find_repository()
0.200.452 by Jelmer Vernooij
Rename converter -> object_store, provide utility function for getting ObjectStore's.
190
        object_store = get_object_store(repo)
0.200.1788 by Jelmer Vernooij
Use context managers.
191
        with object_store.lock_read():
0.200.422 by Jelmer Vernooij
'bzr git-object' without arguments now prints the available git objects.
192
            if sha1 is not None:
193
                try:
0.200.532 by Jelmer Vernooij
The object store requires plain strings.
194
                    obj = object_store[str(sha1)]
0.200.422 by Jelmer Vernooij
'bzr git-object' without arguments now prints the available git objects.
195
                except KeyError:
0.200.1483 by Jelmer Vernooij
Translate more strings.
196
                    raise BzrCommandError(gettext("Object not found: %s") % sha1)
0.200.423 by Jelmer Vernooij
Support pretty-printing git objects.
197
                if pretty:
198
                    text = obj.as_pretty_string()
199
                else:
200
                    text = obj.as_raw_string()
201
                self.outf.write(text)
0.200.419 by Jelmer Vernooij
Add hidden git-cat command that can cat git objects from Bazaar repositories.
202
            else:
0.200.422 by Jelmer Vernooij
'bzr git-object' without arguments now prints the available git objects.
203
                for sha1 in object_store:
204
                    self.outf.write("%s\n" % sha1)
0.200.873 by Jelmer Vernooij
Add convenience command for accessing virtual git refs.
205
206
207
class cmd_git_refs(Command):
208
    """Output all of the virtual refs for a repository.
209
210
    """
211
212
    hidden = True
213
0.200.1521 by Jelmer Vernooij
Fix git-refs command.
214
    takes_args = ["location?"]
0.200.873 by Jelmer Vernooij
Add convenience command for accessing virtual git refs.
215
216
    @display_command
0.200.1521 by Jelmer Vernooij
Fix git-refs command.
217
    def run(self, location="."):
0.200.1648 by Jelmer Vernooij
Fix compatibility with newer versions of breezy.
218
        from ...controldir import (
219
            ControlDir,
0.200.873 by Jelmer Vernooij
Add convenience command for accessing virtual git refs.
220
            )
0.200.1644 by Jelmer Vernooij
More relative imports.
221
        from .refs import (
0.200.1487 by Jelmer Vernooij
Use peeling.
222
            get_refs_container,
0.200.873 by Jelmer Vernooij
Add convenience command for accessing virtual git refs.
223
            )
0.200.1644 by Jelmer Vernooij
More relative imports.
224
        from .object_store import (
0.200.873 by Jelmer Vernooij
Add convenience command for accessing virtual git refs.
225
            get_object_store,
226
            )
0.200.1648 by Jelmer Vernooij
Fix compatibility with newer versions of breezy.
227
        controldir, _ = ControlDir.open_containing(location)
228
        repo = controldir.find_repository()
0.200.1212 by Jelmer Vernooij
Support read locking object stores.
229
        object_store = get_object_store(repo)
0.200.1788 by Jelmer Vernooij
Use context managers.
230
        with object_store.lock_read():
0.200.1648 by Jelmer Vernooij
Fix compatibility with newer versions of breezy.
231
            refs = get_refs_container(controldir, object_store)
0.200.1487 by Jelmer Vernooij
Use peeling.
232
            for k, v in refs.as_dict().iteritems():
0.200.873 by Jelmer Vernooij
Add convenience command for accessing virtual git refs.
233
                self.outf.write("%s -> %s\n" % (k, v))
0.200.895 by Jelmer Vernooij
Add initial work on git-apply.
234
235
236
class cmd_git_apply(Command):
237
    """Apply a series of git-am style patches.
238
239
    This command will in the future probably be integrated into 
240
    "bzr pull".
241
    """
242
0.200.1050 by Jelmer Vernooij
Add --force option to 'bzr git-apply'.
243
    takes_options = [
244
        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
245
        Option('force',
246
            help='Apply patches even if tree has uncommitted changes.')
247
        ]
0.200.895 by Jelmer Vernooij
Add initial work on git-apply.
248
    takes_args = ["patches*"]
249
0.200.1049 by Jelmer Vernooij
Add --signoff option to 'bzr git-apply'.
250
    def _apply_patch(self, wt, f, signoff):
251
        """Apply a patch.
252
253
        :param wt: A Bazaar working tree object.
254
        :param f: Patch file to read.
255
        :param signoff: Add Signed-Off-By flag.
256
        """
0.200.1644 by Jelmer Vernooij
More relative imports.
257
        from . import gettext
258
        from ...errors import BzrCommandError
0.200.895 by Jelmer Vernooij
Add initial work on git-apply.
259
        from dulwich.patch import git_am_patch_split
0.200.1043 by Jelmer Vernooij
Finish git-apply command.
260
        import subprocess
0.200.895 by Jelmer Vernooij
Add initial work on git-apply.
261
        (c, diff, version) = git_am_patch_split(f)
0.200.1043 by Jelmer Vernooij
Finish git-apply command.
262
        # FIXME: Cope with git-specific bits in patch
0.200.1298 by Jelmer Vernooij
Fix compatibility with newer versions of dulwich.
263
        # FIXME: Add new files to working tree
264
        p = subprocess.Popen(["patch", "-p1"], stdin=subprocess.PIPE,
265
            cwd=wt.basedir)
0.200.1043 by Jelmer Vernooij
Finish git-apply command.
266
        p.communicate(diff)
267
        exitcode = p.wait()
268
        if exitcode != 0:
0.200.1483 by Jelmer Vernooij
Translate more strings.
269
            raise BzrCommandError(gettext("error running patch"))
0.200.1049 by Jelmer Vernooij
Add --signoff option to 'bzr git-apply'.
270
        message = c.message
271
        if signoff:
272
            signed_off_by = wt.branch.get_config().username()
273
            message += "Signed-off-by: %s\n" % signed_off_by.encode('utf-8')
274
        wt.commit(authors=[c.author], message=message)
0.200.895 by Jelmer Vernooij
Add initial work on git-apply.
275
0.200.1050 by Jelmer Vernooij
Add --force option to 'bzr git-apply'.
276
    def run(self, patches_list=None, signoff=False, force=False):
0.200.1644 by Jelmer Vernooij
More relative imports.
277
        from ...errors import UncommittedChanges
278
        from ...workingtree import WorkingTree
0.200.895 by Jelmer Vernooij
Add initial work on git-apply.
279
        if patches_list is None:
280
            patches_list = []
0.200.1040 by Jelmer Vernooij
Error out about applying to a tree with changes.
281
0.200.895 by Jelmer Vernooij
Add initial work on git-apply.
282
        tree, _ = WorkingTree.open_containing(".")
0.200.1050 by Jelmer Vernooij
Add --force option to 'bzr git-apply'.
283
        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.
284
            raise UncommittedChanges(tree)
0.200.1788 by Jelmer Vernooij
Use context managers.
285
        with tree.lock_write():
0.200.895 by Jelmer Vernooij
Add initial work on git-apply.
286
            for patch in patches_list:
0.200.1788 by Jelmer Vernooij
Use context managers.
287
                with open(patch, 'r') as f:
0.200.1049 by Jelmer Vernooij
Add --signoff option to 'bzr git-apply'.
288
                    self._apply_patch(tree, f, signoff=signoff)
0.277.6 by Jelmer Vernooij
Add 'bzr git-push-pristine-tar' command.
289
290
291
class cmd_git_push_pristine_tar_deltas(Command):
292
    """Push pristine tar deltas to a git repository."""
293
294
    takes_options = [Option('directory',
295
        short_name='d',
296
        help='Location of repository.', type=unicode)]
297
    takes_args = ['target', 'package']
298
299
    def run(self, target, package, directory='.'):
0.200.1644 by Jelmer Vernooij
More relative imports.
300
        from ...branch import Branch
301
        from ...errors import (
0.277.6 by Jelmer Vernooij
Add 'bzr git-push-pristine-tar' command.
302
            BzrCommandError,
303
            NoSuchRevision,
304
            )
0.200.1644 by Jelmer Vernooij
More relative imports.
305
        from ...trace import warning
306
        from ...repository import Repository
307
        from .object_store import get_object_store
308
        from .pristine_tar import (
0.277.6 by Jelmer Vernooij
Add 'bzr git-push-pristine-tar' command.
309
            revision_pristine_tar_data,
310
            store_git_pristine_tar_data,
311
            )
312
        source = Branch.open_containing(directory)[0]
313
        target_bzr = Repository.open(target)
314
        target = getattr(target_bzr, '_git', None)
315
        if target is None:
316
            raise BzrCommandError("Target not a git repository")
0.200.1788 by Jelmer Vernooij
Use context managers.
317
        git_store = get_object_store(source.repository)
318
        with git_store.lock_read():
319
            tag_dict = source.tags.get_tag_dict()
320
            for name, revid in tag_dict.iteritems():
321
                try:
322
                    rev = source.repository.get_revision(revid)
323
                except NoSuchRevision:
324
                    continue
325
                try:
326
                    delta, kind = revision_pristine_tar_data(rev)
327
                except KeyError:
328
                    continue
329
                gitid = git_store._lookup_revision_sha1(revid)
330
                if not (name.startswith('upstream/') or name.startswith('upstream-')):
331
                    warning("Unexpected pristine tar revision tagged %s. Ignoring.",
332
                         name)
333
                    continue
334
                upstream_version = name[len("upstream/"):]
335
                filename = '%s_%s.orig.tar.%s' % (package, upstream_version, kind)
336
                if not gitid in target:
337
                    warning("base git id %s for %s missing in target repository",
338
                            gitid, filename)
339
                store_git_pristine_tar_data(target, filename.encode('utf-8'),
340
                    delta, gitid)