-
Notifications
You must be signed in to change notification settings - Fork 523
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #363 from GoogleCloudPlatform/dev-klausw-pre-push-…
…hook Refactor hooks, add opt-in pre-push hook.
- Loading branch information
Showing
13 changed files
with
314 additions
and
163 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,82 @@ | ||
#!/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. Use an empty string if | ||
# unset to avoid "unbound variable" failures. | ||
is_prepare_commit="${PREPARE_COMMIT_HOOK:-}" | ||
|
||
# Execute all the checks first, they'll print informational messages on stderr. | ||
status=0 | ||
|
||
files_need_boilerplate=($("${HOOKS_DIR}/lib/check-boilerplate.sh" "$@")) | ||
|
||
files_with_lint_errors=($("${HOOKS_DIR}/lib/check-lint.sh" "$@")) | ||
|
||
# Do unit tests last since these don't have a separate reporting step. | ||
# This will show unit test diagnostics on STDERR followed by the report. | ||
if ! "${HOOKS_DIR}/lib/check-unittest.sh"; then | ||
echo "*** ERROR: *** Unit test failure." | ||
if [ -n "$is_prepare_commit" ]; then | ||
echo "Your commit will be aborted unless you fix these." | ||
echo " COMMIT_BLOCKED_ON_UNIT_TESTS" | ||
fi | ||
status=1 | ||
fi | ||
|
||
# Now print reports for previously 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/lib/boilerplate.*.txt:" | ||
for file in "${files_need_boilerplate[@]}"; do | ||
echo " ${file}" | ||
done | ||
echo "See hooks/boilerplate.*.txt for required headers." | ||
if [ -n "$is_prepare_commit" ]; 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 "$is_prepare_commit" ]; then | ||
echo "Your commit will be aborted unless you fix these." | ||
echo " COMMIT_BLOCKED_ON_LINT" | ||
fi | ||
status=1 | ||
fi | ||
|
||
if [ -n "$is_prepare_commit" ]; then | ||
# For prepare-commit, only return a failure status if there were | ||
# unexpected problems. Failed checks should just produce a report. | ||
exit 0 | ||
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.