#!/usr/bin/env bash # Buf lint and breaking-change policy for the Stable gRPC schema (grpc/grpc-contract, buf.yaml there). # # scripts/buf.sh lint # scripts/buf.sh breaking # # protoc (the Gradle protobuf plugin) compiles the schema; Buf owns lint and breaking policy. The # root `ci` stage runs both through :grpc:grpc-contract:bufLint and :bufBreaking. # # breaking compares the working tree with a baseline commit: # BUF_AGAINST set → that ref # a pull request (GITHUB_BASE_REF) → the merge base with origin/ # otherwise → the merge base with origin/main, or HEAD^ when HEAD is on it # (or when a local clone has no origin/main) # A baseline without grpc/grpc-contract/buf.yaml has nothing to be compatible with, and the check # says so and passes. A baseline Git cannot find fails: a shallow clone cannot answer the question. set -euo pipefail root=$(git rev-parse --show-toplevel) module=grpc/grpc-contract buf=$("$root/scripts/tool.sh" buf) cd "$root/$module" case "${1:-}" in lint) "$buf" lint ;; breaking) if [ -n "${BUF_AGAINST:-}" ]; then baseline=$BUF_AGAINST else base_branch="origin/${GITHUB_BASE_REF:-main}" if git rev-parse --verify --quiet "$base_branch^{commit}" >/dev/null; then baseline=$(git merge-base HEAD "$base_branch") elif [ "${GITHUB_ACTIONS:-}" = true ]; then # In CI the base branch must be there; a missing one means a shallow checkout, and comparing # with something else would answer a different question. echo "buf.sh: $base_branch is not in this clone; fetch it (actions/checkout fetch-depth: 0)." >&2 exit 1 else echo "buf.sh: no $base_branch here; comparing with HEAD^ (set BUF_AGAINST to choose)." baseline=$(git rev-parse HEAD) fi if [ -z "${GITHUB_BASE_REF:-}" ] && [ "$baseline" = "$(git rev-parse HEAD)" ]; then baseline=$(git rev-parse --verify --quiet 'HEAD^' || true) fi fi commit=$(git rev-parse --verify --quiet "${baseline}^{commit}" || true) if [ -z "$commit" ]; then echo "buf.sh: no baseline commit for '${baseline:-HEAD^}'; nothing to compare (first commit)." >&2 [ -z "${baseline:-}" ] && exit 0 || exit 1 fi if ! git cat-file -e "$commit:$module/buf.yaml" 2>/dev/null; then echo "buf.sh: baseline ${commit:0:12} has no $module/buf.yaml; breaking check skipped." exit 0 fi echo "buf.sh: breaking against ${commit:0:12}" "$buf" breaking --against "$root/.git#ref=$commit,subdir=$module" ;; *) echo "usage: scripts/buf.sh lint|breaking" >&2 exit 2 ;; esac