From f11ba4a1b57309cf7c75470df40ea7ab4865e12b Mon Sep 17 00:00:00 2001 From: Tim Hockin Date: Tue, 12 May 2015 21:59:44 -0700 Subject: [PATCH 1/4] Switch git hooks to use pre-commit --- docs/devel/development.md | 3 +- hooks/commit-msg | 10 --- hooks/{prepare-commit-msg => pre-commit} | 82 +++++++++++------------- 3 files changed, 39 insertions(+), 56 deletions(-) delete mode 100755 hooks/commit-msg rename hooks/{prepare-commit-msg => pre-commit} (54%) diff --git a/docs/devel/development.md b/docs/devel/development.md index 556f7c2259a..6d6bdb86118 100644 --- a/docs/devel/development.md +++ b/docs/devel/development.md @@ -105,8 +105,7 @@ directory. This will keep you from accidentally committing non-gofmt'd go code. ``` cd kubernetes/.git/hooks/ -ln -s ../../hooks/prepare-commit-msg . -ln -s ../../hooks/commit-msg . +ln -s ../../hooks/pre-commit . ``` ## Unit tests diff --git a/hooks/commit-msg b/hooks/commit-msg deleted file mode 100755 index d9fa585966f..00000000000 --- a/hooks/commit-msg +++ /dev/null @@ -1,10 +0,0 @@ -#!/bin/bash - -if [[ "$(grep -c "COMMIT_BLOCKED" $1)" -gt 0 ]]; then - echo "FAILED: Unresolved errors - aborting the commit." - echo "The message of your attempted commit was:" - cat $1 - exit 1 -fi - -exit 0 diff --git a/hooks/prepare-commit-msg b/hooks/pre-commit similarity index 54% rename from hooks/prepare-commit-msg rename to hooks/pre-commit index bfb6a07b77b..486a02808b6 100755 --- a/hooks/prepare-commit-msg +++ b/hooks/pre-commit @@ -6,6 +6,7 @@ files_need_gofmt=() files_need_boilerplate=() files_need_description=() +echo -ne "Checking for files that need gofmt..." files=($(git diff --cached --name-only --diff-filter ACM | grep "\.go" | grep -v -e "third_party" -e "Godeps")) for file in "${files[@]}"; do # Check for files that fail gofmt. @@ -14,7 +15,9 @@ for file in "${files[@]}"; do files_need_gofmt+=("${file}") fi done +echo "done" +echo -ne "Checking for files that need boilerplate..." boiler="${KUBE_HOOKS_DIR}/boilerplate.py" # Check for go files without the required boilerplate. if [[ ${#files[@]} -gt 0 ]]; then @@ -32,7 +35,9 @@ files=($(git diff --cached --name-only --diff-filter ACM | grep "\.py" | grep -v if [[ ${#files} -gt 0 ]]; then files_need_boilerplate+=($("${boiler}" "py" "${files[@]}")) fi +echo "done" +echo -ne "Checking for API descriptions..." # Check API schema definitions for field descriptions for file in $(git diff --cached --name-only --diff-filter ACM | egrep "pkg/api/v.[^/]*/types\.go" | grep -v "third_party"); do # Check for files with fields without description tags @@ -41,59 +46,48 @@ for file in $(git diff --cached --name-only --diff-filter ACM | egrep "pkg/api/v files_need_description+=("${file}") fi done +echo "done" +ret=0 if [[ "${#files_need_gofmt[@]}" -ne 0 ]]; then - ( - echo - echo "# *** ERROR: *** Some files have not been gofmt'd. To fix these" - echo "# errors, run gofmt -s -w , or cut and paste the following:" - echo "# gofmt -s -w ${files_need_gofmt[@]}" - echo "#" - echo "# Your commit will be aborted unless you override this warning. To" - echo "# commit in spite of these format errors, delete the following line:" - echo "# COMMIT_BLOCKED_ON_GOFMT" - ) >> $1 + echo + echo "# *** ERROR: *** Some files have not been gofmt'd. To fix these" + echo "# errors, run gofmt -s -w , or cut and paste the following:" + echo "# gofmt -s -w ${files_need_gofmt[@]}" + echo + ret=1 fi 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 "#" - echo "# Your commit will be aborted unless you fix these." - echo "# COMMIT_BLOCKED_ON_BOILERPLATE" - echo - ) >> $1 + 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 + ret=1 fi if [[ "${#files_need_description[@]}" -ne 0 ]]; then - ( - echo - echo "# *** ERROR: *** Some API files are missing the required field descriptions" - echo "# Add description tags to all non-inline fields in the following files:" - for file in "${files_need_description[@]}"; do - echo "# ${file}" - done - echo "#" - echo "# Your commit will be aborted unless you fix these." - echo "# COMMIT_BLOCKED_ON_DESCRIPTION" - echo - ) >> $1 + echo + echo "# *** ERROR: *** Some API files are missing the required field descriptions" + echo "# Add description tags to all non-inline fields in the following files:" + for file in "${files_need_description[@]}"; do + echo "# ${file}" + done + echo + ret=1 fi +echo -ne "Checking for docs that need updating..." if ! hack/verify-gendocs.sh > /dev/null; then - ( - echo - echo "# *** ERROR: *** docs are out of sync between cli and markdown" - echo "# run hack/run-gendocs.sh > docs/kubectl.md to regenerate" - echo - echo "#" - echo "# Your commit will be aborted unless you regenerate docs." - echo " COMMIT_BLOCKED_ON_GENDOCS" - echo - ) >> $1 + echo + echo "# *** ERROR: *** docs are out of sync between cli and markdown" + echo "# run hack/run-gendocs.sh > docs/kubectl.md to regenerate" + echo + ret=1 fi +echo "done" + +exit $ret From d0b2a418db0d93a30bb814bf9eb175fd7ea0bbc6 Mon Sep 17 00:00:00 2001 From: Tim Hockin Date: Tue, 12 May 2015 22:47:24 -0700 Subject: [PATCH 2/4] Make git hooks print more nicely --- hooks/pre-commit | 93 +++++++++++++++++++++++++----------------------- 1 file changed, 49 insertions(+), 44 deletions(-) diff --git a/hooks/pre-commit b/hooks/pre-commit index 486a02808b6..2f0eec94577 100755 --- a/hooks/pre-commit +++ b/hooks/pre-commit @@ -2,11 +2,10 @@ KUBE_HOOKS_DIR="$(dirname "$(test -L "$0" && echo "$(dirname $0)/$(readlink "$0")" || echo "$0")")" -files_need_gofmt=() -files_need_boilerplate=() -files_need_description=() +exit_code=0 -echo -ne "Checking for files that need gofmt..." +echo -ne "Checking for files that need gofmt... " +files_need_gofmt=() files=($(git diff --cached --name-only --diff-filter ACM | grep "\.go" | grep -v -e "third_party" -e "Godeps")) for file in "${files[@]}"; do # Check for files that fail gofmt. @@ -15,9 +14,20 @@ for file in "${files[@]}"; do files_need_gofmt+=("${file}") fi done -echo "done" -echo -ne "Checking for files that need boilerplate..." +if [[ "${#files_need_gofmt[@]}" -ne 0 ]]; then + echo "ERROR!" + echo "Some files have not been gofmt'd. To fix these errors, " + echo "cut and paste the following:" + echo " gofmt -s -w ${files_need_gofmt[@]}" + exit_code=1 +else + echo "OK" +fi +echo + +echo -ne "Checking for files that need boilerplate... " +files_need_boilerplate=() boiler="${KUBE_HOOKS_DIR}/boilerplate.py" # Check for go files without the required boilerplate. if [[ ${#files[@]} -gt 0 ]]; then @@ -35,9 +45,22 @@ files=($(git diff --cached --name-only --diff-filter ACM | grep "\.py" | grep -v if [[ ${#files} -gt 0 ]]; then files_need_boilerplate+=($("${boiler}" "py" "${files[@]}")) fi -echo "done" -echo -ne "Checking for API descriptions..." +if [[ "${#files_need_boilerplate[@]}" -ne 0 ]]; then + echo "ERROR!" + echo "Some files are missing the required boilerplate header" + echo "from hooks/boilerplate.txt:" + for file in "${files_need_boilerplate[@]}"; do + echo " ${file}" + done + exit_code=1 +else + echo "OK" +fi +echo + +echo -ne "Checking for API descriptions... " +files_need_description=() # Check API schema definitions for field descriptions for file in $(git diff --cached --name-only --diff-filter ACM | egrep "pkg/api/v.[^/]*/types\.go" | grep -v "third_party"); do # Check for files with fields without description tags @@ -46,48 +69,30 @@ for file in $(git diff --cached --name-only --diff-filter ACM | egrep "pkg/api/v files_need_description+=("${file}") fi done -echo "done" - -ret=0 -if [[ "${#files_need_gofmt[@]}" -ne 0 ]]; then - echo - echo "# *** ERROR: *** Some files have not been gofmt'd. To fix these" - echo "# errors, run gofmt -s -w , or cut and paste the following:" - echo "# gofmt -s -w ${files_need_gofmt[@]}" - echo - ret=1 -fi - -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 - ret=1 -fi if [[ "${#files_need_description[@]}" -ne 0 ]]; then - echo - echo "# *** ERROR: *** Some API files are missing the required field descriptions" - echo "# Add description tags to all non-inline fields in the following files:" + echo "ERROR!" + echo "Some API files are missing the required field descriptions." + echo "Add description tags to all non-inline fields in the following files:" for file in "${files_need_description[@]}"; do - echo "# ${file}" + echo " ${file}" done - echo - ret=1 + exit_code=1 +else + echo "OK" fi +echo -echo -ne "Checking for docs that need updating..." +echo -ne "Checking for docs that need updating... " if ! hack/verify-gendocs.sh > /dev/null; then - echo - echo "# *** ERROR: *** docs are out of sync between cli and markdown" - echo "# run hack/run-gendocs.sh > docs/kubectl.md to regenerate" - echo - ret=1 + echo "ERROR!" + echo "Some docs are out of sync between CLI and markdown." + echo "To regenerate docs, run:" + echo " hack/run-gendocs.sh > docs/kubectl.md" + exit_code=1 +else + echo "OK" fi -echo "done" +echo -exit $ret +exit $exit_code From 8e7970f2a11dde693d49e8e0eac49cb945f52740 Mon Sep 17 00:00:00 2001 From: Tim Hockin Date: Tue, 12 May 2015 22:51:57 -0700 Subject: [PATCH 3/4] mkdir -p in doc generation --- hack/lib/util.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hack/lib/util.sh b/hack/lib/util.sh index f4851d409f0..5bf95b43039 100644 --- a/hack/lib/util.sh +++ b/hack/lib/util.sh @@ -130,7 +130,7 @@ kube::util::gen-doc() { # We do this in a tmpdir in case the dest has other non-autogenned files # We don't want to include them in the list of gen'd files local tmpdir="${KUBE_ROOT}/doc_tmp" - mkdir "${tmpdir}" + mkdir -p "${tmpdir}" # generate the new files ${cmd} "${tmpdir}" # create the list of generated files From 4e9d3f77009b111240756bae64da420c570c42ee Mon Sep 17 00:00:00 2001 From: Tim Hockin Date: Wed, 13 May 2015 12:59:04 -0700 Subject: [PATCH 4/4] Use color in pre-commit --- hooks/pre-commit | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/hooks/pre-commit b/hooks/pre-commit index 2f0eec94577..3a240b3d14b 100755 --- a/hooks/pre-commit +++ b/hooks/pre-commit @@ -1,5 +1,9 @@ #!/bin/bash +readonly reset=$(tput sgr0) +readonly red=$(tput bold; tput setaf 1) +readonly green=$(tput bold; tput setaf 2) + KUBE_HOOKS_DIR="$(dirname "$(test -L "$0" && echo "$(dirname $0)/$(readlink "$0")" || echo "$0")")" exit_code=0 @@ -16,15 +20,15 @@ for file in "${files[@]}"; do done if [[ "${#files_need_gofmt[@]}" -ne 0 ]]; then - echo "ERROR!" + echo "${red}ERROR!" echo "Some files have not been gofmt'd. To fix these errors, " echo "cut and paste the following:" echo " gofmt -s -w ${files_need_gofmt[@]}" exit_code=1 else - echo "OK" + echo "${green}OK" fi -echo +echo "${reset}" echo -ne "Checking for files that need boilerplate... " files_need_boilerplate=() @@ -47,7 +51,7 @@ if [[ ${#files} -gt 0 ]]; then fi if [[ "${#files_need_boilerplate[@]}" -ne 0 ]]; then - echo "ERROR!" + echo "${red}ERROR!" echo "Some files are missing the required boilerplate header" echo "from hooks/boilerplate.txt:" for file in "${files_need_boilerplate[@]}"; do @@ -55,9 +59,9 @@ if [[ "${#files_need_boilerplate[@]}" -ne 0 ]]; then done exit_code=1 else - echo "OK" + echo "${green}OK" fi -echo +echo "${reset}" echo -ne "Checking for API descriptions... " files_need_description=() @@ -71,7 +75,7 @@ for file in $(git diff --cached --name-only --diff-filter ACM | egrep "pkg/api/v done if [[ "${#files_need_description[@]}" -ne 0 ]]; then - echo "ERROR!" + echo "${red}ERROR!" echo "Some API files are missing the required field descriptions." echo "Add description tags to all non-inline fields in the following files:" for file in "${files_need_description[@]}"; do @@ -79,20 +83,20 @@ if [[ "${#files_need_description[@]}" -ne 0 ]]; then done exit_code=1 else - echo "OK" + echo "${green}OK" fi -echo +echo "${reset}" echo -ne "Checking for docs that need updating... " if ! hack/verify-gendocs.sh > /dev/null; then - echo "ERROR!" + echo "${red}ERROR!" echo "Some docs are out of sync between CLI and markdown." echo "To regenerate docs, run:" echo " hack/run-gendocs.sh > docs/kubectl.md" exit_code=1 else - echo "OK" + echo "${green}OK" fi -echo +echo "${reset}" exit $exit_code