/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

Avoid invoking git directly.

Show diffs side-by-side

added added

removed removed

Lines of Context:
65
65
 
66
66
from bzrlib.plugins.git.mapping import (
67
67
    DEFAULT_FILE_MODE,
68
 
    GitFileIdMap,
69
68
    mode_is_executable,
70
69
    mode_kind,
71
 
    squash_revision,
72
70
    warn_unusual_mode,
73
71
    )
74
72
from bzrlib.plugins.git.object_store import (
157
155
                tuple(parent_keys), ie.text_sha1, chunks)])
158
156
    invdelta = []
159
157
    if base_hexsha is not None:
160
 
        old_path = path # Renames are not supported yet
 
158
        old_path = path.decode("utf-8") # Renames are not supported yet
161
159
        if stat.S_ISDIR(base_mode):
162
160
            invdelta.extend(remove_disappeared_children(base_inv, old_path,
163
161
                lookup_object(base_hexsha), [], lookup_object))
164
162
    else:
165
163
        old_path = None
166
 
    invdelta.append((old_path, path, file_id, ie))
 
164
    new_path = path.decode("utf-8")
 
165
    invdelta.append((old_path, new_path, file_id, ie))
167
166
    if base_hexsha != hexsha:
168
 
        store_updater.add_object(blob, ie)
 
167
        store_updater.add_object(blob, ie, path)
169
168
    return invdelta
170
169
 
171
170
 
195
194
 
196
195
def remove_disappeared_children(base_inv, path, base_tree, existing_children,
197
196
        lookup_object):
 
197
    """Generate an inventory delta for removed children.
 
198
 
 
199
    :param base_inv: Base inventory against which to generate the 
 
200
        inventory delta.
 
201
    :param path: Path to process (unicode)
 
202
    :param base_tree: Git Tree base object
 
203
    :param existing_children: Children that still exist
 
204
    :param lookup_object: Lookup a git object by its SHA1
 
205
    :return: Inventory delta, as list
 
206
    """
 
207
    assert type(path) is unicode
198
208
    ret = []
199
209
    for name, mode, hexsha in base_tree.iteritems():
200
210
        if name in existing_children:
201
211
            continue
202
212
        c_path = posixpath.join(path, name.decode("utf-8"))
203
 
        ret.append((c_path, None, base_inv.path2id(c_path), None))
 
213
        file_id = base_inv.path2id(c_path)
 
214
        assert file_id is not None
 
215
        ret.append((c_path, None, file_id, None))
204
216
        if stat.S_ISDIR(mode):
205
217
            ret.extend(remove_disappeared_children(
206
218
                base_inv, c_path, lookup_object(hexsha), [], lookup_object))
214
226
    """Import a git tree object into a bzr repository.
215
227
 
216
228
    :param texts: VersionedFiles object to add to
217
 
    :param path: Path in the tree
 
229
    :param path: Path in the tree (str)
 
230
    :param name: Name of the tree (str)
218
231
    :param tree: A git tree object
219
232
    :param base_inv: Base inventory against which to return inventory delta
220
233
    :return: Inventory delta for this subtree
221
234
    """
 
235
    assert type(path) is str
 
236
    assert type(name) is str
222
237
    if base_hexsha == hexsha and base_mode == mode:
223
238
        # If nothing has changed since the base revision, we're done
224
239
        return [], {}
232
247
        old_path = None # Newly appeared here
233
248
    else:
234
249
        base_tree = lookup_object(base_hexsha)
235
 
        old_path = path # Renames aren't supported yet
 
250
        old_path = path.decode("utf-8") # Renames aren't supported yet
 
251
    new_path = path.decode("utf-8")
236
252
    if base_tree is None or type(base_tree) is not Tree:
237
253
        ie.revision = revision_id
238
 
        invdelta.append((old_path, path, ie.file_id, ie))
 
254
        invdelta.append((old_path, new_path, ie.file_id, ie))
239
255
        texts.insert_record_stream([
240
256
            ChunkedContentFactory((ie.file_id, ie.revision), (), None, [])])
241
257
    # Remember for next time
279
295
            child_modes[child_path] = child_mode
280
296
    # Remove any children that have disappeared
281
297
    if base_tree is not None and type(base_tree) is Tree:
282
 
        invdelta.extend(remove_disappeared_children(base_inv, old_path, 
 
298
        invdelta.extend(remove_disappeared_children(base_inv, old_path,
283
299
            base_tree, existing_children, lookup_object))
284
 
    store_updater.add_object(tree, ie)
 
300
    store_updater.add_object(tree, ie, path)
285
301
    return invdelta, child_modes
286
302
 
287
303
 
292
308
        raise AssertionError("unusual modes don't match: %r != %r" % (
293
309
            unusual_modes, new_unusual_modes))
294
310
    # Verify that we can reconstruct the commit properly
295
 
    rec_o = target_git_object_retriever._reconstruct_commit(rev, o.tree)
 
311
    rec_o = target_git_object_retriever._reconstruct_commit(rev, o.tree, True)
296
312
    if rec_o != o:
297
313
        raise AssertionError("Reconstructed commit differs: %r != %r" % (
298
314
            rec_o, o))
325
341
def import_git_commit(repo, mapping, head, lookup_object,
326
342
                      target_git_object_retriever, trees_cache):
327
343
    o = lookup_object(head)
328
 
    rev = mapping.import_commit(o)
 
344
    rev = mapping.import_commit(o,
 
345
            lambda x: target_git_object_retriever.lookup_git_sha(x)[1][0])
329
346
    # We have to do this here, since we have to walk the tree and
330
347
    # we need to make sure to import the blobs / trees with the right
331
348
    # path; this may involve adding them more than once.
339
356
        base_tree = lookup_object(o.parents[0]).tree
340
357
        base_mode = stat.S_IFDIR
341
358
    store_updater = target_git_object_retriever._get_updater(rev)
342
 
    store_updater.add_object(o, None)
343
 
    root_tree = lookup_object(o.tree)
344
 
    try:
345
 
        file_id_map_sha = root_tree[mapping.BZR_FILE_IDS_FILE][1]
346
 
    except KeyError:
347
 
        file_ids = {}
348
 
    else:
349
 
        file_ids = mapping.import_fileid_map(lookup_object(file_id_map_sha))
350
 
    lookup_file_id = GitFileIdMap(file_ids, mapping).lookup_file_id
 
359
    store_updater.add_object(o, None, None)
 
360
    fileid_map = mapping.get_fileid_map(lookup_object, o.tree)
351
361
    inv_delta, unusual_modes = import_git_tree(repo.texts,
352
 
            mapping, "", u"", (base_tree, o.tree), base_inv, 
 
362
            mapping, "", "", (base_tree, o.tree), base_inv,
353
363
            None, rev.revision_id, [p.inventory for p in parent_trees],
354
364
            lookup_object, (base_mode, stat.S_IFDIR), store_updater,
355
 
            lookup_file_id,
 
365
            fileid_map.lookup_file_id,
356
366
            allow_submodules=getattr(repo._format, "supports_tree_reference", False))
357
367
    store_updater.finish()
358
368
    if unusual_modes != {}:
365
375
        basis_id = NULL_REVISION
366
376
        base_inv = None
367
377
    rev.inventory_sha1, inv = repo.add_inventory_by_delta(basis_id,
368
 
              inv_delta, rev.revision_id, rev.parent_ids,
369
 
              base_inv)
 
378
              inv_delta, rev.revision_id, rev.parent_ids, base_inv)
370
379
    ret_tree = RevisionTree(repo, inv, rev.revision_id)
371
380
    trees_cache.add(ret_tree)
372
381
    repo.add_revision(rev.revision_id, rev)
405
414
        except KeyError:
406
415
            continue
407
416
        if isinstance(o, Commit):
408
 
            rev = mapping.import_commit(o)
 
417
            rev = mapping.import_commit(o, lambda x: None)
409
418
            if repo.has_revision(rev.revision_id):
410
419
                continue
411
 
            squash_revision(repo, rev)
412
420
            graph.append((o.id, o.parents))
413
421
            heads.extend([p for p in o.parents if p not in checked])
414
422
        elif isinstance(o, Tag):
466
474
        """See InterRepository.copy_content."""
467
475
        self.fetch(revision_id, pb, find_ghosts=False)
468
476
 
469
 
    def fetch(self, revision_id=None, pb=None, find_ghosts=False,
470
 
        mapping=None, fetch_spec=None):
471
 
        self.fetch_refs(revision_id=revision_id, pb=pb,
472
 
            find_ghosts=find_ghosts, mapping=mapping, fetch_spec=fetch_spec)
473
 
 
474
477
 
475
478
class InterGitNonGitRepository(InterGitRepository):
476
479
    """Base InterRepository that copies revisions from a Git into a non-Git
477
480
    repository."""
478
481
 
479
 
    def fetch_refs(self, revision_id=None, pb=None, find_ghosts=False,
 
482
    def fetch(self, revision_id=None, pb=None, find_ghosts=False,
480
483
              mapping=None, fetch_spec=None):
481
484
        if mapping is None:
482
485
            mapping = self.source.get_mapping()
492
495
            if interesting_heads is None:
493
496
                ret = [sha for (ref, sha) in refs.iteritems() if not ref.endswith("^{}")]
494
497
            else:
495
 
                ret = [mapping.revision_id_bzr_to_foreign(revid)[0] for revid in interesting_heads if revid not in (None, NULL_REVISION)]
496
 
            return [rev for rev in ret if not self.target.has_revision(mapping.revision_id_foreign_to_bzr(rev))]
 
498
                ret = [self.source.lookup_bzr_revision_id(revid)[0] for revid in interesting_heads if revid not in (None, NULL_REVISION)]
 
499
            return [rev for rev in ret if not self.target.has_revision(self.source.lookup_foreign_revision_id(rev))]
497
500
        (pack_hint, _) = self.fetch_objects(determine_wants, mapping, pb)
498
501
        if pack_hint is not None and self.target._format.pack_compresses:
499
502
            self.target.pack(hint=pack_hint)
500
 
        if interesting_heads is not None:
501
 
            present_interesting_heads = self.target.has_revisions(interesting_heads)
502
 
            missing_interesting_heads = set(interesting_heads) - present_interesting_heads
503
 
            if missing_interesting_heads:
504
 
                raise AssertionError("Missing interesting heads: %r" %
505
 
                    missing_interesting_heads)
506
503
        return self._refs
507
504
 
508
505
 
629
626
        else:
630
627
            raise AssertionError
631
628
 
632
 
    def fetch_refs(self, revision_id=None, pb=None, find_ghosts=False,
 
629
    def fetch(self, revision_id=None, pb=None, find_ghosts=False,
633
630
              mapping=None, fetch_spec=None, branches=None):
634
631
        if mapping is None:
635
632
            mapping = self.source.get_mapping()
646
643
            determine_wants = lambda x: [y for y in args if not y in r.object_store]
647
644
        return self.fetch_objects(determine_wants, mapping)[0]
648
645
 
649
 
 
650
646
    @staticmethod
651
647
    def is_compatible(source, target):
652
648
        """Be compatible with GitRepository."""