Windows 上的 GIT Hooks 批处理

Jon*_*zzi 4 git batch-file githooks

我正在尝试创建一个 git hook 规则,只有在文件myexamplefile.txt被更改时才能进行提交。

我将我的文件设置.git\hooks\commit-msg为:

for /f "tokens=*" %i in ('git status ^| find /c "AssemblyInfo.cs"') do set x=%i
if %x% (
    echo "nice"
    exit 0
)
else (
    echo "it's bad"
    exit 1
)
Run Code Online (Sandbox Code Playgroud)

我在这段代码中工作了一段时间,但出现了几个错误,现在我得到:

第 28 行:意外标记附近的语法错误 `"tokens=*"'

我怎么写这个钩子?重要的是要注意我在 windows 环境中。

Von*_*onC 5

默认情况下,任何 git 挂钩都将在 bash 会话中执行,而不是在 CMD 会话中。
在 Windows 上,Git for Windows 嵌入式 bash 将解释钩子脚本。

所以尝试用 bash 重写你的钩子:

#!/bin/bash

f=$(git status | grep "AssemblyInfo.cs")
if [[ "${f}" != "" ]]; then
    echo "nice"
    exit 0
else
    echo "darn it"
    exit 1
fi
Run Code Online (Sandbox Code Playgroud)

  • 您也可以简单地从 bash 挂钩调用批处理文件,请参阅[此处](/sf/answers/3269115881/) 或[此处](https://www.gamedev.net/forums/topic /636258-git-hooks-on-windows-using-batch)。 (2认同)