~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/branch.py

  • Committer: Martin Pool
  • Date: 2005-05-30 06:45:30 UTC
  • Revision ID: mbp@sourcefrog.net-20050530064530-b740512e80908f7b
- Better Branch.relpath that doesn't match on 
  paths that have only a string prefix in common

Show diffs side-by-side

added added

removed removed

Lines of Context:
66
66
        finally:
67
67
            self.unlock()
68
68
    return d
 
69
 
 
70
 
 
71
def _relpath(base, path):
 
72
    """Return path relative to base, or raise exception.
 
73
 
 
74
    The path may be either an absolute path or a path relative to the
 
75
    current working directory.
 
76
 
 
77
    Lifted out of Branch.relpath for ease of testing.
 
78
 
 
79
    os.path.commonprefix (python2.4) has a bad bug that it works just
 
80
    on string prefixes, assuming that '/u' is a prefix of '/u2'.  This
 
81
    avoids that problem."""
 
82
    rp = os.path.abspath(path)
 
83
 
 
84
    s = []
 
85
    head = rp
 
86
    while len(head) >= len(base):
 
87
        if head == base:
 
88
            break
 
89
        head, tail = os.path.split(head)
 
90
        if tail:
 
91
            s.insert(0, tail)
 
92
    else:
 
93
        from errors import NotBranchError
 
94
        raise NotBranchError("path %r is not within branch %r" % (rp, base))
 
95
 
 
96
    return os.sep.join(s)
69
97
        
70
98
 
71
99
def find_branch_root(f=None):
215
243
        """Return path relative to this branch of something inside it.
216
244
 
217
245
        Raises an error if path is not in this branch."""
218
 
        rp = os.path.realpath(path)
219
 
        # FIXME: windows
220
 
        if not rp.startswith(self.base):
221
 
            from errors import NotBranchError
222
 
            raise NotBranchError("path %r is not within branch %r" % (rp, self.base))
223
 
        rp = rp[len(self.base):]
224
 
        rp = rp.lstrip(os.sep)
225
 
        return rp
 
246
        return _relpath(self.base, path)
226
247
 
227
248
 
228
249
    def controlfilename(self, file_or_path):