git: Fix issues with post-receive hook

And run any repo-local hooks.
This commit is contained in:
Alex Palaistras 2022-01-01 19:31:19 +00:00
parent df490a3a7e
commit 346f31c0b0

View File

@ -6,8 +6,9 @@
set -euo pipefail set -euo pipefail
# Base configuration. # Base configuration.
GIT_PUBLIC_DIR="/var/lib/git/public" GIT_REPO_PATH="$(pwd)"
GIT_SERVE_DIR="/var/lib/git-serve" GIT_PUBLIC_PATH="/var/lib/git/public"
GIT_SERVE_PATH="/var/lib/git-serve"
# Other global variables. # Other global variables.
__GIT_FORCE_PUSH= __GIT_FORCE_PUSH=
@ -16,7 +17,7 @@ __GIT_FORCE_PUSH=
function get-repository-name() { function get-repository-name() {
local name="$1" local name="$1"
if test -z "$name"; then if test -z "$name"; then
name=$(basename "$(pwd)") name=$(basename "$GIT_REPO_PATH")
fi fi
echo -n "$name" echo -n "$name"
@ -46,7 +47,7 @@ function is-force-push() {
# Generate static HTML files for given repository. # Generate static HTML files for given repository.
function stagit-generate-repo() { function stagit-generate-repo() {
local repo=$(get-repository-name "$1") local repo=$(get-repository-name "$1")
local repopath="$GIT_PUBLIC_DIR/$repo" local repopath="$GIT_PUBLIC_PATH/$repo"
if ! test -d "$repopath"; then if ! test -d "$repopath"; then
echo "Repository '$repo' is not a public repository, skipping..." >&2 echo "Repository '$repo' is not a public repository, skipping..." >&2
@ -56,17 +57,17 @@ function stagit-generate-repo() {
local name=$(basename "${repo}" ".git") local name=$(basename "${repo}" ".git")
echo -n "Generating static HTML pages for '$name'... " echo -n "Generating static HTML pages for '$name'... "
mkdir -p "$GIT_SERVE_DIR/$name" mkdir -p "$GIT_SERVE_PATH/$name"
cd "$GIT_SERVE_DIR/$name" || return 1 cd "$GIT_SERVE_PATH/$name" || return 1
# Remove commits and cache file on 'git push -f', this recreated later on. # Remove commits and cache file on 'git push -f', this recreated later on.
if is-force-push; then if is-force-push; then
rm -Rf "$GIT_SERVE_DIR/$name/commit" rm -Rf "$GIT_SERVE_PATH/$name/commit"
rm -f "$GIT_SERVE_DIR/.$name.cache" rm -f "$GIT_SERVE_PATH/.$name.cache"
fi fi
# Generate static HTML for given repository path. # Generate static HTML for given repository path.
stagit -c "$GIT_SERVE_DIR/.$name.cache" "$repopath" stagit -c "$GIT_SERVE_PATH/.$name.cache" "$repopath"
ln -sf log.html index.html ln -sf log.html index.html
for f in style.css logo.png; do for f in style.css logo.png; do
@ -79,12 +80,12 @@ function stagit-generate-repo() {
# Generate static HTML files for all repositories in public directory. # Generate static HTML files for all repositories in public directory.
function stagit-generate-index() { function stagit-generate-index() {
echo -n "Generating static HTML pages for public git index... " echo -n "Generating static HTML pages for public git index... "
stagit-index "$GIT_PUBLIC_DIR/"*/ > "$GIT_SERVE_DIR/index.html" stagit-index "$GIT_PUBLIC_PATH/"*/ > "$GIT_SERVE_PATH/index.html"
# Copy example assets if none are found being served. # Copy example assets if none are found being served.
for f in style.css logo.png; do for f in style.css logo.png; do
if ! test -f "$GIT_SERVE_DIR/$f"; then if ! test -f "$GIT_SERVE_PATH/$f"; then
cp -f "/usr/share/doc/stagit/$f" "$GIT_SERVE_DIR/$f" cp -f "/usr/share/doc/stagit/$f" "$GIT_SERVE_PATH/$f"
fi fi
done done
@ -92,4 +93,9 @@ function stagit-generate-index() {
} }
# Attempt to generate static files for public repository. # Attempt to generate static files for public repository.
stagit-generate-repo "$1" && stagit-generate-index stagit-generate-repo "${1:-}" && stagit-generate-index
# Attempt to run any repo-level hooks.
if test -x "$GIT_REPO_PATH/hooks/post-receive"; then
cd "$GIT_REPO_PATH" && source "$GIT_REPO_PATH/hooks/post-receive"
fi