/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/git/fetch.py

  • Committer: Richard Wilbur
  • Date: 2016-02-04 19:07:28 UTC
  • mto: This revision was merged to the branch mainline in revision 6618.
  • Revision ID: richard.wilbur@gmail.com-20160204190728-p0zvfii6zase0fw7
Update COPYING.txt from the original http://www.gnu.org/licenses/gpl-2.0.txt  (Only differences were in whitespace.)  Thanks to Petr Stodulka for pointing out the discrepancy.

Show diffs side-by-side

added added

removed removed

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