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
return self.fileid_map.lookup_file_id(path.encode('utf-8'))
77
def get_root_id(self):
78
return self.path2id("")
80
def has_or_had_id(self, file_id):
81
return self.has_id(file_id)
83
def has_id(self, file_id):
85
path = self.id2path(file_id)
86
except errors.NoSuchId:
88
return self.has_filename(path)
90
def kind(self, file_id, path=None):
92
path = self.id2path(file_id)
94
(mode, hexsha) = tree_lookup_path(self.store.__getitem__, self.tree,
97
raise errors.NoSuchId(self, file_id)
99
# the tree root is a directory
101
return mode_kind(mode)
103
def has_filename(self, path):
105
tree_lookup_path(self.store.__getitem__, self.tree,
106
path.encode("utf-8"))
112
def list_files(self, include_root=False, from_dir=None, recursive=True):
115
(mode, hexsha) = tree_lookup_path(self.store.__getitem__, self.tree,
116
from_dir.encode("utf-8"))
117
if mode is None: # Root
118
root_ie = self._get_dir_ie("", None)
120
parent_path = posixpath.dirname(from_dir.encode("utf-8"))
121
parent_id = self.fileid_map.lookup_file_id(parent_path)
122
if mode_kind(mode) == 'directory':
123
root_ie = self._get_dir_ie(from_dir.encode("utf-8"), parent_id)
125
root_ie = self._get_file_ie(from_dir.encode("utf-8"),
126
posixpath.basename(from_dir), mode, hexsha)
127
if from_dir != "" or include_root:
128
yield (from_dir, "V", root_ie.kind, root_ie.file_id, root_ie)
130
if root_ie.kind == 'directory':
131
todo.add((from_dir.encode("utf-8"), hexsha, root_ie.file_id))
133
(path, hexsha, parent_id) = todo.pop()
134
tree = self.store[hexsha]
135
for name, mode, hexsha in tree.iteritems():
136
child_path = posixpath.join(path, name)
137
if stat.S_ISDIR(mode):
138
ie = self._get_dir_ie(child_path, parent_id)
140
todo.add((child_path, hexsha, ie.file_id))
142
ie = self._get_file_ie(child_path, name, mode, hexsha, parent_id)
143
yield child_path, "V", ie.kind, ie.file_id, ie
145
def _get_file_ie(self, path, name, mode, hexsha, parent_id):
146
kind = mode_kind(mode)
147
file_id = self.fileid_map.lookup_file_id(path)
148
ie = inventory.entry_factory[kind](file_id, name.decode("utf-8"), parent_id)
149
if kind == 'symlink':
150
ie.symlink_target = self.store[hexsha].data
152
data = self.store[hexsha].data
153
ie.text_sha1 = osutils.sha_string(data)
154
ie.text_size = len(data)
155
ie.executable = mode_is_executable(mode)
158
def _get_dir_ie(self, path, parent_id):
159
file_id = self.fileid_map.lookup_file_id(path)
160
return inventory.InventoryDirectory(file_id,
161
posixpath.basename(path).decode("utf-8"), parent_id)
163
def iter_entries_by_dir(self, specific_file_ids=None, yield_parents=False):
164
# FIXME: Support yield parents
165
if specific_file_ids is not None:
166
specific_paths = [self.id2path(file_id) for file_id in specific_file_ids]
167
if specific_paths in ([u""], []):
168
specific_paths = None
170
specific_paths = set(specific_paths)
172
specific_paths = None
173
todo = set([("", self.tree, None)])
175
path, tree_sha, parent_id = todo.pop()
176
ie = self._get_dir_ie(path, parent_id)
177
if specific_paths is None or path in specific_paths:
179
tree = self.store[tree_sha]
180
for name, mode, hexsha in tree.iteritems():
181
child_path = posixpath.join(path, name)
182
if stat.S_ISDIR(mode):
183
if (specific_paths is None or
184
any(filter(lambda p: p.startswith(child_path), specific_paths))):
185
todo.add((child_path, hexsha, ie.file_id))
186
elif specific_paths is None or child_path in specific_paths:
188
self._get_file_ie(child_path, name, mode, hexsha,
54
191
def get_revision_id(self):
55
192
"""See RevisionTree.get_revision_id."""
56
193
return self._revision_id
195
def get_file_sha1(self, file_id, path=None):
196
return osutils.sha_string(self.get_file_text(file_id, path))
198
def get_file_verifier(self, file_id, path=None, stat_value=None):
200
path = self.id2path(file_id)
201
(mode, hexsha) = tree_lookup_path(self.store.__getitem__, self.tree,
203
return ("GIT", hexsha)
58
205
def get_file_text(self, file_id, path=None):
59
206
"""See RevisionTree.get_file_text."""
61
entry = self._inventory._get_ie(path)
208
path = self.id2path(file_id)
209
(mode, hexsha) = tree_lookup_path(self.store.__getitem__, self.tree, path)
210
if stat.S_ISREG(mode):
211
return self.store[hexsha].data
63
entry = self._inventory[file_id]
64
if entry.kind in ('directory', 'tree-reference'):
66
return entry.object.data
215
def _comparison_data(self, entry, path):
217
return None, False, None
218
return entry.kind, entry.executable, None
69
221
def tree_delta_from_git_changes(changes, mapping,