diff --git a/.git_hooks/README.md b/.git_hooks/README.md index 4fd933418..d05476583 100755 --- a/.git_hooks/README.md +++ b/.git_hooks/README.md @@ -7,7 +7,7 @@ ln -s .git_hooks/ ``` > Example: > -> `ln -s .git_hooks/pre-push.sh .git/hooks/pre-push` +> `ln -s ../../.git_hooks/pre-push.sh .git/hooks/pre-push` Alternatively you can change your hooks' path config: ``` diff --git a/.git_hooks/pre-commit.sh b/.git_hooks/pre-commit.sh index c813ff460..dc8053cfa 100755 --- a/.git_hooks/pre-commit.sh +++ b/.git_hooks/pre-commit.sh @@ -1,8 +1,8 @@ #!/bin/sh - set -e -cd "$(git rev-parse --git-dir)/.." +GO_TO="$(git rev-parse --show-toplevel)" +cd "${GO_TO}" #files=$(git diff --diff-filter=AM -STODO --name-only) #echo "$files" @@ -13,20 +13,4 @@ cd "$(git rev-parse --git-dir)/.." # fi #fi -todos=$(git diff --staged --diff-filter=AM -U0 --no-prefix --pickaxe-regex -S"((//|(^|( )+\*)|#)( )*TODO)|\*( )*(@todo)" HEAD) - -echo "${todos}" | awk ' - /^diff / {f="?"; next} - f=="?" {if (/^\+\+\+ /) f=$0"\n"; next} - /^@@/ {n=$3; sub(/,.*/,"",n); n=0+$3; next} - /^\+.*(\/\/( )*TODO|(^|( )+\*)( )*TODO|#( )*TODO|@todo)/ {print f n ":" substr($0,2); f=""} - substr($0,1,1)~/[ +]/ {n++}' - -if [ -n "${todos}" ]; then - echo "" - echo "You have staged TODOs. You have to:" - echo " * Make sure you didn't add a TODO, staged it, removed it and forgot to stage the removal. This script only checks staged files" - echo " * Fix them" - echo " * Use --no-verify flag to avoid this check" - exit 1 -fi +bash .git_hooks/scripts/check_todos.sh diff --git a/.git_hooks/pre-push.sh b/.git_hooks/pre-push.sh index 79fe936b4..803bd975b 100755 --- a/.git_hooks/pre-push.sh +++ b/.git_hooks/pre-push.sh @@ -1,26 +1,20 @@ +#!/bin/bash +set -e -CHECK_TODOS=true -CHECK_FMT=false +FAIL_TODOS=true +CHECK_FMT=true + +GO_TO="$(git rev-parse --show-toplevel)" +cd "${GO_TO}" ############################# # CHECK TODOS # ############################# -todos=$(git diff --diff-filter=AM -U0 --no-prefix --pickaxe-regex -S"((//|(^|( )+\*)|#)( )*TODO)|(@todo)" upstream/main HEAD) - -echo "${todos}" | awk ' - /^diff / {f="?"; next} - f=="?" {if (/^\+\+\+ /) f=$0"\n"; next} - /^@@/ {n=$3; sub(/,.*/,"",n); n=0+$3; next} - /^\+.*(\/\/( )*TODO|(^|( )+\*)( )*TODO|#( )*TODO|@todo)/ {print f n ":" substr($0,2); f=""} - substr($0,1,1)~/[ +]/ {n++}' - -if [ ${CHECK_TODOS} = true ] && [ -n "${todos}" ]; then - echo "" - echo "You have TODOs. You have to:" - echo " * Fix them" - echo " * Use --no-verify flag to avoid this check" - exit 1 +if [ ${FAIL_TODOS} = true ]; then + bash .git_hooks/scripts/check_todos.sh --compare-branch "upstream/main" +else + bash .git_hooks/scripts/check_todos.sh --compare-branch "upstream/main" --warning-mode fi ############################# @@ -28,7 +22,8 @@ fi ############################# if [ ${CHECK_FMT} = true ]; then - echo "sbt scalafmtCheckAll" + echo "[DEBUG] Checking scala format..." + sbt --warn scalafmtCheckAll fi # Check & autoformat --> Disabled: I don't like not knowing the changes diff --git a/.git_hooks/scripts/check_todos.sh b/.git_hooks/scripts/check_todos.sh new file mode 100755 index 000000000..dcd3c53b6 --- /dev/null +++ b/.git_hooks/scripts/check_todos.sh @@ -0,0 +1,100 @@ +#!/bin/bash +set -e + +: ' +The following script shows the TODOs comments added comparing to the given/default branch +' + +############################# +# UTILITIES +############################# +function exitFun { + local exit_code=$? + echo "[DEBUG] ${0} exited with status: ${exit_code}" + exit ${exit_code} +} +# Call the exit function +trap exitFun EXIT + +echo "[DEBUG] Executing $0 with arguments [${*}]" + +############################# +# DEFAULT VALUES +############################# +FAIL_IF_TODOS=true + +############################# +# READ ARGS +############################# +USAGE="Script usage: + bash $(basename "${0}") [-w] [-b upstream/master] [-h] +Flags with * are mandatory. + -w | --warning-mode: If present, do not fail if TODOs found + -b | --compare-branch : Compare HEAD to in order to look for TODOs. If no branch provided, check local changes. + -h | --help: Show script usage" + +while test "${#}" -gt 0; do + case "${1}" in + -w | --warning-mode) + FAIL_IF_TODOS=false + shift 1 + ;; + -b | --compare-branch) + COMPARE_BRANCH=$2 + shift 2 + ;; + -h | --help) + echo "${USAGE}" + exit 0 + ;; + --) + break + ;; + *) + echo -e "[ERROR] $1 is not a recognized argument!\n${USAGE}" + exit 1 + ;; + esac +done + +############################# +# SCRIPT BODY +############################# + +# Get diff for TODOs excluding this file +if [ -n "${COMPARE_BRANCH}" ]; then + echo "[DEBUG] Diff HEAD vs ${COMPARE_BRANCH}" + git_todos=$(git diff --diff-filter=AM -U0 --no-prefix --pickaxe-regex -S"((//|(^|( )+\*)|#)( )*TODO)|(@todo)" "${COMPARE_BRANCH}" HEAD -- "$(basename "${0}")") +else + echo "[DEBUG] Diff HEAD vs local changes" + git_todos=$(git diff --diff-filter=AM -U0 --no-prefix --pickaxe-regex -S"((//|(^|( )+\*)|#)( )*TODO)|(@todo)" HEAD -- "$(basename "${0}")") +fi + +# Format TODOs to print the file and the line where the TODO was found +formatted_todos=$(echo "${git_todos}" | awk ' + /^diff / {f="?"; next} + f=="?" {if (/^\+\+\+ /) f=$0"\n"; next} + /^@@/ {n=$3; sub(/,.*/,"",n); n=0+$3; next} + /^\+.*(\/\/( )*TODO|(^|( )+\*)( )*TODO|#( )*TODO|@todo)/ {print f n ":" substr($0,2); f=""} + substr($0,1,1)~/[ +]/ {n++}') + +if [ -n "${git_todos}" ]; then + echo "[DEBUG] TODOs found" + if [ -z "${formatted_todos}" ]; then + echo "[ERROR] TODOs found but something went wrong when formatting them" + exit 1 + fi + echo "${formatted_todos}" + echo "" + total_todos=$(grep --ignore-case --only-match --count "TODO" <<< "${formatted_todos}") + files_with_todos=$(grep --count "+++" <<< "${formatted_todos}") + echo "[INFO] SUMMARY -> Found ${total_todos} TODOs in ${files_with_todos} files" + + if [ "${FAIL_IF_TODOS}" = true ]; then + echo "" + echo "[ERROR] You have TODOs. You have to:" + echo " * Fix them" + echo " * Use git push --no-verify flag to avoid this check" + exit 1 + fi +fi