Skip to content

Enhancements, improvements... #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 91 additions & 13 deletions src/GitRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is bad practise, SSL verify is usefull. Right way is fix SSL issue on server. Alternatively you can extends class GitRepository and change constructor in your code base:

class OwnGitRepository extends \Cz\Git\GitRepository
{
  public function __construct($repository) {
    parent::__construct($repository);
    exec('git config --global http.sslVerify false');
  }
}

}

$this->repository = realpath($repository);
Expand Down Expand Up @@ -351,7 +353,7 @@ public function commit($message, $params = NULL)


/**
* Exists changes?
* Exists any changes?
* `git status` + magic
* @return bool
*/
Expand All @@ -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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is BC break. Method hasChanges() means "has changes to commit" (I will update phpdoc).

What is your use-case? In what case is last line "branch is behind"? Can you give a example? Probably better will be new method, something like your suggested hasRemoteChanges() or isRemoteSynced().

}


/**
* 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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about something like getHistory($branch = NULL, $limit = NULL)? Branch origin/master cannot be hard-coded, by default it must be current branch/HEAD. Value 10 for $limit is maybe too restrictive.

{
$output = "";
$this->begin();
$commits = [];
if (!is_numeric($limit)) {
$command = "git log $limit..$branch --oneline";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Values of $limit and $branch must be escaped - use self::processCommand() and maybe add ' 2>&1' (it redirects error output to STDOUT).

exec($command,$shorts);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if exec fails (for example if git is not installed)?

}
if (count($shorts)) $limit = count($shorts)-1;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Undefined variable $shorts in some cases.

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('<br>',$body);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use \n instead of <br>.

$author = exec($command.'"%aN"');
$date = exec($command.'"%aD"');
$commit = [
'head'=>$head,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe hash is better than head.

'shortHead'=>$shortHead,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is shortHead really needed? It can be obtained from hash.

'subject'=>$subject,
'body'=>$body,
'author'=>$author,
'date'=>$date
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think better is date in format like 2013-08-18T15:34:40+00:00 (--date=iso-strict) or 2013-09-12 09:43:52 +0000 (--date=iso) than default Sun Aug 18 14:49:42 2013 +0000.

];
array_push($commits,$commit);
$i++;
} while ($i <= $limit);
}
$this->end();
return $commits;
}
public function getRev() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably better is getCurrentRevision()? It's consistent with getCurrentBranch().

$this->begin();
$head = exec('git rev-parse HEAD');
$this->end();
return $head;
}
/**
* Pull changes from a remote
* @param string|NULL
Expand All @@ -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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's BC break. Why you need git pull output?

}


Expand Down Expand Up @@ -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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's BC break. Why you need git fetch output?

}


Expand Down Expand Up @@ -579,12 +656,13 @@ protected function run($cmd/*, $options = NULL*/)
$cmd = self::processCommand($args);
exec($cmd . ' 2>&1', $output, $ret);

$result = implode (" ",$output);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is BC break.

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;
}


Expand Down
25 changes: 22 additions & 3 deletions src/IGit.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -143,8 +161,10 @@ function commit($message, $params = NULL);
* Exists changes?
* @return bool
*/
function hasChanges();

function hasLocalChanges();

function hasRemoteChanges();

/**
* Pull changes from a remote
Expand All @@ -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);

Expand Down