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

fix: exit 2 when git cannot read the repository - #654

Merged
shenxianpeng merged 1 commit into
mainfrom
fix/fail-on-unreadable-repo
Nov 4, 2026
Merged

shenxianpeng merged 1 commit into
mainfrom
fix/fail-on-unreadable-repo

Conversation

@shenxianpeng

@shenxianpeng shenxianpeng commented Nov 4, 2026 •

Copy link
Copy Markdown
Member

Summary

Sometimes git can't read the directory at all: it isn't a repository, or git rejects it for "dubious ownership". That second case is common when a checkout is mounted into a container or runs on a Jenkins or self-hosted agent. Every git call then fails, and each check took that to mean there was nothing to check. The run listed every check as skipped and exited 0, so CI went green without checking anything. In the same place, --rev already exited 2.

Now, when a requested check needs git, the CLI first asks git once whether it can read the repository. If git can't, the CLI prints git's own reason (which includes the safe.directory hint for dubious ownership) and exits 2, the same code the CLI uses for a broken setup.

These still work without a repository:

  • a message file or a piped message (-m msg.txt, echo … | commit-check -m)
  • a branch name or author piped in on stdin
  • a repository with no commits yet, whose checks are still skipped as before

Before vs after

$ cd /src            # checkout owned by another user, e.g. mounted into a container
$ commit-check -m -b --conventional-branch=true --compact; echo $?

Before (2.19.4):

⊘ skipped (not validated): message, subject-max-length, subject-min-length, branch
0

After:

Error: git cannot read a repository here, so nothing can be checked:
fatal: detected dubious ownership in repository at '/src'
To add an exception for this directory, call:

	git config --global --add safe.directory /src
2
Run Before After
-m, -b, -n, -e, -t, -f, --no-force-push in a directory git refuses skipped, exit 0 git's reason, exit 2
--rev HEAD outside a repository --rev 'HEAD' does not resolve, exit 2 git's reason, exit 2
-m msg.txt, or a piped message, outside a repository checked checked (unchanged)
echo bad_branch | commit-check -b outside a repository checked checked (unchanged)
repository with no commits skipped, exit 0 skipped, exit 0 (unchanged)

Changes

  • commit_check/util.py: git_repository_error() runs git rev-parse --git-dir and returns git's message, or None when git can read the repository.
  • commit_check/main.py: _reads_repository() decides whether the requested checks need git. If they do and git can't read the repository, the CLI exits 2 with git's reason. --rev runs the same check before resolving the revision. The --help exit-code text mentions this case.
  • README.md: the exit-code line covers it too.
  • Tests: every git-reading flag outside a repository, git's reason printed as given, the inputs that need no repository, a piped message next to -b, and the helper's four outcomes.

Testing

  • pytest: 1464 pass. The one failure, test_load_config_file_permission_error, happens only because the tests ran as root locally. It is unrelated.
  • Coverage stays at 100% lines and branches, and ruff check / ruff format --check are clean.
  • By hand: a copy of a repository owned by nobody, run as root (dubious ownership), and /tmp (not a repository).

Outside a repository, or in one git refuses for dubious ownership (a
mounted checkout in a container, say), every git call failed and each
check read that as nothing to check. The run reported every check as
skipped and exited 0, so CI went green having checked nothing; --rev
already exited 2 in the same place.

When a requested check has to read git, ask git once whether it can and,
if not, print its reason and exit 2. A message file, a piped message and
a piped branch or author still need no repository, and a repository with
no commits is still readable.
@shenxianpeng
shenxianpeng requested a review from a team as a code owner November 4, 2026 16:47
@coderabbitai

coderabbitai Bot commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 55 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

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: 5a786a80-dd20-4e56-a833-93a24051feff
Commits

Reviewing files that changed from the base of the PR and between 7eec0ac and f001638.

Files selected for processing: 5
  • README.md
  • commit_check/main.py
  • commit_check/util.py
  • tests/main_test.py
  • tests/util_test.py
  • 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

@shenxianpeng shenxianpeng self-assigned this Nov 4, 2026
@github-actions github-actions Bot added the bug Something isn't working label Nov 4, 2026
@sonarqubecloud

sonarqubecloud Bot commented Nov 4, 2026

Copy link
Copy Markdown

@codecov

codecov Bot commented Nov 4, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (7eec0ac) to head (f001638).

Additional details and impacted files
@@            Coverage Diff            @@
##              main      #654   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           14        14           
  Lines         2429      2456   +27     
  Branches       470       477    +7     
=========================================
+ Hits          2429      2456   +27     

☔ 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 4, 2026

Copy link
Copy Markdown

Merging this PR will degrade performance by 10.76%

3 warnings

⚠️ 130 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

⚠️ 1 benchmark with a significant change ran on different hardware

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

Learn more about Macro Runners

See affected benchmarks

⚠️ 1 benchmark with a significant change ran in a different software environment

Its base and head results were measured with a different OS or toolchain, such as compiler, runtime or library versions, so the change might come from this difference rather than from the code. Open the benchmark to see what changed.

See affected benchmarks

❌ 1 regressed benchmark
✅ 652 untouched benchmarks
⏩ 126 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Benchmark BASE HEAD Efficiency
❌ test_no_force_push_pre_commit_env_fetches_remote_sha 11.4 ms 12.7 ms -10.76%

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing fix/fail-on-unreadable-repo (f001638) with main (7eec0ac)

Open in CodSpeed

Footnotes

  1. 126 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. ↩

@shenxianpeng
shenxianpeng merged commit 9c07c12 into main Nov 4, 2026
64 of 71 checks passed
@shenxianpeng
shenxianpeng deleted the fix/fail-on-unreadable-repo branch November 4, 2026 22:31
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