precommit_hook.sh (2357B)
1 #!/bin/sh 2 3 # use as .git/hooks/pre-commit 4 5 # Note: keep this script portable! 6 7 # LICENSE 8 # 9 # Copyright (c) 2025 Karlson2k (Evgeny Grin) <k2k@drgrin.dev> 10 # Copyright (C) 2019,2024 Christian Grothoff 11 # Copying and distribution of this file, with or without modification, are 12 # permitted in any medium without royalty provided the copyright notice 13 # and this notice are preserved. This file is offered as-is, without any 14 # warranty. 15 16 # Enable 'pipefail' if supported -- this breaks git commit if -o piefail is not supported! 17 # set -o pipefail 2>/dev/null || : 18 19 # Redirect all output to stderr 20 exec 1>&2 21 22 # Note: use full set of symbols in regexp instead of ranges to as ranges 23 # can be exanded differently depending on LC_* and LANG variables. 24 whtlst_chrsF="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789._" 25 whtlst_chrsO="${whtlst_chrsF}~-" # symbol '-' must be the last! 26 whtlst_fname="[${whtlst_chrsF}][${whtlst_chrsO}]*" 27 BAD_NAMES=`git diff --cached --name-only --no-renames --diff-filter=ACR \ 28 | ${SED-sed} "/^${whtlst_fname}\(\/${whtlst_fname}\)*\$/d; \ 29 /^po\/en@${whtlst_fname}\$/d"` || exit 5 30 if test -n "$BAD_NAMES"; then 31 echo "The following file(s) have illigal names, rename them before commiting: 32 $BAD_NAMES" 33 exit 2 34 fi 35 36 if command -v "command" >/dev/null 2>&1; then 37 have_cmd() 38 { 39 command -v "$1" >/dev/null 2>&1 40 } 41 elif type "type" >/dev/null 2>&1; then 42 have_cmd() 43 { 44 type "$1" >/dev/null 2>&1 45 } 46 else 47 have_cmd() 48 { 49 : # dummy fallback 50 } 51 fi 52 53 : "${UNCRUSTIFY=uncrustify}" 54 UNCRUSTIFY_CFG="uncrustify.cfg" 55 56 if have_cmd ${UNCRUSTIFY}; then :; else 57 echo "Uncrustify not detected, code formating cannot be checked." 58 exit 6 59 fi 60 61 STAGED_FILES=`git diff --cached --name-only --no-renames --diff-filter=ACM | ${SED-sed} -n '/\.[ch]$/p'` || exit 5 62 BROKEN_FILES="" 63 64 GIT_NOGLOB_PATHSPECS="1" 65 precomm_save_IFS="$IFS" 66 IFS=" 67 " 68 for f in ${STAGED_FILES}; do 69 IFS="$precomm_save_IFS" 70 if git cat-file blob -- ":0:$f" | "${UNCRUSTIFY}" -q -c "$UNCRUSTIFY_CFG" --check --assume "$f" >/dev/null 2>&1; then :; else 71 BROKEN_FILES="$BROKEN_FILES '$f'" 72 fi 73 done 74 IFS="$precomm_save_IFS" 75 76 if test -n "${BROKEN_FILES}"; then 77 echo "Run" 78 echo " ${UNCRUSTIFY} -c '$UNCRUSTIFY_CFG' --replace --no-backup${BROKEN_FILES}" 79 echo "in '`pwd`' directory and then stage modified files before committing." 80 exit 3 81 fi 82 83 exit 0