Skip to content

Commit 9e483eb

Browse files
committed
Extract fu_split_path and fu_relative_components_from
1 parent ed9bc51 commit 9e483eb

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

lib/fileutils.rb

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -741,14 +741,10 @@ def ln_sr(src, dest, force: nil, noop: nil, verbose: nil)
741741
srcs = Array.try_convert(src) || [src]
742742
link = proc do |s, target_directory = true|
743743
s = File.path(s)
744-
srcdirs = (File.realdirpath(s) rescue File.expand_path(s)).split(File::SEPARATOR)
745-
destdirs = File.realdirpath(dest).split(File::SEPARATOR)
744+
srcdirs = fu_split_path((File.realdirpath(s) rescue File.expand_path(s)))
745+
destdirs = fu_split_path(File.realdirpath(dest))
746746
destdirs.pop unless target_directory
747-
while srcdirs.first&.== destdirs.first
748-
srcdirs.shift
749-
destdirs.shift
750-
end
751-
s = File.join(Array.new(destdirs.size, '..'), srcdirs)
747+
s = File.join(fu_relative_components_from(srcdirs, destdirs))
752748
d = target_directory ? File.join(dest, File.basename(s)) : dest
753749
fu_output_message "ln -s#{force ? 'f' : ''} #{s} #{d}" if verbose
754750
remove_file d, true if force
@@ -2495,6 +2491,20 @@ def fu_output_message(msg) #:nodoc:
24952491
end
24962492
private_module_function :fu_output_message
24972493

2494+
def fu_split_path(path)
2495+
File.path(path).split(File::SEPARATOR)
2496+
end
2497+
private_module_function :fu_split_path
2498+
2499+
def fu_relative_components_from(target, base) #:nodoc:
2500+
i = 0
2501+
while target[i]&.== base[i]
2502+
i += 1
2503+
end
2504+
Array.new(base.size-i, '..').concat(target[i..-1])
2505+
end
2506+
private_module_function :fu_relative_components_from
2507+
24982508
# This hash table holds command options.
24992509
OPT_TABLE = {} #:nodoc: internal use only
25002510
(private_instance_methods & methods(false)).inject(OPT_TABLE) {|tbl, name|

0 commit comments

Comments
 (0)