class PreCommit::Checks::Gpg
pre-commit gem plugin to verify GPG signatures when either the file or signature changes
Public Class Methods
description of the plugin
# File lib/plugins/pre_commit/checks/gpg.rb, line 24 def self.description "Finds GPG verification problems" end
Public Instance Methods
Finds files with signature and verifies them
@param staged_files [Array<String>] list of files to check
@return [nil|Array<PreCommit::ErrorList>] nil when no errors,
list of errors otherwise
# File lib/plugins/pre_commit/checks/gpg.rb, line 35 def call(staged_files) signature_files = staged_files.map { |file| get_signature(file) }.compact.uniq return if signature_files.empty? errors = signature_files.map { |file| run_check(file) }.compact return if errors.empty? errors end
Private Instance Methods
@param binary [String] the name of binary to find on PATH
@return [nil|String] path to the searched binary or nil
# File lib/plugins/pre_commit/checks/gpg.rb, line 116 def find_binary(binary) result = execute_raw( "which #{binary}", :success_status => false ) and result.strip end
Checks
if the given file is a signature or has one
@param file [String] the file to check
@return [nil|String] signature file when found, nil otherwise
# File lib/plugins/pre_commit/checks/gpg.rb, line 54 def get_signature(file) if File.exist?(file + ".asc") then file + ".asc" elsif File.extname(file) == ".asc" && File.exist?(file.sub(/.asc$/, "")) then file end end
@return [nil|String] path to the GPG binary or nil
# File lib/plugins/pre_commit/checks/gpg.rb, line 107 def gpg_program @gpg_program ||= find_binary(:gpg2) || find_binary(:gpg) end
convert verification failure string into ErrorList
@param errors [String] Output of failed GPG verification to parse @param file [String] File that versification failed
@return [nil|PreCommit::ErrorList] nil when file verified,
ErrorList when verification failed
# File lib/plugins/pre_commit/checks/gpg.rb, line 95 def parse_error(errors, file) return if errors.nil? PreCommit::ErrorList.new( errors.split(/\n/).map do |error| PreCommit::Line.new(error, file) end ) end
Verify given file GPG signature
@param file [String] path to file to verify
@return [nil|PreCommit::ErrorList] nil when file verified,
ErrorList when no GPG found to verify ErrorList when verification failed
# File lib/plugins/pre_commit/checks/gpg.rb, line 76 def run_check(file) if gpg_program then parse_error( execute(gpg_program, "--verify", file), file ) else PreCommit::ErrorList.new(PreCommit::Line.new("No GPG program found to run verification", file)) end end