2017-12-21 16 views
0

git hook을 구현하는 방법을 보여 주시겠습니까?Git hook 구현 - prePush 및 preCommit

커밋하기 전에 후크는 파이썬 스크립트를 실행해야합니다. 다음과 같은 것 :

cd c:\my_framework & run_tests.py --project Proxy-Tests\Aeries \ 
    --client Aeries --suite <Commit_file_Name> --dryrun 

드라이 런이 실패하면 커밋을 중지해야합니다.

답변

1

드라이 런이 실패하는 방식을 알려주십시오. 오류가있는 출력 .txt가 있습니까? 터미널에 오류가 표시됩니까?

어떤 경우 든 사전 커밋 스크립트의 이름을 pre-commit으로 지정하고 .git/hooks/ 디렉토리에 저장해야합니다.

드라이 런 스크립트가 사전 커밋 스크립트와 다른 경로에있는 것처럼 보일 수 있으므로 다음은 스크립트를 찾아 실행하는 예입니다.

나는 당신이 Windows 머신에 있다고 당신의 경로에있는 백 슬래시로부터 가정하고, 당신이 말한 실행 스크립트는 git가 설치된 동일한 프로젝트와 tools 폴더에 포함되어 있다고 가정한다. 이것을 실제 폴더로 변경할 수 있습니다).

#!/bin/sh 

#Path of your python script 
FILE_PATH=tools/run_tests.py/ 

#Get relative path of the root directory of the project 
rdir=`git rev-parse --git-dir` 
rel_path="$(dirname "$rdir")" 

#Cd to that path and run the file. 
cd $rel_path/$FILE_PATH 
echo "Running dryrun script..." 
python run_tests.py 

#From that point on you need to handle the dry run error/s. 
#For demonstrating purproses I'll asume that an output.txt file that holds 
#the result is produced. 

#Extract the result from the output file 
final_res="tac output | grep -m 1 . | grep 'error'" 

echo -e "--------Dry run result---------\n"${final_res} 

#If a warning and/or error exists abort the commit 
eval "$final_res" | while read -r line; do 
if [ $line != "0" ]; then 
    echo -e "Dry run failed.\nAborting commit..." 
    exit 1 
fi 
done 

지금 때마다 당신은 git commit -m을 해고 사전 커밋 stagin 영역에서 파일을 유지, 건조 실행 파일을 실행하고 오류가 발생 한 경우 커밋 중단됩니다 스크립트를.

+0

감사합니다. kingJulian. 대답에 너무나 감사드립니다. 귀하의 질문에 - 예, outpul xml이 생성됩니다 - 1 중요한 테스트, 0 통과, 1 실패 같은 상태. 또는 콘솔 로그에 "[ERROR]"가 표시됩니다. 두 경우 모두 커밋이 실패합니다. –

+0

안녕하세요 kingJulian, 여러 파일 커밋에 대한 논리를 알려주시겠습니까? –

+0

그러면 XML 파일을 구문 분석하고 실패를 검색해야합니다. 실패한 경우 0 이외의 숫자가 올 경우 커밋을 중단해야합니다. 여러 파일을 커밋하려면'git add file1, file2, etc' 명령으로 스테이징 영역에 추가 한 다음'git commit -m ""를 사용하여 단일 커밋을 수행해야합니다. 그런 다음 사전 커밋 훅이 정상적으로 실행됩니다. – kingJulian

0

나는 이것을 내 후크에 구현했습니다. 다음은 코드 스 니펫입니다.

#!/bin/sh 
#Path of your python script 

RUN_TESTS="run_tests.py" 
FRAMEWORK_DIR="/my-framework/" 
CUR_DIR=`echo ${PWD##*/}` 

`$`#Get full path of the root directory of the project under RUN_TESTS_PY_FILE 

rDIR=`git rev-parse --git-dir --show-toplevel | head -2 | tail -1` 
OneStepBack=/../ 
CD_FRAMEWORK_DIR="$rDIR$OneStepBack$FRAMEWORK_DIR" 

#Find list of modified files - to be committed 
LIST_OF_FILES=`git status --porcelain | awk -F" " '{print $2}' | grep ".txt" ` 

for FILE in $LIST_OF_FILES; do 
    cd $CD_FRAMEWORK_DIR 
     python $RUN_TESTS --dryrun --project $CUR_DIR/$FILE 
    OUT=$? 

    if [ $OUT -eq 0 ];then 
     continue 
    else 
     return 1 
    fi 
done