Skip to content

Instantly share code, notes, and snippets.

@aiKrice
Created June 15, 2024 21:27
Show Gist options
  • Select an option

  • Save aiKrice/6c39036509d5e49de7856d5d308b3522 to your computer and use it in GitHub Desktop.

Select an option

Save aiKrice/6c39036509d5e49de7856d5d308b3522 to your computer and use it in GitHub Desktop.
Medium prepare-commit-msg
#! /bin/zsh
source "githooks/android/toolbox.sh"
hook="Prepare-commit-msg check"
progress "$hook"
COMMIT_EDITMSG=$1
commit_message=$(cat $COMMIT_EDITMSG)
char_count=$(echo $commit_message | wc -m)
# Check min char in commit text
if [ $char_count -le $min_char_commit_size ];then
exitWithMessage "You commit message must greater or equal $min_char_commit_size"
fi
# This part will read the COMMIT_EDITMSG file and will prepare by editing it with a convention
# Feel free to change the implementation here to adapt to your convention
# This code describe a conventionnal commit lint and will prefix the text accordingly to this convention
# feat/hooks-12345_branch_name -> feat(hooks-12345): commit message
if ! [[ "$commit_message" =~ $conventionnal_commit_pattern ]]; then #git commit --amend --no-edit
prefix=$(echo "$branchName" | sed -nE 's/^(chore|ci|docs|feat|fix|perf|refactor|style|test)\/.*$/\1/p')
scope=$(echo "$branchName" | sed -nE 's/^[a-z]+\/((hooks)-[0-9]+)_[A-Za-z0-9\-_]{6,}$/\1/p')
if [[ "$commit_message" =~ "^# Please enter the commit" ]]; then #git commit
echo "$prefix($scope):" > $COMMIT_EDITMSG
else # git commit --amend / git commit -m
echo "$prefix($scope): $commit_message" > $COMMIT_EDITMSG
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment