Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions spec/std/path_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -966,6 +966,7 @@ describe Path do
it_relativizes("C:\\Projects", "c:\\projects\\src", "../c:\\projects\\src", "src")
it_relativizes("C:\\Projects", "c:\\projects", "../c:\\projects", ".")
it_relativizes("C:\\Projects\\a\\..", "c:\\projects", "../c:\\projects", ".")
it_relativizes("C:\\foo", "C:/bar", "../C:/bar", "..\\bar")
end
end

Expand Down
10 changes: 5 additions & 5 deletions src/path.cr
Original file line number Diff line number Diff line change
Expand Up @@ -979,19 +979,19 @@ struct Path
# to the same roots (e.g. `\\.\C:\foo` and `C:\foo` are not equivalent, nor
# are `\\?\UNC\server\share\foo` and `\\server\share\foo`).
def relative_to?(base : Path) : Path?
# work on normalized paths otherwise we would need to backtrack on `..` parts
base = base.normalize
target = self.normalize

base_anchor = base.anchor
target_anchor = self.anchor
target_anchor = target.anchor

# if paths have a different anchors, there can't be a relative path between
# them.
if base_anchor != target_anchor
return nil
end

# work on normalized paths otherwise we would need to backtrack on `..` parts
base = base.normalize
target = self.normalize

# check for trivial case of equal paths
if base == target
return new_instance(".")
Expand Down