gpt4 book ai didi

git - 在 Git 中运行预提交 Hook 。有没有办法验证脚本是否正在运行?

转载 作者:行者123 更新时间:2023-12-05 00:21:47 25 4
gpt4 key购买 nike

我想运行一个 Git,按照博客的建议,我使用了 $git init 来初始化存储库,然后在 hooks 所在的位置创建了一个 .git 文件夹存在于 hooks 目录中。然后按照脚本的建议,我将 pre-commit.sample 重命名为 pre-commit 它没有用,所以我将它重命名为 pre-commit.sh 它仍然没有'它似乎在后台运行,或者我至少不知道它在后台运行。

因此,为了了解它是否在后台运行,我使用了 echo 语句,但我从未出现过。感觉迷茫了,接下来我尝试在 powershell 中手动运行脚本 .\pre-commit.sh ,但控制台上没有打印 echo 语句。其一,我不知道即使 echo 语句成功通过使用用户的输入提要停止脚本的进度,控制台是否会显示。

所以请告诉我——如何设置预提交 Hook ?我看错程序了吗?我在这里错过了什么吗?请务必告诉我。

我使用的 Hook 是默认的预提交 Hook 实现,只是为了方便,我将它放在这里——希望如此

#!/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".

if git rev-parse --verify HEAD >/dev/null 2>&1
then
against=HEAD
else
# Initial commit: diff against an empty tree object
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi

# If you want to allow non-ASCII filenames set this variable to true.
allownonascii=$(git config --bool hooks.allownonascii)

# Redirect output to stderr.
exec 1>&2

# Cross platform projects tend to avoid non-ASCII filenames; prevent
# them from being added to the repository. We exploit the fact that the
# printable range starts at the space character and ends with tilde.
if [ "$allownonascii" != "true" ] &&
# Note that the use of brackets around a tr range is ok here, (it's
# even required, for portability to Solaris 10's /usr/bin/tr), since
# the square bracket bytes happen to fall in the designated range.
test $(git diff --cached --name-only --diff-filter=A -z $against |
LC_ALL=C tr -d '[ -~]\0' | wc -c) != 0
then
cat <<\EOF
Error: Attempt to add a non-ASCII file name.

This can cause problems if you want to work with people on other platforms.

To be portable it is advisable to rename the file.

If you know what you are doing you can disable this check using:

git config hooks.allownonascii true
EOF
exit 1
fi

# If there are whitespace errors, print the offending file names and fail.
exec git diff-index --check --cached $against --

最佳答案

首先,您可以添加一个简单的回显。

其次,脚本将由 msys bash-like shell 运行,因此您不能直接从 PowerShell Windows session 运行它。

第三,确保你确实向索引添加了一些元素,以便 git commit 有东西要提交(并且预提交 Hook 有理由被触发)。


dennis添加 in the comments :

  • it should be renamed as "pre-commit" and not "pre-commit.sh" and
  • the script does run and display the echo-ed statement on commandprompt / powershell.

关于git - 在 Git 中运行预提交 Hook 。有没有办法验证脚本是否正在运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25188028/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com