Revert "GIT: Switches to shallow clones"

This reverts commit 601e7aa1c186f51a42c0cd9a0316518a492f3391.

Reason for revert: Causes static tests jobs to fail, more testing is required before putting this in again

Change-Id: I475727b2676d585647e4efb44e5736cf1a49b0be
diff --git a/clone.sh b/clone.sh
index 7060091..2ee3308 100755
--- a/clone.sh
+++ b/clone.sh
@@ -53,8 +53,9 @@
     fi
 
     if [ ! -f "${SHARE_FOLDER}/${REPO_NAME}.tar.gz" ]; then
-        git_clone $REPO_URL "${SHARE_FOLDER}/${REPO_NAME}" ${REPO_REFSPEC} ${SYNC_CMD}
+        git_clone $REPO_URL "${SHARE_FOLDER}/${REPO_NAME}"
         # Compress for shared area
+        git_checkout "${SHARE_FOLDER}/${REPO_NAME}" $REPO_REFSPEC $SYNC_CMD
         cd ${SHARE_FOLDER}
         if [ "${REPO_NAME}" = "${TFM_NAME}" ] || [ "${REPO_NAME}" = "${TFM_TESTS_NAME}" ]; then
             # These two need to remain as directories for now for further usage
diff --git a/run-build.sh b/run-build.sh
index 47b9acd..5a9e6db 100755
--- a/run-build.sh
+++ b/run-build.sh
@@ -33,8 +33,7 @@
         REPO_REFSPEC="$(echo "${repo}" | awk -F ';' '{print $2}')"
 
         if [ ! -z "$REPO_REFSPEC" ] ; then
-            echo "Refspec was not pulled correctly"
-            exit 1
+            git_checkout $REPO_PATH $REPO_REFSPEC
         fi
     done
 }
diff --git a/utils/util_git.sh b/utils/util_git.sh
index 5bfca07..b7a775b 100644
--- a/utils/util_git.sh
+++ b/utils/util_git.sh
@@ -1,6 +1,6 @@
 #!/usr/bin/env bash
 #
-# Copyright (c) 2025 Arm Limited. All rights reserved.
+# Copyright (c) 2023 Arm Limited. All rights reserved.
 #
 # SPDX-License-Identifier: BSD-3-Clause
 #
@@ -15,8 +15,6 @@
     # Parse the repo elements
     local REPO_URL=$1
     local REPO_PATH=$2
-    local REPO_REFSPEC=$3
-    local REPO_SYNC_CMD=$4
 
     # In case repository is not defined, just skip it
     if [ -z "${REPO_URL}" ]; then
@@ -25,24 +23,43 @@
 
     # Clone if it does not exit
     if [ ! -d ${REPO_PATH} ]; then
-                # Shallow clone the repo without checkout
-        git clone --quiet ${GIT_CLONE_PARAMS} "${REPO_URL}" "${REPO_PATH}"
+        git clone --quiet ${GIT_CLONE_PARAMS} ${REPO_URL} ${REPO_PATH}
+    fi
+}
 
-        # If a refspec or commit SHA was provided, fetch & checkout shallowly
-        if [ -n "${REPO_REFSPEC}" ]; then
-            git -C "${REPO_PATH}" fetch --quiet --depth=1 origin "${REPO_REFSPEC}" \
-                || git -C "${REPO_PATH}" fetch --quiet --all --depth=1
+function git_checkout() {
+    # Parse the repo elements
+    local REPO_PATH=$1
+    local REPO_REFSPEC=$2
+    local SYNC_CMD=$3
 
-            git -C "${REPO_PATH}" checkout --quiet FETCH_HEAD 2>/dev/null \
-                || git -C "${REPO_PATH}" checkout --quiet "${REPO_REFSPEC}"
+    # Checkout if repo exits
+    if [ -d ${REPO_PATH} ]; then
+        cd ${REPO_PATH}
+
+        # Fetch the corresponding refspec
+        REPO_FETCH_HEAD=$(git ls-remote --quiet | grep ${REPO_REFSPEC} | awk -v d=" " '{s=(NR==1?s:s d)$1} END{print s}')
+
+        if [ -z "${REPO_FETCH_HEAD}" ]; then
+            git fetch --all
+        else
+            git fetch origin ${REPO_FETCH_HEAD}
         fi
 
-        # If requested, shallow-init and update all submodules
-        if [ "${REPO_SYNC_CMD}" = "SYNC_ALL_SUBMODULES" ]; then
-            git -C "${REPO_PATH}" submodule update --init --recursive --depth=1 --quiet
+        # Checkout to specified refspec
+        if [[ "${REPO_REFSPEC}" =~ "refs/" ]]; then
+            # Refspec in "refs/" format cannot be directly used to checkout
+            git checkout ${REPO_FETCH_HEAD}
+        else
+            git checkout ${REPO_REFSPEC}
         fi
 
-        echo -e "Share Folder ${REPO_PATH} $(git -C "${REPO_PATH}" rev-parse --short HEAD)\n"
+        if [ "${SYNC_CMD}" = "SYNC_ALL_SUBMODULES" ]; then
+            # Make sure that any submodule is also inited and updated if present
+            git submodule update --init --recursive
+        fi
 
+        echo -e "Share Folder ${REPO_PATH} $(git rev-parse --short HEAD)\n"
+        cd $OLDPWD
     fi
 }