Skip to content

Instantly share code, notes, and snippets.

@disouzam
Created December 24, 2022 02:57
Show Gist options
  • Save disouzam/21831510dbcc117a0609a7c3d1138a46 to your computer and use it in GitHub Desktop.
Save disouzam/21831510dbcc117a0609a7c3d1138a46 to your computer and use it in GitHub Desktop.
Pre commit hook
#!/bin/sh
BLUE='\033[0;34m'
RED='\033[0;31m'
NOCOLOR='\033[0m'
# Check if the version of pre-commit hook was updated in version control
# since it is not possible to version control the actual pre-commit script
original_pre_commit_hook=".git/hooks/pre-commit"
pre_commit_hook_in_version_control=".git-hooks-for-reuse/pre-commit"
if ! cmp -s "$original_pre_commit_hook" "$pre_commit_hook_in_version_control";
then
echo -e "${pre_commit_hook_in_version_control} ${RED}is different${NOCOLOR} than the executed version ${BLUE}${original_pre_commit_hook}${NOCOLOR}"
echo ""
echo ""
echo -e "${BLUE}Please update version-controlled pre-commit script (${RED}${pre_commit_hook_in_version_control}${NOCOLOR})"
echo ""
echo "Aborting execution of pre-commit hook..."
exit 1
fi
# {mainProjectName}
folder="{mainProjectName}"
changelogfile="${folder}/ChangeLog.md"
# Checking whether folder has been renamed or change log file is not present in folder
if [[ ! -f ${changelogfile} ]]; then
echo -e "${RED}${changelogfile} was not found."
echo -e "${BLUE}Please add this file before resuming your commit..."
exit 1
fi
changelist=$(git diff --cached --stat ${folder}/**)
changeliststat=$(git diff --cached --shortstat ${folder}/**)
changelogfilediff=$(git diff --cached --shortstat ${changelogfile})
if [[ ${changeliststat} =~ .*"file"*. ]]
then
echo ""
echo -e "${BLUE}Changes in folder to be committed: ${folder}${NOCOLOR}"
echo "${changelist}"
echo ""
echo "There are changes in ${folder} folder..."
if [[ ! ${changelogfilediff} =~ .*"file"*. ]]
then
echo -e " ${RED}but ${folder}/ChangeLog.md is OUTDATED.${NOCOLOR}"
echo ""
echo "Aborting commit..."
exit 1
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment