Skip to content

fix(hooks): fall back to default-branch base on force-push - #117

Open
akacolf wants to merge 2 commits into
prooflinedev:mainfrom
akacolf:fix/pre-push-force-push-base
Open

akacolf wants to merge 2 commits into
prooflinedev:mainfrom
akacolf:fix/pre-push-force-push-base

Conversation

@akacolf

@akacolf akacolf commented Nov 29, 2026

Copy link
Copy Markdown

Problem

In src/hooks.rs, Hook::PrePush previously used pushed.remote_sha unconditionally as the diff base whenever it was non-zero.

When a developer force-pushes after rebasing onto an updated target branch (or amending a commit), remote_sha is no longer an ancestor of local_sha:

  • git diff <remote_sha> <local_sha> diffs the stale pre-rebase tip against the rebased tip, uploading unrelated commits that landed on the target branch (or hitting patch size/file limits).
  • If remote_sha is absent from the local object store (e.g., shallow clone or an un-fetched remote tip overwritten elsewhere), git diff fails and the push is reported as Unreviewed.

Solution

  • Added git::is_ancestor(dir, ancestor, descendant) in src/git.rs using git merge-base --is-ancestor, returning Ok(true) on exit 0, Ok(false) on exit 1 (not an ancestor) or on exit 128 only when descendant resolves and ancestor is missing locally (GitError::UnknownRevision), and Err(GitError::Failed | GitError::NotSha | GitError::Unavailable) otherwise.
  • Updated hooks::subjects in src/hooks.rs so non-zero remote_sha values are only used as the diff base when git::is_ancestor(here, &pushed.remote_sha, &pushed.local_sha) returns Ok(true). On Ok(false), it falls back to git::new_branch_base(here, remote, &pushed.local_sha) (the merge-base with refs/remotes/<remote>/HEAD), matching new-branch behavior and keeping fast-forward pushes unchanged.

Verification

  • Added unit test git::tests::is_ancestor_distinguishes_linear_diverged_and_missing_commits in src/git.rs.
  • Added integration test force_push_of_rebased_or_missing_remote_tip_falls_back_to_default_branch_base in tests/hooks.rs.
  • cargo fmt --check — passed.
  • cargo clippy --locked --all-targets -- -D warnings — passed.
  • cargo test --locked --lib git::tests — 5 passed, 0 failed.
  • cargo test --locked --test hooks — 5 passed, 0 failed, 1 ignored (the_pre_commit_framework_preserves_selection_policy_and_existing_configuration, requires pre-commit >= 3.2.0).

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