1
\$\begingroup\$

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 git@github.com: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();

?> 
\$\endgroup\$
  • 1
    \$\begingroup\$ Have you heard of libgit2? It has php language binding. Can't say I've used it in php, but it's made my life easy in C#. \$\endgroup\$ – RubberDuck Mar 13 '15 at 17:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.