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

Avoid invoking git directly.

Show diffs side-by-side

added added

removed removed

Lines of Context:
43
43
    mapping_registry,
44
44
    symlink_to_blob,
45
45
    )
46
 
from bzrlib.plugins.git.shamap import (
 
46
from bzrlib.plugins.git.cache import (
47
47
    from_repository as cache_from_repository,
48
48
    )
49
49
 
71
71
        self._cache = lru_cache.LRUSizeCache(max_size=MAX_TREE_CACHE_SIZE,
72
72
            after_cleanup_size=None, compute_size=approx_tree_size)
73
73
 
74
 
    def revision_tree(self, revid):            
 
74
    def revision_tree(self, revid):
75
75
        try:
76
 
            return self._cache[revid] 
 
76
            tree = self._cache[revid]
77
77
        except KeyError:
78
78
            tree = self.repository.revision_tree(revid)
79
79
            self.add(tree)
80
 
            return tree
 
80
        assert tree.get_revision_id() == tree.inventory.revision_id
 
81
        return tree
81
82
 
82
83
    def iter_revision_trees(self, revids):
83
 
        trees = dict([(k, self._cache.get(k)) for k in revids]) 
84
 
        for tree in self.repository.revision_trees(
85
 
                [r for r, v in trees.iteritems() if v is None]):
 
84
        trees = {}
 
85
        todo = []
 
86
        for revid in revids:
 
87
            try:
 
88
                tree = self._cache[revid]
 
89
            except KeyError:
 
90
                todo.append(revid)
 
91
            else:
 
92
                assert tree.get_revision_id() == revid
 
93
                assert tree.inventory.revision_id == revid
 
94
                trees[revid] = tree
 
95
        for tree in self.repository.revision_trees(todo):
86
96
            trees[tree.get_revision_id()] = tree
87
97
            self.add(tree)
88
98
        return (trees[r] for r in revids)
142
152
            expected_sha))
143
153
 
144
154
 
145
 
def _tree_to_objects(tree, parent_trees, idmap, unusual_modes, dummy_file_name=None):
 
155
def _tree_to_objects(tree, parent_trees, idmap, unusual_modes,
 
156
                     dummy_file_name=None):
146
157
    """Iterate over the objects that were introduced in a revision.
147
158
 
148
159
    :param idmap: id map
149
 
    :param unusual_modes: Unusual file modes
 
160
    :param parent_trees: Parent revision trees
 
161
    :param unusual_modes: Unusual file modes dictionary
150
162
    :param dummy_file_name: File name to use for dummy files
151
163
        in empty directories. None to skip empty directories
152
164
    :return: Yields (path, object, ie) entries
173
185
                    pie.symlink_target == ie.symlink_target):
174
186
                    return pie
175
187
        raise KeyError
 
188
 
 
189
    # Find all the changed blobs
176
190
    for (file_id, path, changed_content, versioned, parent, name, kind,
177
191
         executable) in tree.iter_changes(base_tree):
178
192
        if kind[1] == "file":
179
193
            ie = tree.inventory[file_id]
180
194
            if changed_content:
181
 
                
182
195
                try:
183
196
                    pie = find_unchanged_parent_ie(ie, other_parent_trees)
184
197
                except KeyError:
185
198
                    pass
186
199
                else:
187
 
                    shamap[ie.file_id] = idmap.lookup_blob_id(
188
 
                        pie.file_id, pie.revision)
 
200
                    try:
 
201
                        shamap[ie.file_id] = idmap.lookup_blob_id(
 
202
                            pie.file_id, pie.revision)
 
203
                    except KeyError:
 
204
                        # no-change merge ?
 
205
                        blob = Blob()
 
206
                        blob.data = tree.get_file_text(ie.file_id)
 
207
                        shamap[ie.file_id] = blob.id
189
208
            if not file_id in shamap:
190
209
                new_blobs.append((path[1], ie))
191
210
            new_trees[posixpath.dirname(path[1])] = parent[1]
201
220
            new_trees[posixpath.dirname(path[1])] = parent[1]
202
221
        elif kind[1] not in (None, "directory"):
203
222
            raise AssertionError(kind[1])
204
 
        if path[0] is not None:
 
223
        if path[0] is not None and parent[0] in tree.inventory and tree.inventory[parent[0]].kind == "directory":
 
224
            # Removal
205
225
            new_trees[posixpath.dirname(path[0])] = parent[0]
206
226
    
 
227
    # Fetch contents of the blobs that were changed
207
228
    for (path, ie), chunks in tree.iter_files_bytes(
208
229
        [(ie.file_id, (path, ie)) for (path, ie) in new_blobs]):
209
230
        obj = Blob()
214
235
    for path in unusual_modes:
215
236
        parent_path = posixpath.dirname(path)
216
237
        new_trees[parent_path] = tree.path2id(parent_path)
217
 
    
 
238
 
218
239
    trees = {}
219
240
    while new_trees:
220
241
        items = new_trees.items()
221
242
        new_trees = {}
222
243
        for path, file_id in items:
223
 
            try:
224
 
                parent_id = tree.inventory[file_id].parent_id
225
 
            except errors.NoSuchId:
226
 
                # Directory was removed recursively perhaps ?
227
 
                continue
 
244
            parent_id = tree.inventory[file_id].parent_id
228
245
            if parent_id is not None:
229
246
                parent_path = urlutils.dirname(path)
230
247
                new_trees[parent_path] = parent_id
324
341
            try:
325
342
                return self._lookup_revision_sha1(revid)
326
343
            except errors.NoSuchRevision:
327
 
                trace.warning("Ignoring ghost parent %s", revid)
328
344
                return None
329
345
        return self.mapping.export_commit(rev, tree_sha, parent_lookup,
330
346
            roundtrip)
331
347
 
 
348
    def _create_fileid_map_blob(self, inv):
 
349
        # FIXME: This can probably be a lot more efficient, 
 
350
        # not all files necessarily have to be processed.
 
351
        file_ids = {}
 
352
        for (path, ie) in inv.iter_entries():
 
353
            if self.mapping.generate_file_id(path) != ie.file_id:
 
354
                file_ids[path] = ie.file_id
 
355
        return self.mapping.export_fileid_map(file_ids)
 
356
 
332
357
    def _revision_to_objects(self, rev, tree, roundtrip):
333
358
        """Convert a revision to a set of git objects.
334
359
 
355
380
                root_tree = Tree()
356
381
            else:
357
382
                base_sha1 = self._lookup_revision_sha1(rev.parent_ids[0])
358
 
                root_tree = self[base_sha1]
 
383
                root_tree = self[self[base_sha1].tree]
359
384
            root_ie = tree.inventory.root
360
 
        if roundtrip:
361
 
            # FIXME: This can probably be a lot more efficient, 
362
 
            # not all files necessarily have to be processed.
363
 
            file_ids = {}
364
 
            for (path, ie) in tree.inventory.iter_entries():
365
 
                if self.mapping.generate_file_id(path) != ie.file_id:
366
 
                    file_ids[path] = ie.file_id
367
 
            b = self.mapping.export_fileid_map(file_ids)
 
385
        if roundtrip and self.mapping.BZR_FILE_IDS_FILE is not None:
 
386
            b = self._create_fileid_map_blob(tree.inventory)
368
387
            if b is not None:
369
388
                root_tree[self.mapping.BZR_FILE_IDS_FILE] = ((stat.S_IFREG | 0644), b.id)
370
389
                yield self.mapping.BZR_FILE_IDS_FILE, b, None
371
390
        yield "", root_tree, root_ie
372
 
        commit_obj = self._reconstruct_commit(rev, root_tree.id, roundtrip=roundtrip)
 
391
        commit_obj = self._reconstruct_commit(rev, root_tree.id,
 
392
            roundtrip=roundtrip)
373
393
        try:
374
394
            foreign_revid, mapping = mapping_registry.parse_revision_id(
375
395
                rev.revision_id)
388
408
        updater = self._get_updater(rev)
389
409
        for path, obj, ie in self._revision_to_objects(rev, tree,
390
410
            roundtrip=True):
391
 
            updater.add_object(obj, ie)
 
411
            updater.add_object(obj, ie, path)
392
412
        commit_obj = updater.finish()
393
413
        return commit_obj.id
394
414
 
443
463
                raise AssertionError("unknown entry kind '%s'" % entry.kind)
444
464
        tree = directory_to_tree(inv[fileid], get_ie_sha1, unusual_modes,
445
465
            self.mapping.BZR_DUMMY_FILE)
 
466
        if (inv.root.file_id == fileid and
 
467
            self.mapping.BZR_FILE_IDS_FILE is not None):
 
468
            b = self._create_fileid_map_blob(inv)
 
469
            # If this is the root tree, add the file ids
 
470
            tree[self.mapping.BZR_FILE_IDS_FILE] = ((stat.S_IFREG | 0644), b.id)
446
471
        _check_expected_sha(expected_sha, tree)
447
472
        return tree
448
473
 
465
490
            try:
466
491
                return mapping_registry.parse_revision_id(revid)[0]
467
492
            except errors.InvalidRevisionId:
468
 
                self._update_sha_map(revid)
 
493
                self.repository.lock_read()
 
494
                try:
 
495
                    self._update_sha_map(revid)
 
496
                finally:
 
497
                    self.repository.unlock()
469
498
                return self._cache.idmap.lookup_commit(revid)
470
499
 
471
500
    def get_raw(self, sha):
483
512
            if type == "commit":
484
513
                return self.repository.has_revision(type_data[0])
485
514
            elif type == "blob":
486
 
                return self.repository.texts.has_version(type_data)
 
515
                return self.repository.texts.has_key(type_data)
487
516
            elif type == "tree":
488
517
                return self.repository.has_revision(type_data[1])
489
518
            else:
492
521
            return False
493
522
 
494
523
    def lookup_git_shas(self, shas, update_map=True):
 
524
        from dulwich.protocol import ZERO_SHA
495
525
        ret = {}
496
526
        for sha in shas:
 
527
            if sha == ZERO_SHA:
 
528
                ret[sha] = ("commit", (NULL_REVISION, None))
 
529
                continue
497
530
            try:
498
531
                ret[sha] = self._cache.idmap.lookup_git_sha(sha)
499
532
            except KeyError:
550
583
        else:
551
584
            raise AssertionError("Unknown object type '%s'" % type)
552
585
 
 
586
    def generate_lossy_pack_contents(self, have, want, progress=None,
 
587
            get_tagged=None):
 
588
        return self.generate_pack_contents(have, want, progress, get_tagged,
 
589
            lossy=True)
 
590
 
553
591
    def generate_pack_contents(self, have, want, progress=None,
554
 
            get_tagged=None):
 
592
            get_tagged=None, lossy=False):
555
593
        """Iterate over the contents of a pack file.
556
594
 
557
595
        :param have: List of SHA1s of objects that should not be sent
589
627
                pb.update("generating git objects", i, len(todo))
590
628
                rev = self.repository.get_revision(revid)
591
629
                tree = self.tree_cache.revision_tree(revid)
592
 
                for path, obj, ie in self._revision_to_objects(rev, tree):
 
630
                for path, obj, ie in self._revision_to_objects(rev, tree,
 
631
                    roundtrip=not lossy):
593
632
                    ret.append((obj, path))
594
633
        finally:
595
634
            pb.finished()