Skip to content

Add pre-push hook to protect reviewed PRs from history rewrites - #1610

Open
firasrg wants to merge 4 commits into
developfrom
firasrg/git-prepush-setup
Open

firasrg wants to merge 4 commits into
developfrom
firasrg/git-prepush-setup

Conversation

@firasrg

@firasrg firasrg commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Rebasing/squashing and force pushing a branch rewrites history, which detaches review comments on an open PR and makes reviewers' lives harder.

Solution

  • Add scripts/pre-push: on a non-fast-forward push, it asks GitHub (via gh) whether the branch has an open PR with at least one review, and blocks the push if so.
  • Install it alongside pre-commit via the installLocalGitHook in build.gradle.
  • Add short docs on how it works.

Behavior

  • Fast-forward pushes, new branches, deletions and tags: untouched, no API call.
  • gh missing or GitHub unreachable: fails open and logs why.
  • Intentional rewrites: git push --no-verify.

Testing

Verified manually: fast-forward, non-FF without a PR, non-FF with a reviewed PR
(blocked), --no-verify, and the fail-open path.

Git Console Logs:

[pre-push] Starting history-rewrite check...
[pre-push] Non-fast-forward push to 'johndoe/branch-cool-name', checking for reviewed PRs...
[pre-push] BLOCKED: 'johndoe/branch-cool-name' has an open PR with reviews and this push rewrites history.
[pre-push] Push a fixup commit instead, or bypass with --no-verify.
[pre-push] Check finished: push blocked.
error: failed to push some refs to 'https://github.com/Together-Java/TJ-Bot.git'

Comment thread scripts/pre-push Resolved
@firasrg
firasrg force-pushed the firasrg/git-prepush-setup branch 2 times, most recently from 6761e98 to 58bdd99 Compare December 3, 2026 19:05
@firasrg
firasrg force-pushed the firasrg/git-prepush-setup branch from 58bdd99 to d31893a Compare December 3, 2026 19:08
@sonarqubecloud

sonarqubecloud Bot commented Dec 3, 2026

Copy link
Copy Markdown

@firasrg firasrg changed the title THIS IS A TEST PULL REQUEST, PLEASE IGNORE IT Add pre-push hook to protect reviewed PRs from history rewrites Dec 3, 2026
@firasrg
firasrg marked this pull request as ready for review December 3, 2026 19:32
@firasrg
firasrg requested a review from a team as a code owner December 3, 2026 19:32
@Tais993

Tais993 commented Dec 3, 2026 •

Copy link
Copy Markdown
Member

GitHub CLI is separate from Git, I personally don't have it installed on my laptop as I don't use it.

If this hook fails because of that, especially for beginners, that will make it significantly more complicated

I misunderstood your description, the code just ignores the check if GitHub CLI is not installed

Comment thread scripts/pre-push
Comment on lines +29 to +30
git merge-base --is-ancestor "$remote_oid" "$local_oid" 2>/dev/null && return 0

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
git merge-base --is-ancestor "$remote_oid" "$local_oid" 2>/dev/null && return 0
git merge-base --is-ancestor "$remote_oid" "$local_oid" 2>/dev/null
case "$?" in
0) return 0 ;;
1) ;; # Non-fast-forward, continue checking
*)
log "Could not determine commit ancestry, skipping check."
return 0
;;
esac

If the command returns an error a non 0 number, it would run the Git Hook even if it is a fast-forward merge

Comment thread scripts/pre-push
Comment on lines +14 to +18
count_reviewed_prs() {
gh pr list --repo "$UPSTREAM_REPO" --head "$1" --state open \
--json reviews --jq '[.[] | select(.reviews | length > 0)] | length' \
</dev/null 2>/dev/null
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very technically, I think this could fail if 2 PRs exist from the same branch name (but different remote)

At the start of the project, we have had cases where 2 branches exist with the same name, but that was very particular with some bigger stuff, thats not an issue anymore so I personally dont think this is an issue (one can just ignore the check)

Comment thread scripts/pre-push
Comment on lines +45 to +46
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."

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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."
log "BLOCKED: '$branch' has an open PR with existing reviews."
log "Rewriting history can make review comments outdated and make it harder for reviewers, and you to track changes."
log "Consider pushing an additional commit instead."
log "To intentionally bypass this check, use git push --no-verify."

Maybe explain the reasons more thoroughly?

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The pre-push hook can wrongly block pushes when ancestry is uncertain or a branch name matches across forks, and the build can overwrite developers’ existing hooks.

2 open issues
What changed in this PR

This PR adds a local Git hook to help preserve review comments by blocking history rewrites on branches with reviewed pull requests.

Changes:

  • Checks GitHub for open pull requests with reviews when a push appears to rewrite branch history.
  • Skips fast-forward pushes, new branches, deletions, and tags. If the GitHub CLI is unavailable or the query fails, it logs why and allows the push.
  • Installs the hook alongside pre-commit during the Gradle build.
File Description
scripts/​pre-push Checks branch ancestry and review status before allowing a push.
build.gradle Adds pre-push to the local Git hook installer.

Review effort: Balanced

Comment thread scripts/pre-push
# number of open PRs from branch $1 that have at least one review.
# </dev/null so github can't swallow the ref lines we're reading from stdin.
count_reviewed_prs() {
gh pr list --repo "$UPSTREAM_REPO" --head "$1" --state open \
Comment thread scripts/pre-push
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants