post-receive-email 22 KB

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