我如何在commit-msg钩子中提示用户? [英] How do I prompt the user from within a commit-msg hook?
问题描述
我想警告用户他们的提交消息是否不遵循一定的指导原则,然后给他们编辑提交消息,忽略警告或取消提交的选项。问题是我似乎没有访问标准输入。
I want to warn the user if their commit message doesn't follow a certain set of guidelines, and then give them the option to edit their commit message, ignore the warning, or cancel the commit. The problem is that I don't seem to have access to stdin.
以下是我的提交msg文件:
Below is my commit-msg file:
function verify_info {
if [ -z "$(grep '$2:.*[a-zA-Z]' $1)" ]
then
echo >&2 $2 information should not be omitted
local_editor=`git config --get core.editor`
if [ -z "${local_editor}" ]
then
local_editor=${EDITOR}
fi
echo "Do you want to"
select CHOICE in "edit the commit message" "ignore this warning" "cancel the commit"; do
case ${CHOICE} in
i*) echo "Warning ignored"
;;
e*) ${local_editor} $1
verify_info "$1" $2
;;
*) echo "CHOICE = ${CHOICE}"
exit 1
;;
esac
done
fi
}
verify_info "$1" "Scope"
if [ $# -ne 0 ];
then
exit $#
fi
verify_info "$1" "Affects"
if [ $# -ne 0 ];
then
exit $#
fi
exit 0
以下是我将范围信息留为空白时的输出:
Here is the output when I leave the Scope information blank:
Scope information should not be omitted
Do you want to:
1) edit the commit message 3) cancel the commit
2) ignore this warning
#?
该消息正确,但实际上并未停止输入。我也尝试使用更简单的读取命令,它也有同样的问题。看来问题是,在这一点上,git控制着stdin并提供自己的输入。如何解决这个问题?
The message is correct, but it doesn't actually stop for input. I've also tried using the simpler "read" command, and it has the same problem. It seems that the problem is that at this point git has control of stdin and is providing its own input. How do I fix this?
更新:看起来这可能是,这不幸似乎表明我运气不好。
Update: It seems this might be a duplicate of this question which unfortunately seems to suggest I'm out of luck.
推荐答案
调用 exec< / dev / tty
为键盘分配标准输入。适用于我的后提交git钩子:
Calling exec < /dev/tty
assigns standard input to the keyboard. Works for me in a post-commit git hook:
#!/bin/sh
echo "[post-commit hook] Commit done!"
# Allows us to read user input below, assigns stdin to keyboard
exec < /dev/tty
while true; do
read -p "[post-commit hook] Check for outdated gems? (Y/n) " yn
if [ "$yn" = "" ]; then
yn='Y'
fi
case $yn in
[Yy] ) bundle outdated --pre; break;;
[Nn] ) exit;;
* ) echo "Please answer y or n for yes or no.";;
esac
done
这篇关于我如何在commit-msg钩子中提示用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!