summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbill-auger <mr.j.spam.me@gmail.com>2019-10-16 23:31:13 -0400
committerbill-auger <mr.j.spam.me@gmail.com>2019-11-27 23:42:52 -0500
commitdee3cfc0df7e5a4fb117b8145d38d220c1b2d594 (patch)
tree1f7b0712b2274324b54db66ff600712f09dcd004
parentf70d267609e168e0e164edbe4df1b0787861bcdf (diff)
refactor notify pbot git hook
-rwxr-xr-xnotify-pbot-git-hook71
1 files changed, 54 insertions, 17 deletions
diff --git a/notify-pbot-git-hook b/notify-pbot-git-hook
index 6cb993d..72ed1b4 100755
--- a/notify-pbot-git-hook
+++ b/notify-pbot-git-hook
@@ -1,19 +1,56 @@
#!/bin/bash
-eval $( sed -En 's|^([0-9a-f]{40}) ([0-9a-f]{40}) .*/([^/]*)$|prev=\1 curr=\2 branch=\3|p')
-author="$( git log -1 --format='%an')"
-log_msg="$(git log -1 --format='%s')"
-n_behind=$(git rev-list $curr..$prev --count)
-n_ahead=$( git rev-list $prev..$curr --count)
-
-if false # DEBUG
-then echo "prev=$prev" > ./notify-pbot.debug
- echo "curr=$curr" >> ./notify-pbot.debug
- echo "branch=$branch" >> ./notify-pbot.debug
- echo "author=$author" >> ./notify-pbot.debug
- echo "log_msg=$log_msg" >> ./notify-pbot.debug
- echo "n_behind=$n_behind" >> ./notify-pbot.debug
- echo "n_ahead=$n_ahead" >> ./notify-pbot.debug
-fi # DEBUG
-
-pbot-say "${author} just pushed (-${n_behind} +${n_ahead}) commits to $(basename $(pwd))/${branch}: ${log_msg}"
+# refs data via STDIN - one line per target ref (where ref_data is: prev_cid curr_cid ref_file)
+# e.g.
+# a559985626bbf4448ccf9041857449078db6d731 1d5002ade3f71200274f1ba39e35a732d86220b5 refs/heads/master
+# 0000000000000000000000000000000000000000 1d5002ade3f71200274f1ba39e35a732d86220b5 refs/heads/new-branch
+readonly CID_REGEX='^([0-9a-f]{40})$'
+
+
+ParseRefData() # (prev curr ref_file)
+{
+ local prev=$1
+ local curr=$2
+ local ref_file=$3
+ local ref=${ref_file##*/}
+ local is_deletion=$(git show-ref "${ref}" > /dev/null && echo 0 || echo 1)
+
+ # validate ref data
+ [[ "$#" == '3' ]] && \
+ [[ "${prev}" =~ ${CID_REGEX} ]] && \
+ [[ "${curr}" =~ ${CID_REGEX} ]] && \
+ [[ "${ref}" ]] || return
+
+ # collect push details
+ 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})
+ local n_ahead=$( git rev-list --count ${prev}..${curr})
+ local n_total=$( git rev-list --count ${ref} )
+ local repo=$( basename $(pwd) )
+
+ # detect new ref
+ [[ ${prev} =~ ^0{40}$ ]] && n_behind='∞' n_ahead=${n_total}
+
+ # prepare pbot message
+ if (( ${is_deletion} ))
+ then echo "${author} deleted ${repo}/${ref}"
+ else echo "${author} pushed (-${n_behind} +${n_ahead}) commits to ${repo}/${ref}: ${log_msg}"
+ fi
+
+
+ # debug
+ local debug=1 ; (( $debug )) && echo "ref_data=${ref_data}" > ./notify-pbot-${ref} && \
+ echo "prev=${prev}" >> ./notify-pbot-${ref} && \
+ echo "curr=${curr}" >> ./notify-pbot-${ref} && \
+ echo "ref=${ref}" >> ./notify-pbot-${ref} && \
+ echo "author=${author}" >> ./notify-pbot-${ref} && \
+ echo "log_msg=${log_msg}" >> ./notify-pbot-${ref} && \
+ echo "n_behind=${n_behind}" >> ./notify-pbot-${ref} && \
+ echo "n_ahead=${n_ahead}" >> ./notify-pbot-${ref} && \
+ echo "n_total=${n_total}" >> ./notify-pbot-${ref} && \
+ echo "repo=${repo}" >> ./notify-pbot-${ref}
+}
+
+
+while read ref_data ; do pbot-say "$(ParseRefData ${ref_data})" ; done ;