Skip to content

Commit

Permalink
feat: [+] improve git_hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
eruizalo committed Jan 22, 2023
1 parent 4806fd3 commit ada4e81
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 38 deletions.
2 changes: 1 addition & 1 deletion .git_hooks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ ln -s .git_hooks/<SOURCE_FILE> <LINK_NAME_NO_EXTENSION>
```
> 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:
```
Expand Down
22 changes: 3 additions & 19 deletions .git_hooks/pre-commit.sh
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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
31 changes: 13 additions & 18 deletions .git_hooks/pre-push.sh
Original file line number Diff line number Diff line change
@@ -1,34 +1,29 @@
#!/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

#############################
# CHECK SCALAFMT #
#############################

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
Expand Down
100 changes: 100 additions & 0 deletions .git_hooks/scripts/check_todos.sh
Original file line number Diff line number Diff line change
@@ -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 <branch>: Compare HEAD to <branch> 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

0 comments on commit ada4e81

Please sign in to comment.