Visual studio 2019 git 预提交挂钩忽略 perl 命令

Visual studio 2019 git pre-commit hook is ignoring perl command

我为我的项目创建了预提交挂钩。如果我通过命令行或 Git 扩展 GUI 提交更改,它工作正常。但是,如果我通过 Visual studio 提交我的更改,由于某种原因,perl 命令会以某种方式被忽略,什么也不做,输出文件是相同的。

我正在尝试删除 .json 文件中的一些行,如果它与我的正则表达式匹配。这是我在预提交挂钩中使用的脚本。

for file in $(git diff --cached --name-only)
do
    if [[ $file == "path/to/my/file"* ]]; then
        file="./${file}"
        perl -i -pne 'BEGIN {undef $/} s/^\s*"id":.*?\r?\n//img' $file
        perl -i -pne 'BEGIN {undef $/} s/,(\s*})//isg' $file
        git add $file
    fi
done

似乎 Visual studio 有自己的 Git 实现,它没有 perl 命令,所以它被忽略了。有 2 种可能的解决方案:

  • 正在安装官方 Git 到 VS 安装目录 (Configure Visual Studio to use system-installed Git.exe)
  • 使用 sed 或其他命令替换所需的字符串