diff --git a/src/GitRepository.php b/src/GitRepository.php index 5b959da..2abcd9e 100644 --- a/src/GitRepository.php +++ b/src/GitRepository.php @@ -25,6 +25,8 @@ public function __construct($repository) if(basename($repository) === '.git') { $repository = dirname($repository); + // Fix ssl errors. + exec('git config --global http.sslVerify false'); } $this->repository = realpath($repository); @@ -351,7 +353,7 @@ public function commit($message, $params = NULL) /** - * Exists changes? + * Exists any changes? * `git status` + magic * @return bool */ @@ -360,19 +362,92 @@ public function hasChanges() $this->begin(); $lastLine = exec('git status'); $this->end(); - return (strpos($lastLine, 'nothing to commit')) === FALSE; // FALSE => changes + return ((strpos($lastLine, 'nothing to commit')) || (strpos($lastLine, 'branch is behind'))) === FALSE; + } + + + /** + * Exists local changes? + * `git status` + magic + * @return bool + */ + public function hasLocalChanges() + { + $this->begin(); + $lastLine = exec('git status'); + $this->end(); + return !preg_match("/nothing to commit/",$lastLine); } /** - * @deprecated + * Exists local changes? + * `git status` + magic + * @return bool */ - public function isChanges() + public function hasRemoteChanges() { - return $this->hasChanges(); + $this->begin(); + exec('git status',$lines); + $this->end(); + return (preg_match("/fast-forwarded/",implode(" ",$lines))); } + + + /** + * Read Log Messages to JSON + * + * @param string $branch - The branch to read logs from + * @param string|int $limit - Number of commits to return, or the commit hash to return logs until + * @throws Cz\Git\GitException + * @return array $logs + */ + + public function readLog($branch="origin/master",$limit=10) + { + $output = ""; + $this->begin(); + $commits = []; + if (!is_numeric($limit)) { + $command = "git log $limit..$branch --oneline"; + exec($command,$shorts); + } + if (count($shorts)) $limit = count($shorts)-1; + if (is_numeric($limit)) { + $i = 0; + do { + $command = "git log $branch -1 --pretty=format:"; + if ($i) $command = "git log $branch --skip $i -1 --pretty=format:"; + $head = exec($command.'"%H"'); + $shortHead = exec($command.'"%h"'); + $subject = exec($command.'"%s"'); + exec($command.'"%b"',$body); + $body = implode('
',$body); + $author = exec($command.'"%aN"'); + $date = exec($command.'"%aD"'); + $commit = [ + 'head'=>$head, + 'shortHead'=>$shortHead, + 'subject'=>$subject, + 'body'=>$body, + 'author'=>$author, + 'date'=>$date + ]; + array_push($commits,$commit); + $i++; + } while ($i <= $limit); + } + $this->end(); + return $commits; + } + public function getRev() { + $this->begin(); + $head = exec('git rev-parse HEAD'); + $this->end(); + return $head; + } /** * Pull changes from a remote * @param string|NULL @@ -387,9 +462,10 @@ public function pull($remote = NULL, array $params = NULL) $params = array(); } - return $this->begin() - ->run("git pull $remote", $params) - ->end(); + $this->begin(); + $result = $this->run("git pull $remote", $params); + $this->end(); + return $result; } @@ -427,9 +503,10 @@ public function fetch($remote = NULL, array $params = NULL) $params = array(); } - return $this->begin() - ->run("git fetch $remote", $params) - ->end(); + $this->begin(); + $result = $this->run("git fetch $remote", $params); + $this->end(); + return $result; } @@ -579,12 +656,13 @@ protected function run($cmd/*, $options = NULL*/) $cmd = self::processCommand($args); exec($cmd . ' 2>&1', $output, $ret); + $result = implode (" ",$output); if($ret !== 0) { - throw new GitException("Command '$cmd' failed (exit-code $ret).", $ret); + $result = "Command '$cmd' failed (exit-code $ret): $ret"; } - return $this; + return $result; } diff --git a/src/IGit.php b/src/IGit.php index e7d9453..2e32aaa 100644 --- a/src/IGit.php +++ b/src/IGit.php @@ -114,6 +114,24 @@ function removeFile($file); function addFile($file); + /** + * Read Log Messages to JSON + * + * @param string $branch - The branch to read logs from + * @param string|int $limit - Number of commits to return, or the commit hash to return logs until + * @throws Cz\Git\GitException + * @return array $logs + */ + + function readLog($branch="origin/master",$limit=10); + + /** + * Gets the current revision of the local repository. + * @returns String + */ + function getRev(); + + /** * Adds all created, modified & removed files. * @throws Cz\Git\GitException @@ -143,8 +161,10 @@ function commit($message, $params = NULL); * Exists changes? * @return bool */ - function hasChanges(); + function hasLocalChanges(); + + function hasRemoteChanges(); /** * Pull changes from a remote @@ -170,8 +190,7 @@ function push($remote = NULL, array $params = NULL); * Run fetch command to get latest branches * @param string|NULL * @param array|NULL - * @return self - * @throws GitException + * @return string Result */ function fetch($remote = NULL, array $params = NULL);