~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/inventory.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2009-10-15 23:26:39 UTC
  • mfrom: (4733.1.2 2.1-chk-inv-path2id)
  • Revision ID: pqm@pqm.ubuntu.com-20091015232639-sgyy127lljaca1ty
(jam) Small tweaks to Inventory.path2id

Show diffs side-by-side

added added

removed removed

Lines of Context:
958
958
        descend(self.root, u'')
959
959
        return accum
960
960
 
961
 
    def path2id(self, name):
 
961
    def path2id(self, relpath):
962
962
        """Walk down through directories to return entry of last component.
963
963
 
964
 
        names may be either a list of path components, or a single
965
 
        string, in which case it is automatically split.
 
964
        :param relpath: may be either a list of path components, or a single
 
965
            string, in which case it is automatically split.
966
966
 
967
967
        This returns the entry of the last component in the path,
968
968
        which may be either a file or a directory.
969
969
 
970
970
        Returns None IFF the path is not found.
971
971
        """
972
 
        if isinstance(name, basestring):
973
 
            name = osutils.splitpath(name)
974
 
 
975
 
        # mutter("lookup path %r" % name)
 
972
        if isinstance(relpath, basestring):
 
973
            names = osutils.splitpath(relpath)
 
974
        else:
 
975
            names = relpath
976
976
 
977
977
        try:
978
978
            parent = self.root
981
981
            return None
982
982
        if parent is None:
983
983
            return None
984
 
        for f in name:
 
984
        for f in names:
985
985
            try:
986
986
                children = getattr(parent, 'children', None)
987
987
                if children is None:
2170
2170
            delta.append((old_path, new_path, file_id, entry))
2171
2171
        return delta
2172
2172
 
2173
 
    def path2id(self, name):
 
2173
    def path2id(self, relpath):
2174
2174
        """See CommonInventory.path2id()."""
2175
2175
        # TODO: perhaps support negative hits?
2176
 
        result = self._path_to_fileid_cache.get(name, None)
 
2176
        result = self._path_to_fileid_cache.get(relpath, None)
2177
2177
        if result is not None:
2178
2178
            return result
2179
 
        if isinstance(name, basestring):
2180
 
            names = osutils.splitpath(name)
 
2179
        if isinstance(relpath, basestring):
 
2180
            names = osutils.splitpath(relpath)
2181
2181
        else:
2182
 
            names = name
 
2182
            names = relpath
2183
2183
        current_id = self.root_id
2184
2184
        if current_id is None:
2185
2185
            return None
2186
2186
        parent_id_index = self.parent_id_basename_to_file_id
 
2187
        cur_path = None
2187
2188
        for basename in names:
2188
 
            # TODO: Cache each path we figure out in this function.
 
2189
            if cur_path is None:
 
2190
                cur_path = basename
 
2191
            else:
 
2192
                cur_path = cur_path + '/' + basename
2189
2193
            basename_utf8 = basename.encode('utf8')
2190
 
            key_filter = [(current_id, basename_utf8)]
2191
 
            file_id = None
2192
 
            for (parent_id, name_utf8), file_id in parent_id_index.iteritems(
2193
 
                key_filter=key_filter):
2194
 
                if parent_id != current_id or name_utf8 != basename_utf8:
2195
 
                    raise errors.BzrError("corrupt inventory lookup! "
2196
 
                        "%r %r %r %r" % (parent_id, current_id, name_utf8,
2197
 
                        basename_utf8))
 
2194
            file_id = self._path_to_fileid_cache.get(cur_path, None)
2198
2195
            if file_id is None:
2199
 
                return None
 
2196
                key_filter = [(current_id, basename_utf8)]
 
2197
                items = parent_id_index.iteritems(key_filter)
 
2198
                for (parent_id, name_utf8), file_id in items:
 
2199
                    if parent_id != current_id or name_utf8 != basename_utf8:
 
2200
                        raise errors.BzrError("corrupt inventory lookup! "
 
2201
                            "%r %r %r %r" % (parent_id, current_id, name_utf8,
 
2202
                            basename_utf8))
 
2203
                if file_id is None:
 
2204
                    return None
 
2205
                else:
 
2206
                    self._path_to_fileid_cache[cur_path] = file_id
2200
2207
            current_id = file_id
2201
 
        self._path_to_fileid_cache[name] = current_id
2202
2208
        return current_id
2203
2209
 
2204
2210
    def to_lines(self):