Skip to content

Commit

Permalink
Fix relpath when path and startpath are in the same drive (JuliaLang#…
Browse files Browse the repository at this point in the history
…40323)

* Fix relpath when path and startpath are in the same drive

When startpath == ".", assume the startpath is in the same drive.
This subsequently required tweaking the existing logic to then
canonicalize the drive casing in instances where the drive casing
differs.
  • Loading branch information
musm authored and antoine-levitt committed May 9, 2021
1 parent 16f23d4 commit 6892fce
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
14 changes: 9 additions & 5 deletions base/path.jl
Original file line number Diff line number Diff line change
Expand Up @@ -517,12 +517,16 @@ function relpath(path::String, startpath::String = ".")
curdir = "."
pardir = ".."
path == startpath && return curdir
path_drive, path_without_drive = splitdrive(path)
startpath_drive, startpath_without_drive = splitdrive(startpath)
path_arr = split(abspath(path_without_drive), path_separator_re)
start_arr = split(abspath(startpath_without_drive), path_separator_re)
if Sys.iswindows()
lowercase(path_drive) != lowercase(startpath_drive) && return abspath(path)
path_drive, path_without_drive = splitdrive(path)
startpath_drive, startpath_without_drive = splitdrive(startpath)
isempty(startpath_drive) && (startpath_drive = path_drive) # by default assume same as path drive
uppercase(path_drive) == uppercase(startpath_drive) || return abspath(path) # if drives differ return first path
path_arr = split(abspath(path_drive * path_without_drive), path_separator_re)
start_arr = split(abspath(path_drive * startpath_without_drive), path_separator_re)
else
path_arr = split(abspath(path), path_separator_re)
start_arr = split(abspath(startpath), path_separator_re)
end
i = 0
while i < min(length(path_arr), length(start_arr))
Expand Down
4 changes: 4 additions & 0 deletions test/path.jl
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,10 @@
# Additional cases
@test_throws ArgumentError relpath(S("$(sep)home$(sep)user$(sep)dir_withendsep$(sep)"), "")
@test_throws ArgumentError relpath(S(""), S("$(sep)home$(sep)user$(sep)dir_withendsep$(sep)"))

# issue 40237
path = "..$(sep)a$(sep)b$(sep)c"
@test relpath(abspath(path)) == path
end
test_relpath()
end
Expand Down

0 comments on commit 6892fce

Please sign in to comment.