diff options
author | Parabola git <git@parabola.nu> | 2020-12-14 06:00:02 +0000 |
---|---|---|
committer | Parabola git <git@parabola.nu> | 2020-12-14 06:00:02 +0000 |
commit | 15dbd0089eee0fd0c5070e05f46cf85953037392 (patch) | |
tree | c329c5f3f64809b3cd10478dafae6d360d932ee3 | |
parent | 45d67dbc4d2537716aac1295331c92f5b4457313 (diff) |
Update from cron
-rwxr-xr-x | .config/git/hooks/post-receive.d/notify-pbot-git-hook | 116 |
1 files changed, 56 insertions, 60 deletions
diff --git a/.config/git/hooks/post-receive.d/notify-pbot-git-hook b/.config/git/hooks/post-receive.d/notify-pbot-git-hook index 5100b40..b830222 100755 --- a/.config/git/hooks/post-receive.d/notify-pbot-git-hook +++ b/.config/git/hooks/post-receive.d/notify-pbot-git-hook @@ -1,54 +1,48 @@ #!/bin/bash -# refs data via STDIN - one line per target ref (where ref_data is: prev_sha curr_sha ref_file) +# notify-pbot-git-hook - git post-receive hook for pbot-say +# +# Copyright 2019-2020 bill-auger <bill-auger@programmer.net> +# +# This file is part of the 'pbot' program +# +# 'pbot' is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# 'pbot' is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License version 3 +# along with 'pbot'. If not, see <http://www.gnu.org/licenses/>. + + +# this hook receives data via STDIN - one line per pushed ref +# each as 'ref_data' - where 'ref_data' is: prev_sha curr_sha ref_file # e.g. -# a559985626bbf4448ccf9041857449078db6d731 1d5002ade3f71200274f1ba39e35a732d86220b5 refs/heads/master -# 0000000000000000000000000000000000000000 1d5002ade3f71200274f1ba39e35a732d86220b5 refs/heads/new-branch +# 0000000000000000000000000000000000000000 0123456789abcdef0123456789abcdef01234567 refs/heads/new-branch +# 0123456789abcdef0123456789abcdef01234567 89abcdef0123456789abcdef0123456789abcdef refs/heads/existing-branch +# 89abcdef0123456789abcdef0123456789abcdef 0000000000000000000000000000000000000000 refs/heads/deleted-branch ## constants ## + readonly LOG_PBOT_GIT_HOOK=1 +readonly LIB_DIR=/usr/lib/parabola-hackers readonly LOG_DIR=/var/log/pbot readonly SHA_REGEX='^([0-9a-f]{40})$' +readonly REPO=$(basename $(pwd)) +readonly LOG_FILE=${LOG_DIR}/notify-pbot-$(date +%Y.%m.%d-%H.%M.%S)-${REPO} ## logging ## -LogFile() # (repo ref) -{ - local repo=$1 - local ref=$2 - - echo ${LOG_DIR}/notify-pbot-${repo}-${ref} -} - -LogParseRefDataIn() # (ref_data prev_sha curr_sha ref_file repo ref is_deletion) +LogParseRefData() { - local ref_data=$1 ; local prev_sha=$2 ; local curr_sha=$3 ; local ref_file=$4 ; - local repo=$5 ; local ref=$6 ; local is_deletion=$7 ; - local log_file=$(LogFile ${repo} ${ref}) - - (( $LOG_PBOT_GIT_HOOK )) && echo "ref_data=${ref_data}" > ${log_file} && \ - echo "prev=${prev}" >> ${log_file} && \ - echo "curr=${curr}" >> ${log_file} && \ - echo "ref_file=${ref_file}" >> ${log_file} && \ - echo "ref=${ref}" >> ${log_file} && \ - echo "is_deletion=${is_deletion}" >> ${log_file} -} - -LogParseRefDataOut() # (hacker author log_msg n_behind n_ahead n_total repo ref) -{ - local hacker=$1 ; local author=$2 ; local log_msg=$3 ; local n_behind=$4 ; - local n_ahead=$5 ; local n_total=$6 ; local repo=$7 ; local ref=$8 ; - local log_file=$(LogFile ${repo} ${ref}) - - (( $LOG_PBOT_GIT_HOOK )) && echo "hacker=${hacker}" >> ${log_file} && \ - echo "author=${author}" >> ${log_file} && \ - echo "log_msg=${log_msg}" >> ${log_file} && \ - echo "n_behind=${n_behind}" >> ${log_file} && \ - echo "n_ahead=${n_ahead}" >> ${log_file} && \ - echo "n_total=${n_total}" >> ${log_file} && \ - echo "repo=${repo}" >> ${log_file} + while (( LOG_PBOT_GIT_HOOK && $# )) ; do echo "$1=${!1}" >> ${LOG_FILE} ; shift ; done ; } @@ -56,46 +50,48 @@ LogParseRefDataOut() # (hacker author log_msg n_behind n_ahead n_total repo ref) ParseRefData() # (prev_sha curr_sha ref_file) { - local prev=$1 - local curr=$2 + local ref_data="$@" + local prev_sha=$1 + local curr_sha=$2 local ref_file=$3 local ref=${ref_file##*/} - local repo=$(basename $(pwd)) local is_deletion=$(git show-ref "${ref}" > /dev/null && echo 0 || echo 1) - - LogParseRefDataIn ${ref_data} ${prev_sha} ${curr_sha} ${ref_file} ${repo} ${ref} ${is_deletion} + local log_refdata_args=(REPO ref_data prev_sha curr_sha ref_file ref is_deletion) # validate ref data - [[ "$#" == '3' ]] && \ - [[ "${prev}" =~ ${SHA_REGEX} ]] && \ - [[ "${curr}" =~ ${SHA_REGEX} ]] && \ - [[ "${ref}" ]] || return + if [[ "$#" == '3' ]] && \ + [[ "${prev_sha}" =~ ${SHA_REGEX} ]] && \ + [[ "${curr_sha}" =~ ${SHA_REGEX} ]] && \ + [[ -n "${ref}" ]] + then LogParseRefData ${log_refdata_args[@]} + else return 1 $(LogParseRefData invalid ${log_refdata_args[@]}) + fi # collect push details -# local hacker=$( /usr/lib/parabola-hackers/last-hacker-login ) - local hacker='' - local author="$( git log -1 --format='%an' ${curr} )" - local log_msg="$(git log -1 --format='%s' ${curr} )" - local n_behind=$(git rev-list --count ${curr}..${prev} 2> /dev/null) - local n_ahead=$( git rev-list --count ${prev}..${curr} 2> /dev/null) - local n_total=$( git rev-list --count ${ref} 2> /dev/null) + local hacker=$( ${LIB_DIR}/last-hacker-login ) + local author="$( git log -1 --format='%an' ${curr_sha} )" + local log_msg="$(git log -1 --format='%s' ${curr_sha} )" + local n_behind=$(git rev-list --count ${curr_sha}..${prev_sha} 2> /dev/null) + local n_ahead=$( git rev-list --count ${prev_sha}..${curr_sha} 2> /dev/null) + local n_total=$( git rev-list --count ${ref} 2> /dev/null) - LogParseRefDataOut ${hacker} ${author} ${log_msg} ${n_behind} ${n_ahead} ${n_total} ${repo} ${ref} + LogParseRefData hacker author log_msg n_behind n_ahead n_total ; echo >> ${LOG_FILE} ; - # detect new hacker - [[ "${hacker}" ]] || hacker="${author}" + # fall-back to author name if the hacker detector fails + [[ -n "${author}" ]] || author='someone' # deletion + [[ -n "${hacker}" ]] || hacker="${author}" # broken/buggy last-hacker-login # detect new ref - [[ ${prev} =~ ^0{40}$ ]] && n_behind='∞' n_ahead=${n_total} + [[ ${prev_sha} =~ ^0{40}$ ]] && n_behind='∞' n_ahead=${n_total} # prepare pbot message if (( ${is_deletion} )) - then echo "${hacker} deleted ${repo}/${ref}" - else echo "${hacker} pushed (-${n_behind} +${n_ahead}) commits to ${repo}/${ref}: ${log_msg}" + then echo "${hacker} deleted branch: ${REPO}/${ref}" + else echo "${hacker} pushed (-${n_behind} +${n_ahead}) commits to ${REPO}/${ref}: ${log_msg}" fi } ## main entry ## -while read ref_data ; do pbot-say "$(ParseRefData ${ref_data})" ; done ; +while read ref_data ; do pbot-say "$(ParseRefData ${ref_data})" ; sleep 2 ; done ; |