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
8set -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.
12if (git log -1 --pretty=format:'%an' | grep -q "Protobuf Team Bot"); then
13  echo "Previous commit was authored by bot"
14  exit 0
15fi
16
17cd $(dirname -- "$0")/..
18bazel test //cmake:test_generated_files || bazel-bin/cmake/test_generated_files --fix
19
20# Try to determine the most recent pull request number.
21title=$(git log -1 --pretty='%s')
22pr_from_merge=$(echo "$title" | sed -n 's/^Merge pull request #\([0-9]\+\).*/\1/p')
23pr_from_squash=$(echo "$title" | sed -n 's/^.*(#\([0-9]\+\))$/\1/p')
24
25pr=""
26if [ ! -z "$pr_from_merge" ]; then
27  pr="$pr_from_merge"
28elif [ ! -z "$pr_from_squash" ]; then
29  pr="$pr_from_squash"
30fi
31
32if [ ! -z "$pr" ]; then
33  commit_message="Auto-generate CMake file lists after PR #$pr"
34else
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"
39fi
40
41git add -A
42git diff --staged --quiet || git commit -am "$commit_message"
43git push
44