class PathSpec

Attributes

specs[R]

Public Class Methods

from_filename(filename, type=:git) click to toggle source

Generate specs from a filename, such as a .gitignore

# File lib/pathspec.rb, line 77
def self.from_filename(filename, type=:git)
  self.from_lines(File.open(filename, 'r'))
end
from_lines(lines, type=:git) click to toggle source
# File lib/pathspec.rb, line 81
def self.from_lines(lines, type=:git)
  self.new lines, type
end
new(lines=nil, type=:git) click to toggle source
# File lib/pathspec.rb, line 9
def initialize(lines=nil, type=:git)
  @specs = []

  if lines
    add(lines, type)
  end

  self
end

Public Instance Methods

add(obj, type=:git) click to toggle source

Generate specs from lines of text

# File lib/pathspec.rb, line 86
def add(obj, type=:git)
  spec_class = spec_type(type)

  if obj.respond_to?(:each_line)
    obj.each_line do |l|
      spec = spec_class.new(l.rstrip)

      if !spec.regex.nil? && !spec.inclusive?.nil?
        @specs << spec
      end
    end
  elsif obj.respond_to?(:each)
    obj.each do |l|
      add(l, type)
    end
  else
    raise 'Cannot make Pathspec from non-string/non-enumerable object.'
  end

  self
end
drive_letter_to_path(path) click to toggle source
# File lib/pathspec.rb, line 72
def drive_letter_to_path(path)
  path.gsub(/^([a-zA-z]):\//, '/\1/')
end
empty?() click to toggle source
# File lib/pathspec.rb, line 108
def empty?
  @specs.empty?
end
match(path) click to toggle source

Check if a path matches the pathspecs described Returns true if there are matches and none are excluded Returns false if there aren't matches or none are included

# File lib/pathspec.rb, line 22
def match(path)
  matches = specs_matching(path.to_s)
  !matches.empty? && matches.all? {|m| m.inclusive?}
end
match_path(path, root='/') click to toggle source
# File lib/pathspec.rb, line 52
def match_path(path, root='/')
  rootpath = Pathname.new(drive_letter_to_path(root))
  relpath = Pathname.new(drive_letter_to_path(path)).relative_path_from(rootpath).to_s
  relpath = relpath + '/' if path[-1].chr == '/'

  match(relpath)
end
match_paths(paths, root='/') click to toggle source
# File lib/pathspec.rb, line 60
def match_paths(paths, root='/')
  matching = []

  paths.each do |path|
    if match_path(path, root)
      matching << path
    end
  end

  matching
end
match_tree(root) click to toggle source

Check if any files in a given directory or subdirectories match the specs Returns matched paths or nil if no paths matched

# File lib/pathspec.rb, line 37
def match_tree(root)
  rootpath = Pathname.new(root)
  matching = []

  Find.find(root) do |path|
    relpath = Pathname.new(path).relative_path_from(rootpath).to_s
    relpath += '/' if File.directory? path
    if match(relpath)
      matching << path
    end
  end

  matching
end
spec_type(type) click to toggle source
# File lib/pathspec.rb, line 112
def spec_type(type)
  if type == :git
    GitIgnoreSpec
  elsif type == :regex
    RegexSpec
  else
    raise "Unknown spec type #{type}"
  end
end
specs_matching(path) click to toggle source
# File lib/pathspec.rb, line 27
def specs_matching(path)
  @specs.select do |spec|
    if spec.match(path)
      spec
    end
  end
end