intel_sync_upstream: Avoid unintended file deletions during commits.

This commit adds a preventative measure to avoid the unintended removal
of essential files like OWNER and DIR_METADATA. The code now includes a
check that verifies if these files have been deleted, and if so, it
automatically restores them to their original state.

BUG=none
TEST=`intel_sync_upstream.sh ptl fsp 'PTL.3015.00' 1234567` and
ensured that the required files are restored. performed similar checks
for edk2 and edk2-platform.

Change-Id: I607a541445583e46067f0c2b167fb073887d0da8
Signed-off-by: Jayvik Desai <jayvik@google.com>
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/dev-util/+/6367899
Reviewed-by: Nick Vaccaro <nvaccaro@google.com>
Commit-Queue: ChromeOS Auto Runner <chromeos-auto-runner@chromeos-bot.iam.gserviceaccount.com>
Reviewed-by: Kapil Porwal <kapilporwal@chromium.org>
Commit-Queue: Kapil Porwal <kapilporwal@chromium.org>
diff --git a/contrib/firmware/intel_sync_upstream.sh b/contrib/firmware/intel_sync_upstream.sh
index 07ff4cd..7f8d6d9 100755
--- a/contrib/firmware/intel_sync_upstream.sh
+++ b/contrib/firmware/intel_sync_upstream.sh
@@ -201,6 +201,29 @@
 
 echo "Merge of ${STAGING_NAME}-${VERSION} complete, ready for upload."
 
+# Restore the required file if deleted
+FILES_TO_RESTORE=("OWNERS" "DIR_METADATA")
+
+restore_file() {
+  local file_name="$1"
+  local full_path="${SRC_DIR}/${file_name}"
+
+  if [ ! -f "${full_path}" ]; then
+    echo "File '${file_name}' is missing. Restoring..."
+    if git -C "${SRC_DIR}" checkout cros-internal/"${CHROMEOS_BRANCH}" -- "${file_name}"; then
+      git commit --amend --no-edit > /dev/null
+      echo "File '${file_name}' restored successfully."
+    else
+      echo -e "\033[31mError: Failed to restore '${file_name}' using git.\033[0m"
+    fi
+  fi
+}
+
+# Restore each file in the list
+for file in "${FILES_TO_RESTORE[@]}"; do
+  restore_file "${file}"
+done
+
 while true
 do
   read -r -p "Do you want to push your changes to cros-internal? [y/n] >" input