← 输入框|github.com|刷新
Skip to content

fix: check every commit of a forced push over unfetched commits - #703

Merged
shenxianpeng merged 2 commits into
mainfrom
fix/push-range-fallback
Nov 17, 2026
Merged

shenxianpeng merged 2 commits into
mainfrom
fix/push-range-fallback

Conversation

@shenxianpeng

@shenxianpeng shenxianpeng commented Nov 17, 2026 •

Copy link
Copy Markdown
Member

Before: A forced push over commits the clone never fetched (someone else, or a bot, pushed to the branch in between) checked only the last commit of the push. The pre-push hook is given the remote's current sha, the clone does not have that object, so the pushed range could not be read and get_push_commits fell back to the tip. A file added by an earlier commit of the same push went through:

$ git log --oneline origin/main..HEAD
7c4a8b6 feat: bump x
848080d chore: add deploy key        # adds keys.pem
$ # pre-push line for `git push --force`; the remote tip was never fetched
$ printf 'refs/heads/main %s refs/heads/main %s\n' "$TIP" "$REMOTE_TIP" \
    | commit-check --files --files-prohibited-patterns '*.pem'
$ echo $?
0

After: The same push fails on the earlier commit:

✖ CC303 file-pattern failed: keys.pem (pattern *.pem)
  File path matches a prohibited pattern
  Suggest: Remove the file from the commit (rotate any committed secrets), or adjust prohibited_patterns in the [files] config
  Docs:    https://commit-check.com/rules/#cc303
$ echo $?
1

When the range from the remote's sha cannot be read, the commits no remote-tracking ref reaches are checked instead of the tip alone.

How: get_push_commits tries <remote>..<local> first, as before. If git cannot resolve it, it asks for <local> --not --remotes, the same set a new branch is already checked against. The tip alone stays as the last resort when neither can be computed. An ordinary push is unchanged and costs no extra git call; the second call is made only after the first one fails.

This applies to everything that reads the pushed range: the [files] rules (CC302, CC303, CC304) and the tag check on pre-commit's push stage. One side effect in the same case: forcing a branch back to a commit the remote already has now carries nothing to check, where it used to re-check that old commit.

Tests: a real-git test for the forced push (both commits are returned, and a push with nothing new returns none); the existing fallback test still covers the last resort. Two TestFilesValidator pre-push tests passed made-up shas to real git and relied on that fallback; they now state the pushed range themselves and start no git process, which is also why their benchmarks no longer move with this change.

Summary by CodeRabbit

  • Bug Fixes
    • Push checks now more reliably identify commits included in a push, including when the remote commit is unavailable locally.
    • Earlier commits in a push are included in checks, while pushes that match the remote’s existing base correctly report no new commits.

A forced push over commits this clone never fetched hands the pre-push
hook a remote sha the clone lacks. The pushed range cannot be read from
it, and only the tip was checked: a file added by an earlier commit of
the same push went through.

The commits no remote-tracking ref reaches are now checked in that case,
the same set a new branch is checked against. The tip alone remains the
last resort when that cannot be computed either.
@shenxianpeng
shenxianpeng requested a review from a team as a code owner November 17, 2026 11:11
@coderabbitai

coderabbitai Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

get_push_commits retries an unresolved remote commit range using commits not reachable from remote-tracking refs. Tests cover this case and use tip-only results for pre-push file-validation assertions.

Changes: Push commit range discovery

Layer / File(s) Summary
Commit range fallback and validation
commit_check/util.py, tests/util_test.py, tests/engine_test.py
get_push_commits retries an unresolved range and returns the first computed list, or the local tip if all attempts fail. A real-Git test covers an unknown remote SHA. Engine tests use a helper that returns only the pushed tip.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Merge Risk: Minimal · up to 5a73d

A forced push over unfetched commits now has every new commit checked instead of only the tip. Ordinary pushes behave as before. In multi-remote setups the client-side check can miss commits that another remote-tracking ref already reaches, which is an accepted limitation of the hook. No blocking risk remains.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: when a forced push references a remote SHA the clone cannot resolve, the code now checks every commit of the push instead of only the tip. The raw summary confirms tha…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Full details: Title check

Explanation

The title names the main change: when a forced push references a remote SHA the clone cannot resolve, the code now checks every commit of the push instead of only the tip. The raw summary confirms that get_push_commits tries the remote-unreached range as a fallback, and the tests cover that behavior. The title is specific and concise and matches the changeset.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: f956c650-af2d-4c3d-a172-edf93d897e5d
Commits

Reviewing files that changed from the base of the PR and between 56b449a and 5a73d5c.

Files selected for processing: 3

  • commit_check/util.py
  • tests/engine_test.py
  • tests/util_test.py

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

@github-actions github-actions Bot added the bug Something isn't working label Nov 17, 2026
@codecov

codecov Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (56b449a) to head (5a73d5c).

Additional details and impacted files
@@            Coverage Diff            @@
##              main      #703   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           15        15           
  Lines         2705      2707    +2     
  Branches       536       537    +1     
=========================================
+ Hits          2705      2707    +2     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@codspeed

codspeed Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Merging this PR will improve performance by 65.89%

2 warnings

⚠️ 136 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

⚠️ 2 benchmarks with a significant change ran on different hardware

Their base and head results were measured on machines with a different CPU, core count or memory, so the changes might not come from the code. Run base and head on the same runner type.

Learn more about Macro Runners

See affected benchmarks

⚡ 2 improved benchmarks
✅ 683 untouched benchmarks
⏩ 131 skipped benchmarks1

Performance Changes

Benchmark BASE HEAD Efficiency
⚡ test_pre_push_stdin_validates_every_pushed_ref 2.6 ms 1.4 ms +82.31%
⚡ test_pre_push_stdin_validates_the_pushed_sha 2.2 ms 1.4 ms +50.95%

Tip

Curious why performance improved? Comment @codspeedbot explain why performance improved on this PR, or directly use the CodSpeed MCP with your agent.


Comparing fix/push-range-fallback (5a73d5c) with main (914d74b)2

Open in CodSpeed

Footnotes

  1. 131 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

  2. No successful run was found on main (56b449a) during the generation of this report, so 914d74b was used instead as the comparison base. There might be some changes unrelated to this pull request in this report. ↩

Both tests passed made-up shas through the pre-push line and left
get_push_commits unpatched, so git itself was asked about commits that
do not exist. They now state the range, as the first test of the class
does, and no longer start a git process.
@sonarqubecloud

Copy link
Copy Markdown

@shenxianpeng
shenxianpeng merged commit 49a0b99 into main Nov 17, 2026
35 checks passed
@shenxianpeng
shenxianpeng deleted the fix/push-range-fallback branch November 17, 2026 15:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant