Skip to content

Commit

Permalink
minor updates
Browse files Browse the repository at this point in the history
  • Loading branch information
tuhaihe committed Sep 27, 2023
1 parent 07b573e commit 9cf2471
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
2 changes: 2 additions & 0 deletions src/tools/hooks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ as you like:

```
# Run the command under src/tools/hooks dir
git config core.hooksPath $PWD
```

* Or link the hook files to the `.git/hooks` relative files:

```
# Run the command under src/tool/hooks dir
ln -sf ./pre-commit ../../../.git/hooks/pre-commit
ln -sf ./prepare-commit-msg ../../../.git/hooks/prepare-commit-msg
ln -sf ./commit-msg ../../../.git/hooks/commit-msg
Expand Down
39 changes: 21 additions & 18 deletions src/tools/hooks/commit-msg
Original file line number Diff line number Diff line change
Expand Up @@ -27,64 +27,67 @@ commit_msg=$(cat "$commit_msg_file")

echo -n "Check if the commit message is written... "
if [ -z "$commit_msg" ]; then
echo "${red}Error: Commit message is empty. Please write a commit message."
echo -e "\n ${red}Error: Commit message is empty. Please write a commit message."
exit_code=1
else
echo "${green}OK"
echo "${green}Done"
fi
echo "${reset}"

echo -n "Check the commit title format... "
commit_title=$(echo "$commit_msg" | head -n 1)
if [[ ! "$commit_title" =~ ^[A-Z] ]]; then
echo "${red}Error: Commit title must start with an uppercase letter."
echo -e "\n ${red}Warn: Commit title must start with an uppercase letter!"
exit_code=1
elif [[ "$commit_title" =~ \.$ ]]; then
echo "${red}Error: Commit title cannot end with a period ('.')."
echo -e "\n ${red}Error: Commit title cannot end with a period ('.')."
exit_code=1
elif [[ "$commit_title" =~ '[,。!?]' ]]; then
echo "${red}Error: Commit title cannot contain Non-English punctuation."
echo -e "\n ${red}Error: Commit title cannot contain Non-English punctuation."
exit_code=1
else
echo "${green}OK"
echo "${green}Done"
fi
echo "${reset}"

echo -n "Check the commit title length... "
if [ ${#commit_title} -gt 50 ]; then
echo "${red}Error: Commit title length exceeds 50 characters."
echo -e "\n ${red}Error: Commit title length exceeds 50 characters."
exit_code=1
else
echo "${green}OK"
echo "${green}Done"
fi
echo "${reset}"

echo -n "Check the commit body... "
commit_body=$(echo "$commit_msg" | sed -n '2,$p')
while IFS= read -r line; do
if [ ${#line} -gt 72 ]; then
echo "${red}Error: Commit body line length cannot exceed 72 characters."
echo "${red}For Vim editor, you can use `:set textwidth=72` to autowrap."
echo -e "\n ${red}Error: Commit body line length cannot exceed 72 characters."
echo -e "\n ${red}For Vim editor, you can use `:set textwidth=72` to autowrap."
exit_code=1
elif [[ "$line" =~ '[,。!?]' ]]; then
echo "${red}Error: Commit body line cannot contain Non-English punctuation."
echo -e "\n ${red}Error: Commit body line cannot contain Non-English punctuation."
exit_code=1
else
echo "${green}OK"
echo "${green}Done"
fi
done
echo "${reset}"

echo -n "Check the commit tailer... "
commit_tailer=$(echo "$commit_msg" | tail -n 3)
commit_tailer=$(echo "$commit_msg" | tail -n 2)
if startsWith "$commit_tailer" "See:" && [ ${#commit_tailer} -gt 72 ]; then
echo "${green}OK"
echo "${green}Done"
fi
echo "${reset}"

# If all checks pass, the commit message is valid
if [[ "${exit_code}" != 0 ]]; then
echo "${red}The Checks have been done. Some errors happended!"
echo "Please check and rewrite your commit message again."
echo "You can take our commit conventions as the reference.${reset}"
echo "${red}---------------------------------------------------"
echo "All Checks have been done. Warning happened!"
echo "Please update your commit message again."
echo "---------------------------------------------------"
echo ${reset}
fi
exit ${exit_code}
exit 0

0 comments on commit 9cf2471

Please sign in to comment.