【问题标题】:npm installed pre-push hook linked to a custom script is always skipped始终跳过链接到自定义脚本的 npm 安装的 pre-push 挂钩
【发布时间】:2016-04-16 04:57:09
【问题描述】:

我想阻止所有开发人员执行 git push to master(--force 除外)。我不想要求每个人在 .git/hooks 文件夹中创建一个预推送文件,而是想使用 npm 自动化该过程并将所需的文件添加到远程 git 存储库。

我在 package.json 中添加了指向自定义脚本的“预推送”,

"pre-push": {
  "run": [
    "prepushscript"
  ]
},

"scripts": {
  "start": "node app.js",
  "test": "node_modules/.bin/gulp test",
  "prepushscript": "prepushscript"
},

"devDependencies": {
  .
  .
  "pre-push": "^0.1.0",
  .
  .
}

注意:自定义脚本 prepushscript 与 package.json 处于同一文件级别。

prepushscript 文件相当简单

protected_branch='master'  
current_branch=$(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,')

if [ $protected_branch = $current_branch ]  
then  
    echo "Error: You are attempting to push to a protected branch."
exit 1 # push will not execute
fi

自定义脚本有效,因为在 .git/hooks 文件夹中创建本地预推送文件并尝试推送到受保护的分支会导致看到脚本的回显错误消息。 Npm install 也会在 .git/hooks 文件夹中创建符号链接。一旦我准备好在本地主机上推送并运行 git push 的提交,推送就会通过,我得到以下信息,

pre-push:
pre-push: Failed to find the root of this git repository, cannot locate the `package.json`.
pre-push: Skipping the pre-push hook.
pre-push:
Everything up-to-date

我尝试用 package.json 中的简单 echo 语句替换 prepushscript 条目,以查看脚本是否有问题,但我得到了相同的控制台日志。进行的其他尝试是向 prepushscript 文件添加扩展名以查看它是否运行。

我无法弄清楚的主要问题是我无法找到错误的来源以及它与 package.json 的关系。感谢您的帮助!

【问题讨论】:

    标签: git npm hook package.json


    【解决方案1】:

    我在使用“pre-push”npm 包时遇到了同样的错误。似乎我的窗口没有带空格的 git 完整路径,例如“程序文件”。最后我不得不选择“git-pre-push”。

    【讨论】:

      【解决方案2】:

      这条线失败了:

       this.root = this.exec(this.git, ['rev-parse', '--show-toplevel']);
      

      https://github.com/dflourusso/pre-push/blob/master/index.js#L173

      这是试图获取'顶级'目录的绝对路径。

      this.git 包含 git 可执行文件的路径。使用 git GUI 并使用自己的嵌入式可执行文件时会出现问题。我在使用 SourceTree 时遇到了这个问题并更改了我的首选项,以便它使用系统可执行文件:

      SourceTree > 首选项 > Git > 使用系统 Git(Git 版本)

      如果您使用不同的 GUI,请转到其首选项并确保它使用的是系统可执行文件。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-05-28
        • 2021-02-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-10-20
        • 2017-11-03
        • 2022-08-05
        相关资源
        最近更新 更多