git_infra

Git infra scripts for git.bracken.jp
git clone https://git.bracken.jp/git_infra.git
Log | Files | Refs | LICENSE

gout (2758B)


      1 #!/bin/sh -e
      2 
      3 export LC_CTYPE="en_US.UTF-8"
      4 
      5 # Add Mastodon verification URL to index header.
      6 me_url="https://famichiki.jp/@akande"
      7 
      8 # HTML root directory into which gout will write.
      9 www_root="/usr/local/www/git.bracken.jp"
     10 
     11 # Hooks are called from the repo directory.
     12 repo_dir=$(pwd)
     13 
     14 # The gout cache file.
     15 # This is an optimisation to avoid regenerating all pages on each push.
     16 cachefile="${repo_dir}/.htmlcache"
     17 
     18 # The default branch of the repository.
     19 default_branch=$(git symbolic-ref HEAD 2>/dev/null || echo "refs/heads/main")
     20 
     21 # The directory under which all repos are located.
     22 # Repos under this directory will be added to the repo index page.
     23 repos_root=$(dirname "$repo_dir")
     24 
     25 # The user-friendly name of the repository.
     26 repo_name=$(basename "$repo_dir" '.git')
     27 
     28 # Detect --force pushes.
     29 cd "${repo_dir}" || exit 1
     30 force=0
     31 while read -r old new ref; do
     32   # Ignore force pushes to non-default branches.
     33   [ "${ref}" = "${default_branch}" ] || continue
     34 
     35   [ "${old}" = "0000000000000000000000000000000000000000" ] && continue
     36   [ "${new}" = "0000000000000000000000000000000000000000" ] && continue
     37 
     38   hasrevs=$(git rev-list "$old" "^$new" | sed 1q)
     39   if [ -n "$hasrevs" ]; then
     40     echo "[gout] Force push detected on ${ref}: cleaning cache"
     41     force=1
     42     break
     43   fi
     44 done
     45 
     46 # If there was a --force push, delete all existing pages. The lack of a cache
     47 # file will trigger a full rebuild below.
     48 if [ "$force" = "1" ]; then
     49   rm -f "$cachefile"
     50   rm -rf "commit" "file"
     51 fi
     52 
     53 # Build the pages. If $cachefile is not present, a full rebuild is performed.
     54 if [ -e "${repo_dir}/git-daemon-export-ok" ]; then
     55   # Change to the directory where we will output HTML pages.
     56   repo_web_dir="${www_root}/${repo_name}"
     57   mkdir -p "$repo_web_dir"
     58   cd "$repo_web_dir" || exit 1
     59 
     60   # Generate the index page.
     61   # Any repo containing a gout-no-index file will not be indexed.
     62   echo "[gout] Building repo index"
     63   find "${repos_root}/." -maxdepth 1            \
     64     -type d                                     \
     65     -name "*.git"                               \
     66     -exec test -e "{}/git-daemon-export-ok" \;  \
     67     -exec test ! -e "{}/gout-no-index" \;     \
     68     -print                                      \
     69     | sort -f                                   \
     70     | sed -e 's/"/"\\""/g' -e 's/.*/"&"/'       \
     71     | xargs gout_index -m "$me_url"           \
     72     > "${www_root}/index.html"
     73 
     74   # Rebuild the pages for the repo (including for unlisted repos).
     75   echo "[gout] Building pages"
     76   gout -c "$cachefile" "$repo_dir"
     77 
     78   # use log as index page
     79   echo "[gout] Linking assets"
     80   ln -sf log.html index.html
     81   ln -sf ../style.css style.css
     82   ln -sf ../logo.png logo.png
     83   ln -sf ../favicon.png favicon.png
     84 else
     85   echo "[gout] Not building pages: private repo"
     86 fi