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