Skip to content

Instantly share code, notes, and snippets.

@quynhethereal
Last active April 22, 2022 04:30
Show Gist options
  • Save quynhethereal/e9fe0983620e3ecc1ce92f5114145b14 to your computer and use it in GitHub Desktop.
Save quynhethereal/e9fe0983620e3ecc1ce92f5114145b14 to your computer and use it in GitHub Desktop.
Git pre-commit script to scan for unsafe HTTP usage in PR
#!/bin/bash
#This script is used in .git/hooks/pre-commit file. If you run it from another folder, you may need
#add flag --relative to git diff line so that the output of git diff is correct.
echo '------Scanning for unsafe HTTP usage'
#git diff --cached --name-only will return the names of the changed files
git diff --cached --name-only| while read FILE; do
if [[ $(echo "$FILE") ]]; then
content=$(<"$FILE")
if [[ $(echo "$content" | grep -e "http://" -e "HTTP://") ]]; then
echo -e "You have used unsafe HTTP connection in the file: "$FILE". Change it to HTTPS!" >&2
exit 1
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment