mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2026-02-11 03:59:35 +00:00
Fixed git commit hash logic
This commit is contained in:
parent
29a08d152a
commit
883e3b271d
1 changed files with 18 additions and 12 deletions
|
|
@ -85,20 +85,26 @@ final readonly class GitVersionInfoProvider
|
|||
*/
|
||||
public function getCommitHash(int $length = 8): ?string
|
||||
{
|
||||
$filename = $this->getGitDirectory() . '/refs/remotes/origin/'.$this->getBranchName();
|
||||
if (is_file($filename)) {
|
||||
$head = file($filename);
|
||||
|
||||
if (!isset($head[0])) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$hash = $head[0];
|
||||
|
||||
return substr($hash, 0, $length);
|
||||
$path = $this->getGitDirectory() . '/HEAD';
|
||||
if (!file_exists($path)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return null; // this is not a Git installation
|
||||
$head = trim(file_get_contents($path));
|
||||
|
||||
// If it's a symbolic ref (e.g., "ref: refs/heads/main")
|
||||
if (str_starts_with($head, 'ref:')) {
|
||||
$refPath = $this->getGitDirectory() . '/' . trim(substr($head, 5));
|
||||
if (file_exists($refPath)) {
|
||||
$hash = trim(file_get_contents($refPath));
|
||||
}
|
||||
} else {
|
||||
// Otherwise, it's a detached HEAD (the hash is right there)
|
||||
$hash = $head;
|
||||
}
|
||||
|
||||
return isset($hash) ? substr($hash, 0, $length) : null;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue