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).

Instance Attribute Summary

Attributes inherited from Base

#config

Instance Method Summary collapse

Methods inherited from Base

#applicable_files, #command, #description, #enabled?, #execute, #execute_in_background, #flags, #in_path?, #initialize, #name, #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



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/overcommit/hook/pre_commit/execute_permissions.rb', line 6

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