2015-12-18 35 views
0

我有一个混帐预推钩子调用名为pre-push.sh像这样混帐挂钩不与输入流工作

#!/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; 

伯爵提交一个shell脚本是这样

#!/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 

当直接调用prepush.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' 

做混帐推时,当

+ 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钩子以允许来自用户的输入?

回答

0

从`男人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参数。

+0

好的,有没有办法使用该标志来忽略该检查?在prepush.sh内我有其他一些检查,我想保留? – user3626708