1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

Fix conversion from absolute path to relative path

This commit is contained in:
Nobuyoshi Nakada 2022-07-16 17:58:52 +09:00
parent c093e7d645
commit 6c1d7dab52
No known key found for this signature in database
GPG key ID: 7CD2805BFA3770C6

View file

@ -30,23 +30,28 @@ unless respond_to?(:ln_sr)
def ln_sr(src, dest, target_directory: true, force: nil, noop: nil, verbose: nil) def ln_sr(src, dest, target_directory: true, force: nil, noop: nil, verbose: nil)
options = "#{force ? 'f' : ''}#{target_directory ? '' : 'T'}" options = "#{force ? 'f' : ''}#{target_directory ? '' : 'T'}"
dest = File.path(dest) dest = File.path(dest)
srcs = Array.try_convert(src) || [src] srcs = Array(src)
link = proc do |s, target_dir_p = true| link = proc do |s, target_dir_p = true|
s = File.path(s) s = File.path(s)
if target_dir_p
d = File.join(destdirs = dest, File.basename(s))
else
destdirs = File.dirname(d = dest)
end
destdirs = fu_split_path(File.realpath(destdirs))
if fu_starting_path?(s) if fu_starting_path?(s)
srcdirs = fu_split_path((File.realdirpath(s) rescue File.expand_path(s))) srcdirs = fu_split_path((File.realdirpath(s) rescue File.expand_path(s)))
base = fu_relative_components_from(srcdirs, destdirs)
s = File.join(*base)
else else
srcdirs = fu_clean_components(*fu_split_path(s)) srcdirs = fu_clean_components(*fu_split_path(s))
end
destdirs = fu_split_path(File.realdirpath(dest))
destdirs.pop unless target_dir_p
base = fu_relative_components_from(fu_split_path(Dir.pwd), destdirs) base = fu_relative_components_from(fu_split_path(Dir.pwd), destdirs)
while srcdirs.first&. == ".." and base.last and !fu_starting_path?(base.last) while srcdirs.first&. == ".." and base.last&.!=("..") and !fu_starting_path?(base.last)
srcdirs.shift srcdirs.shift
base.pop base.pop
end end
s = File.join(*base, *srcdirs) s = File.join(*base, *srcdirs)
d = target_dir_p ? File.join(dest, File.basename(s)) : dest end
fu_output_message "ln -s#{options} #{s} #{d}" if verbose fu_output_message "ln -s#{options} #{s} #{d}" if verbose
next if noop next if noop
remove_file d, true if force remove_file d, true if force