git - Powershell 在 TortoiseGit Hook 中不返回任何内容

标签 git powershell hook tortoisegit pre-commit

我正在为 git 开发简单的钩子(Hook)。 如果在 Windows 计算机上打开 Acrobat,则应取消提交/推送。 由于 Git Hook 无法直接找到 Windows 进程,我正在运行下一个名为 pre-commit.ps1

的 Powershell 脚本

UPD 到目前为止还没有解决方案。 Powershell exit 1 在 GitBash 上工作正常,但在通过 TortoiseGit 调用时被忽略。它在 2014 年 8 月声明为修复,但对我来说看起来并非如此

参见https://gitlab.com/tortoisegit/tortoisegit/issues/2143

我使用的是 2015 年 9 月起的 TortoiseGit 1.8.15.2

$acrobat = Get-Process Acrobat -ErrorAction SilentlyContinue
if ($acrobat) {
 "Acrobat is Running"
  exit 1
} else {
 "Not running"
  exit 0
}
Remove-Variable acrobat

我将名为 pre-commit 的文件放置在项目的 .git\hooks 文件夹中。 它有下一个内容

#!/bin/sh
# An  hook script to verify that Acrobat is closed. 


powershell.exe -ExecutionPolicy RemoteSigned -File   '.\.\.\common\external\hooks\pre-commit.ps1'
ret=$?;
#echo "Acrobat check: $ret"
if [ $ret == 1 ]
then
    echo "Commit canceled: Acrobat is open"
    exit 1
fi
exit 0

问题是: 当我直接运行 pre-commit.ps1 时,一旦 Acrobat 打开,它会给出正确的返回 1,如果未打开,则返回 0。 但在 hook 中它总是给我 0。

您能给我提示一下这里有什么问题吗? 或者可能有更简单的解决方案在没有 Powershell 的情况下在 git hook 中返回 1 ?

最佳答案

该脚本有很多问题。

首先,您实际上并没有为 runString 变量分配任何内容。赋值语句的 = 两侧不能有空格。您的任务应该如下所示:

runString="C:/Windows/System32/WindowsPowerShell/v1.0/powershell.exe -ExecutionPolicy RemoteSigned -Command -File 'C:/project/common/external/hooks/pre-commit.ps1'"

假设您的作业正确,您就没有正确运行它。 exec 将当前 shell 脚本替换为该行上的命令,以便您的脚本将使用 powershell 命令替换自身,并且不会运行该行之后的代码。

综上所述,假设 powershell.exe 退出并返回脚本的返回状态,那么您的脚本可能比这简单得多。

这应该有效:

#!/bin/sh
# A hook script to verify that Acrobat is closed.
exec C:/Windows/System32/WindowsPowerShell/v1.0/powershell.exe -ExecutionPolicy RemoteSigned -File 'C:/project/common/external/hooks/pre-commit.ps1'

关于git - Powershell 在 TortoiseGit Hook 中不返回任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32893280/

相关文章:

C#:如何在按下鼠标左/右键时获取鼠标的坐标?

git - 如何从 git repo 中删除一个文件夹?

GitHub - 查找与提交关联的 pull 请求

powershell - 打开和关闭 RDP 窗口

powershell 远程安装 msi 失败

Webpack-dev-server 插件 Tap hooks

git - 如何指定在 git 中使用哪个 SSH key 进行 git push 以便将 gitorious 作为镜像?

python - "Error while running ' $ python manage.py collectstatic --noinput '"在 django 上更改数据库后

Git 管道命令找出给定版本中哪些文件已更改

csv - 基于 CSV 内容的 Powershell 复制文件