运行在Windows中运行git命令的php文件

时间:2015-03-16 07:24:17

标签: php git

我要去的地方是在php中构建一个更新脚本,它通过xampp中的exec执行git命令

update.php

 <?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 'C:\xampp\htdocs\'");

#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:example/system.git | head -1 | sed 's/HEAD//'", $getGit->lastRemoteCommitId);

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://example.com/updates/system.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();

?> 

此脚本在linux(ubuntu)中正常运行,并且在执行时遇到以下错误

1)C:\server-x9\php\php.exe C:\\xampp\htdocs\Controller\update.php它说php.exe不是有效的32个应用程序。

怎么能解决这个问题?

2)C:\\xampp\htdocs也不是.git存储库

我怎样才能解决这个问题?

我在这里做错了吗?

0 个答案:

没有答案