是否可以将 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/