From 6c14bb69a8b33ccbdce823522ec5d0229411176e Mon Sep 17 00:00:00 2001 From: digitalhigh Date: Thu, 26 Oct 2017 11:49:18 -0500 Subject: [PATCH 1/2] Enhancements, Improvements Fix an error with SSL in some instances causing failures. Add methods to determine if there are local or remote changes only, in addition to checking for all changes. Add method to get the current revision of local git. Add method to read commit changes between local and remote. Update iGit documentation. Change fetch, run methods to return command output, versus throwing an error. --- src/GitRepository.php | 105 ++++++++++++++++++++++++++++++++++++------ src/IGit.php | 25 ++++++++-- 2 files changed, 113 insertions(+), 17 deletions(-) diff --git a/src/GitRepository.php b/src/GitRepository.php index 7ff27bf..848c83a 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; } @@ -578,13 +655,13 @@ protected function run($cmd/*, $options = NULL*/) $args = func_get_args(); $cmd = self::processCommand($args); exec($cmd, $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); From 0573cea81ab5ab32a4c126f0a0704381baed0e2b Mon Sep 17 00:00:00 2001 From: digitalhigh Date: Thu, 26 Oct 2017 11:52:24 -0500 Subject: [PATCH 2/2] Minor update... Merge piping to sdout. --- src/GitRepository.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/GitRepository.php b/src/GitRepository.php index 848c83a..dcc4428 100644 --- a/src/GitRepository.php +++ b/src/GitRepository.php @@ -654,7 +654,7 @@ protected function run($cmd/*, $options = NULL*/) { $args = func_get_args(); $cmd = self::processCommand($args); - exec($cmd, $output, $ret); + exec($cmd . ' 2>&1', $output, $ret); $result = implode (" ",$output); if($ret !== 0) {