Last active
July 20, 2024 15:50
-
-
Save samoylenkodmitry/0e988cd3445a0b390be20814eebce589 to your computer and use it in GitHub Desktop.
kotlin_and_java_diff_precommit_hook
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
#!/bin/bash | |
# Setting up guide http://dmitrysamoylenko.com/2020/12/06/configure_lints.html | |
# https://github.com/checkstyle/checkstyle | |
# https://github.com/pinterest/ktlint | |
# https://github.com/detekt/detekt | |
# Determine the Java command to use to start the JVM. | |
if [ -n "$JAVA_HOME" ]; then | |
if [ -x "$JAVA_HOME/jre/sh/java" ]; then | |
# IBM's JDK on AIX uses strange locations for the executables | |
JAVACMD="$JAVA_HOME/jre/sh/java" | |
else | |
JAVACMD="$JAVA_HOME/bin/java" | |
fi | |
if [ ! -x "$JAVACMD" ]; then | |
die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME | |
Please set the JAVA_HOME variable in your environment to match the | |
location of your Java installation." | |
fi | |
else | |
JAVACMD="java" | |
which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. | |
Please set the JAVA_HOME variable in your environment to match the | |
location of your Java installation." | |
fi | |
GIT_ROOT_DIR=$(git rev-parse --show-toplevel) | |
cd ${GIT_ROOT_DIR} | |
#echo ${GIT_ROOT_DIR} | |
f=() | |
while read line; do | |
if [ -n "$line" ]; then | |
if [[ "$line" =~ .*"/build/".* ]]; then | |
true #skip generated code | |
elif [[ "$line" =~ .*"/generated/".* ]]; then | |
true #skip generated code | |
else | |
f+=("$line") | |
fi | |
fi | |
done <<<"$(git diff --diff-filter=d --staged --name-only)" | |
filesJava="" | |
filesKt="" | |
countJava=0 | |
countKt=0 | |
for i in "${!f[@]}"; do | |
if [[ "${f[i]}" == *.java ]]; then | |
filesJava+=" ${f[i]}" | |
countJava=$((countJava + 1)) | |
fi | |
if [[ "${f[i]}" == *.kt ]]; then | |
filesKt+=" ${f[i]}" | |
countKt=$((countKt + 1)) | |
fi | |
done | |
for i in "${!f[@]}"; do | |
if [[ "${f[i]}" =~ .*.(java|kt)$ ]]; then | |
lineNum=0 | |
while IFS= read line; do | |
if [ -n "$line" ]; then | |
lineNum=$((lineNum+1)) | |
if [[ "$line" =~ ^\ +[^\*].* ]]; then | |
echo "Line starts with spaces. Please apply project code style. File: ${f[i]}:$lineNum, line: $line" | |
exit 1 | |
elif [[ "$line" =~ .*oleg.*|.*xoxoxo.* ]]; then | |
echo "forbidden word in line. File: ${f[i]}:$lineNum, line: $line" | |
exit 1 | |
else | |
true #skip good code | |
fi | |
fi | |
done < "${f[i]}" | |
fi | |
done | |
if [ ${#filesJava} -eq 0 ]; then | |
echo "No *.java files to check." | |
else | |
configloc=-Dconfig_loc=${GIT_ROOT_DIR}/ivi/config/checkstyle | |
config=${GIT_ROOT_DIR}/ivi/config/checkstyle/checkstyle.xml | |
params="${configloc} -jar ${GIT_ROOT_DIR}/githooks/checkstyle-8.38-all.jar -c ${config}${filesJava}" | |
${JAVACMD} $params | |
result=$? | |
if [ $result -ne 0 ]; then | |
echo "Please fix the checkstyle problems before submit the commit!" | |
exit $result | |
else | |
echo "#java files: $countJava" | |
fi | |
fi | |
if [ ${#filesKt} -eq 0 ]; then | |
echo "No *.kt files to check." | |
exit 0 | |
fi | |
# ktlint check | |
git diff --diff-filter=d --staged --name-only | grep '\.kt[s"]\?$' | grep -v '.*generated.*' | xargs ./ktlint . | |
result=$? | |
if [ $result -ne 0 ]; then | |
echo "Please fix the ktlint problems before submit the commit!" | |
exit $result | |
fi | |
#detekt check | |
check_by_detekt() { | |
arg=$1 | |
files=${arg%?} | |
if [ ${#files} -eq 0 ]; then | |
true #skip | |
else | |
params="--fail-fast --config default-detekt-config.yml --input ${files}" | |
./detekt ${params} | |
result=$? | |
if [ $result -ne 0 ]; then | |
echo "Please fix the detekt problems before submit the commit!" | |
exit $result | |
fi | |
fi | |
} | |
count=0 | |
filesd="" | |
for i in "${!f[@]}"; do | |
if [[ "${f[i]}" == *.kt ]]; then | |
filesd+="${f[i]}," | |
count=$((count + 1)) | |
# split into batches | |
if [ $count -gt 1000 ]; then | |
check_by_detekt $filesd | |
count=0 | |
filesd="" | |
fi | |
fi | |
done | |
check_by_detekt $filesd | |
echo "# kt files: $count" | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment