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