我有一个 git 钩子,它可以在循环中执行一些work
,直到用户输入y/Y
(n/N
除外(。
这适用于所有人(提交、合并、修正等(。除了变基,我得到
.git-hooks/commit-msg: line xx: /dev/tty: No such device or address
当我改写/编辑合并提交时出错。
示例:如果我rebase -i head~4 -p
并重写所有提交(包括合并提交(,那么我在合并提交时会收到此错误。
钩:
#!/bin/sh
# git hook
work() {
echo "working..."
}
user_input() {
while true; do
work "$@"
echo -e -n "done?"
read -p '' ans
case $ans in
[nN] )
;;
* )
break ;;
esac
done
}
exec < /dev/tty
user_input "$@"
exec <&-
这个问题发布已经有一段时间了,但我仍然想给出反馈,以防其他人遇到同样的问题。
我设法通过以下检查解决了这个问题:
# check if a rebase is taking place
# https://stackoverflow.com/questions/3921409/how-to-know-if-there-is-a-git-rebase-in-progress
if [ -d "./.git/rebase-merge" ] || [ -d "./.git/rebase-apply" ]; then
exit
else
# allow to use bash prompt in hook
# https://stackoverflow.com/questions/48640615/bash-confirmation-wont-wait-for-user-input
exec < /dev/tty
fi
玩得愉快!