Skip to content

Instantly share code, notes, and snippets.

@fukajun
Last active August 19, 2020 05:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fukajun/03a7635d34dc0acd8ab95855fbb73942 to your computer and use it in GitHub Desktop.
Save fukajun/03a7635d34dc0acd8ab95855fbb73942 to your computer and use it in GitHub Desktop.
pre-commit-check-zenkaku

Usage

install && update

invoke at git repository root

$ curl -o - https://gist.githubusercontent.com/fukajun/03a7635d34dc0acd8ab95855fbb73942/raw/install.sh | bash
curl https://gist.githubusercontent.com/fukajun/03a7635d34dc0acd8ab95855fbb73942/raw/pre-commit -o - > .git/hooks/pre-commit && chmod +x .git/hooks/pre-commit
#!/usr/bin/env ruby
def check
is_valid = true
check_target_files = `git diff-index --name-status HEAD | cut -c3-`.split("\n")
invalid_results = []
check_target_files.each do |file_path|
line_no = 1
begin
file = open(file_path)
file.readlines.each do |line|
if line.match(/\u3000/)
is_valid = false
invalid_results << "file_path:#{file_path} line:#{line_no} reason:zenkaku-space code:\"#{line.sub(/\n$/, '')}\""
end
line_no += 1
end
ensure
file.close if file
end
end
if invalid_results.size >= 1
puts '--------------------------------------'
puts 'This commit is including invalid line.'
puts '--------------------------------------'
puts "#{invalid_results.join("\n")}"
puts '--------------------------------------'
end
is_valid
end
invalid_exit_code = ENV['IGNORE_PRE_COMMIT_CHECK'] ? 0 : 1
exit(check ? 0 : invalid_exit_code)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment