在Git中运行预提交钩子。有没有方法来验证脚本正在运行? [英] Run a precommit hook in Git. Is there a way to verify that the script is running?

查看:466
本文介绍了在Git中运行预提交钩子。有没有方法来验证脚本正在运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想运行一个Git,并按照博客的建议,我使用 $ git init 来初始化存储库,然后使用 .git 文件夹被创建在钩子目录中的钩子处。然后,如脚本所示,我将 pre-commit.sample重命名为pre-commit ,但它不起作用,因此将其重命名为 pre-commit。 sh 它似乎还没有在后台运行,或者我至少不知道它的确如此。

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



那么请告诉我 - 如何设置预提交钩子?我误解了程序吗?我在这里错过了什么吗?请让我知道。



我使用的钩子是一个默认的pre-commit钩子实现,只是为了方便我将它放在这里 - 希望它

 #!/ bin / sh 

#一个钩子脚本示例,用于验证将要发生的事情承诺。
#由不带参数的git commit调用。如果
#想要停止提交,那么在发出适当的消息后,钩子应该以非零状态退出
#。

#要启用此挂钩,请将此文件重命名为预先提交。

if git rev-parse --verify HEAD> / dev / null 2>& 1
then
against = HEAD
else
#初始提交:针对空树对象的差异
针对= 4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi

#如果您希望允许非ASCII文件名将此变量设置为true。
allownonascii = $(git config --bool hooks.allownonascii)

#将输出重定向到stderr。
exec 1>& 2

#跨平台项目倾向于避免非ASCII文件名;防止
#被添加到存储库中。我们利用
#可打印范围从空格字符开始并以波浪号结尾的事实。
if [$ allownonascii!=true]&&
#请注意,在tr范围内使用括号是可以的(为了便携到Solaris 10的/ usr / bin / tr,它是
#甚至是必需的),因为
#square括号字节落在指定范围内。
test $(git diff --cached --name-only --diff-filter = A -z $ against |
LC_ALL = C tr -d'[ - 〜] \'0'| wc -c)!= 0
然后
cat << \ EOF
错误:尝试添加非ASCII文件名。

如果您想与其他平台上的人员合作,可能会导致问题。

为了便于携带,建议重新命名文件。

如果您知道自己在做什么,可以使用以下命令禁用此检查:

git config hooks.allownonascii true
EOF
exit 1
fi

#如果有空白错误,则打印出错的文件名并失败。
exec git diff-index --check --cached $ against -


解决方法

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

第二,脚本将由 msys bash-like shell ,因此您无法直接从PowerShell Windows会话中运行它。



第三,确保你确实在索引中添加了一些元素,以便git提交可以提交某些内容(并且预提交的钩子可以触发原因)。






dennis 添加在评论中:



  • 它应该重命名为 pre-commit 而不是 precommit.sh 一个d

  • 脚本运行并在commandprompt / powershell上显示echo-ed语句。


I wanted to run a Git and as suggested by the blogs, I used $git init to initialize the repository and then a .git folder is created where the hooks are present in the hooks directory. Then as suggested by the script I renamed pre-commit.sample as pre-commit it didn't work so I renamed it as pre-commit.sh it still didn't seem to be running in the background or I atleast didn't know it did.

So, in order to have a clue as to whether it was running in the background I used an echo statement but I never showed up. Feeling lost, I next tried to manually run the script .\pre-commit.sh in powershell the echo statement was not printed on console. For one, I don't know if the console will be displayed even if the echo statement is successful by halting the progress of the script with a input feed from the user.

So please do tell me -- how to setup a pre-commit hook? Did I misread the procedure? Am I missing something here? Please do let me know.

The hook that I am using is a default pre-commit hook implementation by just for convenience I'll put it here -- hope it

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

解决方案

First, you can add a simple echo.

Second, the script will be run by the msys bash-like shell, so you can't run it directly from a PowerShell Windows session.

Third, make sure you did add some elements to the index in order for the git commit to have something to commit (and for the pre-commit hook to have a reason to be triggered).


dennis adds 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中运行预提交钩子。有没有方法来验证脚本正在运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆