/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to tree.py

Cope with tuples in refs dictionary.

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 
18
18
"""Git Trees."""
19
19
 
20
 
from dulwich.object_store import tree_lookup_path
21
 
import stat
22
 
import posixpath
23
 
 
24
20
from bzrlib import (
25
21
    delta,
26
22
    errors,
27
 
    inventory,
28
 
    osutils,
29
23
    revisiontree,
30
24
    tree,
31
25
    )
32
26
 
 
27
from bzrlib.plugins.git.inventory import (
 
28
    GitInventory,
 
29
    )
33
30
from bzrlib.plugins.git.mapping import (
34
31
    mode_is_executable,
35
32
    mode_kind,
42
39
    def __init__(self, repository, revision_id):
43
40
        self._revision_id = revision_id
44
41
        self._repository = repository
45
 
        self.store = repository._git.object_store
 
42
        store = repository._git.object_store
46
43
        assert isinstance(revision_id, str)
47
 
        self.commit_id, self.mapping = repository.lookup_bzr_revision_id(revision_id)
 
44
        git_id, self.mapping = repository.lookup_bzr_revision_id(revision_id)
48
45
        try:
49
 
            commit = self.store[self.commit_id]
 
46
            commit = store[git_id]
50
47
        except KeyError, r:
51
48
            raise errors.NoSuchRevision(repository, revision_id)
52
49
        self.tree = commit.tree
53
 
        self._fileid_map = self.mapping.get_fileid_map(self.store.__getitem__, self.tree)
54
 
 
55
 
    def get_file_revision(self, file_id, path=None):
56
 
        if path is 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,
60
 
            self.commit_id)
61
 
        return self._repository.lookup_foreign_revision_id(commit_id, self.mapping)
62
 
 
63
 
    def get_file_mtime(self, file_id, path=None):
64
 
        revid = self.get_file_revision(file_id, path)
65
 
        try:
66
 
            rev = self._repository.get_revision(revid)
67
 
        except errors.NoSuchRevision:
68
 
            raise errors.FileTimestampUnavailable(path)
69
 
        return rev.timestamp
70
 
 
71
 
    def id2path(self, file_id):
72
 
        return self._fileid_map.lookup_path(file_id)
73
 
 
74
 
    def path2id(self, path):
75
 
        if self.mapping.is_special_file(path):
76
 
            return None
77
 
        return self._fileid_map.lookup_file_id(path.encode('utf-8'))
78
 
 
79
 
    def get_root_id(self):
80
 
        return self.path2id("")
81
 
 
82
 
    def has_or_had_id(self, file_id):
83
 
        return self.has_id(file_id)
84
 
 
85
 
    def has_id(self, file_id):
86
 
        try:
87
 
            path = self.id2path(file_id)
88
 
        except errors.NoSuchId:
89
 
            return False
90
 
        return self.has_filename(path)
91
 
 
92
 
    def kind(self, file_id, path=None):
93
 
        if path is None:
94
 
            path = self.id2path(file_id)
95
 
        try:
96
 
            (mode, hexsha) = tree_lookup_path(self.store.__getitem__, self.tree,
97
 
                path)
98
 
        except KeyError:
99
 
            raise errors.NoSuchId(self, file_id)
100
 
        if mode is None:
101
 
            # the tree root is a directory
102
 
            return "directory"
103
 
        return mode_kind(mode)
104
 
 
105
 
    def has_filename(self, path):
106
 
        try:
107
 
            tree_lookup_path(self.store.__getitem__, self.tree,
108
 
                path.encode("utf-8"))
109
 
        except KeyError:
110
 
            return False
111
 
        else:
112
 
            return True
113
 
 
114
 
    def list_files(self, include_root=False, from_dir=None, recursive=True):
115
 
        if from_dir is None:
116
 
            from_dir = u""
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)
121
 
        else:
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)
126
 
            else:
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)
131
 
        todo = set()
132
 
        if root_ie.kind == 'directory':
133
 
            todo.add((from_dir.encode("utf-8"), hexsha, root_ie.file_id))
134
 
        while todo:
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):
139
 
                    continue
140
 
                child_path = posixpath.join(path, name)
141
 
                if stat.S_ISDIR(mode):
142
 
                    ie = self._get_dir_ie(child_path, parent_id)
143
 
                    if recursive:
144
 
                        todo.add((child_path, hexsha, ie.file_id))
145
 
                else:
146
 
                    ie = self._get_file_ie(child_path, name, mode, hexsha, parent_id)
147
 
                yield child_path, "V", ie.kind, ie.file_id, ie
148
 
 
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
 
        else:
156
 
            data = self.store[hexsha].data
157
 
            ie.text_sha1 = osutils.sha_string(data)
158
 
            ie.text_size = len(data)
159
 
            ie.executable = mode_is_executable(mode)
160
 
        return ie
161
 
 
162
 
    def _get_dir_ie(self, path, parent_id):
163
 
        file_id = self._fileid_map.lookup_file_id(path)
164
 
        return inventory.InventoryDirectory(file_id,
165
 
            posixpath.basename(path).decode("utf-8"), parent_id)
166
 
 
167
 
    def iter_entries_by_dir(self, specific_file_ids=None, yield_parents=False):
168
 
        # FIXME: Support yield parents
169
 
        if specific_file_ids is not None:
170
 
            specific_paths = [self.id2path(file_id) for file_id in specific_file_ids]
171
 
            if specific_paths in ([u""], []):
172
 
                specific_paths = None
173
 
            else:
174
 
                specific_paths = set(specific_paths)
175
 
        else:
176
 
            specific_paths = None
177
 
        todo = set([("", self.tree, None)])
178
 
        while todo:
179
 
            path, tree_sha, parent_id = todo.pop()
180
 
            ie = self._get_dir_ie(path, parent_id)
181
 
            if specific_paths is None or path in specific_paths:
182
 
                yield path, ie
183
 
            tree = self.store[tree_sha]
184
 
            for name, mode, hexsha  in tree.iteritems():
185
 
                if self.mapping.is_special_file(name):
186
 
                    continue
187
 
                child_path = posixpath.join(path, name)
188
 
                if stat.S_ISDIR(mode):
189
 
                    if (specific_paths is None or
190
 
                        any(filter(lambda p: p.startswith(child_path), specific_paths))):
191
 
                        todo.add((child_path, hexsha, ie.file_id))
192
 
                elif specific_paths is None or child_path in specific_paths:
193
 
                    yield (child_path,
194
 
                            self._get_file_ie(child_path, name, mode, hexsha,
195
 
                           ie.file_id))
 
50
        fileid_map = self.mapping.get_fileid_map(store.__getitem__, self.tree)
 
51
        self._inventory = GitInventory(self.tree, self.mapping, fileid_map,
 
52
            store, revision_id)
196
53
 
197
54
    def get_revision_id(self):
198
55
        """See RevisionTree.get_revision_id."""
199
56
        return self._revision_id
200
57
 
201
 
    def get_file_sha1(self, file_id, path=None, stat_value=None):
202
 
        return osutils.sha_string(self.get_file_text(file_id, path))
203
 
 
204
 
    def get_file_verifier(self, file_id, path=None, stat_value=None):
205
 
        if path is None:
206
 
            path = self.id2path(file_id)
207
 
        (mode, hexsha) = tree_lookup_path(self.store.__getitem__, self.tree,
208
 
            path)
209
 
        return ("GIT", hexsha)
210
 
 
211
58
    def get_file_text(self, file_id, path=None):
212
59
        """See RevisionTree.get_file_text."""
213
 
        if path is None:
214
 
            path = self.id2path(file_id)
215
 
        (mode, hexsha) = tree_lookup_path(self.store.__getitem__, self.tree, path)
216
 
        if stat.S_ISREG(mode):
217
 
            return self.store[hexsha].data
 
60
        if path is not None:
 
61
            entry = self._inventory._get_ie(path)
218
62
        else:
 
63
            entry = self._inventory[file_id]
 
64
        if entry.kind in ('directory', 'tree-reference'):
219
65
            return ""
220
 
 
221
 
    def _comparison_data(self, entry, path):
222
 
        if entry is None:
223
 
            return None, False, None
224
 
        return entry.kind, entry.executable, None
 
66
        return entry.object.data
225
67
 
226
68
 
227
69
def tree_delta_from_git_changes(changes, mapping,
264
106
    """
265
107
    for (oldpath, newpath), (oldmode, newmode), (oldsha, newsha) in changes:
266
108
        path = (oldpath, newpath)
267
 
        if mapping.is_special_file(oldpath) or mapping.is_special_file(newpath):
268
 
            continue
269
109
        if oldpath is None:
270
110
            fileid = mapping.generate_file_id(newpath)
271
111
            oldexe = None