Skip to content
Snippets Groups Projects
Commit 3ccfd73f authored by Martin Schroschk's avatar Martin Schroschk
Browse files

Extend usabilty

* Add functionality to search in all tracked markdown files
* Add usage output
* Counte forbidden patterns found
parent 2c3211be
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.
...@@ -21,16 +21,47 @@ i \<hrskii\> ...@@ -21,16 +21,47 @@ i \<hrskii\>
i hpc \+system i hpc \+system
i hpc[ -]\+da\>" i hpc[ -]\+da\>"
function grepExceptions(){ function grepExceptions () {
if [ $# -gt 0 ]; then if [ $# -gt 0 ]; then
firstPattern=$1 firstPattern=$1
shift shift
grep -v "$firstPattern" | grepExceptions "$@" grep -v "$firstPattern" | grepExceptions "$@"
else else
cat - cat -
fi 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" branch="preview"
if [ -n "$CI_MERGE_REQUEST_TARGET_BRANCH_NAME" ]; then if [ -n "$CI_MERGE_REQUEST_TARGET_BRANCH_NAME" ]; then
branch="origin/$CI_MERGE_REQUEST_TARGET_BRANCH_NAME" branch="origin/$CI_MERGE_REQUEST_TARGET_BRANCH_NAME"
...@@ -38,29 +69,44 @@ fi ...@@ -38,29 +69,44 @@ fi
any_fails=false 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 for f in $files; do
if [ "$f" != doc.zih.tu-dresden.de/README.md -a "${f: -3}" == ".md" ]; then if [ "$f" != doc.zih.tu-dresden.de/README.md -a "${f: -3}" == ".md" ]; then
while IFS=$'\t' read -r flags pattern exceptionPatterns; do echo "Check wording in file $f"
while IFS=$'\t' read -r -a exceptionPatternsArray; do while IFS=$'\t' read -r flags pattern exceptionPatterns; do
echo "Checking wording of $f: $pattern" while IFS=$'\t' read -r -a exceptionPatternsArray; do
case "$flags" in if [ $silent = false ]; then
"i") echo " $pattern"
if grep -n -i "$pattern" "$f" | grepExceptions "${exceptionPatternsArray[@]}" ; then fi
any_fails=true case "$flags" in
fi "i")
;; if grep -n -i "$pattern" "$f" | grepExceptions "${exceptionPatternsArray[@]}" ; then
"s") ((cnt=cnt+1))
if grep -n "$pattern" "$f" | grepExceptions "${exceptionPatternsArray[@]}" ; then any_fails=true
any_fails=true fi
fi ;;
;; "s")
esac if grep -n "$pattern" "$f" | grepExceptions "${exceptionPatternsArray[@]}" ; then
done <<< $exceptionPatterns ((cnt=cnt+1))
done <<< $ruleset any_fails=true
fi fi
;;
esac
done <<< $exceptionPatterns
done <<< $ruleset
fi
done done
echo ""
echo "Found Forbidden Patterns: $cnt"
if [ "$any_fails" == true ]; then if [ "$any_fails" == true ]; then
exit 1 exit 1
fi 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