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

Set version string.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2009 Canonical Ltd
 
1
# Copyright (C) 2009 Jelmer Vernooij <jelmer@samba.org>
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
16
16
 
17
17
"""Map from Git sha's to Bazaar objects."""
18
18
 
19
 
import bzrlib
20
 
 
21
 
from bzrlib import ui
22
 
 
23
 
from bzrlib.errors import NoSuchRevision
 
19
from dulwich.objects import (
 
20
    Blob,
 
21
    Tree,
 
22
    )
 
23
import stat
 
24
 
 
25
from bzrlib import (
 
26
    ui,
 
27
    )
24
28
 
25
29
from bzrlib.plugins.git.mapping import (
26
30
    inventory_to_tree_and_blobs,
27
31
    revision_to_commit,
28
32
    )
29
 
from bzrlib.plugins.git.shamap import GitShaMap
30
 
 
31
 
from dulwich.objects import (
32
 
    Blob,
 
33
from bzrlib.plugins.git.shamap import (
 
34
    SqliteGitShaMap,
33
35
    )
34
36
 
35
37
 
36
 
class GitObjectConverter(object):
 
38
class BazaarObjectStore(object):
 
39
    """A Git-style object store backed onto a Bazaar repository."""
37
40
 
38
41
    def __init__(self, repository, mapping=None):
39
42
        self.repository = repository
41
44
            self.mapping = self.repository.get_mapping()
42
45
        else:
43
46
            self.mapping = mapping
44
 
        self._idmap = GitShaMap(self.repository._transport)
 
47
        self._idmap = SqliteGitShaMap(self.repository._transport)
45
48
 
46
49
    def _update_sha_map(self):
47
50
        all_revids = self.repository.all_revision_ids()
55
58
                pb.update("updating git map", i, len(all_revids))
56
59
                self._update_sha_map_revision(revid)
57
60
        finally:
 
61
            self._idmap.commit()
58
62
            pb.finished()
59
63
 
60
64
    def _update_sha_map_revision(self, revid):
61
65
        inv = self.repository.get_inventory(revid)
62
 
        objects = inventory_to_tree_and_blobs(self.repository, self.mapping, revid)
 
66
        objects = inventory_to_tree_and_blobs(self.repository, self.mapping,
 
67
            revid)
63
68
        for sha, o, path in objects:
64
69
            if path == "":
65
70
                tree_sha = sha
66
71
            ie = inv[inv.path2id(path)]
67
72
            if ie.kind in ("file", "symlink"):
68
73
                self._idmap.add_entry(sha, "blob", (ie.file_id, ie.revision))
 
74
            elif ie.kind == "directory":
 
75
                self._idmap.add_entry(sha, "tree", (path, ie.revision))
69
76
            else:
70
 
                self._idmap.add_entry(sha, "tree", (ie.file_id, ie.revision))
 
77
                raise AssertionError()
71
78
        rev = self.repository.get_revision(revid)
72
 
        commit_obj = revision_to_commit(rev, tree_sha, self._idmap._parent_lookup)
73
 
        self._idmap.add_entry(commit_obj.sha().hexdigest(), "commit", (revid, tree_sha))
 
79
        commit_obj = revision_to_commit(rev, tree_sha,
 
80
            self._idmap._parent_lookup)
 
81
        self._idmap.add_entry(commit_obj.sha().hexdigest(), "commit",
 
82
            (revid, tree_sha))
74
83
 
75
84
    def _get_blob(self, fileid, revision):
76
 
        text = self.repository.texts.get_record_stream([(fileid, revision)], "unordered", True).next().get_bytes_as("fulltext")
 
85
        """Return a Git Blob object from a fileid and revision stored in bzr.
 
86
        
 
87
        :param fileid: File id of the text
 
88
        :param revision: Revision of the text
 
89
        """
 
90
        text = self.repository.texts.get_record_stream([(fileid, revision)],
 
91
            "unordered", True).next().get_bytes_as("fulltext")
77
92
        blob = Blob()
78
93
        blob._text = text
79
94
        return blob
80
95
 
81
 
    def _get_tree(self, fileid, revid):
82
 
        raise NotImplementedError(self._get_tree)
 
96
    def _get_tree(self, path, revid, inv=None):
 
97
        """Return a Git Tree object from a path and a revision stored in bzr.
 
98
 
 
99
        :param path: path in the tree.
 
100
        :param revision: Revision of the tree.
 
101
        """
 
102
        if inv is None:
 
103
            inv = self.repository.get_inventory(revid)
 
104
        tree = Tree()
 
105
        fileid = inv.path2id(path)
 
106
        for name, ie in inv[fileid].children.iteritems():
 
107
            if ie.kind == "directory":
 
108
                subtree = self._get_tree(inv.id2path(ie.file_id), revid, inv)
 
109
                tree.add(stat.S_IFDIR, name.encode('UTF-8'),
 
110
                    subtree.sha().hexdigest())
 
111
            elif ie.kind == "file":
 
112
                blob = self._get_blob(ie.file_id, ie.revision)
 
113
                mode = stat.S_IFREG | 0644
 
114
                if ie.executable:
 
115
                    mode |= 0111
 
116
                tree.add(mode, name.encode('UTF-8'), blob.sha().hexdigest())
 
117
            elif ie.kind == "symlink":
 
118
                raise AssertionError("Symlinks not yet supported")
 
119
        tree.serialize()
 
120
        return tree
83
121
 
84
122
    def _get_commit(self, revid, tree_sha):
85
123
        rev = self.repository.get_revision(revid)
86
124
        return revision_to_commit(rev, tree_sha, self._idmap._parent_lookup)
87
125
 
 
126
    def get_raw(self, sha):
 
127
        obj = self[sha]
 
128
        assert obj.id == sha
 
129
        return obj._text
 
130
 
88
131
    def __getitem__(self, sha):
89
132
        # See if sha is in map
90
133
        try: