: bharatvaj/scripts

commit dc65e62a24a64cf70eb6a4058e5ae6b36265dd82
parent 11cb1e75fcbc126b091bc60e50712be321109d46
Author: Bharatvaj Hemanth <bharatvaj@yahoo.com>
Date: Thu, 29 Oct 2026 20:18:31 +0530

Reuse stagit publisher for queued Gitolite push updates
5 files changed, 169 insertions(+), 44 deletions(-)
A
server/check-stagit-publish.py
|
48
++++++++++++++++++++++++++++++++++++++++++++++++
M
server/setup-stagit-pages.sh
|
92
+++++++++++++++++++++++++++++++++++++++++--------------------------------------
A
server/stagit-publish.md
|
46
++++++++++++++++++++++++++++++++++++++++++++++
A
server/stagit-publish.path
|
9
+++++++++
A
server/stagit-publish.service
|
18
++++++++++++++++++
diff --git a/server/check-stagit-publish.py b/server/check-stagit-publish.py
@@ -0,0 +1,48 @@
+"""Run with stagit, stagit-index, git and rsync on PATH."""
+import os
+from pathlib import Path
+import subprocess
+import tempfile
+
+here = Path(__file__).resolve().parent
+hook = Path(os.environ["STAGIT_TEST_HOOK"]).resolve() if "STAGIT_TEST_HOOK" in os.environ else None
+
+
+def run(*args, cwd=None, env=None):
+    return subprocess.check_output(args, cwd=cwd, env=env, text=True)
+
+
+with tempfile.TemporaryDirectory() as tmp:
+    root = Path(tmp)
+    repos, docroot = root / "repos", root / "www"
+    docroot.mkdir()
+    (docroot / "style.css").write_text("/* user styles */")
+    for name in ("group/public", "group/private"):
+        repo = repos / (name + ".git")
+        repo.parent.mkdir(parents=True, exist_ok=True)
+        run("git", "init", "--bare", str(repo))
+    public = repos / "group/public.git"
+    private = repos / "group/private.git"
+    run("git", "--git-dir=" + str(public), "config", "gitolite.stagit", "true")
+    env = os.environ.copy()
+    env.update(STAGIT_SITE_NAME="Test", STAGIT_CLONE_PREFIX="https://git.example.org/",
+               STAGIT_SCRIPT="/enhancements.js", STAGIT_SITE_LOGO="/logo.png")
+    publisher = str(here / "setup-stagit-pages.sh")
+    run("sh", publisher, str(repos), cwd=docroot, env=env)
+    assert 'href="group/public/"' in (docroot / "index.html").read_text()
+    assert "group/private" not in (docroot / "index.html").read_text()
+    assert not (docroot / "group/private").exists()
+    assert "https://git.example.org/group/public" in (docroot / "group/public/index.html").read_text()
+    (docroot / "group/public/style.css").write_text("/* custom repo styles */")
+    run("sh", publisher, str(repos), cwd=docroot, env=env)
+    assert (docroot / "style.css").read_text() == "/* user styles */"
+    assert (docroot / "group/public/style.css").read_text() == "/* custom repo styles */"
+    if hook:
+        pending = root / "pending"
+        env.update(GL_REPO="group/private", STAGIT_PENDING=str(pending))
+        subprocess.run(["sh", str(hook)], cwd=private, env=env, input="", check=True, text=True)
+        assert not pending.exists()
+        env["GL_REPO"] = "group/public"
+        subprocess.run(["sh", str(hook)], cwd=public, env=env, input="", check=True, text=True)
+        assert pending.exists()
+print("Publisher and hook checks passed")
diff --git a/server/setup-stagit-pages.sh b/server/setup-stagit-pages.sh
@@ -1,44 +1,48 @@
-#!/bin/sh -e
-
-# setup-stagit-pages.sh <bare_repo_dir>
-# Generates stagit pages for reposities present in $bare_repo_dir
-# The output folders will be generated in the current folder
-
-bare_repos_dir=${1:-/home/git/repositories}
-gitolite_dir="/root/gitolite-admin"
-
-[ -d "${bare_repos_dir}" ] || { echo "Directory ${bare_repos_dir} not found, skipping stagit run"; exit 0; }
-
-[ -d "${gitolite_dir}" ] || { echo "Directory ${gitolite_dir} not found"; exit 1; }
-git --git-dir "${gitolite_dir}"/.git pull
-
-for i in $(grep @projects "${gitolite_dir}/conf/gitolite.conf" -m1 | cut -d= -f 2); do
-    projects="${projects} $i.git"
-done
-
-echo "# Generating web pages for: $projects"
-
-project_dirs=""
-for project in ${projects}; do
-    project_dirs="${project_dirs} ${bare_repos_dir}/${project}"
-done
-
-echo "# Generating index.html"
-( set -x; stagit-index ${project_dirs} > index.html )
-
-echo "# Generating web page for $i"
-for i in ${projects}; do
-    repo_name=${i}
-    dir_name=${repo_name%%.git}
-    [ -d ${dir_name} ] || mkdir ${dir_name}
-    cd ${dir_name}
-    ( set -x; stagit ${bare_repos_dir}/$i )
-    cd ..
-done
-
-
-echo "# Copying assets"
-for i in */; do
-    cp style.css $i/
-    [ -f $i/logo.png ] || cp logo.png $i/
-done
+#!/bin/sh
+set -eu
+
+# Run from the web docroot. Only explicitly enabled Gitolite repos are published.
+# Site branding/clone prefixes are environment settings, not repository content.
+bare_repos_dir=$(cd "${1:-/home/git/repositories}" && pwd -P)
+site_name=${STAGIT_SITE_NAME:-Repositories}
+clone_prefix=${STAGIT_CLONE_PREFIX:-}
+stage=$(mktemp -d)
+trap 'rm -rf "$stage"' EXIT HUP INT TERM
+
+find "$bare_repos_dir" -type d -name '*.git' -prune | sort > "$stage/repos"
+set --
+last_group=
+while IFS= read -r repo; do
+    [ "$(git --git-dir="$repo" config --bool --get gitolite.stagit || :)" = true ] || continue
+    name=${repo#"$bare_repos_dir"/}
+    name=${name%.git}
+    # Do not let a repository name escape the docroot or become a CLI option.
+    case "$name" in ''|-*|/*|..|../*|*/../*|*/..) echo "Unsafe repo name: $name" >&2; exit 1;; esac
+    group=${name%/*}
+    [ "$group" != "$name" ] || group=Repositories
+    if [ "$group" != "$last_group" ]; then
+        set -- "$@" -c "$group"
+        last_group=$group
+    fi
+    set -- "$@" "$repo"
+    root_path=$(printf '%s\n' "$name" | awk -F/ '{ for (i=1; i<=NF; i++) printf "../" }')
+    mkdir -p "$stage/site/$name"
+    (
+        cd "$stage/site/$name"
+        STAGIT_REPO_PREFIX="$bare_repos_dir/" STAGIT_ROOT_PATH="$root_path" \
+            stagit -n "$site_name" -h "$clone_prefix" "$repo"
+    )
+    # Keep existing user styles/assets; stagit itself does not generate these.
+    mkdir -p "$name"
+    rsync -a "$stage/site/$name/" "$name/"
+    for asset in style.css logo.png; do
+        [ ! -f "$asset" ] || [ -e "$name/$asset" ] || cp "$asset" "$name/$asset"
+    done
+    printf 'Published %s\n' "$name"
+done < "$stage/repos"
+
+[ "$#" -gt 0 ] || { echo 'No repositories enabled with gitolite.stagit' >&2; exit 1; }
+STAGIT_REPO_PREFIX="$bare_repos_dir/" stagit-index -n "$site_name" "$@" > "$stage/index.html"
+# Publish the index atomically on the docroot filesystem.
+cp "$stage/index.html" .stagit-index.tmp
+mv .stagit-index.tmp index.html
diff --git a/server/stagit-publish.md b/server/stagit-publish.md
@@ -0,0 +1,46 @@
+# Push-triggered stagit publication
+
+`setup-stagit-pages.sh` is the publisher; the copy under a repository's `raw/`
+web path is output, not the installed executable. Install the reviewed script
+as root at `/usr/local/libexec/setup-stagit-pages.sh` (mode 755, not git-writable).
+
+The common Gitolite post-receive hook queues a rebuild by touching
+`/var/lib/stagit/pending` only for repositories with `gitolite.stagit=true`.
+Configure that flag only on public repository groups in gitolite-admin.
+The hook does not execute pushed scripts, run sudo, or wait for page generation.
+Existing mirroring and MinCI remain independent.
+
+Install the `.path` and `.service` files in `/etc/systemd/system/`. Create
+`/var/lib/stagit` owned by git, and grant git write access to the static docroot
+and generated public repo directories. Do not grant git write access to the
+publisher, systemd units, or environment configuration.
+
+Site settings go in `/etc/default/stagit-publish` (root-owned):
+
+```ini
+STAGIT_SITE_NAME=git.nply.org
+STAGIT_CLONE_PREFIX=https://git.nply.org/
+STAGIT_SITE_LOGO=/logo.png
+STAGIT_SCRIPT=/enhancements.js
+```
+
+Enable with `systemctl enable --now stagit-publish.path`.
+
+- Full rebuild: `touch /var/lib/stagit/pending` as git, or start the service.
+- Logs: `journalctl -u stagit-publish.service`.
+- Disable automatic builds: `systemctl disable --now stagit-publish.path`.
+- Test: `STAGIT_TEST_HOOK=/path/to/post-receive python3 check-stagit-publish.py`.
+
+The service runs as git with a filesystem sandbox. It clears the pending marker
+before building, so pushes during generation leave another marker for the next
+run. `flock` serializes builds. Multiple pushes are coalesced into a full rebuild
+of enabled repos; use per-repo queuing only if build duration becomes a problem.
+The index is replaced atomically; repo HTML is copied after each generation.
+This is not an atomic whole-site deployment. Failures are visible in the journal;
+a new push or manual queue retries them.
+
+The generator never changes repository HEADs or pulls worktrees. It preserves
+existing CSS/logo files. It does not remove previously published paths when
+files are deleted or a repo is made private: revocation requires removing that
+repo's public output explicitly. Disabling its flag prevents future publication
+but is not a privacy cleanup mechanism.
diff --git a/server/stagit-publish.path b/server/stagit-publish.path
@@ -0,0 +1,9 @@
+[Unit]
+Description=Watch for queued stagit publication
+
+[Path]
+PathExists=/var/lib/stagit/pending
+Unit=stagit-publish.service
+
+[Install]
+WantedBy=multi-user.target
diff --git a/server/stagit-publish.service b/server/stagit-publish.service
@@ -0,0 +1,18 @@
+[Unit]
+Description=Publish public Gitolite repositories with stagit
+
+[Service]
+Type=oneshot
+User=git
+Group=git
+WorkingDirectory=/srv/http/git.nply.org
+EnvironmentFile=/etc/default/stagit-publish
+ExecStartPre=/usr/bin/rm -f /var/lib/stagit/pending
+ExecStart=/usr/bin/flock /var/lib/stagit/publish.lock /usr/local/libexec/setup-stagit-pages.sh /home/git/repositories
+UMask=0022
+NoNewPrivileges=true
+PrivateTmp=true
+ProtectSystem=strict
+ProtectHome=read-only
+ReadWritePaths=/srv/http/git.nply.org /var/lib/stagit
+TimeoutStartSec=15min