. * @author niel * @copyright 2014 nZEDb */ namespace Blacklight\utility; use Cz\Git\GitRepository; /** * Class Git - Wrapper for various git operations. */ class Git extends GitRepository { /** * @var string */ private $branch; /** * @var array */ private $mainBranches = ['dev', 'master']; /** * Git constructor. * * @param array $options * @throws \Cz\Git\GitException */ public function __construct(array $options = []) { $defaults = [ 'create' => false, 'initialise' => false, 'filepath' => base_path().'/', ]; $options += $defaults; parent::__construct($options['filepath']); $this->branch = $this->getCurrentBranchName(); } /** * Return the number of commits made to repo. * * @throws \Cz\Git\GitException */ public function commits(): int { $count = 0; $log = explode("\n", $this->log()); foreach ($log as $line) { if (0 === strpos($line, 'commit')) { $count++; } } return $count; } /** * @param null $options * @return \Cz\Git\GitRepository * @throws \Cz\Git\GitException */ public function describe($options = null): GitRepository { return $this->run("describe $options"); } /** * @return string */ public function getBranch(): string { return $this->branch; } /** * @param $gitObject * * @return false|bool */ public function isCommited($gitObject): bool { $cmd = "cat-file -e $gitObject"; try { $result = $this->run($cmd); } catch (\Throwable $e) { $message = explode("\n", $e->getMessage()); if ($message[0] === "fatal: Not a valid object name $gitObject") { $result = false; } else { throw new \RuntimeException($message); } } return $result === ''; } /** * @param null $options * @return \Cz\Git\GitRepository * @throws \Cz\Git\GitException */ public function log($options = null): GitRepository { return $this->run("log $options"); } /** * @return array */ public function mainBranches(): array { return $this->mainBranches; } /** * @param null $options * @return \Cz\Git\GitRepository * @throws \Cz\Git\GitException */ public function tag($options = null): GitRepository { return $this->run("tag $options"); } /** * @return \Cz\Git\GitRepository * @throws \Cz\Git\GitException */ public function tagLatest(): GitRepository { return $this->describe('--tags --abbrev=0 HEAD'); } }