Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Xcode Project Git Hooks

DZone's Guide to

Xcode Project Git Hooks

Add some discipline in your source control procedure with git hooks.

· Mobile Zone
Free Resource

Launching an app doesn’t need to be daunting. Whether you’re just getting started or need a refresher on mobile app testing best practices, this guide is your resource! Brought to you in partnership with Perfecto

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:

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:

  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?

Keep up with the latest DevTest Jargon with the latest Mobile DevTest Dictionary. Brought to you in partnership with Perfecto.

Topics:
xcode ,git

Published at DZone with permission of Paul Zabelin. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}