~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/osutils.py

  • Committer: Aaron Bentley
  • Date: 2005-10-17 20:17:55 UTC
  • mfrom: (1185.16.59)
  • mto: (1185.25.1)
  • mto: This revision was merged to the branch mainline in revision 1474.
  • Revision ID: abentley@panoramicfeedback.com-20051017201755-48ed4650792388ab
Merged latest from Martin

Show diffs side-by-side

added added

removed removed

Lines of Context:
30
30
import types
31
31
 
32
32
import bzrlib
33
 
from bzrlib.errors import BzrError
 
33
from bzrlib.errors import BzrError, NotBranchError
34
34
from bzrlib.trace import mutter
35
35
 
36
36
 
442
442
            return True
443
443
    else:
444
444
        return False
 
445
 
 
446
 
 
447
def relpath(base, path):
 
448
    """Return path relative to base, or raise exception.
 
449
 
 
450
    The path may be either an absolute path or a path relative to the
 
451
    current working directory.
 
452
 
 
453
    os.path.commonprefix (python2.4) has a bad bug that it works just
 
454
    on string prefixes, assuming that '/u' is a prefix of '/u2'.  This
 
455
    avoids that problem."""
 
456
    rp = os.path.abspath(path)
 
457
 
 
458
    s = []
 
459
    head = rp
 
460
    while len(head) >= len(base):
 
461
        if head == base:
 
462
            break
 
463
        head, tail = os.path.split(head)
 
464
        if tail:
 
465
            s.insert(0, tail)
 
466
    else:
 
467
        # XXX This should raise a NotChildPath exception, as its not tied
 
468
        # to branch anymore.
 
469
        raise NotBranchError("path %r is not within branch %r" % (rp, base))
 
470
 
 
471
    return os.sep.join(s)