commit 435b19d78705b09c6615315984c4379aad4781d8
parent d58e226781f7a4546a1a8b5c0736af593297585b
Author: Evgeny Grin (Karlson2k) <k2k@drgrin.dev>
Date: Sat, 24 May 2025 20:46:59 +0200
uncrustify_precommit: fixed portability, added support for staged files and strange file names (with spaces)
Previously, if file on disk has been fixed, and unfixed version is still
staged, the check succeed, but unfixed version is committed.
Can be backported.
Update your repo after pulling this commit by running:
rm -f .git/hooks/pre-commit && ./bootstrap
Diffstat:
3 files changed, 77 insertions(+), 92 deletions(-)
diff --git a/bootstrap b/bootstrap
@@ -81,8 +81,10 @@ if test ! -f './src/include/microhttpd2.h'; then
exit 2
fi
-if have_cmd uncrustify; then
- UNCRUSTIFY_VER=`uncrustify --version | ver_filter`
+: "${UNCRUSTIFY=uncrustify}"
+
+if have_cmd ${UNCRUSTIFY}; then
+ UNCRUSTIFY_VER=`${UNCRUSTIFY} --version | ver_filter`
UNCRUSTIFY_VER_NEEDED="0.78.0"
if ver_cmp "$UNCRUSTIFY_VER" -ge "$UNCRUSTIFY_VER_NEEDED"; then
if test -f uncrustify.cfg; then
@@ -95,7 +97,7 @@ if have_cmd uncrustify; then
echo "Failed to install uncrustify configuration file" 1>&2
fi
if test -f uncrustify.cfg; then
- if test -d '.git' && test -x /bin/bash; then
+ if test -d '.git'; then
if ver_cmp `git --version 2>/dev/null | ver_filter` -lt "1.5.5.2"; then
echo "git version 1.5.5.2 (or later) not detected; pre-commit hook not installed."
else
@@ -104,13 +106,13 @@ if have_cmd uncrustify; then
else
echo "Installing uncrustify pre-commit git hook"
rm -f ./.git/hooks/pre-commit
- ln -s ../../contrib/uncrustify_precommit .git/hooks/pre-commit || \
- cp contrib/uncrustify_precommit .git/hooks/pre-commit || \
+ ln -s ../../contrib/uncrustify_precommit.sh .git/hooks/pre-commit || \
+ cp contrib/uncrustify_precommit.sh .git/hooks/pre-commit || \
echo "Failed to install pre-commit git hook" 1>&2
fi
fi
else
- echo "No '.git' directory or no bash shell found, skipping installation of pre-commit git hook."
+ echo "No '.git' directory found, skipping installation of pre-commit git hook."
fi
fi
else
diff --git a/contrib/uncrustify_precommit b/contrib/uncrustify_precommit
@@ -1,86 +0,0 @@
-#!/bin/bash
-
-# use as .git/hooks/pre-commit
-
-function vercomp () {
- if [[ $1 == $2 ]]
- then
- return 0
- fi
- local IFS=.
- local i ver1=($1) ver2=($2)
- # fill empty fields in ver1 with zeros
- for ((i=${#ver1[@]}; i<${#ver2[@]}; i++))
- do
- ver1[i]=0
- done
- for ((i=0; i<${#ver1[@]}; i++))
- do
- if [[ -z ${ver2[i]} ]]
- then
- # fill empty fields in ver2 with zeros
- ver2[i]=0
- fi
- if ((10#${ver1[i]} > 10#${ver2[i]}))
- then
- return 1
- fi
- if ((10#${ver1[i]} < 10#${ver2[i]}))
- then
- return 2
- fi
- done
- return 0
-}
-
-exec 1>&2
-
-if ! uncrustify --version > /dev/null
-then
- echo 'uncrustify required'
- exit 1
-fi
-
-HAVE=$(uncrustify --version | sed -e 's/.*-//' -e 's/_.//')
-
-vercomp 0.78.0 "$HAVE"
-case $? in
- 0)
- ;;
- 1)
- echo "your uncrustify is too old";
- exit 1
- ;;
- 2)
- ;;
-esac
-
-RET=0
-changed=$(git diff --cached --name-only)
-crustified=""
-
-for f in $changed;
-do
- if echo $f | grep \\.[c,h]\$ > /dev/null
- then
- # compare result of uncrustify with changes
- #
- # only change any of the invocations here if
- # they are portable across all cmp and shell
- # implementations !
- uncrustify -q -c uncrustify.cfg -f $f | cmp -s $f -
- if test $? = 1 ;
- then
- crustified=" $crustified $f"
- RET=1
- fi
- fi
-done
-
-if [ $RET = 1 ];
-then
- echo "Run"
- echo "uncrustify --no-backup -c uncrustify.cfg ${crustified}"
- echo "before committing."
-fi
-exit $RET
diff --git a/contrib/uncrustify_precommit.sh b/contrib/uncrustify_precommit.sh
@@ -0,0 +1,69 @@
+#!/bin/sh
+
+# use as .git/hooks/pre-commit
+
+# Note: keep this script portable!
+
+# LICENSE
+#
+# Copyright (c) 2025 Karlson2k (Evgeny Grin) <k2k@drgrin.dev>
+# Copyright (C) 2019,2024 Christian Grothoff
+# Copying and distribution of this file, with or without modification, are
+# permitted in any medium without royalty provided the copyright notice
+# and this notice are preserved. This file is offered as-is, without any
+# warranty.
+
+# Enable 'pipefail' if supported
+set -o pipefail 2>/dev/null || :
+
+# Redirect all output to stderr
+exec 1>&2
+
+if command -v "command" >/dev/null 2>&1; then
+have_cmd()
+{
+ command -v "$1" >/dev/null 2>&1
+}
+elif type "type" >/dev/null 2>&1; then
+have_cmd()
+{
+ type "$1" >/dev/null 2>&1
+}
+else
+have_cmd()
+{
+ : # dummy fallback
+}
+fi
+
+: "${UNCRUSTIFY=uncrustify}"
+UNCRUSTIFY_CFG="uncrustify.cfg"
+
+if have_cmd ${UNCRUSTIFY}; then :; else
+ echo "Uncrustify not detected, code formating cannot be checked."
+ exit 6
+fi
+
+STAGED_FILES=`git diff --cached --name-only --no-renames --diff-filter=ACM | ${SED-sed} -n '/\.[ch]$/p'` || exit 5
+BROKEN_FILES=""
+
+GIT_NOGLOB_PATHSPECS="1"
+precomm_save_IFS="$IFS"
+IFS="
+"
+for f in ${STAGED_FILES}; do
+ IFS="$precomm_save_IFS"
+ if git cat-file blob -- ":0:$f" | "${UNCRUSTIFY}" -q -c "$UNCRUSTIFY_CFG" --check --assume "$f" >/dev/null 2>&1; then :; else
+ BROKEN_FILES="$BROKEN_FILES '$f'"
+ fi
+done
+IFS="$precomm_save_IFS"
+
+if test -n "${BROKEN_FILES}"; then
+ echo "Run"
+ echo " ${UNCRUSTIFY} -c '$UNCRUSTIFY_CFG' --replace --no-backup${BROKEN_FILES}"
+ echo "in '`pwd`' directory and then stage modified files before committing."
+ exit 3
+fi
+
+exit 0