I'm using Git and PHP to create an update script which is sync local branch and the master. It checks about new updates, and if yes, it proceeds to the Git pull request and any response got from the sync may send to the server via CURL. What kind of area should I improve for this script and how do I modify it to work with OSX?
<?php
$getGit = new stdClass();
$getGit->lastLocalCommitDate = array();
$getGit->lastLocalCommitId = array();
$getGit->lastLocalCommitMessage = array();
$getGit->lastRemoteCommitId = array();
$getGit->pullOutput = array();
include_once dirname(__FILE__) . '/../Modal/config.php';
#change the directory
exec("cd ~/system");
#get the date from last commit from git
exec("git log -1 --format=%cd", $getGit->lastLocalCommitDate);
$getDateObject = new DateTime($getGit->lastLocalCommitDate[0]);
#get the last commit id from git
exec("git rev-parse HEAD", $getGit->lastLocalCommitId);
#get the last commit message from git
exec("git log -1 --pretty=%B", $getGit->lastLocalCommitMessage);
#get the last commit id from remote branch
exec("git ls-remote [email protected]:xxx/xxx.git | head -1 | sed 's/HEAD//'", $getGit->lastRemoteCommitId);
#check if the branch is uptodate
if ($getGit->lastLocalCommitId[0] == $getGit->lastRemoteCommitId[0]) {
$getGit->pullOutput = array(
'System is upto date'
);
} else {
#Trying to get the pull requested from the branch/master
exec("git pull", $getGit->pullOutput);
}
#clear all stdClass object arrays if not all output getting append to the array
unset($getGit->lastLocalCommitDate);
unset($getGit->lastLocalCommitId);
unset($getGit->lastLocalCommitMessage);
unset($getGit->lastRemoteCommitId);
#get the date from last commit from git
exec("git log -1 --format=%cd", $getGit->lastLocalCommitDate);
$getDateObject = new DateTime($getGit->lastLocalCommitDate[0]);
#get the last commit id from git
exec("git rev-parse HEAD", $getGit->lastLocalCommitId);
#get the last commit message from git
exec("git log -1 --pretty=%B", $getGit->lastLocalCommitMessage);
#curlClass
class curl
{
private $ch;
public $serverReturn;
public $url;
public $data;
public function __construct($url, $data)
{
$this->url = $url;
$this->data = $data;
}
public function sendCurl()
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($this->data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$serverReturn = json_decode(curl_exec($ch), true);
curl_close($ch);
return $serverReturn;
}
}
#post pull requested update though curl
$curlObject = new curl('https://xxx.com/xxx/xxx.php', array(
'branch' => DATABASE,
'commitId' => $getGit->lastLocalCommitId[0],
'commitM' => $getGit->lastLocalCommitMessage[0],
'lastUpdateDate' => $getDateObject->format('Y-m-d H:i:s'),
'response' => implode(',', $getGit->pullOutput)
));
$curlObject->sendCurl();
?>