diff --git a/tools/branch-deploy.sh b/tools/branch-deploy.sh index a2bfb9c..260bfd0 100755 --- a/tools/branch-deploy.sh +++ b/tools/branch-deploy.sh @@ -7,10 +7,19 @@ set -u TARGET_FOLDER=$(realpath $1) BASE_URL="$2" +# Arguments to add to the Hugo call +HUGO_ARGUMENTS="--gc" + +# Ensure the target folder exists mkdir -p "${TARGET_FOLDER}" -# Track all remote branches locally +# Prune branches that have disappeared from origin git remote prune origin > /dev/null +PRUNE_BRANCHES=$(git fetch -p ; git branch -r | awk '{print $1}' | egrep -v -f /dev/fd/0 <(git branch -vv | grep origin) | awk '{print $1}') +echo $PRUNE_BRANCHES | xargs --no-run-if-empty git branch -d > /dev/null +echo $PRUNE_BRANCHES | xargs --no-run-if-empty -I '{}' rm -rf ${TARGET_FOLDER}/{} + +# Track all remote branches locally git branch -r | grep -v '\->' | sed "s,\x1B\[[0-9;]*[a-zA-Z],,g" | while read remote; do git branch --track "${remote#origin/}" "$remote" >/dev/null 2>&1; done # Pull the branches @@ -29,7 +38,7 @@ for BRANCH in $(git for-each-ref --format='%(refname:short)' refs/heads); do git --work-tree="${TEMP_FOLDER}" checkout "${BRANCH}" -- . # Build to the destination folder - hugo --gc -b "${BASE_URL}/${BRANCH}" -s "${TEMP_FOLDER}" -d "${TARGET_FOLDER}/${BRANCH}" > /dev/null + hugo ${HUGO_ARGUMENTS} -b "${BASE_URL}/${BRANCH}" -s "${TEMP_FOLDER}" -d "${TARGET_FOLDER}/${BRANCH}" > /dev/null # Cleanup the temp folder rm -rf "${TEMP_FOLDER}"