Skip to content

Commit

Permalink
Fix relpath when path and startpath are in the same drive (#40323)
Browse files Browse the repository at this point in the history
* 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.

(cherry picked from commit 89fff18)
  • Loading branch information
musm authored and KristofferC committed Apr 4, 2021
1 parent 85d25a1 commit 79559ac
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 @@ -514,12 +514,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 79559ac

Please sign in to comment.