1 #!/bin/bash 2 # 3 # Copyright (c) 2015 Google Inc. 4 # 5 # This is a pre-push hook that does the following before uploading a 6 # CL for review: 7 # 1) check that python sources have been formatted with pyformat. 8 # 2) allows the user to run the unit tests. 9 10 # This redirects stdin. Make sure to run after stdin has been read. 11 run_UnitTests() { 12 save_dir=$(pwd) 13 status=0 14 valid=0 15 16 # Make sure we can read the stdin from terminal 17 exec < /dev/tty 18 19 while [[ $valid -eq 0 ]] ; do 20 read -p "Run unit tests? [y/n] " choice 21 case "$choice" in 22 n|N ) valid=1 ;; 23 y|Y ) valid=1; cd crosperf; ./run_tests.sh; status=$? ; 24 cd $save_dir;; 25 * ) echo "Must choose y or n." 26 esac 27 done 28 if [[ $status -ne 0 ]]; then 29 exit $status 30 fi 31 } 32 33 run_PyFormat() { 34 pyformat="./bin/tc_pyformat" 35 range=$1 36 files=$(git show --pretty="format:" --name-only $range) 37 for f in $files; do 38 [[ $f == *.py ]] || continue 39 # File could have been removed as part of the commit. 40 [[ -e $f ]] || continue 41 diffs=$($pyformat -d $f) 42 if [[ $? -ne 0 ]]; then 43 echo "Error: $pyformat $f returned with error code $?" 44 exit 1 45 fi 46 if [[ -n "$diffs" ]]; then 47 echo -e "Error: $f is not formatted correctly. Run $pyformat -i $f\n" 48 echo -e "diffs:\n$diffs\n" 49 exit 2 50 fi 51 done 52 } 53 54 z40=0000000000000000000000000000000000000000 55 56 while IFS=' ' read local_ref local_sha remote_ref remote_sha; do 57 if [[ "$local_sha" != $z40 ]]; then 58 if [[ "$remote_sha" == $z40 ]]; then 59 # New branch, examine commit on top of branch. 60 range="$local_sha" 61 else 62 # Update to existing branch, examine new commits 63 range="$remote_sha..$local_sha" 64 fi 65 run_PyFormat $range 66 fi 67 done 68 69 run_UnitTests 70 71 exit 0 72