76
77
This assumes that both paths are already fully specified file:// URLs.
78
assert len(base) >= MIN_ABS_FILEURL_LENGTH, ('Length of base must be equal or'
79
' exceed the platform minimum url length (which is %d)' %
80
MIN_ABS_FILEURL_LENGTH)
79
if len(base) < MIN_ABS_FILEURL_LENGTH:
80
raise ValueError('Length of base must be equal or'
81
' exceed the platform minimum url length (which is %d)' %
82
MIN_ABS_FILEURL_LENGTH)
82
83
base = local_path_from_url(base)
83
84
path = local_path_from_url(path)
84
85
return escape(osutils.relpath(base, path))
249
250
raise errors.InvalidURL(url, 'Win32 UNC path urls'
250
251
' have form file://HOST/path')
251
252
return unescape(win32_url)
254
# allow empty paths so we can serve all roots
255
if win32_url == '///':
252
258
# usual local path with drive letter
253
259
if (win32_url[3] not in ('abcdefghijklmnopqrstuvwxyz'
254
260
'ABCDEFGHIJKLMNOPQRSTUVWXYZ')
270
276
# which actually strips trailing space characters.
271
277
# The worst part is that under linux ntpath.abspath has different
272
278
# semantics, since 'nt' is not an available module.
273
282
win32_path = osutils._win32_abspath(path)
274
283
# check for UNC path \\HOST\path
275
284
if win32_path.startswith('//'):
574
583
:return: A unicode string which can be safely encoded into the
575
584
specified encoding.
577
assert encoding is not None, 'you cannot specify None for the display encoding.'
587
raise ValueError('you cannot specify None for the display encoding')
578
588
if url.startswith('file://'):
580
590
path = local_path_from_url(url)
635
645
return from_location[sep+1:]
637
647
return from_location
650
def _is_absolute(url):
651
return (osutils.pathjoin('/foo', url) == url)
654
def rebase_url(url, old_base, new_base):
655
"""Convert a relative path from an old base URL to a new base URL.
657
The result will be a relative path.
658
Absolute paths and full URLs are returned unaltered.
660
scheme, separator = _find_scheme_and_separator(url)
661
if scheme is not None:
663
if _is_absolute(url):
665
old_parsed = urlparse.urlparse(old_base)
666
new_parsed = urlparse.urlparse(new_base)
667
if (old_parsed[:2]) != (new_parsed[:2]):
668
raise errors.InvalidRebaseURLs(old_base, new_base)
669
return determine_relative_path(new_parsed[2],
670
join(old_parsed[2], url))
673
def determine_relative_path(from_path, to_path):
674
"""Determine a relative path from from_path to to_path."""
675
from_segments = osutils.splitpath(from_path)
676
to_segments = osutils.splitpath(to_path)
678
for count, (from_element, to_element) in enumerate(zip(from_segments,
680
if from_element != to_element:
684
unique_from = from_segments[count:]
685
unique_to = to_segments[count:]
686
segments = (['..'] * len(unique_from) + unique_to)
687
if len(segments) == 0:
689
return osutils.pathjoin(*segments)