contrib / hooks / multimail / post-receive.exampleon commit Merge branch 'jk/proto-v2-hidden-refs-fix' (d3b0178)
   1#! /usr/bin/env python
   2
   3"""Example post-receive hook based on git-multimail.
   4
   5The simplest way to use git-multimail is to use the script
   6git_multimail.py directly as a post-receive hook, and to configure it
   7using Git's configuration files and command-line parameters.  You can
   8also write your own Python wrapper for more advanced configurability,
   9using git_multimail.py as a Python module.
  10
  11This script is a simple example of such a post-receive hook.  It is
  12intended to be customized before use; see the comments in the script
  13to help you get started.
  14
  15Using git-multimail as a Python module as done here provides more
  16flexibility.  It has the following advantages:
  17
  18* The tool's behavior can be customized using arbitrary Python code,
  19  without having to edit git_multimail.py.
  20
  21* Configuration settings can be read from other sources; for example,
  22  user names and email addresses could be read from LDAP or from a
  23  database.  Or the settings can even be hardcoded in the importing
  24  Python script, if this is preferred.
  25
  26This script is a very basic example of how to use git_multimail.py as
  27a module.  The comments below explain some of the points at which the
  28script's behavior could be changed or customized.
  29
  30"""
  31
  32import sys
  33
  34# If necessary, add the path to the directory containing
  35# git_multimail.py to the Python path as follows.  (This is not
  36# necessary if git_multimail.py is in the same directory as this
  37# script):
  38
  39#LIBDIR = 'path/to/directory/containing/module'
  40#sys.path.insert(0, LIBDIR)
  41
  42import git_multimail
  43
  44# It is possible to modify the output templates here; e.g.:
  45
  46#git_multimail.FOOTER_TEMPLATE = """\
  47#
  48#-- \n\
  49#This email was generated by the wonderful git-multimail tool.
  50#"""
  51
  52
  53# Specify which "git config" section contains the configuration for
  54# git-multimail:
  55config = git_multimail.Config('multimailhook')
  56
  57# Set some Git configuration variables. Equivalent to passing var=val
  58# to "git -c var=val" each time git is called, or to adding the
  59# configuration in .git/config (must come before instanciating the
  60# environment) :
  61#git_multimail.Config.add_config_parameters('multimailhook.commitEmailFormat=html')
  62#git_multimail.Config.add_config_parameters(('user.name=foo', 'user.email=foo@example.com'))
  63
  64# Select the type of environment:
  65try:
  66    environment = git_multimail.GenericEnvironment(config=config)
  67    #environment = git_multimail.GitoliteEnvironment(config=config)
  68except git_multimail.ConfigurationException:
  69    sys.stderr.write('*** %s\n' % sys.exc_info()[1])
  70    sys.exit(1)
  71
  72
  73# Choose the method of sending emails based on the git config:
  74mailer = git_multimail.choose_mailer(config, environment)
  75
  76# Alternatively, you may hardcode the mailer using code like one of
  77# the following:
  78
  79# Use "/usr/sbin/sendmail -oi -t" to send emails.  The envelopesender
  80# argument is optional:
  81#mailer = git_multimail.SendMailer(
  82#    command=['/usr/sbin/sendmail', '-oi', '-t'],
  83#    envelopesender='git-repo@example.com',
  84#    )
  85
  86# Use Python's smtplib to send emails.  Both arguments are required.
  87#mailer = git_multimail.SMTPMailer(
  88#    environment=environment,
  89#    envelopesender='git-repo@example.com',
  90#    # The smtpserver argument can also include a port number; e.g.,
  91#    #     smtpserver='mail.example.com:25'
  92#    smtpserver='mail.example.com',
  93#    )
  94
  95# OutputMailer is intended only for testing; it writes the emails to
  96# the specified file stream.
  97#mailer = git_multimail.OutputMailer(sys.stdout)
  98
  99
 100# Read changes from stdin and send notification emails:
 101git_multimail.run_as_post_receive_hook(environment, mailer)