Skip to content

Instantly share code, notes, and snippets.

@nishimura
Created March 1, 2010 09:18
Show Gist options
  • Save nishimura/318229 to your computer and use it in GitHub Desktop.
Save nishimura/318229 to your computer and use it in GitHub Desktop.
post-commit-email for Japanese Encoding.
<?php
$from = `git config hooks.envelopesender`;
$to = `git config hooks.mailinglist`;
$emailprefix = `git config hooks.emailprefix`;
if (!$emailprefix)
$emailprefix = '[SCM]';
if (!isset($argc) || $argc < 3){
$stdin = file_get_contents('php://stdin');
sscanf($stdin, '%s %s %s', $oldrev, $newrev, $refname);
}else{
$oldrev = $argv[2];
$newrev = $argv[3];
$refname = $argv[1];
}
if (preg_match('/^0+$/', $oldrev))
$mode = 'create';
else if (preg_match('/^0+$/', $newrev))
$mode = 'delete';
else
$mode = 'update';
ini_set('mbstring.language', 'Japanese');
ini_set('mbstring.internal_encoding', 'UTF-8');
date_default_timezone_set('Asia/Tokyo');
$param = `git config hooks.envelopesender`;
$headers = array(
'Content-Type' => 'Content-Type: text/plain; charset=ISO-2022-JP',
'Content-Transfer-Encoding' => 'Content-Transfer-Encoding: 7bit'
);
$param = "-f$from";
if ($mode === 'create'){
if ($refname === 'refs/heads/master'){
$revspec = "$newrev";
}else{
$oldrev = trim(`git merge-base master $newrev`);
$revspec = "$oldrev..$newrev";
}
}elseif ($mode === 'delete'){
$revspec = $oldrev;
}else{
$revspec = "$oldrev..$newrev";
}
$data = `git rev-list --pretty $revspec`;
$line = explode("\n", $data);
if ($mode === 'delete'){
$title = "$refname deleted.";
}else{
foreach ($line as $k => $v){
if (strlen(trim($v)) === 0)
break;
}
if (isset($line[$k+1]))
$title = trim($line[$k+1]);
else
$title = $mode;
}
$title = $emailprefix . ' ' . $title;
if (preg_match('/Author: ([^<]+)(.*)/', $data, $matches)){
$headers[] = 'From: '
. mb_encode_mimeheader(trim($matches[1]), 'ISO-2022-JP')
. $matches[2];
}
$headers = implode("\n", $headers);
if ($mode === 'delete')
$message = "$refname deleted.\n";
else
$message = "$refname\n$data\n----\n" . `git diff-tree --stat $revspec`
. "\n\n" . `git diff-tree --binary $revspec`;
mb_send_mail($to, $title, $message, $headers, $param);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment