diff --git a/build.gradle b/build.gradle index 175f552f52..104784a105 100644 --- a/build.gradle +++ b/build.gradle @@ -33,7 +33,9 @@ sonarqube { // Install git hooks tasks.register('installLocalGitHook', Copy) { - from new File(rootProject.rootDir, 'scripts/pre-commit') + from(new File(rootProject.rootDir, 'scripts')) { + include 'pre-commit', 'pre-push' + } into new File(rootProject.rootDir, '.git/hooks') filePermissions { unix("rwxrwxr-x") diff --git a/scripts/pre-push b/scripts/pre-push new file mode 100644 index 0000000000..0153ddc399 --- /dev/null +++ b/scripts/pre-push @@ -0,0 +1,65 @@ +#!/bin/sh +# pre-push hook: blocks history-rewriting pushes to branches whose open PR +# already has reviews. Bypass deliberately with: git push --no-verify + +UPSTREAM_REPO="Together-Java/TJ-Bot" +ZERO_OID="0000000000000000000000000000000000000000" + +log() { + printf '[pre-push] %s\n' "$*" >&2 +} + +# number of open PRs from branch $1 that have at least one review. +# 0)] | length' \ + /dev/null +} + +# Returns 0 if the ref may be pushed, 1 if it must be blocked. +check_ref() { + local_oid=$1 + remote_ref=$2 + remote_oid=$3 + + case "$remote_ref" in refs/heads/*) ;; *) return 0 ;; esac + [ "$local_oid" = "$ZERO_OID" ] && return 0 + [ "$remote_oid" = "$ZERO_OID" ] && return 0 + git merge-base --is-ancestor "$remote_oid" "$local_oid" 2>/dev/null && return 0 + + branch=${remote_ref#refs/heads/} + log "Non-fast-forward push to '$branch', checking for reviewed PRs..." + + if ! command -v gh >/dev/null 2>&1; then + log "'gh' not found, skipping check." + return 0 + fi + + if ! reviewed=$(count_reviewed_prs "$branch"); then + log "Could not query GitHub (offline or not authenticated), skipping check." + return 0 + fi + + if [ "${reviewed:-0}" -gt 0 ]; then + log "BLOCKED: '$branch' has an open PR with reviews and this push rewrites history." + log "Push a fixup commit instead, or bypass with --no-verify." + return 1 + fi + + log "No reviewed PR found for '$branch', OK." + return 0 +} + +log "Starting history-rewrite check..." +status=0 +while read -r _local_ref local_oid remote_ref remote_oid; do + check_ref "$local_oid" "$remote_ref" "$remote_oid" || status=1 +done + +if [ "$status" -eq 0 ]; then + log "Check finished: push allowed." +else + log "Check finished: push blocked." +fi +exit "$status" \ No newline at end of file diff --git a/scripts/readme.md b/scripts/readme.md new file mode 100644 index 0000000000..d7be325dee --- /dev/null +++ b/scripts/readme.md @@ -0,0 +1,67 @@ +# Local Git hooks + +This repository installs a pre-commit hook for formatting and linting checks and +a pre-push hook that protects reviewed pull requests. + +## Pre-commit hook: check your changes + +Before each commit, this hook formats the staged Java changes with Spotless and +runs SonarLint. Formatting fixes are staged automatically; if either check fails, +the commit is stopped. Your unstaged changes are kept separate. + +To skip the checks for one commit: + +```bash +git commit --no-verify +``` + +## Pre-push hook: protect reviewed PRs + +Rebasing, amending or squashing and then force pushing rewrites history, which +detaches review comments on an open PR. This hook blocks that push. + +### How it works + +On `git push`, for each ref: + +1. Skip tags, branch deletions and new branches. +2. Skip **fast-forward** pushes (remote tip is an ancestor of your commit), so + normal pushes never hit the API. +3. For **non-fast-forward** pushes, ask GitHub if the branch has an open PR with + at least one review: + ```sh + gh pr list --repo Together-Java/TJ-Bot --head "" --state open \ + --json reviews --jq '[.[] | select(.reviews | length > 0)] | length' + ``` +4. Block if the count is above `0`, otherwise allow. + +If `gh` is missing or the query fails, it **fails open** and logs why. + +### Bypass + +```bash +git push --force-with-lease --no-verify +``` + +### Good practice instead + +Add `git commit --fixup` commits, merge the base branch instead of rebasing, +and squash at merge time. + +### Notes + +- `--force-with-lease` doesn't know about reviews, so it doesn't replace this hook. +- `--head` matches by branch name only, so same-named branches from other forks + can cause a false block. +- It can't tell a harmless force push from a destructive one, so it errs on the + side of blocking. + +## Install the hooks + +The hooks are installed as part of the build. To install them manually: + +```bash +./gradlew installLocalGitHook +``` + +Requires an authenticated [`gh`](https://cli.github.com/) and LF line endings. \ No newline at end of file