module PreCommit::Utils::StagedFiles
Constants
- BINARIES
- IGNORED_EXTENSIONS
- IMAGES
- SOURCE_FILES
Public Instance Methods
appears_binary?(filename)
click to toggle source
Make a best guess to determine if this is a binary file. This is not an exact science ;)
# File lib/pre-commit/utils/staged_files.rb, line 62 def appears_binary?(filename) size = File.size(filename) size > 1_000_000 || (size > 20 && binary?(filename)) end
ignore_extension?(filename)
click to toggle source
Try to bail out quickly based on filename.
If the extension is `.jpg` this is likely not a source code file. So let's not waste time checking to see if it's “binary” (as best we can guess) and let's not run any checks on it.
# File lib/pre-commit/utils/staged_files.rb, line 56 def ignore_extension?(filename) IGNORED_EXTENSIONS.include?(File.extname(filename)) end
repo_ignored?(filename)
click to toggle source
# File lib/pre-commit/utils/staged_files.rb, line 67 def repo_ignored?(filename) repo_ignores.any? { |ignore| File.fnmatch?(ignore, filename) } end
set_staged_files(*args)
click to toggle source
# File lib/pre-commit/utils/staged_files.rb, line 21 def set_staged_files(*args) case args[0].to_s when "all" then staged_files_all when "git" then staged_files_git_all when "" then staged_files else self.staged_files=args end end
source_file?(filename)
click to toggle source
Definitely include this file in the checks.
# File lib/pre-commit/utils/staged_files.rb, line 47 def source_file?(filename) SOURCE_FILES.include?(File.extname(filename)) end
staged_files()
click to toggle source
# File lib/pre-commit/utils/staged_files.rb, line 34 def staged_files @staged_files ||= filter_files(staged_from_git) end
staged_files=(args)
click to toggle source
# File lib/pre-commit/utils/staged_files.rb, line 30 def staged_files=(args) @staged_files = args end
staged_files_all()
click to toggle source
# File lib/pre-commit/utils/staged_files.rb, line 38 def staged_files_all @staged_files = filter_files(staged_from_dir) end
staged_files_git_all()
click to toggle source
# File lib/pre-commit/utils/staged_files.rb, line 42 def staged_files_git_all @staged_files = filter_files(staged_from_git_all) end
Private Instance Methods
binary?(file)
click to toggle source
from github.com/djberg96/ptools/blob/master/lib/ptools.rb#L90
# File lib/pre-commit/utils/staged_files.rb, line 73 def binary?(file) bytes = File.stat(file).blksize.to_i bytes = 4096 if bytes > 4096 s = (File.read(file, bytes) || "").split(//) ((s.size - s.grep(" ".."~").size) / s.size.to_f) > 0.30 end
filter_files(files)
click to toggle source
# File lib/pre-commit/utils/staged_files.rb, line 81 def filter_files(files) first_pass = files.reject do |file| repo_ignored?(file) || ignore_extension?(file) || File.directory?(file) || !File.exists?(file) end # If it's a source file, definitely check it. # Otherwise, attempt to guess if the file is binary or not. first_pass.select do |file| source_file?(file) || !appears_binary?(file) end end
repo_ignores()
click to toggle source
# File lib/pre-commit/utils/staged_files.rb, line 112 def repo_ignores @repo_ignores ||= File.read(repo_ignores_file).split("\n").compact rescue Errno::ENOENT @repo_ignores = [] end
repo_ignores_file()
click to toggle source
# File lib/pre-commit/utils/staged_files.rb, line 108 def repo_ignores_file File.join(top_level, ".pre_commit.ignore") end
staged_from_dir()
click to toggle source
# File lib/pre-commit/utils/staged_files.rb, line 104 def staged_from_dir Dir["**/*"] end
staged_from_git()
click to toggle source
# File lib/pre-commit/utils/staged_files.rb, line 96 def staged_from_git `git diff --cached --name-only --diff-filter=ACM`.split(/\n/) end
staged_from_git_all()
click to toggle source
# File lib/pre-commit/utils/staged_files.rb, line 100 def staged_from_git_all `git ls-files`.split(/\n/) end