Skip to content

Instantly share code, notes, and snippets.

@goyalmohit
Created April 14, 2019 11:46
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save goyalmohit/fc8d12031ab53009b7d4785cf5d18f34 to your computer and use it in GitHub Desktop.
Save goyalmohit/fc8d12031ab53009b7d4785cf5d18f34 to your computer and use it in GitHub Desktop.
Python script for git post-commit hook
#!/usr/bin/env python
import smtplib
from email.mime.text import MIMEText
from subprocess import check_output
# Get the git log --stat entry of the new commit
log = check_output(['git', 'log', '-1', '--stat', 'HEAD'])
# Create a plaintext email message
msg = MIMEText("Look, I'm actually doing some work:\n\n%s" % log)
msg['Subject'] = 'Git post-commit hook notification'
msg['From'] = 'git@example.com'
msg['To'] = 'dev-team@example.com'
# Send the message
SMTP_SERVER = 'smtp.example.com'
SMTP_PORT = 587
session = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
session.ehlo()
session.starttls()
session.ehlo()
session.login(msg['From'], 'secretPassword')
session.sendmail(msg['From'], msg['To'], msg.as_string())
session.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment