From 3dd8cea8bcd4b1333cf487badfba24dae7fea0bd Mon Sep 17 00:00:00 2001 From: Firas Regaieg Date: Thu, 26 Nov 2026 20:09:16 +0100 Subject: [PATCH 1/5] Adding pre-push hook --- build.gradle | 4 +++- scripts/pre-push | 2 ++ 2 files changed, 5 insertions(+), 1 deletion(-) create mode 100644 scripts/pre-push 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..905cb72e7b --- /dev/null +++ b/scripts/pre-push @@ -0,0 +1,2 @@ +#!/bin/sh +# TODO: pre-push hook \ No newline at end of file From 6896bf685cf73fb132f588e2e12fb0972627dbf8 Mon Sep 17 00:00:00 2001 From: Firas Regaieg Date: Fri, 27 Nov 2026 19:52:16 +0100 Subject: [PATCH 2/5] test commit --- scripts/pre-push | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/scripts/pre-push b/scripts/pre-push index 905cb72e7b..a63a299952 100644 --- a/scripts/pre-push +++ b/scripts/pre-push @@ -1,2 +1,32 @@ #!/bin/sh -# TODO: pre-push hook \ No newline at end of file +# pre-push hook (step 1: skeleton). Classifies pushes, never blocks. + +ZERO_OID="0000000000000000000000000000000000000000" + +log() { + printf '[pre-push] %s\n' "$*" >&2 +} + +classify() { + local_oid=$1 + remote_ref=$2 + remote_oid=$3 + + case "$remote_ref" in refs/heads/*) ;; *) echo "skip (not a branch)"; return ;; esac + [ "$local_oid" = "$ZERO_OID" ] && { echo "skip (deletion)"; return; } + [ "$remote_oid" = "$ZERO_OID" ] && { echo "skip (new branch)"; return; } + if git merge-base --is-ancestor "$remote_oid" "$local_oid" 2>/dev/null; then + echo "fast-forward" + else + echo "NON-FAST-FORWARD (would need review check)" + fi +} + +log "Starting history-rewrite check..." +while read -r _local_ref local_oid remote_ref remote_oid; do + log "${remote_ref#refs/heads/}: $(classify "$local_oid" "$remote_ref" "$remote_oid")" +done +log "Check finished: push allowed." +exit 0 + +# test \ No newline at end of file From d31893a5cc871ee29dbdca437d4847378b0d2a80 Mon Sep 17 00:00:00 2001 From: Firas Regaieg Date: Thu, 3 Dec 2026 19:54:22 +0100 Subject: [PATCH 3/5] test commit 2 --- scripts/pre-push | 60 +++++++++++++++++++++++++++++++++++++----------- 1 file changed, 47 insertions(+), 13 deletions(-) diff --git a/scripts/pre-push b/scripts/pre-push index a63a299952..00384187a8 100644 --- a/scripts/pre-push +++ b/scripts/pre-push @@ -1,32 +1,66 @@ #!/bin/sh -# pre-push hook (step 1: skeleton). Classifies pushes, never blocks. +# pre-push hook: blocks history-rewriting pushes to branches whose open PR +# already has reviews. Bypass deliberately with: git push --no-verify +UPSTREAM_REPO="TogetherJava/TJ-Bot" ZERO_OID="0000000000000000000000000000000000000000" log() { printf '[pre-push] %s\n' "$*" >&2 } -classify() { +# 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/*) ;; *) echo "skip (not a branch)"; return ;; esac - [ "$local_oid" = "$ZERO_OID" ] && { echo "skip (deletion)"; return; } - [ "$remote_oid" = "$ZERO_OID" ] && { echo "skip (new branch)"; return; } - if git merge-base --is-ancestor "$remote_oid" "$local_oid" 2>/dev/null; then - echo "fast-forward" - else - echo "NON-FAST-FORWARD (would need review check)" + 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 - log "${remote_ref#refs/heads/}: $(classify "$local_oid" "$remote_ref" "$remote_oid")" + check_ref "$local_oid" "$remote_ref" "$remote_oid" || status=1 done -log "Check finished: push allowed." -exit 0 -# test \ No newline at end of file +if [ "$status" -eq 0 ]; then + log "Check finished: push allowed." +else + log "Check finished: push blocked." +fi +exit "$status" + From 9a15d9933f762fd67177daf332dd13e5a84c2ba5 Mon Sep 17 00:00:00 2001 From: Firas Regaieg Date: Thu, 3 Dec 2026 20:13:32 +0100 Subject: [PATCH 4/5] test commit 3 --- scripts/pre-push | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/pre-push b/scripts/pre-push index 00384187a8..6e85f4a0cd 100644 --- a/scripts/pre-push +++ b/scripts/pre-push @@ -2,7 +2,7 @@ # pre-push hook: blocks history-rewriting pushes to branches whose open PR # already has reviews. Bypass deliberately with: git push --no-verify -UPSTREAM_REPO="TogetherJava/TJ-Bot" +UPSTREAM_REPO="Together-Java/TJ-Bot" ZERO_OID="0000000000000000000000000000000000000000" log() { From 0e6dc0b19d3d435ab9396aa05ec86cc19c08b4d8 Mon Sep 17 00:00:00 2001 From: Firas Regaieg Date: Fri, 4 Dec 2026 20:41:00 +0100 Subject: [PATCH 5/5] Add readme for git hooks --- scripts/pre-push | 3 +-- scripts/readme.md | 67 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 scripts/readme.md diff --git a/scripts/pre-push b/scripts/pre-push index 6e85f4a0cd..0153ddc399 100644 --- a/scripts/pre-push +++ b/scripts/pre-push @@ -62,5 +62,4 @@ if [ "$status" -eq 0 ]; then else log "Check finished: push blocked." fi -exit "$status" - +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