IT

Git 사전 푸시 후크

lottoking 2020. 8. 14. 07:59
반응형

Git 사전 푸시 후크


모든 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

참고 URL : https://stackoverflow.com/questions/4196148/git-pre-push-hooks

반응형