Adam Cozzette | 4a9a7ca | 2022-10-11 15:53:12 +0000 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
| 3 | # This script updates checked-in generated files (currently CMakeLists.txt, |
| 4 | # descriptor.upb.h, and descriptor.upb.c), commits the resulting change, and |
| 5 | # pushes it. This does not do anything useful when run manually, but should be |
| 6 | # run by our GitHub action instead. |
| 7 | |
| 8 | set -ex |
| 9 | |
| 10 | # Exit early if the previous commit was made by the bot. This reduces the risk |
| 11 | # of a bug causing an infinite loop of auto-generated commits. |
| 12 | if (git log -1 --pretty=format:'%an' | grep -q "Protobuf Team Bot"); then |
| 13 | echo "Previous commit was authored by bot" |
| 14 | exit 0 |
| 15 | fi |
| 16 | |
| 17 | cd $(dirname -- "$0")/.. |
| 18 | bazel test //cmake:test_generated_files || bazel-bin/cmake/test_generated_files --fix |
| 19 | |
| 20 | # Try to determine the most recent pull request number. |
| 21 | title=$(git log -1 --pretty='%s') |
| 22 | pr_from_merge=$(echo "$title" | sed -n 's/^Merge pull request #\([0-9]\+\).*/\1/p') |
| 23 | pr_from_squash=$(echo "$title" | sed -n 's/^.*(#\([0-9]\+\))$/\1/p') |
| 24 | |
| 25 | pr="" |
| 26 | if [ ! -z "$pr_from_merge" ]; then |
| 27 | pr="$pr_from_merge" |
| 28 | elif [ ! -z "$pr_from_squash" ]; then |
| 29 | pr="$pr_from_squash" |
| 30 | fi |
| 31 | |
| 32 | if [ ! -z "$pr" ]; then |
| 33 | commit_message="Auto-generate CMake file lists after PR #$pr" |
| 34 | else |
| 35 | # If we are unable to determine the pull request number, we fall back on this |
| 36 | # default commit message. Typically this should not occur, but could happen |
| 37 | # if a pull request was merged via a rebase. |
| 38 | commit_message="Auto-generate CMake file lists" |
| 39 | fi |
| 40 | |
| 41 | git add -A |
| 42 | git diff --staged --quiet || git commit -am "$commit_message" |
| 43 | git push |