Skip to content

Commit

Permalink
Add pre commit hook (#1344)
Browse files Browse the repository at this point in the history
* testing commit

* test hook 2

* restart hook test

* hook test 4

* Update scripts/hooks/prevent-illegal-characters.sh

Co-authored-by: Markus <1720843+maxmarkus@users.noreply.github.com>

* Update scripts/hooks/prevent-illegal-characters.sh

Co-authored-by: Markus <1720843+maxmarkus@users.noreply.github.com>

* update comments

Co-authored-by: Markus <1720843+maxmarkus@users.noreply.github.com>
  • Loading branch information
ndricimrr and maxmarkus authored May 19, 2020
1 parent d58292e commit 57711d9
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
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

0 comments on commit 57711d9

Please sign in to comment.