What are git hooks?

Git has ability to run special script along with standard git commands. There are many uses for git hooks from enforcing code style before commit to deploying on push, etc. See more on http://githooks.com.

Git pre-commit hook

Git pre-commit hook can help developer accidentally committing temporary changes to the source code made for debugging or testing.

Example of git pre-commit hook

Xcode projects that use Specta or Quick can take advantage of focus tests. Focus tests allows to focus on a subset of a test suite. See how temporarily run a subset of focused examples in Quick. Focus tests speed up Test Driven Development as we can focus on one failing spec at a time. The only problem with focus tests, they require source code modification that we might commit and limit our ability to run full suite of tests. Here is a blog post on Git Pre-Commit Hooks and Specta’s Focused Examples. Unfortunately git hooks are not part of the repository. If we want to share git hooks on the project between multiple developers or even between multiple computers we have to install hooks on each clone.

Git hook manager

One way to share git hooks across multiple clones is to use tool like overcommit . Overcommit is “A fully configurable and extendable Git hook manager”. It adds .overcommit.yml to configure hooks and allows adding custom hooks to .git-hooks/ folder in the project root folder.

Overcommit and Xcode

To make overcommit available for Xcode inside IDE we have to install overcommit gem at a system level:

rvm system
gem install overcommit

If you are not using RVM - Ruby Version Manager you can skip first line and run this in shell:

gem install overcommit

Xcode pre-commit hook for Focused Examples

Here is the gist of a pre-commit hook that checks tests for temporary focused examples:

To install:

  1. update .overcommit.yml with the above lines
  2. save above script to .git-hooks/pre_commit/no_focused_examples.rb

Now when we try to commit a focused test we get an error message similar to:

Running pre-commit hooks
Checking for temporary focused examples...........[NoFocusedExamples] FAILED
UnitTests/SomeSpec.swift:108 has focused it block `fit`

✗ One or more pre-commit hooks failed
Same message will be shown by Xcode when we try to commit from IDE: Xcode error screenshot


What hooks would you add for your Xcode project? See how to add pre-commit hook to prevent boiler plate comments.