class PreCommit::Checks::Gpg

pre-commit gem plugin to verify GPG signatures when either the file or signature changes

Public Class Methods

description() click to toggle source

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

call(staged_files) click to toggle source

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

find_binary(binary) click to toggle source

@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
get_signature(file) click to toggle source

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
gpg_program() click to toggle source

@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
parse_error(errors, file) click to toggle source

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
run_check(file) click to toggle source

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