/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

Add simple HACKING document.

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
 
41
43
            self.mapping = self.repository.get_mapping()
42
44
        else:
43
45
            self.mapping = mapping
44
 
        self._idmap = GitShaMap(self.repository._transport)
 
46
        self._idmap = SqliteGitShaMap(self.repository._transport)
45
47
 
46
48
    def _update_sha_map(self):
47
49
        all_revids = self.repository.all_revision_ids()
55
57
                pb.update("updating git map", i, len(all_revids))
56
58
                self._update_sha_map_revision(revid)
57
59
        finally:
 
60
            self._idmap.commit()
58
61
            pb.finished()
59
62
 
60
63
    def _update_sha_map_revision(self, revid):
61
64
        inv = self.repository.get_inventory(revid)
62
 
        objects = inventory_to_tree_and_blobs(self.repository, self.mapping, revid)
 
65
        objects = inventory_to_tree_and_blobs(self.repository, self.mapping,
 
66
            revid)
63
67
        for sha, o, path in objects:
64
68
            if path == "":
65
69
                tree_sha = sha
66
70
            ie = inv[inv.path2id(path)]
67
71
            if ie.kind in ("file", "symlink"):
68
72
                self._idmap.add_entry(sha, "blob", (ie.file_id, ie.revision))
 
73
            elif ie.kind == "directory":
 
74
                self._idmap.add_entry(sha, "tree", (path, ie.revision))
69
75
            else:
70
 
                self._idmap.add_entry(sha, "tree", (ie.file_id, ie.revision))
 
76
                raise AssertionError()
71
77
        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))
 
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))
74
82
 
75
83
    def _get_blob(self, fileid, revision):
76
 
        text = self.repository.texts.get_record_stream([(fileid, revision)], "unordered", True).next().get_bytes_as("fulltext")
 
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
        """
 
89
        text = self.repository.texts.get_record_stream([(fileid, revision)],
 
90
            "unordered", True).next().get_bytes_as("fulltext")
77
91
        blob = Blob()
78
92
        blob._text = text
79
93
        return blob
80
94
 
81
 
    def _get_tree(self, fileid, revid):
82
 
        raise NotImplementedError(self._get_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)
 
108
                tree.add(stat.S_IFDIR, name.encode('UTF-8'),
 
109
                    subtree.sha().hexdigest())
 
110
            elif ie.kind == "file":
 
111
                blob = self._get_blob(ie.file_id, revid)
 
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
83
120
 
84
121
    def _get_commit(self, revid, tree_sha):
85
122
        rev = self.repository.get_revision(revid)