tkuchikiの日記

新ブログ https://blog.tkuchiki.net

git push したら commit log を mail で送信する

自前で git をホスティングしているリポジトリに対して push したときに、
commit log を mail で送信する方法です。

/path/to/repo/.git/hooks 以下に、所謂コミットフックを行うためのファイルがあります。

そのなかに、post-receive.sample というファイルがあるので、

cd /path/to/repo/.git/hooks
cp post-receive.sample post-receive

して、

##### post-receive #####
- #. /usr/share/git-core/contrib/hooks/post-receive-email
+ . /usr/share/git-core/contrib/hooks/post-receive-email

と変更します(コメントアウトを外す)。

utf-8 を使っていると日本語が文字化けするので、
/usr/share/git-core/contrib/hooks/post-receive-email を編集します。

generate_email_header()
{
   # --- Email (all stdout will be the email)
   # Generate header
   cat <<-EOF
   To: $recipients
   Subject: ${emailprefix}$projectdesc $refname_type, $short_refname, ${change_type}d. $describe
+   MIME-Version: 1.0
+   Content-Type: text/plain; charset="utf-8"
   X-Git-Refname: $refname
   X-Git-Reftype: $refname_type
   X-Git-Oldrev: $oldrev
   X-Git-Newrev: $newrev

   This is an automated email from the git hooks/post-receive script. It was
   generated because a ref change was pushed to the repository containing
   the project "$projectdesc".

   The $refname_type, $short_refname has been ${change_type}d
EOF
}

プロジェクトごとに設定を変えたい場合は、
上記の関数を post-receive-email を 読み込んだあとに再定義して上書きします。

##### post-receive #####

. /usr/share/git-core/contrib/hooks/post-receive-email

generate_email_header()
{
   # --- Email (all stdout will be the email)
   # Generate header
   cat <<-EOF
   To: $recipients
   Subject: ${emailprefix}$projectdesc $refname_type, $short_refname, ${change_type}d. $describe
   MIME-Version: 1.0
   Content-Type: text/plain; charset="utf-8"
   X-Git-Refname: $refname
   X-Git-Reftype: $refname_type
   X-Git-Oldrev: $oldrev
   X-Git-Newrev: $newrev

   This is an automated email from the git hooks/post-receive script. It was
   generated because a ref change was pushed to the repository containing
   the project "$projectdesc".

   The $refname_type, $short_refname has been ${change_type}d
EOF
}

コミットフックの設定ができたら、
メールの設定を行います。

以下を実行します。

$ git config hooks.mailinglist "user@example.com"
$ git config hooks.announcelist  "user@example.com"
$ git config hooks.envelopesender "git@example.com" 
$ git config hooks.emailprefix "[GIT]"

それぞれを説明すると、

hooks.mailinglist push 時の送信先メールアドレス
hooks.announcelist git tag 作成時の送信先メールアドレス
hooks.envelopesender 送信元メールアドレス
hooks.emailprefix メールの件名の先頭につく文字列(default は [SCM])

となります。
hooks.showrev という変数もありますが、こちらは省略します。

最後に、/path/to/repo/.git/description に任意の文字列を書きます。
ここに書いた文字列が、

Subject: ${emailprefix}$projectdesc $refname_type, $short_refname, ${change_type}d. $describe

などで使われている、$projectdesc です。
設定しなくてもメールは送られますが、

remote: sed: ./descriptionを読み込めません: そのようなファイルやディレクトリはありません

といったエラーがでます。

あとは、実際に push してみると、
以下の様なメールが送信されます。

Subject : [GIT] example branch, master, updated. 96017817633eb694ae50be29bad4fe88a325cba3

This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "example".

The branch, master has been updated
       via  96017817633eb694ae50be29bad4fe88a325cba3 (commit)
      from  512a9e60d99ad4768faceb566f2a52d16a023d3f (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
commit 96017817633eb694ae50be29bad4fe88a325cba3
Author: xxx <user@example.com>
Date:   Fri May 23 15:20:27 2014 +0900

    post-receive-email のテスト

-----------------------------------------------------------------------

Summary of changes:


hooks/post-receive

以上で設定は完了です。

Gitポケットリファレンス

Gitポケットリファレンス

を参考に設定しました。
本記事の内容は、上記書籍とほとんど同じですが、
リポジトリごとに generate_email_header を設定する方法は本記事用に検証しました。
また、メールを送るのに必要な環境は整えておく必要がありますので、
適宜設定してください。

Github で同様の設定を行う場合は、
Notifications · GitHub Help が参考になると思います。