diff --git a/doc.zih.tu-dresden.de/util/check-bash-syntax.sh b/doc.zih.tu-dresden.de/util/check-bash-syntax.sh new file mode 100755 index 0000000000000000000000000000000000000000..e5681413d14771e8fb144a2684161cf5e7c1edae --- /dev/null +++ b/doc.zih.tu-dresden.de/util/check-bash-syntax.sh @@ -0,0 +1,79 @@ +#!/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 diff --git a/doc.zih.tu-dresden.de/util/pre-commit b/doc.zih.tu-dresden.de/util/pre-commit index 043320f352b923a7e7be96c04de5914960285b65..b86b75d9a07870a68118aa500ee80781e216c56b 100755 --- a/doc.zih.tu-dresden.de/util/pre-commit +++ b/doc.zih.tu-dresden.de/util/pre-commit @@ -1,7 +1,4 @@ #!/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 ]