class NonAsciiCharactersCheckHook

This hook checks that commit message contains only ASCII characters.

Constants

Hook

applypatch-msg, pre-applypatch, post-applypatch prepare-commit-msg, commit-msg pre-rebase, post-checkout, post-merge, update, post-update, pre-auto-gc, post-rewrite

Public Class Methods

new(options = {}) click to toggle source
# File lib/ruby_git_hooks/non_ascii.rb, line 10
def initialize(options = {})
end

Public Instance Methods

check() click to toggle source
# File lib/ruby_git_hooks/non_ascii.rb, line 13
def check
  if !commit_message || commit_message.length == 0
    STDERR.puts "Commit message is missing or empty!"
    return false
  end

  # Brute force approach. I didn't find any clever way to check for non-ascii
  # using string encoder tricks
  count = 0
  valid_control_chars = [13, 10, 9]
  commit_message.each_byte do |b|
    if b > 127 || (b < 32 && !valid_control_chars.include?(b))
      count = count + 1
    end
  end
  if count > 0
    STDERR.puts "Commit message has #{count} non-ASCII characters"
  end
  return count == 0 ? true : false
end