GIT: Switches to shallow clones

Goes from full git clones to a shallow clone (depth = 1) to reduce space
usage.

Change-Id: Ibdbd03ec319deb22ec21d971e1ce5ba7e678ec1c
Signed-off-by: Matthew Dalzell <matthew.dalzell@arm.com>
diff --git a/clone.sh b/clone.sh
index 2ee3308..7060091 100755
--- a/clone.sh
+++ b/clone.sh
@@ -53,9 +53,8 @@
     fi
 
     if [ ! -f "${SHARE_FOLDER}/${REPO_NAME}.tar.gz" ]; then
-        git_clone $REPO_URL "${SHARE_FOLDER}/${REPO_NAME}"
+        git_clone $REPO_URL "${SHARE_FOLDER}/${REPO_NAME}" ${REPO_REFSPEC} ${SYNC_CMD}
         # 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 5a9e6db..47b9acd 100755
--- a/run-build.sh
+++ b/run-build.sh
@@ -33,7 +33,8 @@
         REPO_REFSPEC="$(echo "${repo}" | awk -F ';' '{print $2}')"
 
         if [ ! -z "$REPO_REFSPEC" ] ; then
-            git_checkout $REPO_PATH $REPO_REFSPEC
+            echo "Refspec was not pulled correctly"
+            exit 1
         fi
     done
 }
diff --git a/utils/util_git.sh b/utils/util_git.sh
index b7a775b..5bfca07 100644
--- a/utils/util_git.sh
+++ b/utils/util_git.sh
@@ -1,6 +1,6 @@
 #!/usr/bin/env bash
 #
-# Copyright (c) 2023 Arm Limited. All rights reserved.
+# Copyright (c) 2025 Arm Limited. All rights reserved.
 #
 # SPDX-License-Identifier: BSD-3-Clause
 #
@@ -15,6 +15,8 @@
     # 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
@@ -23,43 +25,24 @@
 
     # Clone if it does not exit
     if [ ! -d ${REPO_PATH} ]; then
-        git clone --quiet ${GIT_CLONE_PARAMS} ${REPO_URL} ${REPO_PATH}
-    fi
-}
+                # Shallow clone the repo without checkout
+        git clone --quiet ${GIT_CLONE_PARAMS} "${REPO_URL}" "${REPO_PATH}"
 
-function git_checkout() {
-    # Parse the repo elements
-    local REPO_PATH=$1
-    local REPO_REFSPEC=$2
-    local SYNC_CMD=$3
+        # 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
 
-    # 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}
+            git -C "${REPO_PATH}" checkout --quiet FETCH_HEAD 2>/dev/null \
+                || git -C "${REPO_PATH}" checkout --quiet "${REPO_REFSPEC}"
         fi
 
-        # 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}
+        # 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
         fi
 
-        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 -C "${REPO_PATH}" rev-parse --short HEAD)\n"
 
-        echo -e "Share Folder ${REPO_PATH} $(git rev-parse --short HEAD)\n"
-        cd $OLDPWD
     fi
 }