chore: improve refresh-compat.sh with error handling and fix sed application

Add `set -euo pipefail` for strict error handling, check for clean working tree before running, and add trap for cleanup. Move sed replacements from patch file to actual changed Go files to correctly apply sonic-to-json transformations after checkout.
This commit is contained in:
yusing
2026-02-26 00:46:12 +08:00
parent af8cddc1b2
commit 9927267149

View File

@@ -1,11 +1,16 @@
#!/bin/bash
set -euo pipefail
if ! git diff --quiet || ! git diff --cached --quiet; then
echo "Working tree is not clean. Commit or stash changes before running refresh-compat.sh." >&2
exit 1
fi
git fetch origin main compat
git checkout -B compat origin/compat
patch_file="$(mktemp)"
trap 'rm -f "$patch_file"' EXIT
git diff origin/main -- . ':(exclude)**/go.mod' ':(exclude)**/go.sum' >"$patch_file"
sed -i 's/sonic\./json\./g' "$patch_file"
sed -i 's/"github.com\/bytedance\/sonic"/"encoding\/json"/g' "$patch_file"
git checkout -B main origin/main
git branch -D compat
git checkout -b compat
@@ -14,6 +19,8 @@ mapfile -t changed_go_files < <(git diff --name-only -- '*.go')
fmt_go_files=()
for file in "${changed_go_files[@]}"; do
[ -f "$file" ] || continue
sed -i 's/sonic\./json\./g' "$file"
sed -i 's/"github.com\/bytedance\/sonic"/"encoding\/json"/g' "$file"
sed -E -i 's/\bsonic[[:space:]]+"encoding\/json"/json "encoding\/json"/g' "$file"
fmt_go_files+=("$file")
done