1 # Copyright (c) 2022 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 grep-based command and fails if it detects any lines
22 # edited or added since HEAD~ and longer than 80 characters.
23 # The grep output stored to new_lines.log (overwriting).
25 # See lines.log to locate where the lines are.
27 # "set -eu" handles failures from the following two lines.
28 BASH_CHECKS_DIR="$(dirname $(readlink -e "${BASH_SOURCE[0]}"))"
29 BASH_FUNCTION_DIR="$(readlink -e "${BASH_CHECKS_DIR}/../../function")"
30 source "${BASH_FUNCTION_DIR}/common.sh" || {
31 echo "Source failed." >&2
35 # Greps do "fail" on zero line output, we need to ignore that in the final grep.
36 piped_command="set -exuo pipefail && git diff -U0 HEAD~ | grep '^\+' | "
37 piped_command+="cut -c2- | grep -v '^\+\+ ' | { grep '.\{81\}' || true; } | "
38 piped_command+="tee 'new_lines.log' | wc -l"
39 lines="$(bash -c "${piped_command}")" || die
40 if [ "${lines}" != "0" ]; then
41 # TODO: Decide which text goes to stdout and which to stderr.
42 warn "Long lines detected: ${lines}"
43 # TODO: Disable when output size does more harm than good.
44 cat "new_lines.log" >&2
46 warn "New line length checker: FAIL"
51 warn "New line length checker: PASS"