Using git pre-commit hooks in context of GitHub client

ChrisCast picture ChrisCast · Jul 9, 2013 · Viewed 8.7k times · Source

I created a pre-commit script for git, which is working fine when run via command line. Here's that script:

#!/bin/sh
#
# Pre-commit hooks
echo "Running unit tests..."

# Lint stuff before commiting
grunt runtests
RESULT=$?

[ $RESULT -ne 0 ] && echo "Tests (or tasks) failed, aborting the commit" && exit 1

echo "All tests passed, commiting your changes" && exit 0

I'd would like the pre-commit to also work via the GitHub client application, but I can't get that working. The pre-commit script is executed, but it throws an error. Here's the full text it returns in the client alert window:

Running unit tests...
.git/hooks/pre-commit: line 7: grunt: command not found
Tests (or tasks) failed, aborting the commit
 (1)

For some reason, it is not able to find grunt. I've reinstalled the grunt cli again and used the global '-g' flag, but that made no difference. Any ideas how I can get the client to find grunt?

Answer

Sindre Sorhus picture Sindre Sorhus · Jul 9, 2013

GUI apps on OS X doesn't load the stuff in .bashrc/.bash_profile, which means they won't have user specified $PATH additions like /usr/local/bin, which is where the grunt binary is. You can either specify the full path or fix the $PATH in your pre-commit hook, by adding this after the top comments: PATH="/usr/local/bin:$PATH"