1
# Copyright (C) 2009-2010 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.errors import (
29
from bzrlib.repository import (
33
from dulwich.objects import (
40
from bzrlib.plugins.git.mapping import (
45
class GitCommitBuilder(CommitBuilder):
46
"""Commit builder for Git repositories."""
48
supports_record_entry_contents = False
50
def __init__(self, *args, **kwargs):
51
super(GitCommitBuilder, self).__init__(*args, **kwargs)
52
self.store = self.repository._git.object_store
55
def record_entry_contents(self, ie, parent_invs, path, tree,
57
raise NotImplementedError(self.record_entry_contents)
59
def record_delete(self, path, file_id):
60
self._blobs[path] = None
61
self._any_changes = True
63
def record_iter_changes(self, workingtree, basis_revid, iter_changes):
64
index = getattr(workingtree, "index", None)
66
def index_sha1(path, file_id):
67
return index.get_sha1(path.encode("utf-8"))
68
text_sha1 = link_sha1 = index_sha1
70
def link_sha1(path, file_id):
72
blob.data = workingtree.get_symlink_target(file_id)
74
def text_sha1(path, file_id):
76
blob.data = workingtree.get_file_text(file_id, path)
79
for (file_id, path, changed_content, versioned, parent, name, kind,
80
executable) in iter_changes:
81
if kind[1] in ("directory",):
82
if kind[0] in ("file", "symlink"):
83
self.record_delete(path[0], file_id)
88
self.record_delete(path[0], file_id)
92
sha = text_sha1(path[1], file_id)
93
elif kind[1] == "symlink":
95
sha = link_sha1(path[1], file_id)
96
elif kind[1] == "tree-reference":
100
raise AssertionError("Unknown kind %r" % kind[1])
103
self._any_changes = True
104
self._blobs[path[1].encode("utf-8")] = (mode, sha)
105
file_sha1 = workingtree.get_file_sha1(file_id, path[1])
106
_, st = workingtree.get_file_with_stat(file_id, path[1])
107
yield file_id, path[1], (file_sha1, st)
108
if not seen_root and len(self.parents) == 0:
110
# Fill in entries that were not changed
111
basis_tree = workingtree.basis_tree()
112
assert basis_tree.get_revision_id() == basis_revid, "expected %r == %r" % (
113
basis_tree.get_revision_id(), basis_revid)
114
for path, entry in basis_tree.iter_entries_by_dir():
115
if entry.kind not in ("file", "symlink"):
117
if not path in self._blobs:
119
if entry.kind == "symlink":
120
blob.data = basis_tree.get_symlink_target(entry.file_id)
122
blob.data = basis_tree.get_file_text(entry.file_id)
123
self._blobs[path.encode("utf-8")] = (entry_mode(entry), blob.id)
124
self.new_inventory = None
126
def finish_inventory(self):
127
# eliminate blobs that were removed
128
for path, entry in iter(self._blobs.items()):
130
del self._blobs[path]
132
def _iterblobs(self):
133
return ((path, sha, mode) for (path, (mode, sha)) in self._blobs.iteritems())
135
def commit(self, message):
136
self._validate_unicode_text(message, 'commit message')
138
c.parents = [self.repository.lookup_bzr_revision_id(revid)[0] for revid in self.parents]
139
c.tree = commit_tree(self.store, self._iterblobs())
140
c.committer = self._committer
141
c.author = self._revprops.get('author', self._committer)
142
c.commit_time = int(self._timestamp)
143
c.author_time = int(self._timestamp)
144
c.commit_timezone = self._timezone
145
c.author_timezone = self._timezone
147
c.message = message.encode("utf-8")
148
self.store.add_object(c)
149
assert len(c.id) == 40
150
self._new_revision_id = self.repository.get_mapping().revision_id_foreign_to_bzr(c.id)
151
self.repository.commit_write_group()
152
return self._new_revision_id
154
def revision_tree(self):
155
return self.repository.revision_tree(self._new_revision_id)