/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.200.387 by Jelmer Vernooij
Initial work on supporting commit in git trees.
1
# Copyright (C) 2009 Jelmer Vernooij <jelmer@samba.org>
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
0.200.405 by Jelmer Vernooij
More work on commit.
17
18
"""Support for committing in native Git working trees."""
19
20
21
import stat
22
23
from bzrlib import (
24
    osutils,
25
    )
0.200.387 by Jelmer Vernooij
Initial work on supporting commit in git trees.
26
from bzrlib.repository import (
27
    CommitBuilder,
28
    )
29
30
from dulwich.objects import (
31
    Commit,
32
    Tree,
33
    )
34
35
36
class GitCommitBuilder(CommitBuilder):
37
38
    def __init__(self, *args, **kwargs):
39
        super(GitCommitBuilder, self).__init__(*args, **kwargs)
40
        self._trees = {}
41
0.200.405 by Jelmer Vernooij
More work on commit.
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
0.200.387 by Jelmer Vernooij
Initial work on supporting commit in git trees.
48
    def _add_tree(self, path):
0.200.405 by Jelmer Vernooij
More work on commit.
49
        if path in self._trees:
50
            return self._trees[path]
51
        if path == "":
52
            return self._new_tree("")
0.200.387 by Jelmer Vernooij
Initial work on supporting commit in git trees.
53
        dirname, basename = osutils.split(path)
54
        t = self._add_tree(dirname)
0.200.405 by Jelmer Vernooij
More work on commit.
55
        assert isinstance(basename, str)
0.200.387 by Jelmer Vernooij
Initial work on supporting commit in git trees.
56
        if not basename in t:
0.200.405 by Jelmer Vernooij
More work on commit.
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]]
0.200.387 by Jelmer Vernooij
Initial work on supporting commit in git trees.
62
63
    def _change_blob(self, path, value):
0.200.405 by Jelmer Vernooij
More work on commit.
64
        assert isinstance(path, str)
0.200.387 by Jelmer Vernooij
Initial work on supporting commit in git trees.
65
        dirname, basename = osutils.split(path)
66
        t = self._add_tree(dirname)
67
        t[basename] = value
68
69
    def record_delete(self, path, file_id):
70
        dirname, basename = osutils.split(path)
71
        t = self._add_tree(dirname)
72
        del t[basename]
73
74
    def record_iter_changes(self, workingtree, basis_revid, iter_changes):
75
        for (file_id, path, changed_content, versioned, parent, name, kind, 
76
             executable) in iter_changes:
77
            if kind[1] in ("directory",):
78
                if kind[0] in ("file", "symlink"):
79
                    self.record_delete(path[0], file_id)
80
                continue
81
            if kind == "file":
82
                mode = stat.S_IFREG
83
            else:
84
                mode = stat.S_IFLNK
85
            if executable:
86
                mode |= 0111
0.200.405 by Jelmer Vernooij
More work on commit.
87
            self._change_blob(path[1].encode("utf-8"), (mode, workingtree.index.get_sha1(path[1].encode("utf-8"))))
88
            yield file_id, path, (None, None)
0.200.387 by Jelmer Vernooij
Initial work on supporting commit in git trees.
89
0.200.391 by Jelmer Vernooij
Fix syntax error.
90
    def commit(self, message):
0.200.387 by Jelmer Vernooij
Initial work on supporting commit in git trees.
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
        c = Commit()
96
        root_tree = self._add_tree("")
97
        c._tree = root_tree.id 
98
        c._committer = self._committer
99
        c._author = self._revprops.get('author', self._committer)
100
        c._commit_timestamp = self._timestamp
101
        c._author_timestamp = self._timestamp
102
        c._commit_timezone = self._timezone
103
        c._author_timezone = self._timezone
104
        c._message = message.encode("utf-8")
105
        self.repository._git.object_store.add_object(c)