-
Notifications
You must be signed in to change notification settings - Fork 2
/
gitpull.php
executable file
·61 lines (50 loc) · 1.63 KB
/
gitpull.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
#!/usr/bin/php
<?php
/*
* Commands to be run via command line as specified user.
*/
// get script variables
require dirname(__FILE__) . '/config.php';
require dirname(__FILE__) . '/functions.php';
debug(sprintf('gitpull.php called: get_script_user() = %s, is_cli() = %s',
get_script_user(), is_cli()));
// make sure that script is run from command line and as specified repo user
if (get_script_user() == $repo_user && is_cli()) {
// now run git pull for given repo
$output = array();
$return_var = null;
// update repo and any submodules
$cmd = sprintf("cd %s && /usr/bin/git pull && /usr/bin/git submodule sync && /usr/bin/git submodule update --init --recursive", $repo_location);
debug('executing command: ' . $cmd);
exec($cmd, $output, $return_var);
debug('cmd output: ' . implode("\n", $output));
// output command results
echo(implode("\n", $output));
exit($return_var); // exit with command error code
}
// else exit with bad error code
debug('gitpull.php called invalidly');
echo 'Script needs to be run on command line as specified user';
exit(1);
/**
* For some reason php-posix is not installed by default in our server, so just
* run command line command 'whoami' and get result
*/
function get_script_user()
{
return exec('whoami');
}
/**
* Determines if script is run from command line.
* http://www.codediesel.com/php/quick-way-to-determine-if-php-is-running-at-the-command-line/
*
* @return boolean
*/
function is_cli() {
if (php_sapi_name() == 'cli' && empty($_SERVER['REMOTE_ADDR'])) {
return true;
} else {
return false;
}
}
?>