{"thread":{"id":"20156","subject":"[PATCH] post-receive-email: Add diffattachment and replyto config options","startedAt":"2009-07-19T01:44:57Z","lastAt":"2009-07-19T01:44:57Z","messageCount":1,"participants":["Jim Bauer"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"118256","messageId":"1247967897-14166-1-git-send-email-jfbauer@comcast.net","threadId":"20156","inReplyTo":null,"subject":"[PATCH] post-receive-email: Add diffattachment and replyto config options","fromName":"Jim Bauer","fromEmail":"jfbauer@comcast.net","sentAt":"2009-07-19T01:44:57Z","receivedAt":"2009-07-19T01:44:57Z","isPatch":true,"sender":{"key":"jfbauer@comcast.net","avatar":null},"body":"This is for those who need or want to have the patches\n(from post-receive-email) sent as attachments.\n\nSetting hooks.diffattachment (to anything) will cause a 'git diff'\nto be included in the email as an attachment.  The email will be\nsent via mutt which must be installed.\n\nSetting hooks.replyto will add the Reply-To header to\nthe email messages.\n---\n contrib/hooks/post-receive-email |   72 ++++++++++++++++++++++++++++++++++++-\n 1 files changed, 70 insertions(+), 2 deletions(-)\n\ndiff --git a/contrib/hooks/post-receive-email b/contrib/hooks/post-receive-email\nindex 2a66063..f113969 100755\n--- a/contrib/hooks/post-receive-email\n+++ b/contrib/hooks/post-receive-email\n@@ -48,6 +48,14 @@\n #     \"t=%s; printf 'http://.../?id=%%s' \\$t; echo;echo; git show -C \\$t; echo\"\n #   Be careful if \"...\" contains things that will be expanded by shell \"eval\"\n #   or printf.\n+# hooks.diffattachment\n+#   To include patches as an attachment set this to anything.\n+#   If you set this, you probably don't want to use the \"git show...\"\n+#   example above in hooks.showrev that also caused patches to be included\n+#   inline as that would duplicate the patch in emails.\n+#   This requires mutt to work.  So it must be installed and in $PATH\n+# hooks.replyto\n+#   If set then the Reply-To email header is set to this value.\n #\n # Notes\n # -----\n@@ -173,7 +181,10 @@ generate_email()\n \t\tdescribe=$rev\n \tfi\n \n+\tsubject=\"${emailprefix}$projectdesc $refname_type, $short_refname, ${change_type}d. $describe\"\n+\n \tgenerate_email_header\n+\tgenerate_email_announce\n \n \t# Call the correct body generation function\n \tfn_name=general\n@@ -192,16 +203,31 @@ generate_email()\n \n generate_email_header()\n {\n+\tif [ -n \"$diffattachment\" ]; then\n+\t\treturn;\n+\tfi\n+\n \t# --- Email (all stdout will be the email)\n \t# Generate header\n \tcat <<-EOF\n \tTo: $recipients\n-\tSubject: ${emailprefix}$projectdesc $refname_type, $short_refname, ${change_type}d. $describe\n+\tSubject: $subject\n \tX-Git-Refname: $refname\n \tX-Git-Reftype: $refname_type\n \tX-Git-Oldrev: $oldrev\n \tX-Git-Newrev: $newrev\n+\tEOF\n+\n+\tif [ -n \"$replyto\" ]; then\n+\t\techo \"Reply-To: $replyto\"\n+\tfi\n+\n+\techo \"\"\n+}\n \n+generate_email_announce()\n+{\n+\tcat <<-EOF\n \tThis is an automated email from the git hooks/post-receive script. It was\n \tgenerated because a ref change was pushed to the repository containing\n \tthe project \"$projectdesc\".\n@@ -632,6 +658,12 @@ show_new_revisions()\n \t\t\teval $(printf \"$custom_showrev\" $onerev)\n \t\tdone\n \tfi\n+\n+\t# Put patch in attachment if desired\n+\tif [ -n \"$diffattachment\" ]; then\n+\t\tattachment=$temp_dir/diff.txt\n+\t\tgit diff $revspec > $attachment\n+\tfi\n }\n \n \n@@ -644,12 +676,24 @@ send_mail()\n \tfi\n }\n \n+\n+cleanup()\n+{\n+\trm -rf $temp_dir\n+}\n+\n+\n # ---------------------------- main()\n \n # --- Constants\n LOGBEGIN=\"- Log -----------------------------------------------------------------\"\n LOGEND=\"-----------------------------------------------------------------------\"\n \n+# Create place for temp files and make sure we remove then on exit\n+temp_dir=$(mktemp -d /tmp/post-receive.XXXXXXXXXX)\n+trap cleanup 0 2 15\n+\n+\n # --- Config\n # Set GIT_DIR either from the working directory, or from the environment\n # variable.\n@@ -672,6 +716,9 @@ announcerecipients=$(git config hooks.announcelist)\n envelopesender=$(git config hooks.envelopesender)\n emailprefix=$(git config hooks.emailprefix || echo '[SCM] ')\n custom_showrev=$(git config hooks.showrev)\n+diffattachment=$(git config hooks.diffattach)\n+replyto=$(git config hooks.replyto)\n+\n \n # --- Main loop\n # Allow dual mode: run from the command line just like the update hook, or\n@@ -684,6 +731,27 @@ if [ -n \"$1\" -a -n \"$2\" -a -n \"$3\" ]; then\n else\n \twhile read oldrev newrev refname\n \tdo\n-\t\tgenerate_email $oldrev $newrev $refname | send_mail\n+\t\tif [ -z \"$diffattachment\" ]; then\n+\t\t\tgenerate_email $oldrev $newrev $refname | send_mail\n+\t\telse\n+\t\t\tbody=$temp_dir/body\n+\t\t\tgenerate_email $oldrev $newrev $refname > $body\n+\t\t\tif [ -n \"$attachment\" ]; then\n+\t\t\t\tattach_args=\"-a $attachment\"\n+\t\t\tfi\n+\n+\t\t\tif [ -n \"$replyto\" ]; then\n+\t\t\t\texport REPLYTO=\"$replyto\"\n+\t\t\tfi\n+\n+\t\t\tmutt -f /dev/null -s \"$subject\" \\\n+\t\t\t    -e \"my_hdr X-Git-Refname: $refname\" \\\n+\t\t\t    -e \"my_hdr X-Git-Reftype: $refname_type\" \\\n+\t\t\t    -e \"my_hdr X-Git-Oldrev: $oldrev\" \\\n+\t\t\t    -e \"my_hdr X-Git-Newrev: $newrev\" \\\n+\t\t\t    $attach_args $recipients < $body\n+\t\t\trm -f $body $attachment\n+\t\t\tunset attachment\n+\t\tfi\n \tdone\n fi\n-- \n1.6.3.3\n"}]}