/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.200.252 by Jelmer Vernooij
Clarify history, copyright.
1
# Copyright (C) 2009 Jelmer Vernooij <jelmer@samba.org>
0.200.228 by Jelmer Vernooij
Split out map.
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
17
"""Map from Git sha's to Bazaar objects."""
18
0.200.260 by Jelmer Vernooij
Add DictGitShaMap, useful for testing.
19
from dulwich.objects import (
20
    Blob,
21
    Tree,
22
    )
0.200.249 by Jelmer Vernooij
Implement Tree.
23
import stat
24
0.200.260 by Jelmer Vernooij
Add DictGitShaMap, useful for testing.
25
from bzrlib import (
26
    ui,
27
    )
0.200.228 by Jelmer Vernooij
Split out map.
28
0.200.229 by Jelmer Vernooij
More work on converter.
29
from bzrlib.plugins.git.mapping import (
30
    inventory_to_tree_and_blobs,
31
    revision_to_commit,
32
    )
0.200.260 by Jelmer Vernooij
Add DictGitShaMap, useful for testing.
33
from bzrlib.plugins.git.shamap import (
34
    SqliteGitShaMap,
0.200.231 by Jelmer Vernooij
Partially fix pull.
35
    )
36
0.200.228 by Jelmer Vernooij
Split out map.
37
38
class GitObjectConverter(object):
39
40
    def __init__(self, repository, mapping=None):
41
        self.repository = repository
42
        if mapping is None:
43
            self.mapping = self.repository.get_mapping()
44
        else:
45
            self.mapping = mapping
0.200.260 by Jelmer Vernooij
Add DictGitShaMap, useful for testing.
46
        self._idmap = SqliteGitShaMap(self.repository._transport)
0.200.228 by Jelmer Vernooij
Split out map.
47
48
    def _update_sha_map(self):
0.200.231 by Jelmer Vernooij
Partially fix pull.
49
        all_revids = self.repository.all_revision_ids()
50
        graph = self.repository.get_graph()
0.200.230 by Jelmer Vernooij
Implement sha cache.
51
        present_revids = set(self._idmap.revids())
0.200.231 by Jelmer Vernooij
Partially fix pull.
52
        pb = ui.ui_factory.nested_progress_bar()
53
        try:
54
            for i, revid in enumerate(graph.iter_topo_order(all_revids)):
55
                if revid in present_revids:
56
                    continue
57
                pb.update("updating git map", i, len(all_revids))
58
                self._update_sha_map_revision(revid)
59
        finally:
0.200.232 by Jelmer Vernooij
Fix pull from remote branches.
60
            self._idmap.commit()
0.200.231 by Jelmer Vernooij
Partially fix pull.
61
            pb.finished()
0.200.229 by Jelmer Vernooij
More work on converter.
62
63
    def _update_sha_map_revision(self, revid):
64
        inv = self.repository.get_inventory(revid)
0.200.260 by Jelmer Vernooij
Add DictGitShaMap, useful for testing.
65
        objects = inventory_to_tree_and_blobs(self.repository, self.mapping,
66
            revid)
0.200.229 by Jelmer Vernooij
More work on converter.
67
        for sha, o, path in objects:
68
            if path == "":
69
                tree_sha = sha
70
            ie = inv[inv.path2id(path)]
71
            if ie.kind in ("file", "symlink"):
72
                self._idmap.add_entry(sha, "blob", (ie.file_id, ie.revision))
0.200.232 by Jelmer Vernooij
Fix pull from remote branches.
73
            elif ie.kind == "directory":
74
                self._idmap.add_entry(sha, "tree", (path, ie.revision))
0.200.229 by Jelmer Vernooij
More work on converter.
75
            else:
0.200.232 by Jelmer Vernooij
Fix pull from remote branches.
76
                raise AssertionError()
0.200.229 by Jelmer Vernooij
More work on converter.
77
        rev = self.repository.get_revision(revid)
0.200.260 by Jelmer Vernooij
Add DictGitShaMap, useful for testing.
78
        commit_obj = revision_to_commit(rev, tree_sha,
79
            self._idmap._parent_lookup)
80
        self._idmap.add_entry(commit_obj.sha().hexdigest(), "commit",
81
            (revid, tree_sha))
0.200.229 by Jelmer Vernooij
More work on converter.
82
83
    def _get_blob(self, fileid, revision):
0.200.236 by Jelmer Vernooij
require bzr 1.13.
84
        """Return a Git Blob object from a fileid and revision stored in bzr.
85
        
86
        :param fileid: File id of the text
87
        :param revision: Revision of the text
88
        """
0.200.260 by Jelmer Vernooij
Add DictGitShaMap, useful for testing.
89
        text = self.repository.texts.get_record_stream([(fileid, revision)],
90
            "unordered", True).next().get_bytes_as("fulltext")
0.200.229 by Jelmer Vernooij
More work on converter.
91
        blob = Blob()
92
        blob._text = text
93
        return blob
94
0.200.249 by Jelmer Vernooij
Implement Tree.
95
    def _get_tree(self, path, revid, inv=None):
96
        """Return a Git Tree object from a path and a revision stored in bzr.
97
98
        :param path: path in the tree.
99
        :param revision: Revision of the tree.
100
        """
101
        if inv is None:
102
            inv = self.repository.get_inventory(revid)
103
        tree = Tree()
104
        fileid = inv.path2id(path)
105
        for name, ie in inv[fileid].children.iteritems():
106
            if ie.kind == "directory":
107
                subtree = self._get_tree(inv.id2path(ie.file_id), revid, inv)
0.200.260 by Jelmer Vernooij
Add DictGitShaMap, useful for testing.
108
                tree.add(stat.S_IFDIR, name.encode('UTF-8'),
109
                    subtree.sha().hexdigest())
0.200.249 by Jelmer Vernooij
Implement Tree.
110
            elif ie.kind == "file":
0.200.310 by Jelmer Vernooij
Fix pull from remote branches.
111
                blob = self._get_blob(ie.file_id, ie.revision)
0.200.249 by Jelmer Vernooij
Implement Tree.
112
                mode = stat.S_IFREG | 0644
113
                if ie.executable:
114
                    mode |= 0111
115
                tree.add(mode, name.encode('UTF-8'), blob.sha().hexdigest())
116
            elif ie.kind == "symlink":
117
                raise AssertionError("Symlinks not yet supported")
118
        tree.serialize()
119
        return tree
0.200.229 by Jelmer Vernooij
More work on converter.
120
121
    def _get_commit(self, revid, tree_sha):
122
        rev = self.repository.get_revision(revid)
0.200.231 by Jelmer Vernooij
Partially fix pull.
123
        return revision_to_commit(rev, tree_sha, self._idmap._parent_lookup)
0.200.228 by Jelmer Vernooij
Split out map.
124
0.200.310 by Jelmer Vernooij
Fix pull from remote branches.
125
    def get_raw(self, sha):
126
        obj = self[sha]
127
        assert obj.id == sha
128
        return obj._text
129
0.200.228 by Jelmer Vernooij
Split out map.
130
    def __getitem__(self, sha):
131
        # See if sha is in map
132
        try:
133
            (type, type_data) = self._idmap.lookup_git_sha(sha)
134
        except KeyError:
135
            # if not, see if there are any unconverted revisions and add them 
136
            # to the map, search for sha in map again
137
            self._update_sha_map()
138
            (type, type_data) = self._idmap.lookup_git_sha(sha)
139
        # convert object to git object
0.200.229 by Jelmer Vernooij
More work on converter.
140
        if type == "commit":
0.200.228 by Jelmer Vernooij
Split out map.
141
            return self._get_commit(*type_data)
0.200.229 by Jelmer Vernooij
More work on converter.
142
        elif type == "blob":
143
            return self._get_blob(*type_data)
144
        elif type == "tree":
145
            return self._get_tree(*type_data)
0.200.228 by Jelmer Vernooij
Split out map.
146
        else:
147
            raise AssertionError("Unknown object type '%s'" % type)