@@ -83,6 +83,14 @@ def directories_to_watch
83
83
end
84
84
85
85
class PathHelper
86
+ using Module . new {
87
+ refine Pathname do
88
+ def ascendant_of? ( other )
89
+ other . to_s =~ /\A #{ Regexp . quote ( to_s ) } #{ Pathname ::SEPARATOR_PAT } ?/
90
+ end
91
+ end
92
+ }
93
+
86
94
def xpath ( path )
87
95
Pathname . new ( path ) . expand_path
88
96
end
@@ -96,25 +104,24 @@ def normalize_extension(ext)
96
104
def longest_common_subpath ( paths )
97
105
return if paths . empty?
98
106
99
- csp = Pathname . new ( paths [ 0 ] )
107
+ lcsp = Pathname . new ( paths [ 0 ] )
100
108
101
109
paths [ 1 ..-1 ] . each do |path |
102
110
loop do
103
- break if path . ascend do |ascendant |
104
- break true if ascendant == csp
105
- end
111
+ break if lcsp . ascendant_of? ( path )
106
112
107
- if csp . root?
108
- # A root directory is not an ascendant of path. This may happen
109
- # if there are paths in different drives on Windows.
113
+ if lcsp . root?
114
+ # If we get here a root directory is not an ascendant of path.
115
+ # This may happen if there are paths in different drives on
116
+ # Windows.
110
117
return
111
118
else
112
- csp = csp . parent
119
+ lcsp = lcsp . parent
113
120
end
114
121
end
115
122
end
116
123
117
- csp
124
+ lcsp
118
125
end
119
126
120
127
# Returns the deepest existing ascendant, which could be the argument itself.
@@ -139,22 +146,15 @@ def existing_parent(dir)
139
146
def filter_out_descendants ( directories )
140
147
return directories if directories . length < 2
141
148
142
- sorted = directories . sort_by { |dir | dir . each_filename . to_a . length }
149
+ sorted_by_nparts = directories . sort_by { |dir | dir . each_filename . to_a . length }
143
150
descendants = [ ]
144
151
145
- until sorted . empty?
146
- directory = sorted . shift
152
+ until sorted_by_nparts . empty?
153
+ dir = sorted_by_nparts . shift
147
154
148
- sorted . each do |candidate_to_descendant |
149
- if candidate_to_descendant . to_path . start_with? ( directory . to_path )
150
- dparts = directory . each_filename . to_a
151
- cparts = candidate_to_descendant . each_filename . to_a
152
-
153
- if cparts [ 0 , dparts . length ] == dparts
154
- descendants << candidate_to_descendant
155
- end
156
- end
157
- end
155
+ descendants . concat sorted_by_nparts . select { |possible_descendant |
156
+ dir . ascendant_of? ( possible_descendant )
157
+ }
158
158
end
159
159
160
160
directories - descendants
0 commit comments