buff-ignore-1.2.0/0000755000004100000410000000000012760063603013750 5ustar www-datawww-databuff-ignore-1.2.0/Rakefile0000644000004100000410000000027612760063603015422 0ustar www-datawww-datarequire 'bundler/gem_tasks' require 'rspec/core/rake_task' RSpec::Core::RakeTask.new do |t| t.rspec_opts = [ '--color', '--format progress', ].join(' ') end task default: :spec buff-ignore-1.2.0/Gemfile0000644000004100000410000000017112760063603015242 0ustar www-datawww-datasource 'https://rubygems.org' gemspec group :workstation do gem 'yard', '~> 0.8' gem 'redcarpet', '~> 3.0' end buff-ignore-1.2.0/spec/0000755000004100000410000000000012760063603014702 5ustar www-datawww-databuff-ignore-1.2.0/spec/spec_helper.rb0000644000004100000410000000117312760063603017522 0ustar www-datawww-datarequire 'bundler/setup' require 'spork' Spork.prefork do require 'rspec' # Require all supporting libraries Dir['spec/support/**/*.rb'].each { |f| require File.expand_path(f) } # RSpec configuration RSpec.configure do |config| config.include Buff::Ignore::RSpec::PathHelpers config.expect_with(:rspec) { |c| c.syntax = :expect } config.mock_with(:rspec) config.treat_symbols_as_metadata_keys_with_true_values = true config.filter_run(focus: true) config.run_all_when_everything_filtered = true config.before(:each) { clean_tmp_path! } end end Spork.each_run do require 'buff/ignore' end buff-ignore-1.2.0/spec/lib/0000755000004100000410000000000012760063603015450 5ustar www-datawww-databuff-ignore-1.2.0/spec/lib/ignore_file_spec.rb0000644000004100000410000000430012760063603021266 0ustar www-datawww-datarequire 'spec_helper' describe Buff::Ignore::IgnoreFile do let(:ignores) { %w(Gemfile *.lock bacon README.*) } let(:path) { '~/fakepath/fakefile' } before do File.stub(:exists?).with(path).and_return(true) File.stub(:readlines).and_return(ignores) end subject { described_class.new(path) } describe '.initialize' do context 'when the filepath is nil' do it 'raises an exception' do expect { described_class.new(nil) }.to raise_error(Buff::Ignore::IgnoreFileNotFound) end end context 'when the filepath does not exist' do before { File.stub(:exists?).and_return(false) } it 'raises an exception' do expect { described_class.new('/lol/wtf?/really/no.rb') }.to raise_error(Buff::Ignore::IgnoreFileNotFound) end end context 'when the file exists' do subject { described_class.new(path) } it 'sets the filepath instance variable to the expanded path' do filepath = subject.instance_variable_get(:@filepath) expect(filepath).to eq(File.expand_path(path)) end end end describe '#apply' do let(:list) { ['Gemfile', 'Gemfile.lock', 'bacon', 'eggs'] } it 'leaves the original array unmodified' do original = list.dup subject.apply(list) expect(list).to eq(original) end it 'delegates to #apply!' do subject.should_receive(:apply!).with(list).once subject.apply(list) end end describe '#apply!' do let(:list) { ['Gemfile', 'Gemfile.lock', 'bacon', 'eggs', File.expand_path('~') + '/fakepath/README.md'] } it 'removes standard files' do subject.apply!(list) expect(list).to_not include('Gemfile') expect(list).to_not include('bacon') end it 'removes the globs' do subject.apply!(list) expect(list).to_not include('Gemfile.lock') expect(list).to_not include(File.expand_path('~') + '/fakepath/README.md') end it 'does not remove a non-matching pattern' do subject.apply!(list) expect(list).to include('eggs') end it 'returns nil if nothing was removed' do result = subject.apply!([]) expect(result).to be_nil end end end buff-ignore-1.2.0/spec/lib/errors_spec.rb0000644000004100000410000000110012760063603020313 0ustar www-datawww-datarequire 'spec_helper' describe Buff::Ignore::BuffIgnoreError do it 'inherits from StandardError' do expect(subject).to be_a(StandardError) end end describe Buff::Ignore::IgnoreFileNotFound do let(:path) { '/path/to/file' } subject { described_class.new(path) } it 'accepts a filepath as the parameter' do expect { described_class.new(path) }.to_not raise_error end context 'when path is nil' do let(:path) { nil } it 'has the correct message' do expect(subject.message).to eq("No ignore file found at ''!") end end end buff-ignore-1.2.0/spec/support/0000755000004100000410000000000012760063603016416 5ustar www-datawww-databuff-ignore-1.2.0/spec/support/path_helpers.rb0000644000004100000410000000114212760063603021417 0ustar www-datawww-datamodule Buff::Ignore module RSpec module PathHelpers # The tmp path where testing support/workspaces are # # @return [Pathname] def tmp_path @_tmp_path ||= app_root.join('tmp').expand_path end private # The "root" of berkshelf # # @return [Pathname] def app_root @_app_root ||= Pathname.new(File.expand_path('../../..', __FILE__)) end # Clean the temporary directories def clean_tmp_path! FileUtils.rm_rf(tmp_path) FileUtils.mkdir_p(tmp_path) end end end end buff-ignore-1.2.0/.travis.yml0000644000004100000410000000014412760063603016060 0ustar www-datawww-datasudo: false language: ruby rvm: - 2.1.9 - 2.2.5 - 2.3.1 bundler_args: --without workstation buff-ignore-1.2.0/lib/0000755000004100000410000000000012760063603014516 5ustar www-datawww-databuff-ignore-1.2.0/lib/buff/0000755000004100000410000000000012760063603015440 5ustar www-datawww-databuff-ignore-1.2.0/lib/buff/ignore.rb0000644000004100000410000000025412760063603017251 0ustar www-datawww-data# :nodoc: module Buff # @author Seth Vargo module Ignore require_relative 'ignore/errors' require_relative 'ignore/ignore_file' end end buff-ignore-1.2.0/lib/buff/ignore/0000755000004100000410000000000012760063603016723 5ustar www-datawww-databuff-ignore-1.2.0/lib/buff/ignore/ignore_file.rb0000644000004100000410000000542512760063603021540 0ustar www-datawww-datamodule Buff module Ignore # A Ruby representation of an ignore file class IgnoreFile # Regular expression to match comments or plain whitespace # # @return [Regexp] COMMENT_OR_WHITESPACE = /^\s*(?:#.*)?$/.freeze # The path to the ignore file # # @return [String] attr_reader :filepath # Create a new ignore file from the given filepath # # @raise [IgnoreFileNotFound] # if the given filepath does not exist # # @param [String, Pathname] filepath # the path to the ignore file # @param [Hash] options # a list of options to pass to the ignore file # # @option [#to_s] options :base # the base directory to apply ignores from def initialize(filepath, options = {}) raise IgnoreFileNotFound.new(filepath) unless filepath && File.exists?(filepath) @filepath = File.expand_path(filepath) @options = options if @options[:base].nil? @options[:base] = File.directory?(filepath) ? filepath : File.dirname(filepath) end end # Apply the ignore to the list, returning a new list of filtered files # # @example # files = ['Gemfile', 'Gemfile.lock', 'bacon', 'eggs'] # ignore.apply(files) #=> ['bacon', 'eggs'] # # @see IgnoreFile#apply! # # @param [Array] list # the list of files to apply the ignore to # # @return [Array] # the sanitized file list def apply(list) tmp = list.dup apply!(tmp) tmp end # Destructively remove all files from the given list # # @param [Array] list # the list of files to apply the ignore to # # @return [Array, nil] # the elements removed, or nil if none were removed def apply!(list) list.reject! do |item| item.strip.empty? || ignored?(item) end end # Determine if a given filename should be ignored # # @param [String] filename # the file to match # # @return [Boolean] # true if the file should be ignored, false otherwise def ignored?(filename) base = File.expand_path(options[:base] || File.dirname(filepath)) basename = filename.sub(base + File::SEPARATOR, '') ignores.any? { |ignore| File.fnmatch?(ignore, basename) } end private # The list of options # # @return [Hash] attr_reader :options # The parsed contents of the ignore file # # @return [Array] def ignores @ignores ||= File.readlines(filepath).map(&:strip).reject do |line| line.empty? || line =~ COMMENT_OR_WHITESPACE end end end end end buff-ignore-1.2.0/lib/buff/ignore/errors.rb0000644000004100000410000000077112760063603020571 0ustar www-datawww-datamodule Buff::Ignore # @abstract Exceptions from Buff::Ignore class BuffIgnoreError < StandardError; end # Raised when an ignore file cannot be found class IgnoreFileNotFound < BuffIgnoreError # @param [String] path # the path where the ignore file was not found def initialize(path) @path = path end # @return [String] def to_s "No ignore file found at '#{File.expand_path(@path)}'!" rescue "No ignore file found at '#{@path}'!" end end end buff-ignore-1.2.0/lib/buff/ignore/version.rb0000644000004100000410000000013612760063603020735 0ustar www-datawww-datamodule Buff module Ignore # The version of Buff::Ignore VERSION = '1.2.0' end end buff-ignore-1.2.0/.gitignore0000644000004100000410000000023212760063603015735 0ustar www-datawww-data*.gem *.rbc .bundle .config .yardoc Gemfile.lock InstalledFiles _yardoc coverage doc/ lib/bundler/man pkg rdoc spec/reports test/tmp test/version_tmp tmp buff-ignore-1.2.0/buff-ignore.gemspec0000644000004100000410000000221312760063603017516 0ustar www-datawww-data# coding: utf-8 lib = File.expand_path('../lib', __FILE__) $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) require 'buff/ignore/version' Gem::Specification.new do |spec| spec.name = 'buff-ignore' spec.version = Buff::Ignore::VERSION spec.authors = ['Seth Vargo'] spec.email = ['sethvargo@gmail.com'] spec.description = 'Parse ignore files with Ruby' spec.summary = 'A Ruby library for parsing lists of files and applying pattern matching exclusion (such as .gitignore)' spec.homepage = 'https://github.com/sethvargo/buff-ignore' spec.license = 'Apache 2.0' spec.files = `git ls-files`.split($/) spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) spec.require_paths = ['lib'] spec.required_ruby_version = '>= 2.1' spec.add_development_dependency 'bundler', '~> 1.3' spec.add_development_dependency 'cane', '~> 2.6' spec.add_development_dependency 'rake' spec.add_development_dependency 'rspec', '~> 2.13' spec.add_development_dependency 'spork', '~> 0.9' end buff-ignore-1.2.0/LICENSE0000644000004100000410000002514712760063603014766 0ustar www-datawww-data Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright 2013 Seth Vargo Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. buff-ignore-1.2.0/CHANGELOG.md0000644000004100000410000000110212760063603015553 0ustar www-datawww-data# CHANGELOG ## v1.2.0 - Require Ruby 2.1 - Fix the ignore file not being properly loaded ## v1.1.1 - Handle nil file paths ## v1.1.0 - Make `ignored?` a public function ## v1.0.4 - **Critical Fix** - strip all values before fnmatching - Accept a `:base` argument and only parse relative to the base ## v1.0.3 - Use `#to_s` instead of message for a nicer output to the end-user when an ignore file is not found ## v1.0.2 - Only accept a Pathname or String as the primary argument ## v1.0.1 - Use `VERSION` constant - Add CHANGELOG ## v1.0.0 - _(initial release)_ buff-ignore-1.2.0/README.md0000644000004100000410000000453212760063603015233 0ustar www-datawww-data# Buff Ignore [![Gem Version](https://badge.fury.io/rb/buff-ignore.svg)](http://badge.fury.io/rb/buff-ignore) [![Build Status](https://travis-ci.org/sethvargo/buff-ignore.svg)](https://travis-ci.org/sethvargo/buff-ignore) [![Dependency Status](https://gemnasium.com/sethvargo/buff-ignore.svg)](https://gemnasium.com/sethvargo/buff-ignore) [![Code Climate](https://codeclimate.com/github/sethvargo/buff-ignore.svg)](https://codeclimate.com/github/sethvargo/buff-ignore) Buff::Ignore is a Ruby helper library for parsing and managing an ignore file (such as a `.gitignore` or `chefignore`). It uses [`File#fnmatch`](http://www.ruby-doc.org/core-2.0/File.html#method-c-fnmatch). It includes helpful methods for apply ignores to a file list. ## Installation Add buff-ignore to your `Gemfile`: ```gemfile gem 'buff-ignore' ``` And then execute the `bundle` command to install: ``` $ bundle ``` Or install buff-ignore directly: ``` $ gem install buff-ignore ``` ## Usage Buff::Ignore is designed to be used as a library. First, you must require it: ```ruby require 'buff/ignore' ``` Next, create an instance of an ignore file: ```ruby ignore = Buff::Ignore::IgnoreFile.new('/path/to/ignore/file') ``` _(If the file does not exist, an exception will be raised)_ Finally, apply the `ignore` to a list of files: ```ruby list = Dir['**/*'] result = ignore.apply(list) ``` You can also destructively apply changes. This will modify the receiving argument `list`: ```ruby ignore.apply!(list) ``` ## Contributing 1. Fork it 2. Create your feature branch (`git checkout -b my-new-feature`) 3. Commit your changes (`git commit -am 'Add some feature'`) 4. Push to the branch (`git push origin my-new-feature`) 5. Create new Pull Request ## License & Authors - Author: Seth Vargo (sethvargo@gmail.com) ```text Copyright 2013 Seth Vargo Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ```