简体   繁体   English

在GitHub客户端的上下文中使用git pre-commit钩子

[英]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. 我为git创建了一个预提交脚本,它在通过命令行运行时工作正常。 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. 我希望预提交也可以通过GitHub客户端应用程序工作,但我不能让它工作。 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. 我再次重新安装了grunt cli并使用了全局'-g'标志,但这没有任何区别。 Any ideas how I can get the client to find grunt? 任何想法如何让客户找到咕噜声?

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. OS X上的GUI应用程序不会加载.bashrc / .bash_profile中的内容,这意味着它们不会有用户指定的$ PATH添加内容,例如/usr/local/bin ,这是grunt二进制文件的位置。 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" 您可以在预提交挂钩中指定完整路径或修复$ PATH,方法是在顶部注释后添加: PATH="/usr/local/bin:$PATH"

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. 如果您正在使用sourcetree(在Mac上)并且您有预提交和预推钩,请使用命令行打开sourcetree而不是直接打开它,使用以下命令。

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. 我相信它也适用于github应用程序。

If you want to run a script that gets your environment ( $PATH , etc) you should change the first line of your script from this: 如果你想运行一个获取环境的脚本( $PATH等),你应该改变脚本的第一行:

#!/bin/sh

to: 至:

#!/usr/bin/env sh

Then call grunt without the hard coded path. 然后在没有硬编码路径的情况下调用grunt

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. /usr/bin/env将获取脚本运行的用户的环境。 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 . 否则,你可能会找到很多逻辑,寻找可以通过依赖正确填充的$PATH避免的应用程序。

Sindre Sorhus' excellent answer: Sindre Sorhus的优秀答案:

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. OS X上的GUI应用程序不会加载.bashrc / .bash_profile中的内容,这意味着它们不会有用户指定的$ PATH添加内容,例如/usr/local/bin ,这是grunt二进制文件的位置。 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" 您可以在预提交挂钩中指定完整路径或修复$ PATH,方法是在顶部注释后添加: 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. 它将每个Node版本的Node模块存储在一个单独的文件中。 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. 这将检查NVM,如果存在,则加载它并使用它来查找当前使用的Node版本的节点模块的路径。

as a simple workaround specifying the full absolute path to grunt should work. 作为一个简单的解决方法,指定grunt的完整绝对路径应该工作。 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. 如果您需要设置更多的环境,则需要研究github应用程序如何为钩子构建环境。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM