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

  • Committer: Jelmer Vernooij
  • Date: 2010-02-12 00:17:50 UTC
  • mto: (0.200.718 trunk)
  • mto: This revision was merged to the branch mainline in revision 6960.
  • Revision ID: jelmer@samba.org-20100212001750-5bcl5y33fuiag5j9
more work on transportgit.

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
 
from cStringIO import (
18
 
    StringIO,
19
 
    )
20
 
import dulwich as git
21
17
from dulwich.objects import (
22
18
    Commit,
23
19
    Tag,
26
22
from dulwich.object_store import (
27
23
    tree_lookup_path,
28
24
    )
 
25
import re
29
26
import stat
30
27
 
31
28
from bzrlib import (
32
29
    debug,
 
30
    lru_cache,
33
31
    osutils,
34
32
    trace,
35
33
    ui,
36
34
    urlutils,
37
35
    )
38
36
from bzrlib.errors import (
39
 
    InvalidRevisionId,
 
37
    BzrError,
40
38
    NoSuchId,
41
 
    NoSuchRevision,
42
39
    )
43
40
from bzrlib.inventory import (
44
41
    Inventory,
45
42
    InventoryDirectory,
46
43
    InventoryFile,
47
44
    InventoryLink,
48
 
    )
49
 
from bzrlib.lru_cache import (
50
 
    LRUCache,
 
45
    TreeReference,
51
46
    )
52
47
from bzrlib.repository import (
53
48
    InterRepository,
67
62
    inventory_to_tree_and_blobs,
68
63
    mode_is_executable,
69
64
    squash_revision,
70
 
    text_to_blob,
71
65
    warn_unusual_mode,
72
66
    )
73
67
from bzrlib.plugins.git.object_store import (
77
71
    RemoteGitRepository,
78
72
    )
79
73
from bzrlib.plugins.git.repository import (
80
 
    GitRepository, 
 
74
    GitRepository,
81
75
    GitRepositoryFormat,
82
76
    LocalGitRepository,
83
77
    )
84
78
 
85
79
 
86
 
def import_git_blob(texts, mapping, path, hexsha, base_inv, parent_id, 
 
80
MAX_INV_CACHE_SIZE = 50 * 1024 * 1024
 
81
 
 
82
 
 
83
def import_git_blob(texts, mapping, path, hexsha, base_inv, base_ie, parent_id,
87
84
    revision_id, parent_invs, shagitmap, lookup_object, executable, symlink):
88
85
    """Import a git blob object into a bzr repository.
89
86
 
101
98
    ie = cls(file_id, urlutils.basename(path).decode("utf-8"), parent_id)
102
99
    ie.executable = executable
103
100
    # See if this has changed at all
104
 
    try:
105
 
        base_ie = base_inv[file_id]
106
 
    except NoSuchId:
107
 
        base_ie = None
 
101
    if base_ie is None:
108
102
        base_sha = None
109
103
    else:
110
104
        try:
156
150
        ie.revision = revision_id
157
151
        assert file_id is not None
158
152
        assert ie.revision is not None
159
 
        texts.insert_record_stream([FulltextContentFactory((file_id, ie.revision), tuple(parent_keys), ie.text_sha1, blob.data)])
 
153
        if ie.kind == 'symlink':
 
154
            data = ''
 
155
        else: 
 
156
            data = blob.data
 
157
        texts.insert_record_stream([FulltextContentFactory((file_id, ie.revision), tuple(parent_keys), ie.text_sha1, data)])
160
158
        shamap = [(hexsha, "blob", (ie.file_id, ie.revision))]
161
159
    else:
162
160
        shamap = []
163
 
    if file_id in base_inv:
 
161
    invdelta = []
 
162
    if base_ie is not None:
164
163
        old_path = base_inv.id2path(file_id)
 
164
        if base_ie.kind == "directory":
 
165
            invdelta.extend(remove_disappeared_children(old_path, base_ie.children, []))
165
166
    else:
166
167
        old_path = None
167
 
    invdelta = [(old_path, path, file_id, ie)]
168
 
    invdelta.extend(remove_disappeared_children(base_inv, base_ie, []))
 
168
    invdelta.append((old_path, path, file_id, ie))
169
169
    return (invdelta, shamap)
170
170
 
171
171
 
172
 
def import_git_submodule(texts, mapping, path, hexsha, base_inv, parent_id, 
173
 
    revision_id, parent_invs, shagitmap, lookup_object):
174
 
    raise NotImplementedError(import_git_submodule)
175
 
 
176
 
 
177
 
def remove_disappeared_children(base_inv, base_ie, existing_children):
178
 
    if base_ie is None or base_ie.kind != 'directory':
179
 
        return []
 
172
class SubmodulesRequireSubtrees(BzrError):
 
173
    _fmt = """The repository you are fetching from contains submodules. To continue, upgrade your Bazaar repository to a format that supports nested trees, such as 'development-subtree'."""
 
174
    internal = False
 
175
 
 
176
 
 
177
def import_git_submodule(texts, mapping, path, hexsha, base_inv, base_ie,
 
178
    parent_id, revision_id, parent_invs, shagitmap, lookup_object):
 
179
    file_id = mapping.generate_file_id(path)
 
180
    ie = TreeReference(file_id, urlutils.basename(path.decode("utf-8")),
 
181
        parent_id)
 
182
    ie.revision = revision_id
 
183
    if base_ie is None:
 
184
        oldpath = None
 
185
    else:
 
186
        oldpath = path
 
187
        if (base_ie.kind == ie.kind and
 
188
            base_ie.reference_revision == ie.reference_revision):
 
189
            ie.revision = base_ie.revision
 
190
    ie.reference_revision = mapping.revision_id_foreign_to_bzr(hexsha)
 
191
    texts.insert_record_stream([FulltextContentFactory((file_id, ie.revision), (), None, "")])
 
192
    invdelta = [(oldpath, path, file_id, ie)]
 
193
    return invdelta, {}, {}
 
194
 
 
195
 
 
196
def remove_disappeared_children(path, base_children, existing_children):
180
197
    ret = []
181
 
    deletable = [v for k,v in base_ie.children.iteritems() if k not in existing_children]
 
198
    deletable = [(osutils.pathjoin(path, k), v) for k,v in base_children.iteritems() if k not in existing_children]
182
199
    while deletable:
183
 
        ie = deletable.pop()
184
 
        ret.append((base_inv.id2path(ie.file_id), None, ie.file_id, None))
 
200
        (path, ie) = deletable.pop()
 
201
        ret.append((path, None, ie.file_id, None))
185
202
        if ie.kind == "directory":
186
 
            deletable.extend(ie.children.values())
 
203
            for name, child_ie in ie.children.iteritems():
 
204
                deletable.append((osutils.pathjoin(path, name), child_ie))
187
205
    return ret
188
206
 
189
207
 
190
 
def import_git_tree(texts, mapping, path, hexsha, base_inv, parent_id, 
191
 
    revision_id, parent_invs, shagitmap, lookup_object):
 
208
def import_git_tree(texts, mapping, path, hexsha, base_inv, base_ie, parent_id,
 
209
    revision_id, parent_invs, shagitmap, lookup_object, allow_submodules=False):
192
210
    """Import a git tree object into a bzr repository.
193
211
 
194
212
    :param texts: VersionedFiles object to add to
200
218
    invdelta = []
201
219
    file_id = mapping.generate_file_id(path)
202
220
    # We just have to hope this is indeed utf-8:
203
 
    ie = InventoryDirectory(file_id, urlutils.basename(path.decode("utf-8")), 
 
221
    ie = InventoryDirectory(file_id, urlutils.basename(path.decode("utf-8")),
204
222
        parent_id)
205
 
    try:
206
 
        base_ie = base_inv[file_id]
207
 
    except NoSuchId:
 
223
    if base_ie is None:
208
224
        # Newly appeared here
209
 
        base_ie = None
210
225
        ie.revision = revision_id
211
 
        texts.add_lines((file_id, ie.revision), (), [])
 
226
        texts.insert_record_stream([FulltextContentFactory((file_id, ie.revision), (), None, "")])
212
227
        invdelta.append((None, path, file_id, ie))
213
228
    else:
214
229
        # See if this has changed at all
222
237
                return [], {}, []
223
238
        if base_ie.kind != "directory":
224
239
            ie.revision = revision_id
225
 
            texts.add_lines((ie.file_id, ie.revision), (), [])
 
240
            texts.insert_record_stream([FulltextContentFactory((ie.file_id, ie.revision), (), None, "")])
226
241
            invdelta.append((base_inv.id2path(ie.file_id), path, ie.file_id, ie))
 
242
    if base_ie is not None and base_ie.kind == "directory":
 
243
        base_children = base_ie.children
 
244
    else:
 
245
        base_children = {}
227
246
    # Remember for next time
228
247
    existing_children = set()
229
248
    child_modes = {}
235
254
        child_path = osutils.pathjoin(path, name)
236
255
        if stat.S_ISDIR(mode):
237
256
            subinvdelta, grandchildmodes, subshamap = import_git_tree(
238
 
                    texts, mapping, child_path, child_hexsha, base_inv, 
239
 
                    file_id, revision_id, parent_invs, shagitmap, lookup_object)
 
257
                    texts, mapping, child_path, child_hexsha, base_inv,
 
258
                    base_children.get(basename), file_id, revision_id,
 
259
                    parent_invs, shagitmap, lookup_object,
 
260
                    allow_submodules=allow_submodules)
240
261
            invdelta.extend(subinvdelta)
241
262
            child_modes.update(grandchildmodes)
242
263
            shamap.extend(subshamap)
243
264
        elif S_ISGITLINK(mode): # submodule
 
265
            if not allow_submodules:
 
266
                raise SubmodulesRequireSubtrees()
244
267
            subinvdelta, grandchildmodes, subshamap = import_git_submodule(
245
 
                    texts, mapping, child_path, child_hexsha, base_inv,
 
268
                    texts, mapping, child_path, child_hexsha, base_inv, base_children.get(basename),
246
269
                    file_id, revision_id, parent_invs, shagitmap, lookup_object)
247
270
            invdelta.extend(subinvdelta)
248
271
            child_modes.update(grandchildmodes)
249
272
            shamap.extend(subshamap)
250
273
        else:
251
 
            subinvdelta, subshamap = import_git_blob(texts, mapping, 
252
 
                    child_path, child_hexsha, base_inv, file_id, revision_id, 
253
 
                    parent_invs, shagitmap, lookup_object, 
 
274
            subinvdelta, subshamap = import_git_blob(texts, mapping,
 
275
                    child_path, child_hexsha, base_inv, base_children.get(basename), file_id,
 
276
                    revision_id, parent_invs, shagitmap, lookup_object,
254
277
                    mode_is_executable(mode), stat.S_ISLNK(mode))
255
278
            invdelta.extend(subinvdelta)
256
279
            shamap.extend(subshamap)
258
281
                        stat.S_IFLNK, DEFAULT_FILE_MODE|0111):
259
282
            child_modes[child_path] = mode
260
283
    # Remove any children that have disappeared
261
 
    invdelta.extend(remove_disappeared_children(base_inv, base_ie, existing_children))
 
284
    if base_ie is not None and base_ie.kind == "directory":
 
285
        invdelta.extend(remove_disappeared_children(base_inv.id2path(file_id),
 
286
            base_children, existing_children))
262
287
    shamap.append((hexsha, "tree", (file_id, revision_id)))
263
288
    return invdelta, child_modes, shamap
264
289
 
265
290
 
266
 
def import_git_objects(repo, mapping, object_iter, target_git_object_retriever, 
 
291
def approx_inv_size(inv):
 
292
    # Very rough estimate, 1k per inventory entry
 
293
    return len(inv) * 1024
 
294
 
 
295
 
 
296
def import_git_commit(repo, mapping, head, lookup_object,
 
297
                      target_git_object_retriever, parent_invs_cache):
 
298
    o = lookup_object(head)
 
299
    rev = mapping.import_commit(o)
 
300
    # We have to do this here, since we have to walk the tree and
 
301
    # we need to make sure to import the blobs / trees with the right
 
302
    # path; this may involve adding them more than once.
 
303
    parent_invs = []
 
304
    for parent_id in rev.parent_ids:
 
305
        try:
 
306
            parent_invs.append(parent_invs_cache[parent_id])
 
307
        except KeyError:
 
308
            parent_inv = repo.get_inventory(parent_id)
 
309
            parent_invs.append(parent_inv)
 
310
            parent_invs_cache[parent_id] = parent_inv
 
311
    if parent_invs == []:
 
312
        base_inv = Inventory(root_id=None)
 
313
        base_ie = None
 
314
    else:
 
315
        base_inv = parent_invs[0]
 
316
        base_ie = base_inv.root
 
317
    inv_delta, unusual_modes, shamap = import_git_tree(repo.texts,
 
318
            mapping, "", o.tree, base_inv, base_ie, None, rev.revision_id,
 
319
            parent_invs, target_git_object_retriever._idmap, lookup_object,
 
320
            allow_submodules=getattr(repo._format, "supports_tree_reference", False))
 
321
    target_git_object_retriever._idmap.add_entries(shamap)
 
322
    if unusual_modes != {}:
 
323
        for path, mode in unusual_modes.iteritems():
 
324
            warn_unusual_mode(rev.foreign_revid, path, mode)
 
325
        mapping.import_unusual_file_modes(rev, unusual_modes)
 
326
    try:
 
327
        basis_id = rev.parent_ids[0]
 
328
    except IndexError:
 
329
        basis_id = NULL_REVISION
 
330
        base_inv = None
 
331
    rev.inventory_sha1, inv = repo.add_inventory_by_delta(basis_id,
 
332
              inv_delta, rev.revision_id, rev.parent_ids,
 
333
              base_inv)
 
334
    parent_invs_cache[rev.revision_id] = inv
 
335
    repo.add_revision(rev.revision_id, rev)
 
336
    if "verify" in debug.debug_flags:
 
337
        new_unusual_modes = mapping.export_unusual_file_modes(rev)
 
338
        if new_unusual_modes != unusual_modes:
 
339
            raise AssertionError("unusual modes don't match: %r != %r" % (unusual_modes, new_unusual_modes))
 
340
        objs = inventory_to_tree_and_blobs(inv, repo.texts, mapping, unusual_modes)
 
341
        for sha1, newobj, path in objs:
 
342
            assert path is not None
 
343
            oldobj = tree_lookup_path(lookup_object, o.tree, path)
 
344
            if oldobj != newobj:
 
345
                raise AssertionError("%r != %r in %s" % (oldobj, newobj, path))
 
346
 
 
347
 
 
348
def import_git_objects(repo, mapping, object_iter, target_git_object_retriever,
267
349
        heads, pb=None):
268
350
    """Import a set of git objects into a bzr repository.
269
351
 
271
353
    :param mapping: Mapping to use
272
354
    :param object_iter: Iterator over Git objects.
273
355
    """
 
356
    target_git_object_retriever._idmap.start_write_group() # FIXME: try/finally
274
357
    def lookup_object(sha):
275
358
        try:
276
359
            return object_iter[sha]
278
361
            return target_git_object_retriever[sha]
279
362
    # TODO: a more (memory-)efficient implementation of this
280
363
    graph = []
281
 
    root_trees = {}
282
 
    revisions = {}
283
364
    checked = set()
284
365
    heads = list(heads)
285
 
    parent_invs_cache = LRUCache(50)
 
366
    parent_invs_cache = lru_cache.LRUSizeCache(compute_size=approx_inv_size,
 
367
                                               max_size=MAX_INV_CACHE_SIZE)
286
368
    # Find and convert commit objects
287
369
    while heads:
288
370
        if pb is not None:
292
374
        try:
293
375
            o = lookup_object(head)
294
376
        except KeyError:
 
377
            trace.mutter('missing head %s', head)
295
378
            continue
296
379
        if isinstance(o, Commit):
297
380
            rev = mapping.import_commit(o)
298
381
            if repo.has_revision(rev.revision_id):
299
382
                continue
300
383
            squash_revision(repo, rev)
301
 
            root_trees[rev.revision_id] = o.tree
302
 
            revisions[rev.revision_id] = rev
303
 
            graph.append((rev.revision_id, rev.parent_ids))
304
 
            target_git_object_retriever._idmap.add_entry(o.id, "commit", 
 
384
            graph.append((o.id, o.parents))
 
385
            target_git_object_retriever._idmap.add_entry(o.id, "commit",
305
386
                    (rev.revision_id, o.tree))
306
387
            heads.extend([p for p in o.parents if p not in checked])
307
388
        elif isinstance(o, Tag):
308
389
            heads.append(o.object[1])
309
390
        else:
310
391
            trace.warning("Unable to import head object %r" % o)
311
 
        checked.add(head)
 
392
        checked.add(o.id)
 
393
    del checked
312
394
    # Order the revisions
313
395
    # Create the inventory objects
314
 
    for i, revid in enumerate(topo_sort(graph)):
315
 
        if pb is not None:
316
 
            pb.update("fetching revisions", i, len(graph))
317
 
        rev = revisions[revid]
318
 
        # We have to do this here, since we have to walk the tree and 
319
 
        # we need to make sure to import the blobs / trees with the right 
320
 
        # path; this may involve adding them more than once.
321
 
        parent_invs = []
322
 
        for parent_id in rev.parent_ids:
323
 
            try:
324
 
                parent_invs.append(parent_invs_cache[parent_id])
325
 
            except KeyError:
326
 
                parent_inv = repo.get_inventory(parent_id)
327
 
                parent_invs.append(parent_inv)
328
 
                parent_invs_cache[parent_id] = parent_inv
329
 
        if parent_invs == []:
330
 
            base_inv = Inventory(root_id=None)
 
396
    batch_size = 100
 
397
    revision_ids = topo_sort(graph)
 
398
    pack_hints = []
 
399
    for offset in range(0, len(revision_ids), batch_size):
 
400
        repo.start_write_group()
 
401
        try:
 
402
            for i, head in enumerate(revision_ids[offset:offset+batch_size]):
 
403
                if pb is not None:
 
404
                    pb.update("fetching revisions", offset+i, len(revision_ids))
 
405
                import_git_commit(repo, mapping, head, lookup_object,
 
406
                                  target_git_object_retriever,
 
407
                                  parent_invs_cache)
 
408
        except:
 
409
            repo.abort_write_group()
 
410
            raise
331
411
        else:
332
 
            base_inv = parent_invs[0]
333
 
        inv_delta, unusual_modes, shamap = import_git_tree(repo.texts, 
334
 
                mapping, "", root_trees[revid], base_inv, None, revid, 
335
 
                parent_invs, target_git_object_retriever._idmap, lookup_object)
336
 
        target_git_object_retriever._idmap.add_entries(shamap)
337
 
        if unusual_modes != {}:
338
 
            for path, mode in unusual_modes.iteritems():
339
 
                warn_unusual_mode(rev.foreign_revid, path, mode)
340
 
            mapping.import_unusual_file_modes(rev, unusual_modes)
341
 
        try:
342
 
            basis_id = rev.parent_ids[0]
343
 
        except IndexError:
344
 
            basis_id = NULL_REVISION
345
 
        rev.inventory_sha1, inv = repo.add_inventory_by_delta(basis_id,
346
 
                  inv_delta, rev.revision_id, rev.parent_ids)
347
 
        parent_invs_cache[rev.revision_id] = inv
348
 
        repo.add_revision(rev.revision_id, rev)
349
 
        if "verify" in debug.debug_flags:
350
 
            new_unusual_modes = mapping.export_unusual_file_modes(rev)
351
 
            if new_unusual_modes != unusual_modes:
352
 
                raise AssertionError("unusual modes don't match: %r != %r" % (unusual_modes, new_unusual_modes))
353
 
            objs = inventory_to_tree_and_blobs(inv, repo.texts, mapping, unusual_modes)
354
 
            for sha1, newobj, path in objs:
355
 
                assert path is not None
356
 
                oldobj = tree_lookup_path(lookup_object, root_trees[revid], path)
357
 
                if oldobj != newobj:
358
 
                    raise AssertionError("%r != %r in %s" % (oldobj, newobj, path))
359
 
 
360
 
    target_git_object_retriever._idmap.commit()
 
412
            hint = repo.commit_write_group()
 
413
            if hint is not None:
 
414
                pack_hints.extend(hint)
 
415
    target_git_object_retriever._idmap.commit_write_group()
 
416
    return pack_hints
361
417
 
362
418
 
363
419
class InterGitRepository(InterRepository):
379
435
 
380
436
 
381
437
class InterGitNonGitRepository(InterGitRepository):
382
 
    """Base InterRepository that copies revisions from a Git into a non-Git 
 
438
    """Base InterRepository that copies revisions from a Git into a non-Git
383
439
    repository."""
384
440
 
385
 
    def fetch_refs(self, revision_id=None, pb=None, find_ghosts=False, 
 
441
    def fetch_refs(self, revision_id=None, pb=None, find_ghosts=False,
386
442
              mapping=None, fetch_spec=None):
387
443
        if mapping is None:
388
444
            mapping = self.source.get_mapping()
400
456
            else:
401
457
                ret = [mapping.revision_id_bzr_to_foreign(revid)[0] for revid in interesting_heads if revid not in (None, NULL_REVISION)]
402
458
            return [rev for rev in ret if not self.target.has_revision(mapping.revision_id_foreign_to_bzr(rev))]
403
 
        self.fetch_objects(determine_wants, mapping, pb)
 
459
        pack_hint = self.fetch_objects(determine_wants, mapping, pb)
 
460
        if pack_hint is not None and self.target._format.pack_compresses:
 
461
            self.target.pack(hint=pack_hint)
 
462
        if interesting_heads is not None:
 
463
            present_interesting_heads = self.target.has_revisions(interesting_heads)
 
464
            missing_interesting_heads = set(interesting_heads) - present_interesting_heads
 
465
            if missing_interesting_heads:
 
466
                raise AssertionError("Missing interesting heads: %r" % missing_interesting_heads)
404
467
        return self._refs
405
468
 
406
469
 
 
470
_GIT_PROGRESS_RE = re.compile(r"(.*?): +(\d+)% \((\d+)/(\d+)\)")
 
471
def report_git_progress(pb, text):
 
472
    text = text.rstrip("\r\n")
 
473
    g = _GIT_PROGRESS_RE.match(text)
 
474
    if g is not None:
 
475
        (text, pct, current, total) = g.groups()
 
476
        pb.update(text, int(current), int(total))
 
477
    else:
 
478
        pb.update(text, 0, 0)
 
479
 
 
480
 
407
481
class InterRemoteGitNonGitRepository(InterGitNonGitRepository):
408
 
    """InterRepository that copies revisions from a remote Git into a non-Git 
 
482
    """InterRepository that copies revisions from a remote Git into a non-Git
409
483
    repository."""
410
484
 
 
485
    def get_target_heads(self):
 
486
        # FIXME: This should be more efficient
 
487
        all_revs = self.target.all_revision_ids()
 
488
        parent_map = self.target.get_parent_map(all_revs)
 
489
        all_parents = set()
 
490
        map(all_parents.update, parent_map.itervalues())
 
491
        return set(all_revs) - all_parents
 
492
 
411
493
    def fetch_objects(self, determine_wants, mapping, pb=None):
412
494
        def progress(text):
413
 
            pb.update("git: %s" % text.rstrip("\r\n"), 0, 0)
 
495
            report_git_progress(pb, text)
414
496
        store = BazaarObjectStore(self.target, mapping)
415
497
        self.target.lock_write()
416
498
        try:
417
 
            heads = self.target.get_graph().heads(self.target.all_revision_ids())
 
499
            heads = self.get_target_heads()
418
500
            graph_walker = store.get_graph_walker(
419
501
                    [store._lookup_revision_sha1(head) for head in heads])
420
502
            recorded_wants = []
423
505
                wants = determine_wants(heads)
424
506
                recorded_wants.extend(wants)
425
507
                return wants
426
 
        
 
508
 
427
509
            create_pb = None
428
510
            if pb is None:
429
511
                create_pb = pb = ui.ui_factory.nested_progress_bar()
430
512
            try:
431
 
                self.target.start_write_group()
432
 
                try:
433
 
                    objects_iter = self.source.fetch_objects(
434
 
                                record_determine_wants, graph_walker, 
435
 
                                store.get_raw, progress)
436
 
                    import_git_objects(self.target, mapping, objects_iter, 
437
 
                            store, recorded_wants, pb)
438
 
                finally:
439
 
                    self.target.commit_write_group()
 
513
                objects_iter = self.source.fetch_objects(
 
514
                            record_determine_wants, graph_walker,
 
515
                            store.get_raw, progress)
 
516
                return import_git_objects(self.target, mapping,
 
517
                    objects_iter, store, recorded_wants, pb)
440
518
            finally:
441
519
                if create_pb:
442
520
                    create_pb.finished()
447
525
    def is_compatible(source, target):
448
526
        """Be compatible with GitRepository."""
449
527
        # FIXME: Also check target uses VersionedFile
450
 
        return (isinstance(source, RemoteGitRepository) and 
 
528
        return (isinstance(source, RemoteGitRepository) and
451
529
                target.supports_rich_root() and
452
530
                not isinstance(target, GitRepository))
453
531
 
454
532
 
455
533
class InterLocalGitNonGitRepository(InterGitNonGitRepository):
456
 
    """InterRepository that copies revisions from a local Git into a non-Git 
 
534
    """InterRepository that copies revisions from a local Git into a non-Git
457
535
    repository."""
458
536
 
459
537
    def fetch_objects(self, determine_wants, mapping, pb=None):
465
543
        try:
466
544
            self.target.lock_write()
467
545
            try:
468
 
                self.target.start_write_group()
469
 
                try:
470
 
                    import_git_objects(self.target, mapping, 
471
 
                            self.source._git.object_store, 
472
 
                            target_git_object_retriever, wants, pb)
473
 
                finally:
474
 
                    self.target.commit_write_group()
 
546
                return import_git_objects(self.target, mapping,
 
547
                    self.source._git.object_store, target_git_object_retriever,
 
548
                    wants, pb)
475
549
            finally:
476
550
                self.target.unlock()
477
551
        finally:
482
556
    def is_compatible(source, target):
483
557
        """Be compatible with GitRepository."""
484
558
        # FIXME: Also check target uses VersionedFile
485
 
        return (isinstance(source, LocalGitRepository) and 
 
559
        return (isinstance(source, LocalGitRepository) and
486
560
                target.supports_rich_root() and
487
561
                not isinstance(target, GitRepository))
488
562
 
490
564
class InterGitGitRepository(InterGitRepository):
491
565
    """InterRepository that copies between Git repositories."""
492
566
 
493
 
    def fetch_refs(self, revision_id=None, pb=None, find_ghosts=False, 
 
567
    def fetch_objects(self, determine_wants, mapping, pb=None):
 
568
        def progress(text):
 
569
            trace.note("git: %s", text)
 
570
        graphwalker = self.target._git.get_graph_walker()
 
571
        if (isinstance(self.source, LocalGitRepository) and
 
572
            isinstance(self.target, LocalGitRepository)):
 
573
            return self.source._git.fetch(self.target._git, determine_wants,
 
574
                progress)
 
575
        elif (isinstance(self.source, LocalGitRepository) and
 
576
              isinstance(self.target, RemoteGitRepository)):
 
577
            raise NotImplementedError
 
578
        elif (isinstance(self.source, RemoteGitRepository) and
 
579
              isinstance(self.target, LocalGitRepository)):
 
580
            f, commit = self.target._git.object_store.add_thin_pack()
 
581
            try:
 
582
                refs = self.source._git.fetch_pack(determine_wants,
 
583
                    graphwalker, f.write, progress)
 
584
                commit()
 
585
                return refs
 
586
            except:
 
587
                f.close()
 
588
                raise
 
589
        else:
 
590
            raise AssertionError
 
591
 
 
592
    def fetch_refs(self, revision_id=None, pb=None, find_ghosts=False,
494
593
              mapping=None, fetch_spec=None, branches=None):
495
594
        if mapping is None:
496
595
            mapping = self.source.get_mapping()
497
 
        def progress(text):
498
 
            trace.info("git: %s", text)
499
596
        r = self.target._git
500
597
        if revision_id is not None:
501
598
            args = [mapping.revision_id_bzr_to_foreign(revision_id)[0]]
507
604
            determine_wants = r.object_store.determine_wants_all
508
605
        else:
509
606
            determine_wants = lambda x: [y for y in args if not y in r.object_store]
 
607
        return self.fetch_objects(determine_wants, mapping)
510
608
 
511
 
        graphwalker = r.get_graph_walker()
512
 
        f, commit = r.object_store.add_thin_pack()
513
 
        try:
514
 
            refs = self.source.fetch_pack(determine_wants, graphwalker,
515
 
                                          f.write, progress)
516
 
            commit()
517
 
            return refs
518
 
        except:
519
 
            f.close()
520
 
            raise
521
609
 
522
610
    @staticmethod
523
611
    def is_compatible(source, target):
524
612
        """Be compatible with GitRepository."""
525
 
        return (isinstance(source, GitRepository) and 
 
613
        return (isinstance(source, GitRepository) and
526
614
                isinstance(target, GitRepository))