git - 将 PowerShell 脚本作为 git hooks 运行

标签 git powershell githooks

是否可以将 PowerShell 脚本作为 git hooks 运行?

我在 PowerShell 提示符下运行 git,这应该没有任何区别,但我似乎无法让它们工作,因为 Hook 的命名没有扩展名,而 PowerShell 需要(据我所知).ps1 扩展名.我不确定这是问题所在,还是其他原因。

最佳答案

powershell 7.2该脚本在 Windows 上必须具有 .ps1 扩展名。所以这个答案是行不通的。


您可以将 PowerShell 脚本直接嵌入 Hook 文件中。这是我使用的 pre-commit Hook 的示例:

#!/usr/bin/env pwsh

# Verify user's Git config has appropriate email address
if ($env:GIT_AUTHOR_EMAIL -notmatch '@(non\.)?acme\.com$') {
    Write-Warning "Your Git email address '$env:GIT_AUTHOR_EMAIL' is not configured correctly."
    Write-Warning "It should end with '@acme.com' or '@non.acme.com'."
    Write-Warning "Use the command: 'git config --global user.email <name@acme.com>' to set it correctly."
    exit 1
}

exit 0

此示例需要 PowerShell Core,但因此它将跨平台运行(假设此文件在 Linux/macOS 上已被 chmod +x)。

关于git - 将 PowerShell 脚本作为 git hooks 运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5629261/

相关文章:

linux - 亚搏体育appGitLab的密码

regex - 正则表达式在Powershell中不起作用

Powershell从rabbitmqctl获取输出并解析异常

git Hook : is there a clone hook?

git - 有没有在 jenkins pipeline groovy 脚本中调用 git clean 的正确方法?

git - 如何在 Git 中回滚到之前的版本?

git - 如何在 commit-msg 钩子(Hook)中捕获 git commit 消息?

git - 我可以在 Gitlab 网络编辑器提交后触发 git hook 吗?

git - 从弹性 beanstalk 部署中排除目录

powershell - 有没有办法限制Powershell中多文本框中的最大行数?