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."""
21
from dulwich.index import (
26
from bzrlib.repository import (
30
from dulwich.objects import (
37
from bzrlib.plugins.git.mapping import (
42
class GitCommitBuilder(CommitBuilder):
44
def __init__(self, *args, **kwargs):
45
super(GitCommitBuilder, self).__init__(*args, **kwargs)
46
self.store = self.repository._git.object_store
49
def record_entry_contents(self, ie, parent_invs, path, tree,
51
raise NotImplementedError(self.record_entry_contents)
53
def record_delete(self, path, file_id):
54
self._blobs[path] = None
55
self._any_changes = True
57
def record_iter_changes(self, workingtree, basis_revid, iter_changes):
58
index = getattr(workingtree, "index", None)
60
def index_sha1(path, file_id):
61
return index.get_sha1(path.encode("utf-8"))
62
text_sha1 = link_sha1 = index_sha1
64
def link_sha1(path, file_id):
66
blob.data = workingtree.get_symlink_target(file_id)
68
def text_sha1(path, file_id):
70
blob.data = workingtree.get_file_text(file_id, path)
72
for (file_id, path, changed_content, versioned, parent, name, kind,
73
executable) in iter_changes:
74
if kind[1] in ("directory",):
75
if kind[0] in ("file", "symlink"):
76
self.record_delete(path[0], file_id)
79
self.record_delete(path[0], file_id)
83
sha = text_sha1(path[1], file_id)
84
elif kind[1] == "symlink":
86
sha = link_sha1(path[1], file_id)
87
elif kind[1] == "tree-reference":
91
raise AssertionError("Unknown kind %r" % kind[1])
94
self._any_changes = True
95
self._blobs[path[1].encode("utf-8")] = (mode, sha)
96
yield file_id, path, (None, None)
97
# Fill in entries that were not changed
98
basis_tree = workingtree.basis_tree()
99
assert basis_tree.get_revision_id() == basis_revid
100
for path, entry in basis_tree.iter_entries_by_dir():
101
if entry.kind not in ("file", "symlink"):
103
if not path in self._blobs:
105
if entry.kind == "symlink":
106
blob.data = basis_tree.get_symlink_target(entry.file_id)
108
blob.data = basis_tree.get_file_text(entry.file_id)
109
self._blobs[path.encode("utf-8")] = (entry_mode(entry), blob.id)
111
def finish_inventory(self):
112
# eliminate blobs that were removed
113
for path, entry in self._blobs.iteritems():
115
del self._blobs[path]
117
def commit(self, message):
119
c.parents = [self.repository.lookup_git_revid(revid)[0] for revid in self.parents]
120
c.tree = commit_tree(self.store,
121
[(path, sha, mode) for (path, (mode, sha)) in self._blobs.iteritems()])
122
c.committer = self._committer
123
c.author = self._revprops.get('author', self._committer)
124
c.commit_time = int(self._timestamp)
125
c.author_time = int(self._timestamp)
126
c.commit_timezone = self._timezone
127
c.author_timezone = self._timezone
128
c.message = message.encode("utf-8")
129
self.store.add_object(c)
130
return self.repository.get_mapping().revision_id_foreign_to_bzr(c.id)