1
# Copyright (C) 2009 Jelmer Vernooij <jelmer@samba.org>
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.
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.
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
18
"""Support for committing in native Git working trees."""
26
from bzrlib.repository import (
30
from dulwich.objects import (
36
class GitCommitBuilder(CommitBuilder):
38
def __init__(self, *args, **kwargs):
39
super(GitCommitBuilder, self).__init__(*args, **kwargs)
42
def _new_tree(self, path):
44
# FIXME: Inherit children from the base revision
45
self._trees[path] = newtree
48
def _add_tree(self, path):
49
if path in self._trees:
50
return self._trees[path]
52
return self._new_tree("")
53
dirname, basename = osutils.split(path)
54
t = self._add_tree(dirname)
55
assert isinstance(basename, str)
57
newtree = self._new_tree(path)
58
t[basename] = (stat.S_IFDIR, newtree.id)
61
return self.repository._git.object_store[t[basename][1]]
63
def _change_blob(self, path, value):
64
assert isinstance(path, str)
65
dirname, basename = osutils.split(path)
66
t = self._add_tree(dirname)
69
def record_delete(self, path, file_id):
70
dirname, basename = osutils.split(path)
71
t = self._add_tree(dirname)
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)
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)
90
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])
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)