39
42
def __init__(self, repository, revision_id):
40
43
self._revision_id = revision_id
41
44
self._repository = repository
42
store = repository._git.object_store
45
self.store = repository._git.object_store
43
46
assert isinstance(revision_id, str)
44
git_id, self.mapping = repository.lookup_bzr_revision_id(revision_id)
47
self.commit_id, self.mapping = repository.lookup_bzr_revision_id(revision_id)
46
commit = store[git_id]
49
commit = self.store[self.commit_id]
47
50
except KeyError, r:
48
51
raise errors.NoSuchRevision(repository, revision_id)
49
52
self.tree = commit.tree
50
fileid_map = self.mapping.get_fileid_map(store.__getitem__, self.tree)
51
self._inventory = GitInventory(self.tree, self.mapping, fileid_map,
53
self._fileid_map = self.mapping.get_fileid_map(self.store.__getitem__, self.tree)
55
def get_file_revision(self, file_id, path=None):
57
path = self.id2path(file_id)
58
change_scanner = self._repository._file_change_scanner
59
(path, commit_id) = change_scanner.find_last_change_revision(path,
61
return self._repository.lookup_foreign_revision_id(commit_id, self.mapping)
63
def get_file_mtime(self, file_id, path=None):
64
revid = self.get_file_revision(file_id, path)
66
rev = self._repository.get_revision(revid)
67
except errors.NoSuchRevision:
68
raise errors.FileTimestampUnavailable(path)
71
def id2path(self, file_id):
72
return self._fileid_map.lookup_path(file_id)
74
def path2id(self, path):
75
if self.mapping.is_special_file(path):
77
return self._fileid_map.lookup_file_id(path.encode('utf-8'))
79
def get_root_id(self):
80
return self.path2id("")
82
def has_or_had_id(self, file_id):
83
return self.has_id(file_id)
85
def has_id(self, file_id):
87
path = self.id2path(file_id)
88
except errors.NoSuchId:
90
return self.has_filename(path)
92
def kind(self, file_id, path=None):
94
path = self.id2path(file_id)
96
(mode, hexsha) = tree_lookup_path(self.store.__getitem__, self.tree,
99
raise errors.NoSuchId(self, file_id)
101
# the tree root is a directory
103
return mode_kind(mode)
105
def has_filename(self, path):
107
tree_lookup_path(self.store.__getitem__, self.tree,
108
path.encode("utf-8"))
114
def list_files(self, include_root=False, from_dir=None, recursive=True):
117
(mode, hexsha) = tree_lookup_path(self.store.__getitem__, self.tree,
118
from_dir.encode("utf-8"))
119
if mode is None: # Root
120
root_ie = self._get_dir_ie("", None)
122
parent_path = posixpath.dirname(from_dir.encode("utf-8"))
123
parent_id = self._fileid_map.lookup_file_id(parent_path)
124
if mode_kind(mode) == 'directory':
125
root_ie = self._get_dir_ie(from_dir.encode("utf-8"), parent_id)
127
root_ie = self._get_file_ie(from_dir.encode("utf-8"),
128
posixpath.basename(from_dir), mode, hexsha)
129
if from_dir != "" or include_root:
130
yield (from_dir, "V", root_ie.kind, root_ie.file_id, root_ie)
132
if root_ie.kind == 'directory':
133
todo.add((from_dir.encode("utf-8"), hexsha, root_ie.file_id))
135
(path, hexsha, parent_id) = todo.pop()
136
tree = self.store[hexsha]
137
for name, mode, hexsha in tree.iteritems():
138
if self.mapping.is_special_file(name):
140
child_path = posixpath.join(path, name)
141
if stat.S_ISDIR(mode):
142
ie = self._get_dir_ie(child_path, parent_id)
144
todo.add((child_path, hexsha, ie.file_id))
146
ie = self._get_file_ie(child_path, name, mode, hexsha, parent_id)
147
yield child_path, "V", ie.kind, ie.file_id, ie
149
def _get_file_ie(self, path, name, mode, hexsha, parent_id):
150
kind = mode_kind(mode)
151
file_id = self._fileid_map.lookup_file_id(path)
152
ie = inventory.entry_factory[kind](file_id, name.decode("utf-8"), parent_id)
153
if kind == 'symlink':
154
ie.symlink_target = self.store[hexsha].data
155
elif kind == 'tree-reference':
156
ie.reference_revision = self.mapping.revision_id_foreign_to_bzr(hexsha)
158
data = self.store[hexsha].data
159
ie.text_sha1 = osutils.sha_string(data)
160
ie.text_size = len(data)
161
ie.executable = mode_is_executable(mode)
164
def _get_dir_ie(self, path, parent_id):
165
file_id = self._fileid_map.lookup_file_id(path)
166
return inventory.InventoryDirectory(file_id,
167
posixpath.basename(path).decode("utf-8"), parent_id)
169
def iter_entries_by_dir(self, specific_file_ids=None, yield_parents=False):
170
# FIXME: Support yield parents
171
if specific_file_ids is not None:
172
specific_paths = [self.id2path(file_id) for file_id in specific_file_ids]
173
if specific_paths in ([u""], []):
174
specific_paths = None
176
specific_paths = set(specific_paths)
178
specific_paths = None
179
todo = set([("", self.tree, None)])
181
path, tree_sha, parent_id = todo.pop()
182
ie = self._get_dir_ie(path, parent_id)
183
if specific_paths is None or path in specific_paths:
185
tree = self.store[tree_sha]
186
for name, mode, hexsha in tree.iteritems():
187
if self.mapping.is_special_file(name):
189
child_path = posixpath.join(path, name)
190
if stat.S_ISDIR(mode):
191
if (specific_paths is None or
192
any(filter(lambda p: p.startswith(child_path), specific_paths))):
193
todo.add((child_path, hexsha, ie.file_id))
194
elif specific_paths is None or child_path in specific_paths:
196
self._get_file_ie(child_path, name, mode, hexsha,
54
199
def get_revision_id(self):
55
200
"""See RevisionTree.get_revision_id."""
56
201
return self._revision_id
203
def get_file_sha1(self, file_id, path=None, stat_value=None):
204
return osutils.sha_string(self.get_file_text(file_id, path))
206
def get_file_verifier(self, file_id, path=None, stat_value=None):
208
path = self.id2path(file_id)
209
(mode, hexsha) = tree_lookup_path(self.store.__getitem__, self.tree,
211
return ("GIT", hexsha)
58
213
def get_file_text(self, file_id, path=None):
59
214
"""See RevisionTree.get_file_text."""
61
entry = self._inventory._get_ie(path)
216
path = self.id2path(file_id)
217
(mode, hexsha) = tree_lookup_path(self.store.__getitem__, self.tree, path)
218
if stat.S_ISREG(mode):
219
return self.store[hexsha].data
63
entry = self._inventory[file_id]
64
if entry.kind in ('directory', 'tree-reference'):
66
return entry.object.data
223
def get_symlink_target(self, file_id, path=None):
224
"""See RevisionTree.get_symlink_target."""
226
path = self.id2path(file_id)
227
(mode, hexsha) = tree_lookup_path(self.store.__getitem__, self.tree, path)
228
if stat.S_ISLNK(mode):
229
return self.store[hexsha].data
233
def _comparison_data(self, entry, path):
235
return None, False, None
236
return entry.kind, entry.executable, None
69
239
def tree_delta_from_git_changes(changes, mapping,
83
253
if oldpath is None and newpath is None:
85
255
if oldpath is None:
86
ret.added.append((newpath, new_fileid_map.lookup_file_id(newpath.encode("utf-8")), mode_kind(newmode)))
256
file_id = new_fileid_map.lookup_file_id(newpath.encode("utf-8"))
257
ret.added.append((newpath, file_id, mode_kind(newmode)))
87
258
elif newpath is None:
88
ret.removed.append((oldpath, old_fileid_map.lookup_file_id(oldpath.encode("utf-8")), mode_kind(oldmode)))
259
file_id = old_fileid_map.lookup_file_id(oldpath.encode("utf-8"))
260
ret.removed.append((oldpath, file_id, mode_kind(oldmode)))
89
261
elif oldpath != newpath:
90
ret.renamed.append((oldpath, newpath, old_fileid_map.lookup_file_id(oldpath.encode("utf-8")), mode_kind(newmode), (oldsha != newsha), (oldmode != newmode)))
262
file_id = old_fileid_map.lookup_file_id(oldpath.encode("utf-8"))
263
ret.renamed.append((oldpath, newpath, file_id, mode_kind(newmode), (oldsha != newsha), (oldmode != newmode)))
91
264
elif mode_kind(oldmode) != mode_kind(newmode):
92
ret.kind_changed.append((newpath, new_fileid_map.lookup_file_id(newpath.encode("utf-8")), mode_kind(oldmode), mode_kind(newmode)))
265
file_id = new_fileid_map.lookup_file_id(newpath.encode("utf-8"))
266
ret.kind_changed.append((newpath, file_id, mode_kind(oldmode), mode_kind(newmode)))
93
267
elif oldsha != newsha or oldmode != newmode:
94
ret.modified.append((newpath, new_fileid_map.lookup_file_id(newpath.encode("utf-8")), mode_kind(newmode), (oldsha != newsha), (oldmode != newmode)))
268
file_id = new_fileid_map.lookup_file_id(newpath.encode("utf-8"))
269
ret.modified.append((newpath, file_id, mode_kind(newmode), (oldsha != newsha), (oldmode != newmode)))
96
ret.unchanged.append((newpath, new_fileid_map.lookup_file_id(newpath.encode("utf-8")), mode_kind(newmode)))
271
file_id = new_fileid_map.lookup_file_id(newpath.encode("utf-8"))
272
ret.unchanged.append((newpath, file_id, mode_kind(newmode)))