/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/urlutils.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2008-07-15 14:14:22 UTC
  • mfrom: (3242.3.41 stacking-policy)
  • Revision ID: pqm@pqm.ubuntu.com-20080715141422-gwfo1jmu1tm8lcf0
Implement policies for stacking (abentley)

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)