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
20
from __future__ import absolute_import
22
from dulwich.object_store import tree_lookup_path
37
from .mapping import (
43
class GitRevisionTree(revisiontree.RevisionTree):
44
"""Revision tree implementation based on Git objects."""
46
def __init__(self, repository, revision_id):
47
self._revision_id = revision_id
48
self._repository = repository
49
self.store = repository._git.object_store
50
assert isinstance(revision_id, str)
51
self.commit_id, self.mapping = repository.lookup_bzr_revision_id(revision_id)
53
commit = self.store[self.commit_id]
55
raise errors.NoSuchRevision(repository, revision_id)
56
self.tree = commit.tree
57
self._fileid_map = self.mapping.get_fileid_map(self.store.__getitem__, self.tree)
59
def get_file_revision(self, file_id, path=None):
61
path = self.id2path(file_id)
62
change_scanner = self._repository._file_change_scanner
63
(path, commit_id) = change_scanner.find_last_change_revision(path,
65
return self._repository.lookup_foreign_revision_id(commit_id, self.mapping)
67
def get_file_mtime(self, file_id, path=None):
68
revid = self.get_file_revision(file_id, path)
70
rev = self._repository.get_revision(revid)
71
except errors.NoSuchRevision:
72
raise errors.FileTimestampUnavailable(path)
75
def id2path(self, file_id):
76
return self._fileid_map.lookup_path(file_id)
78
def path2id(self, path):
79
if self.mapping.is_special_file(path):
81
return self._fileid_map.lookup_file_id(path.encode('utf-8'))
83
def all_file_ids(self):
84
return set(self._fileid_map.all_file_ids())
86
def get_root_id(self):
87
return self.path2id("")
89
def has_or_had_id(self, file_id):
90
return self.has_id(file_id)
92
def has_id(self, file_id):
94
path = self.id2path(file_id)
95
except errors.NoSuchId:
97
return self.has_filename(path)
99
def is_executable(self, file_id, path=None):
101
path = self.id2path(file_id)
103
(mode, hexsha) = tree_lookup_path(self.store.__getitem__, self.tree,
106
raise errors.NoSuchId(self, file_id)
108
# the tree root is a directory
110
return mode_is_executable(mode)
112
def kind(self, file_id, path=None):
114
path = self.id2path(file_id)
116
(mode, hexsha) = tree_lookup_path(self.store.__getitem__, self.tree,
119
raise errors.NoSuchId(self, file_id)
121
# the tree root is a directory
123
return mode_kind(mode)
125
def has_filename(self, path):
127
tree_lookup_path(self.store.__getitem__, self.tree,
128
path.encode("utf-8"))
134
def list_files(self, include_root=False, from_dir=None, recursive=True):
137
(mode, hexsha) = tree_lookup_path(self.store.__getitem__, self.tree,
138
from_dir.encode("utf-8"))
139
if mode is None: # Root
140
root_ie = self._get_dir_ie("", None)
142
parent_path = posixpath.dirname(from_dir.encode("utf-8"))
143
parent_id = self._fileid_map.lookup_file_id(parent_path)
144
if mode_kind(mode) == 'directory':
145
root_ie = self._get_dir_ie(from_dir.encode("utf-8"), parent_id)
147
root_ie = self._get_file_ie(from_dir.encode("utf-8"),
148
posixpath.basename(from_dir), mode, hexsha)
149
if from_dir != "" or include_root:
150
yield (from_dir, "V", root_ie.kind, root_ie.file_id, root_ie)
152
if root_ie.kind == 'directory':
153
todo.add((from_dir.encode("utf-8"), hexsha, root_ie.file_id))
155
(path, hexsha, parent_id) = todo.pop()
156
tree = self.store[hexsha]
157
for name, mode, hexsha in tree.iteritems():
158
if self.mapping.is_special_file(name):
160
child_path = posixpath.join(path, name)
161
if stat.S_ISDIR(mode):
162
ie = self._get_dir_ie(child_path, parent_id)
164
todo.add((child_path, hexsha, ie.file_id))
166
ie = self._get_file_ie(child_path, name, mode, hexsha, parent_id)
167
yield child_path, "V", ie.kind, ie.file_id, ie
169
def _get_file_ie(self, path, name, mode, hexsha, parent_id):
170
kind = mode_kind(mode)
171
file_id = self._fileid_map.lookup_file_id(path)
172
ie = inventory.entry_factory[kind](file_id, name.decode("utf-8"), parent_id)
173
if kind == 'symlink':
174
ie.symlink_target = self.store[hexsha].data
175
elif kind == 'tree-reference':
176
ie.reference_revision = self.mapping.revision_id_foreign_to_bzr(hexsha)
178
data = self.store[hexsha].data
179
ie.text_sha1 = osutils.sha_string(data)
180
ie.text_size = len(data)
181
ie.executable = mode_is_executable(mode)
184
def _get_dir_ie(self, path, parent_id):
185
file_id = self._fileid_map.lookup_file_id(path)
186
return inventory.InventoryDirectory(file_id,
187
posixpath.basename(path).decode("utf-8"), parent_id)
189
def iter_entries_by_dir(self, specific_file_ids=None, yield_parents=False):
190
# FIXME: Support yield parents
191
if specific_file_ids is not None:
192
specific_paths = [self.id2path(file_id) for file_id in specific_file_ids]
193
if specific_paths in ([u""], []):
194
specific_paths = None
196
specific_paths = set(specific_paths)
198
specific_paths = None
199
todo = set([("", self.tree, None)])
201
path, tree_sha, parent_id = todo.pop()
202
ie = self._get_dir_ie(path, parent_id)
203
if specific_paths is None or path in specific_paths:
205
tree = self.store[tree_sha]
206
for name, mode, hexsha in tree.iteritems():
207
if self.mapping.is_special_file(name):
209
child_path = posixpath.join(path, name)
210
if stat.S_ISDIR(mode):
211
if (specific_paths is None or
212
any(filter(lambda p: p.startswith(child_path), specific_paths))):
213
todo.add((child_path, hexsha, ie.file_id))
214
elif specific_paths is None or child_path in specific_paths:
216
self._get_file_ie(child_path, name, mode, hexsha,
219
def get_revision_id(self):
220
"""See RevisionTree.get_revision_id."""
221
return self._revision_id
223
def get_file_sha1(self, file_id, path=None, stat_value=None):
224
return osutils.sha_string(self.get_file_text(file_id, path))
226
def get_file_verifier(self, file_id, path=None, stat_value=None):
228
path = self.id2path(file_id)
229
(mode, hexsha) = tree_lookup_path(self.store.__getitem__, self.tree,
231
return ("GIT", hexsha)
233
def get_file_text(self, file_id, path=None):
234
"""See RevisionTree.get_file_text."""
236
path = self.id2path(file_id)
237
(mode, hexsha) = tree_lookup_path(self.store.__getitem__, self.tree, path)
238
if stat.S_ISREG(mode):
239
return self.store[hexsha].data
243
def get_symlink_target(self, file_id, path=None):
244
"""See RevisionTree.get_symlink_target."""
246
path = self.id2path(file_id)
247
(mode, hexsha) = tree_lookup_path(self.store.__getitem__, self.tree, path)
248
if stat.S_ISLNK(mode):
249
return self.store[hexsha].data
253
def _comparison_data(self, entry, path):
255
return None, False, None
256
return entry.kind, entry.executable, None
258
def path_content_summary(self, path):
259
"""See Tree.path_content_summary."""
261
(mode, hexsha) = tree_lookup_path(self.store.__getitem__, self.tree, path)
263
return ('missing', None, None, None)
264
kind = mode_kind(mode)
266
executable = mode_is_executable(mode)
267
contents = self.store[hexsha].data
268
return (kind, len(contents), executable, osutils.sha_string(contents))
269
elif kind == 'symlink':
270
return (kind, None, None, self.store[hexsha].data)
272
return (kind, None, None, None)
275
def tree_delta_from_git_changes(changes, mapping,
276
(old_fileid_map, new_fileid_map), specific_file=None,
277
require_versioned=False):
278
"""Create a TreeDelta from two git trees.
280
source and target are iterators over tuples with:
281
(filename, sha, mode)
283
ret = delta.TreeDelta()
284
for (oldpath, newpath), (oldmode, newmode), (oldsha, newsha) in changes:
285
if mapping.is_control_file(oldpath):
287
if mapping.is_control_file(newpath):
289
if oldpath is None and newpath is None:
292
file_id = new_fileid_map.lookup_file_id(newpath)
293
ret.added.append((newpath.decode('utf-8'), file_id, mode_kind(newmode)))
294
elif newpath is None:
295
file_id = old_fileid_map.lookup_file_id(oldpath)
296
ret.removed.append((oldpath.decode('utf-8'), file_id, mode_kind(oldmode)))
297
elif oldpath != newpath:
298
file_id = old_fileid_map.lookup_file_id(oldpath)
299
ret.renamed.append((oldpath.decode('utf-8'), newpath.decode('utf-8'), file_id, mode_kind(newmode), (oldsha != newsha), (oldmode != newmode)))
300
elif mode_kind(oldmode) != mode_kind(newmode):
301
file_id = new_fileid_map.lookup_file_id(newpath)
302
ret.kind_changed.append((newpath.decode('utf-8'), file_id, mode_kind(oldmode), mode_kind(newmode)))
303
elif oldsha != newsha or oldmode != newmode:
304
file_id = new_fileid_map.lookup_file_id(newpath)
305
ret.modified.append((newpath.decode('utf-8'), file_id, mode_kind(newmode), (oldsha != newsha), (oldmode != newmode)))
307
file_id = new_fileid_map.lookup_file_id(newpath)
308
ret.unchanged.append((newpath.decode('utf-8'), file_id, mode_kind(newmode)))
312
def changes_from_git_changes(changes, mapping, specific_file=None,
313
require_versioned=False):
314
"""Create a iter_changes-like generator from a git stream.
316
source and target are iterators over tuples with:
317
(filename, sha, mode)
319
for (oldpath, newpath), (oldmode, newmode), (oldsha, newsha) in changes:
320
path = (oldpath, newpath)
321
if mapping.is_special_file(oldpath) or mapping.is_special_file(newpath):
324
fileid = mapping.generate_file_id(newpath)
330
oldpath = oldpath.decode("utf-8")
331
assert oldmode is not None
332
oldexe = mode_is_executable(oldmode)
333
oldkind = mode_kind(oldmode)
335
(oldparentpath, oldname) = oldpath.rsplit("/", 1)
340
oldparent = mapping.generate_file_id(oldparentpath)
341
fileid = mapping.generate_file_id(oldpath)
348
newpath = newpath.decode("utf-8")
349
assert newmode is not None
350
if newmode is not None:
351
newexe = mode_is_executable(newmode)
352
newkind = mode_kind(newmode)
357
newparentpath, newname = newpath.rsplit("/", 1)
362
newparent = mapping.generate_file_id(newparentpath)
363
yield (fileid, (oldpath, newpath), (oldsha != newsha),
364
(oldpath is not None, newpath is not None),
365
(oldparent, newparent), (oldname, newname),
366
(oldkind, newkind), (oldexe, newexe))
369
class InterGitRevisionTrees(tree.InterTree):
370
"""InterTree that works between two git revision trees."""
372
_matching_from_tree_format = None
373
_matching_to_tree_format = None
374
_test_mutable_trees_to_test_trees = None
377
def is_compatible(cls, source, target):
378
return (isinstance(source, GitRevisionTree) and
379
isinstance(target, GitRevisionTree))
381
def compare(self, want_unchanged=False, specific_files=None,
382
extra_trees=None, require_versioned=False, include_root=False,
383
want_unversioned=False):
384
if self.source._repository._git.object_store != self.target._repository._git.object_store:
386
changes = self.source._repository._git.object_store.tree_changes(
387
self.source.tree, self.target.tree, want_unchanged=want_unchanged)
388
source_fileid_map = self.source.mapping.get_fileid_map(
389
self.source._repository._git.object_store.__getitem__,
391
target_fileid_map = self.target.mapping.get_fileid_map(
392
self.target._repository._git.object_store.__getitem__,
394
return tree_delta_from_git_changes(changes, self.target.mapping,
395
(source_fileid_map, target_fileid_map),
396
specific_file=specific_files)
398
def iter_changes(self, include_unchanged=False, specific_files=None,
399
pb=None, extra_trees=[], require_versioned=True,
400
want_unversioned=False):
401
if self.source._repository._git.object_store != self.target._repository._git.object_store:
403
changes = self.source._repository._git.object_store.tree_changes(
404
self.source.tree, self.target.tree,
405
want_unchanged=include_unchanged)
406
return changes_from_git_changes(changes, self.target.mapping,
407
specific_file=specific_files)
410
tree.InterTree.register_optimiser(InterGitRevisionTrees)