#!/bin/sh # Git has no post-push hook, so this pre-push hook starts a background job # that waits for the push to land (the remote-tracking ref matches the pushed # commit) and then publishes the package. Kody git serves the last published # snapshot until you publish, so this keeps the live site and clones current. # Log: .git/kody-publish.log. Skip with KODY_SKIP_PUBLISH=1 git push. [ "$KODY_SKIP_PUBLISH" = "1" ] && exit 0 command -v kody >/dev/null 2>&1 || { echo "kody CLI not found; skipping auto-publish" >&2; exit 0; } remote="$1" pkg=$(node -p "require('./package.json').name" 2>/dev/null) || exit 0 git_dir=$(git rev-parse --git-dir) log="$git_dir/kody-publish.log" while read -r local_ref local_sha remote_ref remote_sha; do [ "$remote_ref" = "refs/heads/main" ] || continue case "$local_sha" in 0000000000000000000000000000000000000000) continue ;; esac tracking="refs/remotes/$remote/main" ( i=0 while [ $i -lt 120 ]; do if [ "$(git rev-parse -q --verify "$tracking" 2>/dev/null)" = "$local_sha" ]; then echo "[$(date)] publishing $pkg at $local_sha" >>"$log" kody package publish "$pkg" >>"$log" 2>&1 exit 0 fi sleep 1 i=$((i + 1)) done echo "[$(date)] push of $local_sha did not land within 2 min; not publishing" >>"$log" ) /dev/null 2>&1 & echo "Kody: will publish $pkg once the push lands (log: $log)" >&2 done exit 0