~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/urlutils.py

  • Committer: John Arbash Meinel
  • Date: 2008-07-16 18:14:23 UTC
  • mfrom: (3542 +trunk)
  • mto: This revision was merged to the branch mainline in revision 3543.
  • Revision ID: john@arbash-meinel.com-20080716181423-9xbj5va4eakfjlqf
Merge bzr.dev 3542

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
lazy_import(globals(), """
27
27
from posixpath import split as _posix_split, normpath as _posix_normpath
28
28
import urllib
 
29
import urlparse
29
30
 
30
31
from bzrlib import (
31
32
    errors,
636
637
            return from_location[sep+1:]
637
638
        else:
638
639
            return from_location
 
640
 
 
641
 
 
642
def _is_absolute(url):
 
643
    return (osutils.pathjoin('/foo', url) == url)
 
644
 
 
645
 
 
646
def rebase_url(url, old_base, new_base):
 
647
    """Convert a relative path from an old base URL to a new base URL.
 
648
 
 
649
    The result will be a relative path.
 
650
    Absolute paths and full URLs are returned unaltered.
 
651
    """
 
652
    scheme, separator = _find_scheme_and_separator(url)
 
653
    if scheme is not None:
 
654
        return url
 
655
    if _is_absolute(url):
 
656
        return url
 
657
    old_parsed = urlparse.urlparse(old_base)
 
658
    new_parsed = urlparse.urlparse(new_base)
 
659
    if (old_parsed[:2]) != (new_parsed[:2]):
 
660
        raise errors.InvalidRebaseURLs(old_base, new_base)
 
661
    return determine_relative_path(new_parsed[2],
 
662
                                   osutils.pathjoin(old_parsed[2], url))
 
663
 
 
664
 
 
665
def determine_relative_path(from_path, to_path):
 
666
    """Determine a relative path from from_path to to_path."""
 
667
    from_segments = osutils.splitpath(from_path)
 
668
    to_segments = osutils.splitpath(to_path)
 
669
    count = -1
 
670
    for count, (from_element, to_element) in enumerate(zip(from_segments,
 
671
                                                       to_segments)):
 
672
        if from_element != to_element:
 
673
            break
 
674
    else:
 
675
        count += 1
 
676
    unique_from = from_segments[count:]
 
677
    unique_to = to_segments[count:]
 
678
    segments = (['..'] * len(unique_from) + unique_to)
 
679
    if len(segments) == 0:
 
680
        return '.'
 
681
    return osutils.pathjoin(*segments)