Skip to content
Merged
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
31 changes: 14 additions & 17 deletions lib/file/find.rb
Original file line number Diff line number Diff line change
Expand Up @@ -249,33 +249,30 @@ def find
next if @mount && stat_info.dev != @filesystem
next if @links && stat_info.nlink != @links

# Calculate depth early if needed for maxdepth/mindepth checks
if @maxdepth || @mindepth
file_depth = file.split(File::SEPARATOR).reject(&:empty?).length
current_base_path = [@path].flatten.find{ |tpath| file.include?(tpath) }
path_depth = current_base_path.split(File::SEPARATOR).length

depth = file_depth - path_depth
end

if @maxdepth && depth > @maxdepth
if stat_info.directory? && !paths.include?(file)
queue << file
end
next
# Early termination: don't even add directories to queue if beyond maxdepth
if stat_info.directory? && !paths.include?(file)
if @maxdepth && depth && depth >= @maxdepth
# At maxdepth, don't traverse into this directory
else
queue << file
end
end

if @mindepth && depth < @mindepth
if stat_info.directory? && !paths.include?(file)
queue << file
end
next
end
# Skip files that are beyond maxdepth or before mindepth
if @maxdepth && depth && depth > @maxdepth
next
end

# Add directories back onto the list of paths to search unless
# they've already been added
#
if stat_info.directory? && !paths.include?(file)
queue << file
if @mindepth && depth && depth < @mindepth
next
end

next unless File.fnmatch?(@name, File.basename(file))
Expand Down