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

Merge branch 'bash-syntax-checker' into 'preview'

Simple bash syntax checker

See merge request !364
parents 2327c36a 408a9dbc
No related branches found
No related tags found
3 merge requests!392Merge preview into contrib guide for browser users,!366Merge preview into main,!364Simple bash syntax checker
#!/bin/bash
set -euo pipefail
scriptpath=${BASH_SOURCE[0]}
basedir=`dirname "$scriptpath"`
basedir=`dirname "$basedir"`
function usage () {
echo "$0 [options]"
echo "Search for bash files that have an invalid syntax."
echo ""
echo "Options:"
echo " -a Search in all bash files (default: git-changed files)"
echo " -f=FILE Search in a specific bash file"
echo " -s Silent mode"
echo " -h Show help message"
}
# Options
all_files=false
silent=false
file=""
while getopts ":ahsf:" option; do
case $option in
a)
all_files=true
;;
f)
file=$2
shift
;;
s)
silent=true
;;
h)
usage
exit;;
\?) # Invalid option
echo "Error: Invalid option."
usage
exit;;
esac
done
branch="origin/${CI_MERGE_REQUEST_TARGET_BRANCH_NAME:-preview}"
if [ $all_files = true ]; then
echo "Search in all bash files."
files=$(git ls-tree --full-tree -r --name-only HEAD $basedir/docs/ | grep .sh)
elif [[ ! -z $file ]]; then
files=$file
else
echo "Search in git-changed files."
files=`git diff --name-only "$(git merge-base HEAD "$branch")" | grep .sh`
fi
cnt=0
for f in $files; do
if ! bash -n $f; then
if [ $silent = false ]; then
echo "Bash file $f has invalid syntax"
fi
((cnt=cnt+1))
fi
done
case $cnt in
1)
echo "Bash files with invalid syntax: 1 match found"
;;
*)
echo "Bash files with invalid syntax: $cnt matches found"
;;
esac
if [ $cnt -gt 0 ]; then
exit 1
fi
#!/bin/bash
exit_ok=yes
files=$(git diff-index --cached --name-only HEAD)
function testPath(){
path_to_test=doc.zih.tu-dresden.de/docs/$1
test -f "$path_to_test" || echo $path_to_test does not exist
......@@ -15,29 +12,39 @@ fi
export -f testPath
for file in $files
do
if [ $file == doc.zih.tu-dresden.de/mkdocs.yml ]
exit_ok=yes
branch="origin/${CI_MERGE_REQUEST_TARGET_BRANCH_NAME:-preview}"
if [ -f "$GIT_DIR/MERGE_HEAD" ]
then
source_hash=`git merge-base HEAD "$branch"`
else
source_hash=`git rev-parse HEAD`
fi
#Remove everything except lines beginning with --- or +++
files=`git diff $source_hash | sed -E -n 's#^(---|\+\+\+) ((/|./)[^[:space:]]+)$#\2#p'`
#Assume that we have pairs of lines (starting with --- and +++).
while read oldfile; do
read newfile
if [ "$newfile" == doc.zih.tu-dresden.de/mkdocs.yml ]
then
echo Testing $file
echo Testing "$newfile"
sed -n '/^ *- /s#.*: \([A-Za-z_/]*.md\).*#\1#p' doc.zih.tu-dresden.de/mkdocs.yml | xargs -L1 -I {} bash -c "testPath '{}'"
if [ $? -ne 0 ]
then
exit_ok=no
fi
elif [[ $file =~ ^doc.zih.tu-dresden.de/(.*.md)$ ]]
elif [[ "$newfile" =~ ^b/doc.zih.tu-dresden.de/(.*.md)$ ]]
then
filepattern=${BASH_REMATCH[1]}
#lint
echo "Checking linter..."
echo "Linting..."
docker run --name=hpc-compendium --rm -w /docs --mount src="$(pwd)"/doc.zih.tu-dresden.de,target=/docs,type=bind hpc-compendium markdownlint $filepattern
if [ $? -ne 0 ]
then
exit_ok=no
fi
#link-check
echo "Checking links..."
docker run --name=hpc-compendium --rm -w /docs --mount src="$(pwd)"/doc.zih.tu-dresden.de,target=/docs,type=bind hpc-compendium markdown-link-check $filepattern
if [ $? -ne 0 ]
......@@ -45,9 +52,15 @@ do
exit_ok=no
fi
fi
done
done <<< "$files"
echo "Testing syntax of bash files..."
docker run --name=hpc-compendium --rm -w /docs --mount src="$(pwd)",target=/docs,type=bind hpc-compendium ./doc.zih.tu-dresden.de/util/check-bash-syntax.sh
if [ $? -ne 0 ]
then
exit_ok=no
fi
#spell-check
echo "Spell-checking..."
docker run --name=hpc-compendium --rm -w /docs --mount src="$(pwd)",target=/docs,type=bind hpc-compendium ./doc.zih.tu-dresden.de/util/check-spelling.sh
if [ $? -ne 0 ]
......@@ -55,7 +68,6 @@ then
exit_ok=no
fi
#forbidden words checking
echo "Forbidden words checking..."
docker run --name=hpc-compendium --rm -w /docs --mount src="$(pwd)",target=/docs,type=bind hpc-compendium ./doc.zih.tu-dresden.de/util/grep-forbidden-words.sh
if [ $? -ne 0 ]
......
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