git_infra

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

gout_html (2830B)


      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 # root directory into which gout will write pages.
      9 www_root="/var/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 # Directory into which repo pages will be written.
     29 repo_web_dir="${www_root}/${repo_name}"
     30 
     31 # Detect --force pushes.
     32 cd "${repo_dir}" || exit 1
     33 force=0
     34 while read -r old new ref; do
     35   # Ignore force pushes to non-default branches.
     36   [ "${ref}" = "${default_branch}" ] || continue
     37 
     38   [ "${old}" = "0000000000000000000000000000000000000000" ] && continue
     39   [ "${new}" = "0000000000000000000000000000000000000000" ] && continue
     40 
     41   hasrevs=$(git rev-list "$old" "^$new" | sed 1q)
     42   if [ -n "$hasrevs" ]; then
     43     echo "[gout] Force push detected on ${ref}: cleaning cache"
     44     force=1
     45     break
     46   fi
     47 done
     48 
     49 # If there was a --force push, delete all existing pages. The lack of a cache
     50 # file will trigger a full rebuild below.
     51 if [ "$force" = "1" ]; then
     52   rm -f "$cachefile"
     53   rm -rf "${repo_web_dir}/commit" "${repo_web_dir}/file"
     54 fi
     55 
     56 # Build the pages. If $cachefile is not present, a full rebuild is performed.
     57 if [ -e "${repo_dir}/git-daemon-export-ok" ]; then
     58   # Change to the directory where we will output pages.
     59   mkdir -p "$repo_web_dir"
     60   cd "$repo_web_dir" || exit 1
     61 
     62   # Generate the index page.
     63   # Any repo containing a gout-no-index file will not be indexed.
     64   echo "[gout] Building repo index"
     65   find "${repos_root}/." -maxdepth 1            \
     66     -type d                                     \
     67     -name "*.git"                               \
     68     -exec test -e "{}/git-daemon-export-ok" \;  \
     69     -exec test ! -e "{}/gout-no-index" \;     \
     70     -print                                      \
     71     | sort -f                                   \
     72     | sed -e 's/"/"\\""/g' -e 's/.*/"&"/'       \
     73     | xargs gout_index -m "$me_url"           \
     74     > "${www_root}/index.html"
     75 
     76   # Rebuild the pages for the repo (including for unlisted repos).
     77   echo "[gout] Building pages"
     78   gout -c "$cachefile" "$repo_dir"
     79 
     80   # use log as index page
     81   echo "[gout] Linking assets"
     82   ln -sf log.html index.html
     83   ln -sf ../style.css style.css
     84   ln -sf ../logo.png logo.png
     85   ln -sf ../favicon.png favicon.png
     86 else
     87   echo "[gout] Not building pages: private repo"
     88 fi