diff --git a/doc.zih.tu-dresden.de/util/check-links.sh b/doc.zih.tu-dresden.de/util/check-links.sh index 49ee7f2dbe876ed8e82c0706631db3479c51fb16..1867667ecc456221c8ccf4cc1903dfee20d04668 100755 --- a/doc.zih.tu-dresden.de/util/check-links.sh +++ b/doc.zih.tu-dresden.de/util/check-links.sh @@ -8,57 +8,96 @@ ## ## Author: Martin.Schroschk@tu-dresden.de -set -euo pipefail +set -eo pipefail + +scriptpath=${BASH_SOURCE[0]} +basedir=`dirname "$scriptpath"` +basedir=`dirname "$basedir"` usage() { - echo "Usage: bash $0" + cat <<-EOF +usage: $0 [file | -a] +If file is given, checks whether all links in it are reachable. +If parameter -a (or --all) is given instead of the file, checks all markdown files. +Otherwise, checks whether any changed file contains broken links. +EOF } -# Any arguments? -if [ $# -gt 0 ]; then - usage - exit 1 -fi - mlc=markdown-link-check if ! command -v $mlc &> /dev/null; then echo "INFO: $mlc not found in PATH (global module)" mlc=./node_modules/markdown-link-check/$mlc if ! command -v $mlc &> /dev/null; then echo "INFO: $mlc not found (local module)" - echo "INFO: See CONTRIBUTE.md for information." - echo "INFO: Abort." exit 1 fi fi echo "mlc: $mlc" +LINK_CHECK_CONFIG="$basedir/util/link-check-config.json" +if [ ! -f "$LINK_CHECK_CONFIG" ]; then + echo $LINK_CHECK_CONFIG does not exist + exit 1 +fi + branch="preview" if [ -n "$CI_MERGE_REQUEST_TARGET_BRANCH_NAME" ]; then branch="origin/$CI_MERGE_REQUEST_TARGET_BRANCH_NAME" fi -any_fails=false +function checkSingleFile(){ + theFile="$1" + if [ -e "$theFile" ]; then + echo "Checking links in $theFile" + if ! $mlc -q -c "$LINK_CHECK_CONFIG" -p "$theFile"; then + return 1 + fi + fi + return 0 +} -files=$(git diff --name-only "$(git merge-base HEAD "$branch")") +function checkFiles(){ +any_fails=false echo "Check files:" echo "$files" echo "" for f in $files; do - if [ "${f: -3}" == ".md" ]; then - # do not check links for deleted files - if [ "$f" != "doc.zih.tu-dresden.de/README.md" ]; then - if [ -e $f ]; then - echo "Checking links for $f" - if ! $mlc -q -p "$f"; then - any_fails=true - fi - fi - fi + if checkSingleFile "$f"; then + any_fails=true fi done if [ "$any_fails" == true ]; then exit 1 fi +} + +function checkAllFiles(){ +files=$(git ls-tree --full-tree -r --name-only HEAD $basedir/ | grep '.md$' || true) +checkFiles +} + +function checkChangedFiles(){ +files=$(git diff --name-only "$(git merge-base HEAD "$branch")" | grep '.md$' || true) +checkFiles +} + +if [ $# -eq 1 ]; then + case $1 in + help | -help | --help) + usage + exit + ;; + -a | --all) + checkAllFiles + ;; + *) + checkSingleFile "$1" + ;; + esac +elif [ $# -eq 0 ]; then + checkChangedFiles +else + usage +fi diff --git a/doc.zih.tu-dresden.de/util/link-check-config.json b/doc.zih.tu-dresden.de/util/link-check-config.json new file mode 100644 index 0000000000000000000000000000000000000000..69bb0110a938d9123f4cc678b755ff296b0051c1 --- /dev/null +++ b/doc.zih.tu-dresden.de/util/link-check-config.json @@ -0,0 +1,7 @@ +{ + "ignorePatterns": [ + { + "pattern": "^https://gitlab.hrz.tu-chemnitz.de/zih/hpcsupport/hpc-compendium/-/merge_requests/new$" + } + ] +}