File indexing completed on 2024-12-01 04:38:16

0001 #!/bin/sh
0002 
0003 export LC_ALL=en_US.utf8
0004 export LANG=C
0005 MY_NAME="$(basename "$0")"
0006 
0007 SET_VERBOSE=0
0008 ALL_FILES=0
0009 HEAD_COMMIT=0
0010 
0011 function parsearguments {
0012         local ARGS=$(getopt -o "h" -l "verbose,all,head,help" -n "${MY_NAME}" -- "$@")
0013         local PRINT_HELP=0
0014 
0015         #Bad arguments
0016         if [ $? -ne 0 ] ; then
0017                 exit 1
0018         fi
0019 
0020         # A little magic
0021         eval set -- "$ARGS"
0022 
0023         while true ; do
0024                 case "$1" in
0025                 -h|--help)
0026                         PRINT_HELP=1
0027                         shift
0028                 ;;
0029                 --verbose )
0030                         SET_VERBOSE=1
0031                         shift
0032                 ;;
0033                 --all )
0034                         ALL_FILES=1
0035                         HEAD_COMMIT=0
0036                         shift
0037                 ;;
0038                 --head )
0039                         HEAD_COMMIT=1
0040                         ALL_FILES=0
0041                         shift
0042                 ;;
0043                 --)
0044                         shift
0045                         break
0046                 esac
0047         done
0048 
0049         if [[ ${PRINT_HELP} -ne 0 ]] ; then
0050                 echo "Usage: ${MY_NAME} [--all|--head] [--verbose]" >&2
0051                 echo "Format C++ source code and headers in a git-controlled directory structure." >&2
0052                 echo >&2
0053                 echo "Options:" >&2
0054                 echo " --help" >&2
0055                 echo "    Show this help" >&2
0056                 echo " --verbose" >&2
0057                 echo "    Show verbose (more) output" >&2
0058                 echo " --head" >&2
0059                 echo "    Format .cpp/.h files changed in current HEAD commit">&2
0060                 echo " --all" >&2
0061                 echo "    Format all .cpp/.h files, not just those marked by git as added or modified">&2
0062                 exit 1
0063         fi
0064 }
0065 
0066 parsearguments "$@"
0067 
0068 if [[ ${HEAD_COMMIT} -eq 1 ]] ; then
0069         git diff-tree --no-commit-id --name-only -r HEAD | grep -E '[.](cpp|h)$'
0070 elif [[ ${ALL_FILES} -eq 0 ]] ; then
0071         # Format only files seen by git as added or modified,
0072         # filter for .cpp or .h files.
0073         # Trying to handle filenames with spaces correctly (not tested)
0074         git status --untracked-files=no --porcelain | awk '/^\s*[AM]+\s+.*[.](cpp|h)$/ {for(i=2;i<NF;++i){printf $i" "};printf $NF"\n"}'
0075 else
0076         # Format all .cpp or .h files; using "find" to located those files
0077         find . -type f -name '*.cpp' -o -name '*.h'
0078 fi | \
0079 # Sort files alphabetically, omit duplicates
0080 sort -u | \
0081 grep --color=NEVER -vE 'src/config/preferences.(cpp|h)$' | \
0082 while read filename ; do
0083         if [[ ! -s "${filename}" ]] ; then
0084                 echo "${MY_NAME}: File not found: \"${filename}\"" >&2
0085                 continue
0086         fi
0087 
0088         TEMPFILE=$(mktemp -t 'kbibtex_formatted_source_XXXXXXXXXXX.cpp')
0089 
0090         echo "Processing \"${filename}\""
0091         astyle -n --align-reference=name --align-pointer=name --indent=spaces=4 --indent-labels --pad-oper --unpad-paren --pad-header --keep-one-line-statements --convert-tabs <"${filename}" >${TEMPFILE}
0092 
0093         # Astyle has problem with 'foreach' statements
0094         sed -i -e 's/\bforeach(/foreach (/g' ${TEMPFILE}
0095 
0096         # normalize SIGNAL statements as recommended at
0097         # http://marcmutz.wordpress.com/effective-qt/prefer-to-use-normalised-signalslot-signatures/
0098         # astyle would insert spaces etc
0099         grep -Po '(SIGNAL|SLOT)\([^(]+\([^)]*\)\)' "${TEMPFILE}" | while read original ; do
0100                 normalized="$(sed -e 's/const //g;s/&//g;s/ //g;s/\//\\\//g' <<<${original})"
0101                 originalregexpized="$(sed -e 's/\*/\\*/g;s/\//\\\//g' <<<${original})"
0102                 sed -i -e 's/'"${originalregexpized}"'/'"${normalized}"'/g' ${TEMPFILE}
0103         done
0104 
0105         # correct some astyle mis-formattings
0106         if [[ "${filename}" != "${filename/%.h/}" ]] ; then
0107                 sed -i -e 's/^\([ ]*\)\(:[ ][a-zA-Z]\)/\1    \2/g' ${TEMPFILE}
0108         elif [[ "${filename}" != "${filename/%.cpp/}" ]] ; then
0109                 sed -i -e  's/^\([ ]*\)\(:[ ][a-zA-Z]\)/\1    \2/g;s/\(foreach([^)]*\) \([&*]\) /\1 \2/g' ${TEMPFILE}
0110         fi
0111 
0112         # In const_cast operations, ensure space before ampersand
0113         sed -i -r 's!( : const_cast<[^(;]+[^ ])&>[(]!\1 \&>(!g' ${TEMPFILE}
0114 
0115         # Remove superfluous empty lines at file's end
0116         #  http://unix.stackexchange.com/a/81687
0117         #  http://sed.sourceforge.net/sed1line.txt
0118         sed -i -e :a -e '/^\n*$/{$d;N;};/\n$/ba' ${TEMPFILE}
0119 
0120         # Check if copyright year is up to date
0121         head -n 5 ${TEMPFILE} | grep -Eo --color=NEVER '20[0-9][0-9] Thomas' | head -n 1 | while read year rest ; do
0122                 [ "${year}" = $(date '+%Y') ] || echo "File '${filename}' has an outdated copyright year: ${year}"
0123         done
0124 
0125         # only change/touch original file if it has been changed
0126         diff -q ${TEMPFILE} "${filename}" >/dev/null || { echo "Updating \"${filename}\"" ; cp -p ${TEMPFILE} "${filename}" || echo "Cannot copy \"${TEMPFILE}\" \"${filename}\"" ; }
0127 
0128         rm -f ${TEMPFILE}
0129 done
0130 
0131 year=$(date '+%Y')
0132 test -s README && { grep -q "2004-${year} Thomas Fischer" README || echo "README should have current year in copyright: ${year}" ; }
0133 for copyrightfile in src/program/program.cpp src/test/main.cpp src/parts/partfactory.cpp ; do
0134     test -s "${copyrightfile}" && { grep -q "SPDX-FileCopyrightText: 2004-${year} Thomas Fischer" "${copyrightfile}" || echo "'${copyrightfile}' should have current year in copyright: ${year}" ; }
0135 done