94 lines
3.3 KiB
Bash
Executable File
94 lines
3.3 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# sync.sh — copy this notes extension to every other course
|
|
#
|
|
# _extensions/notes/sync.sh [--dry-run] [--force]
|
|
#
|
|
# Run it from the course where you changed the extension: its
|
|
# _extensions/notes/ is copied to the Notes folder's own _extensions/notes/
|
|
# and to <every course>/_extensions/notes/ (the Notes folder is the one
|
|
# holding _course/).
|
|
#
|
|
# Each copy keeps a .sync-manifest: the checksums of what it received. A copy
|
|
# whose files no longer match it was edited in place; sync.sh then skips it
|
|
# and shows what differs, so a change made in another course is never lost.
|
|
# Bring that change here first, or overwrite it with --force.
|
|
# A copy with a higher `version:` in _extension.yml is skipped the same way.
|
|
#
|
|
# --dry-run only report what would happen
|
|
# --force overwrite edited or newer copies too
|
|
|
|
set -euo pipefail
|
|
|
|
dry=false force=false
|
|
for a in "$@"; do
|
|
case "$a" in
|
|
--dry-run) dry=true ;;
|
|
--force) force=true ;;
|
|
-h|--help) sed -n '2,/^$/{s/^# \{0,1\}//;p}' "$0"; exit 0 ;;
|
|
*) echo "sync: unknown option $a" >&2; exit 1 ;;
|
|
esac
|
|
done
|
|
|
|
src="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
MANIFEST=.sync-manifest
|
|
|
|
root="$src"
|
|
while [ "$root" != "/" ] && [ ! -d "$root/_course" ]; do root="$(dirname "$root")"; done
|
|
[ -d "$root/_course" ] || { echo "sync: no folder holding _course/ above $src" >&2; exit 1; }
|
|
|
|
# checksums of an extension copy, manifest excluded
|
|
sums() { (cd "$1" && find . -type f ! -name "$MANIFEST" -printf '%P\n' | LC_ALL=C sort | xargs -d '\n' md5sum); }
|
|
version() { sed -n 's/^version:[[:space:]]*//p' "$1/_extension.yml" 2>/dev/null | tr -d '"'"'"; }
|
|
newer() { [ "$1" != "$2" ] && [ "$(printf '%s\n%s\n' "$1" "$2" | sort -V | tail -n 1)" = "$1" ]; }
|
|
|
|
src_sums="$(sums "$src")"
|
|
src_version="$(version "$src")"
|
|
$dry || printf '%s\n' "$src_sums" > "$src/$MANIFEST"
|
|
|
|
targets=()
|
|
[ -d "$root/_extensions/notes" ] && targets+=("$root/_extensions/notes")
|
|
for d in "$root"/*/_extensions/notes; do [ -d "$d" ] && targets+=("$d"); done
|
|
|
|
status=0
|
|
for t in "${targets[@]}"; do
|
|
t="$(cd "$t" && pwd)"
|
|
[ "$t" = "$src" ] && continue
|
|
name="${t#"$root"/}"
|
|
cur="$(sums "$t")"
|
|
|
|
if [ "$cur" = "$src_sums" ]; then
|
|
echo "up to date $name"
|
|
$dry || printf '%s\n' "$src_sums" > "$t/$MANIFEST"
|
|
continue
|
|
fi
|
|
reason=""
|
|
if [ ! -f "$t/$MANIFEST" ]; then
|
|
reason="never synced, may hold its own changes"
|
|
elif [ "$cur" != "$(cat "$t/$MANIFEST")" ]; then
|
|
reason="edited since its last sync"
|
|
elif newer "$(version "$t")" "$src_version"; then
|
|
reason="has a newer version ($(version "$t") > $src_version)"
|
|
fi
|
|
if [ -n "$reason" ] && ! $force; then
|
|
echo "SKIPPED $name: $reason"
|
|
diff -rq --exclude="$MANIFEST" "$src" "$t" | sed "s|$root/||g; s|^| |" || true
|
|
status=1
|
|
continue
|
|
fi
|
|
|
|
if $dry; then
|
|
echo "would update $name${reason:+ (forced: $reason)}"
|
|
continue
|
|
fi
|
|
# mirror: drop files the source no longer has, then copy everything over
|
|
(cd "$t" && find . -type f ! -name "$MANIFEST" -printf '%P\n') | while IFS= read -r f; do
|
|
[ -e "$src/$f" ] || rm -f "$t/$f"
|
|
done
|
|
cp -a "$src/." "$t/"
|
|
printf '%s\n' "$src_sums" > "$t/$MANIFEST"
|
|
echo "updated $name${reason:+ (forced: $reason)}"
|
|
done
|
|
|
|
[ $status -eq 0 ] || echo "some copies were skipped: bring their changes here, or rerun with --force"
|
|
exit $status
|