post-receive.in (3944B)
1 #!/bin/sh 2 3 # Copyright (c) 2015-2024 Hiltjo Posthuma <hiltjo@codemadness.org> 4 # Copyright (c) 2025 |Méso|Star> <contact@meso-star.com> 5 # 6 # Permission is hereby granted, free of charge, to any person obtaining a 7 # copy of this software and associated documentation files (the "Software"), 8 # to deal in the Software without restriction, including without limitation 9 # the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 # and/or sell copies of the Software, and to permit persons to whom the 11 # Software is furnished to do so, subject to the following conditions: 12 # 13 # The above copyright notice and this permission notice shall be included in 14 # all copies or substantial portions of the Software. 15 # 16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19 # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 # DEALINGS IN THE SOFTWARE. 23 24 # generic git post-receive hook. 25 # change the config options below and call this script in your post-receive 26 # hook or symlink it. 27 # 28 # usage: $0 [name] 29 # 30 # if name is not set the basename of the current directory is used, 31 # this is the directory of the repo when called from the post-receive script. 32 33 export LC_CTYPE="en_US.UTF-8" 34 35 name="$1" 36 if test "${name}" = ""; then 37 name=$(basename "${PWD}") 38 fi 39 40 # config 41 # paths must be absolute. 42 reposdir="@DIR_GIT@" 43 dir="${reposdir}/${name}" 44 htmldir="@DIR_WWW@" 45 stagitdir="/" 46 destdir="${htmldir}${stagitdir}" 47 cachefile=".cache" 48 # /config 49 50 # Create an index from the list of directories in 'destdir' that correspond to 51 # the list of bare repositories in 'reposdir'. 52 make_index() 53 { 54 tmpfile="${TMPDIR:-/tmp}/git-publish-index.txt" 55 56 # Removes trailing slashes. This allows you to write the following 57 # regular expressions for find directives 58 dir__=$(dirname "${reposdir}") 59 git__=$(basename "${reposdir}") 60 reposdir="${dir__}/${git__}" 61 dir__=$(dirname "${destdir}") 62 www__=$(basename "${destdir}") 63 destdir="${dir__}/${www__}" 64 65 # Build list of candidate git repositories from the directories 66 # of the publicly exposed WWW directory 67 find "${destdir}" -type d -path "${destdir}/*" -prune \ 68 -exec sh -c " 69 printf '%s\n' \"\$@\" \ 70 | sed 's;${destdir}/\(.\{1,\}\)$;${reposdir}/\1.git;' \ 71 | sort" -- {} + > "${tmpfile}" 72 73 # Compare the candidate list to the list of publicly exposed git 74 # repositories. The intersection corresponds to the repositories 75 # to exposed in the HTML index 76 repo_list=$(find "${reposdir}" -path "${reposdir}/*.git" -prune | sort \ 77 | join - "${tmpfile}" | tr '\n' ' ') 78 79 # Generate the index 80 # shellcheck disable=SC2086 81 stagit-index ${repo_list} > "${destdir}/index.html" 82 83 rm -f "${tmpfile}" 84 } 85 86 if ! test -d "${dir}"; then 87 echo "${dir} does not exist" >&2 88 exit 1 89 fi 90 cd "${dir}" || exit 1 91 92 # detect git push -f 93 force=0 94 # shellcheck disable=SC2034 95 while read -r old new ref; do 96 test "${old}" = "0000000000000000000000000000000000000000" && continue 97 test "${new}" = "0000000000000000000000000000000000000000" && continue 98 99 hasrevs=$(git rev-list "${old}" "^${new}" | sed 1q) 100 if test -n "${hasrevs}"; then 101 force=1 102 break 103 fi 104 done 105 106 # strip .git suffix. 107 r=$(basename "${name}") 108 d=$(basename "${name}" ".git") 109 printf "[%s] stagit HTML pages... " "${d}" 110 111 mkdir -p "${destdir}/${d}" 112 cd "${destdir}/${d}" || exit 1 113 114 # remove commits and ${cachefile} on git push -f, this recreated later on. 115 if test "${force}" = "1"; then 116 rm -f "${cachefile}" 117 rm -rf "commit" 118 fi 119 120 make_index 121 122 # make pages. 123 stagit -c "${cachefile}" -u "@BASE_URL@/${d}/" "${reposdir}/${r}" 124 125 ln -sf './log.html' ./index.html 126 ln -sf '../style.css' ./style.css 127 ln -sf '../logo.png' ./logo.png 128 129 echo "done"