fix(husky): guard against commits/pushes to merged or closed PR branches (#1979)

Pre-commit now checks gh pr state before allowing any commit — exits 1
with a clear message if the branch PR is MERGED or CLOSED, preventing
orphaned commits (as happened on PRs #1893, #1898, and #1974).

Pre-push: changed 2>/dev/null to 2>&1 so gh errors surface visibly,
and added a state echo line so the current PR state is always logged.
This commit is contained in:
Elie Habib
2026-03-21 12:25:19 +04:00
committed by GitHub
parent 97f5aa8af7
commit 90a626dd26
2 changed files with 16 additions and 1 deletions

View File

@@ -1,2 +1,16 @@
BRANCH=$(git branch --show-current)
if [ -n "$BRANCH" ] && [ "$BRANCH" != "main" ] && [ "$BRANCH" != "master" ]; then
PR_STATE=$(gh pr view "$BRANCH" --json state --jq '.state' 2>/dev/null)
if [ "$PR_STATE" = "MERGED" ] || [ "$PR_STATE" = "CLOSED" ]; then
echo ""
echo "============================================================"
echo "ERROR: PR for branch '$BRANCH' is $PR_STATE."
echo "Do NOT commit to a merged/closed PR branch — commits will be orphaned."
echo "Run: git checkout main && git pull && git checkout -b fix/new-branch"
echo "============================================================"
exit 1
fi
fi
echo "Running Unicode safety check (staged files)..."
node scripts/check-unicode-safety.mjs --staged || exit 1

View File

@@ -7,7 +7,7 @@ fi
echo "Checking PR status for current branch..."
BRANCH=$(git branch --show-current)
if [ -n "$BRANCH" ] && [ "$BRANCH" != "main" ] && [ "$BRANCH" != "master" ]; then
PR_STATE=$(gh pr view "$BRANCH" --json state --jq '.state' 2>/dev/null)
PR_STATE=$(gh pr view "$BRANCH" --json state --jq '.state' 2>&1)
if [ "$PR_STATE" = "MERGED" ] || [ "$PR_STATE" = "CLOSED" ]; then
echo ""
echo "============================================================"
@@ -17,6 +17,7 @@ if [ -n "$BRANCH" ] && [ "$BRANCH" != "main" ] && [ "$BRANCH" != "master" ]; the
echo "============================================================"
exit 1
fi
echo " Branch PR state: ${PR_STATE:-unknown}"
fi
echo "Running type check..."