Xcode Project Git Hooks
Add some discipline in your source control procedure with git hooks.
Join the DZone community and get the full member experience.
Join For Freewhat 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:
precommit:
nofocusedexamples:
include: ['*tests/*.swift', '*tests/*.m']
enabled: true
description: 'checking for temporary focused examples'
# .git-hooks/pre_commit/no_focused_examples.rb
# check bdd specs for temporary focused examples
module overcommit::hook::precommit
class nofocusedexamples < base
def run
errors = []
focused = ['describe', 'context', 'it']
applicable_files.each do |file|
file.open(file, 'r').each_with_index do |line, index|
focused.each do |block_name|
if line.index("f#{block_name}(")
relative = pathname(file).relative_path_from(pathname(overcommit::utils.repo_root))
errors << "#{relative}:#{index} has focused #{block_name} block `f#{block_name}`"
end
end
end
end
return :fail, errors.join("\n") if errors.any?
:pass
end
end
end
to install:
-
update
.overcommit.yml
with the above lines -
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:
what hooks would you add for your xcode project?
Published at DZone with permission of Paul Zabelin. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments