|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* Copyright (C) 2020 Jan Böhmer |
|
4
|
|
|
* |
|
5
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
6
|
|
|
* it under the terms of the GNU Affero General Public License as published |
|
7
|
|
|
* by the Free Software Foundation, either version 3 of the License, or |
|
8
|
|
|
* (at your option) any later version. |
|
9
|
|
|
* |
|
10
|
|
|
* This program is distributed in the hope that it will be useful, |
|
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13
|
|
|
* GNU Affero General Public License for more details. |
|
14
|
|
|
* |
|
15
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
16
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
namespace App\Services; |
|
20
|
|
|
|
|
21
|
|
|
namespace App\Services; |
|
22
|
|
|
|
|
23
|
|
|
use Symfony\Component\HttpKernel\KernelInterface; |
|
24
|
|
|
use Symfony\Contracts\Cache\CacheInterface; |
|
25
|
|
|
use Symfony\Contracts\Cache\ItemInterface; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* This service allows to extract informations about the current git commit (useful for version info). |
|
29
|
|
|
*/ |
|
30
|
|
|
class GitVersionInfo |
|
31
|
|
|
{ |
|
32
|
|
|
protected $project_dir; |
|
33
|
|
|
protected $cache; |
|
34
|
|
|
|
|
35
|
|
|
public function __construct(KernelInterface $kernel, CacheInterface $cache) |
|
36
|
|
|
{ |
|
37
|
|
|
$this->project_dir = $kernel->getProjectDir(); |
|
38
|
|
|
$this->cache = $cache; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Get the Git branch name of the installed system. |
|
43
|
|
|
* The information is cached. |
|
44
|
|
|
* |
|
45
|
|
|
* @return string|null The current git branch name. Null, if this is no Git installation |
|
46
|
|
|
*/ |
|
47
|
|
|
public function getGitBranchName(): ?string |
|
48
|
|
|
{ |
|
49
|
|
|
return $this->cache->get('git_branch', function (ItemInterface $item) { |
|
50
|
|
|
$item->expiresAfter(4320); //Recache every 12h |
|
51
|
|
|
if (is_file($this->project_dir.'/.git/HEAD')) { |
|
52
|
|
|
$git = file($this->project_dir.'/.git/HEAD'); |
|
53
|
|
|
$head = explode('/', $git[0], 3); |
|
54
|
|
|
|
|
55
|
|
|
if (!isset($head[2])) { |
|
56
|
|
|
return null; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
return trim($head[2]); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
return null; // this is not a Git installation |
|
63
|
|
|
}); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Get hash of the last git commit (on remote "origin"!). |
|
68
|
|
|
* The information is cached. |
|
69
|
|
|
* |
|
70
|
|
|
* If this method does not work, try to make a "git pull" first! |
|
71
|
|
|
* |
|
72
|
|
|
* @param int $length if this is smaller than 40, only the first $length characters will be returned |
|
73
|
|
|
* |
|
74
|
|
|
* @return string|null The hash of the last commit, null If this is no Git installation |
|
75
|
|
|
*/ |
|
76
|
|
|
public function getGitCommitHash(int $length = 7): ?string |
|
77
|
|
|
{ |
|
78
|
|
|
return $this->cache->get('git_hash', function (ItemInterface $item) use ($length) { |
|
79
|
|
|
$item->expiresAfter(4320); //Recache every 12h |
|
80
|
|
|
|
|
81
|
|
|
$filename = $this->project_dir.'/.git/refs/remotes/origin/'.$this->getGitBranchName(); |
|
82
|
|
|
if (is_file($filename)) { |
|
83
|
|
|
$head = file($filename); |
|
84
|
|
|
|
|
85
|
|
|
if (!isset($head[0])) { |
|
86
|
|
|
return null; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
$hash = $head[0]; |
|
90
|
|
|
|
|
91
|
|
|
return substr($hash, 0, $length); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
return null; // this is not a Git installation |
|
95
|
|
|
}); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|