【问题标题】:git hook not working with input streamgit hook 不能处理输入流
【发布时间】:2015-12-18 04:33:20
【问题描述】:

我有一个 git pre-push 钩子,它像这样调用一个名为 pre-push.sh 的 shell 脚本

#!/bin/bash

#Fail if one script fails and print all info to console
set -ex

#Font variables
underline=`tput smul`
nounderline=`tput rmul`

#Run grunt
grunt;

#Make sure there is only one commit being pushed
printf "\n${underline}Validating Number of Commits${nounderline}\n";
src/scripts/hooks/pre-push/countCommits.sh;

Count commits 就是这样

#!/bin/bash


confirm () {
    # call with a prompt string or use a default
    read -r -p "${1:-Are you sure? [y/N]} " response
    case $response in
        [yY][eE][sS]|[yY]) 
            true
            ;;
        *)
            false
            ;;
    esac
}
#This script counts the number of commits being pushed and fails if it is greater than 1 commit being pushed
countCommits(){
    NUMBER_OF_COMMITS=`git rev-list @{u}.. | wc -l`
    if [ "$NUMBER_OF_COMMITS" -gt "1" ]; then
        return 1
        #exit 1
     else
        return 0
    fi

}


if countCommits; then
    printf "\e[0;32mDone, without errors.\e[0m"
else
    printf "$NUMBER_OF_COMMITS commits were pushed. We recommend pushing only 1 commit."
    if confirm "$NUMBER_OF_COMMITS commits were found. We recommend pushing only 1 commit. Do you wish to continue? [y/N]"; then
        :
    else
        printf "Please squash your commits by running \e[1;34m'git rebase -i'\e[0m\n"
        exit 1
    fi
fi

当直接调用 pre-push.sh 时,它可以正常工作,即等待用户说出 'y' 或 'n' 如果他们有超过 1 个提交。但是,当我将它用作预推送挂钩时,它不会等待用户提示并继续沿无路径前进

直接调用pre-push.sh时

+ src/scripts/hooks/pre-push/countCommits.sh
       5 commits were pushed. We recommend pushing only 1 commit.       5 commits were found. We recommend pushing only 1 commit. Do you wish to continue? [y/N] n
Please squash your commits by running 'git rebase -i'

执行 git push 时

+ src/countCommits.sh
       5 commits were pushed. We recommend pushing only 1 commit.Please squash your commits by running 'git rebase -i'
error: failed to push some refs

如何让 git 钩子允许用户输入?

【问题讨论】:

    标签: git bash shell githooks


    【解决方案1】:

    来自`man githooks

       Information about what is to be pushed is provided on the hook's standard input with lines of the form:
    
           <local ref> SP <local sha1> SP <remote ref> SP <remote sha1> LF
    

    所以脚本不会等待您的手动输入,它只是使用第一行,这肯定与您的模式不匹配。

    您应该告诉用户,如果他们真的想要推送,他们应该使用--no-verify 参数重新运行命令,而不是提示是否继续。

    【讨论】:

    • 好的有没有办法使用该标志来忽略该检查?在 pre-push.sh 中我还有其他一些我想保留的检查吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-22
    • 1970-01-01
    • 2018-05-08
    • 2020-01-27
    • 2016-01-16
    • 1970-01-01
    • 2015-05-17
    相关资源
    最近更新 更多