Skip to content

Instantly share code, notes, and snippets.

@damaon
Created January 15, 2016 15:44
Show Gist options
  • Save damaon/f34f4ddea64b404075dc to your computer and use it in GitHub Desktop.
Save damaon/f34f4ddea64b404075dc to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# If errors spotted return true
def check_file_size!(path, file)
if file.lines.size > 150
puts "[FILE SIZE][#{path}] MAX: 150 lines"
return true
end
end
# if error spotted return true
def check_lines_length!(path, file)
any_errors = false
file.lines.each_with_index do |line, index|
next unless line.size > 100
error_msg = "MAX: 100 chars"
puts "[LINE LENGTH][#{path}:#{index}] #{error_msg}"
any_errors = true
end
return any_errors
end
def check_files_commited
puts "[pre-commit] Checking files commited ..."
any_errors = false
git_root = `git rev-parse --show-toplevel`.strip
changed_files_paths = `git diff --cached --name-only --diff-filter=ACM`.split("\n").map(&:strip)
changed_files_paths.each do |path|
next if path.size == 0
full_path = File.join(git_root, path)
file = File.read(full_path)
any_errors ||= check_file_size!(path, file)
any_errors ||= check_lines_length!(path, file)
end
if any_errors
puts "[pre-commit] Fix errors above before commiting changes"
exit 1
else
puts "[pre-commit] GJ!"
end
end
check_files_commited
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment