/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

Properly set InventoryEntry revision when changing symlink targets.

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