Repository navigation
Conversation
6761e98 to
58bdd99
Compare
58bdd99 to
d31893a
Compare
|
|
I misunderstood your description, the code just ignores the check if GitHub CLI is not installed |
| git merge-base --is-ancestor "$remote_oid" "$local_oid" 2>/dev/null && return 0 | ||
|
|
There was a problem hiding this comment.
| 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
| 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 | ||
| } |
There was a problem hiding this comment.
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)
| 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." |
There was a problem hiding this comment.
| 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?
There was a problem hiding this comment.
🟡 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-commitduring 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
| # 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 \ |
| 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 |




Rebasing/squashing and force pushing a branch rewrites history, which detaches review comments on an open PR and makes reviewers' lives harder.
Solution
scripts/pre-push: on a non-fast-forward push, it asks GitHub (viagh) whether the branch has an open PR with at least one review, and blocks the push if so.pre-commitvia theinstallLocalGitHookinbuild.gradle.Behavior
ghmissing or GitHub unreachable: fails open and logs why.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: