Last active
December 11, 2018 08:06
-
-
Save lukeocodes/5501074 to your computer and use it in GitHub Desktop.
Current git version.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class QuickGit | |
{ | |
/** @var int */ | |
private $major = 1; | |
/** @var int */ | |
private $minor = 0; | |
/** @var string */ | |
private $patch = ''; | |
/** @var int */ | |
private $commits = 0; | |
/** @var string */ | |
private $commit = ''; | |
/** | |
* @method __construct | |
*/ | |
public function __construct() | |
{ | |
// Collect git data. | |
exec('git describe --always', $gitHashShort); | |
$this->patch = $gitHashShort; | |
exec('git rev-list HEAD | wc -l', $gitCommits); | |
$this->commits = $gitCommits; | |
exec('git log -1', $gitHashLong); | |
$this->commit = $gitHashLong; | |
} | |
/** | |
* @return string | |
*/ | |
public function toString($format = 'short') | |
{ | |
switch ($format) { | |
case 'long': | |
return sprintf( | |
'%d.%d.%s (#%d, %s)', | |
$this->major, | |
$this->minor, | |
$this->patch, | |
$this->commits, | |
$this->commit | |
); | |
default: | |
return sprintf( | |
'%d.%d.%s', | |
$this->major, | |
$this->minor, | |
$this->patch | |
); | |
} | |
} | |
/** | |
* @method __toString | |
*/ | |
public function __toString() | |
{ | |
return $this->toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Not tested even since having PSR'd/improved it.