/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

  • Committer: Jelmer Vernooij
  • Date: 2010-05-05 09:58:55 UTC
  • mto: (0.200.912 trunk)
  • mto: This revision was merged to the branch mainline in revision 6960.
  • Revision ID: jelmer@samba.org-20100505095855-i0165hooflvk9chy
Ignore control files in inventories.

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