|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +# Inspired by https://github.com/kubernetes/website/blob/main/scripts/lsync.sh |
| 4 | + |
| 5 | +EXTRA_DIFF_ARGS="--numstat" |
| 6 | +TARGET_PATHS="" |
| 7 | + |
| 8 | +function _usage() { |
| 9 | + cat <<EOS |
| 10 | +Usage: $(basename "$0") [options] TARGET_PATH ... |
| 11 | +
|
| 12 | + For each localized page target, this script reports whether the English |
| 13 | + language version of that page has changed since the localized file was |
| 14 | + first written or last edited. |
| 15 | +
|
| 16 | + TARGET_PATH can be a single markdown file of a localized page, such as |
| 17 | + 'content/ja/_index.md', or a directory of localized pages, such as 'content/ja'. |
| 18 | +
|
| 19 | + -h Output this usage info. |
| 20 | + -d Output diff details. |
| 21 | + -v Verbose mode. |
| 22 | +EOS |
| 23 | +} |
| 24 | + |
| 25 | +function usage() { |
| 26 | + local status=${1:-0} |
| 27 | + _usage 1>&2 |
| 28 | + exit $status |
| 29 | +} |
| 30 | + |
| 31 | +function process_CLI_args() { |
| 32 | + while getopts ":hdv" opt; do |
| 33 | + case $opt in |
| 34 | + h) |
| 35 | + usage |
| 36 | + ;; |
| 37 | + d) |
| 38 | + EXTRA_DIFF_ARGS="" |
| 39 | + ;; |
| 40 | + v) |
| 41 | + VERBOSE=1 |
| 42 | + ;; |
| 43 | + \?) |
| 44 | + echo "ERROR: unrecognized flag: -$OPTARG" |
| 45 | + usage 1 |
| 46 | + ;; |
| 47 | + esac |
| 48 | + done |
| 49 | + |
| 50 | + shift $((OPTIND-1)) |
| 51 | + if [ "$#" -lt 1 ]; then |
| 52 | + echo "ERROR: target path argument is missing" >&2 |
| 53 | + usage 1 |
| 54 | + fi |
| 55 | + |
| 56 | + TARGET_PATHS="$@" |
| 57 | + |
| 58 | + if [[ -f "TARGET_PATHS" && ! -e "$TARGET_PATHS" ]] ; then |
| 59 | + echo "Path not found: '$TARGET_PATHS'" >&2 |
| 60 | + exit 2 |
| 61 | + fi |
| 62 | +} |
| 63 | + |
| 64 | +function main() { |
| 65 | + process_CLI_args "$@" |
| 66 | + |
| 67 | + if [ -f "$TARGET_PATHS" ] ; then |
| 68 | + TARGETS="$TARGET_PATHS" |
| 69 | + else |
| 70 | + TARGETS=$(find $TARGET_PATHS -name "*.md") |
| 71 | + if [[ -z "$TARGETS" ]]; then |
| 72 | + echo "ERROR: target directory contains no markdown files: '$TARGET_PATHS'" >&2 |
| 73 | + exit 1 |
| 74 | + fi |
| 75 | + # if [[ -n $VERBOSE ]]; then echo -e "All targets: $TARGETS"; fi |
| 76 | + fi |
| 77 | + |
| 78 | + SYNCED=1 |
| 79 | + for f in $TARGETS; do |
| 80 | + # if [[ -n $VERBOSE ]]; then echo -e "Checking\t$f"; fi |
| 81 | + EN_VERSION=$(echo "$f" | sed "s/content\/.\{2,5\}\//content\/en\//g") |
| 82 | + if [[ ! -e "$EN_VERSION" ]]; then |
| 83 | + echo "Base file renamed or removed: $EN_VERSION" |
| 84 | + SYNCED=0 |
| 85 | + continue |
| 86 | + fi |
| 87 | + |
| 88 | + LASTCOMMIT=$(git log -n 1 --pretty=format:%h -- "$f") |
| 89 | + git diff --exit-code $EXTRA_DIFF_ARGS $LASTCOMMIT...HEAD "$EN_VERSION" |
| 90 | + if [ $? -ne 0 ] ; then |
| 91 | + SYNCED=0 |
| 92 | + elif [[ -n $VERBOSE ]]; then |
| 93 | + echo -e "File is in sync\t$f" |
| 94 | + fi |
| 95 | + done |
| 96 | + if [ $SYNCED -ne 1 ]; then |
| 97 | + exit 1 |
| 98 | + fi |
| 99 | + |
| 100 | + echo "$TARGET_PATHS is still in sync" |
| 101 | +} |
| 102 | + |
| 103 | +main "$@" |
0 commit comments