Skip to content

Keep default-branch commits out of pre-push reviews after a merge or rebase - #92

Open
Utkarsh-Srivastava29 wants to merge 7 commits into
prooflinedev:mainfrom
utkarshlab-1:fix/pre-push-base-after-sync
Open

Utkarsh-Srivastava29 wants to merge 7 commits into
prooflinedev:mainfrom
utkarshlab-1:fix/pre-push-base-after-sync

Conversation

@Utkarsh-Srivastava29

Copy link
Copy Markdown

Summary

After a developer merges or rebases main into their branch, the proofline hooks install --pre-push hook sent everyone else's newly merged main code to review along with the push. This PR makes the hook review only what the branch adds, the same view its pull request shows, without ever turning a real change into an empty review.

Walkthrough (2 min)

PR-walkthrough.mp4

Problem

For a branch the remote already has, the hook diffed the remote's old tip against the new local tip (remote_sha..local_sha). After syncing with main, that range contains every commit that landed on main in the meantime, so the push review:

  • spends credits re-reviewing code already reviewed on main
  • reports findings in files the push never touched
  • grows toward the 5 MiB / 3,000-file limits, comes back incomplete, and then blocks the push under onIncomplete=block

Reproduced on main (39b8065) with a real git push through the installed hook. Two branches were pushed, a teammate added teammate.rs to main, then one branch merged main and the other rebased onto it:

push uploaded before uploaded after
feature-a: git merge origin/main, edit, push feature-a.rs, teammate.rs feature-a.rs
feature-b: git rebase origin/main, edit, push --force feature-b.rs, teammate.rs feature-b.rs

Fix

git::pushed_branch_base picks the base for each destination the remote already has. The hook now keeps the destination ref from Git's pre-push input (PushedRef::remote_ref).

  1. The branch took in new main commits (merge or rebase): compare with its merge-base with <remote>/HEAD, like a pull request.
  2. Otherwise: compare with the remote's commit, as before. Ordinary pushes, and amend + force-push on the same base, still review only what they add.
  3. A stale <remote>/HEAD never produces "nothing to review". The remote's commit stays the base for pushes to the default branch itself, to refs outside refs/heads/ (such as refs/for/main), and whenever the merge-base has the same tree as the pushed commit. If that commit is missing locally, the push prints as not reviewed (… names no commit in this checkout), so onIncomplete applies.

Branches with no history in common with main (such as gh-pages), or no <remote>/HEAD at all, keep the old behaviour.

Trade-off: after a merge or rebase, the review covers the whole branch, like a PR, rather than only the commits since the last push. Git can't express "only my new commits" as a single base once main is interleaved, and the alternative is re-reviewing other people's code.

Changes: src/git.rs, src/hooks.rs, tests/hooks.rs.

Verification

  • Tests (tests/hooks.rs, the real binary against the existing stub console): five new tests cover merge, rebase, amend, a missing remote commit, a stale tracking ref after a rewind (to the default branch, another branch and refs/for/main), a force-pushed empty commit, and orphan branches. Each failed before its fix.
  • The tests catch regressions: removing either destination check, reading the base from HEAD, or swapping the source and destination refs makes at least one test fail.
  • End to end: a script installs the hook in a real clone, pushes to a local bare remote, and records what the CLI uploads to a small HTTP stub. The table above is its output for main and for this branch.
  • Repo checks: cargo fmt --check, cargo clippy --locked --all-targets -- -D warnings, cargo test --locked, cargo deny check and the script tests all pass.

Reproduce by hand:

git checkout -b feature && echo 'fn feature() {}' > feature.rs && git add . && git commit -m feature
git push -u origin feature                        # hook reviews feature.rs
# ...someone merges teammate.rs into main...
git fetch && git merge origin/main && git push    # before: hook also reviews teammate.rs

Proofline review

I ran Proofline on this change in my own org, utkarshlab-1/cli#1, across 8 pushes.

run what it caught outcome
1 Orphan branches printed as not reviewed (a regression I introduced); the amend test didn't check the patch was incremental Fixed in 4a9a05c
2 Serious: restoring a rewound main with a stale origin/main found "nothing to review" and passed under onIncomplete=block; the tests always pushed HEAD Fixed in 35eb68e
3 The same gap on any branch and on refs/for/*; the tests didn't separate source and destination refs Fixed in 7bb6b44. Declined one location, where swapping the refs can't change the base
4 Serious: a missing remote commit gave an empty review; the destination checks were untested on their own Fixed in ba71aa2, returning UnknownRevision rather than the suggested NoBase because it names the real cause. Declined a "still present" on the parser test: breaking the parser makes it fail
5 A missing commit on the default branch or a non-branch ref still fell back to the stale tracking ref; the rewind tests passed without the destination checks Fixed in ad48c72
6 An empty commit force-pushed over a branch found "nothing to review"; the refs/for/main tests pushed from main Fixed in 1643c64 by comparing trees, not commit IDs. Couldn't reproduce the symbolic-ref exit-128 claim on git 2.43, but added the guard anyway
7–8 Minor follow-ups and repeats (run 8 is the same code after rewording the commit messages) No code changes; listed below

Assessment: Proofline caught five real regressions my own tests missed, each of which could let a push through unreviewed or with the wrong base, and several tests that passed for the wrong reason. I reproduced every finding with a failing test, or by breaking the code on purpose, before changing anything. I declined two findings with evidence and stopped changing code after run 6, once the findings had narrowed to rare corner cases.

Known limitations

  • New refs (an all-zero advertised commit, including Gerrit-style refs/for/*) still use the existing new_branch_base, which this PR doesn't change.
  • A missing remote commit with no usable default branch prints the NoBase hint (git remote set-head) instead of naming the missing commit.
  • A remote whose fetch repository differs from its push repository: <remote>/HEAD then doesn't describe the destination.
  • After the remote's default branch is rewound, a topic branch pushed with new commits on top of the stale tracking tip can leave the restored commits out of the review. Catching this needs a network call from the hook.

…rebase

The pre-push hook compared each updated branch with the commit the remote
had. Once the branch merged or rebased onto newer default-branch commits,
that diff also carried those commits, so the push review included other
people's already-merged changes: credits spent on code already reviewed,
findings in code the push did not add, and larger patches more likely to
come back incomplete.

The remote's commit stays the base while the branch takes in nothing new
from the default branch, so ordinary pushes still review only what they
add. Otherwise the branch is compared with its merge-base with the default
branch, as a new branch already is. A remote commit missing from the
checkout falls back the same way instead of failing the diff.
…lt branch

A branch with no history in common with the default branch, such as
gh-pages, has no merge-base with it. `git merge-base` then exits 1, which
the pre-push base selection treated as a failure, so the push printed as
not reviewed where it was reviewed against the remote's commit before.
Exit status 1 now means "no common ancestor", and the remote's commit
stays the base.

The amend case of the regression test now also checks that a patch
against the remote's commit holds only the change to it, and that a
patch against a merge-base adds the whole file.

Both found by a Proofline review of this change.
A push to the default branch itself compared its tip with this
checkout's tracking ref for that branch. When the remote's default branch
had been rewound and the push restored the commit the stale tracking ref
still held, the merge-base was the pushed commit, so the hook found
nothing to review and let the push through even with onIncomplete=block.
The hook now keeps the destination ref from Git's input, and an update of
the remote's default branch is compared with the commit the remote
advertises, as before this change.

The base-selection tests now push a branch that is not checked out, so a
base read from HEAD instead of the pushed commit fails them.

Both found by a second Proofline review of this change.
The merge-base with the tracking ref of the default branch is only a
better base than the remote's commit when it leaves out commits the
remote already has. A stale tracking ref breaks that. When the remote
rewound a branch and the push restored a commit the tracking ref still
held, the merge-base was the pushed commit itself, so the hook found
nothing to review and let the push through. A destination that is not a
branch, such as refs/for/main, had the same empty patch.

Those pushes now keep the remote's commit as their base, as before this
change: a push of a commit the tracking ref already holds, and a push to
a ref outside refs/heads/. The tests push from a differently named local
branch, so they fail if the hook reads the source ref where it needs the
destination.

Found by a third Proofline review of this change.
When the remote's commit was missing from the checkout and the stale
tracking ref already held the pushed commit, the merge-base fallback
was the pushed commit itself. The hook then compared the commit with
itself, found nothing to review and let the push through even with
onIncomplete=block. Before this change the missing commit surfaced as
a failed diff. It now surfaces as "names no commit in this checkout",
so the push prints as not reviewed and onIncomplete applies.

Found by a fourth Proofline review of this change.
A push to the remote's default branch, or to a destination outside
refs/heads/, is compared only with the commit the remote advertises.
When that commit was missing from the checkout, the selector still fell
back to the merge-base with the stale tracking ref and could review the
wrong commits. Those pushes now print as not reviewed, as the missing
commit did before this change, so onIncomplete applies.

The rewind tests also push a further local commit, whose merge-base
with the tracking ref differs from the pushed commit, so removing either
destination check fails them.

Found by a fifth Proofline review of this change.
The selector fell back to the remote's commit only when the merge-base
with the tracking ref was the pushed commit itself. A different commit
with the same files, such as an empty commit on the default branch's tip
force-pushed over a branch, still produced an empty patch, so a push
that removed the branch's changes found nothing to review. The fallback
now applies whenever the merge-base has the pushed commit's tree.

A missing refs/remotes/<remote>/HEAD is now read as no default branch
before `git symbolic-ref` runs, whatever status it exits with for a
missing ref.

The refs/for/main cases push from a branch that is not the default, so
passing the source ref where the destination belongs fails the tests.

Found by a sixth Proofline review of this change.

This branch has not been deployed

No deployments
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.

1 participant