blog

Source files for chris.bracken.jp
git clone https://git.bracken.jp/blog.git
Log | Files | Refs | Submodules | README | LICENSE

publish.sh (1352B)


      1 #!/bin/sh
      2 
      3 PUBLISH_REPO=git@git.bracken.jp:chris.bracken.jp.git
      4 
      5 # Returns whether the git repo at path $1 has any uncommitted diffs.
      6 has_diffs() {
      7   git -C $1 status > /dev/null
      8   git -C $1 diff-index --quiet HEAD -- && return 1 || return 0
      9 }
     10 
     11 # Prompts the user with $1. Returns whether user replied y/Y.
     12 prompt_yn() {
     13   read -p "$1" -r REPLY
     14   echo
     15   test `echo $REPLY | tr a-z A-Z | head -c 1` = Y
     16 }
     17 
     18 # Check for hugo command.
     19 command -v hugo >/dev/null 2>&1 || { echo >&2 "hugo not found. Aborting."; exit 1; }
     20 
     21 # If blog repo has uncommitted diffs, abort.
     22 if has_diffs .; then
     23   echo >&2 "Not all diffs have been committed. Commit and re-run. Aborting."
     24   exit 1
     25 fi
     26 
     27 # Ensure submodules are updated.
     28 git submodule update --init
     29 
     30 # If public dir exists, abort.
     31 if [ -d public ]; then
     32   echo >&2 "public directory exists. Remove and re-run. Aborting."
     33   exit 1
     34 fi
     35 
     36 # Clone the repo and build.
     37 git clone $PUBLISH_REPO public
     38 hugo || { echo >&2 "hugo build failed. Aborting."; exit 1; }
     39 
     40 # Check diffs and publish.
     41 echo "Build succeeded. Checking diffs..."
     42 if ! has_diffs public; then
     43   echo >&2 "No changes to published site."
     44 else
     45   git -C public diff
     46   if prompt_yn "Commit and publish? "; then
     47     git -C public add .
     48     git -C public commit -S -m "Publish site"
     49     git -C public push origin master
     50   fi
     51 fi
     52 
     53 echo "Cleaning up..."
     54 rm -rf public