-
Notifications
You must be signed in to change notification settings - Fork 0
/
sync.php
executable file
·510 lines (436 loc) · 15.4 KB
/
sync.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
<?php
/*
* Free to use, copy and modify.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
?>
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title>git sync tool</title>
</head>
<body style="background-color: #404040; color: #FFFFFF;">
<?php
putenv("PATH=" .$_ENV["PATH"]. ':/usr/local/bin');
////////////////////////////////
// Init variables //
// Log file name.
$logfn = '/sync_log_' . str_replace( '.', '', (string)microtime( true ) );
error_reporting( E_ALL );
ini_set( 'error_log', dirname( __FILE__ ) . $logfn . '_errors.log' );
ini_set( 'display_errors', 'On' );
ini_set( 'log_errors_max_len', 0 );
ini_set( 'log_errors', 'On' );
set_time_limit(3600); // 1 hour
// Read configuration file
$config = json_decode(file_get_contents("config.json"));
$hadErrors = false;
$branch = null;
$project = null;
// Payload data from github
if ( !empty( $_POST['payload'] ) ) {
// php < 5.4 retardness
if ( function_exists( 'get_magic_quotes_gpc' ) && get_magic_quotes_gpc() ) {
$_POST['payload'] = stripslashes( $_POST['payload'] );
}
$push = json_decode($_POST['payload']);
$project = $push->repository->name;
$refPath = explode("/", $push->ref);
$branch = $refPath[count($refPath) - 1];
}
// some defaults
if ( !property_exists( $config, 'logs' ) ) {
$config->logs = true;
}
if ( !property_exists( $config, 'debug' ) ) {
$config->debug = false;
}
if ( !property_exists( $config, 'supportEmail' ) ) {
$config->supportEmail = null;
}
if ( !property_exists( $config, 'supportEmailFrom' ) ) {
$config->supportEmailFrom = null;
}
if ( !property_exists( $config, 'commandOnFinish' ) ) {
$config->commandOnFinish = null;
}
if ( !property_exists( $config, 'urlOnFinish' ) ) {
$config->urlOnFinish = null;
}
register_shutdown_function( '_atexit' );
_output( '<div style="box-sizing: border-box; padding: 15px; width: 100%; height: 100%; background-color: #404040; color: #FFFFFF;">' );
//Project
if ( !empty( $_GET['project'] ) ) {
$project = $_GET['project'];
}
if ( empty( $project ) ) {
_output( 'No project specified.' );
exit( 1 );
}
//Branch
if ( !empty( $_GET['branch'] ) ) {
$branch = $_GET['branch'];
}
if ( empty( $branch ) ) {
_output( 'No branch specified.' );
exit( 1 );
}
//Check for given project in configuration
if ( $project != '*' && !property_exists( $config->projects, $project ) ) {
_output( 'Unknown project ' . $project . '.' );
exit( 1 );
//Check for given branch in configuration
if ( $branch != '*' &&
!property_exists( $config->projects->$project->branches, $branch ) &&
!property_exists( $config->projects, '*' )
) {
_output( 'Unknown branch ' . $branch . '.' );
exit( 1 );
}
}
$clean = array_key_exists( 'clean', $_GET ) ? $_GET['clean'] : null;
$forcesync = array_key_exists( 'forcesync', $_GET ) ? $_GET['forcesync'] : null;
$noemail = array_key_exists( 'noemail', $_GET ) ? $_GET['noemail'] : null;
$noonfinish = array_key_exists( 'noonfinish', $_GET ) ? $_GET['noonfinish'] : null;
////////////////////////////////
// Display useful information //
_output( 'Running GitHub synchronization...<br/>' );
_output( '<br/>Project: '.$project );
_output( '<br/>Branch: '.$branch );
if ( $config->logs !== false ) {
_output( '<br/>Logs: ...' . $logfn . '...<br/>' );
}
// Check max execution time
//_output( 'Info - PHP max execution time: '.ini_get('max_execution_time').'sec<br/>' );
// Check user name and access to github
_executeCommand('Running the script as user', 'whoami');
if(_executeCommand('Testing ssh access to github', 'ssh -T [email protected]') == 255) {
// Host key verification failed.
_emailSupport($config->supportEmail);
exit( 1 );
}
////////////////////////////////////////////
// Process synchronization of the project //
foreach ( $config->projects as $projectName => $projectConfig ) {
$finishedSomethingPrj = false;
if ( $project != '*' && $project != $projectName ) {
continue;
}
_output( '<br/><br/>####<br/>#### Processing project: <b>'.$projectName.'</b><br/>####<br/>' );
if (
property_exists( $projectConfig, 'autosync' ) &&
$projectConfig->autosync === false &&
!$forcesync ) {
_output( '<br/>Autosync is disabled for this projects in the configuration file. Skipping.' );
continue;
}
if ( !property_exists( $projectConfig, 'supportEmail' ) ) {
$projectConfig->supportEmail = $config->supportEmail;
}
if ( !property_exists( $projectConfig, 'commandOnFinish' ) ) {
$projectConfig->commandOnFinish = null;
}
if ( !property_exists( $projectConfig, 'urlOnFinish' ) ) {
$projectConfig->urlOnFinish = null;
}
$finishedSomething = false;
foreach ( $projectConfig->branches as $branchName => $branchConfig ) {
// If there is a given branch we will update only it.
// If there isn't a given branch we will update all branches
if ( $branch != '*' && $branch != $branchName && $branchName != '*' ) {
continue;
}
if ( !property_exists( $branchConfig, 'autosync' ) ) {
$branchConfig->autosync = true;
}
if ( !property_exists( $branchConfig, 'bare' ) ) {
$branchConfig->bare = false;
}
if ( !property_exists( $branchConfig, 'deep' ) ) {
$branchConfig->deep = false;
}
if ( !property_exists( $branchConfig, 'syncSubmodules' ) ) {
$branchConfig->syncSubmodules = true;
}
if ( !property_exists( $branchConfig, 'commandOnFinish' ) ) {
$branchConfig->commandOnFinish = null;
}
if ( !property_exists( $branchConfig, 'urlOnFinish' ) ) {
$branchConfig->urlOnFinish = null;
}
if ( !property_exists( $branchConfig, 'supportEmail' ) ) {
$branchConfig->supportEmail = null;
}
_output( '<br/><br/>### Processing branch: <b>'.$branchName.'</b><br/>' );
// Check autosync option
if(!$branchConfig->autosync && !$forcesync) {
// Autosync is disabled.
_output( '<br/>Autosync is disabled for this branch in the configuration file. Skipping.' );
continue;
}
$projectExistsLocaly = file_exists($branchConfig->local);
// Check config to full clean local direcotry.
if($clean && is_dir( $branchConfig->local )) {
if(_executeCommand('Deleting project directory: '.$branchConfig->local, 'rm -rf '.$branchConfig->local)) {
// Error (permission)
_emailSupport($projectConfig->supportEmail);
break;
}
$projectExistsLocaly = false;
}
if(!$projectExistsLocaly) {
// recursive option to clone submodules
$cloneRecursive = $branchConfig->syncSubmodules ? ' --recursive ' : '';
// options
$bare = $branchConfig->bare ? ' --bare ' : '';
$branch = $branchName != '*' ? ' --branch '.$branchName.' ' : '';
$deep = $branchConfig->deep ? '' : ' --depth 1 ';
//Clone only latest version of the given branch
$command = 'git clone '.$deep.$branch.$cloneRecursive.$bare.' '.$projectConfig->remote.' '.$branchConfig->local;
$returnCode = _executeCommand("Local doesn't exist. Will clone remote.", $command, $config->retryOnErrorCount);
if($returnCode) {
// Error, stop execution
_emailSupport($projectConfig->supportEmail);
break;
}
}
// Change directory to project location
if(!chdir($branchConfig->local)) {
_output( "<br/>Error: cant change directory to ".$branchConfig->local );
// Error, stop execution
_emailSupport($projectConfig->supportEmail);
break;
}
// Sync sorce tree
if($projectExistsLocaly) {
// Reset. This will reset changed files to the last commit
if ( !$branchConfig->bare ) {
//saving uncommitted stuff
$command = 'git stash';
$returncode = _executeCommand('Stashing.', $command);
if($returnCode) {
// Error, stop execution
_emailSupport($projectConfig->supportEmail);
break;
}
$command = 'git reset --hard';
$returnCode = _executeCommand('Reseting.', $command);
if($returnCode) {
// Error, stop execution
_emailSupport($projectConfig->supportEmail);
break;
}
$command = 'git submodule foreach --recursive git reset --hard';
$returnCode = _executeCommand('Reseting submodules.', $command);
if($returnCode) {
// Error, stop execution
_emailSupport($projectConfig->supportEmail);
break;
}
}
// Pull
$branchcmd = $branchName != '*' ? 'origin '.$branchName : '"+refs/heads/*:refs/heads/*"';
$pull = $branchConfig->bare ? 'fetch' : 'pull -s recursive -X theirs';
$command = 'git '.$pull.' '.$branchcmd;
$returnCode = _executeCommand('Pulling', $command, $config->retryOnErrorCount, '_cleanUntrackedStuff', $branchConfig );
if($returnCode) {
// Error, stop execution
_emailSupport($projectConfig->supportEmail);
break;
}
if ( $branchConfig->syncSubmodules && !$branchConfig->bare ) {
//Submodules
$command = 'git submodule update --init --recursive';
$returnCode = _executeCommand('Update submodules', $command, $config->retryOnErrorCount);
if($returnCode) {
// Error, stop execution
_emailSupport($projectConfig->supportEmail);
break;
}
}
}
_postFinish( $branchConfig, $branchConfig, $projectConfig );
$finishedSomething = true;
$finishedSomethingPrj = true;
}
if ( $finishedSomethingPrj ) {
_postFinish( $projectConfig, $projectConfig );
}
}
if ( $finishedSomething ) {
_postFinish( $config, $config );
}
////////////////////////////////
// Used functions //
function _postFinish ( $config, $emailConfig, $emailConfig2 = null ) {
global $noonfinish;
if ( $noonfinish ) {
return;
}
$gconfig = $GLOBALS['config'];
$email = null;
if ( $emailConfig instanceof Object && !empty( $emailConfig->supportEmail ) ) {
$email = $emailConfig->supportEmail;
}
else if ( $emailConfig2 instanceof Object && !empty( $emailConfig2->supportEmail ) ) {
$email = $emailConfig2->supportEmail;
}
else if ( $gconfig instanceof Object && !empty( $gconfig->supportEmail ) ) {
$email = $gconfig->supportEmail;
}
if ( $config->commandOnFinish ) {
$cmds = is_array( $config->commandOnFinish ) ? $config->commandOnFinish : array( $config->commandOnFinish );
foreach ( $cmds as $cmd ) {
$returnCode = _executeCommand( 'Executing command on finish.', $cmd );
if ( $returnCode ) {
// Error executing given command
_emailSupport( $emailConfig->supportEmail );
exit( 1 );
}
}
}
if ( $config->urlOnFinish ) {
$urls = is_array( $config->urlOnFinish ) ? $config->urlOnFinish : array( $config->urlOnFinish );
foreach ( $urls as $url ) {
_output( '<br/> Loading url on finish: ' . $url );
if ( file_get_contents( $url ) === false ) {
// Error loading given url
_output( '<br/> Error on loading the url.' );
_emailSupport( $emailConfig->supportEmail );
exit( 1 );
}
}
}
}
/**
* Writes given string to the output.
*/
function _output ( $str, $last = false ) {
global $output, $log;
$output .= $str;
echo $str;
if ( !$last ) {
echo '<script type="text/javascript">window.scrollTo( 0, document.body.offsetHeight );</script>';
}
flush();
}
/**
* This function executes at the end of php script.
* Writes end html tags and logs to file if enabled in configuration.
*/
function _atexit () {
_output( '<br/><br/>##########' );
_output( '</div></body></html>', true );
_saveLogs();
}
function _saveLogs () {
global $output, $config, $logfn, $hadErrors;
if ( empty( $config->logs ) || $config->logs === false ) {
return;
}
// don't save logs if we don't have errors and we didn't request it explicitly
if ( $hadErrors === false && $config->debug !== true ) {
return;
}
if ( $config->logs === true ) {
$config->logs = dirname( __FILE__ );
}
if ( is_string( $config->logs ) ) {
$config->logs = realpath( $config->logs );
if ( is_dir( $config->logs ) ) {
$fn = $config->logs . $logfn;
@file_put_contents( $fn . '.txt', strip_tags( str_replace( ' ', ' ', str_replace( '<br/>', "\n", $output ) ) ) );
if ( !empty( $_POST['payload'] ) ) {
@file_put_contents( $fn . '_payload.json', $_POST['payload'] );
}
}
}
}
/**
* Executes given command and writes it to to output.
*/
function _executeCommand($description, $command, $retryOnErrorCount = 0, $customRetry = null, $customArg = null ) {
global $config;
// Execute command. Append 2>&1 to show errors.
exec($command.' 2>&1', $result, $returnCode);
// Output
_output( '<br/>' );
_output( '<span style="font-size: smaller; color: #888888;">' . @date( 'c' ) . '</span><br/>' );
if($description) {
_output( $description.'<br/>' );
}
_output(
'<div style="font-family: monospace; width: 100%; box-sizing: border-box; overflow-x: auto;"><span style="color: #6BE234;">$</span> <span style="color: #729FCF;">'.$command.'</span><br/>' .
'<pre style="padding-left: 15px;">' . implode('<br/> ', $result) . '</pre></div><br/>'
);
//_output( ' resultCode: '.$returnCode.'<br/>' );
// Check for error
if ( $returnCode ) {
if ( is_callable( $customRetry ) ) {
$returnCode = $customRetry( $command, implode( "\n", $result ), $customArg );
}
// Sleep some time.
if ( $returnCode && $retryOnErrorCount ) {
sleep(5);
// Retry
return _executeCommand('', $command, --$retryOnErrorCount);
}
}
return $returnCode;
}
function _cleanUntrackedStuff ( $command, $result, $branchConfig ) {
$ret = preg_match( "/error: The following untracked working tree files would be overwritten by merge:\n([\s\S]*)\nPlease move or remove them before you can merge\./m", $result, $matches );
if ( $ret === 1 ) {
$torm = array();
foreach ( explode( "\n", $matches[1] ) as $fn ) {
$fn = trim( $fn );
if ( !empty( $fn ) ) {
$torm[] = escapeshellarg( $branchConfig->local . '/' . $fn );
}
}
if ( !empty( $torm ) ) {
$torm = 'rm -rf ' . implode( ' ', $torm );
if ( !_executeCommand( 'Cleaning conflicting untracked files.', $torm ) ) {
return _executeCommand( 'Retrying pull.', $command );
}
}
}
return 1;
}
/**
* Sends text/html email.
*/
function _emailSupport ( $toEmails, $subject = "GitHub synchronization failed" ) {
global $output, $config, $hadErrors, $noemail;
$hadErrors = true;
if ( $noemail || empty( $toEmails ) ) {
_output( '<br/>An error occured. ' );
return;
}
if ( !is_array( $toEmails ) ) {
$toEmails = array( $toEmails );
}
_output( '<br/>Send email to support: ' . implode( ', ', $toEmails ) );
$from = $config->supportEmailFrom;
$headers = "From: $from\r\n";
$headers .= "Content-type: text/html\r\n";
foreach ( $toEmails as $toEmail ) {
if ( empty( $from ) ) {
$from = $toEmail;
}
mail( $toEmail, $subject, $output, $headers );
}
}
?>