TEXT   13
git post commit hook
Guest on 15th March 2023 02:20:18 PM


  1. #! /bin/zsh -f
  2.  
  3. # Details of the most recent commit
  4. commit_full_hash=$(git log -1 --pretty=format:%H)
  5. commit_hash=$(git log -1 --pretty=format:%h)
  6. commit_author_name=$(git log -1 --pretty=format:%an)
  7. commit_author_email=$(git log -1 --pretty=format:%ae)
  8. commit_date=$(git log -1 --pretty=format:%cD)
  9. commit_subject=$(git log -1 --pretty=format:%s)
  10. commit_changes=$(git log -1 --pretty=format:%b)
  11.  
  12. escaped_message=$(git log -1 --pretty=format:%b | \
  13.    sed -e 's,&,&amp;,g' -e 's,<,&lt;,g' -e 's,>,&gt;,g')
  14.  
  15. # Details about this branch:
  16. refname=$(git symbolic-ref HEAD 2>/dev/null)
  17. refname=${refname##refs/heads/}
  18. merged=$(git rev-parse HEAD)
  19. rev=$(git describe ${merged} 2>/dev/null)
  20.  
  21.  
  22.  
  23. sendmail_wrapper()
  24. {
  25.     if [ $# -ne 3 ] ; then
  26.         echo 1>&2 "sendmail usage error: need 3 arguments"
  27.         exit 1
  28.     fi
  29.     if [ "$1" != "-s" ] ; then
  30.         echo 1>&2 "sendmail usage error: first argument must be -s"
  31.         exit 1
  32.     fi
  33.     (
  34.         cat <<EOF
  35. To: $3
  36. Message-ID: <${rev}@${commit_full_hash}>
  37. BCC: srivasta@debian.org
  38. Subject: $2
  39. X-PTS-Approved: Yes
  40.  
  41. EOF
  42.         cat
  43.     )  | /usr/sbin/sendmail -oi -t
  44. }
  45. # replace the call to  /usr/sbin/sendmail -oi -t by: tee /tmp/junk
  46.  
  47. echo "$commit_changes" | perl -e '
  48. my %Seen;
  49. {
  50.   local $/; # enable localized slurp mode
  51.   my $string=<>;
  52.   while ( $string =~ m/closes:\s*(?:bug)?\#\s*\d+(?:,\s*(?:bug)?\#\s*\d+)*/gsmi ) {
  53.     my $match="$&";
  54.     while ($match =~ /(\d+)/g) {
  55.       $Seen{$1}++;
  56.     }
  57.   }
  58. }
  59. for (sort keys %Seen) { print "$_\n"; }
  60. ' | while read bug; do
  61.         echo |         \
  62.             sendmail_wrapper -s "[$commit_hash] Fix for Bug#$bug committed to git" \
  63.             $bug@bugs.debian.org,control@bugs.debian.org<<EOF
  64. tags $bug +pending
  65. thanks
  66. Hi,
  67.  
  68.      The following change has been committed for this bug by
  69.  $commit_author_name <$commit_author_email> on $commit_date.
  70.  The fix will be in the next upload.
  71. =========================================================================
  72. $commit_subject
  73.  
  74. $commit_changes
  75. =========================================================================
  76.  
  77. EOF
  78.  
  79. done

Raw Paste

Login or Register to edit or fork this paste. It's free.