From 33cb51928e06f5e0bf88b3108a6332e71a5ec18a Mon Sep 17 00:00:00 2001 From: yequari Date: Thu, 17 Aug 2023 10:50:47 -0700 Subject: [PATCH] initial commit --- site-update.sh | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100755 site-update.sh diff --git a/site-update.sh b/site-update.sh new file mode 100755 index 0000000..3b8836b --- /dev/null +++ b/site-update.sh @@ -0,0 +1,82 @@ +#!/usr/bin/env bash +# update a git repository and copy its contents to a directory +set -o errexit +set -o nounset +set -o pipefail +if [[ "${TRACE-0}" == "1" ]]; then + set -o xtrace +fi + +help() { + echo "Update a git repository and copy the files to OUTDIR" + echo "Intended for use with static websites" + echo + echo "Usage: ./site-update.sh [-b BRANCH|d SRCDIR|f FILE|h|p] OUTDIR" + echo "Options" + echo "b BRANCH git branch to update, defaults to the current branch in SRCDIR" + echo "d SRCDIR directory containing git repository to update, defaults to current working directory" + echo "f FILE update a batch of repositories, as defined in FILE" + echo "h print this help message" + echo "p publish files only, do not update" +} + +get_changes() { + git fetch origin + if [[ $(git status | grep behind) ]]; then + git merge origin/"${BRANCH}" + fi +} + +publish () { + cp -r "${SRCDIR/*}" "${OUTDIR}" +} + +main() { + echo "$@" + OUTDIR="$1" + if [[ ! -d "$OUTDIR" ]]; then + echo "Invalid directory $OUTDIR" + fi + if [[ -n "$SRCDIR" ]]; then + cd "${SRCDIR}" + fi + if [[ -n "$BRANCH" ]]; then + git checkout "${BRANCH}" + else + BRANCH=$(git branch --show-current) + fi + if [[ "$PUBONLY" == false ]]; then + get_changes + fi + diff "${SRCDIR} ${OUTDIR}" + local rc=$? + if [[ $rc -eq 1 ]]; then + publish + fi +} + +SRCDIR= +BRANCH= +FILE= +PUBONLY=false + +while getopts ":hd:f:b:p" option; do + case $option in + b) # select branch + BRANCH=$OPTARG;; + f) # file listing directories + FILE=$OPTARG;; + d) # directory to act on + SRCDIR=$OPTARG;; + h) #display help + help + exit;; + p) #publish only, do not update + PUBONLY=true;; + \?) #invalid option + echo "Error: Invalid option" + exit;; + esac +done + +main "${@:$OPTIND}"