1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ballen\GitVersionNumber; |
4
|
|
|
|
5
|
|
|
use Ballen\Executioner\Executioner as Executable; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Git Version Number |
9
|
|
|
* |
10
|
|
|
* A library for extracting and utilising your project's Git version information. |
11
|
|
|
* |
12
|
|
|
* @author Bobby Allen <[email protected]> |
13
|
|
|
* @license https://raw.githubusercontent.com/allebb/git-version-number/master/LICENSE |
14
|
|
|
* @link https://github.com/allebb/git-version-number |
15
|
|
|
* @link http://bobbyallen.me |
16
|
|
|
* |
17
|
|
|
*/ |
18
|
|
|
class Version |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The git binary path. |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
private $gitBin; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Optional path to the .git directory (root of the project if it is not the same as the PHP script) |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
private $gitPath; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* The Git Tag version number. |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
private $version = '0.0.0-0-0000000'; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* The Git commit hash. |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
private $hash = null; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Class constructor |
47
|
|
|
* @param string $gitPath The root project path (if not the current directory) |
48
|
|
|
* @param string $gitBin The path to the Git binary (by default will use the system PATH variable.) |
49
|
|
|
*/ |
50
|
8 |
|
public function __construct($gitPath = '.', $gitBin = 'git') |
51
|
|
|
{ |
52
|
8 |
|
$this->gitBin = $gitBin; |
53
|
8 |
|
$this->setRepositoryPath($gitPath); |
54
|
8 |
|
$this->extractVersion(); |
55
|
8 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Get the version number as a dot seperated string. |
59
|
|
|
* @param int $bits Optional version number bits to return. |
60
|
|
|
* @return string |
61
|
|
|
*/ |
62
|
16 |
View Code Duplication |
public function getVersionString($bits = null) |
|
|
|
|
63
|
|
|
{ |
64
|
16 |
|
if (is_null($bits)) { |
65
|
16 |
|
return $this->version; |
66
|
|
|
} |
67
|
4 |
|
return implode('.', $this->versionFromBits($bits)); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Returns the version number as a full number (integer) this is the same as the version string but without dots and will drop leading zeros! |
72
|
|
|
* @param int $bits Optional version number bits to return. |
73
|
|
|
* @return int |
74
|
|
|
*/ |
75
|
6 |
View Code Duplication |
public function getVersionNumber($bits = null) |
|
|
|
|
76
|
|
|
{ |
77
|
6 |
|
if (is_null($bits)) { |
78
|
2 |
|
return (int)str_replace('.', '', $this->version); |
79
|
|
|
} |
80
|
4 |
|
return implode('', $this->versionFromBits($bits)); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Returns the version hash (if one is present) (eg. g0106dc9) |
85
|
|
|
* @return string |
86
|
|
|
*/ |
87
|
4 |
|
public function getVersionHash() |
88
|
|
|
{ |
89
|
4 |
|
return $this->hash; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Returns an array containing the version string parts. |
94
|
|
|
* @return array |
95
|
|
|
*/ |
96
|
12 |
|
public function getVersionBits() |
97
|
|
|
{ |
98
|
12 |
|
return explode('.', $this->getVersionString()); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Method overload, outputs the full text version number as outputted by the 'git describe --tags' command. |
103
|
|
|
* @return string |
104
|
|
|
*/ |
105
|
2 |
|
public function __toString() |
106
|
|
|
{ |
107
|
2 |
|
return $this->version; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Sets the Git repository path ensuring the /.git is appended to the end of the path. |
112
|
|
|
* @return void |
113
|
|
|
*/ |
114
|
8 |
|
private function setRepositoryPath($path) |
115
|
|
|
{ |
116
|
8 |
|
$this->gitPath = rtrim($path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . '.git'; |
117
|
8 |
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Extracts the version from the Git repository. |
121
|
|
|
* @return void |
122
|
|
|
*/ |
123
|
8 |
|
private function extractVersion() |
124
|
|
|
{ |
125
|
8 |
|
$git = Executable::make($this->gitBin) |
126
|
8 |
|
->addArgument(sprintf('--git-dir=%s', $this->gitPath)) |
127
|
8 |
|
->addArgument('describe') |
128
|
8 |
|
->addArgument('--tags') |
129
|
8 |
|
->addArgument('--always'); |
130
|
|
|
|
131
|
|
|
try { |
132
|
8 |
|
$git->execute(); |
133
|
2 |
|
} catch (\Ballen\Executioner\Exceptions\ExecutionException $exception) { |
134
|
|
|
// We could not obtain/execute the git command. |
135
|
|
|
} |
136
|
|
|
|
137
|
8 |
|
$version = trim($git->resultAsText()); |
138
|
8 |
|
$this->version = '0.0.0-0-' . $version; |
139
|
|
|
|
140
|
8 |
|
if (strlen($version) > 7) { |
141
|
2 |
|
$this->version = str_replace('v', '', $version); |
142
|
|
|
} |
143
|
8 |
|
$this->versionBits(); |
144
|
8 |
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Computes and sets the version number and object hash properties. |
148
|
|
|
* @return void |
149
|
|
|
*/ |
150
|
8 |
|
private function versionBits() |
151
|
|
|
{ |
152
|
8 |
|
$versionBits = explode('-', $this->version); |
153
|
8 |
|
if (strlen($versionBits[0])) { |
154
|
8 |
|
$this->version = $versionBits[0]; |
155
|
8 |
|
if (isset($versionBits[1])) { |
156
|
8 |
|
$this->version = $versionBits[0] . '.' . $versionBits[1]; |
157
|
|
|
} |
158
|
8 |
|
if (isset($versionBits[2])) { |
159
|
8 |
|
$this->hash = $versionBits[2]; |
160
|
|
|
} |
161
|
|
|
} |
162
|
8 |
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Returns an customised array of the total number of version bits. |
166
|
|
|
* @param int $bits The total number of bits (segments) to return. |
167
|
|
|
* @return array |
168
|
|
|
*/ |
169
|
8 |
|
private function versionFromBits($bits) |
170
|
|
|
{ |
171
|
8 |
|
$version = []; |
172
|
8 |
|
foreach (range(0, ($bits - 1)) as $bit) { |
173
|
8 |
|
$version[$bit] = $this->getVersionBits()[$bit]; |
174
|
|
|
} |
175
|
8 |
|
return $version; |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.