From 3ccfd73fa05c6d46e4fd8d18c96e002885731df9 Mon Sep 17 00:00:00 2001
From: Martin Schroschk <martin.schroschk@tu-dresden.de>
Date: Thu, 26 Aug 2021 09:21:18 +0200
Subject: [PATCH] Extend usabilty

* Add functionality to search in all tracked markdown files
* Add usage output
* Counte forbidden patterns found
---
 .../util/grep-forbidden-words.sh              | 104 +++++++++++++-----
 1 file changed, 75 insertions(+), 29 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 3b121662f..ff6f679d5 100755
--- a/doc.zih.tu-dresden.de/util/grep-forbidden-words.sh
+++ b/doc.zih.tu-dresden.de/util/grep-forbidden-words.sh
@@ -21,16 +21,47 @@ i	\<hrskii\>
 i	hpc \+system
 i	hpc[ -]\+da\>"
 
-function grepExceptions(){
-if [ $# -gt 0 ]; then
-firstPattern=$1
-shift
-grep -v "$firstPattern" | grepExceptions "$@"
-else
-cat -
-fi
+function grepExceptions () {
+  if [ $# -gt 0 ]; then
+    firstPattern=$1
+    shift
+    grep -v "$firstPattern" | grepExceptions "$@"
+  else
+    cat -
+  fi
 }
 
+function usage () {
+  echo "$0 [options]"
+  echo "Search forbidden patterns in markdown files."
+  echo ""
+  echo "Options:"
+  echo "  -a     Search in all markdown files (default: git-changed files)" 
+  echo "  -s     Silent mode"
+  echo "  -h     Show help message"
+}
+
+# Options
+all_files=false
+silent=false
+while getopts ":ahs" option; do
+ case $option in
+   a)
+     all_files=true
+     ;;
+   s)
+     silent=true
+     ;;
+   h)
+     usage
+     exit;;
+   \?) # Invalid option
+     echo "Error: Invalid option."
+     usage
+     exit;;
+ esac
+done
+
 branch="preview"
 if [ -n "$CI_MERGE_REQUEST_TARGET_BRANCH_NAME" ]; then
     branch="origin/$CI_MERGE_REQUEST_TARGET_BRANCH_NAME"
@@ -38,29 +69,44 @@ fi
 
 any_fails=false
 
-files=`git diff --name-only "$(git merge-base HEAD "$branch")"`
+if [ $all_files = true ]; then
+  echo "Search in all markdown files."
+  files=$(git ls-tree --full-tree -r --name-only HEAD $basedir/docs/ | grep .md)
+else
+  echo "Search in git-changed files."
+  files=`git diff --name-only "$(git merge-base HEAD "$branch")"`
+fi
+
+cnt=0
 for f in $files; do
-    if [ "$f" != doc.zih.tu-dresden.de/README.md -a "${f: -3}" == ".md" ]; then
-        while IFS=$'\t' read -r flags pattern exceptionPatterns; do
-            while IFS=$'\t' read -r -a exceptionPatternsArray; do
-                echo "Checking wording of $f: $pattern"
-                case "$flags" in
-                    "i")
-                        if grep -n -i "$pattern" "$f" | grepExceptions "${exceptionPatternsArray[@]}" ; then
-                            any_fails=true
-                        fi
-                    ;;
-                    "s")
-                        if grep -n "$pattern" "$f" | grepExceptions "${exceptionPatternsArray[@]}" ; then
-                            any_fails=true
-                        fi
-                    ;;
-                esac
-            done <<< $exceptionPatterns
-        done <<< $ruleset
-    fi
+  if [ "$f" != doc.zih.tu-dresden.de/README.md -a "${f: -3}" == ".md" ]; then
+    echo "Check wording in file $f"
+    while IFS=$'\t' read -r flags pattern exceptionPatterns; do
+      while IFS=$'\t' read -r -a exceptionPatternsArray; do
+        if [ $silent = false ]; then
+          echo "  $pattern"
+        fi
+        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
+          ;;
+        esac
+      done <<< $exceptionPatterns
+    done <<< $ruleset
+  fi
 done
 
+echo "" 
+echo "Found Forbidden Patterns: $cnt"
 if [ "$any_fails" == true ]; then
-    exit 1
+  exit 1
 fi
-- 
GitLab