From e5b20885e3f94a91cd749ad811202e26bdf453a4 Mon Sep 17 00:00:00 2001
From: Jan Frenzel <jan.frenzel@tu-dresden.de>
Date: Thu, 26 Aug 2021 10:08:53 +0200
Subject: [PATCH] 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
---
 .../util/grep-forbidden-words.sh              | 36 +++++++++----------
 1 file changed, 16 insertions(+), 20 deletions(-)

diff --git a/doc.zih.tu-dresden.de/util/grep-forbidden-words.sh b/doc.zih.tu-dresden.de/util/grep-forbidden-words.sh
index ff6f679d5..21e83a5a5 100755
--- a/doc.zih.tu-dresden.de/util/grep-forbidden-words.sh
+++ b/doc.zih.tu-dresden.de/util/grep-forbidden-words.sh
@@ -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
-- 
GitLab