git-server

git server-side stuff | stagit
git clone git://git.ckyln.com/~cem/hooks.git
Log | Files | Refs | README

post-receive (4323B)


      1 #!/bin/sh
      2 # shellcheck source=./config.def
      3 
      4 # A post-receive hook to form stagit pages, creating tarballs, reinitializing
      5 # stagit-index.
      6 #
      7 # This script can be run for each repository, without pushing, by going to the
      8 # repository directory and running 'hooks/post-receive'.
      9 
     10 # Parse the configuration file
     11 hooksdir=$(readlink -f "$0") hooksdir=${hooksdir%/*}
     12 . "$hooksdir/config"
     13 . "$hooksdir/lib"
     14 
     15 # Store the repository name in a variable. We strip the directory names and
     16 # '.git' here.
     17 repository=${PWD##*/} repository=${repository%.git}
     18 repository_path=${PWD}
     19 
     20 # Push to mirrors if a 'mirrors' file exists on the base directory.
     21 [ -f mirrors ] && while read -r mirror; do
     22     git push --mirror "$mirror"
     23 done < mirrors
     24 
     25 # Generate files parsed by 'stagit'.
     26 [ -f owner ] || printf '%s\n' "$OWNER" > owner
     27 [ -f url   ] || printf '%s\n' "$BASE_URL/$repository" > url
     28 
     29 # Make sure we export the current repository.
     30 :> git-daemon-export-ok
     31 
     32 # Recreate the stagit structure
     33 rm -rf "${STAGIT_DIR:?}/$repository"
     34 mkdir -p "$STAGIT_DIR/$repository"
     35 
     36 # Generate the index for every exported repository
     37 set --
     38 for repo in "$REPO_DIR/"*/git-daemon-export-ok; do set -- "$@" "${repo%/*}"; done
     39 stagit-index "$@" |
     40     sed 's|log\.html|index.html|' |
     41     sed "s|<title>Repositories</title>|<title>${TITLE:-Repositories}</title>|" |
     42     sed "s|\"desc\">Repositories|\"desc\">${TITLE:-Repositories}|" |
     43     if [ "$EXTERN_URL" ]; then
     44         sed "s|<img|<a href=\"$EXTERN_URL\"><img|;s|</td>\$|</a></td>|"
     45     else
     46         cat
     47     fi > "$STAGIT_DIR/index.html"
     48 
     49 # Install style files
     50 [ "$CSS" ]     && install -Dm644 "$CSS"     "$STAGIT_DIR/style.css"
     51 [ "$LOGO" ]    && install -Dm644 "$LOGO"    "$STAGIT_DIR/logo.png"
     52 [ "$FAVICON" ] && install -Dm644 "$FAVICON" "$STAGIT_DIR/favicon.png"
     53 
     54 (
     55 cd "$STAGIT_DIR/$repository" || return 1
     56 
     57 # We create a cache to the .cache file so log creation is faster on larger
     58 # repositories.
     59 stagit -c .cache "$repository_path"
     60 
     61 for file in style.css logo.png favicon.png; do
     62     [ -f "../$file" ] && ln -sf "../$file" "$file"
     63 done
     64 
     65 # If a README file exists, link the index to the README. If it doesn't, link the
     66 # index to the repository log.
     67 rm -f index.html
     68 # for file in file/*.html; do
     69 #     [ -f "$file" ] || continue
     70 #     case "${file##*/}" in README*)
     71 #         sed 's|"../|"|' "$file" > index.html
     72 #     esac
     73 # done
     74 [ -f index.html ] || ln -sf log.html index.html
     75 )
     76 
     77 
     78 # Create archives and plain raw files
     79 [ "$ARCHIVE_ALL" = 1 ] || [ -f archive ] && {
     80     mkdir -p "$STAGIT_DIR/archive/$repository"
     81     git show-ref --tags --heads |
     82         while read -r _ ref; do
     83             tag=${ref#refs/*/}
     84             tarball="$STAGIT_DIR/archive/$repository/$repository-$tag.tar.gz"
     85 
     86             # Unless we are working with a branch, skip recreation of a tarball.
     87             [ "${ref##refs/heads/*}" ] && [ -f "$tarball" ] && continue
     88 
     89             git archive \
     90                 --format tar.gz \
     91                 --prefix "$repository-$tag/" \
     92                 -o "$tarball" \
     93                 -- "$tag"
     94         done
     95     for dir in archive "archive/$repository"; do (
     96         cd "$STAGIT_DIR/$dir" || exit 1
     97         lsindex "$STAGIT_DIR" > index.html
     98     ) done
     99 }
    100 
    101 [ "$RAW_ALL" = 1 ] || [ -f raw ] && {
    102     git show-ref --heads |
    103         while read -r _ ref; do
    104             branch=${ref##*/}
    105             branchdir="$STAGIT_DIR/raw/$repository/$branch"
    106 
    107             # Make sure we clean up the directory by removing it completely and
    108             # recreating it completely.
    109             rm -rf "$branchdir"; mkdir -p "$branchdir"
    110 
    111             # We create and extract a tar archive. I don't know a better way than
    112             # this, so suggestions are welcome.
    113             git archive --format tar -- "$branch" |
    114                 (cd "$branchdir" || return 1 ; tar xf -)
    115 
    116             # Create an index for all directories in the repository
    117             find "$branchdir" -type d | while read -r dir; do (
    118                 cd "$dir" || exit 1
    119                 lsindex "$STAGIT_DIR" > index.html
    120             ) done
    121 
    122         done
    123     for dir in raw "raw/$repository"; do (
    124         cd "$STAGIT_DIR/$dir" || exit 1
    125         lsindex "$STAGIT_DIR" > index.html
    126     ) done
    127 }
    128 
    129 # If a repository has an individual hook named post-receive.extra, run it.
    130 [ -f hooks/post-receive.extra ] && . hooks/post-receive.extra