: bharatvaj/scripts

1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
33 
34 
35 
36 
37 
38 
39 
40 
41 
42 
43 
44 
45 
46 
47 
48 
#!/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