fix: make merge base fetching more robust

Several static check scripts rely on identifying the Git merge base via
get_merge_base, which may fail if the local Git history is too shallow.
This causes issues when checking patches on CI or LTS branches with
limited fetch depth.

To address this:

- Reuse a single evaluated merge_base instead of invoking get_merge_base
  multiple times.
- If merge_base is not found initially, attempt to fetch more history
  from the remote using the appropriate GERRIT_REFSPEC.
- Exit with an error if a merge base cannot be found even after
  fetching.

This improves reliability of static checks when working on partial
clones or CI environments where fetch depth is restricted.

Change-Id: Icccec0eb2f29d254e54bbd6b639f6c1ef11291a3
Signed-off-by: Harrison Mutai <harrison.mutai@arm.com>
(cherry picked from commit 789decfc87c56fc559c6b0212f82217cbc905797)
diff --git a/script/static-checks/static-checks.sh b/script/static-checks/static-checks.sh
index ffd58f2..1424349 100755
--- a/script/static-checks/static-checks.sh
+++ b/script/static-checks/static-checks.sh
@@ -17,7 +17,31 @@
 
 . $CI_ROOT/script/static-checks/common.sh
 
-# Initialize log file
+merge_base=$(get_merge_base)
+if [[ -z "$merge_base" ]]; then
+    echo "Failed to find merge base, fetching entire change history"
+
+    # Set GERRIT_REFSPEC if not already defined
+    if [[ -z "$GERRIT_REFSPEC" ]]; then
+        if [[ "$TF_GERRIT_PROJECT" == *tf-a-tests ]]; then
+            GERRIT_REFSPEC="$TFTF_GERRIT_REFSPEC"
+        else
+            GERRIT_REFSPEC="$TF_GERRIT_REFSPEC"
+        fi
+    fi
+
+    git fetch --depth=100 origin "$GERRIT_REFSPEC"
+    git checkout FETCH_HEAD
+
+    merge_base=$(get_merge_base)
+
+    if [[ -z "$merge_base" ]]; then
+        echo "Failed to determine merge base after fetching. Exiting." >&2
+        exit 1
+    fi
+fi
+
+export merge_base
 
 export LOG_TEST_FILENAME=$(pwd)/static-checks.log
 
@@ -29,10 +53,11 @@
 echo >> "$LOG_TEST_FILENAME"
 
 echo "Patch series being checked:" >> "$LOG_TEST_FILENAME"
-git log --oneline $(get_merge_base)..HEAD >> "$LOG_TEST_FILENAME"
+git log --oneline ${merge_base}..HEAD >> "$LOG_TEST_FILENAME"
 echo >> "$LOG_TEST_FILENAME"
 echo "Base branch reference commit:" >> "$LOG_TEST_FILENAME"
-git log --oneline -1 $(get_merge_base) >> "$LOG_TEST_FILENAME"
+git log --oneline -1 ${merge_base} >> "$LOG_TEST_FILENAME"
+
 
 echo >> "$LOG_TEST_FILENAME"