Skip to content
Snippets Groups Projects
Commit e5b20885 authored by Jan Frenzel's avatar Jan Frenzel
Browse files

Improved grep-forbidden-words.sh:

* removed two unnecessary variables
* simplified case-sensitivity check
* CI_MERGE_REQUEST_TARGET_BRANCH_NAME doesn't have to be set
parent 3ccfd73f
No related branches found
No related tags found
3 merge requests!322Merge preview into main,!319Merge preview into main,!273Added tab-separated ruleset to grep-forbidden-words.sh to ease adding more rules.
......@@ -5,7 +5,6 @@ set -euo pipefail
scriptpath=${BASH_SOURCE[0]}
basedir=`dirname "$scriptpath"`
basedir=`dirname "$basedir"`
forbiddenpatternsfile=$(realpath $basedir/forbidden.patterns)
#This is the ruleset. Each line represents a rule of tab-separated fields.
#The first field represents whether the match should be case-sensitive (s) or insensitive (i).
......@@ -62,12 +61,7 @@ while getopts ":ahs" option; do
esac
done
branch="preview"
if [ -n "$CI_MERGE_REQUEST_TARGET_BRANCH_NAME" ]; then
branch="origin/$CI_MERGE_REQUEST_TARGET_BRANCH_NAME"
fi
any_fails=false
branch="origin/${CI_MERGE_REQUEST_TARGET_BRANCH_NAME:-preview}"
if [ $all_files = true ]; then
echo "Search in all markdown files."
......@@ -84,29 +78,31 @@ for f in $files; do
while IFS=$'\t' read -r flags pattern exceptionPatterns; do
while IFS=$'\t' read -r -a exceptionPatternsArray; do
if [ $silent = false ]; then
echo " $pattern"
echo " Pattern: $pattern"
fi
grepflag=
case "$flags" in
"i")
if grep -n -i "$pattern" "$f" | grepExceptions "${exceptionPatternsArray[@]}" ; then
((cnt=cnt+1))
any_fails=true
fi
;;
"s")
if grep -n "$pattern" "$f" | grepExceptions "${exceptionPatternsArray[@]}" ; then
((cnt=cnt+1))
any_fails=true
fi
grepflag=-i
;;
esac
if grep -n $grepflag "$pattern" "$f" | grepExceptions "${exceptionPatternsArray[@]}" ; then
((cnt=cnt+1))
fi
done <<< $exceptionPatterns
done <<< $ruleset
fi
done
echo ""
echo "Found Forbidden Patterns: $cnt"
if [ "$any_fails" == true ]; then
case $cnt in
1)
echo "Forbidden Patterns: 1 match found"
;;
*)
echo "Forbidden Patterns: $cnt matches found"
;;
esac
if [ $cnt -gt 0 ]; then
exit 1
fi
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment