636
637
return from_location[sep+1:]
638
639
return from_location
642
def _is_absolute(url):
643
return (osutils.pathjoin('/foo', url) == url)
646
def rebase_url(url, old_base, new_base):
647
"""Convert a relative path from an old base URL to a new base URL.
649
The result will be a relative path.
650
Absolute paths and full URLs are returned unaltered.
652
scheme, separator = _find_scheme_and_separator(url)
653
if scheme is not None:
655
if _is_absolute(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))
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)
670
for count, (from_element, to_element) in enumerate(zip(from_segments,
672
if from_element != to_element:
676
unique_from = from_segments[count:]
677
unique_to = to_segments[count:]
678
segments = (['..'] * len(unique_from) + unique_to)
679
if len(segments) == 0:
681
return osutils.pathjoin(*segments)