/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 bzrlib/osutils.py

  • Committer: Robert Collins
  • Date: 2005-10-16 22:04:54 UTC
  • mto: This revision was merged to the branch mainline in revision 1458.
  • Revision ID: robertc@lifelesslap.robertcollins.net-20051016220454-0418f1911d37b342
move branch._relpath into osutils as relpath

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
 
437
437
            return True
438
438
    else:
439
439
        return False
 
440
 
 
441
 
 
442
def relpath(base, path):
 
443
    """Return path relative to base, or raise exception.
 
444
 
 
445
    The path may be either an absolute path or a path relative to the
 
446
    current working directory.
 
447
 
 
448
    os.path.commonprefix (python2.4) has a bad bug that it works just
 
449
    on string prefixes, assuming that '/u' is a prefix of '/u2'.  This
 
450
    avoids that problem."""
 
451
    rp = os.path.abspath(path)
 
452
 
 
453
    s = []
 
454
    head = rp
 
455
    while len(head) >= len(base):
 
456
        if head == base:
 
457
            break
 
458
        head, tail = os.path.split(head)
 
459
        if tail:
 
460
            s.insert(0, tail)
 
461
    else:
 
462
        # XXX This should raise a NotChildPath exception, as its not tied
 
463
        # to branch anymore.
 
464
        raise NotBranchError("path %r is not within branch %r" % (rp, base))
 
465
 
 
466
    return os.sep.join(s)