git_infra

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

commit d70c5dcceadb579de140acb04aa76d7d9c8d5e03
Author: Chris Bracken <chris@bracken.jp>
Date:   Wed, 11 Aug 2021 06:01:45 +0000

Add stagit git post-receive hooks

stagit is static site generation tool for git repos. This commit adds
scripts that can be wired into a git post-receive hook to automatically
regenerate the repo index and pages for the specifie d repo.

To use these scripts, they can be copied (or symlinked) to
`hooks/post-receive` and `hooks/post-receive/stagit` in a git repo
initialised with --bare.

Diffstat:
Apost-receive | 15+++++++++++++++
Apost-receive.d/stagit | 70++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 85 insertions(+), 0 deletions(-)

diff --git a/post-receive b/post-receive @@ -0,0 +1,15 @@ +#!/usr/bin/env sh +data=$(cat) +exitcodes="" +hookname=$(basename $0) +GIT_DIR=${GIT_DIR:-$(dirname $0)/..} + +for hook in ${GIT_DIR}/hooks/${hookname}.d/*; do +test -x "${hook}" && test -f "${hook}" || continue +echo "${data}" | "${hook}" +exitcodes="${exitcodes} $?" +done + +for i in ${exitcodes}; do +[ ${i} -eq 0 ] || exit ${i} +done diff --git a/post-receive.d/stagit b/post-receive.d/stagit @@ -0,0 +1,70 @@ +#!/bin/sh -e + +export LC_CTYPE="en_US.UTF-8" + +# HTML root directory into which stagit will write. +www_root="/jails/git_bracken_jp/usr/local/www/stagit" + +# Hooks are called from the repo directory. +repo_dir=$(pwd) + +# The stagit cache file. +# This is an optimisation to avoid regenerating all pages on each push. +cachefile="${repo_dir}/.htmlcache" + +# The directory under which all repos are located. +# Repos under this directory will be added to the repo index page. +repos_root=$(dirname "$repo_dir") + +# The user-friendly name of the repository. +repo_name=$(basename "$repo_dir" '.git') + +# Detect --force pushes. +cd "${repo_dir}" || exit 1 +force=0 +while read -r old new _; do + [ "${old}" = "0000000000000000000000000000000000000000" ] && continue + [ "${new}" = "0000000000000000000000000000000000000000" ] && continue + + hasrevs=$(git rev-list "$old" "^$new" | sed 1q) + if [ -n "$hasrevs" ]; then + echo "[stagit] Force push detected: cleaning cache" + force=1 + break + fi +done + +# Change to the directory where we will output HTML pages. +repo_web_dir="${www_root}/${repo_name}" +mkdir -p "$repo_web_dir" +cd "$repo_web_dir" || exit 1 + +# If there was a --force push, delete all existing pages. The lack of a cache +# file will trigger a full rebuild below. +if [ "$force" = "1" ]; then + rm -f "$cachefile" + rm -rf "commit" "file" +fi + +# Generate the index page. +# Any repo containing a stagit-no-index file will be skipped. +echo "[stagit] Building repo index" +find "${repos_root}/." -maxdepth 1 \ + -type d \ + -name "*.git" \ + -exec test ! -e "{}/stagit-no-index" \; \ + -print \ + | sort -f \ + | sed -e 's/"/"\\""/g' -e 's/.*/"&"/' \ + | xargs stagit-index \ + > "${www_root}/index.html" + +# Build the pages. If $cachefile is not present, a full rebuild is performed. +echo "[stagit] Building pages" +stagit -c "$cachefile" "$repo_dir" + +# use log as index page +echo "[stagit] Linking assets" +ln -sf log.html index.html +ln -sf ../style.css style.css +ln -sf ../logo.png logo.png