pathspec 0.0.2 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: a0eb87d15ea86cb98dce7e2ca41f6939418c3b57
4
- data.tar.gz: 084dc186f9fbf2f8c4a7ed8268cfa1c78e296728
2
+ SHA256:
3
+ metadata.gz: 959defcb28199d86bb69d1b7854937c677e37dc71521f44693a42411897acf01
4
+ data.tar.gz: dfa517e7b72d5aede4eb9daf642fda9266c1f82454bed03b3b776934f5c56c95
5
5
  SHA512:
6
- metadata.gz: 17ccc33fba7022b67fe0da1f9c02fbb6bd4e2d6d8e40fcb0d8ed0420f9733ace1692de35e69151e948b788cf007d7ea49f8c1760809ab02b672e61af16aac9e0
7
- data.tar.gz: a1d2fa238ea0fbc47083c25a4372a6d55848e200b1b850701e63ef12a191c06141b00f0a1871c14b46fe3e8366dcf1dfcf3b588a99f81b2d48117fbefe6eadd5
6
+ metadata.gz: b1824b7ce0c951017a51d501f5e18299d1fa0400ca0c6db54b9b774c4aa08a3587504d98cd578b99296cf34aad2e07593292563014a0fcc09f72697194b1327c
7
+ data.tar.gz: 96ab89b84afdc3f69b24f0f84bd5740c2ebee3048246ca5d482bc3f24f08dbda542b03f3a6efb02d33d6cdd225646e1ef2cadaa43be3d1069f1912cfa4f99da1
@@ -1 +1,28 @@
1
- 0.0.1: Initial version.
1
+ # pathspec-ruby CHANGELOG
2
+
3
+ ## 0.2.0 (Minor Release)
4
+ - (Feature) A CLI tool, pathspec-rb, is now provided with the gem.
5
+ - (API Change) New namespace for gem: `PathSpec`: Everything is now namespaced under `PathSpec`, to prevent naming collisions with other libraries. Thanks @tenderlove!
6
+ - (License) License version updated to Apache 2. Thanks @kytrinyx!
7
+ - (Maint) Pruned Supported Ruby Versions. We now test: 2.2.9, 2.3.6 and 2.4.3.
8
+ - (Maint) Ruby 2.5.0 testing is blocked on Travis, but should work locally. Thanks @SumLare!
9
+ - (Maint) Added Rubocop and made some corrections
10
+
11
+ ## 0.1.2 (Patch/Bug Fix Release)
12
+ - Fix for regexp matching Thanks @incase! #16
13
+ - File handling cleanup Thanks @martinandert! #13
14
+ - `from_filename` actually works now! Thanks @martinandert! #12
15
+
16
+ ## 0.1.0 (Minor Release)
17
+ - Port new edgecase handling from [python-path-specification](https://github.com/cpburnz/python-path-specification/pull/8). Many thanks to @jdpace! :)
18
+ - Removed EOL Ruby support
19
+ - Added current Ruby stable to Travis testing
20
+
21
+ ## 0.0.2 (Patch/Bug Fix Release)
22
+ - Fixed issues with Ruby 1.8.7/2.1.1
23
+ - Added more testing scripts
24
+ - Fixed Windows path related issues
25
+ - Cleanup unnecessary things in gem
26
+
27
+ ## 0.0.1
28
+ - Initial version.
data/README.md CHANGED
@@ -1,5 +1,12 @@
1
- pathspec-ruby
2
- =============
1
+ # pathspec-ruby
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/pathspec.svg)](https://badge.fury.io/rb/pathspec) [![Build Status](https://travis-ci.org/highb/pathspec-ruby.svg?branch=master)](https://travis-ci.org/highb/pathspec-ruby) [![Maintainability](https://api.codeclimate.com/v1/badges/4f3b5917e01fb34f790d/maintainability)](https://codeclimate.com/github/highb/pathspec-ruby/maintainability)
4
+
5
+ [Supported Rubies](https://www.ruby-lang.org/en/downloads/):
6
+
7
+ - 2.4.6 (Maintenance)
8
+ - 2.5.6 (Stable)
9
+ - 2.6.4 (Stable)
3
10
 
4
11
  Match Path Specifications, such as .gitignore, in Ruby!
5
12
 
@@ -7,21 +14,42 @@ Follows .gitignore syntax defined on [gitscm](http://git-scm.com/docs/gitignore)
7
14
 
8
15
  .gitignore functionality ported from [Python pathspec](https://pypi.python.org/pypi/pathspec/0.2.2) by [@cpburnz](https://github.com/cpburnz/python-path-specification)
9
16
 
10
- [Travis Status](https://travis-ci.org/highb/pathspec-ruby) ![Travis CI Status](https://travis-ci.org/highb/pathspec-ruby.svg?branch=master)
11
-
12
17
  ## Build/Install from Rubygems
18
+
13
19
  ```shell
14
20
  gem install pathspec
15
21
  ```
16
22
 
23
+ ## CLI Usage
24
+
25
+ ```bash
26
+ ➜ test-pathspec cat .gitignore
27
+ *.swp
28
+ /coverage/
29
+ ➜ test-pathspec be pathspec-rb specs_match "coverage/foo"
30
+ /coverage/
31
+ ➜ test-pathspec be pathspec-rb specs_match "file.swp"
32
+ *.swp
33
+ ➜ test-pathspec be pathspec-rb match "file.swp"
34
+ ➜ test-pathspec echo $?
35
+ 0
36
+ ➜ test-pathspec ls
37
+ Gemfile Gemfile.lock coverage file.swp source.rb
38
+ ➜ test-pathspec be pathspec-rb tree .
39
+ ./coverage
40
+ ./coverage/index.html
41
+ ./file.swp
42
+ ```
43
+
17
44
  ## Usage
45
+
18
46
  ```ruby
19
47
  require 'pathspec'
20
48
 
21
49
  # Create a .gitignore-style Pathspec by giving it newline separated gitignore
22
50
  # lines, an array of gitignore lines, or any other enumable object that will
23
51
  # give strings matching the .gitignore-style (File, etc.)
24
- gitignore = Pathspec.new File.read('.gitignore', 'r')
52
+ gitignore = Pathspec.from_filename('spec/files/gitignore_readme')
25
53
 
26
54
  # Our .gitignore in this example contains:
27
55
  # !**/important.txt
@@ -29,25 +57,31 @@ gitignore = Pathspec.new File.read('.gitignore', 'r')
29
57
 
30
58
  # true, matches "abc/**"
31
59
  gitignore.match 'abc/def.rb'
60
+ # CLI equivalent: pathspec.rb -f spec/files/gitignore_readme match 'abc/def.rb'
32
61
 
33
62
  # false, because it has been negated using the line "!**/important.txt"
34
63
  gitignore.match 'abc/important.txt'
64
+ # CLI equivalent: pathspec.rb -f spec/files/gitignore_readme match 'abc/important.txt'
35
65
 
36
66
  # Give a path somewhere in the filesystem, and the Pathspec will return all
37
67
  # matching files underneath.
38
68
  # Returns ['/src/repo/abc/', '/src/repo/abc/123']
39
69
  gitignore.match_tree '/src/repo'
70
+ # CLI equivalent: pathspec.rb -f spec/files/gitignore_readme tree /src/repo
40
71
 
41
72
  # Give an enumerable of paths, and Pathspec will return the ones that match.
42
73
  # Returns ['/abc/123', '/abc/']
43
74
  gitignore.match_paths ['/abc/123', '/abc/important.txt', '/abc/']
75
+ # There is no CLI equivalent to this.
44
76
  ```
45
77
 
46
78
  ## Building/Installing from Source
79
+
47
80
  ```shell
48
81
  git clone git@github.com:highb/pathspec-ruby.git
49
82
  cd pathspec-ruby && bash ./build_from_source.sh
50
83
  ```
51
84
 
52
85
  ## Contributing
86
+
53
87
  Pull requests, bug reports, and feature requests welcome! :smile: I've tried to write exhaustive tests but who knows what cases I've missed.
@@ -0,0 +1,80 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'optionparser'
5
+ require 'pathspec'
6
+ options = {
7
+ spec_type: :git,
8
+ spec_filename: '.gitignore'
9
+ }
10
+
11
+ optparser = OptionParser.new do |opts|
12
+ opts.banner = 'Usage: pathspec-rb [options] [subcommand] [path]
13
+ Subcommands:
14
+ specs_match: Finds all specs matching path.
15
+ tree: Finds all files under path matching the spec.
16
+ match: Checks if the path matches any spec.
17
+ EXIT STATUS:
18
+ 0 Matches found.
19
+ 1 No matches found.
20
+ >1 An error occured.
21
+ '
22
+ opts.on('-f', '--file FILENAME', String,
23
+ 'A spec file to load. Default: .gitignore') do |filename|
24
+ unless File.readable?(filename)
25
+ puts "Error: I couldn't read #{filename}"
26
+ exit 2
27
+ end
28
+
29
+ options[:spec_filename] = filename
30
+ end
31
+ opts.on('-t', '--type [git|regex]', %i[git regex],
32
+ 'Spec file type in FILENAME. Default: git. Available: git and regex.') do |type|
33
+ options[:spec_type] = type
34
+ end
35
+ opts.on('-v', '--verbose', 'Only output if there are matches.') do |_verbose|
36
+ options[:verbose] = true
37
+ end
38
+ end
39
+
40
+ optparser.parse!
41
+
42
+ command = ARGV[0]
43
+ path = ARGV[1]
44
+ if path
45
+ spec = PathSpec.from_filename(options[:spec_filename], options[:spec_type])
46
+ else
47
+ puts optparser.help
48
+ exit 2
49
+ end
50
+
51
+ case command
52
+ when 'specs_match'
53
+ if spec.match?(path)
54
+ puts "#{path} matches the following specs from #{options[:spec_filename]}:" if options[:verbose]
55
+ puts spec.specs_matching(path)
56
+ else
57
+ puts "#{path} does not match any specs from #{options[:spec_filename]}" if options[:verbose]
58
+ exit 1
59
+ end
60
+ when 'tree'
61
+ tree_matches = spec.match_tree(path)
62
+ if tree_matches.any?
63
+ puts "Files in #{path} that match #{options[:spec_filename]}" if options[:verbose]
64
+ puts tree_matches
65
+ else
66
+ puts "No file in #{path} matched #{options[:spec_filename]}" if options[:verbose]
67
+ exit 1
68
+ end
69
+ when 'match', ''
70
+ if spec.match?(path)
71
+ puts "#{path} matches a spec in #{options[:spec_filename]}" if options[:verbose]
72
+ else
73
+ puts "#{path} does not match any specs in #{options[:spec_filename]}" if options[:verbose]
74
+ exit 1
75
+ end
76
+ else
77
+ puts "Unknown sub-command #{command}."
78
+ puts optparser.help
79
+ exit 2
80
+ end
@@ -0,0 +1,82 @@
1
+ <h1 id="pathspec-rb1">pathspec-rb(1)</h1>
2
+
3
+ <h2 data-date="2020/01/04" id="name">NAME</h2>
4
+
5
+ <p>pathspec - Test pathspecs against a specific path</p>
6
+
7
+ <h2 id="synopsis">SYNOPSIS</h2>
8
+
9
+ <p><code>pathspec-rb</code> [<code>OPTIONS</code>] [<code>SUBCOMMAND</code>] [<code>PATH</code>] NAME PATH</p>
10
+
11
+ <h2 id="description">DESCRIPTION</h2>
12
+
13
+ <p><code>pathspc-rb</code> is a tool that accompanies the pathspec-ruby library to help
14
+ you test what match results the library would find using path specs. You can
15
+ either find all specs matching a path, find all files matching specs, or
16
+ verify that a path would match any spec.</p>
17
+
18
+ <p>https://github.com/highb/pathspec-ruby</p>
19
+
20
+ <h2 id="sub-commands">SUB-COMMANDS</h2>
21
+
22
+ <table>
23
+ <thead>
24
+ <tr>
25
+ <th>Name</th>
26
+ <th>Description</th>
27
+ </tr>
28
+ </thead>
29
+ <tbody>
30
+ <tr>
31
+ <td><em>specs_match</em></td>
32
+ <td>Find all specs matching path</td>
33
+ </tr>
34
+ </tbody>
35
+ <tbody>
36
+ <tr>
37
+ <td><em>tree</em></td>
38
+ <td>Find all files under path matching the spec</td>
39
+ </tr>
40
+ </tbody>
41
+ <tbody>
42
+ <tr>
43
+ <td><em>match</em></td>
44
+ <td>Check if the path matches any spec</td>
45
+ </tr>
46
+ </tbody>
47
+ </table>
48
+
49
+ <h2 id="options">OPTIONS</h2>
50
+
51
+ <dl>
52
+ <dt><code>-f &lt;FILENAME&gt;</code>, <code>--file &lt;FILENAME&gt;</code></dt>
53
+ <dd>Load path specs from the file passed in as argument. If this option is not specified, <code>pathspec-rb</code> defaults to loading <code>.gitignore</code>.</dd>
54
+ <dt><code>-t [git|regex]</code>, <code>--type [git|regex]</code></dt>
55
+ <dd>Type of spec expected in the loaded specs file (see <code>-f</code> option). Defaults to <code>git</code>.</dd>
56
+ <dt><code>-v</code>, <code>--verbose</code></dt>
57
+ <dd>Only output if there are matches.</dd>
58
+ </dl>
59
+
60
+ <h2 id="example">EXAMPLE</h2>
61
+
62
+ <p>Find all files ignored by git under your source directory:</p>
63
+
64
+ <pre><code> $ pathspec-rb tree src/
65
+ </code></pre>
66
+
67
+ <p>List all spec rules that would match for the specified path:</p>
68
+
69
+ <pre><code> $ pathspec-rb specs_match build/
70
+ </code></pre>
71
+
72
+ <p>Check that a path matches at least one of the specs in a new version of a
73
+ gitignore file:</p>
74
+
75
+ <pre><code> $ pathspec-rb match -f .gitignore.new spec/fixtures/
76
+ </code></pre>
77
+
78
+ <h2 id="author">AUTHOR</h2>
79
+
80
+ <p>Brandon High highb@users.noreply.github.com</p>
81
+
82
+ <p>Gabriel Filion</p>
@@ -0,0 +1,67 @@
1
+ .\" generated by kramdown
2
+ .TH "PATHSPEC\-RB" "1"
3
+ .SH "NAME"
4
+ pathspec \- Test pathspecs against a specific path
5
+ .SH "SYNOPSIS"
6
+ \fBpathspec\-rb\fP [\fBOPTIONS\fP] [\fBSUBCOMMAND\fP] [\fBPATH\fP] NAME PATH
7
+ .SH "DESCRIPTION"
8
+ \fBpathspc\-rb\fP is a tool that accompanies the pathspec\-ruby library to help you test what match results the library would find using path specs\. You can either find all specs matching a path, find all files matching specs, or verify that a path would match any spec\.
9
+ .P
10
+ https://github\.com/highb/pathspec\-ruby
11
+ .SH "SUB\-COMMANDS"
12
+ .TS
13
+ box ;
14
+ lb lb .
15
+ Name Description
16
+ =
17
+ .T&
18
+ l l .
19
+ \fIspecs_match\fP Find all specs matching path
20
+ _
21
+ .T&
22
+ l l .
23
+ \fItree\fP Find all files under path matching the spec
24
+ _
25
+ .T&
26
+ l l .
27
+ \fImatch\fP Check if the path matches any spec
28
+ .TE
29
+ .sp
30
+ .SH "OPTIONS"
31
+ .TP
32
+ \fB\-f <FILENAME>\fP, \fB\-\-file <FILENAME>\fP
33
+ Load path specs from the file passed in as argument\. If this option is not specified, \fBpathspec\-rb\fP defaults to loading \fB\&\.gitignore\fP\&\.
34
+ .TP
35
+ \fB\-t [git|regex]\fP, \fB\-\-type [git|regex]\fP
36
+ Type of spec expected in the loaded specs file (see \fB\-f\fP option)\. Defaults to \fBgit\fP\&\.
37
+ .TP
38
+ \fB\-v\fP, \fB\-\-verbose\fP
39
+ Only output if there are matches\.
40
+ .SH "EXAMPLE"
41
+ Find all files ignored by git under your source directory:
42
+ .sp
43
+ .RS 4
44
+ .EX
45
+ $ pathspec\-rb tree src/
46
+ .EE
47
+ .RE
48
+ .P
49
+ List all spec rules that would match for the specified path:
50
+ .sp
51
+ .RS 4
52
+ .EX
53
+ $ pathspec\-rb specs_match build/
54
+ .EE
55
+ .RE
56
+ .P
57
+ Check that a path matches at least one of the specs in a new version of a gitignore file:
58
+ .sp
59
+ .RS 4
60
+ .EX
61
+ $ pathspec\-rb match \-f \.gitignore\.new spec/fixtures/
62
+ .EE
63
+ .RE
64
+ .SH "AUTHOR"
65
+ Brandon High highb@users\.noreply\.github\.com
66
+ .P
67
+ Gabriel Filion
@@ -0,0 +1,64 @@
1
+ # pathspec-rb(1)
2
+
3
+ {:data-date="2020/01/04"}
4
+
5
+ ## NAME
6
+
7
+ pathspec - Test pathspecs against a specific path
8
+
9
+ ## SYNOPSIS
10
+
11
+ `pathspec-rb` [`OPTIONS`] [`SUBCOMMAND`] [`PATH`] NAME PATH
12
+
13
+ ## DESCRIPTION
14
+
15
+ `pathspc-rb` is a tool that accompanies the pathspec-ruby library to help
16
+ you test what match results the library would find using path specs. You can
17
+ either find all specs matching a path, find all files matching specs, or
18
+ verify that a path would match any spec.
19
+
20
+ https://github.com/highb/pathspec-ruby
21
+
22
+ ## SUB-COMMANDS
23
+
24
+ |-
25
+ | Name | Description
26
+ |-
27
+ | *specs_match* | Find all specs matching path
28
+ |-
29
+ | *tree* | Find all files under path matching the spec
30
+ |-
31
+ | *match* | Check if the path matches any spec
32
+ |-
33
+
34
+ ## OPTIONS
35
+
36
+ `-f <FILENAME>`, `--file <FILENAME>`
37
+ : Load path specs from the file passed in as argument. If this option is not specified, `pathspec-rb` defaults to loading `.gitignore`.
38
+
39
+ `-t [git|regex]`, `--type [git|regex]`
40
+ : Type of spec expected in the loaded specs file (see `-f` option). Defaults to `git`.
41
+
42
+ `-v`, `--verbose`
43
+ : Only output if there are matches.
44
+
45
+ ## EXAMPLE
46
+
47
+ Find all files ignored by git under your source directory:
48
+
49
+ $ pathspec-rb tree src/
50
+
51
+ List all spec rules that would match for the specified path:
52
+
53
+ $ pathspec-rb specs_match build/
54
+
55
+ Check that a path matches at least one of the specs in a new version of a
56
+ gitignore file:
57
+
58
+ $ pathspec-rb match -f .gitignore.new spec/fixtures/
59
+
60
+ ## AUTHOR
61
+
62
+ Brandon High highb@users.noreply.github.com
63
+
64
+ Gabriel Filion
@@ -3,15 +3,14 @@ require 'pathspec/regexspec'
3
3
  require 'find'
4
4
  require 'pathname'
5
5
 
6
+ # Main PathSpec class, provides interfaces to various spec implementations
6
7
  class PathSpec
7
8
  attr_reader :specs
8
9
 
9
- def initialize(lines=nil, type=:git)
10
+ def initialize(lines = nil, type = :git)
10
11
  @specs = []
11
12
 
12
- if lines
13
- add(lines, type)
14
- end
13
+ add(lines, type) if lines
15
14
 
16
15
  self
17
16
  end
@@ -26,9 +25,7 @@ class PathSpec
26
25
 
27
26
  def specs_matching(path)
28
27
  @specs.select do |spec|
29
- if spec.match(path)
30
- spec
31
- end
28
+ spec if spec.match(path)
32
29
  end
33
30
  end
34
31
 
@@ -41,58 +38,52 @@ class PathSpec
41
38
  Find.find(root) do |path|
42
39
  relpath = Pathname.new(path).relative_path_from(rootpath).to_s
43
40
  relpath += '/' if File.directory? path
44
- if match(relpath)
45
- matching << path
46
- end
41
+ matching << path if match(relpath)
47
42
  end
48
43
 
49
44
  matching
50
45
  end
51
46
 
52
- def match_path(path, root='/')
47
+ def match_path(path, root = '/')
53
48
  rootpath = Pathname.new(drive_letter_to_path(root))
54
49
  relpath = Pathname.new(drive_letter_to_path(path)).relative_path_from(rootpath).to_s
55
- relpath = relpath + '/' if path[-1].chr == '/'
50
+ relpath += '/' if path[-1].chr == '/'
56
51
 
57
52
  match(relpath)
58
53
  end
59
54
 
60
- def match_paths(paths, root='/')
55
+ def match_paths(paths, root = '/')
61
56
  matching = []
62
57
 
63
58
  paths.each do |path|
64
- if match_path(path, root)
65
- matching << path
66
- end
59
+ matching << path if match_path(path, root)
67
60
  end
68
61
 
69
62
  matching
70
63
  end
71
64
 
72
65
  def drive_letter_to_path(path)
73
- path.gsub(/^([a-zA-z]):\//, '/\1/')
66
+ path.gsub(%r{^([a-zA-Z]):/}, '/\1/')
74
67
  end
75
68
 
76
69
  # Generate specs from a filename, such as a .gitignore
77
- def self.from_filename(filename, type=:git)
78
- self.from_lines(File.open(filename, 'r'))
70
+ def self.from_filename(filename, type = :git)
71
+ File.open(filename, 'r') { |io| from_lines(io, type) }
79
72
  end
80
73
 
81
- def self.from_lines(lines, type=:git)
82
- self.new lines, type
74
+ def self.from_lines(lines, type = :git)
75
+ new lines, type
83
76
  end
84
77
 
85
78
  # Generate specs from lines of text
86
- def add(obj, type=:git)
79
+ def add(obj, type = :git)
87
80
  spec_class = spec_type(type)
88
81
 
89
82
  if obj.respond_to?(:each_line)
90
83
  obj.each_line do |l|
91
84
  spec = spec_class.new(l.rstrip)
92
85
 
93
- if !spec.regex.nil? && !spec.inclusive?.nil?
94
- @specs << spec
95
- end
86
+ @specs << spec if !spec.regex.nil? && !spec.inclusive?.nil?
96
87
  end
97
88
  elsif obj.respond_to?(:each)
98
89
  obj.each do |l|
@@ -110,9 +101,10 @@ class PathSpec
110
101
  end
111
102
 
112
103
  def spec_type(type)
113
- if type == :git
104
+ case type
105
+ when :git
114
106
  GitIgnoreSpec
115
- elsif type == :regex
107
+ when :regex
116
108
  RegexSpec
117
109
  else
118
110
  raise "Unknown spec type #{type}"