Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using git pre-commit hooks in context of GitHub client

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?

like image 975
ChrisCast Avatar asked Jul 09 '13 01:07

ChrisCast


2 Answers

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"

like image 139
Sindre Sorhus Avatar answered Nov 03 '22 11:11

Sindre Sorhus


If you are using sourcetree (on Mac) and you have pre-commit and pre-push hooks, open sourcetree with command line instead of opening it directly, with following command.

open /Applications/SourceTree.app/Contents/MacOS/SourceTree

Now your hooks will work when you try to commit and push. I am sure it work for github app as well.

like image 27
Aamir Afridi Avatar answered Nov 03 '22 09:11

Aamir Afridi