require 'set'

module Overcommit::HookContext
  # Simulates a pre-commit context pretending that all files have been changed.
  #
  # This results in pre-commit hooks running against the entire repository,
  # which is useful for automated CI scripts.
  class RunAll < Base
    def modified_files
      all_files
    end

    # Returns all lines in the file since in this context the entire repo is
    # being scrutinized.
    #
    # @param file [String]
    # @return [Set]
    def modified_lines_in_file(file)
      @modified_lines_in_file ||= {}
      @modified_lines_in_file[file] ||= Set.new(1..(count_lines(file) + 1))
    end

    def hook_class_name
      'PreCommit'
    end

    def hook_type_name
      'pre_commit'
    end

    def hook_script_name
      'pre-commit'
    end

    private

    def all_files
      @all_files ||= Overcommit::GitRepo.all_files
    end

    def count_lines(file)
      result = Overcommit::Utils.execute(%w[wc -l] + [file])
      result.success? ? result.stdout.to_i : 0
    end
  end
end

require 'set'

module Overcommit::HookContext
  # Simulates a pre-commit context pretending that all files have been changed.
  #
  # This results in pre-commit hooks running against the entire repository,
  # which is useful for automated CI scripts.
  class RunAll < Base
    def modified_files
      @modified_files ||= Overcommit::GitRepo.all_files
    end

    # Returns all lines in the file since in this context the entire repo is
    # being scrutinized.
    #
    # @param file [String]
    # @return [Set]
    def modified_lines_in_file(file)
      @modified_lines_in_file ||= {}
      @modified_lines_in_file[file] ||= Set.new(1..(count_lines(file) + 1))
    end

    def hook_class_name
      'PreCommit'
    end

    def hook_type_name
      'pre_commit'
    end

    def hook_script_name
      'pre-commit'
    end

    private

    def count_lines(file)
      result = Overcommit::Utils.execute(%w[wc -l] + [file])
      result.success? ? result.stdout.to_i : 0
    end
  end
end

Remove unnecessary level of indirection from RunAll
Remove unnecessary level of indirection from RunAll

This extra hop doesn't benefit us in any particular way. Simplify.

Change-Id: Ib0bd984b93f0301862c356c7ab408fd9a2bc990b
Reviewed-on: http://gerrit.causes.com/46486
Tested-by: jenkins <d95b56ce41a2e1ac4cecdd398defd7414407cc08@brigade.com>
Reviewed-by: Shane da Silva <67fb7bfe39f841b16a1f86d3928acc568d12c716@brigade.com>
