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

Use new commit_tree function from dulwich.

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
"""Support for committing in native Git working trees."""
19
19
 
20
20
 
 
21
from dulwich.index import (
 
22
    commit_tree,
 
23
    )
21
24
import stat
22
25
 
23
 
from bzrlib import (
24
 
    osutils,
25
 
    )
26
26
from bzrlib.repository import (
27
27
    CommitBuilder,
28
28
    )
29
29
 
30
30
from dulwich.objects import (
 
31
    Blob,
31
32
    Commit,
32
33
    Tree,
33
34
    )
37
38
 
38
39
    def __init__(self, *args, **kwargs):
39
40
        super(GitCommitBuilder, self).__init__(*args, **kwargs)
40
 
        self._trees = {}
41
 
 
42
 
    def _new_tree(self, path):
43
 
        newtree = Tree()
44
 
        # FIXME: Inherit children from the base revision
45
 
        self._trees[path] = newtree
46
 
        return newtree
47
 
 
48
 
    def _add_tree(self, path):
49
 
        if path in self._trees:
50
 
            return self._trees[path]
51
 
        if path == "":
52
 
            return self._new_tree("")
53
 
        dirname, basename = osutils.split(path)
54
 
        t = self._add_tree(dirname)
55
 
        assert isinstance(basename, str)
56
 
        if not basename in t:
57
 
            newtree = self._new_tree(path)
58
 
            t[basename] = (stat.S_IFDIR, newtree.id)
59
 
            return newtree
60
 
        else:
61
 
            return self.repository._git.object_store[t[basename][1]]
62
 
 
63
 
    def _change_blob(self, path, value):
64
 
        assert isinstance(path, str)
65
 
        dirname, basename = osutils.split(path)
66
 
        t = self._add_tree(dirname)
67
 
        t[basename] = value
 
41
        self._blobs = {}
68
42
 
69
43
    def record_delete(self, path, file_id):
70
 
        dirname, basename = osutils.split(path)
71
 
        t = self._add_tree(dirname)
72
 
        del t[basename]
 
44
        self._blobs[path] = None
73
45
 
74
46
    def record_iter_changes(self, workingtree, basis_revid, iter_changes):
 
47
        index = getattr(workingtree, "index", None)
 
48
        if index is not None:
 
49
            def index_sha1(path, file_id):
 
50
                return index.get_sha1(path.encode("utf-8"))
 
51
            text_sha1 = link_sha1 = index_sha1
 
52
        else:
 
53
            def link_sha1(path, file_id):
 
54
                blob = Blob()
 
55
                blob.data = workingtree.get_symlink_target(file_id)
 
56
                return blob.id
 
57
            def text_sha1(path, file_id):
 
58
                blob = Blob()
 
59
                blob.data = workingtree.get_file_text(file_id, path)
 
60
                return blob.id
75
61
        for (file_id, path, changed_content, versioned, parent, name, kind, 
76
62
             executable) in iter_changes:
77
63
            if kind[1] in ("directory",):
80
66
                continue
81
67
            if kind == "file":
82
68
                mode = stat.S_IFREG
 
69
                sha = text_sha1(path[1], file_id)
83
70
            else:
84
71
                mode = stat.S_IFLNK
 
72
                sha = link_sha1(path[1], file_id)
85
73
            if executable:
86
74
                mode |= 0111
87
 
            self._change_blob(path[1].encode("utf-8"), (mode, workingtree.index.get_sha1(path[1].encode("utf-8"))))
 
75
            self._blobs[path[1].encode("utf-8")] = (mode, sha))
88
76
            yield file_id, path, (None, None)
 
77
        # FIXME: Import all blobs not set yet, and eliminate blobs set to None
89
78
 
90
79
    def commit(self, message):
91
 
        # FIXME: Eliminate any empty trees recursively
92
 
        # Write any tree objects to disk
93
 
        for path in sorted(self._trees.keys(), reverse=True):
94
 
            self.repository._git.object_store.add_object(self._trees[path])
95
80
        c = Commit()
96
 
        root_tree = self._add_tree("")
97
 
        c.tree = root_tree.id 
 
81
        c.tree = commit_tree(self.repository._git.object_store, self._blobs)
98
82
        c.committer = self._committer
99
83
        c.author = self._revprops.get('author', self._committer)
100
84
        c.commit_timestamp = self._timestamp
103
87
        c.author_timezone = self._timezone
104
88
        c.message = message.encode("utf-8")
105
89
        self.repository._git.object_store.add_object(c)
 
90
        return self.repository.mapping.revision_id_foreign_to_bzr(c.id)