aboutsummaryrefslogtreecommitdiff
path: root/toolchain_utils_githooks
diff options
context:
space:
mode:
authorLuis Lozano <llozano@chromium.org>2015-12-30 11:12:09 -0800
committerchrome-bot <chrome-bot@chromium.org>2016-01-05 01:55:22 +0000
commit5dd0459f8a7207ddcee26baa4917c2a6e20fba67 (patch)
treea10e529dc10489eeeb21fed06c2500ab1d5a5e27 /toolchain_utils_githooks
parent6736a8e1044141d657aeac5916b32123da664e97 (diff)
downloadtoolchain-utils-5dd0459f8a7207ddcee26baa4917c2a6e20fba67.tar.gz
Modify pre-push hook to run pyformat.
Run pyformat before git push. Give error if files need formatting. Also, made silly cleanup to pstat.py to be able to test the pre-push hook. BUG=None TEST=white box testing Change-Id: I79f12fa0acca4048b551f7dab25b0de8d5f05d75 Reviewed-on: https://chrome-internal-review.googlesource.com/243113 Commit-Ready: Luis Lozano <llozano@chromium.org> Tested-by: Luis Lozano <llozano@chromium.org> Reviewed-by: Luis Lozano <llozano@chromium.org>
Diffstat (limited to 'toolchain_utils_githooks')
-rwxr-xr-xtoolchain_utils_githooks/pre-push33
-rwxr-xr-xtoolchain_utils_githooks/pre-push.real71
2 files changed, 77 insertions, 27 deletions
diff --git a/toolchain_utils_githooks/pre-push b/toolchain_utils_githooks/pre-push
index 2eff76ef..eef8a09a 100755
--- a/toolchain_utils_githooks/pre-push
+++ b/toolchain_utils_githooks/pre-push
@@ -1,30 +1,9 @@
-#!/bin/sh
+#!/bin/bash
#
-# Copyright (c) 2015 Google Inc.
+# Copyright (c) 2016 Google Inc.
#
-# This is a pre-push hook that allows the user to run the unit tests
-# before uploading a CL for review.
-run_UnitTests() {
- save_dir=`pwd`
- status=0
- valid=0
- exec < /dev/tty
- while [ $valid -eq 0 ] ; do
- read -p "Run unit tests? [y/n] " choice
- case "$choice" in
- n|N ) valid=1 ;;
- y|Y ) valid=1; cd crosperf; ./run_tests.sh; status=$? ;
- cd $save_dir;;
- * ) echo "Must choose y or n."
- esac
- done
- if [ $status -ne 0 ]
- then
- exit $status
- fi
-}
-
-run_UnitTests
-
-exit 0
+# Just execute our custom pre-push script.
+# Do this trick so that this file does not need to be updated each time
+# we modify our pre-push script
+exec ./toolchain_utils_githooks/pre-push.real "$@"
diff --git a/toolchain_utils_githooks/pre-push.real b/toolchain_utils_githooks/pre-push.real
new file mode 100755
index 00000000..0f6856ee
--- /dev/null
+++ b/toolchain_utils_githooks/pre-push.real
@@ -0,0 +1,71 @@
+#!/bin/bash
+#
+# Copyright (c) 2015 Google Inc.
+#
+# This is a pre-push hook that does the following before uploading a
+# CL for review:
+# 1) check that python sources have been formatted with pyformat.
+# 2) allows the user to run the unit tests.
+
+# This redirects stdin. Make sure to run after stdin has been read.
+run_UnitTests() {
+ save_dir=$(pwd)
+ status=0
+ valid=0
+
+ # Make sure we can read the stdin from terminal
+ exec < /dev/tty
+
+ while [[ $valid -eq 0 ]] ; do
+ read -p "Run unit tests? [y/n] " choice
+ case "$choice" in
+ n|N ) valid=1 ;;
+ y|Y ) valid=1; cd crosperf; ./run_tests.sh; status=$? ;
+ cd $save_dir;;
+ * ) echo "Must choose y or n."
+ esac
+ done
+ if [[ $status -ne 0 ]]; then
+ exit $status
+ fi
+}
+
+run_PyFormat() {
+ pyformat="./bin/tc_pyformat"
+ range=$1
+ files=$(git show --pretty="format:" --name-only $range)
+ for f in $files; do
+ [[ $f == *.py ]] || continue
+ # File could have been removed as part of the commit.
+ [[ -e $f ]] || continue
+ diffs=$($pyformat -d $f)
+ if [[ $? -ne 0 ]]; then
+ echo "Error: $pyformat $f returned with error code $?"
+ exit 1
+ fi
+ if [[ -n "$diffs" ]]; then
+ echo -e "Error: $f is not formatted correctly. Run $pyformat -i $f\n"
+ echo -e "diffs:\n$diffs\n"
+ exit 2
+ fi
+ done
+}
+
+z40=0000000000000000000000000000000000000000
+
+while IFS=' ' read local_ref local_sha remote_ref remote_sha; do
+ if [[ "$local_sha" != $z40 ]]; then
+ if [[ "$remote_sha" == $z40 ]]; then
+ # New branch, examine commit on top of branch.
+ range="$local_sha"
+ else
+ # Update to existing branch, examine new commits
+ range="$remote_sha..$local_sha"
+ fi
+ run_PyFormat $range
+ fi
+done
+
+run_UnitTests
+
+exit 0