Project

pathspec

0.21
No release in over a year
Use to match path patterns such as gitignore
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Development

~> 2.2
~> 2.5
~> 2.3
~> 13.0
~> 3.10
~> 1.63.0
~> 0.21
 Project Readme

pathspec-ruby

Gem Version Ruby Maintainability

man Page as HTML

Supported Rubies:

  • 3.2 (Stable, Tested)
  • 3.3 (Stable, Tested)
  • 3.4 (Stable, Tested)
  • 4.0.1 (Preview, Tested)

Deprecated Rubies

The following Ruby versions are no longer supported. If you need to use an older Ruby version, please use an earlier version of this gem:

  • Ruby 3.1: Deprecated in version 3.0.0 (current project, not yet released)
  • Ruby 3.0: Deprecated in version 2.0.0 (January 2024)
  • Ruby 2.x (2.6-2.7): Deprecated in version 2.0.0 (January 2024)
  • Ruby 2.5 and earlier: Deprecated in version 1.0.0 (January 2021)
  • Ruby 1.8 and 1.9: Deprecated in version 0.2.0 (circa 2017)

Match Path Specifications, such as .gitignore, in Ruby!

Follows .gitignore syntax defined on gitscm

.gitignore functionality ported from Python pathspec by @cpburnz

Build/Install from Rubygems

gem install pathspec

CLI Usage

➜ cat .gitignore
*.swp
/coverage/
➜ bundle exec pathspec-rb specs_match "coverage/foo"
/coverage/
➜ bundle exec pathspec-rb specs_match "file.swp"
*.swp
➜ bundle exec pathspec-rb match "file.swp"echo $?
0
➜ ls
Gemfile      Gemfile.lock coverage     file.swp     source.rb
➜ bundle exec pathspec-rb tree .
./coverage
./coverage/index.html
./file.swp

Usage

require 'pathspec'

# Create a .gitignore-style Pathspec by giving it newline separated gitignore
# lines, an array of gitignore lines, or any other enumable object that will
# give strings matching the .gitignore-style (File, etc.)
gitignore = PathSpec.from_filename('spec/files/gitignore_readme')

# Our .gitignore in this example contains:
# !**/important.txt
# abc/**

# true, matches "abc/**"
gitignore.match 'abc/def.rb'
# CLI equivalent: pathspec.rb -f spec/files/gitignore_readme match 'abc/def.rb'

# false, because it has been negated using the line "!**/important.txt"
gitignore.match 'abc/important.txt'
# CLI equivalent: pathspec.rb -f spec/files/gitignore_readme match 'abc/important.txt'

# Give a path somewhere in the filesystem, and the Pathspec will return all
# matching files underneath.
# Returns ['/src/repo/abc/', '/src/repo/abc/123']
gitignore.match_tree '/src/repo'
# CLI equivalent: pathspec.rb -f spec/files/gitignore_readme tree /src/repo

# Give an enumerable of paths, and Pathspec will return the ones that match.
# Returns ['/abc/123', '/abc/']
gitignore.match_paths ['/abc/123', '/abc/important.txt', '/abc/']
# There is no CLI equivalent to this.

Example Usage in Gemspec

lib = File.expand_path("lib", __dir__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require "gemspec_pathspec_test/version"
require 'pathspec'

Gem::Specification.new do |spec|
  spec.name          = "gemspec_pathspec_test"
  spec.version       = GemspecPathspecTest::VERSION
  spec.authors       = ["Brandon High"]
  spec.email         = ["highb@users.noreply.github.com"]

  spec.summary = "whatever"

  spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"

  ps = PathSpec.from_filename('.gitignore')
  spec.files         = Dir['lib/*.rb'].reject { |f| ps.match(f) }
  spec.bindir        = "exe"
  spec.executables   = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
  spec.require_paths = ["lib"]

  spec.add_development_dependency "bundler", "~> 2.0"
  spec.add_development_dependency "rake", "~> 10.0"
  spec.add_development_dependency "rspec", "~> 3.0"
end

Building/Installing from Source

git clone git@github.com:highb/pathspec-ruby.git
cd pathspec-ruby && bash ./build_from_source.sh

Development Setup

This project uses mise for managing Ruby and bundler versions.

Install mise

# macOS
brew install mise

# Other platforms: https://mise.jdx.dev/getting-started.html

Activate mise

Add to your shell profile (~/.zshrc, ~/.bashrc, etc.):

eval "$(mise activate zsh)"  # or bash, fish, etc.

Install Dependencies

# Install Ruby and bundler versions defined in .tool-versions
mise install

# Install gem dependencies
mise run install
# or: bundle install

Development Tasks

# Run all tests (rubocop, unit tests, integration tests, docs)
mise run test
# or: bundle exec rake

# Run only unit tests
mise run test:unit
# or: bundle exec rake spec

# Run only integration tests
mise run test:integration
# or: bundle exec rake spec_integration

# Run all specs (unit + integration)
mise run test:all
# or: bundle exec rake spec_all

# Run tests across all Ruby versions (3.2, 3.3, 3.4, 4.0.1) using Docker
mise run test:matrix
# or: bundle exec rake test_matrix

# Build the gem
mise run build
# or: gem build pathspec.gemspec

The test:matrix task runs the full test suite across all supported Ruby versions in Docker containers, matching the CI environment. Integration tests cover the CLI executable (bin/pathspec-rb).

Contributing

Pull requests, bug reports, and feature requests welcome! 😄 I've tried to write exhaustive tests but who knows what cases I've missed.

Releasing

This is mainly a reminder to myself but the release process is:

  1. Make sure CI is passing
  2. Update the CHANGELOG with relevant changes to Gem consumers
  3. Update version in gemspec with correct SemVer bump for scope of changes
  4. Tag/release using GitHub UI and the Build & Push workflow should do the rest.