~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tree.py

  • Committer: Mark Hammond
  • Date: 2008-11-17 06:27:20 UTC
  • mto: (3932.3.1 cicp-1.11)
  • mto: This revision was merged to the branch mainline in revision 3937.
  • Revision ID: mhammond@skippinet.com.au-20081117062720-2xbtytksggumj1sd
 Add get_canonical_path method to the Tree class, plus tests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
321
321
        """
322
322
        raise NotImplementedError(self.get_symlink_target)
323
323
 
 
324
    def get_canonical_path(self, path):
 
325
        """Returns the first entry that case-insensitively matches the input.
 
326
 
 
327
        If the path matches exactly, it is returned. If no path matches exactly
 
328
        but more than one path matches case-insensitively, it is implementation
 
329
        defined which is returned.
 
330
 
 
331
        If no path matches case-insensitively, the input path is returned, but
 
332
        with as many entries that do exist changed to their canonical form.
 
333
 
 
334
        :param path: A path, relative to the root of the tree.
 
335
        :return: The input path adjusted to account for existing elements that
 
336
        match case insensitively.
 
337
        """
 
338
        # First, if the path as specified exists exactly, just use it.
 
339
        if self.path2id(path) is not None:
 
340
            return path
 
341
        # go walkin...
 
342
        cur_id = self.get_root_id()
 
343
        cur_path = ''
 
344
        bit_iter = iter(path.split("/"))
 
345
        for elt in bit_iter:
 
346
            lelt = elt.lower()
 
347
            for child in self.iter_children(cur_id):
 
348
                child_base = os.path.basename(self.id2path(child))
 
349
                if child_base.lower() == lelt:
 
350
                    cur_id = child
 
351
                    cur_path = osutils.pathjoin(cur_path, child_base)
 
352
                    break
 
353
            else:
 
354
                # got to the end of this directory and no entries matched.
 
355
                # Return what matched so far, plus the rest as specified.
 
356
                cur_path = osutils.pathjoin(cur_path, elt, *list(bit_iter))
 
357
                break
 
358
        return cur_path
 
359
 
324
360
    def get_root_id(self):
325
361
        """Return the file_id for the root of this tree."""
326
362
        raise NotImplementedError(self.get_root_id)