Module: PreCommit::Utils::StagedFiles

Includes:
Configuration::TopLevel
Included in:
Runner
Defined in:
lib/pre-commit/utils/staged_files.rb

Constant Summary collapse

BINARIES =
[
  ".dmg", ".gz", ".img", ".iso", ".rar", ".tar", ".xz", ".zip"
].freeze
IMAGES =
[".gif", ".ico", ".jpeg", ".jpg", ".pdf", ".png"].freeze
IGNORED_EXTENSIONS =
BINARIES + IMAGES
SOURCE_FILES =
[
  ".coffee", ".css", ".erb", ".feature", ".html", ".js", ".json",
  ".liquid", ".md", ".rb", ".sass", ".scss", ".slim", ".yml"
].freeze

Instance Method Summary collapse

Methods included from Configuration::TopLevel

#top_level

Instance Method Details

#appears_binary?(filename) ⇒ Boolean

Make a best guess to determine if this is a binary file. This is not an exact science ;)

Returns:

  • (Boolean)

62
63
64
65
# 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) ⇒ Boolean

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.

Returns:

  • (Boolean)

56
57
58
# File 'lib/pre-commit/utils/staged_files.rb', line 56

def ignore_extension?(filename)
  IGNORED_EXTENSIONS.include?(File.extname(filename))
end

#repo_ignored?(filename) ⇒ Boolean

Returns:

  • (Boolean)

67
68
69
# 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) ⇒ Object


21
22
23
24
25
26
27
28
# 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) ⇒ Boolean

Definitely include this file in the checks.

Returns:

  • (Boolean)

47
48
49
# File 'lib/pre-commit/utils/staged_files.rb', line 47

def source_file?(filename)
  SOURCE_FILES.include?(File.extname(filename))
end

#staged_filesObject


34
35
36
# File 'lib/pre-commit/utils/staged_files.rb', line 34

def staged_files
  @staged_files ||= filter_files(staged_from_git)
end

#staged_files=(args) ⇒ Object


30
31
32
# File 'lib/pre-commit/utils/staged_files.rb', line 30

def staged_files=(args)
  @staged_files = args
end

#staged_files_allObject


38
39
40
# 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_allObject


42
43
44
# File 'lib/pre-commit/utils/staged_files.rb', line 42

def staged_files_git_all
  @staged_files = filter_files(staged_from_git_all)
end