diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index db3e21ff135..c7e1e325c8e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -252,7 +252,7 @@ jobs: run: | pip install -r dev_tools/requirements/deps/protos.txt - name: Build protos - run: check/build-changed-protos + run: check/protos-up-to-date coverage: name: Coverage check runs-on: ubuntu-20.04 diff --git a/check/build-changed-protos b/check/build-changed-protos deleted file mode 100755 index df58599498b..00000000000 --- a/check/build-changed-protos +++ /dev/null @@ -1,74 +0,0 @@ -#!/usr/bin/env bash - -################################################################################ -# Runs bazel build for proto files in cirq/google/api/* that have changed. -# -# If any BUILD file for the proto has changed, all targets in that BUILD -# file will be built. -# -# Usage: -# check/build-changed-protos [BASE_REVISION] -# -# You can specify a base git revision to compare against (i.e. to use when -# determining whether or not a line is considered to have "changed"). For -# example, you can compare against 'origin/main' or 'HEAD~1'. -# -# If you don't specify a base revision, the following defaults will be tried, in -# order, until one exists: -# -# 1. upstream/main -# 2. origin/main -# 3. main -# -# If none exists, the script fails. -# -# See build-protos for building all protos. -################################################################################ - -# Get the working directory to the repo root. -thisdir="$(dirname "${BASH_SOURCE[0]}")" || exit $? -topdir="$(git -C "${thisdir}" rev-parse --show-toplevel)" || exit $? -cd "${topdir}" || exit $? - -# Figure out which revision to compare against. -if [ -n "$1" ] && [[ $1 != -* ]]; then - if ! git rev-parse --verify --quiet --no-revs "$1^{commit}"; then - echo -e "\033[31mNo revision '$1'.\033[0m" >&2 - exit 1 - fi - rev=$1 -elif [ "$(git cat-file -t upstream/main 2> /dev/null)" == "commit" ]; then - rev=upstream/main -elif [ "$(git cat-file -t origin/main 2> /dev/null)" == "commit" ]; then - rev=origin/main -elif [ "$(git cat-file -t main 2> /dev/null)" == "commit" ]; then - rev=main -else - echo -e "\033[31mNo default revision found to compare against. Argument #1 must be what to diff against (e.g. 'origin/main' or 'HEAD~1').\033[0m" >&2 - exit 1 -fi -base="$(git merge-base "${rev}" HEAD)" -if [ "$(git rev-parse "${rev}")" == "${base}" ]; then - echo -e "Comparing against revision '${rev}'." >&2 -else - echo -e "Comparing against revision '${rev}' (merge base ${base})." >&2 - rev="${base}" -fi - -# All the protos. -echo "Building protos in $PWD" - -dev_tools/build-protos.sh - -# Filenames with spaces will be ugly (each part will be listed separately) -# but the error logic will still work. -uncommitted=$(git status --porcelain 2>/dev/null | grep -E "^[?][?] cirq-google" | cut -d " " -f 3) - -if [[ -n "$uncommitted" ]]; then - echo -e "\033[31mERROR: Uncommitted generated files found! Please generate and commit these files using dev_tools/build-protos.sh:\033[0m" - for generated in $uncommitted - do - echo -e "\033[31m ${generated}\033[0m" - done - exit 1 -fi diff --git a/check/protos-up-to-date b/check/protos-up-to-date new file mode 100755 index 00000000000..5543533ed13 --- /dev/null +++ b/check/protos-up-to-date @@ -0,0 +1,47 @@ +#!/usr/bin/env bash + +################################################################################ +# Verifies that Python files generated from protobuf definitions are up to date. +# +# See dev_tools/build-protos.sh for building all protos. +################################################################################ + +# Get the working directory to the repo root. +thisdir="$(dirname "${BASH_SOURCE[0]}")" || exit $? +topdir="$(git -C "${thisdir}" rev-parse --show-toplevel)" || exit $? +cd "${topdir}" || exit $? + +git_status="$(git status --short)" +if [[ -n "${git_status}" ]]; then + echo "$0 requires a pristine worktree, but 'git status' shows" + echo "some changes or untracked files." + echo + echo "Please commit or clean these up to try again." + exit 2 +fi + +echo "Removing generated Python files. If not restored by this script use" +echo 'git restore "*_pb2.py*" to recover them back.' +echo + +git rm --quiet "cirq-google/*_pb2.py*" +# restore deleted files in git index +git reset --quiet + +echo "Building protos in $PWD" +echo + +dev_tools/build-protos.sh + +git_status="$(git status --short)" + +if [[ -n "${git_status}" ]]; then + echo + echo -e "\033[31mERROR: dev_tools/build-protos.sh changed generated files!\033[0m" + echo -e "\033[31mPlease update and commit these files using dev_tools/build-protos.sh\033[0m" + echo + echo "Output of 'git status' (note there may be untracked new files or deleted old ones):" + echo + git status + exit 1 +fi