Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pre commit hook #1344

Merged
merged 11 commits into from
May 19, 2020
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
},
"husky": {
"hooks": {
"pre-commit": "./scripts/hooks/apply-formatters.sh && ./scripts/hooks/remove-test-prefixes.sh && ./scripts/hooks/generate-docu.sh"
"pre-commit": "./scripts/hooks/apply-formatters.sh && ./scripts/hooks/remove-test-prefixes.sh && ./scripts/hooks/generate-docu.sh && ./scripts/hooks/prevent-illegal-characters.sh"
}
}
}
42 changes: 42 additions & 0 deletions scripts/hooks/prevent-illegal-characters.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/usr/bin/env bash

# Checks and aborts the commit:
# if (there are files which contain merge conflict markers)
# OR if (`window.addEventListener` is used
# in other files other than EventListenerHelper class)
# conflict_markers = ['<<<<<<<','=======','>>>>>>>']
# Run this script only from Luigi root folder, else not all files will be checked
# ./scripts/hooks/prevent-illegal-characters.sh


function check_conflict_marker() {
declare -a illegal_strings=("<<<<<<<" "=======" ">>>>>>>")
# string diff containing changes already staged for commit
staged_changes=$(git diff --cached --diff-filter=ACMR -- . ':(exclude)scripts/hooks/prevent-illegal-characters.sh')

for illegal in ${illegal_strings[@]}; do
if [[ $staged_changes == *$illegal* ]]; then
echo "Found illegal string: $illegal"
echo "Aborting commit!"
exit 1
fi
done
}

function check_addEventListener_wrong_usage() {
# git search query returning file names that used window.addEventListener illegally
illegal_files=$(git diff --cached --diff-filter=ACMR \
-G 'window.addEventListener' --name-only -- core \
':(exclude)scripts/hooks/prevent-illegal-characters.sh' \
':(exclude)core/src/utilities/helpers/event-listener-helpers.js')
if [ -n "$illegal_files" ]; then
echo "The following files should not contain 'window.addEventListener':"
echo -e "\033[1;31m$illegal_files\033[0m"
echo "Aborting commit!"
exit 1
fi
}

check_conflict_marker

check_addEventListener_wrong_usage