Skip to content

fix: make sure git repo has commit before checking #253

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 9, 2025

Conversation

shenxianpeng
Copy link
Contributor

@shenxianpeng shenxianpeng commented Jul 9, 2025

closes #251

Summary by CodeRabbit

  • Bug Fixes

    • Improved handling for repositories with no commits by skipping checks and validations to prevent errors and irrelevant warnings.
    • Commit message and signoff validations now bypassed when no commits exist.
  • Tests

    • Updated tests to align with new behavior for repositories without commits and removed redundant mock call count assertions.

@shenxianpeng shenxianpeng added the bug Something isn't working label Jul 9, 2025
Copy link
Contributor

coderabbitai bot commented Jul 9, 2025

Walkthrough

The changes introduce early exit checks using the has_commits() function in several commit-checking modules. If there are no commits in the repository, the relevant functions now immediately return PASS and skip further checks. Test cases are updated to reflect the new behavior and the get_commit_info function is simplified.

Changes

File(s) Change Summary
commit_check/author.py, commit_check/branch.py, commit_check/commit.py Added early exit with PASS in main check functions if has_commits() returns False.
commit_check/util.py Removed explicit no-commits check and message from get_commit_info; now always runs git log.
tests/util_test.py Updated tests: removed mock call count assertions and changed expected output for no commits.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant CheckFunction
    participant Util_has_commits

    User->>CheckFunction: Run check (e.g., check_commit_signoff)
    CheckFunction->>Util_has_commits: has_commits()
    alt No commits
        Util_has_commits-->>CheckFunction: False
        CheckFunction-->>User: Return PASS (skip check)
    else Commits exist
        Util_has_commits-->>CheckFunction: True
        CheckFunction->>CheckFunction: Continue with regular validation
    end
Loading

Assessment against linked issues

Objective Addressed Explanation
Prevent failure of hooks (check-branch, check-author-email, check-commit-signoff) when no commits exist (#251)

Assessment against linked issues: Out-of-scope changes

No out-of-scope changes detected.

Possibly related PRs

Poem

In a repo so new, not a commit in sight,
The checks now pass, all is right!
No more false alarms, no need for fright—
The bunny hops on, code shining bright.
🐇✨


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 5f666ab and e4bdd6a.

📒 Files selected for processing (3)
  • commit_check/author.py (1 hunks)
  • commit_check/branch.py (2 hunks)
  • commit_check/commit.py (3 hunks)
🚧 Files skipped from review as they are similar to previous changes (3)
  • commit_check/branch.py
  • commit_check/author.py
  • commit_check/commit.py
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (5)
  • GitHub Check: build
  • GitHub Check: docs
  • GitHub Check: Run benchmarks
  • GitHub Check: Analyze (actions)
  • GitHub Check: Analyze (python)
✨ Finishing Touches
  • 📝 Generate Docstrings

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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@shenxianpeng shenxianpeng marked this pull request as ready for review July 9, 2025 21:57
@shenxianpeng shenxianpeng requested a review from a team as a code owner July 9, 2025 21:57
Copy link

codecov bot commented Jul 9, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (cf9d245) to head (e4bdd6a).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff            @@
##              main      #253   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            7         7           
  Lines          298       300    +2     
=========================================
+ Hits           298       300    +2     

☔ View full report in Codecov by Sentry.
📢 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.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
commit_check/commit.py (1)

58-63: Consider consistent behavior with other check functions.

The function correctly handles empty repositories by returning PASS early, but it's the only one that prints a warning message. Other check functions (check_commit_msg, check_author, check_merge_base) silently return PASS without any output.

For consistency, consider either:

  1. Removing the warning message to match other functions
  2. Adding similar warning messages to other check functions

Current implementation:

if has_commits() is False:
    print(
        f"{YELLOW}No commits found in repository. Skipping commit signoff check.{RESET_COLOR}",
    )
    return PASS

Option 1 (silent like others):

-    if has_commits() is False:
-        print(
-            f"{YELLOW}No commits found in repository. Skipping commit signoff check.{RESET_COLOR}",
-        )
-        return PASS
+    if has_commits() is False:
+        return PASS
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between cf9d245 and 94c58a9.

📒 Files selected for processing (5)
  • commit_check/author.py (1 hunks)
  • commit_check/branch.py (2 hunks)
  • commit_check/commit.py (3 hunks)
  • commit_check/util.py (0 hunks)
  • tests/util_test.py (2 hunks)
💤 Files with no reviewable changes (1)
  • commit_check/util.py
🧰 Additional context used
🧬 Code Graph Analysis (4)
commit_check/author.py (1)
commit_check/util.py (4)
  • has_commits (33-46)
  • print_error_header (128-144)
  • print_error_message (147-159)
  • print_suggestion (162-173)
commit_check/branch.py (1)
commit_check/util.py (1)
  • has_commits (33-46)
tests/util_test.py (1)
commit_check/util.py (1)
  • get_commit_info (48-67)
commit_check/commit.py (1)
commit_check/util.py (1)
  • has_commits (33-46)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
  • GitHub Check: build
  • GitHub Check: Run benchmarks
🔇 Additional comments (8)
commit_check/author.py (2)

4-4: LGTM: Import addition is correct.

The has_commits function is properly imported from the util module and will be used for the early exit check.


8-10: LGTM: Early exit condition handles empty repositories correctly.

The check properly returns PASS when there are no commits, which is the expected behavior since author validation is not applicable to empty repositories. The pattern is consistent with similar changes in other modules.

commit_check/branch.py (2)

4-4: LGTM: Import addition is correct.

The has_commits function is properly imported from the util module and will be used for the early exit check.


36-38: LGTM: Early exit condition prevents merge base check on empty repositories.

The check correctly returns PASS when there are no commits, which is appropriate since merge base validation cannot be performed on empty repositories. This prevents potential errors when running git merge-base commands.

tests/util_test.py (2)

126-126: LGTM: Mock simplification improves test readability.

Removing the variable assignments for mocked functions that aren't being asserted on later is a good cleanup. The mocks are still being set up properly for the test scenarios.

Also applies to: 144-144, 148-148, 160-160


154-154: LGTM: Expected return value aligns with simplified get_commit_info behavior.

The change from "Repo has no commits yet." to " fake commit message " reflects that the get_commit_info function was simplified and no longer handles the no-commits case internally. This is consistent with the PR's approach of handling empty repositories at the calling function level.

commit_check/commit.py (2)

5-5: LGTM: Import addition is correct.

The has_commits function is properly imported from the util module and will be used for the early exit checks.


26-28: LGTM: Early exit condition prevents commit message check on empty repositories.

The check correctly returns PASS when there are no commits, which is appropriate since commit message validation cannot be performed on empty repositories. This prevents potential errors when trying to read commit messages.

Copy link

codspeed-hq bot commented Jul 9, 2025

CodSpeed Performance Report

Merging #253 will degrade performances by 21.9%

Comparing bugfix/check-email (e4bdd6a) with main (cf9d245)

Summary

⚡ 4 improvements
❌ 15 regressions
✅ 74 untouched benchmarks

⚠️ Please fix the performance issues or acknowledge them on CodSpeed.

Benchmarks breakdown

Benchmark BASE HEAD Change
test_check_author 1.4 ms 1.6 ms -13.23%
test_check_author_with_different_check 1.3 ms 1.5 ms -14.02%
test_check_author_with_empty_checks 1.3 ms 1.5 ms -14.07%
test_check_author_with_len0_regex 1.4 ms 1.6 ms -13.27%
test_check_author 1.4 ms 1.6 ms -15.14%
test_check_author_with_accented_letters 1.4 ms 1.6 ms -13.35%
test_check_author_with_different_check 1.3 ms 1.5 ms -13.93%
test_check_author_with_empty_checks 1.3 ms 1.5 ms -13.81%
test_check_author_with_len0_regex 1.4 ms 1.6 ms -13.21%
test_check_merge_base_with_different_check 760 µs 971.9 µs -21.8%
test_check_merge_base_with_empty_checks 758.6 µs 971.4 µs -21.9%
test_check_merge_base_with_empty_regex 773.5 µs 983.6 µs -21.36%
test_check_commit_msg_no_commit_msg_file 1.4 ms 1.6 ms -13.15%
test_check_commit_signoff_with_empty_checks 1.1 ms 1.3 ms -12.34%
test_check_commit_signoff_with_empty_regex 1.1 ms 1.3 ms -12.18%
test_check_commit_with_different_check 1.7 ms 1.5 ms +10.71%
test_check_commit_with_empty_checks 1.7 ms 1.5 ms +10.81%
test_check_commit_with_len0_regex 1.7 ms 1.5 ms +10.15%
test_read_commit_msg_file_not_found 1.7 ms 1.4 ms +23.21%

Copy link

sonarqubecloud bot commented Jul 9, 2025

@shenxianpeng shenxianpeng merged commit 55b000f into main Jul 9, 2025
28 of 29 checks passed
@shenxianpeng shenxianpeng deleted the bugfix/check-email branch July 9, 2025 22:08
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.

[BUG] It should not failed hook when no commits yet
1 participant