60 行
1.7 KiB
Bash
実行ファイル
60 行
1.7 KiB
Bash
実行ファイル
#!/usr/bin/env bash
|
|
set -euo pipefail
|
|
|
|
readonly source_directory=$(cd "$(dirname "$0")/.." && pwd)
|
|
readonly sites_directory=${SITES_DIRECTORY:-$source_directory/sites}
|
|
readonly lock_file=${LOCK_FILE:-$source_directory/sites.lock}
|
|
|
|
content_digest() {
|
|
(cd "$1" && find . -type f -print0 | LC_ALL=C sort -z | xargs -0 sha256sum --binary | sha256sum | cut -d' ' -f1)
|
|
}
|
|
|
|
if [ "$#" -lt 3 ]; then
|
|
printf 'usage: sync.sh PROJECT REPOSITORY REFERENCE [--apply]\n' >&2
|
|
exit 2
|
|
fi
|
|
|
|
readonly project=$1
|
|
readonly repository=$2
|
|
readonly reference=$3
|
|
|
|
if [[ ! $project =~ ^[A-Za-z0-9][A-Za-z0-9._-]*$ ]]; then
|
|
printf 'invalid project name: %s\n' "$project" >&2
|
|
exit 2
|
|
fi
|
|
|
|
if ! commit=$(git -C "$repository" rev-parse --verify --quiet "$reference^{commit}"); then
|
|
printf 'reference not found: %s\n' "$reference" >&2
|
|
exit 2
|
|
fi
|
|
readonly commit
|
|
|
|
if ! git -C "$repository" cat-file -e "$commit:docs" 2>/dev/null; then
|
|
printf 'no docs directory at %s\n' "$commit" >&2
|
|
exit 2
|
|
fi
|
|
|
|
if [ "${4:-}" != "--apply" ]; then
|
|
printf 'would replace: sites/%s with docs of %s\n' "$project" "$commit"
|
|
exit 0
|
|
fi
|
|
|
|
staging=$(mktemp -d)
|
|
trap 'rm -rf "$staging"' EXIT
|
|
|
|
git -C "$repository" -c core.autocrlf=false -c core.eol=lf archive "$commit" docs | tar -x -C "$staging"
|
|
rm -f "$staging/docs/.nojekyll"
|
|
rm -rf "${sites_directory:?}/$project"
|
|
mkdir -p "$sites_directory"
|
|
mv "$staging/docs" "$sites_directory/$project"
|
|
|
|
digest=$(content_digest "$sites_directory/$project")
|
|
remaining=$(mktemp)
|
|
if [ -f "$lock_file" ]; then
|
|
grep -v "^$project " "$lock_file" > "$remaining" || true
|
|
fi
|
|
printf '%s %s %s\n' "$project" "$commit" "$digest" >> "$remaining"
|
|
LC_ALL=C sort "$remaining" > "$lock_file"
|
|
rm -f "$remaining"
|
|
printf 'replaced: sites/%s with docs of %s\n' "$project" "$commit"
|