Class: PreCommit::Utils

Inherits:
Object
  • Object
show all
Defined in:
lib/pre-commit/utils.rb

Class Method Summary collapse

Class Method Details

.binary?(file) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
15
# File 'lib/pre-commit/utils.rb', line 12

def self.binary?(file)
  s = (File.read(file, File.stat(file).blksize) || "").split(//)
  ((s.size - s.grep(" ".."~").size) / s.size.to_f) > 0.30
end

.grepObject



17
18
19
20
21
22
23
24
# File 'lib/pre-commit/utils.rb', line 17

def self.grep
  grep_version = `grep --version | head -n 1 | sed -e 's/^[^0-9.]*\([0-9.]*\)$/\1/'`
  if grep_version =~ /FreeBSD/
    "grep -EnIH"
  else
    "grep -PnIH"
  end
end

.staged_filesObject



4
5
6
7
8
9
# File 'lib/pre-commit/utils.rb', line 4

def self.staged_files
  @staged_files ||= begin
    files = `git diff --cached --name-only --diff-filter=ACM`.split
    files.reject { |f| size = File.size(f); size > 1_000_000 || (size > 20 && binary?(f)) }
  end
end