git hook中出现错误“用户设备中没有这样的设备或地址”

问题描述 投票:1回答:1

我有一个git钩子,它在循环中执行某些work,直到用户输入为y/Yn/N除外)为止。

这适用于所有对象(提交,合并,修改等)。除了变基,我得到

.git-hooks/commit-msg: line xx: /dev/tty: No such device or address

重新输入/编辑合并提交时出错。

示例:如果我rebase -i head~4 -p并重新编写了所有提交(包括合并提交)的字词,则在合并提交时出现此错误。

hook:

#!/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 <&-
git bash githooks
1个回答
0
投票

发布此问题已经有一段时间了,但是我仍然想提供反馈,以防其他人遇到相同的问题。

我通过以下检查设法解决了这个问题:

# 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

玩得开心!

© www.soinside.com 2019 - 2024. All rights reserved.