#!/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. # A break passes only when a commit between the baseline and HEAD declares it (`BREAKING CHANGE:`). 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}" if "$buf" breaking --against "$root/.git#ref=$commit,subdir=$module"; then exit 0 fi # A breaking change passes only when a commit in the range declares it (commit rules: `!` in # the title and a `BREAKING CHANGE:` trailer), which is also what makes it a MAJOR version # (docs/work/release.md 2절). An undeclared one fails, so a wire break cannot slip in unnoticed. if git log --format=%B "$commit..HEAD" | grep -q '^BREAKING CHANGE:'; then echo "buf.sh: the schema breaks against ${commit:0:12}, and a commit in ${commit:0:12}..HEAD declares BREAKING CHANGE; passing." exit 0 fi echo "buf.sh: breaking schema change against ${commit:0:12} that no commit declares. Undo it, or declare it (title '!', trailer 'BREAKING CHANGE:') and release it as MAJOR." >&2 exit 1 ;; *) echo "usage: scripts/buf.sh lint|breaking" >&2 exit 2 ;; esac