diff --git a/scripts/check_links.sh b/scripts/check_links.sh new file mode 100755 index 00000000000..825bf866024 --- /dev/null +++ b/scripts/check_links.sh @@ -0,0 +1,43 @@ +#!/bin/bash +# Copyright (c) Meta Platforms, Inc. and affiliates. +# All rights reserved. +# +# This source code is licensed under the BSD-style license found in the +# LICENSE file in the root directory of this source tree. + +set -euo pipefail + +status=0 +green='\e[1;32m'; red='\e[1;31m'; cyan='\e[1;36m'; yellow='\e[1;33m'; reset='\e[0m' +last_filepath= + +while IFS=: read -r filepath link; do + if [ "$filepath" != "$last_filepath" ]; then + printf '\n%s:\n' "$filepath" + last_filepath=$filepath + fi + if [ -e "$(dirname "$filepath")/${link%%#*}" ]; then + printf " ${green}OK${reset} ${cyan}%s${reset}\n" "$link" + else + printf "${red}FAIL${reset} ${yellow}%s${reset}\n" "$link" >&2 + status=1 + fi +done < <( + git --no-pager grep --no-color -I -o -E \ + '\[[^]]+\]\([^)]*/[^)]*\)|href="[^"]*/[^"]*"' \ + -- '*' \ + ':(exclude).*' \ + ':(exclude)**/.*' \ + ':(exclude)**/*.lock' \ + ':(exclude)**/*.svg' \ + ':(exclude)**/*.xml' \ + ':(exclude)**/third-party/**' \ + | grep -Ev 'https?://' \ + | sed -E \ + -e 's#([^:]+):\[[^]]+\]\(([^)]+)\)#\1:\2#' \ + -e 's#([^:]+):href="([^"]+)"#\1:\2#' \ + -e 's/[[:punct:]]*$//' \ + || true +) + +exit $status