6
6
* @version 0.1 (updated 31-May-2009 @ 06:01 PDT)
7
7
*/
8
8
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
12
14
13
15
define ('EMAIL_FROM ' , 'noreply@example.com ' );
14
16
@@ -85,20 +87,20 @@ function mail_github_post_receive($to, $subj_header, $github_json) {
85
87
$ modified = array_merge ($ modified , $ commit ->{'modified ' });
86
88
}
87
89
} else {
88
- $ ret = "Changed paths: \n" ;
90
+ $ paths = "Changed paths: \n" ;
89
91
if (isset ($ commit ->{'added ' }) && count ($ commit ->{'added ' }) > 0 ) {
90
92
foreach ($ commit ->{'added ' } as $ add )
91
- $ ret .= " A $ add \n" ;
93
+ $ paths .= " A $ add \n" ;
92
94
}
93
95
94
96
if (isset ($ commit ->{'removed ' }) && count ($ commit ->{'removed ' }) > 0 ) {
95
97
foreach ($ commit ->{'removed ' } as $ rem )
96
- $ ret .= " R $ rem \n" ;
98
+ $ paths .= " R $ rem \n" ;
97
99
}
98
100
99
101
if (isset ($ commit ->{'modified ' }) && count ($ commit ->{'modified ' }) > 0 ) {
100
102
foreach ($ commit ->{'modified ' } as $ mod )
101
- $ ret .= " $ mod \n" ;
103
+ $ paths .= " $ mod \n" ;
102
104
}
103
105
}
104
106
@@ -109,18 +111,25 @@ function mail_github_post_receive($to, $subj_header, $github_json) {
109
111
if (!SEND_HTML_EMAIL )
110
112
$ msg = "\n$ msg " ;
111
113
114
+ if (!SHOW_AGGREGATE )
115
+ $ msg = "$ msg \n\n$ paths " ;
116
+
112
117
$ commits .=
113
118
HTML_P . 'Commit: ' . $ id .
114
- HTML_BR . " $ url " .
119
+ HTML_BR . " $ url " .
115
120
HTML_BR . "Author: $ author_name ( " . make_url ($ author_email , $ author_email , true ) . ') ' .
116
121
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 ;
118
127
}
119
128
120
129
// create a list of aggregate additions/deletions/modifications
130
+ $ changes_txt = '' ;
121
131
if (SHOW_AGGREGATE ) {
122
132
$ changes = array ("Additions " =>$ added , "Deletions " =>$ deleted , "Modifications " =>$ modified );
123
- $ changes_txt = '' ;
124
133
foreach ($ changes as $ what => $ what_list ) {
125
134
if (count ($ what_list ) > 0 ) {
126
135
$ changes_txt .= HTML_BR . "$ what: " . HTML_BR ;
@@ -166,23 +175,29 @@ function github_get_diff($repo_owner, $repo, $commit)
166
175
if (SEND_DIFF == false )
167
176
return '' ;
168
177
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
-
175
178
$ ret = '' ;
179
+ if (!USE_LOCAL ) {
180
+ $ json = file_get_contents ("http://github.com/api/v2/json/commits/show/ $ repo_owner/ $ repo/ $ commit " );
176
181
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* ' ;
178
185
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 ();
182
198
}
183
199
}
184
200
185
-
186
201
return $ ret ;
187
202
}
188
203
0 commit comments