From 806b64720c9a691962efc39d07c1df2127158f70 Mon Sep 17 00:00:00 2001
From: Jean-Laurent DUZANT <jean-laurent.duzant@xelians.fr>
Date: Thu, 6 Apr 2023 17:08:34 +0200
Subject: [PATCH] FIX #24074 TIME 0:45 improve getGitRepoInformation function
 && fix hash bug, when tag is in 'detached HEAD'

---
 .../controllers/AuthenticationController.php  | 44 +++++++++++--------
 1 file changed, 26 insertions(+), 18 deletions(-)

diff --git a/src/core/controllers/AuthenticationController.php b/src/core/controllers/AuthenticationController.php
index bf67b18007..e2a2a8c012 100755
--- a/src/core/controllers/AuthenticationController.php
+++ b/src/core/controllers/AuthenticationController.php
@@ -558,23 +558,25 @@ class AuthenticationController
 
     public function getGitRepoInformation(Request $request, Response $response)
     {
-        if (!file_exists('package.json')) {
-            return $response->withJson(['url' => null]);
-        }
-
-        $license = json_decode(file_get_contents('package.json'), true);
-
-        if (empty($license) || empty($license['license'])) {
-            return $response->withJson(['url' => null]);
-        }
-
-        if ($license['license'] == "GPL-3.0") {
-            return $response->withJson(['url' => "https://labs.maarch.org/maarch/MaarchParapheur"]);
-        } elseif (strtolower($license['license']) == "maarch") {
-            return $response->withJson(['url' => "https://labs.maarch.org/maarch/MaarchParapheurPro"]);
-        } else {
-            return $response->withJson(['url' => null]);
+        $url = null;
+
+        if (file_exists('package.json')) {
+            $license = json_decode(file_get_contents('package.json'), true);
+
+            if (!empty($license['license'])) {
+                switch (strtolower($license['license'])) {
+                    case 'gpl-3.0':
+                        $url = 'https://labs.maarch.org/maarch/MaarchParapheur';
+                        break;
+                    case 'maarch':
+                        $url = 'https://labs.maarch.org/maarch/MaarchParapheurPro';
+                        break;
+                    default:
+                        break;
+                }
+            }
         }
+        return $response->withJson(['url' => $url]);
     }
 
     public function getGitCommitInformation(Request $request, Response $response)
@@ -588,8 +590,14 @@ class AuthenticationController
         if ($head === false) {
             return $response->withJson(['hash' => null]);
         }
-        preg_match('#^ref:(.+)$#', $head, $matches);
-        $currentHead = trim($matches[1]);
+
+        if (preg_match('#^ref:(.+)$#', $head, $matches)) {
+            // The HEAD file contains a reference to a branch
+            $currentHead = trim($matches[1]);
+        } else {
+            // The HEAD file contains a hash
+            return $response->withJson(['hash' => trim($head)]);
+        }
 
         if (empty($currentHead)) {
             return $response->withJson(['hash' => null]);
-- 
GitLab