Git 사전 푸시 후크 실행하고 테스트가 실패하면

모든 자식 푸시 전에 단위 테스트를 실행하고 테스트가 실패하면 푸시를 취소하지만 pre-push hook도 찾을 수 없으며 pre-commit 및 pre-rebase 만 있습니다.



답변

차라리 사전 커밋 후크에서 테스트를 실행하고 싶습니다. 커밋 할 때 변경 사항이 이미 기록되어 있기 때문입니다. 밀고 당기는 것은 이미 기록 된 변경 사항에 대한 정보를 교환합니다. 테스트가 실패하면 이미 저장소에 “깨진”개정이있을 것입니다. 당신이 그것을 밀고 있든 안하든.


답변

Git은 릴리스 에서 pre-push후크를 얻었습니다 1.8.2.

샘플 pre-push스크립트 : https://github.com/git/git/blob/87c86dd14abe8db7d00b0df5661ef8cf147a72a3/templates/hooks–pre-push.sample

1.8.2 새로운 프리 푸시 후크에 대한 릴리스 정보 : https://github.com/git/git/blob/master/Documentation/RelNotes/1.8.2.txt


답변

Git은 1.8.2 릴리스에서 사전 푸시 후크를 얻었습니다.

Pre-push 후크는 사전 커밋 후크와 함께 필요한 것입니다. 브랜치를 보호하는 것 외에도 사전 커밋 후크와 결합 된 추가 보안을 제공 할 수도 있습니다.

그리고 사용 방법에 대한 예 ( 이 멋진 항목 에서 가져 와서 채택 및 향상됨 )

방랑자에 로그인하고 테스트를 실행 한 다음 푸시하는 간단한 예제

#!/bin/bash
# Run the following command in the root of your project to install this pre-push hook:
# cp git-hooks/pre-push .git/hooks/pre-push; chmod 700 .git/hooks/pre-push

CMD="ssh vagrant@192.168.33.10 -i ~/.vagrant.d/insecure_private_key 'cd /vagrant/tests; /vagrant/vendor/bin/phpunit'"
protected_branch='master'

# Check if we actually have commits to push
commits=`git log @{u}..`
if [ -z "$commits" ]; then
    exit 0
fi

current_branch=$(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,')

if [[ $current_branch = $protected_branch ]]; then
    eval $CMD
    RESULT=$?
    if [ $RESULT -ne 0 ]; then
        echo "failed $CMD"
        exit 1
    fi
fi
exit 0

보시다시피이 예제는 pre-push hook의 주제 인 보호 된 분기를 사용합니다.


답변

명령 줄을 사용하는 경우 가장 쉬운 방법은 단위 테스트를 실행하고 성공하면 푸시를 완료하는 푸시 스크립트를 작성하는 것입니다.

편집하다

git 1.8.2 부터이 답변은 구식입니다. 위의 manojlds의 답변을 참조하십시오.


답변

푸시는 리포지토리를 수정하는 작업이 아니기 때문에 후크가 없습니다.

수신 측에서 확인을 할 수 있습니다 post-receive. 일반적으로 들어오는 푸시를 거부하는 곳입니다. 단위 테스트를 실행하는 것은 훅에서 수행하는 데 약간 집약적 일 수 있지만 그것은 당신에게 달려 있습니다.


답변

기록을 위해 사전 푸시 후크를 추가하는 Git 1.6에 대한 패치가 있습니다. 1.7에 대해 작동하는지 모르겠습니다.

엉망으로 만드는 대신 @kubi 권장과 같은 푸시 스크립트를 실행할 수 있습니다. 대신 Rake 작업으로 만들 수도 있으므로 저장소에 있습니다. ruby-git 이 도움이 될 수 있습니다. 대상 저장소를 확인하면 프로덕션 저장소로 푸시 할 때만 테스트를 실행할 수 있습니다.

마지막으로 pre-commit후크 에서 테스트를 실행할 수 있지만 어떤 분기가 커밋되고 있는지 확인할 수 있습니다. 그런 다음 production커밋을 수락하기 전에 모든 테스트를 통과해야하지만 master신경 쓰지 않는 분기를 가질 수 있습니다 . 이 시나리오에서는 limerick_rake 가 유용 할 수 있습니다.


답변

응답 높은 투표에 의해 연결 스크립트를 받는 쇼를 매개 변수 등을 pre-push후크 ( $1원격 이름입니다 $2어떻게 커밋에 액세스하는 방법과 URL) (라인 read표준 입력에서이 구조를 가지고 <local ref> <local sha1> <remote ref> <remote sha1>)

#!/bin/sh

# An example hook script to verify what is about to be pushed.  Called by "git
# push" after it has checked the remote status, but before anything has been
# pushed.  If this script exits with a non-zero status nothing will be pushed.
#
# This hook is called with the following parameters:
#
# $1 -- Name of the remote to which the push is being done
# $2 -- URL to which the push is being done
#
# If pushing without using a named remote those arguments will be equal.
#
# Information about the commits which are being pushed is supplied as lines to
# the standard input in the form:
#
#   <local ref> <local sha1> <remote ref> <remote sha1>
#
# This sample shows how to prevent push of commits where the log message starts
# with "WIP" (work in progress).

remote="$1"
url="$2"

z40=0000000000000000000000000000000000000000

while read local_ref local_sha remote_ref remote_sha
do
    if [ "$local_sha" = $z40 ]
    then
        # Handle delete
        :
    else
        if [ "$remote_sha" = $z40 ]
        then
            # New branch, examine all commits
            range="$local_sha"
        else
            # Update to existing branch, examine new commits
            range="$remote_sha..$local_sha"
        fi

        # Check for WIP commit
        commit=`git rev-list -n 1 --grep '^WIP' "$range"`
        if [ -n "$commit" ]
        then
            echo >&2 "Found WIP commit in $local_ref, not pushing"
            exit 1
        fi
    fi
done

exit 0