gitpages

A collection of scripts to securely webhost and publish git repositories.
git clone https://scm.kuandu.systems/git-raw/gitpages.git
Log | Files | Refs | README | LICENSE

gitpages-init-repos.sh (4057B)


      1 #!/bin/sh
      2 #
      3 # Copyright (c) 2026 Fred Großkopf
      4 # 
      5 # Permission to use, copy, modify, and/or distribute this software for any
      6 # purpose with or without fee is hereby granted, provided that the above
      7 # copyright notice and this permission notice appear in all copies.
      8 # 
      9 # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     10 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     11 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     12 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     13 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     14 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     15 # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     16 
     17 set -eu
     18 
     19 log() { printf 'info: %s\n' "$1"; }
     20 warn() { printf 'warning: %s\n' "$1" >&2; }
     21 error() {
     22   printf 'error: %s\n' "$1" >&2
     23   exit 1
     24 }
     25 
     26 # Global temp directory (set once in main)
     27 WORKDIR=""
     28 
     29 cleanup() { [ -n "${WORKDIR-}" ] && rm -rf "$WORKDIR"; }
     30 trap cleanup EXIT INT TERM
     31 
     32 ask() {
     33   prompt="$1"
     34   current="${2:-}"
     35 
     36   # Only show prompt if stdin is a terminal
     37   if [ -t 0 ]; then
     38     if [ -n "$current" ]; then
     39       printf '%s [%s]: ' "$prompt" "$current" >&2
     40     else
     41       printf '%s: ' "$prompt" >&2
     42     fi
     43   fi
     44 
     45   read -r answer
     46   case "$answer" in
     47     "") printf '%s\n' "$current" ;;
     48     *)  printf '%s\n' "$answer" ;;
     49   esac
     50 }
     51 
     52 update_post_receive_hook() {
     53   repo_dir="$1"
     54 
     55 
     56   # Install hook into repo
     57   hook_dir="$repo_dir/hooks"
     58   hook_file="$hook_dir/post-receive"
     59 
     60   mkdir -p "$hook_dir"
     61   cp "$POST_RECEIVE" "$hook_file"
     62   chmod 755 "$hook_file"
     63   log "Installed post-receive hook in $repo_dir"
     64 }
     65 
     66 process_repo() {
     67   repo="$1"
     68   repo_name=$(basename "$repo" .git)
     69 
     70   desc_file="$repo/description"
     71   owner_file="$repo/owner"
     72   url_file="$repo/url"
     73 
     74   # Read current values
     75   current_desc=""
     76   [ -f "$desc_file" ] && current_desc="$(cat "$desc_file")"
     77 
     78   current_owner=""
     79   [ -f "$owner_file" ] && current_owner="$(cat "$owner_file")"
     80 
     81   current_url=""
     82   [ -f "$url_file" ] && current_url="$(cat "$url_file")"
     83 
     84   # Show repo boundary
     85   printf '## Repository: %s ##\n' "$repo_name"
     86 
     87   # Ask for description
     88   ask_desc="$WORKDIR/ask_desc"
     89   ask "Description" "$current_desc" > "$ask_desc"
     90   new_desc="$(cat "$ask_desc")"
     91   if [ -n "$new_desc" ]; then
     92     printf '%s\n' "$new_desc" > "$desc_file"
     93   fi
     94 
     95   # Ask for owner
     96   ask_owner="$WORKDIR/ask_owner"
     97   ask "Owner/Contact" "$current_owner" > "$ask_owner"
     98   new_owner="$(cat "$ask_owner")"
     99   if [ -n "$new_owner" ]; then
    100     printf '%s\n' "$new_owner" > "$owner_file"
    101   fi
    102 
    103   # Ask for URL
    104   ask_url="$WORKDIR/ask_url"
    105   ask "URL" "$current_url" > "$ask_url"
    106   new_url="$(cat "$ask_url")"
    107   if [ -n "$new_url" ]; then
    108     printf '%s\n' "$new_url" > "$url_file"
    109   fi
    110 
    111   update_post_receive_hook "$repo"
    112 }
    113 
    114 main() {
    115   # Decide source directory
    116   if [ $# -eq 1 ]; then
    117     GIT_SRC="$1"
    118   else
    119     CONFIG="/etc/gitpages.conf"
    120     [ -r "$CONFIG" ] || error "Config file $CONFIG not read. Please create it or run as ./gitpages-init-repos.sh /path/to/repos"
    121     GIT_SRC=$(awk -F '=' '
    122       $1 ~ /^[[:space:]]*GIT_SRC[[:space:]]*$/ {
    123         gsub(/^[[:space:]]+/, "", $2)
    124         gsub(/[[:space:]]+$/, "", $2)
    125         print $2
    126         exit
    127       }
    128     ' "$CONFIG")
    129     [ -n "$GIT_SRC" ] || error "GIT_SRC not set in $CONFIG"
    130   fi
    131 
    132   [ -d "$GIT_SRC" ] || error "Directory '$GIT_SRC' not found"
    133 
    134   # Create one working temp dir, removed by trap
    135   WORKDIR="$(mktemp -d -t gitpages_ask.XXXXXX)" ||
    136     error "mktemp failed"
    137 
    138   # Find script directory and locate post‑receive
    139   script_dir=$(cd "$(dirname "$0")" && pwd -P)
    140   POST_RECEIVE="$script_dir/post-receive.hook"
    141   [ -f "$POST_RECEIVE" ] || error "post-receive helper not found in $script_dir"
    142 
    143   # Gather repos into "$@"
    144   set -- $(find "$GIT_SRC" -maxdepth 1 -name '*.git' -type d)
    145 
    146   # Process each repo
    147   for repo in "$@"; do
    148     if [ -d "$repo" ]; then
    149       process_repo "$repo"
    150     fi
    151   done
    152 
    153   log "Initialization complete for all repos in $GIT_SRC"
    154 }
    155 
    156 main "$@"