Using pre commit hook in IOS development


Many people write unit tests and run the continuous integration server to check pull requests. We can also take advantage of things likeDangerSuch a powerful tool.

Using pre commit hook in IOS development

If we want to prevent some common errors at the earliest stage, we can usepre-commit hook

Note: Friends of the original authorSami Samhuri Improved his script, this article has also been updated. In addition, the structure of the translation has been adjusted and a brief description of pre commit has been added.The latest script is here

pre-commit hook

Sometimes some test code or test tools should not be submitted to the remote warehouse, so it is necessary to review the code before submission. Git supports a lot of hooks, which are some in$GIT-DIR/hooksThe script in the directory is triggered and executed by a specific event. The pre commit hook is triggered before submission and can be used to check whether there are errors in the code to be submitted.Here you can see a more detailed description of GIT hooks

Stop what we want

Misplaced views

You have submitted some views that are not in the right position. Do you plan to repair them in the future?

Sometimes Xcode is too eager and has multiple displays (retinal vs non retinal problems…), It’s easy to suddenly put things in the wrong place.

I did.

These misplaced views can be found by scanning the contents of the interface builder file with a simple grep:

  • Mode: misplaced = “yes”

  • File:Specs.swift .storyboard

Focused tests

imageKiwiperhapsQuickSuch a code base allows us to do centralized testing, which can speed up the development speed and is very useful.

But they should never be submitted, otherwise they may inadvertently change something, disable all other tests, and hide some serious problems.

We need to find fdescribe / fit / fccontext and other similar things in the test file:

  • pattern:(fdescribe|fit|fcontext|xdescribe|xit|xcontext)

  • File:*Specs.swift


We just need to confirmStagedIt’s good if the changes include the above things. Don’t check all the files, because it will be annoying when developing.

Fortunately, we cangit diff-index -p -M --cached HEADandgrep '^+'Use together.


#!/usr/bin/env bash
set -eu


if git diff-index -p -M --cached HEAD -- '*Tests.swift' '*Specs.swift' | grep '^+' | egrep "$test_pattern" >/dev/null 2>&1
  echo "COMMIT REJECTED for fdescribe/fit/fcontext/xdescribe/xit/xcontext." >&2
  echo "Remove focused and disabled tests before committing." >&2
  echo '----' >&2
  git grep -E "$test_pattern" '*Tests.swift' '*Specs.swift'  >&2
  echo '----' >&2


if git diff-index -p -M --cached HEAD -- '*.xib' '*.storyboard' | grep '^+' | egrep "$misplaced_pattern" >/dev/null 2>&1
  echo "COMMIT REJECTED for misplaced views. Correct them before committing." >&2
  echo '----' >&2
  git grep -E "$misplaced_pattern" '*.xib' '*.storyboard' >&2
  echo '----' >&2

exit $failed

You can also get the latest scripts here。 This script can be used on the command line and MacOS git client.

Keep hook synchronized in the team

Most of the applications are made by teams, so we want to install git hooks in every git repository. But this is not how git works, so what should we do? Answer: useSymbolic links

Most of the projects I’ve worked on are similar tobootstrap scriptTo load Carthage or do some other preparatory work.

The following simple bootstrap script allows you to install git hooks in your warehouse, making it easy for the whole team to keep synchronized.

#!/usr/bin/env bash
# Usage: scripts/bootstrap

set -eu

ln -s ../../scripts/ .git/hooks/pre-commit
  1. If there is an error, it exits the shell and writes an error message to the standard error if no variables are set.

  2. Create symbolic links between git internal git pre commit hook file and scripts in our warehouse.

This script assumespre-commit.shandbootstrapThe documents are in your warehouseScriptsUnder the folder.


The accrual hook provides us with a very simple way to prevent some common mistakes,

  • One line of code can be configured for everyone in the team

  • Synchronize across the team

  • Git tracks changes and is visible in PR

  • Implemented in script, you can execute it as build phase if you like (although I don’t)

Related reading
Share code in react web and native apps
React native’s first anniversary review
Rapid deployment of test driven development / debug environment

Author’s previous works
IOS – more lightweight appdelegate – service oriented design
Using flowcontrollers to improve IOS application architecture
Dynamic method analysis practice of Objective-C runtime

Author information
Original author: Krzysztof Zab ł ocki
Original link:…
Lipu Suyun leapcloud team of Translation Department_ UX R & D member: Alex sun
About the translator: IOS development
Wechat official account: maxleap_ yidongyanfa