git_infra

Git infra scripts for git.bracken.jp
git clone https://git.bracken.jp/git_infra.git
Log | Files | Refs | LICENSE

publish_gemini (2016B)


      1 #!/bin/sh -e
      2 #
      3 # post-receive.d hook: publish the Gemini capsule to agate's content root.
      4 #
      5 # Replaces yakumo's hourly cronjob:
      6 #   git -C ~/gemini/chris.bracken.jp fetch origin \
      7 #     && git -C ~/gemini/chris.bracken.jp reset --hard origin/master
      8 #
      9 # agate serves one subdirectory per configured hostname, so the target is
     10 # content/<hostname>/ and NOT content/ itself.
     11 #
     12 # Install:
     13 #   /home/git/git_infra/post-receive.d/publish_gemini            (this file, chmod +x)
     14 #   /home/git/<capsule-repo>.git/hooks/post-receive.d/publish_gemini -> symlink
     15 #
     16 # Requires zroot/data/gemini mounted READ-WRITE into the git jail at
     17 # /var/gemini, and the content tree owned by the git user (1001). agate runs
     18 # as gemini (1965) in its own jail and only needs to read, which the world
     19 # bits allow. .certificates stays owned by 1965 - agate may rewrite it.
     20 
     21 BRANCH="master"
     22 TARGET="/var/gemini/content/chris.bracken.jp"
     23 
     24 # agate reads these as a different user in a different jail.
     25 umask 022
     26 
     27 repo_dir=$(pwd)
     28 
     29 if [ ! -d "$TARGET" ]; then
     30     echo "[gemini] ERROR: $TARGET missing - is the nullfs mount attached?" >&2
     31     exit 1
     32 fi
     33 if [ ! -w "$TARGET" ]; then
     34     echo "[gemini] ERROR: $TARGET not writable - check ownership (should be git)" >&2
     35     exit 1
     36 fi
     37 
     38 # stdin gives one line per updated ref: <old> <new> <refname>
     39 pushed=0
     40 while read -r _old new ref; do
     41     [ "$ref" = "refs/heads/${BRANCH}" ] || continue
     42     # Branch deletion - do not wipe the live capsule.
     43     [ "$new" = "0000000000000000000000000000000000000000" ] && continue
     44     pushed=1
     45 done
     46 
     47 if [ "$pushed" = "0" ]; then
     48     echo "[gemini] ${BRANCH} not updated; nothing to publish"
     49     exit 0
     50 fi
     51 
     52 echo "[gemini] Publishing ${BRANCH} to ${TARGET}"
     53 
     54 git --git-dir="$repo_dir" --work-tree="$TARGET" checkout -f "$BRANCH"
     55 
     56 # checkout -f does not remove files deleted from the branch. This is what
     57 # `reset --hard` handled before.
     58 #
     59 # NOTE: deletes ANY untracked file under $TARGET.
     60 git --git-dir="$repo_dir" --work-tree="$TARGET" clean -fd
     61 
     62 echo "[gemini] Done"