Class: Overcommit::Hook::PreCommit::ExecutePermissions

Inherits:
Base
  • Object
show all
Defined in:
lib/overcommit/hook/pre_commit/execute_permissions.rb

Overview

Checks for files with execute permissions, which are usually not necessary in source code files (and are typically caused by a misconfigured editor assigning incorrect default permissions).

Protip: if you have some files that you want to allow execute permissions on, you can disable this hook for those files by using the ‘exclude` option on your .overcommit.yml file. Example:

ExecutePermissions:
  enabled: true
  exclude:
    - 'path/to/my/file/that/should/have/execute/permissions.sh'
    - 'directory/that/should/have/execute/permissions/**/*'

Instance Attribute Summary

Attributes inherited from Base

#config

Instance Method Summary collapse

Methods inherited from Base

#applicable_files, #command, #description, #enabled?, #excluded?, #execute, #execute_in_background, #flags, #in_path?, #included_files, #initialize, #name, #parallelize?, #processors, #quiet?, #required?, #required_executable, #required_libraries, #run?, #run_and_transform, #skip?

Constructor Details

This class inherits a constructor from Overcommit::Hook::Base

Instance Method Details

#runObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/overcommit/hook/pre_commit/execute_permissions.rb', line 18

def run
  file_modes = {}

  # We have to look in two places to determine the execute permissions of a
  # file. The first is the Git tree for currently known file modes of all
  # files, the second is the index for any staged changes to file modes.
  # Staged changes take priority if they exist.
  #
  # This complexity is necessary because this hook can be run in the RunAll
  # context, where there may be no staged changes but we stil want to check
  # the permissions.
  extract_from_git_tree(file_modes) unless initial_commit?
  extract_from_git_index(file_modes)

  file_modes.map do |file, mode|
    next unless execute_permissions?(mode)

    Overcommit::Hook::Message.new(
      :error,
      file,
      nil,
      "File #{file} has unnecessary execute permissions",
    )
  end.compact
end