add hooks that roughly test conventionalcommits and reuse

This commit is contained in:
Daniel Wolf 2024-03-14 17:05:51 -04:00
commit ee45715d07
Signed by: nephatrine
GPG Key ID: 59D70EC2E4AAB4D0
2 changed files with 102 additions and 0 deletions

32
commit-msg Normal file
View File

@ -0,0 +1,32 @@
#!/usr/bin/env bash
#
# https://gist.github.com/sophea/660d115ac500f4cab4a062e5171fd5cf
# @author : Mak Sophea
# @version : 1.0#
# Create a regex for a conventional commit.
commit_types="(fix|feat|build|chore|ci|docs|style|refactor|perf|test|revert|wip)"
convetional_commit_regex="^${commit_types}(\([a-z \-]+\))?!?: .+$"
# Get the commit message (the parameter we're given is just the path to the
# temporary file which holds the message).
commit_message=$(cat "$1")
# Check the message, if we match, all good baby.
if [[ "$commit_message" =~ $convetional_commit_regex ]]; then
echo -e "\e[32mCommit message meets Conventional Commit standards...\e[0m"
exit 0
fi
# Uh-oh, this is not a conventional commit, show an example and link to the spec.
echo -e "\e[31mThe commit message does not meet the Conventional Commit standard\e[0m"
echo "An example of a valid message is: "
echo " feat(login): add the 'remember me' button"
echo "More details at: https://www.conventionalcommits.org/en/v1.0.0/#summary"
echo "***********************************************************************"
echo "Here are the list of message type : ${commit_types}"
echo " <type>: <subject> max 50char ex :- fix: invalid request for login api"
echo " <type(<scope>):> <subject> (Max 50 char) - <scope> is option ex: - fix(user): email address is empty on profile api"
echo "***********************************************************************"
exit 1

70
pre-commit Normal file
View File

@ -0,0 +1,70 @@
#!/usr/bin/env bash
#
# https://medium.com/cognitio/verify-license-header-before-commit-2eb0c1841121
# @author : Gobinath Loganathan
missing_copyright=()
year=$(date +'%Y')
IFS=$'\n'
if git rev-parse --verify HEAD >/dev/null 2>&1; then
against=HEAD
else
# Initial Commit
against=$(git hash-object -t tree /dev/null)
fi
# Retrieve the list of newly added files.
new_files=($(git diff --cached --name-only --diff-filter=AM $against))
if [ -n "$new_files" ]; then
for new_file in "${new_files[@]}"; do
test_Copyright=$(grep -L 'SPDX-FileCopyrightText: [12][0-9][0-9][0-9]' "$new_file" 2>&1)
test_Current=$(egrep -L "SPDX-FileCopyrightText: $year|SPDX-FileCopyrightText: [12][0-9][0-9][0-9] - $year" "$new_file" 2>&1)
test_License=$(grep -L 'SPDX-License-Identifier: .' "$new_file" 2>&1)
dir_name=$(dirname "${new_file}")
file_name=$(basename "${new_file}")
if [[ "$new_file" == LICENSE* || "$new_file" == ".reuse/dep5" || -e "$new_file.license" ]]; then
test_Copyright=""
test_Current=""
test_License=""
elif grep -q "${new_file}" .reuse/dep5 2>/dev/null; then
test_Copyright=""
test_Current=""
test_License=""
elif grep -q "${dir_name}/*" .reuse/dep5 2>/dev/null; then
test_Copyright=""
test_Current=""
test_License=""
elif [[ "$new_file" == override/etc/s6-overlay/s6-rc.d* ]]; then
dir_name=$(basename "${dir_name}")
if [[ "$file_name" == "type" || "$dir_name" == "dependencies.d" || "$dir_name" == "contents.d" ]]; then
missing_copyright+=($new_file)
echo -e "\e[31mAdd DEP5 License Override:\e[0m $new_file"
test_Copyright=""
test_Current=""
test_License=""
fi
fi
if [ -n "$test_Copyright" ]; then
missing_copyright+=($test_Copyright)
echo -e "\e[31mMissing SPDX-FileCopyrightText:\e[0m $test_Copyright"
elif [ -n "$test_License" ]; then
missing_copyright+=($test_License)
echo -e "\e[31mMissing SPDX-License-Identifier:\e[0m $test_License"
elif [ -n "$test_Current" ]; then
missing_copyright+=(${test_Current})
echo -e "\e[31mSPDX-FileCopyrightText Needs $year Update:\e[0m $test_Current"
fi
done
if [ -n "$missing_copyright" ]; then
exit 1
else
echo -e "\e[32mNew files have appropriate SPDX information.\e[0m";
exit 0
fi
fi