Skip to content
This repository was archived by the owner on Dec 13, 2021. It is now read-only.

Commit 1c01a6e

Browse files
committed
Fix the not working stuff + add local clone option for diff
1 parent 789262f commit 1c01a6e

File tree

1 file changed

+36
-21
lines changed

1 file changed

+36
-21
lines changed

github_post_receive.php

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
* @version 0.1 (updated 31-May-2009 @ 06:01 PDT)
77
*/
88

9-
define('SEND_HTML_EMAIL', false);
10-
define('SEND_DIFF', true);
11-
define('SHOW_AGGREGATE', false); // For the moment, this should be !SEND_DIFF
9+
define('SEND_HTML_EMAIL', false); // HTML or plaintext?
10+
define('SEND_DIFF', true); // Add a diff of the changes?
11+
define('SHOW_AGGREGATE', false); // Aggregate the files changed list at the end of the email
12+
// (not recommended if send_diff set)
13+
define('USE_LOCAL', false); // Use a local git repo instead of the github API
1214

1315
define('EMAIL_FROM', 'noreply@example.com');
1416

@@ -85,20 +87,20 @@ function mail_github_post_receive($to, $subj_header, $github_json) {
8587
$modified = array_merge($modified, $commit->{'modified'});
8688
}
8789
} else {
88-
$ret = "Changed paths:\n";
90+
$paths = "Changed paths:\n";
8991
if (isset($commit->{'added'}) && count($commit->{'added'}) > 0) {
9092
foreach($commit->{'added'} as $add)
91-
$ret .= " A $add\n";
93+
$paths .= " A $add\n";
9294
}
9395

9496
if (isset($commit->{'removed'}) && count($commit->{'removed'}) > 0) {
9597
foreach($commit->{'removed'} as $rem)
96-
$ret .= " R $rem\n";
98+
$paths .= " R $rem\n";
9799
}
98100

99101
if (isset($commit->{'modified'}) && count($commit->{'modified'}) > 0) {
100102
foreach($commit->{'modified'} as $mod)
101-
$ret .= " $mod\n";
103+
$paths .= " $mod\n";
102104
}
103105
}
104106

@@ -109,18 +111,25 @@ function mail_github_post_receive($to, $subj_header, $github_json) {
109111
if(!SEND_HTML_EMAIL)
110112
$msg = "\n$msg";
111113

114+
if(!SHOW_AGGREGATE)
115+
$msg = "$msg\n\n$paths";
116+
112117
$commits .=
113118
HTML_P . 'Commit: ' . $id .
114-
HTML_BR . " $url" .
119+
HTML_BR . " $url" .
115120
HTML_BR . "Author: $author_name (" . make_url($author_email, $author_email, true) . ')' .
116121
HTML_BR . "Date: $date" .
117-
HTML_BLOCKQUOTE . str_replace("\n", HTML_BR, $msg . "\n\n" . github_get_diff($repo_owner, $repo, $id)) . HTML_BLOCKQUOTE_END . HTML_P_END;
122+
HTML_BLOCKQUOTE .
123+
str_replace("\n", HTML_BR, $msg . "\n\n" .
124+
(SEND_HTML_EMAIL ?
125+
htmlentities(github_get_diff($repo_owner, $repo, $id)) :
126+
github_get_diff($repo_owner, $repo, $id))) . HTML_BLOCKQUOTE_END . HTML_P_END;
118127
}
119128

120129
// create a list of aggregate additions/deletions/modifications
130+
$changes_txt = '';
121131
if(SHOW_AGGREGATE) {
122132
$changes = array("Additions"=>$added, "Deletions"=>$deleted, "Modifications"=>$modified);
123-
$changes_txt = '';
124133
foreach($changes as $what => $what_list) {
125134
if(count($what_list) > 0) {
126135
$changes_txt .= HTML_BR . "$what:" . HTML_BR;
@@ -166,23 +175,29 @@ function github_get_diff($repo_owner, $repo, $commit)
166175
if (SEND_DIFF == false)
167176
return '';
168177

169-
$json = file_get_contents("http://github.com/api/v2/json/commits/show/$repo_owner/$repo/$commit");
170-
171-
$json = json_decode($json);
172-
if ($json == null)
173-
return '*bad json when retrieving commit diff*';
174-
175178
$ret = '';
179+
if (!USE_LOCAL) {
180+
$json = file_get_contents("http://github.com/api/v2/json/commits/show/$repo_owner/$repo/$commit");
176181

177-
if (isset($json->{'commit'}->{'modified'}) && count($json->{'commit'}->{'modified'}) > 0) {
182+
$json = json_decode($json);
183+
if ($json == null)
184+
return '*bad json when retrieving commit diff*';
178185

179-
foreach($json->{'commit'}->{'modified'} as $mod) {
180-
$ret .= "Modified: " . $mod->{'filename'} . "\n" .
181-
"===================================================================\n" . $mod->{'diff'} . "\n\n";
186+
if (isset($json->{'commit'}->{'modified'}) && count($json->{'commit'}->{'modified'}) > 0) {
187+
foreach($json->{'commit'}->{'modified'} as $mod) {
188+
$ret .= "Modified: " . $mod->{'filename'} . "\n" .
189+
"===================================================================\n" . $mod->{'diff'} . "\n\n";
190+
}
191+
}
192+
} else {
193+
if (preg_match('/^[0-9a-f]+$/', $commit)) {
194+
ob_start();
195+
passthru('./diff_local.sh ' . escapeshellarg($commit));
196+
$ret = ob_get_contents();
197+
ob_end_clean();
182198
}
183199
}
184200

185-
186201
return $ret;
187202
}
188203

0 commit comments

Comments
 (0)