-
Notifications
You must be signed in to change notification settings - Fork 510
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor hooks, add opt-in pre-push hook.
See hooks/README.md for implementation notes. Motivtion: the old hooks are hard to run manually, and code reuse among hooks is difficult. Goal of this refactor is to cleanly split the steps involved: - determine set of changed files - run checks on specific files - report issues based on check results The old logic was based on running checks on individual files, printing "1" on STDOUT for bad files. Now, check-* scripts take a list of files as args and print filenames of bad files on STDOUT. A toplevel "check" script drives these individual checks and contains common hook logic such as problem reporting. The toplevel hooks themselves use the "check" script to take appropriate actions. Also change checks to use strict `-euo pipefail` error handling where possible, and standardize variable naming. (Uppercase for top-of-file constants or vars exported to the environment, otherwise lowercase.)
- Loading branch information
Showing
13 changed files
with
303 additions
and
164 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
#!/bin/bash | ||
|
||
# Copyright 2015 Google Inc. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
set -euo pipefail | ||
|
||
HOOKS_DIR="$(dirname "$(test -L "$0" && echo "$(dirname $0)/$(readlink "$0")" || echo "$0")")" | ||
|
||
# The prepare-commit-msg hook sets an environment variable to indicate | ||
# that we should add details to the report. Set this to an empty | ||
# string if unset to avoid "unbound variable" failures. | ||
PREPARE_COMMIT_HOOK="${PREPARE_COMMIT_HOOK:-}" | ||
|
||
# Execute all the checks first, they'll print informational messages on stderr. | ||
status=0 | ||
|
||
if ! "${HOOKS_DIR}/lib/check-unittest.sh"; then | ||
echo "*** ERROR: *** Unit test failure." | ||
if [ -n "$PREPARE_COMMIT_HOOK" ]; then | ||
echo "Your commit will be aborted unless you fix these." | ||
echo " COMMIT_BLOCKED_ON_UNIT_TESTS" | ||
fi | ||
status=1 | ||
fi | ||
|
||
files_need_boilerplate=($("${HOOKS_DIR}/lib/check-boilerplate.sh" "$@")) | ||
|
||
files_with_lint_errors=($("${HOOKS_DIR}/lib/check-lint.sh" "$@")) | ||
|
||
# Now print reports for all collected errors to ensure that they are | ||
# all visible together. This way, they won't be scrolled offscreen | ||
# by diagnostics. | ||
|
||
if [[ "${#files_need_boilerplate[@]}" -ne 0 ]]; then | ||
echo | ||
echo "*** ERROR: *** Some files are missing the required boilerplate" | ||
echo "header from hooks/boilerplate.txt:" | ||
for file in "${files_need_boilerplate[@]}"; do | ||
echo " ${file}" | ||
done | ||
echo "See hooks/boilerplate.*.txt for required headers." | ||
if [ -n "$PREPARE_COMMIT_HOOK" ]; then | ||
echo "Your commit will be aborted unless you fix these." | ||
echo " COMMIT_BLOCKED_ON_BOILERPLATE" | ||
fi | ||
status=1 | ||
fi | ||
|
||
if [[ "${#files_with_lint_errors[@]}" -ne 0 ]]; then | ||
echo | ||
echo "*** ERROR: *** Some files have lint errors:" | ||
for file in "${files_with_lint_errors[@]}"; do | ||
echo " ${file}" | ||
done | ||
if [ -n "$PREPARE_COMMIT_HOOK" ]; then | ||
echo "Your commit will be aborted unless you fix these." | ||
echo " COMMIT_BLOCKED_ON_LINT" | ||
fi | ||
status=1 | ||
fi | ||
|
||
exit $status |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
#!/bin/bash | ||
|
||
# Copyright 2015 Google Inc. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
set -euo pipefail | ||
|
||
HOOKS_LIB="$(dirname "$(test -L "$0" && echo "$(dirname $0)/$(readlink "$0")" || echo "$0")")" | ||
|
||
# Check for files without the required boilerplate. | ||
for file in "$@"; do | ||
ext=${file##*.} | ||
ref_file="${HOOKS_LIB}/boilerplate.${ext}.txt" | ||
|
||
if [ ! -e $ref_file ]; then | ||
continue | ||
fi | ||
|
||
lines=$(cat "${ref_file}" | wc -l | tr -d ' ') | ||
if [[ "${ext}" == "py" && -x "${file}" ]]; then | ||
# remove shabang and blank line from top of executable python files. | ||
header=$(cat "${file}" | tail --lines=+3 | head "-${lines}") | ||
else | ||
header=$(head "-${lines}" "${file}") | ||
fi | ||
|
||
# Treat errors from diff other than the usual "files differ" | ||
# code 1 as fatal problems. | ||
differ=$(echo "${header}" | diff - "${ref_file}" || [[ $? -le 1 ]]) | ||
|
||
if [[ -z "${differ}" ]]; then | ||
# Header does not differ. | ||
continue | ||
fi | ||
|
||
if [ "$(echo "${differ}" | wc -l)" -eq 4 ]; then | ||
# Header differs by one line. Check if only copyright date differs. If so, | ||
# the contents of "${differ}" should resemble: | ||
# 1c1 | ||
# < # Copyright 2015 Google Inc. All rights reserved. | ||
# --- | ||
# > # Copyright 2014 Google Inc. All rights reserved. | ||
header_line='# Copyright [0-9]+ Google Inc[.] All rights reserved[.]' | ||
diff_prefix='[[:alnum:]]+[[:space:]]*< ' | ||
diff_middle='[[:space:]]*---[[:space:]]*> ' | ||
diff_pattern="^${diff_prefix}${header_line}${diff_middle}${header_line}\$" | ||
if [[ "${differ}" =~ $diff_pattern ]]; then | ||
# Change matches acceptable variation. | ||
continue | ||
fi | ||
fi | ||
|
||
# Report file as invalid boilerplate. | ||
echo "$file" | ||
done | ||
|
||
exit 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#!/bin/bash | ||
|
||
# Copyright 2015 Google Inc. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
set -euo pipefail | ||
|
||
if [[ -z "$(command -v flake8)" ]]; then | ||
>&2 echo "Missing flake8. Install it via 'pip' to enable linting." | ||
exit 1 | ||
fi | ||
|
||
# Treat errors from grep other than the "not found" code 1 as fatal problems. | ||
modified_py=($(printf -- '%s\n' "$@" | grep '\.py$' || [[ $? -le 1 ]])) | ||
|
||
# Don't run linter with no arguments, that would check all files. | ||
if [[ "${#modified_py[@]}" -ne 0 ]]; then | ||
# First, run flake with normal output so that the user sees errors. | ||
# If there are problems, re-run flake8, printing filenames only. | ||
if ! flake8 "${modified_py[@]}" >&2; then | ||
flake8 -q "${modified_py[@]}" || true | ||
fi | ||
fi | ||
|
||
exit 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.