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
There was a problem hiding this comment.
A real fastforward exits 0, so it never reaches the check. The only case that falls through is an error (exit 128), typically when the remote commit isn't in the local object db because the branchh wasn't fetched. That's the situation where over-writing remote work is most likely, so I'd rather run the pr check there than skipit
The cost is one extra gh call, and gh failures already fail open. I've added a comment in the script to make this explicit, check it out
| 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)
There was a problem hiding this comment.
Agreed, it's rare and fails in the safe direction with --no-verify as the escape trick. I've documented the limitation in the readme file ive added today, rather than adding more complexity to the script, what do you think?
| 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: