]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
Maintenance: source maintenance script improvements (#1220)
authorgkinkie@gmail.com <kinkie@squid-cache.org>
Fri, 30 Dec 2022 18:07:12 +0000 (18:07 +0000)
committerSquid Anubis <squid-anubis@squid-cache.org>
Tue, 3 Jan 2023 09:31:45 +0000 (09:31 +0000)
- Allow passing astyle program name via ASTYLE environment variable.
- By default, use "astyle-3.1" if present or "astyle" otherwise.
- Add an option to format changed source files only.

No changes to the default behavior except for environments where a
program named "astyle-3.1" is present but a program named "astyle" was
previously used.

scripts/source-maintenance.sh

index 20ebba80d4010b5e8627b2ce0994799b63a7d674..c491524c98a1b551a36f050f2f25d93110f03873 100755 (executable)
@@ -17,6 +17,7 @@
 KeepGoing="no"
 # the actual name of the directive that enabled keep-going mode
 KeepGoingDirective=""
+
 #
 # The script checks that the version of astyle is TargetAstyleVersion.
 # if it isn't, the default behaviour is to not perform the formatting stage
@@ -36,13 +37,70 @@ CheckAndUpdateCopyright=yes
 # * SHA1/etc: Check commits added after the specified git commit.
 UpdateContributorsSince=auto
 
+# --only-changed-since point
+OnlyChangedSince=""
+
 printUsage () {
-    echo "Usage: $0 [option...]"
-    echo "options:"
-    echo "    --keep-going|-k"
-    echo "    --check-and-update-copyright <yes|no>"
-    echo "    --update-contributors-since <never|auto|revision>"
-    echo "    --with-astyle </path/to/astyle/executable>"
+cat <<USAGE_
+Usage: $0 [option...]
+options:
+--check-and-update-copyright <yes|no>      (default: yes)
+--help|-h
+--keep-going|-k                            (default: stop on error)
+--only-changed-since <fork|commit>         (default: apply to all files)
+--update-contributors-since <never|auto|revision> (default: auto)
+--with-astyle </path/to/astyle/executable> (default: astyle-${TargetAstyleVersion} or astyle)
+
+USAGE_
+}
+
+printHelp () {
+
+cat <<HELP_INTRO_
+This script applies Squid mandatory code style guidelines and generates
+various files derived from Squid sources.
+HELP_INTRO_
+
+printUsage
+
+cat <<HELP_MAIN_
+--help, -h
+
+Print this information and exit.
+
+--only-changed-since <"fork"|commit>
+
+When specifieid, the script only examines for formatting changes those
+files that have changed since the specified git reference point. The
+argument is either a git commit (fed to "git diff") or a special keyword
+"fork". Common commit values include HEAD^, master, origin/master, and the
+branch the current one was forked off. When "fork" is specified, the
+script will look for files changed since the current branch was forked off
+upstream/master (according to "git merge-base --fork-point").
+
+This option does not disable some repository-wide file generation and
+repository-wide non-formatting checks/adjustments.
+
+--update-contributors-since <never|auto|revision>
+
+Configures how to sync CONTRIBUTORS with the current git branch commits:
+* never: Do not update CONTRIBUTORS at all.
+* auto: Check commits added since the last similar update.
+* SHA1/etc: Check commits added after the specified git commit.
+
+--with-astyle </path/to/astyle/executable>
+
+Squid code style guidelines require astyle version $TargetAstyleVersion.
+The path to the astyle binary can be specified using this command line
+option or by exporting the ASTYLE environment variable. If both are
+specified, the command-line option wins.
+
+External dependencies:
+
+* Astyle. See the --with-astyle command line option above.
+* gperf (if you modify certain source files)
+
+HELP_MAIN_
 }
 
 # command-line options
@@ -58,7 +116,7 @@ while [ $# -ge 1 ]; do
         then
             printUsage
             echo "Error: Option $1 expects a yes or no argument but got $2"
-            exit 1;
+            exit 1
         fi
         CheckAndUpdateCopyright=$2
         shift 2
@@ -68,23 +126,27 @@ while [ $# -ge 1 ]; do
         then
             printUsage
             echo "Error: Option $1 expects an argument."
-            exit 1;
+            exit 1
         fi
         UpdateContributorsSince="$2"
-        shift 2;
+        shift 2
         ;;
     --help|-h)
-        printUsage
-        exit 0;
+        printHelp
+        exit 0
         ;;
     --with-astyle)
         ASTYLE=$2
         shift 2
         ;;
+    --only-changed-since)
+        OnlyChangedSince="$2"
+        shift 2
+        ;;
     *)
         printUsage
         echo "Unsupported command-line option: $1"
-        exit 1;
+        exit 1
         ;;
     esac
 done
@@ -92,23 +154,52 @@ 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
 
+# usage: <well-known program name> <program argument(s)> <candidate name>...
+# Finds the first working program among the given candidate program names.
+# The found program name is returned via the $FoundProgram global:
+FoundProgram=""
+findProgram ()
+{
+    wellKnown="$1"
+    shift
+    options="$1"
+    shift
+
+    for candidate in $*
+    do
+        if "$candidate" $options < /dev/null > /dev/null 2> /dev/null
+        then
+            echo "Found ${wellKnown}-like program: $candidate"
+            FoundProgram="$candidate"
+            return 0
+        fi
+    done
+
+    echo "ERROR: Failed to find a ${wellKnown}-like program; tried: $*"
+    FoundProgram=""
+    return 1
+}
+
 made="generated" # a hack: prevents $GeneratedByMe searches matching this file
 GeneratedByMe="This file is $made by scripts/source-maintenance.sh."
 
-${ASTYLE} --version >/dev/null 2>/dev/null
-result=$?
-if test $result -gt 0 ; then
-       echo "ERROR: cannot run ${ASTYLE}"
-       exit 1
+if [ "x$ASTYLE" != "x" ] ; then
+    if ! "${ASTYLE}" --version > /dev/null 2> /dev/null ; then
+        echo "ERROR: Cannot run user-supplied astyle: ${ASTYLE}"
+        exit 1
+    fi
+else
+    findProgram astyle --version astyle-${TargetAstyleVersion} astyle || exit $?
+    ASTYLE=$FoundProgram
 fi
-ASVER=`${ASTYLE} --version 2>&1 | grep -o -E "[0-9.]+"`
+
+ASVER=`"${ASTYLE}" --version 2>&1 | grep -o -E "[0-9.]+"`
 if test "${ASVER}" != "${TargetAstyleVersion}" ; then
        if test "${ASTYLE}" = "astyle" ; then
                echo "Astyle version problem. You have ${ASVER} instead of ${TargetAstyleVersion}"
@@ -119,13 +210,23 @@ if test "${ASVER}" != "${TargetAstyleVersion}" ; then
                echo "Formatting anyway, please double check output before submitting"
        fi
 else
-       echo "Found astyle ${ASVER}. Formatting..."
+       echo "Detected expected astyle version: ${ASVER}"
 fi
 CppFormatter=''
 if test "${ASVER}"; then
     CppFormatter="./scripts/format-cpp.pl --with-astyle ${ASTYLE}"
 fi
 
+if test "x$OnlyChangedSince" = "xfork" ; then
+    ForkPoint=`git merge-base --fork-point upstream/master`
+    if test "x$ForkPoint" = "x" ; then
+        echo "Could not identify fork point - sometimes it happens"
+        echo "Please specify commit-id explicitly"
+        exit 1
+    fi
+    OnlyChangedSince="$ForkPoint"
+fi
+
 if test $CheckAndUpdateCopyright = yes
 then
     COPYRIGHT_YEARS=`date +"1996-%Y"`
@@ -136,7 +237,7 @@ fi
 # in KeepGoing mode, remembers errors and hides them from callers
 run_ ()
 {
-        "$@" && return; # return on success
+        "$@" && return 0 # return on success
         error=$?
 
         if test $KeepGoing = no; then
@@ -215,6 +316,12 @@ collectDebugMessagesFrom ()
     source="$1"
     destination="doc/debug-messages.tmp"
 
+    if test "x$OnlyChangedSince" != "x"; then
+        # Skipping collection due to --only-changed-since.
+        # processDebugMessages() will warn.
+        return 0
+    fi
+
     # Merge multi-line debugs() into one-liners and remove '//...' comments.
     awk 'BEGIN { found=0; dbgLine=""; } {
         if ($0 ~ /[ \t]debugs[ \t]*\(/)
@@ -260,9 +367,14 @@ processDebugMessages ()
     source="doc/debug-messages.tmp"
     destination="doc/debug-messages.dox"
 
+    if test "x$OnlyChangedSince" != "x"; then
+        echo "WARNING: Skipping update of $destination due to --only-changed-since"
+        return 0
+    fi
+
     if test '!' -s "$source"; then
         echo "ERROR: Failed to find debugs() message IDs"
-        return 1;
+        return 1
     fi
 
     repeatedIds=`awk '{print $1}' $source | sort -n | uniq -d`
@@ -270,7 +382,7 @@ processDebugMessages ()
         echo "ERROR: Repeated debugs() message IDs:"
         echo "$repeatedIds"
         echo ""
-        return 1;
+        return 1
     fi
 
     repeatedGists=`awk '{$1=""; print substr($0,2)}' $source | sort | uniq -d`
@@ -278,7 +390,7 @@ processDebugMessages ()
         echo "ERROR: Repeated debugs() message gists:"
         echo "$repeatedGists"
         echo ""
-        return 1;
+        return 1
     fi
 
     cat scripts/boilerplate.h > $destination
@@ -299,7 +411,13 @@ processDebugSections ()
 {
     destination="doc/debug-sections.txt"
 
+    if test "x$OnlyChangedSince" != "x"; then
+        echo "WARNING: Skipping update of $destination due to --only-changed-since"
+        return 0
+    fi
+
     LC_ALL=C sort -u < doc/debug-sections.tmp > doc/debug-sections.tmp2
+
     cat scripts/boilerplate.h > $destination
     echo "" >> $destination
     cat doc/debug-sections.tmp2 >> $destination
@@ -324,7 +442,33 @@ git grep "ifn?def .*_SQUID_" |
 #
 # Scan for file-specific actions
 #
-for FILENAME in `git ls-files`; do
+
+    # The two git commands below will also list any files modified during the
+    # current run (e.g., src/http/RegisteredHeadersHash.cci or icons/icon.am).
+    FilesToOperateOn=""
+    if test "x$OnlyChangedSince" != "x" ; then
+        FilesToOperateOn=`git diff --name-only $OnlyChangedSince`
+        gitResult=$?
+        if test $gitResult -ne 0 ; then
+            echo "ERROR: Cannot use --only-changed-since reference point: $OnlyChangedSince"
+            echo "Consider using a git commit SHA (from git log) instead"
+            return $gitResult
+        fi
+    else
+        FilesToOperateOn=`git ls-files`
+        gitResult=$?
+        # a bit paranoid but protects the empty $FilesToOperateOn check below
+        if test $gitResult -ne 0 ; then
+            echo "ERROR: Cannot find source code file names"
+            return $gitResult
+        fi
+    fi
+    if test "x$FilesToOperateOn" = "x"; then
+        echo "WARNING: No files to scan and format"
+        return 0
+    fi
+
+for FILENAME in $FilesToOperateOn; do
     skip_copyright_check=""
 
     # skip subdirectories, git ls-files is recursive
@@ -529,7 +673,7 @@ generateGperfFile ()
     cciFile=`echo $gperfFile | sed 's/[.]gperf$/.cci/'`
 
     if test $gperfFile -ot $cciFile; then
-        return 0;
+        return 0
     fi
 
     generateRawGperfFile $gperfFile > $cciFile.unformatted || return
@@ -568,7 +712,7 @@ collectAuthors ()
 {
     if test "x$UpdateContributorsSince" = xnever
     then
-        return 0; # successfully did nothing, as requested
+        return 0 # successfully did nothing, as requested
     fi
 
     vettedCommitPhraseRegex='[Rr]eference point for automated CONTRIBUTORS updates'
@@ -583,7 +727,7 @@ collectAuthors ()
         if test "x$humanSha" = x && test "x$botSha" = x
         then
             echo "ERROR: Unable to determine the commit to start contributors extraction from"
-            return 1;
+            return 1
         fi
 
         # find the latest commit among the above one or two commits
@@ -630,6 +774,6 @@ run_ collectAuthors || exit 1
 # Run formatting
 srcFormat || exit 1
 
-rm -f boilerplate_fix.sed
+test -e boilerplate_fix.sed && rm -f boilerplate_fix.sed
 
 exit $SeenErrors