Skip to content

Commit

Permalink
Component: git hooks
Browse files Browse the repository at this point in the history
Add common error function in hooks
Error messages are printed in stderr

Reviewed By: tcberner, #portmgr
Differential Revision: https://reviews.freebsd.org/D38026
  • Loading branch information
pizzamig committed Jan 12, 2023
1 parent 8bdd13c commit 302c208
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 14 deletions.
7 changes: 6 additions & 1 deletion .hooks/pre-commit.d/check_category_makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,19 @@
# Check that ports are hooked into the build
#

common_functions="$(realpath "$(dirname "$0")")/common.sh"
if [ -r "${common_functions}" ]; then
. "${common_functions}"
fi

newish_makefiles=$(git diff --name-only --cached --diff-filter=ACR | grep -E '^[^/]+/[^/]+/Makefile$')
if [ $? -eq 0 ] ; then
for newish_makefile in ${newish_makefiles} ; do
category=$(echo "${newish_makefile}" | awk -F '/' '{print $1}')
port=$(echo "${newish_makefile}" | awk -F '/' '{print $2}')
grep -q -E "^[[:space:]]+SUBDIR[[:space:]]\+=[[:space:]]*${port}\$" ${category}/Makefile
if [ $? -ne 0 ] ; then
echo "[pre-commit] ERROR: Missing 'SUBDIR += ${port}' in ${category}/Makefile"
pre_commit_error "Missing 'SUBDIR += ${port}' in ${category}/Makefile"
exit 1
fi
done
Expand Down
13 changes: 9 additions & 4 deletions .hooks/pre-commit.d/check_created_by
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,20 @@
# Check that ports do not contain a 'Created by' tag
#

common_functions="$(realpath "$(dirname "$0")")/common.sh"
if [ -r "${common_functions}" ]; then
. "${common_functions}"
fi

makefiles=$(git diff --name-only --cached --diff-filter=ACMR | grep -E '^[^/]+/[^/]+/Makefile$')
if [ $? -eq 0 ] ; then
for makefile in ${makefiles} ; do
created_by=$(head -n1 ${makefile} | awk -F : '/Created by/{print $NF}')
if [ -n "${created_by}" ] ; then
echo -e "[pre-commit] ERROR: ${makefile} contains obsolete 'Created by' line\n" \
" Please remove the the line, and append:\n\n" \
" - Removed 'Created by ${created_by}'.\n\n" \
" to your commit message."
pre_commit_error "${makefile} contains obsolete 'Created by' line\n" \
" Please remove the the line, and append:\n\n" \
" - Removed 'Created by ${created_by}'.\n\n" \
" to your commit message."
exit 1
fi
done
Expand Down
9 changes: 7 additions & 2 deletions .hooks/pre-commit.d/check_files
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
# pkg-.*
#

common_functions="$(realpath "$(dirname "$0")")/common.sh"
if [ -r "${common_functions}" ]; then
. "${common_functions}"
fi

category_regex="($(make -VSUBDIR | sed 's# #\|#g'))"
newish_files=$(git diff --name-only --cached --diff-filter=ACR | grep -E "^${category_regex}/[^/]+/[^/]+$")

Expand All @@ -19,12 +24,12 @@ if [ $? -eq 0 ] ; then
file=$(echo "${newish_file}" | awk -F '/' '{print $3}')
valid=$(echo "${file}" | grep -Eq '^((Makefile|distinfo|pkg-)(.*))|(.*\.mk)$')
if [ $? -ne 0 ] ; then
echo "[pre-commit] ERROR: invalid file '${file}' in '${category}/${port}'"
pre_commit_error "ERROR: invalid file '${file}' in '${category}/${port}'"
status=1
fi
done
fi
if [ ${status} -eq 1 ] ; then
echo " Consider moving non-standard files to files/ or force-ignore this hook."
error " Consider moving non-standard files to files/ or force-ignore this hook."
exit 1
fi
9 changes: 7 additions & 2 deletions .hooks/pre-commit.d/check_mk_indentations
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
# Check that Mk/ files are properly indented
#

common_functions="$(realpath "$(dirname "$0")")/common.sh"
if [ -r "${common_functions}" ]; then
. "${common_functions}"
fi

check_indentation() {
local mkfile="$1"
local name=$(echo "${mkfile}" | awk -F / '{print $NF}')
Expand All @@ -13,12 +18,12 @@ check_indentation() {
fi
cp ${mkfile} ${tempdir} && Tools/scripts/indent_make_if.pl ${tempdir}/${name}
if [ $? -ne 0 ] ; then
echo "[pre-commit] failed to run Tools/scripts/indent_make_if.pl"
pre_commit_error "failed to run Tools/scripts/indent_make_if.pl"
exit 2
fi
cmp -s ${tempdir}/*
if [ $? -ne 0 ] ; then
echo "[pre-commit] ${name} is not properly indented -- please use ${tempdir}/${name} which was created using Tools/scripts/indent_make_if.pl"
pre_commit_error "${name} is not properly indented -- please use ${tempdir}/${name} which was created using Tools/scripts/indent_make_if.pl"
exit 1
fi
}
Expand Down
10 changes: 7 additions & 3 deletions .hooks/pre-commit.d/check_moved
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
# Check that newly added MOVED lines are valid
#

common_functions="$(realpath "$(dirname "$0")")/common.sh"
if [ -r "${common_functions}" ]; then
. "${common_functions}"
fi

moved_changed=$(git diff --name-only --cached --diff-filter=M | grep -E '^MOVED$')
if [ $? -eq 0 ] ; then
Expand All @@ -13,9 +17,9 @@ if [ $? -eq 0 ] ; then

errors=$(PORTSDIR=${tree} Tools/scripts/MOVEDlint.awk -v lastdate="${lastdate}")
if [ $? -ne 0 ] ; then
echo -e "[pre-commit] ERROR: MOVED contains errors.\n" \
" Please apply the suggested changes:\n"
echo "${errors}"
pre_commit_error "MOVED contains errors.\n" \
" Please apply the suggested changes:\n"
error "${errors}"
exit 1
fi
fi
9 changes: 7 additions & 2 deletions .hooks/pre-commit.d/check_portepoch
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,22 @@
# Check that PORTEPOCH is not being dropped, and is non-decreasing
#

common_functions="$(realpath "$(dirname "$0")")/common.sh"
if [ -r "${common_functions}" ]; then
. "${common_functions}"
fi

check_epoch() {
local makefile="$1"
local old_epoch=$(git diff --cached -U0 "${makefile}" | grep '^-PORTEPOCH.*=' | grep -oE '[0-9]+')
local new_epoch=$(git diff --cached -U0 "${makefile}" | grep '^+PORTEPOCH.*=' | grep -oE '[0-9]+')
if [ -z "${new_epoch}" -a -n "${old_epoch}" ] ; then
echo "[pre-commit] dropped PORTEPOCH ${old_epoch} in ${makefile}"
pre_commit_error "dropped PORTEPOCH ${old_epoch} in ${makefile}"
exit 1
fi
if [ -n "${old_epoch}" ] ; then
if [ ${new_epoch} -lt ${old_epoch} ] ; then
echo "[pre-commit] PORTEPOCH decreasing from ${old_epoch} to ${new_epoch} in ${makefile}"
pre_commit_error "PORTEPOCH decreasing from ${old_epoch} to ${new_epoch} in ${makefile}"
exit 2
fi
fi
Expand Down
7 changes: 7 additions & 0 deletions .hooks/pre-commit.d/common.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
error() {
echo -e "$*" > /dev/stderr
}

pre_commit_error() {
error "[pre-commit] ERROR: $*"
}

0 comments on commit 302c208

Please sign in to comment.