Skip to content

Commit

Permalink
Add git hooks for Cloudberry Database development
Browse files Browse the repository at this point in the history
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
tuhaihe committed Sep 26, 2023
1 parent d89fc09 commit 78138a6
Show file tree
Hide file tree
Showing 8 changed files with 239 additions and 54 deletions.
6 changes: 0 additions & 6 deletions hooks/install

This file was deleted.

47 changes: 0 additions & 47 deletions hooks/pre-push

This file was deleted.

2 changes: 1 addition & 1 deletion .gitmessage → src/tools/hooks/.gitmessage
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@ Add your commit body here
#
#
#
#
#
90 changes: 90 additions & 0 deletions src/tools/hooks/commit-msg
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}
23 changes: 23 additions & 0 deletions src/tools/hooks/install
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!"
86 changes: 86 additions & 0 deletions src/tools/hooks/pre-commit
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}
29 changes: 29 additions & 0 deletions src/tools/hooks/pre-push
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
10 changes: 10 additions & 0 deletions src/tools/hooks/prepare-commit-msg
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

0 comments on commit 78138a6

Please sign in to comment.