-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpull.php
61 lines (58 loc) · 1.8 KB
/
pull.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
<?php
ignore_user_abort(true);
function syscall ($cmd, $cwd) {
$descriptorspec = array(
1 => array('pipe', 'w'), // stdout is a pipe that the child will write to
2 => array('pipe', 'w') // stderr
);
$resource = proc_open($cmd, $descriptorspec, $pipes, $cwd);
if (is_resource($resource)) {
$output = stream_get_contents($pipes[2]);
$output .= PHP_EOL;
$output .= stream_get_contents($pipes[1]);
$output .= PHP_EOL;
fclose($pipes[1]);
fclose($pipes[2]);
proc_close($resource);
return $output;
}
}
function git_current_branch ($cwd) {
$result = syscall('git branch', $cwd);
if (preg_match('/\\* (.*)/', $result, $matches)) {
return $matches[1];
}
}
$data = file_get_contents("php://input");
// Use in the "Post-Receive URLs" section of your GitHub repo.
if ( $data ) {
$payload = json_decode($data);
$branch = substr($payload->ref, strrpos($payload->ref, '/') + 1);
echo syscall( 'whoami && cd /var/www/kiosk/ && git reset --hard HEAD && git stash && git pull',$cwd );
if ($branch == git_current_branch($cwd)) {
// pull from $branch
$cmd = sprintf('git pull origin %s', $branch);
$result = syscall($cmd, $cwd);
$output = '';
// append commits
foreach ($payload->commits as $commit) {
$output .= $commit->author->name.' a.k.a. '.$commit->author->username;
$output .= PHP_EOL;
foreach (array('added', 'modified', 'removed') as $action) {
if (count($commit->{$action})) {
$output .= sprintf('%s: %s; ', $action, implode(',', $commit->{$action}));
}
}
$output .= PHP_EOL;
$output .= sprintf('because: %s', $commit->message);
$output .= PHP_EOL;
$output .= $commit->url;
$output .= PHP_EOL;
}
$output .= PHP_EOL;
$output .= $result;
echo $output;
mail('[email protected]', 'GitHub hook `'.$cmd.'` result', $output);
}
}
?>