1 # Copyright (c) 2024 Cisco and/or its affiliates.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
6 # http://www.apache.org/licenses/LICENSE-2.0
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
16 # This file should be executed from tox, as the assumed working directory
17 # is different from where this file is located.
18 # This file does not have executable flag nor shebang,
19 # to dissuade non-tox callers.
21 # This script runs a few grep-based commands and fails
22 # if it detects any file edited or added since HEAD~
23 # containing a copyright notice in first 3 lines,
24 # but not the current year (in the same line).
25 # The offending lines are stored to copyright_year.log (overwriting).
27 # 3 lines were chosen, because first two lines could be shebang and empty line.
29 # "set -eu" handles failures from the following two lines.
30 BASH_CHECKS_DIR="$(dirname $(readlink -e "${BASH_SOURCE[0]}"))"
31 BASH_FUNCTION_DIR="$(readlink -e "${BASH_CHECKS_DIR}/../../function")"
32 source "${BASH_FUNCTION_DIR}/common.sh" || {
33 echo "Source failed." >&2
39 files=($(git diff --name-only HEAD~ || true))
41 truncate -s 0 "copyright_year.log" || die
42 # A change can have thousands of files, supress console output for the cycle.
44 for fil in "${files[@]}"; do
45 # Greps do "fail" on 0 line output, we need to ignore that
46 # as 0 lines is good. We need both set +e to ensure everything executes,
47 # and || true later to avoid dying on zero.
48 piped_command="set +ex; head -n 3 '${fil}' | fgrep -i 'Copyright'"
49 piped_command+=" | fgrep -v '${year}' | awk '{print \"${fil}: \" \$0}'"
50 piped_command+=" >> 'copyright_year.log'"
51 wrong_strings="$(bash -c "${piped_command}" || true)" || die
54 lines="$(< "copyright_year.log" wc -l)"
55 if [ "${lines}" != "0" ]; then
56 # TODO: Decide which text goes to stdout and which to stderr.
57 warn "Copyright lines with wrong year detected: ${lines}"
58 # TODO: Disable when output size does more harm than good.
60 cat "copyright_year.log" >&2
62 warn "Copyright year checker: FAIL"
67 warn "Copyright year checker: PASS"