intel_sync_upstream: Fix unreadable files before git add

Add fix_permissions() function to resolve files with 000 permissions
(unreadable files) that prevent git add from working.

In edk2/edk2-platforms repos, submodules are sometimes handled as regular
files and appear with 000 permissions after patching. This causes git add
to fail with permission denied errors.

The function:
- Identifies and lists unreadable files (000 permissions) before fixing them
- Changes permissions from 000 to 664 (rw-rw-r--) to allow git operations
- Only applies to non-FSP directories where this issue occurs
- Called specifically before git add to ensure staging works properly

This prevents git add failures during the upstream sync process for
edk2 and edk2-platforms repositories.

Change-Id: I2aa37a94d0019cd4f5a61fe053bb21c4172bcfd3
Signed-off-by: Manigandan, Balaji <balaji.manigandan@intel.com>
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/dev-util/+/6998889
Reviewed-by: Kapil Porwal <kapilporwal@chromium.org>
Reviewed-by: Pranava Y N <pranavayn@google.com>
diff --git a/contrib/firmware/intel_sync_upstream.sh b/contrib/firmware/intel_sync_upstream.sh
index 3ea5d85..42cc17c 100755
--- a/contrib/firmware/intel_sync_upstream.sh
+++ b/contrib/firmware/intel_sync_upstream.sh
@@ -26,6 +26,36 @@
   fi;
 }
 
+function fix_permissions()
+{
+  # edk2/edk2-platform - when submodules are stored as regular files
+  # we see files with 000 permissions
+  local dir_type="$1"
+  local staging_name="$2"
+  local version="$3"
+
+  # Only apply permission fixes for non-FSP directories
+  if [[ "${dir_type}" != "fsp" ]]; then
+    echo "Checking for files with 000 permissions in ${dir_type}..."
+
+    # Find files with 000 permissions directly
+    local files_with_000_perms
+    files_with_000_perms=$(git diff --name-only \
+      chrome-internal-tot.."${staging_name}"-"${version}" | \
+      xargs -I {} find {} -perm 000 2>/dev/null)
+
+    if [[ -n "${files_with_000_perms}" ]]; then
+      echo "Found files with 000 permissions that will be fixed:"
+      echo "${files_with_000_perms}"
+      echo "Fixing permissions for ${dir_type}..."
+      echo "${files_with_000_perms}" | xargs chmod 664
+      echo "Permissions fixed."
+    else
+      echo "No files with 000 permissions found for ${dir_type}."
+    fi
+  fi
+}
+
 function usage()
 {
   echo "Error: missing parameter."
@@ -176,6 +206,10 @@
         git diff -a chrome-internal-tot.."${STAGING_NAME}"-"${VERSION}" > /tmp/merge-to-tag.patch
         patch -p1 < /tmp/merge-to-tag.patch
         rm /tmp/merge-to-tag.patch
+
+        # edk2/edk2-platforms - fix  permission issues when submodules handled as regular files
+        # git add fails without this change
+        fix_permissions "${DIR}" "${STAGING_NAME}" "${VERSION}"
         git add .
 
         git commit ${COMMIT_ARGS} --message "Merge branch \`${VERSION}\` into chrome-internal-tot"$'\n\n'"BUG=b:${BUG_ID}" --edit