Domanda

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?

È stato utile?

Soluzione

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"

Altri suggerimenti

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.

If you want to run a script that gets your environment ($PATH, etc) you should change the first line of your script from this:

#!/bin/sh

to:

#!/usr/bin/env sh

Then call grunt without the hard coded path.

This way if the path to your executable changes in the future or is different on other machines the script will still work. /usr/bin/env will get the environment of the user that the script is running as. This is really helpful in places where some people use different package managers but need to run the same scripts. Otherwise you could end up with a lot of logic looking for applications that could've been avoided by depending on a properly populated $PATH.

Sindre Sorhus' excellent answer:

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"

In my case this did not work because I am using Node Version Manager, which stores different versions of Node and makes it easy to upgrade and switch Node versions. It stores your Node modules for each version of Node in a separate file. Here is the code I used to get around this problem:

#!/usr/bin/env bash

PATH="/usr/local/bin:$PATH"

if [ -f $HOME/.nvm/nvm.sh ]
then
  . $HOME/.nvm/nvm.sh
  PATH="$HOME/.nvm/versions/node/$(nvm current)/bin:$PATH"
fi

This checks for NVM, and if it exists, loads it and uses it to find the path to the node modules for the currently used version of Node.

as a simple workaround specifying the full absolute path to grunt should work. if you need more of your environment to be set up you need to investigate how the github application builds the environment for the hooks.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top