Visual Studio 无法运行预/后提交钩子: /usr/bin/env 'bash': 没有那个文件或目录。

6

我们有一个现有的带有 .Net 解决方案的 git 仓库。我们正在将 web 应用程序添加到其中。web 应用程序(angular)在特定的子文件夹中。

我们安装了一些预先和后续提交钩子,以确保文件被正确地检查。当使用 VS Code 或 Source Tree 提交时,一切正常。但当我尝试直接从 Visual Studio 提交某些内容时,我会收到以下错误:

/usr/bin/env: 'bash': No such file or directory

有没有办法让它起作用?

以下是我的钩子的参考:

post-checkout

#!/bin/sh
command -v git-lfs >/dev/null 2>&1 || { echo >&2 "\nThis repository is configured for Git LFS but 'git-lfs' was not found on your path. If you no longer wish to use Git LFS, remove this hook by deleting '.git/hooks/post-checkout'.\n"; exit 2; }
git lfs post-checkout "$@"

提交后处理

#!/bin/sh
command -v git-lfs >/dev/null 2>&1 || { echo >&2 "\nThis repository is configured for Git LFS but 'git-lfs' was not found on your path. If you no longer wish to use Git LFS, remove this hook by deleting '.git/hooks/post-commit'.\n"; exit 2; }
git lfs post-commit "$@"

合并后

#!/bin/sh
command -v git-lfs >/dev/null 2>&1 || { echo >&2 "\nThis repository is configured for Git LFS but 'git-lfs' was not found on your path. If you no longer wish to use Git LFS, remove this hook by deleting '.git/hooks/post-merge'.\n"; exit 2; }
git lfs post-merge "$@"

cd ./Src/Frontend
npx git-pull-run --pattern "package-lock.json" --command "npm install"

pre-commit

#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

cd ./Src/Frontend
npm run lint

pre-push

#!/bin/sh
command -v git-lfs >/dev/null 2>&1 || { echo >&2 "\nThis repository is configured for Git LFS but 'git-lfs' was not found on your path. If you no longer wish to use Git LFS, remove this hook by deleting '.git/hooks/pre-push'.\n"; exit 2; }
git lfs pre-push "$@"

在使用VS Code和VS提交代码的同时,是否还有一种使用预/后提交挂钩的方法呢?我该怎么做?

1个回答

7

首先检查您的%PATH%,其中应包含C:\Program Files\Git\bin,这是bash.exe所在的位置。

或者为了测试,修改shebang指令

#!C:/Program\ Files/Git/git-bash.exe

J4v4scr1pt在评论中补充道:

  • 找到了如何设置环境变量

  • 添加typicode / husky问题1038建议的内容

    只需将C:\ Program Files \ Git \ usr \ bin \ cygpath.exe添加到您的PATH环境变量中,它就可以完美地工作。

  • 声明:

    我将“C:\ Program Files \ Git \ usr \ bin \ ”添加到“userName的用户变量”中,其中“/usr/”起到了作用。
    而且我需要重新启动VS。


感谢您的回答。目前看来,我的PATH中有C:\Program files\Git\cmd,但没有bin。由于git.exe同时存在于cmd和bin目录中,应该先指定哪一个? - J4N
1
@J4N 两者都包含相同的 git.exe,所以没有关系。我通常先设置 bin,然后是 cmd。还有 C:\Program Files\Git\mingw64\libexec\git-core,因为它包含用于 HTTPS 认证的 git-credential-manager-core.exe(来自 GCM 项目)。 - VonC
我在cmd后面添加了路径,重新启动了VS,现在它可以工作了。我想我之前已经在使用git-credential-manager-core了,所以它可能已经在我的路径中了。谢谢! - J4N
@J4N 好的,干得好。 - VonC
1
@J4v4Scr1pt PATH设置示例(在Windows上):“git bash在Windows 10下无法执行vagrant version”。 - VonC
显示剩余4条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接