commit 25373ae115f6712fff6179f76f445917edbcd058 parent cfda4a72d45208ae49b583beefd5cc02687cc46c Author: Chris Bracken <chris@bracken.jp> Date: Thu, 11 Apr 2019 20:43:56 -0700 Improve publish.sh Extract out has_diffs and prompt_yn bash functions. Diffstat:
M | publish.sh | | | 31 | +++++++++++++++++++++---------- |
1 file changed, 21 insertions(+), 10 deletions(-)
diff --git a/publish.sh b/publish.sh @@ -2,17 +2,31 @@ PUBLISH_REPO=git@github.com:cbracken/cbracken.github.io.git -if ! git diff-index --quiet HEAD --; then - echo >&2 "Not all diffs have been committed. Aborting." - exit 1 -fi +# Returns whether the git repo at path $1 has any uncommitted diffs. +function has_diffs() { + git -C $1 status > /dev/null + git -C $1 diff-index --quiet HEAD -- && return 1 || return 0 +} + +# Prompts the user with $1. Returns whether user replied y/Y. +function prompt_yn() { + read -p "$1" -n 1 -r + echo + if [[ $REPLY =~ ^[Yy]$ ]]; then return 0; else return 1; fi +} # Check for hugo command. command -v hugo >/dev/null 2>&1 || { echo >&2 "hugo not found. Aborting."; exit 1; } +# If blog repo has uncommitted diffs, abort. +if has_diffs .; then + echo >&2 "Not all diffs have been committed. Commit and re-run. Aborting." + exit 1 +fi + # If public dir exists, abort. if [[ -d public ]]; then - echo >&2 "public directory already exists. Aborting." + echo >&2 "public directory exists. Remove and re-run. Aborting." exit 1 fi @@ -22,14 +36,11 @@ hugo || { echo >&2 "hugo build failed. Aborting."; exit 1; } # Check diffs and publish. echo "Build succeeded. Checking diffs..." -git -C public status -if git -C public diff-index --quiet HEAD --; then +if ! has_diffs public; then echo >&2 "No changes to published site." else git -C public diff - read -p "Commit and publish? " -n 1 -r - echo - if [[ $REPLY =~ ^[Yy]$ ]]; then + if prompt_yn "Commit and publish? "; then git -C public add . git -C public commit -m "Publish site" git -C public push origin master