Fixed git commit hash logic

This commit is contained in:
Jan Böhmer 2026-02-02 21:02:08 +01:00
parent 29a08d152a
commit 883e3b271d

View file

@ -85,20 +85,26 @@ final readonly class GitVersionInfoProvider
*/ */
public function getCommitHash(int $length = 8): ?string public function getCommitHash(int $length = 8): ?string
{ {
$filename = $this->getGitDirectory() . '/refs/remotes/origin/'.$this->getBranchName(); $path = $this->getGitDirectory() . '/HEAD';
if (is_file($filename)) { if (!file_exists($path)) {
$head = file($filename); return null;
if (!isset($head[0])) {
return null;
}
$hash = $head[0];
return substr($hash, 0, $length);
} }
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;
} }
/** /**