Static git server

Stagit allows us to display git repositories running on a previously installed git server.

We give the user git the home: /srv/git and store the repos as /srv/git/repos/my_repo. The web server has its root at /var/www/git/.

Helper-script for executing stagit and stagit-index

First, we create a helper script that scans the repos/folders inside /srv/git/repos and generates a website if a file git-daemon-export-ok exists. Finally, all these public repos are listed in /var/www/git/index.html.

Create /usr/local/bin/update-stagit.sh with

#!/bin/sh

set -eu

REPO_ROOT="/srv/git/repos"
HTML_ROOT="/var/www/git"

mkdir -p "$HTML_ROOT"

INDEX_LIST="$(mktemp)" # creates some temporary file to list repos

# For each repo with a git-daemon-export-ok file, we run stagit
for REPO_PATH in "$REPO_ROOT"/*.git; do

    REPO_NAME="$(basename "$REPO_PATH" .git)"
    REPO_HTML="$HTML_ROOT/$REPO_NAME"

    if [ -f "$REPO_PATH/git-daemon-export-ok" ]; then
        echo "Generating HTML for $REPO_NAME..."
        mkdir -p "$REPO_HTML"
        cd "$REPO_HTML"
        stagit "$REPO_PATH"
        echo "$REPO_PATH" >> "$INDEX_LIST"

    else
        # Delete the folder if it was created earlier
        rm -rf "$REPO_HTML"
    fi

done

# All such repos are then included in the index.html
if [ -s "$INDEX_LIST" ]; then
    echo "Regenerating git index..."
    cd "$HTML_ROOT"
    stagit-index $(cat "$INDEX_LIST") > index.html
else
    echo "No repos with git-daemon-export-ok file found"
    rm -f "$HTML_ROOT/index.html"
fi

rm -f "$INDEX_LIST"
echo "done"

and make it executable:

# chmod +x /usr/local/bin/update-git.sh

Post-receive hook

We now set up a post-receive hook that creates the folder structure for new repos.

Inside /srv/git/template/hooks/post-receive:

#!/bin/sh
/usr/local/bin/update-stagit.sh

Make it executable as well with chmod +x ....

Set in default template

Instruct git to use it as a template:

# git config --system init.templateDir /srv/git/template

Finally, we give all users reading rights to all subfolders of the repo folders:

# chmod -R a+rX /srv/git/repos
# chmod -R a+rX /var/www/git

Usage

New repos can now be initiated on the server with

$ git init --bare my_repo.git

The website and the repo endpoint will be updated upon the first push (and future ones). Make sure to create a git-daemon-export-ok file to make the repo public.