From 21c89bcab197ce7245892e313c4e57f74a3bb119 Mon Sep 17 00:00:00 2001 From: Patrice Chalin Date: Wed, 5 Jun 2024 07:52:52 -0400 Subject: [PATCH] [CI] Script to help know if localized pages need updating --- scripts/lsync.sh | 103 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100755 scripts/lsync.sh diff --git a/scripts/lsync.sh b/scripts/lsync.sh new file mode 100755 index 000000000000..29b633a4f8c7 --- /dev/null +++ b/scripts/lsync.sh @@ -0,0 +1,103 @@ +#!/bin/bash +# +# Inspired by https://github.com/kubernetes/website/blob/main/scripts/lsync.sh + +EXTRA_DIFF_ARGS="--numstat" +TARGET_PATHS="" + +function _usage() { + cat <&2 + exit $status +} + +function process_CLI_args() { + while getopts ":hdv" opt; do + case $opt in + h) + usage + ;; + d) + EXTRA_DIFF_ARGS="" + ;; + v) + VERBOSE=1 + ;; + \?) + echo "ERROR: unrecognized flag: -$OPTARG" + usage 1 + ;; + esac + done + + shift $((OPTIND-1)) + if [ "$#" -lt 1 ]; then + echo "ERROR: target path argument is missing" >&2 + usage 1 + fi + + TARGET_PATHS="$@" + + if [[ -f "TARGET_PATHS" && ! -e "$TARGET_PATHS" ]] ; then + echo "Path not found: '$TARGET_PATHS'" >&2 + exit 2 + fi +} + +function main() { + process_CLI_args "$@" + + if [ -f "$TARGET_PATHS" ] ; then + TARGETS="$TARGET_PATHS" + else + TARGETS=$(find $TARGET_PATHS -name "*.md") + if [[ -z "$TARGETS" ]]; then + echo "ERROR: target directory contains no markdown files: '$TARGET_PATHS'" >&2 + exit 1 + fi + # if [[ -n $VERBOSE ]]; then echo -e "All targets: $TARGETS"; fi + fi + + SYNCED=1 + for f in $TARGETS; do + # if [[ -n $VERBOSE ]]; then echo -e "Checking\t$f"; fi + EN_VERSION=$(echo "$f" | sed "s/content\/.\{2,5\}\//content\/en\//g") + if [[ ! -e "$EN_VERSION" ]]; then + echo "Base file renamed or removed: $EN_VERSION" + SYNCED=0 + continue + fi + + LASTCOMMIT=$(git log -n 1 --pretty=format:%h -- "$f") + git diff --exit-code $EXTRA_DIFF_ARGS $LASTCOMMIT...HEAD "$EN_VERSION" + if [ $? -ne 0 ] ; then + SYNCED=0 + elif [[ -n $VERBOSE ]]; then + echo -e "File is in sync\t$f" + fi + done + if [ $SYNCED -ne 1 ]; then + exit 1 + fi + + echo "$TARGET_PATHS is still in sync" +} + +main "$@"