2

I'm using the post-receive-email script included with git. (Source is here.) It works just fine, but I want each email to be sent from the author of the commits pushed. How do I do it?

My post-receive file currently looks like this, and I want to customize the from-email-address.

#!/bin/sh

export USER_EMAIL=from-email-address@blah.com
$(dirname $0)/post-receive-email
Jonathan Tran
  • 15,214
  • 9
  • 60
  • 67

3 Answers3

8

Use git log to pull out the email address.

For example, in post-receive:

#!/bin/sh

# Use the email address of the author of the last commit.
export USER_EMAIL=$(git log -1 --format=format:%ae HEAD)
$(dirname $0)/post-receive-email

You could also map the email addresses, if for example, people are using their gmail or personal domain addresses, but you would like to map them to a single domain.

#!/bin/sh

# Use the mapped email address (specified in .mailmap) of the author of the last commit.
export USER_EMAIL=$(git log -1 --format=format:%aE HEAD)
$(dirname $0)/post-receive-email

You can read more about .mailmap here.

Jonathan Tran
  • 15,214
  • 9
  • 60
  • 67
  • 1
    and it doesn't work for branches. If someone commits in a branch, the auther will always be the last one from the master. – 2ni Apr 27 '11 at 01:29
  • You're right, I do have the branch problem you mention. Do you know how to fix it? It's never bothered me enough to look into it. – Jonathan Tran Apr 27 '11 at 20:25
  • Instead of getting the last log entry, you could use: git log GIT_COMMIT_HASH --pretty=format:%ae | head -1 – justsee Jul 21 '12 at 15:05
  • You may use `git log -all` instead of `git log HEAD` to make it work for all branches too (see my longer response) – Olivier Berger Feb 25 '14 at 15:58
1

The following may be better, to correctly handle commits made on branches (used on a Debian system) :

#! /bin/sh
git config hooks.envelopesender $(git log -all -1 --pretty=format:%ae)
. /usr/share/git-core/contrib/hooks/post-receive-email

The git log --all instead of git log HEAD will use the latest commit on all branches, supposedly the right one to notify about.

The git config hooks.envelopesender may be replaced by another of the variants mentioned above. YMMV.

Olivier Berger
  • 1,949
  • 1
  • 11
  • 7
1

You can try another hook system like http://github.com/jtek/git-hook-update-notify-email

shingara
  • 46,608
  • 11
  • 99
  • 105