Skip to content

Instantly share code, notes, and snippets.

@vlasikhin
Created September 28, 2018 09:58
Show Gist options
  • Save vlasikhin/69159a5afe612ccb25f68210c22ea7c7 to your computer and use it in GitHub Desktop.
Save vlasikhin/69159a5afe612ccb25f68210c22ea7c7 to your computer and use it in GitHub Desktop.
# сохраняем файл в корень проекта
# добавляем ссылку в гит:
# ln -s ../../rubocop_pre_commit_hook.rb .git/hooks/pre-commit
# проверяем добавился ли файл:
# ls -al .git/hooks/pre-commit
# должно быть что то типа:
# Aug 11 13:17 .git/hooks/pre-commit -> ../../rubocop_pre_commit_hook.rb
# даем права:
# chmod +x .git/hooks/pre-commit
# проверяем:
# .git/hooks/pre-commit
# Теперь перед каждым коммитом автоматически будет запускаться rubocop
#!/usr/bin/env ruby
ADDED_OR_MODIFIED = /^\s*(A|AM|M)/.freeze
changed_files = `git status --porcelain`.split(/\n/)
unstaged_files = `git ls-files -m`.split(/\n/)
changed_files = changed_files.select { |f| f =~ ADDED_OR_MODIFIED }
changed_files = changed_files.map { |f| f.split(" ")[1] }
changed_files -= (unstaged_files - changed_files)
changed_files = changed_files.select { |file_name| File.extname(file_name) == ".rb" }
changed_files = changed_files.join(" ")
exit(0) if changed_files.empty?
success = system(%(
rubocop #{changed_files}
))
STDIN.reopen('/dev/tty')
if success == false
puts "Would you like to continue press 'any key' or 'n/N' to halt? "
exit(1) if %w(N n).include?(gets.chomp)
end
@amkisko
Copy link

amkisko commented Mar 6, 2020

Спасибо за крутой хак STDIN.reopen('/dev/tty').

Моя реализация pre-commit скрипта: https://gist.github.com/amkisko/126461451ab3e8ca8401287466dd48e7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment