1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
#!/bin/bash
# This scripts updates all the "Copyright" lines in source files with the
# correct most recent modification years.
#
# It must be given a file in argument. A typical usage can be to run it on all
# code source files:
#
# find \( -name '*.m' -or -name '*.cc' -or -name '*.hh' -or -name '*.c' -or -name '*.h' -or -name '*.f08' -or -name '*.F08' \) -exec scripts/fix-copyright-years '{}' ';'
#
# The LAST_UPDATE_COMMIT variable must be updated to contain the number of the
# last commit in which such a copyright update has been done.
#
# Note that the script does not understand some unusual copyright notices: it
# will display a message in that case.
LAST_UPDATE_COMMIT=a3afba706bf2a6e3ba8f96874cc47ded7e39db9d # dynare.git
#LAST_UPDATE_COMMIT=12c91b786ce59e2f1fc7fefcbf0a94a062ea0c5d # dseries.git
#LAST_UPDATE_COMMIT=a1728ec6bfc760ab7f0d48f5897ec27baebf211b # preprocessor.git
if [[ -z $1 ]]; then
echo "Give a filename in argument"
exit 1
fi
if [[ ! -f $1 ]]; then
echo "$1: Unknown file"
exit 1
fi
GITYEAR=$(git log $LAST_UPDATE_COMMIT.. --date=short --pretty=format:%cd -n 1 "$1")
GITYEAR=${GITYEAR%%-*}
if [[ -z $GITYEAR ]]; then
# Not in git
exit 0
fi
COPYLINES=$(grep -c Copyright "$1")
if ((COPYLINES == 0)); then
echo "$1: File has no copyright line"
exit 1
fi
if ((COPYLINES == 2)); then
COPYLINES=$(grep Copyright "$1")
echo "$1: File has too many copyright lines (Git year: $GITYEAR): $COPYLINES"
exit 1
fi
COPYLINE=$(grep -o -E "Copyright © ([[:digit:]]{4}-)?[[:digit:]]{4} Dynare Team" "$1")
if [[ -z $COPYLINE ]]; then
COPYLINE=$(grep Copyright "$1")
echo "$1: Unsupported copyright notice (Git year: $GITYEAR): $COPYLINE"
exit 1
fi
[[ $COPYLINE =~ Copyright\ ©\ ([0-9]{4}-)?([0-9]{4})\ Dynare\ Team ]]
ACTUALYEAR=${BASH_REMATCH[2]}
if ((GITYEAR > ACTUALYEAR)); then
sed -E -i "s/Copyright © ([0-9]{4})(-[0-9]{4})? Dynare Team/Copyright © \1-$GITYEAR Dynare Team/" "$1"
echo "$1: Updating to $GITYEAR"
fi
|