takafumi blog

日々の勉強メモ

Git入門 その11 / git push時にメールを送信する

環境   git version 1.8.3.1

git push時にメールを送信する。

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

にあるファイルに何をどうすればいいか書いてあるので、これを見て設定すれば一通りOK.

大体以下のような作業内容。

実行権限は最初からある場合もあるが、大元のスクリプトに実行権限を付与。

$ chmod 755 /usr/share/git-core/contrib/hooks/post-receive-email

自分のリポジトリに設定ファイルをリンク

$ cd /path/to/myproject.git
$ ln -s /usr/share/git-core/contrib/hooks/post-receive-email ./hooks/post-receive

あとは送信先などをgit configで設定する。

# メール送信する宛て先を設定。スペース区切りで複数可
$ git config --global --add hooks.mailinglist 'root@localhost.localdomain'

# 送信元アドレス設定
$ git config --global --add hooks.envelopesender 'myproject@hostname.server'

# SubjectのPrefix
$ git config --global --add hooks.emailprefix '[MyProject]'

# メールにcommit差分(diff)を追加
$ git config --global hooks.showrev "git show -C %s; echo"


これで以下のようなメールが送信される

From myproject@hostname.server  Sun Mar 15 18:02:03 2015
To: root@localhost.localdomain
Subject: [MyProject]UNNAMED PROJECT branch master updated. ed864b8180e0c6c41a50679e300e630878a82bf2
From: myproject@hostname.server (root)

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 "UNNAMED PROJECT".

The branch, master has been updated
       via  ed864b8180e0c6c41a50679e300e630878a82bf2 (commit)
      from  cd8a39a05c3c833b532fad1599910539c65c8170 (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 ed864b8180e0c6c41a50679e300e630878a82bf2
Author: Me <me@example.com>
Date:   Sun Mar 15 18:01:58 2015 +0900

    Email Test

diff --git a/hoge.txt b/hoge.txt
new file mode 100644
index 0000000..9128c3e
--- /dev/null
+++ b/hoge.txt
@@ -0,0 +1 @@
+fuga

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

Summary of changes:
 hoge.txt | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 hoge.txt


hooks/post-receive
--
UNNAMED PROJECT


「UNNAMED PROJECT」は

/path/to/myproject.git/description

にプロジェクト名を追加すれば、その名前が表示される。


takafumi-s.hatenablog.com