1#!/bin/sh 2# 3# Copyright (c) 2007 Andy Parkins 4# 5# An example hook script to mail out commit update information. This hook 6# sends emails listing new revisions to the repository introduced by the 7# change being reported. The rule is that (for branch updates) each commit 8# will appear on one email and one email only. 9# 10# This hook is stored in the contrib/hooks directory. Your distribution 11# will have put this somewhere standard. You should make this script 12# executable then link to it in the repository you would like to use it in. 13# For example, on debian the hook is stored in 14# /usr/share/doc/git-core/contrib/hooks/post-receive-email: 15# 16# chmod a+x post-receive-email 17# cd /path/to/your/repository.git 18# ln -sf /usr/share/doc/git-core/contrib/hooks/post-receive-email hooks/post-receive 19# 20# This hook script assumes it is enabled on the central repository of a 21# project, with all users pushing only to it and not between each other. It 22# will still work if you don't operate in that style, but it would become 23# possible for the email to be from someone other than the person doing the 24# push. 25# 26# To help with debugging and use on pre-v1.5.1 git servers, this script will 27# also obey the interface of hooks/update, taking its arguments on the 28# command line. Unfortunately, hooks/update is called once for each ref. 29# To avoid firing one email per ref, this script just prints its output to 30# the screen when used in this mode. The output can then be redirected if 31# wanted. 32# 33# Config 34# ------ 35# hooks.mailinglist 36# This is the list that all pushes will go to; leave it blank to not send 37# emails for every ref update. 38# hooks.announcelist 39# This is the list that all pushes of annotated tags will go to. Leave it 40# blank to default to the mailinglist field. The announce emails lists 41# the short log summary of the changes since the last annotated tag. 42# hooks.envelopesender 43# If set then the -f option is passed to sendmail to allow the envelope 44# sender address to be set 45# hooks.emailprefix 46# All emails have their subjects prefixed with this prefix, or "[SCM]" 47# if emailprefix is unset, to aid filtering 48# hooks.showrev 49# The shell command used to format each revision in the email, with 50# "%s" replaced with the commit id. Defaults to "git rev-list -1 51# --pretty %s", displaying the commit id, author, date and log 52# message. To list full patches separated by a blank line, you 53# could set this to "git show -C %s; echo". 54# To list a gitweb/cgit URL *and* a full patch for each change set, use this: 55# "t=%s; printf 'http://.../?id=%%s' \$t; echo;echo; git show -C \$t; echo" 56# Be careful if "..." contains things that will be expanded by shell "eval" 57# or printf. 58# hooks.emailmaxlines 59# The maximum number of lines that should be included in the generated 60# email body. If not specified, there is no limit. 61# Lines beyond the limit are suppressed and counted, and a final 62# line is added indicating the number of suppressed lines. 63# hooks.diffopts 64# Alternate options for the git diff-tree invocation that shows changes. 65# Default is "--stat --summary --find-copies-harder". Add -p to those 66# options to include a unified diff of changes in addition to the usual 67# summary output. 68# 69# Notes 70# ----- 71# All emails include the headers "X-Git-Refname", "X-Git-Oldrev", 72# "X-Git-Newrev", and "X-Git-Reftype" to enable fine tuned filtering and 73# give information for debugging. 74# 75 76# ---------------------------- Functions 77 78# 79# Function to prepare for email generation. This decides what type 80# of update this is and whether an email should even be generated. 81# 82prep_for_email() 83{ 84# --- Arguments 85 oldrev=$(git rev-parse $1) 86 newrev=$(git rev-parse $2) 87 refname="$3" 88 maxlines=$4 89 90# --- Interpret 91# 0000->1234 (create) 92# 1234->2345 (update) 93# 2345->0000 (delete) 94ifexpr"$oldrev":'0*$'>/dev/null 95then 96 change_type="create" 97else 98ifexpr"$newrev":'0*$'>/dev/null 99then 100 change_type="delete" 101else 102 change_type="update" 103fi 104fi 105 106# --- Get the revision types 107 newrev_type=$(git cat-file -t $newrev 2> /dev/null) 108 oldrev_type=$(git cat-file -t "$oldrev" 2> /dev/null) 109case"$change_type"in 110 create|update) 111rev="$newrev" 112 rev_type="$newrev_type" 113;; 114 delete) 115rev="$oldrev" 116 rev_type="$oldrev_type" 117;; 118esac 119 120# The revision type tells us what type the commit is, combined with 121# the location of the ref we can decide between 122# - working branch 123# - tracking branch 124# - unannoted tag 125# - annotated tag 126case"$refname","$rev_type"in 127 refs/tags/*,commit) 128# un-annotated tag 129 refname_type="tag" 130 short_refname=${refname##refs/tags/} 131;; 132 refs/tags/*,tag) 133# annotated tag 134 refname_type="annotated tag" 135 short_refname=${refname##refs/tags/} 136# change recipients 137if[-n"$announcerecipients"];then 138 recipients="$announcerecipients" 139fi 140;; 141 refs/heads/*,commit) 142# branch 143 refname_type="branch" 144 short_refname=${refname##refs/heads/} 145;; 146 refs/remotes/*,commit) 147# tracking branch 148 refname_type="tracking branch" 149 short_refname=${refname##refs/remotes/} 150echo>&2"*** Push-update of tracking branch,$refname" 151echo>&2"*** - no email generated." 152return1 153;; 154*) 155# Anything else (is there anything else?) 156echo>&2"*** Unknown type of update to$refname($rev_type)" 157echo>&2"*** - no email generated" 158return1 159;; 160esac 161 162# Check if we've got anyone to send to 163if[-z"$recipients"];then 164case"$refname_type"in 165"annotated tag") 166 config_name="hooks.announcelist" 167;; 168*) 169 config_name="hooks.mailinglist" 170;; 171esac 172echo>&2"***$config_nameis not set so no email will be sent" 173echo>&2"*** for$refnameupdate$oldrev->$newrev" 174return1 175fi 176 177return0 178} 179 180# 181# Top level email generation function. This calls the appropriate 182# body-generation routine after outputting the common header. 183# 184# Note this function doesn't actually generate any email output, that is 185# taken care of by the functions it calls: 186# - generate_email_header 187# - generate_create_XXXX_email 188# - generate_update_XXXX_email 189# - generate_delete_XXXX_email 190# - generate_email_footer 191# 192# Note also that this function cannot 'exit' from the script; when this 193# function is running (in hook script mode), the send_mail() function 194# is already executing in another process, connected via a pipe, and 195# if this function exits without, whatever has been generated to that 196# point will be sent as an email... even if nothing has been generated. 197# 198generate_email() 199{ 200# Email parameters 201# The email subject will contain the best description of the ref 202# that we can build from the parameters 203 describe=$(git describe $rev 2>/dev/null) 204if[-z"$describe"];then 205 describe=$rev 206fi 207 208 generate_email_header 209 210# Call the correct body generation function 211 fn_name=general 212case"$refname_type"in 213"tracking branch"|branch) 214 fn_name=branch 215;; 216"annotated tag") 217 fn_name=atag 218;; 219esac 220 221if[-z"$maxlines"];then 222 generate_${change_type}_${fn_name}_email 223else 224 generate_${change_type}_${fn_name}_email | limit_lines $maxlines 225fi 226 227 generate_email_footer 228} 229 230generate_email_header() 231{ 232# --- Email (all stdout will be the email) 233# Generate header 234cat<<-EOF 235 To:$recipients 236 Subject:${emailprefix}$projectdesc$refname_type$short_refname${change_type}d.$describe 237 X-Git-Refname:$refname 238 X-Git-Reftype:$refname_type 239 X-Git-Oldrev:$oldrev 240 X-Git-Newrev:$newrev 241 242 This is an automated email from the git hooks/post-receive script. It was 243 generated because a ref change was pushed to the repository containing 244 the project "$projectdesc". 245 246 The$refname_type,$short_refnamehas been${change_type}d 247 EOF 248} 249 250generate_email_footer() 251{ 252 SPACE=" " 253cat<<-EOF 254 255 256 hooks/post-receive 257 --${SPACE} 258$projectdesc 259 EOF 260} 261 262# --------------- Branches 263 264# 265# Called for the creation of a branch 266# 267generate_create_branch_email() 268{ 269# This is a new branch and so oldrev is not valid 270echo" at$newrev($newrev_type)" 271echo"" 272 273echo$LOGBEGIN 274 show_new_revisions 275echo$LOGEND 276} 277 278# 279# Called for the change of a pre-existing branch 280# 281generate_update_branch_email() 282{ 283# Consider this: 284# 1 --- 2 --- O --- X --- 3 --- 4 --- N 285# 286# O is $oldrev for $refname 287# N is $newrev for $refname 288# X is a revision pointed to by some other ref, for which we may 289# assume that an email has already been generated. 290# In this case we want to issue an email containing only revisions 291# 3, 4, and N. Given (almost) by 292# 293# git rev-list N ^O --not --all 294# 295# The reason for the "almost", is that the "--not --all" will take 296# precedence over the "N", and effectively will translate to 297# 298# git rev-list N ^O ^X ^N 299# 300# So, we need to build up the list more carefully. git rev-parse 301# will generate a list of revs that may be fed into git rev-list. 302# We can get it to make the "--not --all" part and then filter out 303# the "^N" with: 304# 305# git rev-parse --not --all | grep -v N 306# 307# Then, using the --stdin switch to git rev-list we have effectively 308# manufactured 309# 310# git rev-list N ^O ^X 311# 312# This leaves a problem when someone else updates the repository 313# while this script is running. Their new value of the ref we're 314# working on would be included in the "--not --all" output; and as 315# our $newrev would be an ancestor of that commit, it would exclude 316# all of our commits. What we really want is to exclude the current 317# value of $refname from the --not list, rather than N itself. So: 318# 319# git rev-parse --not --all | grep -v $(git rev-parse $refname) 320# 321# Get's us to something pretty safe (apart from the small time 322# between refname being read, and git rev-parse running - for that, 323# I give up) 324# 325# 326# Next problem, consider this: 327# * --- B --- * --- O ($oldrev) 328# \ 329# * --- X --- * --- N ($newrev) 330# 331# That is to say, there is no guarantee that oldrev is a strict 332# subset of newrev (it would have required a --force, but that's 333# allowed). So, we can't simply say rev-list $oldrev..$newrev. 334# Instead we find the common base of the two revs and list from 335# there. 336# 337# As above, we need to take into account the presence of X; if 338# another branch is already in the repository and points at some of 339# the revisions that we are about to output - we don't want them. 340# The solution is as before: git rev-parse output filtered. 341# 342# Finally, tags: 1 --- 2 --- O --- T --- 3 --- 4 --- N 343# 344# Tags pushed into the repository generate nice shortlog emails that 345# summarise the commits between them and the previous tag. However, 346# those emails don't include the full commit messages that we output 347# for a branch update. Therefore we still want to output revisions 348# that have been output on a tag email. 349# 350# Luckily, git rev-parse includes just the tool. Instead of using 351# "--all" we use "--branches"; this has the added benefit that 352# "remotes/" will be ignored as well. 353 354# List all of the revisions that were removed by this update, in a 355# fast-forward update, this list will be empty, because rev-list O 356# ^N is empty. For a non-fast-forward, O ^N is the list of removed 357# revisions 358 fast_forward="" 359rev="" 360forrevin$(git rev-list $newrev..$oldrev) 361do 362 revtype=$(git cat-file -t "$rev") 363echo" discards$rev($revtype)" 364done 365if[-z"$rev"];then 366 fast_forward=1 367fi 368 369# List all the revisions from baserev to newrev in a kind of 370# "table-of-contents"; note this list can include revisions that 371# have already had notification emails and is present to show the 372# full detail of the change from rolling back the old revision to 373# the base revision and then forward to the new revision 374forrevin$(git rev-list $oldrev..$newrev) 375do 376 revtype=$(git cat-file -t "$rev") 377echo" via$rev($revtype)" 378done 379 380if["$fast_forward"];then 381echo" from$oldrev($oldrev_type)" 382else 383# 1. Existing revisions were removed. In this case newrev 384# is a subset of oldrev - this is the reverse of a 385# fast-forward, a rewind 386# 2. New revisions were added on top of an old revision, 387# this is a rewind and addition. 388 389# (1) certainly happened, (2) possibly. When (2) hasn't 390# happened, we set a flag to indicate that no log printout 391# is required. 392 393echo"" 394 395# Find the common ancestor of the old and new revisions and 396# compare it with newrev 397 baserev=$(git merge-base $oldrev $newrev) 398 rewind_only="" 399if["$baserev"="$newrev"];then 400echo"This update discarded existing revisions and left the branch pointing at" 401echo"a previous point in the repository history." 402echo"" 403echo" * -- * -- N ($newrev)" 404echo"\\" 405echo" O -- O -- O ($oldrev)" 406echo"" 407echo"The removed revisions are not necessarilly gone - if another reference" 408echo"still refers to them they will stay in the repository." 409 rewind_only=1 410else 411echo"This update added new revisions after undoing existing revisions. That is" 412echo"to say, the old revision is not a strict subset of the new revision. This" 413echo"situation occurs when you --force push a change and generate a repository" 414echo"containing something like this:" 415echo"" 416echo" * -- * -- B -- O -- O -- O ($oldrev)" 417echo"\\" 418echo" N -- N -- N ($newrev)" 419echo"" 420echo"When this happens we assume that you've already had alert emails for all" 421echo"of the O revisions, and so we here report only the revisions in the N" 422echo"branch from the common base, B." 423fi 424fi 425 426echo"" 427if[-z"$rewind_only"];then 428echo"Those revisions listed above that are new to this repository have" 429echo"not appeared on any other notification email; so we list those" 430echo"revisions in full, below." 431 432echo"" 433echo$LOGBEGIN 434 show_new_revisions 435 436# XXX: Need a way of detecting whether git rev-list actually 437# outputted anything, so that we can issue a "no new 438# revisions added by this update" message 439 440echo$LOGEND 441else 442echo"No new revisions were added by this update." 443fi 444 445# The diffstat is shown from the old revision to the new revision. 446# This is to show the truth of what happened in this change. 447# There's no point showing the stat from the base to the new 448# revision because the base is effectively a random revision at this 449# point - the user will be interested in what this revision changed 450# - including the undoing of previous revisions in the case of 451# non-fast-forward updates. 452echo"" 453echo"Summary of changes:" 454 git diff-tree$diffopts $oldrev..$newrev 455} 456 457# 458# Called for the deletion of a branch 459# 460generate_delete_branch_email() 461{ 462echo" was$oldrev" 463echo"" 464echo$LOGEND 465 git show -s --pretty=oneline $oldrev 466echo$LOGEND 467} 468 469# --------------- Annotated tags 470 471# 472# Called for the creation of an annotated tag 473# 474generate_create_atag_email() 475{ 476echo" at$newrev($newrev_type)" 477 478 generate_atag_email 479} 480 481# 482# Called for the update of an annotated tag (this is probably a rare event 483# and may not even be allowed) 484# 485generate_update_atag_email() 486{ 487echo" to$newrev($newrev_type)" 488echo" from$oldrev(which is now obsolete)" 489 490 generate_atag_email 491} 492 493# 494# Called when an annotated tag is created or changed 495# 496generate_atag_email() 497{ 498# Use git for-each-ref to pull out the individual fields from the 499# tag 500eval $(git for-each-ref --shell --format=' 501 tagobject=%(*objectname) 502 tagtype=%(*objecttype) 503 tagger=%(taggername) 504 tagged=%(taggerdate)'$refname 505) 506 507echo" tagging$tagobject($tagtype)" 508case"$tagtype"in 509 commit) 510 511# If the tagged object is a commit, then we assume this is a 512# release, and so we calculate which tag this tag is 513# replacing 514 prevtag=$(git describe --abbrev=0 $newrev^ 2>/dev/null) 515 516if[-n"$prevtag"];then 517echo" replaces$prevtag" 518fi 519;; 520*) 521echo" length$(git cat-file -s $tagobject)bytes" 522;; 523esac 524echo" tagged by$tagger" 525echo" on$tagged" 526 527echo"" 528echo$LOGBEGIN 529 530# Show the content of the tag message; this might contain a change 531# log or release notes so is worth displaying. 532 git cat-file tag $newrev|sed-e'1,/^$/d' 533 534echo"" 535case"$tagtype"in 536 commit) 537# Only commit tags make sense to have rev-list operations 538# performed on them 539if[-n"$prevtag"];then 540# Show changes since the previous release 541 git rev-list --pretty=short "$prevtag..$newrev"| git shortlog 542else 543# No previous tag, show all the changes since time 544# began 545 git rev-list --pretty=short $newrev| git shortlog 546fi 547;; 548*) 549# XXX: Is there anything useful we can do for non-commit 550# objects? 551;; 552esac 553 554echo$LOGEND 555} 556 557# 558# Called for the deletion of an annotated tag 559# 560generate_delete_atag_email() 561{ 562echo" was$oldrev" 563echo"" 564echo$LOGEND 565 git show -s --pretty=oneline $oldrev 566echo$LOGEND 567} 568 569# --------------- General references 570 571# 572# Called when any other type of reference is created (most likely a 573# non-annotated tag) 574# 575generate_create_general_email() 576{ 577echo" at$newrev($newrev_type)" 578 579 generate_general_email 580} 581 582# 583# Called when any other type of reference is updated (most likely a 584# non-annotated tag) 585# 586generate_update_general_email() 587{ 588echo" to$newrev($newrev_type)" 589echo" from$oldrev" 590 591 generate_general_email 592} 593 594# 595# Called for creation or update of any other type of reference 596# 597generate_general_email() 598{ 599# Unannotated tags are more about marking a point than releasing a 600# version; therefore we don't do the shortlog summary that we do for 601# annotated tags above - we simply show that the point has been 602# marked, and print the log message for the marked point for 603# reference purposes 604# 605# Note this section also catches any other reference type (although 606# there aren't any) and deals with them in the same way. 607 608echo"" 609if["$newrev_type"="commit"];then 610echo$LOGBEGIN 611 git show --no-color --root -s --pretty=medium $newrev 612echo$LOGEND 613else 614# What can we do here? The tag marks an object that is not 615# a commit, so there is no log for us to display. It's 616# probably not wise to output git cat-file as it could be a 617# binary blob. We'll just say how big it is 618echo"$newrevis a$newrev_type, and is$(git cat-file -s $newrev)bytes long." 619fi 620} 621 622# 623# Called for the deletion of any other type of reference 624# 625generate_delete_general_email() 626{ 627echo" was$oldrev" 628echo"" 629echo$LOGEND 630 git show -s --pretty=oneline $oldrev 631echo$LOGEND 632} 633 634 635# --------------- Miscellaneous utilities 636 637# 638# Show new revisions as the user would like to see them in the email. 639# 640show_new_revisions() 641{ 642# This shows all log entries that are not already covered by 643# another ref - i.e. commits that are now accessible from this 644# ref that were previously not accessible 645# (see generate_update_branch_email for the explanation of this 646# command) 647 648# Revision range passed to rev-list differs for new vs. updated 649# branches. 650if["$change_type"= create ] 651then 652# Show all revisions exclusive to this (new) branch. 653 revspec=$newrev 654else 655# Branch update; show revisions not part of $oldrev. 656 revspec=$oldrev..$newrev 657fi 658 659 other_branches=$(git for-each-ref --format='%(refname)' refs/heads/ | 660 grep -F -v$refname) 661 git rev-parse --not$other_branches| 662 if [ -z "$custom_showrev" ] 663 then 664 git rev-list --pretty --stdin$revspec 665 else 666 git rev-list --stdin$revspec| 667 while read onerev 668 do 669 eval$(printf "$custom_showrev" $onerev) 670 done 671 fi 672} 673 674 675limit_lines() 676{ 677 lines=0 678 skipped=0 679 while IFS="" read -r line; do 680 lines=$((lines + 1)) 681 if [$lines-gt$1]; then 682 skipped=$((skipped + 1)) 683 else 684 printf "%s\n" "$line" 685 fi 686 done 687 if [$skipped-ne 0 ]; then 688 echo "...$skippedlines suppressed ..." 689 fi 690} 691 692 693send_mail() 694{ 695 if [ -n "$envelopesender" ]; then 696 /usr/sbin/sendmail -t -f "$envelopesender" 697 else 698 /usr/sbin/sendmail -t 699 fi 700} 701 702# ---------------------------- main() 703 704# --- Constants 705LOGBEGIN="- Log -----------------------------------------------------------------" 706LOGEND="-----------------------------------------------------------------------" 707 708# --- Config 709# Set GIT_DIR either from the working directory, or from the environment 710# variable. 711GIT_DIR=$(git rev-parse --git-dir 2>/dev/null) 712if [ -z "$GIT_DIR" ]; then 713 echo >&2 "fatal: post-receive: GIT_DIR not set" 714 exit 1 715fi 716 717projectdesc=$(sed -ne '1p' "$GIT_DIR/description" 2>/dev/null) 718# Check if the description is unchanged from it's default, and shorten it to 719# a more manageable length if it is 720ifexpr"$projectdesc":"Unnamed repository.*$">/dev/null 721then 722 projectdesc="UNNAMED PROJECT" 723fi 724 725recipients=$(git config hooks.mailinglist) 726announcerecipients=$(git config hooks.announcelist) 727envelopesender=$(git config hooks.envelopesender) 728emailprefix=$(git config hooks.emailprefix || echo '[SCM] ') 729custom_showrev=$(git config hooks.showrev) 730maxlines=$(git config hooks.emailmaxlines) 731diffopts=$(git config hooks.diffopts) 732:${diffopts:="--stat --summary --find-copies-harder"} 733 734# --- Main loop 735# Allow dual mode: run from the command line just like the update hook, or 736# if no arguments are given then run as a hook script 737if[-n"$1"-a -n"$2"-a -n"$3"];then 738# Output to the terminal in command line mode - if someone wanted to 739# resend an email; they could redirect the output to sendmail 740# themselves 741 prep_for_email $2 $3 $1&& PAGER= generate_email 742else 743whileread oldrev newrev refname 744do 745 prep_for_email $oldrev $newrev $refname||continue 746 generate_email $maxlines| send_mail 747done 748fi