文章目录
Git钩子是在Git仓库中特定事件发生时自动运行的脚本。可以定制一些钩子,这些钩子可以在特定的情况下被执行,分为Client端的钩子和Server端的钩子。Client端钩子被operation触发,比如commit,merge等,Server端钩子被网络动作触发。
$project_name/.git/hooks
-rwxr-xr-x 1 wubaoguo staff 478B 11 6 00:34 applypatch-msg.sample -rwxr-xr-x 1 wubaoguo staff 896B 11 6 00:34 commit-msg.sample -rwxr-xr-x 1 wubaoguo staff 189B 11 6 00:34 post-update.sample -rwxr-xr-x 1 wubaoguo staff 424B 11 6 00:34 pre-applypatch.sample -rwxr-xr-x 1 wubaoguo staff 1.6K 11 6 00:34 pre-commit.sample -rwxr-xr-x 1 wubaoguo staff 1.3K 11 6 00:34 pre-push.sample -rwxr-xr-x 1 wubaoguo staff 4.8K 11 6 00:34 pre-rebase.sample -rwxr-xr-x 1 wubaoguo staff 1.2K 11 6 00:34 prepare-commit-msg.sample -rwxr-xr-x 1 wubaoguo staff 3.5K 11 6 00:34 update.sample
通过post-commit发送commit通知
#!/usr/bin/env python import smtplib from email.mime.text import MIMEText from subprocess import check_output # 获得新提交的git log --stat输出 log = check_output(['git', 'log', '-1', '--stat', 'HEAD']) # 创建一个纯文本的邮件内容 msg = MIMEText("Look, I'm actually doing some work:/n/n%s" % log) msg['Subject'] = 'Git post-commit hook notification' msg['From'] = 'mary@example.com' msg['To'] = 'boss@example.com' # 发送信息 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()
Git钩子:自定义你的工作流