This repository was archived by the owner on Dec 31, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGitInfo.php
More file actions
97 lines (77 loc) · 3.11 KB
/
GitInfo.php
File metadata and controls
97 lines (77 loc) · 3.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php
/*
BluffingoCore
Copyright (C) 2025 Chaziz
BluffingoCore is free software: you can redistribute it and/or modify it
under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at
your option) any later version.
BluffingoCore is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
namespace BluffingoCore;
use Exception;
class GitInfo
{
private bool $isSubmodule;
private string $gitCommitHash;
private string $gitBranch;
private string $gitPath;
public function __construct($path = null)
{
$this->gitBranch = "";
$this->gitCommitHash = "";
if (isset($path) && file_exists($path)) {
$this->gitPath = $path;
$this->isSubmodule = true;
} elseif (file_exists(BLUFF_GIT_PATH)) {
$this->gitPath = BLUFF_GIT_PATH;
$this->isSubmodule = false;
} else {
throw new Exception("The Git path does not exist.");
}
if ($this->isSubmodule) {
$gitFile = $this->gitPath . '/.git';
$content = file_get_contents($gitFile);
if (preg_match('/^gitdir: (.*)$/m', $content, $matches)) {
$gitDir = trim($matches[1]);
// handle relative paths
if ($gitDir[0] !== '/') {
$gitDir = $this->gitPath . '/' . $gitDir;
}
$gitHeadLocation = $gitDir . '/HEAD';
$gitHead = file_get_contents($gitHeadLocation);
// if detached head
if (preg_match('/^[0-9a-f]{40}$/', trim($gitHead))) {
$this->gitCommitHash = substr(trim($gitHead), 0, 7);
}
// if on a branch
if (preg_match('/ref: refs\/heads\/(.*)$/', $gitHead, $matches)) {
$this->gitBranch = trim($matches[1]);
$commitFile = $gitDir . '/refs/heads/' . $this->gitBranch;
if (file_exists($commitFile)) {
$commit = file_get_contents($commitFile); // kind of bad but hey it works
$this->gitCommitHash = substr(trim($commit), 0, 7);
}
}
}
} else {
$gitHead = file_get_contents(BLUFF_GIT_PATH . '/HEAD');
$this->gitBranch = rtrim(preg_replace("/(.*?\/){2}/", '', $gitHead));
$commit = file_get_contents(BLUFF_GIT_PATH . '/refs/heads/' . $this->gitBranch); // kind of bad but hey it works
$this->gitCommitHash = substr($commit, 0, 7);
}
}
public function getGitBranch()
{
return $this->gitBranch;
}
public function getGitCommitHash()
{
return $this->gitCommitHash;
}
}