From 4c99a7d8f88b52c1639e5694a3933431c3c99bde Mon Sep 17 00:00:00 2001 From: William Desportes Date: Wed, 31 Mar 2021 00:01:25 +0200 Subject: [PATCH] Split out git common dir detection to handle file_get_contents error cases Signed-off-by: William Desportes --- libraries/classes/Git.php | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/libraries/classes/Git.php b/libraries/classes/Git.php index 4aa19d5140..83558030ec 100644 --- a/libraries/classes/Git.php +++ b/libraries/classes/Git.php @@ -497,6 +497,19 @@ class Git return [$hash, $branch]; } + private function getCommonDirContents(string $gitFolder): ?string + { + if (! is_file($gitFolder . '/commondir')) { + return null; + } + $commonDirContents = @file_get_contents($gitFolder . '/commondir'); + if ($commonDirContents === false) { + return null; + } + + return trim($commonDirContents); + } + /** * detects Git revision, if running inside repo */ @@ -518,10 +531,9 @@ class Git return null; } - $common_dir_contents = @file_get_contents($gitFolder . '/commondir'); - - if ($common_dir_contents !== false) { - $gitFolder .= DIRECTORY_SEPARATOR . trim($common_dir_contents); + $commonDirContents = $this->getCommonDirContents($gitFolder); + if ($commonDirContents !== null) { + $gitFolder .= DIRECTORY_SEPARATOR . $commonDirContents; } [$hash, $branch] = $this->getHashFromHeadRef($gitFolder, $ref_head);