example_post-receive.sh (2485B) - raw


      1 #!/bin/sh
      2 # generic git post-receive hook.
      3 # change the config options below and call this script in your post-receive
      4 # hook or symlink it.
      5 #
      6 # NOTE, things to do manually (once) before running this script:
      7 # - modify the categories in the for loop with your own.
      8 #
      9 # usage: sh example_post-receive.sh [reponame]
     10 #
     11 # if reponame is not set the basename of the current directory is used,
     12 # this is the directory of the repo when called from the post-receive script.
     13 
     14 # NOTE: needs to be set for correct locale (expects UTF-8) otherwise the
     15 #       default is LC_CTYPE="POSIX".
     16 export LC_CTYPE="en_US.UTF-8"
     17 
     18 # paths must be absolute
     19 reposdir="/srv/git"
     20 webdir="/srv/git/html"
     21 cachefile=".stagit-build-cache"
     22 
     23 is_public_and_listed() {
     24     if [ ! -f "$1/git-daemon-export-ok" ] || [ ! -f "$1/category" ]; then
     25         return 1
     26     fi
     27     return 0
     28 }
     29 
     30 is_forced_update() {
     31     while read -r old new ref; do
     32         test "$old" = "0000000000000000000000000000000000000000" && continue
     33         test "$new" = "0000000000000000000000000000000000000000" && continue
     34 
     35         hasrevs="$(git rev-list "$old" "^$new" | sed 1q)"
     36         if test -n "$hasrevs"; then
     37             return 0
     38         fi
     39     done
     40     return 1
     41 }
     42 
     43 make_repo_web() {
     44     reponame="$(basename "$1" ".git")"
     45     printf "[%s] stagit HTML pages... " "$reponame"
     46 
     47     # if forced update, remove directory and cache file
     48     is_forced_update && printf "forced update... " && rm -rf "$webdir/$reponame"
     49 
     50     mkdir -p "$webdir/$reponame"
     51     cd "$webdir/$reponame" || return 1
     52 
     53     # make pages
     54     stagit -c "$cachefile" -u "https://git.oscarbenedito.com/$reponame/" "$1"
     55 
     56     # symlinks
     57     [ -f "about.html" ] \
     58         && ln -sf "about.html" "index.html" \
     59         || ln -sf "log.html" "index.html"
     60 
     61     echo "done"
     62 }
     63 
     64 make_stagit_index() {
     65     printf "Generating stagit index... "
     66 
     67     # generate index arguments
     68     args=""
     69     for category in "Projects" "Personal setup" "Miscellanea"; do
     70         args="$args -c \"$category\""
     71         for repo in "$reposdir/"*.git/; do
     72             repo="${repo%/}"
     73             is_public_and_listed "$repo" || continue
     74             [ "$(cat "$repo/category")" = "$category" ] && args="$args $repo"
     75         done
     76     done
     77 
     78     # make index
     79     echo "$args" | xargs stagit-index > "$webdir/index.html"
     80 
     81     echo "done"
     82 }
     83 
     84 if [ "$1" = "" ]; then
     85     repo="$(pwd)"
     86 else
     87     repo="$reposdir/$1"
     88 fi
     89 
     90 cd "$repo" || exit 1
     91 is_public_and_listed "$repo" || exit 0
     92 
     93 make_repo_web "$repo"
     94 make_stagit_index