Git 预提交钩子在 GitHub for mac 中失败(在命令行上工作)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12881975/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 07:38:02  来源:igfitidea点击:

Git pre-commit hook failing in GitHub for mac (works on command line)

gitgithubatlassian-sourcetreegithub-for-mac

提问by isNaN1247

I've created a very simple pre-commit script:-

我创建了一个非常简单的预提交脚本:-

#!/usr/bin/env sh

# Run tests
npm test
if [ $? -ne 0 ]; then
  echo "Tests failed. Aborting.."
  exit 1
fi

exit 0


When I run git commitat the command line (with failing tests), I get the expected exit 1 with the message Tests failed. Aborting...

当我git commit在命令行上运行(测试失败)时,我得到了预期的 exit 1 消息Tests failed. Aborting..

However, If I use GitHub for Mac however I get:

但是,如果我使用 GitHub for Mac 但是我得到:

.git/hooks/pre-commit: line 5: npm: command not found
Tests failed. Aborting..
 (256)

I'm guessing its down to npmnot being available to the execution environment that GitHub for Mac is using, but I've been tearing my hair out trying to work out how to fix this.

我猜这npm是因为 GitHub for Mac 正在使用的执行环境无法使用,但我一直在努力研究如何解决这个问题。

回答by isNaN1247

Resolved. As globally installed node modules end up under /usr/local/binI simply needed to add the following at the beginning of my pre-commit:

解决。由于全局安装的节点模块最终在/usr/local/bin我之下,我只需要在我的预提交开始时添加以下内容:

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

i.e. appending both /usr/local/binand /usr/local/sbinto PATHat the point of execution.

即既追加/usr/local/bin/usr/local/sbinPATH在执行点。

回答by RustyToms

The $PATH variable that is available in GUI environments like Github Desktop and Sourcetree is different than the one available in the terminal. By default the $PATH available in the GUI environments can't find your node modules. As was stated in the previous answer, you can ensure that /usr/local/bin is in the path by adding

在 Github Desktop 和 Sourcetree 等 GUI 环境中可用的 $PATH 变量与在终端中可用的变量不同。默认情况下,GUI 环境中可用的 $PATH 找不到您的节点模块。正如上一个答案中所述,您可以通过添加来确保 /usr/local/bin 在路径中

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

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:

在我的情况下,这不起作用,因为我使用的是Node Version Manager,它存储不同版本的 Node 并使升级和切换 Node 版本变得容易。它将每个 Node 版本的 node_modules 存储在一个单独的文件中。这是我用来解决这个问题的代码:

#!/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. If you are only trying to access node modules and don't need to get at anything special you can skip adding the sbin folder, e.g. /usr/local/sbin

这会检查 NVM,如果存在,则加载它并使用它来查找当前使用的 Node.js 版本的节点模块的路径。如果您只是尝试访问节点模块并且不需要获取任何特殊内容,则可以跳过添加sbin 文件夹,例如/usr/local/sbin

回答by Brianna Lee

For me it was that I didn't have a dependency installed that was being called from pre-commit. In my case this was composer, so a brew install composerhelped me out.

对我来说,我没有安装从预提交调用的依赖项。就我而言,这是composer,所以brew install composer帮助了我。

For future peeps, check if you are missing any dependancies being called in your pre-commit file by opening your-project-directory/.git/hooks/pre-commitin your favorite text editor and install missing the dependancies as needed.

对于未来的窥视,通过your-project-directory/.git/hooks/pre-commit在您喜欢的文本编辑器中打开并根据需要安装缺少的依赖项,检查您是否缺少在预提交文件中调用的任何依赖项。