/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/plugins/git/fetch.py

  • Committer: Jelmer Vernooij
  • Date: 2018-06-11 20:48:24 UTC
  • mfrom: (6974.2.1 python3-graph)
  • Revision ID: jelmer@jelmer.uk-20180611204824-zll0a3pzyciekd1f
merge lp:~jelmer/brz/python3-graph

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2008 Canonical Ltd
 
1
# Copyright (C) 2008-2018 Jelmer Vernooij <jelmer@jelmer.uk>
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
 
 
17
 
from bzrlib import osutils
18
 
from bzrlib.errors import InvalidRevisionId
19
 
from bzrlib.inventory import Inventory
20
 
from bzrlib.repository import InterRepository
21
 
from bzrlib.trace import info
22
 
 
23
 
from bzrlib.plugins.git import git
24
 
from bzrlib.plugins.git.repository import LocalGitRepository, GitRepository, GitFormat
25
 
from bzrlib.plugins.git.remote import RemoteGitRepository
26
 
 
27
 
from dulwich.objects import Commit
28
 
 
29
 
from cStringIO import StringIO
30
 
 
31
 
 
32
 
class BzrFetchGraphWalker(object):
33
 
 
34
 
    def __init__(self, repository, mapping):
35
 
        self.repository = repository
36
 
        self.mapping = mapping
37
 
        self.done = set()
38
 
        self.heads = set(repository.all_revision_ids())
39
 
        self.parents = {}
40
 
 
41
 
    def ack(self, sha):
42
 
        revid = self.mapping.revision_id_foreign_to_bzr(sha)
43
 
        self.remove(revid)
44
 
 
45
 
    def remove(self, revid):
46
 
        self.done.add(revid)
47
 
        if ref in self.heads:
48
 
            self.heads.remove(revid)
49
 
        if revid in self.parents:
50
 
            for p in self.parents[revid]:
51
 
                self.remove(p)
52
 
 
53
 
    def next(self):
54
 
        while self.heads:
55
 
            ret = self.heads.pop()
56
 
            ps = self.repository.get_parent_map([ret])[ret]
57
 
            self.parents[ret] = ps
58
 
            self.heads.update([p for p in ps if not p in self.done])
59
 
            try:
60
 
                self.done.add(ret)
61
 
                return self.mapping.revision_id_bzr_to_foreign(ret)
62
 
            except InvalidRevisionId:
63
 
                pass
64
 
        return None
65
 
 
66
 
 
67
 
def import_git_blob(repo, mapping, path, blob):
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
 
 
17
"""Fetching from git into bzr."""
 
18
 
 
19
from __future__ import absolute_import
 
20
 
 
21
from dulwich.objects import (
 
22
    Commit,
 
23
    Tag,
 
24
    Tree,
 
25
    S_IFGITLINK,
 
26
    S_ISGITLINK,
 
27
    ZERO_SHA,
 
28
    )
 
29
from dulwich.object_store import (
 
30
    tree_lookup_path,
 
31
    )
 
32
from dulwich.walk import Walker
 
33
import posixpath
 
34
import stat
 
35
 
 
36
from ... import (
 
37
    debug,
 
38
    errors,
 
39
    osutils,
 
40
    trace,
 
41
    ui,
 
42
    )
 
43
from ...errors import (
 
44
    BzrError,
 
45
    )
 
46
from ...bzr.inventory import (
 
47
    InventoryDirectory,
 
48
    InventoryFile,
 
49
    InventoryLink,
 
50
    TreeReference,
 
51
    )
 
52
from ...repository import (
 
53
    InterRepository,
 
54
    )
 
55
from ...revision import (
 
56
    NULL_REVISION,
 
57
    )
 
58
from ...bzr.inventorytree import InventoryRevisionTree
 
59
from ...testament import (
 
60
    StrictTestament3,
 
61
    )
 
62
from ...tsort import (
 
63
    topo_sort,
 
64
    )
 
65
from ...bzr.versionedfile import (
 
66
    ChunkedContentFactory,
 
67
    )
 
68
 
 
69
from .mapping import (
 
70
    DEFAULT_FILE_MODE,
 
71
    mode_is_executable,
 
72
    mode_kind,
 
73
    warn_unusual_mode,
 
74
    )
 
75
from .object_store import (
 
76
    LRUTreeCache,
 
77
    _tree_to_objects,
 
78
    )
 
79
from .refs import (
 
80
    is_tag,
 
81
    )
 
82
from .remote import (
 
83
    RemoteGitRepository,
 
84
    )
 
85
from .repository import (
 
86
    GitRepository,
 
87
    GitRepositoryFormat,
 
88
    LocalGitRepository,
 
89
    )
 
90
 
 
91
 
 
92
def import_git_blob(texts, mapping, path, name, hexshas,
 
93
        base_bzr_tree, parent_id, revision_id,
 
94
        parent_bzr_trees, lookup_object, modes, store_updater,
 
95
        lookup_file_id):
68
96
    """Import a git blob object into a bzr repository.
69
97
 
70
 
    :param repo: bzr repository
 
98
    :param texts: VersionedFiles to add to
71
99
    :param path: Path in the tree
72
100
    :param blob: A git blob
73
 
    """
74
 
    file_id = mapping.generate_file_id(path)
75
 
    repo.texts.add_lines((file_id, blob.id),
76
 
        [], #FIXME 
77
 
        osutils.split_lines(blob.data))
78
 
    inv.add_path(path, "file", file_id)
79
 
 
80
 
 
81
 
def import_git_tree(repo, mapping, path, tree, inv, lookup_object):
 
101
    :return: Inventory delta for this file
 
102
    """
 
103
    (base_mode, mode) = modes
 
104
    (base_hexsha, hexsha) = hexshas
 
105
    if mapping.is_special_file(path):
 
106
        return []
 
107
    if base_hexsha == hexsha and base_mode == mode:
 
108
        # If nothing has changed since the base revision, we're done
 
109
        return []
 
110
    file_id = lookup_file_id(path)
 
111
    if stat.S_ISLNK(mode):
 
112
        cls = InventoryLink
 
113
    else:
 
114
        cls = InventoryFile
 
115
    ie = cls(file_id, name.decode("utf-8"), parent_id)
 
116
    if ie.kind == "file":
 
117
        ie.executable = mode_is_executable(mode)
 
118
    if base_hexsha == hexsha and mode_kind(base_mode) == mode_kind(mode):
 
119
        base_exec = base_bzr_tree.is_executable(path)
 
120
        if ie.kind == "symlink":
 
121
            ie.symlink_target = base_bzr_tree.get_symlink_target(path)
 
122
        else:
 
123
            ie.text_size = base_bzr_tree.get_file_size(path)
 
124
            ie.text_sha1 = base_bzr_tree.get_file_sha1(path)
 
125
        if ie.kind == "symlink" or ie.executable == base_exec:
 
126
            ie.revision = base_bzr_tree.get_file_revision(path)
 
127
        else:
 
128
            blob = lookup_object(hexsha)
 
129
    else:
 
130
        blob = lookup_object(hexsha)
 
131
        if ie.kind == "symlink":
 
132
            ie.revision = None
 
133
            ie.symlink_target = blob.data.decode("utf-8")
 
134
        else:
 
135
            ie.text_size = sum(map(len, blob.chunked))
 
136
            ie.text_sha1 = osutils.sha_strings(blob.chunked)
 
137
    # Check what revision we should store
 
138
    parent_keys = []
 
139
    for ptree in parent_bzr_trees:
 
140
        try:
 
141
            ppath = ptree.id2path(file_id)
 
142
        except errors.NoSuchId:
 
143
            continue
 
144
        pkind = ptree.kind(ppath, file_id)
 
145
        if (pkind == ie.kind and
 
146
            ((pkind == "symlink" and ptree.get_symlink_target(ppath, file_id) == ie.symlink_target) or
 
147
             (pkind == "file" and ptree.get_file_sha1(ppath, file_id) == ie.text_sha1 and
 
148
                ptree.is_executable(ppath, file_id) == ie.executable))):
 
149
            # found a revision in one of the parents to use
 
150
            ie.revision = ptree.get_file_revision(ppath, file_id)
 
151
            break
 
152
        parent_key = (file_id, ptree.get_file_revision(ppath, file_id))
 
153
        if not parent_key in parent_keys:
 
154
            parent_keys.append(parent_key)
 
155
    if ie.revision is None:
 
156
        # Need to store a new revision
 
157
        ie.revision = revision_id
 
158
        if ie.revision is None:
 
159
            raise ValueError("no file revision set")
 
160
        if ie.kind == 'symlink':
 
161
            chunks = []
 
162
        else:
 
163
            chunks = blob.chunked
 
164
        texts.insert_record_stream([
 
165
            ChunkedContentFactory((file_id, ie.revision),
 
166
                tuple(parent_keys), ie.text_sha1, chunks)])
 
167
    invdelta = []
 
168
    if base_hexsha is not None:
 
169
        old_path = path.decode("utf-8") # Renames are not supported yet
 
170
        if stat.S_ISDIR(base_mode):
 
171
            invdelta.extend(remove_disappeared_children(base_bzr_tree, old_path,
 
172
                lookup_object(base_hexsha), [], lookup_object))
 
173
    else:
 
174
        old_path = None
 
175
    new_path = path.decode("utf-8")
 
176
    invdelta.append((old_path, new_path, file_id, ie))
 
177
    if base_hexsha != hexsha:
 
178
        store_updater.add_object(blob, (ie.file_id, ie.revision), path)
 
179
    return invdelta
 
180
 
 
181
 
 
182
class SubmodulesRequireSubtrees(BzrError):
 
183
    _fmt = ("The repository you are fetching from contains submodules, "
 
184
            "which require a Bazaar format that supports tree references.")
 
185
    internal = False
 
186
 
 
187
 
 
188
def import_git_submodule(texts, mapping, path, name, hexshas,
 
189
    base_bzr_tree, parent_id, revision_id, parent_bzr_trees, lookup_object,
 
190
    modes, store_updater, lookup_file_id):
 
191
    """Import a git submodule."""
 
192
    (base_hexsha, hexsha) = hexshas
 
193
    (base_mode, mode) = modes
 
194
    if base_hexsha == hexsha and base_mode == mode:
 
195
        return [], {}
 
196
    file_id = lookup_file_id(path)
 
197
    invdelta = []
 
198
    ie = TreeReference(file_id, name.decode("utf-8"), parent_id)
 
199
    ie.revision = revision_id
 
200
    if base_hexsha is not None:
 
201
        old_path = path.decode("utf-8") # Renames are not supported yet
 
202
        if stat.S_ISDIR(base_mode):
 
203
            invdelta.extend(remove_disappeared_children(base_bzr_tree, old_path,
 
204
                lookup_object(base_hexsha), [], lookup_object))
 
205
    else:
 
206
        old_path = None
 
207
    ie.reference_revision = mapping.revision_id_foreign_to_bzr(hexsha)
 
208
    texts.insert_record_stream([
 
209
        ChunkedContentFactory((file_id, ie.revision), (), None, [])])
 
210
    invdelta.append((old_path, path, file_id, ie))
 
211
    return invdelta, {}
 
212
 
 
213
 
 
214
def remove_disappeared_children(base_bzr_tree, path, base_tree, existing_children,
 
215
        lookup_object):
 
216
    """Generate an inventory delta for removed children.
 
217
 
 
218
    :param base_bzr_tree: Base bzr tree against which to generate the
 
219
        inventory delta.
 
220
    :param path: Path to process (unicode)
 
221
    :param base_tree: Git Tree base object
 
222
    :param existing_children: Children that still exist
 
223
    :param lookup_object: Lookup a git object by its SHA1
 
224
    :return: Inventory delta, as list
 
225
    """
 
226
    if type(path) is not unicode:
 
227
        raise TypeError(path)
 
228
    ret = []
 
229
    for name, mode, hexsha in base_tree.iteritems():
 
230
        if name in existing_children:
 
231
            continue
 
232
        c_path = posixpath.join(path, name.decode("utf-8"))
 
233
        file_id = base_bzr_tree.path2id(c_path)
 
234
        if file_id is None:
 
235
            raise TypeError(file_id)
 
236
        ret.append((c_path, None, file_id, None))
 
237
        if stat.S_ISDIR(mode):
 
238
            ret.extend(remove_disappeared_children(
 
239
                base_bzr_tree, c_path, lookup_object(hexsha), [], lookup_object))
 
240
    return ret
 
241
 
 
242
 
 
243
def import_git_tree(texts, mapping, path, name, hexshas,
 
244
        base_bzr_tree, parent_id, revision_id, parent_bzr_trees,
 
245
        lookup_object, modes, store_updater,
 
246
        lookup_file_id, allow_submodules=False):
82
247
    """Import a git tree object into a bzr repository.
83
248
 
84
 
    :param repo: A Bzr repository object
85
 
    :param path: Path in the tree
 
249
    :param texts: VersionedFiles object to add to
 
250
    :param path: Path in the tree (str)
 
251
    :param name: Name of the tree (str)
86
252
    :param tree: A git tree object
87
 
    :param inv: Inventory object
 
253
    :param base_bzr_tree: Base inventory against which to return inventory delta
 
254
    :return: Inventory delta for this subtree
88
255
    """
89
 
    file_id = mapping.generate_file_id(path)
90
 
    repo.texts.add_lines((file_id, tree.id),
91
 
        [], #FIXME 
92
 
        [])
93
 
    inv.add_path(path, "directory", file_id)
94
 
    for mode, name, hexsha in tree.entries():
95
 
        entry_kind = (mode & 0700000) / 0100000
96
 
        basename = name.decode("utf-8")
97
 
        if path == "":
98
 
            child_path = name
99
 
        else:
100
 
            child_path = urlutils.join(path, name)
101
 
        if entry_kind == 0:
102
 
            import_git_tree(repo, mapping, child_path, lookup_object, inv)
103
 
        elif entry_kind == 1:
104
 
            import_git_blob(repo, mapping, child_path, lookup_object, inv)
105
 
        else:
106
 
            raise AssertionError("Unknown blob kind, perms=%r." % (mode,))
107
 
 
108
 
 
109
 
def import_git_objects(repo, mapping, object_iter):
 
256
    (base_hexsha, hexsha) = hexshas
 
257
    (base_mode, mode) = modes
 
258
    if type(path) is not str:
 
259
        raise TypeError(path)
 
260
    if type(name) is not str:
 
261
        raise TypeError(name)
 
262
    if base_hexsha == hexsha and base_mode == mode:
 
263
        # If nothing has changed since the base revision, we're done
 
264
        return [], {}
 
265
    invdelta = []
 
266
    file_id = lookup_file_id(path)
 
267
    # We just have to hope this is indeed utf-8:
 
268
    ie = InventoryDirectory(file_id, name.decode("utf-8"), parent_id)
 
269
    tree = lookup_object(hexsha)
 
270
    if base_hexsha is None:
 
271
        base_tree = None
 
272
        old_path = None # Newly appeared here
 
273
    else:
 
274
        base_tree = lookup_object(base_hexsha)
 
275
        old_path = path.decode("utf-8") # Renames aren't supported yet
 
276
    new_path = path.decode("utf-8")
 
277
    if base_tree is None or type(base_tree) is not Tree:
 
278
        ie.revision = revision_id
 
279
        invdelta.append((old_path, new_path, ie.file_id, ie))
 
280
        texts.insert_record_stream([
 
281
            ChunkedContentFactory((ie.file_id, ie.revision), (), None, [])])
 
282
    # Remember for next time
 
283
    existing_children = set()
 
284
    child_modes = {}
 
285
    for name, child_mode, child_hexsha in tree.iteritems():
 
286
        existing_children.add(name)
 
287
        child_path = posixpath.join(path, name)
 
288
        if type(base_tree) is Tree:
 
289
            try:
 
290
                child_base_mode, child_base_hexsha = base_tree[name]
 
291
            except KeyError:
 
292
                child_base_hexsha = None
 
293
                child_base_mode = 0
 
294
        else:
 
295
            child_base_hexsha = None
 
296
            child_base_mode = 0
 
297
        if stat.S_ISDIR(child_mode):
 
298
            subinvdelta, grandchildmodes = import_git_tree(texts, mapping,
 
299
                child_path, name, (child_base_hexsha, child_hexsha),
 
300
                base_bzr_tree, file_id, revision_id, parent_bzr_trees,
 
301
                lookup_object, (child_base_mode, child_mode), store_updater,
 
302
                lookup_file_id, allow_submodules=allow_submodules)
 
303
        elif S_ISGITLINK(child_mode): # submodule
 
304
            if not allow_submodules:
 
305
                raise SubmodulesRequireSubtrees()
 
306
            subinvdelta, grandchildmodes = import_git_submodule(texts, mapping,
 
307
                child_path, name, (child_base_hexsha, child_hexsha),
 
308
                base_bzr_tree, file_id, revision_id, parent_bzr_trees,
 
309
                lookup_object, (child_base_mode, child_mode), store_updater,
 
310
                lookup_file_id)
 
311
        else:
 
312
            if not mapping.is_special_file(name):
 
313
                subinvdelta = import_git_blob(texts, mapping, child_path, name,
 
314
                    (child_base_hexsha, child_hexsha), base_bzr_tree, file_id,
 
315
                    revision_id, parent_bzr_trees, lookup_object,
 
316
                    (child_base_mode, child_mode), store_updater, lookup_file_id)
 
317
            else:
 
318
                subinvdelta = []
 
319
            grandchildmodes = {}
 
320
        child_modes.update(grandchildmodes)
 
321
        invdelta.extend(subinvdelta)
 
322
        if child_mode not in (stat.S_IFDIR, DEFAULT_FILE_MODE,
 
323
                        stat.S_IFLNK, DEFAULT_FILE_MODE|0o111,
 
324
                        S_IFGITLINK):
 
325
            child_modes[child_path] = child_mode
 
326
    # Remove any children that have disappeared
 
327
    if base_tree is not None and type(base_tree) is Tree:
 
328
        invdelta.extend(remove_disappeared_children(base_bzr_tree, old_path,
 
329
            base_tree, existing_children, lookup_object))
 
330
    store_updater.add_object(tree, (file_id, revision_id), path)
 
331
    return invdelta, child_modes
 
332
 
 
333
 
 
334
def verify_commit_reconstruction(target_git_object_retriever, lookup_object,
 
335
    o, rev, ret_tree, parent_trees, mapping, unusual_modes, verifiers):
 
336
    new_unusual_modes = mapping.export_unusual_file_modes(rev)
 
337
    if new_unusual_modes != unusual_modes:
 
338
        raise AssertionError("unusual modes don't match: %r != %r" % (
 
339
            unusual_modes, new_unusual_modes))
 
340
    # Verify that we can reconstruct the commit properly
 
341
    rec_o = target_git_object_retriever._reconstruct_commit(rev, o.tree, True,
 
342
        verifiers)
 
343
    if rec_o != o:
 
344
        raise AssertionError("Reconstructed commit differs: %r != %r" % (
 
345
            rec_o, o))
 
346
    diff = []
 
347
    new_objs = {}
 
348
    for path, obj, ie in _tree_to_objects(ret_tree, parent_trees,
 
349
        target_git_object_retriever._cache.idmap, unusual_modes,
 
350
        mapping.BZR_DUMMY_FILE):
 
351
        old_obj_id = tree_lookup_path(lookup_object, o.tree, path)[1]
 
352
        new_objs[path] = obj
 
353
        if obj.id != old_obj_id:
 
354
            diff.append((path, lookup_object(old_obj_id), obj))
 
355
    for (path, old_obj, new_obj) in diff:
 
356
        while (old_obj.type_name == "tree" and
 
357
               new_obj.type_name == "tree" and
 
358
               sorted(old_obj) == sorted(new_obj)):
 
359
            for name in old_obj:
 
360
                if old_obj[name][0] != new_obj[name][0]:
 
361
                    raise AssertionError("Modes for %s differ: %o != %o" %
 
362
                        (path, old_obj[name][0], new_obj[name][0]))
 
363
                if old_obj[name][1] != new_obj[name][1]:
 
364
                    # Found a differing child, delve deeper
 
365
                    path = posixpath.join(path, name)
 
366
                    old_obj = lookup_object(old_obj[name][1])
 
367
                    new_obj = new_objs[path]
 
368
                    break
 
369
        raise AssertionError("objects differ for %s: %r != %r" % (path,
 
370
            old_obj, new_obj))
 
371
 
 
372
 
 
373
def ensure_inventories_in_repo(repo, trees):
 
374
    real_inv_vf = repo.inventories.without_fallbacks()
 
375
    for t in trees:
 
376
        revid = t.get_revision_id()
 
377
        if not real_inv_vf.get_parent_map([(revid, )]):
 
378
            repo.add_inventory(revid, t.inventory, t.get_parent_ids())
 
379
 
 
380
 
 
381
def import_git_commit(repo, mapping, head, lookup_object,
 
382
                      target_git_object_retriever, trees_cache):
 
383
    o = lookup_object(head)
 
384
    # Note that this uses mapping.revision_id_foreign_to_bzr. If the parents
 
385
    # were bzr roundtripped revisions they would be specified in the
 
386
    # roundtrip data.
 
387
    rev, roundtrip_revid, verifiers = mapping.import_commit(
 
388
        o, mapping.revision_id_foreign_to_bzr)
 
389
    if roundtrip_revid is not None:
 
390
        original_revid = rev.revision_id
 
391
        rev.revision_id = roundtrip_revid
 
392
    # We have to do this here, since we have to walk the tree and
 
393
    # we need to make sure to import the blobs / trees with the right
 
394
    # path; this may involve adding them more than once.
 
395
    parent_trees = trees_cache.revision_trees(rev.parent_ids)
 
396
    ensure_inventories_in_repo(repo, parent_trees)
 
397
    if parent_trees == []:
 
398
        base_bzr_tree = trees_cache.revision_tree(NULL_REVISION)
 
399
        base_tree = None
 
400
        base_mode = None
 
401
    else:
 
402
        base_bzr_tree = parent_trees[0]
 
403
        base_tree = lookup_object(o.parents[0]).tree
 
404
        base_mode = stat.S_IFDIR
 
405
    store_updater = target_git_object_retriever._get_updater(rev)
 
406
    tree_supplement = mapping.get_fileid_map(lookup_object, o.tree)
 
407
    inv_delta, unusual_modes = import_git_tree(repo.texts,
 
408
            mapping, "", "", (base_tree, o.tree), base_bzr_tree,
 
409
            None, rev.revision_id, parent_trees,
 
410
            lookup_object, (base_mode, stat.S_IFDIR), store_updater,
 
411
            tree_supplement.lookup_file_id,
 
412
            allow_submodules=getattr(repo._format, "supports_tree_reference",
 
413
                False))
 
414
    if unusual_modes != {}:
 
415
        for path, mode in unusual_modes.iteritems():
 
416
            warn_unusual_mode(rev.foreign_revid, path, mode)
 
417
        mapping.import_unusual_file_modes(rev, unusual_modes)
 
418
    try:
 
419
        basis_id = rev.parent_ids[0]
 
420
    except IndexError:
 
421
        basis_id = NULL_REVISION
 
422
        base_bzr_inventory = None
 
423
    else:
 
424
        try:
 
425
            base_bzr_inventory = base_bzr_tree.root_inventory
 
426
        except AttributeError: # bzr < 2.6
 
427
            base_bzr_inventory = base_bzr_tree.inventory
 
428
    rev.inventory_sha1, inv = repo.add_inventory_by_delta(basis_id,
 
429
              inv_delta, rev.revision_id, rev.parent_ids,
 
430
              base_bzr_inventory)
 
431
    ret_tree = InventoryRevisionTree(repo, inv, rev.revision_id)
 
432
    # Check verifiers
 
433
    if verifiers and roundtrip_revid is not None:
 
434
        testament = StrictTestament3(rev, ret_tree)
 
435
        calculated_verifiers = { "testament3-sha1": testament.as_sha1() }
 
436
        if calculated_verifiers != verifiers:
 
437
            trace.mutter("Testament SHA1 %r for %r did not match %r.",
 
438
                         calculated_verifiers["testament3-sha1"],
 
439
                         rev.revision_id, verifiers["testament3-sha1"])
 
440
            rev.revision_id = original_revid
 
441
            rev.inventory_sha1, inv = repo.add_inventory_by_delta(basis_id,
 
442
              inv_delta, rev.revision_id, rev.parent_ids, base_bzr_tree)
 
443
            ret_tree = InventoryRevisionTree(repo, inv, rev.revision_id)
 
444
    else:
 
445
        calculated_verifiers = {}
 
446
    store_updater.add_object(o, calculated_verifiers, None)
 
447
    store_updater.finish()
 
448
    trees_cache.add(ret_tree)
 
449
    repo.add_revision(rev.revision_id, rev)
 
450
    if "verify" in debug.debug_flags:
 
451
        verify_commit_reconstruction(target_git_object_retriever,
 
452
            lookup_object, o, rev, ret_tree, parent_trees, mapping,
 
453
            unusual_modes, verifiers)
 
454
 
 
455
 
 
456
def import_git_objects(repo, mapping, object_iter,
 
457
    target_git_object_retriever, heads, pb=None, limit=None):
110
458
    """Import a set of git objects into a bzr repository.
111
459
 
112
 
    :param repo: Bazaar repository
 
460
    :param repo: Target Bazaar repository
113
461
    :param mapping: Mapping to use
114
462
    :param object_iter: Iterator over Git objects.
 
463
    :return: Tuple with pack hints and last imported revision id
115
464
    """
116
 
    # TODO: a more (memory-)efficient implementation of this
117
 
    objects = {}
118
 
    for o in object_iter:
119
 
        objects[o.id] = o
120
 
    root_trees = {}
 
465
    def lookup_object(sha):
 
466
        try:
 
467
            return object_iter[sha]
 
468
        except KeyError:
 
469
            return target_git_object_retriever[sha]
 
470
    graph = []
 
471
    checked = set()
 
472
    heads = list(set(heads))
 
473
    trees_cache = LRUTreeCache(repo)
121
474
    # Find and convert commit objects
122
 
    for o in objects.iterkeys():
 
475
    while heads:
 
476
        if pb is not None:
 
477
            pb.update("finding revisions to fetch", len(graph), None)
 
478
        head = heads.pop()
 
479
        if head == ZERO_SHA:
 
480
            continue
 
481
        if type(head) is not str:
 
482
            raise TypeError(head)
 
483
        try:
 
484
            o = lookup_object(head)
 
485
        except KeyError:
 
486
            continue
123
487
        if isinstance(o, Commit):
124
 
            rev = mapping.import_commit(o)
125
 
            root_trees[rev] = objects[o.tree_sha]
 
488
            rev, roundtrip_revid, verifiers = mapping.import_commit(o,
 
489
                mapping.revision_id_foreign_to_bzr)
 
490
            if (repo.has_revision(rev.revision_id) or
 
491
                (roundtrip_revid and repo.has_revision(roundtrip_revid))):
 
492
                continue
 
493
            graph.append((o.id, o.parents))
 
494
            heads.extend([p for p in o.parents if p not in checked])
 
495
        elif isinstance(o, Tag):
 
496
            if o.object[1] not in checked:
 
497
                heads.append(o.object[1])
 
498
        else:
 
499
            trace.warning("Unable to import head object %r" % o)
 
500
        checked.add(o.id)
 
501
    del checked
 
502
    # Order the revisions
126
503
    # Create the inventory objects
127
 
    for rev, root_tree in root_trees.iteritems():
128
 
        # We have to do this here, since we have to walk the tree and 
129
 
        # we need to make sure to import the blobs / trees with the riht 
130
 
        # path; this may involve adding them more than once.
131
 
        inv = Inventory()
132
 
        def lookup_object(sha):
133
 
            if sha in objects:
134
 
                return objects[sha]
135
 
            return reconstruct_git_object(repo, mapping, sha)
136
 
        import_git_tree(repo, mapping, "", tree, inv, lookup_object)
137
 
        repo.add_revision(rev.revision_id, rev, inv)
138
 
 
139
 
 
140
 
def reconstruct_git_commit(repo, rev):
141
 
    raise NotImplementedError(self.reconstruct_git_commit)
142
 
 
143
 
 
144
 
def reconstruct_git_object(repo, mapping, sha):
145
 
    # Commit
146
 
    revid = mapping.revision_id_foreign_to_bzr(sha)
147
 
    try:
148
 
        rev = repo.get_revision(revid)
149
 
    except NoSuchRevision:
150
 
        pass
151
 
    else:
152
 
        return reconstruct_git_commit(rev)
153
 
 
154
 
    # TODO: Tree
155
 
    # TODO: Blob
156
 
    raise KeyError("No such object %s" % sha)
157
 
 
158
 
 
159
 
class InterGitRepository(InterRepository):
160
 
 
161
 
    _matching_repo_format = GitFormat()
162
 
 
163
 
    @staticmethod
164
 
    def _get_repo_format_to_test():
165
 
        return None
166
 
 
167
 
    def copy_content(self, revision_id=None, pb=None):
168
 
        """See InterRepository.copy_content."""
169
 
        self.fetch(revision_id, pb, find_ghosts=False)
170
 
 
171
 
    def fetch(self, revision_id=None, pb=None, find_ghosts=False, 
172
 
              mapping=None):
173
 
        if mapping is None:
174
 
            mapping = self.source.get_mapping()
175
 
        def progress(text):
176
 
            if pb is not None:
177
 
                pb.note("git: %s" % text)
178
 
            else:
179
 
                info("git: %s" % text)
180
 
        def determine_wants(heads):
181
 
            if revision_id is None:
182
 
                ret = heads.values()
183
 
            else:
184
 
                ret = [mapping.revision_id_bzr_to_foreign(revision_id)]
185
 
            return [rev for rev in ret if not self.target.has_revision(mapping.revision_id_foreign_to_bzr(rev))]
186
 
        graph_walker = BzrFetchGraphWalker(self.target, mapping)
187
 
        self.target.lock_write()
 
504
    batch_size = 1000
 
505
    revision_ids = topo_sort(graph)
 
506
    pack_hints = []
 
507
    if limit is not None:
 
508
        revision_ids = revision_ids[:limit]
 
509
    last_imported = None
 
510
    for offset in range(0, len(revision_ids), batch_size):
 
511
        target_git_object_retriever.start_write_group()
188
512
        try:
189
 
            import_git_objects(self.target, mapping,
190
 
                self.source.fetch_objects(determine_wants, graph_walker, 
191
 
                    progress))
192
 
        finally:
193
 
            self.target.unlock()
194
 
 
195
 
    @staticmethod
196
 
    def is_compatible(source, target):
197
 
        """Be compatible with GitRepository."""
198
 
        # FIXME: Also check target uses VersionedFile
199
 
        return (isinstance(source, LocalGitRepository) and 
200
 
                target.supports_rich_root())
 
513
            repo.start_write_group()
 
514
            try:
 
515
                for i, head in enumerate(
 
516
                    revision_ids[offset:offset+batch_size]):
 
517
                    if pb is not None:
 
518
                        pb.update("fetching revisions", offset+i,
 
519
                                  len(revision_ids))
 
520
                    import_git_commit(repo, mapping, head, lookup_object,
 
521
                        target_git_object_retriever, trees_cache)
 
522
                    last_imported = head
 
523
            except:
 
524
                repo.abort_write_group()
 
525
                raise
 
526
            else:
 
527
                hint = repo.commit_write_group()
 
528
                if hint is not None:
 
529
                    pack_hints.extend(hint)
 
530
        except:
 
531
            target_git_object_retriever.abort_write_group()
 
532
            raise
 
533
        else:
 
534
            target_git_object_retriever.commit_write_group()
 
535
    return pack_hints, last_imported
 
536
 
 
537
 
 
538
class DetermineWantsRecorder(object):
 
539
 
 
540
    def __init__(self, actual):
 
541
        self.actual = actual
 
542
        self.wants = []
 
543
        self.remote_refs = {}
 
544
 
 
545
    def __call__(self, refs):
 
546
        if type(refs) is not dict:
 
547
            raise TypeError(refs)
 
548
        self.remote_refs = refs
 
549
        self.wants = self.actual(refs)
 
550
        return self.wants
 
551
 
 
552
 
 
553