fast_ignore 0.5.1 → 0.5.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: dd3cffbf805ab15bbbfe22e5e4afb0462ada58ec1baaf6281e9bb8ef5d80697a
4
- data.tar.gz: 81a228932c87cc9fbec0a21562b6a6d295a3f45aed8502f9bb38dd4237a826f9
3
+ metadata.gz: 35a905f9dd8c28df7b46d6c0c61ad1fcaf44a9566c19b704a88584668f49ba20
4
+ data.tar.gz: 620917002cd9696bb5787819675034d6b5269d70812f0e5247523b10091d37b9
5
5
  SHA512:
6
- metadata.gz: 24adff6dfcc51add546f9f7ed2e67eee3e71ef574f449f834161d642426e019095394f73733d5cd29a4f0b3db40b7b845cb024a3958b2b6ca8ef27490023e51f
7
- data.tar.gz: cac2c9eea6dbcce19adbe0ed7c2c0c332e8453e3ff25f0124e2f7850133cec84fdd977c7e24d931654d9150d2b808dd91f7ad43639b9bc9ce7bb133ba58ff2fd
6
+ metadata.gz: 6406dd9e6998fb9a3345981239e7fa0046f1356852a599f2c347d4b7bec4654069ac5704351c3079ce79c0965925a601dbcf56990ac6850aefef5f3a109793fa
7
+ data.tar.gz: 83a7aacfd25b3350a4100d6247c683f6b4f1754c6311d9b6064631e5033890ea2fcc0c8dd5b5757f8f0e9a39af55cfc3f7560918813e69ab6e9139734abc4fa1
data/CHANGELOG.md CHANGED
@@ -1,3 +1,6 @@
1
+ # v0.5.2
2
+ - performance improvements
3
+
1
4
  # v0.5.1
2
5
  - restore `.allowed?`. now i have tests for it. oops
3
6
 
@@ -20,5 +20,9 @@ class FastIgnore
20
20
  def inspect
21
21
  "#<Rule #{'!' if negation?}#{rule}#{'/' if dir_only?}>"
22
22
  end
23
+
24
+ def match?(path)
25
+ ::File.fnmatch?(@rule, path, 14)
26
+ end
23
27
  end
24
28
  end
@@ -10,31 +10,29 @@ class FastIgnore
10
10
  @rules = []
11
11
  @non_dir_only_rules = []
12
12
  @project_root = project_root
13
- @allowed_unrecursive = { @project_root => true }
14
- @allowed_recursive = { @project_root => true }
13
+
15
14
  @any_not_anchored = false
16
15
  @empty = true
17
16
  @allow = allow
18
17
  end
19
18
 
20
- def allowed_unrecursive?(path, dir)
21
- @allowed_unrecursive.fetch(path) do
22
- (dir ? @rules : @non_dir_only_rules).reverse_each do |rule|
23
- # 14 = Rule::FNMATCH_OPTIONS
24
- return @allowed_unrecursive[path] = rule.negation? if ::File.fnmatch?(rule.rule, path, 14)
25
- end
26
-
27
- @allowed_unrecursive[path] = default?(dir)
28
- end
29
- end
30
-
31
19
  def allowed_recursive?(path, dir)
20
+ @allowed_recursive ||= { @project_root => true }
32
21
  @allowed_recursive.fetch(path) do
33
22
  @allowed_recursive[path] =
34
23
  allowed_recursive?(::File.dirname(path), true) && allowed_unrecursive?(path, dir)
35
24
  end
36
25
  end
37
26
 
27
+ def allowed_unrecursive?(path, dir)
28
+ (dir ? @rules : @non_dir_only_rules).reverse_each do |rule|
29
+ # 14 = Rule::FNMATCH_OPTIONS
30
+ return rule.negation? if ::File.fnmatch?(rule.rule, path, 14)
31
+ end
32
+
33
+ default?(dir)
34
+ end
35
+
38
36
  def parse_rules(rule_line, root: @root, expand_path: false)
39
37
  ::FastIgnore::RuleParser.new_rule(rule_line, rule_set: self, allow: @allow, root: root, expand_path: expand_path)
40
38
  end
@@ -49,8 +47,7 @@ class FastIgnore
49
47
  end
50
48
 
51
49
  def clear_cache
52
- @allowed_unrecursive = { @project_root => true }
53
- @allowed_recursive = { @project_root => true }
50
+ @allowed_recursive = nil
54
51
  end
55
52
 
56
53
  private
@@ -4,7 +4,7 @@ require_relative 'rule_set'
4
4
 
5
5
  class FastIgnore
6
6
  class RuleSetBuilder
7
- attr_reader :rules
7
+ attr_reader :rule_set
8
8
 
9
9
  def initialize(root:, allow: false)
10
10
  @rule_set = RuleSet.new(project_root: root, allow: allow)
@@ -33,13 +33,5 @@ class FastIgnore
33
33
 
34
34
  @rule_set.clear_cache
35
35
  end
36
-
37
- def allowed_unrecursive?(path, dir)
38
- @rule_set.allowed_unrecursive?(path, dir)
39
- end
40
-
41
- def allowed_recursive?(path, dir)
42
- @rule_set.allowed_recursive?(path, dir)
43
- end
44
36
  end
45
37
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class FastIgnore
4
- VERSION = '0.5.1'
4
+ VERSION = '0.5.2'
5
5
  end
data/lib/fast_ignore.rb CHANGED
@@ -15,7 +15,7 @@ class FastIgnore
15
15
  using ::FastIgnore::Backports::DirEachChild
16
16
  end
17
17
 
18
- def initialize( # rubocop:disable Metrics/ParameterLists
18
+ def initialize( # rubocop:disable Metrics/ParameterLists, Metrics/MethodLength, Metrics/AbcSize
19
19
  relative: false,
20
20
  root: ::Dir.pwd,
21
21
  ignore_rules: nil,
@@ -24,24 +24,27 @@ class FastIgnore
24
24
  include_rules: nil,
25
25
  include_files: nil
26
26
  )
27
- @ignore = ::FastIgnore::RuleSetBuilder.new(root: root)
28
- @only = ::FastIgnore::RuleSetBuilder.new(allow: true, root: root)
29
- @only.add_files(Array(include_files))
30
- @only.add_rules(Array(include_rules), expand_path: true)
27
+ @root = root.delete_suffix('/')
28
+ @root_trailing_slash = "#{@root}/"
29
+ ignore = ::FastIgnore::RuleSetBuilder.new(root: @root)
30
+ only = ::FastIgnore::RuleSetBuilder.new(allow: true, root: @root)
31
+ only.add_files(Array(include_files))
32
+ only.add_rules(Array(include_rules), expand_path: true)
33
+ @only = only.rule_set
31
34
 
32
- @ignore.add_rules(['.git'])
33
- @ignore.add_files([gitignore]) if gitignore && ::File.exist?(gitignore)
34
- @ignore.add_files(Array(ignore_files))
35
- @ignore.add_rules(Array(ignore_rules))
35
+ ignore.add_rules(['.git'])
36
+ ignore.add_files([gitignore]) if gitignore && ::File.exist?(gitignore)
37
+ ignore.add_files(Array(ignore_files))
38
+ ignore.add_rules(Array(ignore_rules))
39
+ @ignore = ignore.rule_set
36
40
  @relative = relative
37
- @root = root
38
41
  end
39
42
 
40
43
  def each(&block)
41
44
  if block_given?
42
- all_allowed.each(&block)
45
+ all_allowed(&block)
43
46
  else
44
- all_allowed.each
47
+ enum_for(:all_allowed)
45
48
  end
46
49
  end
47
50
 
@@ -52,33 +55,32 @@ class FastIgnore
52
55
  end
53
56
 
54
57
  def all_allowed
55
- allowed = []
56
- find_children(@root) do |path, dir|
58
+ find_children(@root_trailing_slash) do |path, dir|
57
59
  next false unless @ignore.allowed_unrecursive?(path, dir)
58
60
  next false unless @only.allowed_unrecursive?(path, dir)
59
61
  next true if dir
60
- next false unless ::File.readable?(path)
61
62
 
62
- allowed << prepare_path(path)
63
+ yield prepare_path(path)
63
64
 
64
65
  false
65
66
  end
66
- allowed
67
67
  end
68
68
 
69
69
  private
70
70
 
71
71
  def prepare_path(path)
72
- @relative ? path.delete_prefix("#{@root}/") : path
72
+ @relative ? path.delete_prefix(@root_trailing_slash) : path
73
73
  end
74
74
 
75
- def find_children(path, &block)
75
+ def find_children(path, &block) # rubocop:disable Metrics/MethodLength
76
76
  Dir.each_child(path) do |child|
77
77
  begin
78
- child = ::File.join(path, child)
79
- dir = ::File.directory?(child)
80
- look_at_children = block.call child, dir
81
- find_children(child, &block) if look_at_children
78
+ child = path + child
79
+ stat = ::File.stat(child)
80
+ next unless stat.readable?
81
+
82
+ look_at_children = block.call child, stat.directory?
83
+ find_children("#{child}/", &block) if look_at_children
82
84
  rescue Errno::ENOENT, Errno::EACCES, Errno::ENOTDIR, Errno::ELOOP, Errno::ENAMETOOLONG
83
85
  nil
84
86
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fast_ignore
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
4
+ version: 0.5.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dana Sherson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-02-12 00:00:00.000000000 Z
11
+ date: 2020-02-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler