From 1d2ed53dd9d9a509fd4e51f1bafd916d6f817267 Mon Sep 17 00:00:00 2001 From: Amos Jeffries Date: Sat, 11 Apr 2020 18:21:40 +0000 Subject: [PATCH] Support plugin-style scripts for source format enforcement (#531) Allow the source-maintenance script to run arbitrary code or sub-scripts to perform enforcement of Squid code style and content. Code placed in the scripts/maintenance/ sub-folder MUST meet the following criteria: * be self-executable, * receive filename of the code file to be touched as one and only command-line parameter, * always dump the file contents to stdout (with or without edits), * not depend on any other code in this sub-folder being run first. --- scripts/source-maintenance.sh | 84 +++++++++++++++++++++++++++++++++-- 1 file changed, 81 insertions(+), 3 deletions(-) diff --git a/scripts/source-maintenance.sh b/scripts/source-maintenance.sh index a4ecabd163..872857ee77 100755 --- a/scripts/source-maintenance.sh +++ b/scripts/source-maintenance.sh @@ -21,6 +21,38 @@ # If code alteration takes place the process is halted for manual intervention. # +# whether to continue execution after a failure +# TODO: Expand the subset of failures covered by this feature; see run_(). +KeepGoing="no" +# the actual name of the directive that enabled keep-going mode +KeepGoingDirective="" + +# command-line options +while [ $# -ge 1 ]; do + case "$1" in + --keep-going|-k) + KeepGoing=yes + KeepGoingDirective=$1 + shift + ;; + *) + echo "Usage: $0 [--keep-going|-k]" + echo "Unsupported command-line option: $1" + exit 1; + ;; + esac +done + +# an error code seen by a KeepGoing-aware command (or zero) +SeenErrors=0 + + +if ! git diff --quiet; then + echo "There are unstaged changes. This script may modify sources." + echo "Stage changes to avoid permanent losses when things go bad." + exit 1 +fi + # On squid-cache.org we have to use the python scripted md5sum HOST=`hostname` if test "$HOST" = "squid-cache.org" ; then @@ -40,7 +72,48 @@ fi COPYRIGHT_YEARS=`date +"1996-%Y"` echo "s/1996-2[0-9]+ The Squid Software Foundation and contributors/${COPYRIGHT_YEARS} The Squid Software Foundation and contributors/g" >>boilerplate_fix.sed -srcformat () +# executes the specified command +# in KeepGoing mode, remembers errors and hides them from callers +run_ () +{ + "$@" && return; # return on success + error=$? + + if test $KeepGoing = no; then + return $error + fi + + echo "ERROR: Continuing after a failure ($error) due to $KeepGoingDirective" + SeenErrors=$error # TODO: Remember the _first_ error instead + return 0 # hide error from the caller +} + +updateIfChanged () +{ + original="$1" + updated="$2" + message="$3" + + if ! cmp -s "${original}" "${updated}"; then + echo "NOTICE: File ${original} changed: ${message}" + run_ mv "${updated}" "${original}" || return + else + run_ rm -f "${updated}" || exit $? + fi +} + +# uses the given script to update the given source file +applyPlugin () +{ + script="$1" + source="$2" + + new="$source.new" + $script "$source" > "$new" && + updateIfChanged "$source" "$new" "by $script" +} + +srcFormat () { # # Scan for incorrect use of #ifdef/#ifndef @@ -66,7 +139,10 @@ for FILENAME in `git ls-files`; do # # Code Style formatting maintenance # - if test "${ASVER}"; then + for SCRIPT in `git ls-files scripts/maintenance/`; do + run_ applyPlugin ${SCRIPT} "${FILENAME}" || return + done + if test "${ASVER}"; then ./scripts/formater.pl ${FILENAME} if test -e $FILENAME -a -e "$FILENAME.astylebak"; then md51=`cat $FILENAME| tr -d "\n \t\r" | $MD5`; @@ -287,8 +363,10 @@ make -C src/http gperf-files # Run formatting echo "" >doc/debug-sections.tmp -srcformat || exit 1 +srcFormat || exit 1 sort -u doc/debug-sections.tmp2 cat scripts/boilerplate.h doc/debug-sections.tmp2 >doc/debug-sections.txt rm doc/debug-sections.tmp doc/debug-sections.tmp2 rm boilerplate_fix.sed + +exit $SeenErrors -- 2.47.2