sites

Source for the daat.foo sites
git clone https://git.daat.foo/sites.git
Log | Files | Refs | Submodules

git-host.sh (4163B)


      1 #!/bin/sh
      2 # Minimal Git + stagit hosting manager.
      3 set -eu
      4 
      5 REPOS=${REPOS:-/srv/git}
      6 WWW=${WWW:-/var/www/git}
      7 OWNER=${OWNER:-cowmonk}
      8 CLONE_BASE=${CLONE_BASE:-git@100.x.y.z:/srv/git}
      9 SELF=/srv/git-host/git-host.sh
     10 
     11 die() {
     12     echo "git-host: $*" >&2
     13     exit 1
     14 }
     15 
     16 valid_name() {
     17     case "$1" in
     18         ""|.*|*/*|*..*|*[!a-zA-Z0-9_.-]*)
     19             die "invalid repository name: $1" ;;
     20     esac
     21 }
     22 
     23 repo_path() {
     24     printf '%s/%s.git\n' "$REPOS" "$1"
     25 }
     26 
     27 setup_meta() {
     28     name=$1
     29     desc=$2
     30     owner=$3
     31     url=$4
     32     repo=$(repo_path "$name")
     33 
     34     printf '%s\n' "$desc" > "$repo/description"
     35     printf '%s\n' "$owner" > "$repo/owner"
     36     printf '%s\n' "$url" > "$repo/url"
     37 
     38     git -C "$repo" config core.sharedRepository false
     39     git -C "$repo" config receive.denyNonFastForwards true
     40 
     41     # Update static Git HTTP metadata, if used later.
     42     git -C "$repo" update-server-info
     43 
     44     cat > "$repo/hooks/post-receive" <<EOF
     45 #!/bin/sh
     46 exec "$SELF" build "$name"
     47 EOF
     48     chmod 755 "$repo/hooks/post-receive"
     49 }
     50 
     51 create() {
     52     name=$1
     53     desc=$2
     54     owner=$3
     55     url=$4
     56 
     57     valid_name "$name"
     58     repo=$(repo_path "$name")
     59 
     60     [ ! -e "$repo" ] || die "repository already exists: $name"
     61 
     62     mkdir -p "$REPOS" "$WWW"
     63     git init --bare "$repo"
     64     setup_meta "$name" "$desc" "$owner" "$url"
     65 
     66     echo "Created $repo"
     67     index
     68 }
     69 
     70 import_repo() {
     71     name=$1
     72     source=$2
     73     desc=$3
     74     owner=$4
     75     url=$5
     76 
     77     valid_name "$name"
     78     repo=$(repo_path "$name")
     79 
     80     [ ! -e "$repo" ] || die "repository already exists: $name"
     81 
     82     mkdir -p "$REPOS" "$WWW"
     83     git clone --bare "$source" "$repo"
     84     setup_meta "$name" "$desc" "$owner" "$url"
     85 
     86     build "$name"
     87 }
     88 
     89 build() {
     90     name=$1
     91     valid_name "$name"
     92     repo=$(repo_path "$name")
     93 
     94     [ -d "$repo" ] || die "unknown repository: $name"
     95 
     96     # Empty repositories cannot be rendered yet.
     97     if ! git -C "$repo" rev-parse --verify HEAD >/dev/null 2>&1; then
     98         echo "Skipping empty repository: $name"
     99         index
    100         return
    101     fi
    102 
    103     out="$WWW/$name"
    104     mkdir -p "$out"
    105 
    106     # Rebuild completely, including after force pushes.
    107     # Only files inside this repository's output are removed.
    108     find "$out" -mindepth 1 -maxdepth 1 -exec rm -rf -- {} +
    109 
    110     (
    111         cd "$out"
    112         stagit "$repo"
    113         ln -sf log.html index.html
    114         ln -sf ../style.css style.css
    115 
    116         for asset in favicon.png logo.png; do
    117             if [ -f "$WWW/$asset" ]; then
    118                 ln -sf "../$asset" "$asset"
    119             fi
    120         done
    121     )
    122 	export_dir="$WWW/$name.git"
    123 	
    124 	if [ ! -d "$export_dir" ]; then
    125     	git clone --bare "$repo" "$export_dir"
    126 	else
    127     	git -C "$export_dir" fetch --prune "$repo" \
    128         	'+refs/heads/*:refs/heads/*' \
    129         	'+refs/tags/*:refs/tags/*'
    130 	fi
    131 	
    132 	git -C "$export_dir" update-server-info
    133 
    134     git -C "$repo" update-server-info
    135     index
    136     echo "Built $name"
    137 }
    138 
    139 index() {
    140     mkdir -p "$WWW"
    141     set --
    142 
    143     for repo in "$REPOS"/*.git; do
    144         [ -d "$repo" ] || continue
    145         if git -C "$repo" rev-parse --verify HEAD >/dev/null 2>&1; then
    146             set -- "$@" "$repo"
    147         fi
    148     done
    149 
    150     if [ "$#" -eq 0 ]; then
    151         echo "No published repositories yet."
    152         return
    153     fi
    154 
    155     tmp="$WWW/.index.$$"
    156     trap 'rm -f "$tmp"' EXIT HUP INT TERM
    157     stagit-index "$@" > "$tmp"
    158     mv -f "$tmp" "$WWW/index.html"
    159     trap - EXIT HUP INT TERM
    160 }
    161 
    162 all() {
    163     for repo in "$REPOS"/*.git; do
    164         [ -d "$repo" ] || continue
    165         name=${repo##*/}
    166         name=${name%.git}
    167         build "$name"
    168     done
    169     index
    170 }
    171 
    172 usage() {
    173     cat <<EOF
    174 Usage:
    175   git-host.sh new NAME DESCRIPTION OWNER URL
    176   git-host.sh import NAME SOURCE DESCRIPTION OWNER URL
    177   git-host.sh build NAME
    178   git-host.sh index
    179   git-host.sh all
    180 EOF
    181     exit 1
    182 }
    183 
    184 cmd=${1:-}
    185 [ "$#" -gt 0 ] && shift || :
    186 
    187 case "$cmd" in
    188     new)
    189         [ "$#" -eq 4 ] || usage
    190         create "$@" ;;
    191     import)
    192         [ "$#" -eq 5 ] || usage
    193         import_repo "$@" ;;
    194     build)
    195         [ "$#" -eq 1 ] || usage
    196         build "$1" ;;
    197     index)
    198         [ "$#" -eq 0 ] || usage
    199         index ;;
    200     all)
    201         [ "$#" -eq 0 ] || usage
    202         all ;;
    203     *)
    204         usage ;;
    205 esac
    206