Class: NonAsciiCharactersCheckHook

Inherits:
RubyGitHooks::Hook show all
Defined in:
lib/ruby_git_hooks/non_ascii.rb

Overview

This hook checks that commit message contains only ASCII characters.

Constant Summary collapse

Hook =
RubyGitHooks::Hook

Constants inherited from RubyGitHooks::Hook

RubyGitHooks::Hook::HOOK_INFO, RubyGitHooks::Hook::HOOK_TYPE_SETUP

Instance Method Summary collapse

Methods inherited from RubyGitHooks::Hook

get_hooks_to_run, initial_setup, register, run, run_as_specific_githook, #setup, shell!

Constructor Details

#initialize(options = {}) ⇒ NonAsciiCharactersCheckHook



10
11
# File 'lib/ruby_git_hooks/non_ascii.rb', line 10

def initialize(options = {})
end

Instance Method Details

#checkObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# 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