Git Hook manager "overcommit" is perfect!


overcommit is a fully configurable git hook manager.
This article explains some hooks which work in the following timings:

  • Pre-Commit

    • RuboCop: A Ruby static code analyzer
    • Brakeman: Vulnerability scanner
    • HamlLint: Style Checker for HAML
    • YamlSyntax: Style Checker for YAML
    • BundleCheck: Dependency Check
    • RailsSchemaUpToDate: Migration Check
    • HardTabs: Checks for hard tabs in files
  • PrePush

    • RSpec: Unit & Integration TEST
    • BundleInstall: bundle install checker

overcommit


๐Ÿ—ป Short Brief

If you want to create new Rails app project, you can use rails5_application_template
which includes the following configurations. Itโ€™s so easy, so I recommend to use the template!

๐Ÿ€ Gemfile

You should add the following gems and execute bundle install:

group :development do
# Syntax Checker
# hook event pre-commit, pre-push
gem 'overcommit', require: false

# A static analysis security vulnerability scanner
gem 'brakeman', require: false

# Syntax checker for HAML
gem 'haml-lint', require: false

# Syntax checker for CSS
gem 'ruby_css_lint', require: false

# A Ruby static code analyzer
gem 'rubocop', require: false
end

๐Ÿ˜€ Configuration of overcommit

I tried some hook tools and selected some effective tool.
If you agree with my recommended tools, please write the following configuration to .overcommit.yml:

PreCommit:
# Style Check
RuboCop:
enabled: true
command: ['bundle', 'exec', 'rubocop', '-c', './.rubocop.yml']
on_warn: fail

# Security Check
Brakeman:
enabled: true
command: ['bundle', 'exec', 'brakeman']
on_warn: fail

# Style Check in HAML
HamlLint:
enabled: true
command: ['bundle', 'exec', 'haml-lint', 'app/views/']
on_warn: fail

# YAML Check
YamlSyntax:
enabled: true
on_warn: fail

# Dependency Check
BundleCheck:
enabled: true
on_warn: fail

# Migration Check
RailsSchemaUpToDate:
enabled: true
on_warn: fail

# Checks for hard tabs in files
HardTabs:
enabled: true
on_warn: fail

PrePush:
# Unit & Integration TEST
RSpec:
enabled: true
command: ['bundle', 'exec', 'rspec', 'spec']
on_warn: fail

BundleInstall:
enabled: true
on_warn: fail

๐Ÿ Configuration for RuboCop

In my opinion, I really love a tool to improve my productivity.
So, I choose the configuration in RuboCop to improve programmerโ€™s productivity.
You can write the following configuration to .rubocop.yml:

AllCops:
TargetRubyVersion: 2.3
Exclude:
- bin/**/*
- config/boot.rb
- db/schema.rb
- script/**/*
- tmp/**/*
- vendor/**/*
- Rakefile
- Gemfile
- config/unicorn/*
DisplayCopNames: true

Rails:
Enabled: true

Style/AccessModifierIndentation:
Enabled: false

Style/AccessorMethodName:
Enabled: false

Style/Alias:
Enabled: false

Style/AlignArray:
Enabled: false

Style/AlignHash:
Enabled: false

Style/AlignParameters:
Enabled: false

Style/AndOr:
Enabled: true

Style/ArrayJoin:
Enabled: false

# Allow Asian comment
#Style/AsciiComments:
# Enabled: false

Style/AsciiIdentifiers:
Enabled: false

Style/Attr:
Enabled: false

Style/BeginBlock:
Enabled: false

Style/BarePercentLiterals:
Enabled: false

Style/BlockComments:
Enabled: false

Style/BlockEndNewline:
Enabled: false

Style/BracesAroundHashParameters:
Enabled: false

Style/CaseEquality:
Enabled: false

Style/CaseIndentation:
Enabled: false

Style/CharacterLiteral:
Enabled: false

Style/ClassAndModuleCamelCase:
Enabled: false

Style/ClassAndModuleChildren:
Enabled: false

Style/ClassCheck:
Enabled: false

Style/ClassMethods:
Enabled: false

Style/ClassVars:
Enabled: false

Style/ColonMethodCall:
Enabled: false

Style/CommentAnnotation:
Enabled: false

Style/CommentIndentation:
Enabled: false

Style/ConstantName:
Enabled: false

Style/DefWithParentheses:
Enabled: false

Style/DeprecatedHashMethods:
Enabled: false

Style/Documentation:
Enabled: false

Style/DotPosition:
Enabled: false

Style/DoubleNegation:
Enabled: false

Style/EachWithObject:
Enabled: false

Style/ElseAlignment:
Enabled: false

Style/EmptyElse:
Enabled: false

Style/EmptyLineBetweenDefs:
Enabled: false

Style/EmptyLines:
Enabled: false

Style/EmptyLinesAroundAccessModifier:
Enabled: false

Style/EmptyLinesAroundBlockBody:
Enabled: false

Style/EmptyLinesAroundClassBody:
Enabled: false

Style/EmptyLinesAroundModuleBody:
Enabled: false

Style/EmptyLinesAroundMethodBody:
Enabled: false

Style/EmptyLiteral:
Enabled: false

Style/EndBlock:
Enabled: false

Style/EndOfLine:
Enabled: false

Style/EvenOdd:
Enabled: false

Style/FileName:
Enabled: false

Style/FirstParameterIndentation:
Enabled: false

Style/FlipFlop:
Enabled: false

Style/For:
Enabled: false

Style/FormatString:
Enabled: false

Style/GlobalVars:
Enabled: true

Style/GuardClause:
Enabled: false

Style/HashSyntax:
Enabled: false

Style/IfUnlessModifier:
Enabled: false

Style/IfWithSemicolon:
Enabled: false

Style/IndentationConsistency:
Enabled: false

Style/IndentationWidth:
Enabled: false

Style/IndentArray:
Enabled: false

Style/IndentHash:
Enabled: false

Style/InfiniteLoop:
Enabled: false

Style/Lambda:
Enabled: false

Style/LambdaCall:
Enabled: false

Style/LeadingCommentSpace:
Enabled: false

Style/LineEndConcatenation:
Enabled: false

Style/MethodCallParentheses:
Enabled: false

Style/MethodDefParentheses:
Enabled: false

Style/MethodName:
Enabled: false

Style/ModuleFunction:
Enabled: false

Style/MultilineBlockChain:
Enabled: false

Style/MultilineBlockLayout:
Enabled: false

Style/MultilineIfThen:
Enabled: false

Style/MultilineOperationIndentation:
Enabled: false

Style/MultilineTernaryOperator:
Enabled: false

Style/NegatedIf:
Enabled: false

Style/NegatedWhile:
Enabled: false

Style/NestedTernaryOperator:
Enabled: false

Style/Next:
Enabled: false

Style/NilComparison:
Enabled: false

Style/NonNilCheck:
Enabled: false

Style/Not:
Enabled: false

Style/NumericLiterals:
Enabled: false

Style/OneLineConditional:
Enabled: false

Style/OpMethod:
Enabled: false

Style/ParenthesesAroundCondition:
Enabled: false

Style/PercentLiteralDelimiters:
Enabled: false

Style/PercentQLiterals:
Enabled: false

Style/PerlBackrefs:
Enabled: false

Style/PredicateName:
Enabled: false

Style/Proc:
Enabled: false

Style/RaiseArgs:
Enabled: false

Style/RedundantBegin:
Enabled: false

Style/RedundantException:
Enabled: false

Style/RedundantReturn:
Enabled: false

Style/RedundantSelf:
Enabled: false

Style/RegexpLiteral:
Enabled: false

Style/RescueModifier:
Enabled: false

Style/SelfAssignment:
Enabled: false

Style/Semicolon:
Enabled: false

Style/SignalException:
Enabled: false

Style/SingleLineBlockParams:
Enabled: false

Style/SingleLineMethods:
Enabled: false

Style/SpaceAfterColon:
Enabled: false

Style/SpaceAfterComma:
Enabled: false

Style/SpaceAfterControlKeyword:
Enabled: false

Style/SpaceAfterMethodName:
Enabled: false

Style/SpaceAfterNot:
Enabled: false

Style/SpaceAfterSemicolon:
Enabled: false

Style/SpaceBeforeBlockBraces:
Enabled: false

Style/SpaceBeforeComma:
Enabled: false

Style/SpaceBeforeComment:
Enabled: false

Style/SpaceBeforeSemicolon:
Enabled: false

Style/SpaceInsideBlockBraces:
Enabled: false

Style/SpaceAroundBlockParameters:
Enabled: false

Style/SpaceAroundEqualsInParameterDefault:
Enabled: false

Style/SpaceAroundOperators:
Enabled: false

Style/SpaceBeforeModifierKeyword:
Enabled: false

Style/SpaceInsideBrackets:
Enabled: false

Style/SpaceInsideHashLiteralBraces:
Enabled: false

Style/SpaceInsideParens:
Enabled: false

Style/SpaceInsideRangeLiteral:
Enabled: false

Style/SpecialGlobalVars:
Enabled: false

Style/StringLiterals:
Enabled: false

Style/StringLiteralsInInterpolation:
Enabled: false

Style/StructInheritance:
Enabled: false

Style/SymbolProc:
Enabled: false

Style/Tab:
Enabled: false

Style/TrailingBlankLines:
Enabled: false

Style/TrailingCommaInLiteral:
Enabled: false

Style/TrailingWhitespace:
Enabled: false

Style/TrivialAccessors:
Enabled: false

Style/UnlessElse:
Enabled: false

Style/UnneededCapitalW:
Enabled: false

Style/UnneededPercentQ:
Enabled: false

Style/VariableInterpolation:
Enabled: false

Style/VariableName:
Enabled: false

Style/WhenThen:
Enabled: false

Style/WhileUntilDo:
Enabled: false

Style/WhileUntilModifier:
Enabled: false

Style/WordArray:
Enabled: false

Style/FrozenStringLiteralComment:
Enabled: false

Lint/DeprecatedClassMethods:
Enabled: false

Lint/StringConversionInInterpolation:
Enabled: false

Metrics/AbcSize:
Enabled: false

Metrics/BlockNesting:
Enabled: false

Metrics/ClassLength:
Enabled: false

Metrics/CyclomaticComplexity:
Enabled: false

Metrics/LineLength:
Enabled: false

Metrics/MethodLength:
Enabled: false

Metrics/ParameterLists:
Enabled: false

Metrics/PerceivedComplexity:
Enabled: false

Style/BlockDelimiters:
Enabled:

๐Ÿ  Configuration for haml-lint

Please write the following configuration to .haml-lint.yml:

linters:
LineLength:
max: 200

๐ŸŽƒ Running overcommit

Letโ€™s run overcommit:

# Commit change .overyml
bundle exec overcommit --sign

# Run overcommit
bundle exec overcommit --run

After finishing to set the above configuration, the hooks in git-commit or git-push will work.
Letโ€™s enjoy programming!

๐ŸšŒ Tips

If you want to ignore, a warning of overcommit, please add OVERCOMMIT_DISABLE=1 to your command like this:

OVERCOMMIT_DISABLE=1 GIT_COMMAND

๐Ÿ–ฅ Recommended VPS Service

VULTR provides high performance cloud compute environment for you. Vultr has 15 data-centers strategically placed around the globe, you can use a VPS with 512 MB memory for just $ 2.5 / month ($ 0.004 / hour). In addition, Vultr is up to 4 times faster than the competition, so please check it => Check Benchmark Results!!