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.
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。
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
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.
These misplaced views can be found by scanning the contents of the interface builder file with a simple grep:
Mode: misplaced = “yes”
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:
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 can
git diff-index -p -M --cached HEADand
grep '^+'Use together.
#!/usr/bin/env bash set -eu failed=0 test_pattern='\b(fdescribe|fit|fcontext|xdescribe|xit|xcontext)\b' if git diff-index -p -M --cached HEAD -- '*Tests.swift' '*Specs.swift' | grep '^+' | egrep "$test_pattern" >/dev/null 2>&1 then 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 failed=1 fi misplaced_pattern='misplaced="YES"' if git diff-index -p -M --cached HEAD -- '*.xib' '*.storyboard' | grep '^+' | egrep "$misplaced_pattern" >/dev/null 2>&1 then echo "COMMIT REJECTED for misplaced views. Correct them before committing." >&2 echo '----' >&2 git grep -E "$misplaced_pattern" '*.xib' '*.storyboard' >&2 echo '----' >&2 failed=1 fi 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/pre-commit.sh .git/hooks/pre-commit
If there is an error, it exits the shell and writes an error message to the standard error if no variables are set.
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)
Original author: Krzysztof Zab ł ocki
Lipu Suyun leapcloud team of Translation Department_ UX R & D member: Alex sun
About the translator: IOS development
Wechat official account: maxleap_ yidongyanfa