Files
grpc-library/scripts/buf.sh
DongHyeonka d9d1cfb55a ci(buf): 선언한 깨지는 schema 변경만 bufBreaking을 통과시킨다
bufBreaking은 기준(PR이면 merge base, main이면 직전 커밋)과 schema를
비교해 깨짐이면 무조건 실패했다. 그래서 일부러 깨는 변경(바로 앞 커밋의
proto 패키지 이름 바꾸기 같은 것)은 PR도 병합 뒤 main도 빨갛게 만들고,
통과시킬 길이 기준을 손으로 바꾸는 것뿐이었다.

기준과 HEAD 사이 커밋 하나가 `BREAKING CHANGE:` 꼬리말로 선언하면
통과시킨다. 커밋 규칙이 이미 깨지는 변경에 요구하는 표시이고, 그 표시가
MAJOR 버전의 근거다(docs/work/release.md 2절). 선언 없는 깨짐은 전처럼
실패하고, 고치는 길(되돌리기 또는 선언)을 말한다.

버린 대안: 첫 릴리스 전에는 검사를 끄기(태그 뒤의 첫 깨짐을 놓친다),
마지막 릴리스 태그와 비교(한 번 선언한 깨짐 뒤로 다음 릴리스까지 모든
PR이 빨개진다).

검증(desktop): HEAD c8f8721(BREAKING CHANGE 선언)과 기준 bbe49ac — 깨짐을
알리고 rc=0. 같은 트리에 꼬리말 없는 메시지로 바꾼 버리는 사본 — rc=1과
안내. gates.md 4절에 규칙을 적었다.
2026-11-08 14:08:07 +09:00

77 lines
3.5 KiB
Bash
Executable File

#!/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/<base>
# 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