commit e9abe83bf452f0db21c7a26ffcbf86bfc8c70ca2
Author: Brian C. Lane <bcl@brianlane.com>
Date: Wed, 9 Jun 2021 07:23:52 -0700
Initial commit
Diffstat:
A | README.md | | | 47 | +++++++++++++++++++++++++++++++++++++++++++++++ |
A | post-receive | | | 80 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
A | setup-stagit-remote | | | 49 | +++++++++++++++++++++++++++++++++++++++++++++++++ |
3 files changed, 176 insertions(+), 0 deletions(-)
diff --git a/README.md b/README.md
@@ -0,0 +1,47 @@
+# Self hosting git repositories as static html pages
+
+The [stagit project](https://codemadness.org/git/stagit/file/README.html)
+generates static html pages for your git repositories. You can even set it up
+as a remote so that pushing to it will update the pages and copy them to
+wherever you are hosting your domain.
+
+* Check out `git://git.codemadness.org/stagit` and run make
+* Install the binary to a location in your `$PATH`
+* Create a top level directory to hold the stagit copy of the repos. This is called `reposdir` in `setup-stagit-remote`.
+* Copy the `*.png` files from the stagit repository to `reposdir` or use your own.
+* Copy the `post-receive` script somewhere outside the `stagit-files` repo so that it can be customized.
+* Edit the `setup-stagit-remote` script:
+** set `reposdir` to the directory you created to hold the repos.
+** set `post_receive` to the path of the copy of the script.
+* Edit the copy of the `post-receive` script:
+** Set BASEURL to where you are serving the git repos from
+** `RSYNC_DEST` if you want to use the `rsync` at the end of the file to copy them over
+** `reposdir` to match the `reposdir` from setup-stagit-remote
+** Check the rsync at the end of the script and replace with your own upload method if needed.
+
+*WARNING* Do not put sensitive information in the post-receive script, it is uploaded to the html
+server as part of the git repository and is exposed to the world.
+
+The `reposdir` should *not* be the same directory as your normal repos. It will
+contain bare clones of them and only be used to build the html pages and upload
+them to your html server. Multiple repos will be under this directory, with an
+`index.html` file pointing to them.
+
+The `setup-stagit-script` only supports `master` and `main` branches, if you
+have named the `master` branch something else edit the script and add it to the
+case that currently only matches `origin/main`.
+
+
+# Setup a new repo
+
+`cd` to the working directory of one of your repos and check out the primary
+branch. Then run `setup-stagit-remote`. It will prompt you to enter a
+description of the project, this will be displayed on the html pages. It will
+then make a bare clone of the repo and add a new remote, named `stagit`, to
+your git config for this repo.
+
+You can now run `git push stagit master` and it will push to the `$reposdir`
+repo, run `stagit` on it, and upload it to your html server.
+
+Now you can visit your webpage, and test cloning one of the repos to make sure
+everything is working.
diff --git a/post-receive b/post-receive
@@ -0,0 +1,80 @@
+#!/bin/sh
+# generic git post-receive hook.
+# change the config options below and call this script in your post-receive
+# hook or symlink it.
+#
+# usage: $0 [name]
+#
+# if name is not set the basename of the current directory is used,
+# this is the directory of the repo when called from the post-receive script.
+
+# NOTE: needs to be set for correct locale (expects UTF-8) otherwise the
+# default is LC_CTYPE="POSIX".
+export LC_CTYPE="en_US.UTF-8"
+
+name="$1"
+if test "${name}" = ""; then
+ name=$(basename "$(pwd)")
+fi
+
+# config
+# paths must be absolute.
+BASEURL="https://www.yourhost.com/git"
+RSYNC_DEST="yourhost.com:public_html/git/"
+reposdir="$HOME/stagit-repos"
+
+dir="${reposdir}/${name}"
+htmldir="${reposdir}"
+stagitdir="/"
+destdir="${htmldir}${stagitdir}"
+cachefile=".htmlcache"
+# /config
+
+if ! test -d "${dir}"; then
+ echo "${dir} does not exist" >&2
+ exit 1
+fi
+cd "${dir}" || exit 1
+
+# detect git push -f
+force=0
+while read -r old new ref; do
+ test "${old}" = "0000000000000000000000000000000000000000" && continue
+ test "${new}" = "0000000000000000000000000000000000000000" && continue
+
+ hasrevs=$(git rev-list "${old}" "^${new}" | sed 1q)
+ if test -n "${hasrevs}"; then
+ force=1
+ break
+ fi
+done
+
+printf "[%s] stagit HTML pages... " "${name}"
+
+mkdir -p "${destdir}/${name}"
+cd "${destdir}/${name}" || exit 1
+
+# remove commits and ${cachefile} on git push -f, this recreated later on.
+if test "${force}" = "1"; then
+ rm -f "${cachefile}"
+ rm -rf "commit"
+fi
+
+# make index.
+stagit-index "${reposdir}/"*/ > "${destdir}/index.html"
+
+# Create the URL for clone
+echo "${BASEURL}/${name}" > url
+git update-server-info
+
+# make pages.
+stagit -c "${cachefile}" "${reposdir}/${name}"
+
+ln -sf log.html index.html
+ln -sf ../style.css style.css
+ln -sf ../logo.png logo.png
+
+# Update the server
+rsync -a "${destdir}/" "${RSYNC_DEST}"
+
+echo "done"
diff --git a/setup-stagit-remote b/setup-stagit-remote
@@ -0,0 +1,49 @@
+#!/usr/local/bin/bash
+reposdir="$HOME/stagit-repos"
+post_receive="$HOME/stagit-files/post-receive"
+
+if [ ! -e "${post_receive}" ]; then
+ echo "${post_receive} script is missing"
+ exit 1
+fi
+
+name="$(basename "$PWD")"
+if [ -e "${reposdir}/${name}" ]; then
+ echo "Project ${name} already setup"
+ exit 1
+fi
+if git remote|grep -q stagit; then
+ echo "stagit remote already setup"
+ exit 1
+fi
+
+read -p "Enter a description of the ${name} project: " description
+if [ -z "${description}" ]; then
+ echo "Please enter a description"
+ exit 1
+fi
+
+# Try to figure out the name of the main/master branch
+BRANCH="master"
+for b in $(git branch -r); do
+ case $b in
+ origin/main)
+ BRANCH="main"
+ break
+ ;;
+ *)
+ ;;
+ esac
+done
+echo "Using '${BRANCH}' as the name of main branch"
+
+# Create the bare repo
+pushd "${reposdir}" || exit 1
+git init "${name}" -b "${BRANCH}" --bare
+echo "${description}" > "${name}/description"
+popd || exit 1
+
+cp "${post_receive}" "${reposdir}/${name}/hooks"
+git remote add stagit "${reposdir}/${name}"
+
+echo "${name} setup for stagit updates"