【发布时间】:2011-03-25 23:27:57
【问题描述】:
如果用户的提交消息不遵循特定的准则,我想警告用户,然后让他们选择编辑提交消息、忽略警告或取消提交。问题是我似乎无法访问标准输入。
下面是我的 commit-msg 文件:
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
这是我将 Scope 信息留空时的输出:
Scope information should not be omitted
Do you want to:
1) edit the commit message 3) cancel the commit
2) ignore this warning
#?
消息是正确的,但实际上并没有停止输入。我也尝试过使用更简单的“读取”命令,它也有同样的问题。似乎问题在于此时 git 可以控制标准输入并提供自己的输入。我该如何解决这个问题?
更新:看来这可能是 this question 的副本,不幸的是这似乎表明我不走运。
【问题讨论】:
-
当您可以访问 X 服务器时,您可以转至图形对话工具。丑陋但有效
-
您可以简单地提供信息性错误消息来代替错误消息 - 包括回显必要的命令以忽略警告。
-
@btspierre,这就是我最终采用的方法。在 John Feminella 的建议下,我允许使用环境变量来覆盖警告,并在遇到不好的情况时回显警告。
-
@Rudi:我不确定你会逃到 X 服务器,因为 git 似乎完全控制了标准输入。
-
10 年后,正在讨论这个问题:public-inbox.org/git/…
标签: git hook commit-message