Sha256: 80540b47737795e60cb856562e1eddc90c571e1d7923f2ca5e7e9bcb752d3523

Contents?: true

Size: 1012 Bytes

Versions: 2

Compression:

Stored size: 1012 Bytes

Contents

# frozen_string_literal: true

module Guard
  module Git
    # Only matches files that both:
    #
    # * Match the passed-in pattern AND
    # * Are either ignored by git or identified as changed
    class ChangedFilesMatcher
      def initialize(pattern)
        @pattern = pattern
      end

      def match(filename_or_pathname)
        path = filename_or_pathname.to_s
        result = @pattern.match(path)
        return nil if result.nil? || skip_file?(path)

        result
      end

      private

      def skip_file?(path)
        !changed?(path) && !always_allowed?(path)
      end

      def changed?(path)
        changed_files.include?(path)
      end

      def always_allowed?(path)
        ignored_files.include?(path)
      end

      def ignored_files
        `git ls-files --others -i --exclude-standard`.lines.map(&:chomp)
      end

      def changed_files
        `git status --porcelain`.lines.map do |line|
          line.chomp.split(' ').last
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
guard-git-0.1.1 lib/guard/git/changed_files_matcher.rb
guard-git-0.1.0 lib/guard/git/changed_files_matcher.rb