post-receive-email.sh 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  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. X-Git-Refname: $refname
  186. X-Git-Reftype: $refname_type
  187. X-Git-Oldrev: $oldrev
  188. X-Git-Newrev: $newrev
  189. This is an automated email from the git hooks/post-receive script. It was
  190. generated because a ref change was pushed to the repository containing
  191. the project "$projectdesc".
  192. The $refname_type, $short_refname has been ${change_type}d
  193. EOF
  194. }
  195. generate_email_footer()
  196. {
  197. SPACE=" "
  198. cat <<-EOF
  199. hooks/post-receive
  200. --${SPACE}
  201. $projectdesc
  202. EOF
  203. }
  204. # --------------- Branches
  205. #
  206. # Called for the creation of a branch
  207. #
  208. generate_create_branch_email()
  209. {
  210. # This is a new branch and so oldrev is not valid
  211. echo " at $newrev ($newrev_type)"
  212. echo ""
  213. echo $LOGBEGIN
  214. show_new_revisions
  215. echo $LOGEND
  216. }
  217. #
  218. # Called for the change of a pre-existing branch
  219. #
  220. generate_update_branch_email()
  221. {
  222. # Consider this:
  223. # 1 --- 2 --- O --- X --- 3 --- 4 --- N
  224. #
  225. # O is $oldrev for $refname
  226. # N is $newrev for $refname
  227. # X is a revision pointed to by some other ref, for which we may
  228. # assume that an email has already been generated.
  229. # In this case we want to issue an email containing only revisions
  230. # 3, 4, and N. Given (almost) by
  231. #
  232. # git rev-list N ^O --not --all
  233. #
  234. # The reason for the "almost", is that the "--not --all" will take
  235. # precedence over the "N", and effectively will translate to
  236. #
  237. # git rev-list N ^O ^X ^N
  238. #
  239. # So, we need to build up the list more carefully. git rev-parse
  240. # will generate a list of revs that may be fed into git rev-list.
  241. # We can get it to make the "--not --all" part and then filter out
  242. # the "^N" with:
  243. #
  244. # git rev-parse --not --all | grep -v N
  245. #
  246. # Then, using the --stdin switch to git rev-list we have effectively
  247. # manufactured
  248. #
  249. # git rev-list N ^O ^X
  250. #
  251. # This leaves a problem when someone else updates the repository
  252. # while this script is running. Their new value of the ref we're
  253. # working on would be included in the "--not --all" output; and as
  254. # our $newrev would be an ancestor of that commit, it would exclude
  255. # all of our commits. What we really want is to exclude the current
  256. # value of $refname from the --not list, rather than N itself. So:
  257. #
  258. # git rev-parse --not --all | grep -v $(git rev-parse $refname)
  259. #
  260. # Get's us to something pretty safe (apart from the small time
  261. # between refname being read, and git rev-parse running - for that,
  262. # I give up)
  263. #
  264. #
  265. # Next problem, consider this:
  266. # * --- B --- * --- O ($oldrev)
  267. # \
  268. # * --- X --- * --- N ($newrev)
  269. #
  270. # That is to say, there is no guarantee that oldrev is a strict
  271. # subset of newrev (it would have required a --force, but that's
  272. # allowed). So, we can't simply say rev-list $oldrev..$newrev.
  273. # Instead we find the common base of the two revs and list from
  274. # there.
  275. #
  276. # As above, we need to take into account the presence of X; if
  277. # another branch is already in the repository and points at some of
  278. # the revisions that we are about to output - we don't want them.
  279. # The solution is as before: git rev-parse output filtered.
  280. #
  281. # Finally, tags: 1 --- 2 --- O --- T --- 3 --- 4 --- N
  282. #
  283. # Tags pushed into the repository generate nice shortlog emails that
  284. # summarise the commits between them and the previous tag. However,
  285. # those emails don't include the full commit messages that we output
  286. # for a branch update. Therefore we still want to output revisions
  287. # that have been output on a tag email.
  288. #
  289. # Luckily, git rev-parse includes just the tool. Instead of using
  290. # "--all" we use "--branches"; this has the added benefit that
  291. # "remotes/" will be ignored as well.
  292. # List all of the revisions that were removed by this update, in a
  293. # fast forward update, this list will be empty, because rev-list O
  294. # ^N is empty. For a non fast forward, O ^N is the list of removed
  295. # revisions
  296. fast_forward=""
  297. rev=""
  298. for rev in $(git rev-list $newrev..$oldrev)
  299. do
  300. revtype=$(git cat-file -t "$rev")
  301. echo " discards $rev ($revtype)"
  302. done
  303. if [ -z "$rev" ]; then
  304. fast_forward=1
  305. fi
  306. # List all the revisions from baserev to newrev in a kind of
  307. # "table-of-contents"; note this list can include revisions that
  308. # have already had notification emails and is present to show the
  309. # full detail of the change from rolling back the old revision to
  310. # the base revision and then forward to the new revision
  311. for rev in $(git rev-list $oldrev..$newrev)
  312. do
  313. revtype=$(git cat-file -t "$rev")
  314. echo " via $rev ($revtype)"
  315. done
  316. if [ "$fast_forward" ]; then
  317. echo " from $oldrev ($oldrev_type)"
  318. else
  319. # 1. Existing revisions were removed. In this case newrev
  320. # is a subset of oldrev - this is the reverse of a
  321. # fast-forward, a rewind
  322. # 2. New revisions were added on top of an old revision,
  323. # this is a rewind and addition.
  324. # (1) certainly happened, (2) possibly. When (2) hasn't
  325. # happened, we set a flag to indicate that no log printout
  326. # is required.
  327. echo ""
  328. # Find the common ancestor of the old and new revisions and
  329. # compare it with newrev
  330. baserev=$(git merge-base $oldrev $newrev)
  331. rewind_only=""
  332. if [ "$baserev" = "$newrev" ]; then
  333. echo "This update discarded existing revisions and left the branch pointing at"
  334. echo "a previous point in the repository history."
  335. echo ""
  336. echo " * -- * -- N ($newrev)"
  337. echo " \\"
  338. echo " O -- O -- O ($oldrev)"
  339. echo ""
  340. echo "The removed revisions are not necessarilly gone - if another reference"
  341. echo "still refers to them they will stay in the repository."
  342. rewind_only=1
  343. else
  344. echo "This update added new revisions after undoing existing revisions. That is"
  345. echo "to say, the old revision is not a strict subset of the new revision. This"
  346. echo "situation occurs when you --force push a change and generate a repository"
  347. echo "containing something like this:"
  348. echo ""
  349. echo " * -- * -- B -- O -- O -- O ($oldrev)"
  350. echo " \\"
  351. echo " N -- N -- N ($newrev)"
  352. echo ""
  353. echo "When this happens we assume that you've already had alert emails for all"
  354. echo "of the O revisions, and so we here report only the revisions in the N"
  355. echo "branch from the common base, B."
  356. fi
  357. fi
  358. echo ""
  359. if [ -z "$rewind_only" ]; then
  360. echo "Those revisions listed above that are new to this repository have"
  361. echo "not appeared on any other notification email; so we list those"
  362. echo "revisions in full, below."
  363. echo ""
  364. echo $LOGBEGIN
  365. show_new_revisions
  366. # XXX: Need a way of detecting whether git rev-list actually
  367. # outputted anything, so that we can issue a "no new
  368. # revisions added by this update" message
  369. echo $LOGEND
  370. else
  371. echo "No new revisions were added by this update."
  372. fi
  373. # The diffstat is shown from the old revision to the new revision.
  374. # This is to show the truth of what happened in this change.
  375. # There's no point showing the stat from the base to the new
  376. # revision because the base is effectively a random revision at this
  377. # point - the user will be interested in what this revision changed
  378. # - including the undoing of previous revisions in the case of
  379. # non-fast forward updates.
  380. echo ""
  381. echo "Summary of changes:"
  382. git diff-tree --stat --summary --find-copies-harder $oldrev..$newrev
  383. }
  384. #
  385. # Called for the deletion of a branch
  386. #
  387. generate_delete_branch_email()
  388. {
  389. echo " was $oldrev"
  390. echo ""
  391. echo $LOGEND
  392. git show -s --pretty=oneline $oldrev
  393. echo $LOGEND
  394. }
  395. # --------------- Annotated tags
  396. #
  397. # Called for the creation of an annotated tag
  398. #
  399. generate_create_atag_email()
  400. {
  401. echo " at $newrev ($newrev_type)"
  402. generate_atag_email
  403. }
  404. #
  405. # Called for the update of an annotated tag (this is probably a rare event
  406. # and may not even be allowed)
  407. #
  408. generate_update_atag_email()
  409. {
  410. echo " to $newrev ($newrev_type)"
  411. echo " from $oldrev (which is now obsolete)"
  412. generate_atag_email
  413. }
  414. #
  415. # Called when an annotated tag is created or changed
  416. #
  417. generate_atag_email()
  418. {
  419. # Use git for-each-ref to pull out the individual fields from the
  420. # tag
  421. eval $(git for-each-ref --shell --format='
  422. tagobject=%(*objectname)
  423. tagtype=%(*objecttype)
  424. tagger=%(taggername)
  425. tagged=%(taggerdate)' $refname
  426. )
  427. echo " tagging $tagobject ($tagtype)"
  428. case "$tagtype" in
  429. commit)
  430. # If the tagged object is a commit, then we assume this is a
  431. # release, and so we calculate which tag this tag is
  432. # replacing
  433. prevtag=$(git describe --abbrev=0 $newrev^ 2>/dev/null)
  434. if [ -n "$prevtag" ]; then
  435. echo " replaces $prevtag"
  436. fi
  437. ;;
  438. *)
  439. echo " length $(git cat-file -s $tagobject) bytes"
  440. ;;
  441. esac
  442. echo " tagged by $tagger"
  443. echo " on $tagged"
  444. echo ""
  445. echo $LOGBEGIN
  446. # Show the content of the tag message; this might contain a change
  447. # log or release notes so is worth displaying.
  448. git cat-file tag $newrev | sed -e '1,/^$/d'
  449. echo ""
  450. case "$tagtype" in
  451. commit)
  452. # Only commit tags make sense to have rev-list operations
  453. # performed on them
  454. if [ -n "$prevtag" ]; then
  455. # Show changes since the previous release
  456. git rev-list --pretty=short "$prevtag..$newrev" | git shortlog
  457. else
  458. # No previous tag, show all the changes since time
  459. # began
  460. git rev-list --pretty=short $newrev | git shortlog
  461. fi
  462. ;;
  463. *)
  464. # XXX: Is there anything useful we can do for non-commit
  465. # objects?
  466. ;;
  467. esac
  468. echo $LOGEND
  469. }
  470. #
  471. # Called for the deletion of an annotated tag
  472. #
  473. generate_delete_atag_email()
  474. {
  475. echo " was $oldrev"
  476. echo ""
  477. echo $LOGEND
  478. git show -s --pretty=oneline $oldrev
  479. echo $LOGEND
  480. }
  481. # --------------- General references
  482. #
  483. # Called when any other type of reference is created (most likely a
  484. # non-annotated tag)
  485. #
  486. generate_create_general_email()
  487. {
  488. echo " at $newrev ($newrev_type)"
  489. generate_general_email
  490. }
  491. #
  492. # Called when any other type of reference is updated (most likely a
  493. # non-annotated tag)
  494. #
  495. generate_update_general_email()
  496. {
  497. echo " to $newrev ($newrev_type)"
  498. echo " from $oldrev"
  499. generate_general_email
  500. }
  501. #
  502. # Called for creation or update of any other type of reference
  503. #
  504. generate_general_email()
  505. {
  506. # Unannotated tags are more about marking a point than releasing a
  507. # version; therefore we don't do the shortlog summary that we do for
  508. # annotated tags above - we simply show that the point has been
  509. # marked, and print the log message for the marked point for
  510. # reference purposes
  511. #
  512. # Note this section also catches any other reference type (although
  513. # there aren't any) and deals with them in the same way.
  514. echo ""
  515. if [ "$newrev_type" = "commit" ]; then
  516. echo $LOGBEGIN
  517. git show --no-color --root -s --pretty=medium $newrev
  518. echo $LOGEND
  519. else
  520. # What can we do here? The tag marks an object that is not
  521. # a commit, so there is no log for us to display. It's
  522. # probably not wise to output git cat-file as it could be a
  523. # binary blob. We'll just say how big it is
  524. echo "$newrev is a $newrev_type, and is $(git cat-file -s $newrev) bytes long."
  525. fi
  526. }
  527. #
  528. # Called for the deletion of any other type of reference
  529. #
  530. generate_delete_general_email()
  531. {
  532. echo " was $oldrev"
  533. echo ""
  534. echo $LOGEND
  535. git show -s --pretty=oneline $oldrev
  536. echo $LOGEND
  537. }
  538. # --------------- Miscellaneous utilities
  539. #
  540. # Show new revisions as the user would like to see them in the email.
  541. #
  542. show_new_revisions()
  543. {
  544. # This shows all log entries that are not already covered by
  545. # another ref - i.e. commits that are now accessible from this
  546. # ref that were previously not accessible
  547. # (see generate_update_branch_email for the explanation of this
  548. # command)
  549. # Revision range passed to rev-list differs for new vs. updated
  550. # branches.
  551. if [ "$change_type" = create ]
  552. then
  553. # Show all revisions exclusive to this (new) branch.
  554. revspec=$newrev
  555. else
  556. # Branch update; show revisions not part of $oldrev.
  557. revspec=$oldrev..$newrev
  558. fi
  559. git rev-parse --not --branches | grep -v $(git rev-parse $refname) |
  560. if [ -z "$custom_showrev" ]
  561. then
  562. git rev-list --pretty --stdin $revspec
  563. else
  564. git rev-list --stdin $revspec |
  565. while read onerev
  566. do
  567. eval $(printf "$custom_showrev" $onerev)
  568. done
  569. fi
  570. }
  571. send_mail()
  572. {
  573. if [ -n "$envelopesender" ]; then
  574. /usr/sbin/sendmail -t -f "$envelopesender"
  575. else
  576. /usr/sbin/sendmail -t
  577. fi
  578. }
  579. # ---------------------------- main()
  580. # --- Constants
  581. LOGBEGIN="- Log -----------------------------------------------------------------"
  582. LOGEND="-----------------------------------------------------------------------"
  583. # --- Config
  584. # Set GIT_DIR either from the working directory, or from the environment
  585. # variable.
  586. GIT_DIR=$(git rev-parse --git-dir 2>/dev/null)
  587. if [ -z "$GIT_DIR" ]; then
  588. echo >&2 "fatal: post-receive: GIT_DIR not set"
  589. exit 1
  590. fi
  591. projectdesc=$(sed -ne '1p' "$GIT_DIR/description")
  592. # Check if the description is unchanged from it's default, and shorten it to
  593. # a more manageable length if it is
  594. if expr "$projectdesc" : "Unnamed repository.*$" >/dev/null
  595. then
  596. projectdesc="UNNAMED PROJECT"
  597. fi
  598. recipients=$(git config hooks.mailinglist)
  599. announcerecipients=$(git config hooks.announcelist)
  600. envelopesender=$(git config hooks.envelopesender)
  601. emailprefix=$(git config hooks.emailprefix || echo '[SCM] ')
  602. custom_showrev=$(git config hooks.showrev)
  603. # --- Main loop
  604. # Allow dual mode: run from the command line just like the update hook, or
  605. # if no arguments are given then run as a hook script
  606. if [ -n "$1" -a -n "$2" -a -n "$3" ]; then
  607. # Output to the terminal in command line mode - if someone wanted to
  608. # resend an email; they could redirect the output to sendmail
  609. # themselves
  610. PAGER= generate_email $2 $3 $1
  611. else
  612. while read oldrev newrev refname
  613. do
  614. generate_email $oldrev $newrev $refname | send_mail
  615. done
  616. fi