-
Notifications
You must be signed in to change notification settings - Fork 136
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add git hooks for Cloudberry Database development
For better development quality, we customize the Git hooks to remind developers to check their work. We use pre-commit, prepare-commit-msg, commit-msg, and pre-push hooks to trigger automatic checks when running `git commit` and `git push`.
- Loading branch information
Showing
8 changed files
with
239 additions
and
54 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,4 +44,4 @@ Add your commit body here | |
# | ||
# | ||
# | ||
# | ||
# |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
#!/bin/sh | ||
# | ||
# A hook script to check the commit log message. Called by "git commit" with | ||
# one argument, the name of the file that has the commit message. The hook | ||
# should exit with non-zero status after issuing an appropriate message if it | ||
# wants to stop the commit. The hook is allowed to edit the commit message | ||
# file. | ||
# | ||
# Function to check if a string starts with a specific prefix | ||
|
||
readonly reset=$(tput sgr0) | ||
readonly red=$(tput bold; tput setaf 1) | ||
readonly green=$(tput bold; tput setaf 2) | ||
|
||
exit_code=0 | ||
|
||
startsWith() { | ||
case $1 in | ||
"$2"*) return 0;; | ||
*) return 1;; | ||
esac | ||
} | ||
|
||
# Read the commit message from the commit-msg hook input | ||
commit_msg_file=$1 | ||
commit_msg=$(cat "$commit_msg_file") | ||
|
||
echo -ne "Check if the commit message is written... " | ||
if [ -z "$commit_msg" ]; then | ||
echo "${red}Error: Commit message is empty. Please write a commit message." | ||
exit_code=1 | ||
else | ||
echo "${green}OK" | ||
fi | ||
echo "${reset}" | ||
|
||
echo -ne "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." | ||
exit_code=1 | ||
elif [[ "$commit_title" =~ \.$ ]]; then | ||
echo "${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." | ||
exit_code=1 | ||
elif | ||
echo "${green}OK" | ||
fi | ||
echo "${reset}" | ||
|
||
echo -ne "Check the commit title length... " | ||
if [ ${#commit_title} -gt 50 ]; then | ||
echo "${red}Error: Commit title length exceeds 50 characters." | ||
exit_code=1 | ||
else | ||
echo "${green}OK" | ||
fi | ||
echo "${reset}" | ||
|
||
echo -ne "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." | ||
exit_code=1 | ||
elif [[ "$line" =~ '[,。!?]' ]]; then | ||
echo "${red}Error: Commit body line cannot contain Non-English punctuation." | ||
exit_code=1 | ||
else | ||
echo "${green}OK" | ||
fi | ||
done <<< "$commit_body" | ||
echo "${reset}" | ||
|
||
echo -ne "Check the commit tailer... " | ||
commit_tailer=$(echo "$commit_msg" | tail -n 3) | ||
if startsWith "$commit_tailer" "See:" && [ ${#commit_tailer} -gt 72 ]; then | ||
echo "${green}OK" | ||
fi | ||
|
||
# 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}" | ||
fi | ||
exit ${exit_code} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#!/bin/sh | ||
# This script is used to configure src/tools/hooks as the git hooks dir. | ||
|
||
readonly red=$(tput bold; tput setaf 1) | ||
readonly green=$(tput bold; tput setaf 2) | ||
|
||
echo "Now config the hooks for Cloudberry Database...\n${red}Don't stop me!" | ||
|
||
sleep 1 | ||
|
||
echo "Configure the Hookspath as $PWD" | ||
git config core.hooksPath $PWD | ||
|
||
echo "Make the Hooks as executable..." | ||
|
||
chmod +x commit-msg | ||
chmod +x install | ||
chmod +x pre-commit | ||
chmod +x pre-push | ||
chmod +x prepare-commit-msg | ||
|
||
sleep 1 | ||
echo "\n${green}Done! Happy Hacking!\nThanks for your support!" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
#!/bin/sh | ||
# | ||
# A hook script to verify what is about to be committed. Called by | ||
# "git commit" with no arguments. The hook should exit with non-zero | ||
# status after issuing an appropriate message if it wants to stop the | ||
# commit. | ||
|
||
readonly reset=$(tput sgr0) | ||
readonly red=$(tput bold; tput setaf 1) | ||
readonly green=$(tput bold; tput setaf 2) | ||
|
||
exit_code=0 | ||
|
||
echo -ne "Checking if new commits exits... " | ||
if git rev-parse --verify HEAD >/dev/null 2>&1 | ||
then | ||
against=HEAD | ||
else | ||
# Initial commit: diff against an empty tree object | ||
against=$(git hash-object -t tree /dev/null) | ||
fi | ||
echo "${green}Done" | ||
echo "${reset}" | ||
|
||
# The following script is copied from pre-commit.sample to avoid | ||
# non-ASCII filenames from being added to the repository. | ||
|
||
echo -ne "Running non-ASCII filenames check..." | ||
allownonascii=$(git config --type=bool hooks.allownonascii) | ||
exec 1>&2 | ||
if [ "$allownonascii" != "true" ] && | ||
test $(git diff --cached --name-only --diff-filter=A -z $against | | ||
LC_ALL=C tr -d '[ -~]\0' | wc -c) != 0 | ||
then | ||
cat <<\EOF | ||
${red}Error: Attempt to add a non-ASCII file name. | ||
This can cause problems if you want to work with people on other platforms. | ||
To be portable it is advisable to rename the file. | ||
If you know what you are doing you can disable this check using: | ||
git config hooks.allownonascii true | ||
EOF | ||
echo "${reset}" | ||
exit_code=1 | ||
else | ||
echo "${green}Done" | ||
fi | ||
|
||
# If there are whitespace errors, print the offending file names and fail. | ||
echo -ne "Running whitespace checks... " | ||
exec git diff-index --check --cached $against -- | ||
|
||
# This following hook is modified from PostgreSQL wiki, which is used | ||
# to check the changed code by pgindent. See the original link: | ||
# https://wiki.postgresql.org/wiki/Working_with_Git#Using_git_hooks | ||
|
||
set -u | ||
: ${PGAUTOINDENT:=no} | ||
|
||
branch=$(git rev-parse --abbrev-ref HEAD) | ||
files=$(git diff --cached --name-only --diff-filter=ACMR) | ||
|
||
check_indent () { | ||
# no need to filter files - pgindent ignores everything that isn't a | ||
# .c or .h file | ||
src/tools/pgindent/pgindent --silent-diff $files && return 0 | ||
exec 2>&1 | ||
if [ "$PGAUTOINDENT" = yes ] ; then | ||
echo "Running pgindent on changed files... " | ||
src/tools/pgindent/pgindent $files | ||
echo "${red}Commit abandoned! Rerun git commit to adopt pgindent changes." | ||
else | ||
echo 'You need a pgindent run, e.g:' | ||
echo -n 'src/tools/pgindent/pgindent ' | ||
echo '`git diff --name-only --diff-filter=ACMR`' | ||
fi | ||
exit_code=1 | ||
} | ||
|
||
# nothing to do if there are no files | ||
test -z "$files" && exit 0 | ||
check_indent | ||
|
||
if [[ "${exit_code}" != 0 ]]; then | ||
echo "${red}Please update your commits again per the errors.${reset}" | ||
fi | ||
exit ${exit_code} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#!/bin/bash | ||
# A hook for the reminder before pushing to the original repo. | ||
|
||
remote="$1" | ||
url="$2" | ||
|
||
z40=0000000000000000000000000000000000000000 | ||
|
||
function user_input_to_proceed() { | ||
read -p 'Proceed with push? (y/N): ' yesno </dev/tty | ||
case "$yesno" in | ||
y|Y|yes|Yes|YES) ;; | ||
*) echo "Aborting push" >&2; exit 1 ;; | ||
esac | ||
} | ||
|
||
while read local_ref local_sha remote_ref remote_sha ; do | ||
if [ "$local_sha" = $z40 ] ; then | ||
# Handle delete | ||
continue | ||
fi | ||
if [ "$remote_sha" = $z40 ] ; then | ||
# New branch | ||
continue | ||
fi | ||
# Update to existing branch, examine new commits | ||
range="$remote_sha..$local_sha" | ||
|
||
exit 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#!/bin/sh | ||
# | ||
# A hook script to prepare the commit log message. Called by "git commit" with | ||
# the name of the file that has the commit message, followed by the | ||
# description of the commit message's source. The hook's purpose is to edit | ||
# the commit message file. If the hook fails with a non-zero status, the | ||
# commit is aborted. | ||
# | ||
|
||
cat .gitmessage > $1 |