【问题标题】:Git pre-commit hook in Visual Studio 2022Visual Studio 2022 中的 Git 预提交挂钩
【发布时间】:2021-12-14 08:50:02
【问题描述】:

我正在尝试在 VS2022 的预提交挂钩中使用 dotnet 格式进行一些 linting。问题是,当我使用 VS Git gui 提交更改时,似乎没有调用 pre-commit 挂钩,即使它在我使用命令行时有效。因此,即使命令行正确取消了提交,如下所示,gui 也会错误地创建提交。

如何让 VS 实际使用预提交挂钩?我已经看到它正确使用了预推钩。

预提交:

#!/bin/sh
#
# An example hook script to verify what is about to be committed.
# Called by "git commit" with no arguments.  The hook should
# exit with non-zero status after issuing an appropriate message if
# it wants to stop the commit.
#
# To enable this hook, rename this file to "pre-commit".

echo >&2 "Running git format"
LC_ALL=C
# Select files to format
FILES=$(git diff --name-only --diff-filter=ACM "*.cs" | sed 's| |\\ |g')
[ -z "$FILES" ] && exit 0
# Format all selected files
echo "$FILES" | cat | xargs | sed -e 's/ /,/g' | xargs dotnet-format --files
# Add back the modified files to staging
echo "$FILES" | xargs git add
echo >&2 "Deliberate failure"
exit 1

命令行输出:

C:\Users\<username>\source\repos\WpfApp1>git commit -m "Test"
Running git format
xargs: dotnet-format: No such file or directory
Deliberate failure

【问题讨论】:

    标签: git visual-studio-2022


    【解决方案1】:

    我最近发现(我自己的 VS 预提交问题)Visual Studio 实现了它自己的 git 内部版本(包括 grepsed)。其中,不允许使用 xargs 。 Visual Studio 不会告诉你它失败了,只会提交。尝试从您的代码中删除任何 xargs 引用。另外,dotnet-format 也是自定义安装程序,所以不能通过 VS 运行。

    如果您想要所有声明的功能,将 VS 切换为使用系统安装的 git 可能会更好:Configure Visual Studio to use system-installed Git.exe

    为了排除故障/让您的代码正常工作,我建议您测试一下 Visual Studio 中是否允许使用某些命令。只需(临时)通过将如下代码放在预提交文件的顶部来测试命令,然后尝试从 Visual Studio 提交:

    xargs
    echo "This outputs in VS git error window"
    exit 1
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-10
      • 2010-12-22
      • 1970-01-01
      • 2019-12-25
      • 2015-09-10
      • 1970-01-01
      相关资源
      最近更新 更多