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