如何在Git挂钩中要求用户输入? [英] How to ask for user input in a Git hook?

查看:64
本文介绍了如何在Git挂钩中要求用户输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试编写一个简单的bash脚本作为pre-push挂钩,在推送Java代码时,我在其中检查缺少的测试文件.

I have been trying to write a simple bash script as a pre-push hook, in which I check for missing test files when pushing Java code.

问题是:read命令没有等待用户输入,它就像没有输入任何内容一样进行.

The problem is: the read command isn't waiting for user input, it proceed as if no input has been entered.

has_java="git diff --stat --cached origin/master | grep \"src\/main.*\.java\""
has_test="git diff --stat --cached origin/master | grep \"src\/test.*\.java\""

exit_val=0

if eval $has_java; then
    if eval $has_test; then
        :
    else
        echo "*** NO TESTS WERE FOUND WHILE PUSHING JAVA CODE ***"
        read -n1 -p "Do you want to CONTINUE pushing? [Y/n]" doit
        case $doit in  
            n|N) exit_val=1 ;; 
            y|Y) exit_val=0 ;;
            *) exit_val=0 ;;
        esac
    fi
fi

推荐答案

Git钩子不使用标准输入.因此,必须附加来自终端的输入:dev/tty.

Git hooks do not use standard input. Thus, one must attach the input from the terminal: dev/tty.

只需在命令末尾附加终端即可使它工作:

Simply appending the terminal at the end of the command makes it work:

read -n1 -p "Do you want to CONTINUE pushing? [Y/n]" doit < /dev/tty

这篇关于如何在Git挂钩中要求用户输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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