highb / pathspec-ruby

Match Path Specifications, such as .gitignore, in Ruby!
Apache License 2.0
23 stars 9 forks source link

How to implement a deny list? #53

Open bastelfreak opened 2 weeks ago

bastelfreak commented 2 weeks ago

Hi! I'm struggling to implement something and I hope you could help me out. I've some ruby code that parses a file with .gitignore style filters and afterward creates a tar archive:

https://github.com/puppetlabs/puppet-modulebuilder/blob/main/lib/puppet/modulebuilder/builder.rb#L251-L268 (ignore_file is set to .pmtigore) -> https://github.com/puppetlabs/puppet-modulebuilder/blob/main/lib/puppet/modulebuilder/builder.rb#L149-L153 -> https://github.com/puppetlabs/puppet-modulebuilder/blob/main/lib/puppet/modulebuilder/builder.rb#L76-L92

Current .pmtignore is something like this:

/docs/
/pkg/
/Gemfile
/Gemfile.lock
/Gemfile.local
/vendor/
/.vendor/
/spec/
/Rakefile
/.vagrant/
/.bundle/
/.ruby-version
/coverage/
/log/
/.idea/
/.dependencies/
/.github/
/.librarian/
/Puppetfile.lock
*.iml
/.editorconfig
/.fixtures.yml
/.gitignore
/.msync.yml
/.overcommit.yml
/.pmtignore
/.rspec
/.rspec_parallel
/.rubocop.yml
/.sync.yml
.*.sw?
/.yardoc/
/.yardopts
/Dockerfile
/HISTORY.md

And I need to switch from an denylist to an allowlist, roughly:

*
!/manifests
!/README*
!/metadata.json
!/LICENSE
!/hiera.yaml
!/data
!/templates
!/files
!/CHANGELOG*
!/docs
!/REFERENCE.md
!/locales
!/scripts
!/tasks
!/plans
!/types
!/bolt_plugin.json

So I want to exclude everything except for the directories/files prefixed with a !. When I use this new filter I always end up with some empty directories that aren't supposed to be there:

.
├── CHANGELOG.md
├── examples
├── files
├── lib
│   ├── facter
│   ├── puppet
│   │   ├── functions
│   │   │   └── archive
│   │   ├── provider
│   │   │   └── archive
│   │   └── type
│   └── puppet_x
│       └── bodeco
├── LICENSE
├── manifests
├── metadata.json
├── README.md
├── REFERENCE.md
└── spec
    ├── acceptance
    ├── classes
    ├── defines
    ├── fixtures
    │   ├── checksum
    │   └── manifests
    ├── functions
    ├── hosts
    ├── support
    └── unit
        ├── facter
        ├── puppet
        │   ├── provider
        │   │   └── archive
        │   └── type
        └── puppet_x
            └── bodeco
ekohl commented 2 weeks ago

As suggested in https://github.com/puppetlabs/puppet-modulebuilder/pull/79#discussion_r1641286658 I think your first line needs to be recursive: ** instead of *.