job_specs: add wireguard perf tests to regression
[csit.git] / resources / libraries / bash / entry / check / copyright_year.sh
1 # Copyright (c) 2021 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:
5 #
6 #     http://www.apache.org/licenses/LICENSE-2.0
7 #
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.
13
14 set -exuo pipefail
15
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.
20
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).
26 #
27 # 3 lines were chosen, because first two lines could be shebang and empty line,
28 # and more than 3 lines would start failing on files with multiple copyright
29 # holders. There, only the last updating entity needs to bump its year,
30 # and put other copyright lines below.
31
32 # "set -eu" handles failures from the following two lines.
33 BASH_CHECKS_DIR="$(dirname $(readlink -e "${BASH_SOURCE[0]}"))"
34 BASH_FUNCTION_DIR="$(readlink -e "${BASH_CHECKS_DIR}/../../function")"
35 source "${BASH_FUNCTION_DIR}/common.sh" || {
36     echo "Source failed." >&2
37     exit 1
38 }
39
40 year=$(date +'%Y')
41 IFS=$'\n'
42 files=($(git diff --name-only HEAD~ || true))
43 unset IFS
44 truncate -s 0 "copyright_year.log" || die
45 # A change can have thousands of files, supress console output in the cycle.
46 set +x
47 for fil in "${files[@]}"; do
48     # Greps do "fail" on 0 line output, we need to ignore that
49     # as 0 lines is good. We need both set +e to ensure everything executes,
50     # and || true later to avoid dying on zero.
51     piped_command="set +ex; head -n 3 '${fil}' | fgrep -i 'Copyright'"
52     piped_command+=" | fgrep -v '${year}' | awk '{print \"${fil}: \" \$0}'"
53     piped_command+=" >> 'copyright_year.log'"
54     wrong_strings="$(bash -c "${piped_command}" || true)" || die
55 done
56 set -x
57 lines="$(< "copyright_year.log" wc -l)"
58 if [ "${lines}" != "0" ]; then
59     # TODO: Decide which text goes to stdout and which to stderr.
60     warn "Copyright lines with wrong year detected: ${lines}"
61     # TODO: Disable when output size does more harm than good.
62     pwd
63     cat "copyright_year.log" >&2
64     warn
65     warn "Copyright year checker: FAIL"
66     exit 1
67 fi
68
69 warn
70 warn "Copyright year checker: PASS"