~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/urlutils.py

  • Committer: Aaron Bentley
  • Date: 2008-06-17 21:44:22 UTC
  • mto: This revision was merged to the branch mainline in revision 3538.
  • Revision ID: aaron@aaronbentley.com-20080617214422-odiao8cz30swjqtk
Implement rebase_url

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
def _is_absolute(url):
 
642
    return (osutils.pathjoin('/foo', url) == url)
 
643
 
 
644
def rebase_url(url, old_base, new_base):
 
645
    """Convert a relative path from an old base URL to a new base URL.
 
646
 
 
647
    The result will be a relative path.
 
648
    Absolute paths and full URLs are returned unaltered.
 
649
    """
 
650
    scheme, separator = _find_scheme_and_separator(url)
 
651
    if scheme is not None:
 
652
        return url
 
653
    if _is_absolute(url):
 
654
        return url
 
655
    old_parsed = urlparse.urlparse(old_base)
 
656
    new_parsed = urlparse.urlparse(new_base)
 
657
    if (old_parsed[:2]) != (new_parsed[:2]):
 
658
        raise ValueError('URLs cannot have relative paths: %s %s' %
 
659
                         (old_base, new_base))
 
660
    return determine_relative_path(new_parsed.path,
 
661
                                   osutils.pathjoin(old_parsed.path, url))
 
662
 
 
663
 
 
664
def determine_relative_path(from_path, to_path):
 
665
    """Determine a relative path from from_path to to_path."""
 
666
    from_segments = osutils.splitpath(from_path)
 
667
    to_segments = osutils.splitpath(to_path)
 
668
    count = -1
 
669
    for count, (from_element, to_element) in enumerate(zip(from_segments,
 
670
                                                       to_segments)):
 
671
        if from_element != to_element:
 
672
            break
 
673
    else:
 
674
        count += 1
 
675
    unique_from = from_segments[count:]
 
676
    unique_to = to_segments[count:]
 
677
    segments = (['..'] * len(unique_from) + unique_to)
 
678
    if len(segments) == 0:
 
679
        return '.'
 
680
    return osutils.pathjoin(*segments)