Update auto-generation script to handle well-known types

I went ahead and deleted the update_file_list.sh script, because (a)
there was no good reason for it to be in a separate script and (b) we
now need to handle the well-known types in addition to file_lists.cmake.
With this change, we just invoke the staleness tests from the main
script to update everything.

While I was at it I made a couple small fixes:
- Don't skip the update step just because the previous commit was by
  "Protobuf Team Bot". Copybara commits use this name and we still want
  to do the auto-update step after them.
- Include the CL number in the description if the previous commit came
  from a CL.

PiperOrigin-RevId: 487231324
diff --git a/.github/workflows/generated_cmake.yml b/.github/workflows/generate_files.yml
similarity index 76%
rename from .github/workflows/generated_cmake.yml
rename to .github/workflows/generate_files.yml
index 640bd13..c1562b3 100644
--- a/.github/workflows/generated_cmake.yml
+++ b/.github/workflows/generate_files.yml
@@ -1,12 +1,12 @@
-name: Generate CMake File Lists
+name: Auto-generate checked-in files
 
 on:
   push:
     branches:
       - main
       - '[0-9]+.x'
-      # The 21.x branch predates support for auto-generation of the CMake file
-      # lists, so we make sure to exclude it.
+      # The 21.x branch predates support for auto-generation, so we make sure
+      # to exclude it.
       - '!21.x'
 
 jobs:
@@ -26,4 +26,4 @@
       - name: Configure name and email address in Git
         run: cd ${{ github.workspace }} && git config user.name "Protobuf Team Bot" && git config user.email "protobuf-team-bot@google.com"
       - name: Commit and push update
-        run: cd ${{ github.workspace }} && ./cmake/push_auto_update.sh
+        run: cd ${{ github.workspace }} && ./push_auto_update.sh
diff --git a/BUILD.bazel b/BUILD.bazel
index b980d99..c9b2472 100644
--- a/BUILD.bazel
+++ b/BUILD.bazel
@@ -551,7 +551,6 @@
         "WORKSPACE",
         "cmake/CMakeLists.txt",
         "cmake/README.md",
-        "cmake/update_file_lists.sh",
         "generate_descriptor_proto.sh",
         "maven_install.json",
         "//third_party:BUILD.bazel",
diff --git a/cmake/push_auto_update.sh b/cmake/push_auto_update.sh
deleted file mode 100755
index abae4f4..0000000
--- a/cmake/push_auto_update.sh
+++ /dev/null
@@ -1,41 +0,0 @@
-#!/bin/bash
-
-# This script updates the CMake file lists (i.e. src/file_lists.cmake), commits
-# the resulting change, and pushes it. This does not do anything useful when
-# run manually, but should be run by our GitHub action instead.
-
-set -ex
-
-# Exit early if the previous commit was made by the bot. This reduces the risk
-# of a bug causing an infinite loop of auto-generated commits.
-if (git log -1 --pretty=format:'%an' | grep -q "Protobuf Team Bot"); then
-  echo "Previous commit was authored by bot"
-  exit 0
-fi
-
-$(dirname -- "$0")/update_file_lists.sh
-
-# Try to determine the most recent pull request number.
-title=$(git log -1 --pretty='%s')
-pr_from_merge=$(echo "$title" | sed -n 's/^Merge pull request #\([0-9]\+\).*/\1/p')
-pr_from_squash=$(echo "$title" | sed -n 's/^.*(#\([0-9]\+\))$/\1/p')
-
-pr=""
-if [ ! -z "$pr_from_merge" ]; then
-  pr="$pr_from_merge"
-elif [ ! -z "$pr_from_squash" ]; then
-  pr="$pr_from_squash"
-fi
-
-if [ ! -z "$pr" ]; then
-  commit_message="Auto-generate CMake file lists after PR #$pr"
-else
-  # If we are unable to determine the pull request number, we fall back on this
-  # default commit message. Typically this should not occur, but could happen
-  # if a pull request was merged via a rebase.
-  commit_message="Auto-generate CMake file lists"
-fi
-
-git add -A
-git diff --staged --quiet || git commit -am "$commit_message"
-git push
diff --git a/cmake/update_file_lists.sh b/cmake/update_file_lists.sh
deleted file mode 100755
index a08bdf3..0000000
--- a/cmake/update_file_lists.sh
+++ /dev/null
@@ -1,8 +0,0 @@
-#!/bin/bash -u
-
-# This script generates file lists from Bazel for CMake.
-
-set -e
-
-bazel build //pkg:gen_src_file_lists
-cp -v bazel-bin/pkg/src_file_lists.cmake src/file_lists.cmake
diff --git a/push_auto_update.sh b/push_auto_update.sh
new file mode 100755
index 0000000..52fe903
--- /dev/null
+++ b/push_auto_update.sh
@@ -0,0 +1,46 @@
+#!/bin/bash
+
+# This script updates src/file_lists.cmake and the checked-in generated code
+# for the well-known types, commits the resulting changes, and pushes them.
+# This does not do anything useful when run manually, but should be run by our
+# GitHub action instead.
+
+set -ex
+
+# Cd to the repo root.
+cd $(dirname -- "$0")
+
+previous_commit_title=$(git log -1 --pretty='%s')
+
+# Exit early if the previous commit was auto-generated. This reduces the risk
+# of a bug causing an infinite loop of auto-generated commits.
+if (echo "$previous_commit_title" | grep -q "^Auto-generate files"); then
+  echo "Previous commit was auto-generated"
+  exit 0
+fi
+
+# Run the staleness tests and use them to update any stale files.
+bazel test //src:cmake_lists_staleness_test || ./bazel-bin/src/cmake_lists_staleness_test --fix
+bazel test //src/google/protobuf:well_known_types_staleness_test || ./bazel-bin/src/google/protobuf/well_known_types_staleness_test --fix
+
+# Try to determine the most recent CL or pull request.
+pr_from_merge=$(echo "$previous_commit_title" | sed -n 's/^Merge pull request #\([0-9]\+\).*/\1/p')
+pr_from_squash=$(echo "$previous_commit_title" | sed -n 's/^.*(#\([0-9]\+\))$/\1/p')
+cl=$(git log -1 --pretty='%b' | sed -n 's/^PiperOrigin-RevId: \([0-9]*\)$/\1/p')
+
+if [ ! -z "$pr_from_merge" ]; then
+  commit_message="Auto-generate files after PR #$pr_from_merge"
+elif [ ! -z "$pr_from_squash" ]; then
+  commit_message="Auto-generate files after PR #$pr_from_squash"
+elif [ ! -z "$cl" ]; then
+  commit_message="Auto-generate files after cl/$cl"
+else
+  # If we are unable to determine the CL or pull request number, we fall back
+  # on this default commit message. Typically this should not occur, but could
+  # happen if a pull request was merged via a rebase.
+  commit_message="Auto-generate files"
+fi
+
+git add -A
+git diff --staged --quiet || git commit -am "$commit_message"
+git push