Skip to content

fix: display a clear message when there are no commits #244

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 5 commits into from
Jul 6, 2025
Merged

Conversation

shenxianpeng
Copy link
Contributor

@shenxianpeng shenxianpeng commented Jul 6, 2025

close #242

Summary by CodeRabbit

  • New Features
    • Improved handling of empty repositories by displaying a clear message when there are no commits.
  • Bug Fixes
    • Enhanced reliability when retrieving commit information in repositories without commits.
  • Tests
    • Added and updated tests to verify correct behavior when a repository has no commits.
  • Style
    • Minor formatting adjustments in configuration comments for consistency.

Copy link
Contributor

coderabbitai bot commented Jul 6, 2025

Walkthrough

A new function was introduced to detect if a Git repository contains any commits. The main commit info retrieval function now checks for existing commits and returns a specific message if none are present. Corresponding unit tests were added and updated to verify this behavior, and minor comment/formatting changes were made in the pre-commit configuration.

Changes

File(s) Change Summary
commit_check/util.py Added has_commits() function; updated get_commit_info() to handle empty repositories.
tests/util_test.py Added tests for has_commits(); updated TestGetCommitInfo for new empty repo logic.
.pre-commit-config.yaml Renamed commented hook ID; minor formatting adjustment in comments.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant get_commit_info
    participant has_commits
    participant cmd_output

    User->>get_commit_info: Call with format_string, sha
    get_commit_info->>has_commits: Check if repo has commits
    alt Repo has commits
        get_commit_info->>cmd_output: Run git log command
        cmd_output-->>get_commit_info: Return commit info
        get_commit_info-->>User: Return commit info
    else Repo has no commits
        get_commit_info-->>User: Return "Repo has no commits yet."
    end
Loading

Assessment against linked issues

Objective Addressed Explanation
Fix error when running commit-check hooks on first commit in empty repo by handling absence of commits (#242)

Poem

In a burrow of code where the Git logs sleep,
A bunny checks commits—no history to reap!
Now if the repo’s empty, there’s nothing to dread,
“No commits yet,” the message is gently spread.
With tests that hop and logic neat,
This patch makes first commits a treat!
🐇✨

Warning

Review ran into problems

🔥 Problems

Check-run timed out after 90 seconds. Some checks/pipelines were still in progress when the timeout was reached. Consider increasing the reviews.tools.github-checks.timeout_ms value in your CodeRabbit configuration to allow more time for checks to complete.


📜 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 d870942 and b22b53f.

📒 Files selected for processing (3)
  • .pre-commit-config.yaml (1 hunks)
  • commit_check/util.py (2 hunks)
  • tests/util_test.py (4 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
tests/util_test.py (1)
commit_check/util.py (2)
  • has_commits (32-45)
  • get_commit_info (47-68)
⏰ 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). (1)
  • GitHub Check: install (3.9, macos-latest)
🔇 Additional comments (8)
commit_check/util.py (2)

32-46: LGTM! Clean and robust implementation.

The has_commits() function correctly uses git rev-parse --verify HEAD to check for commits existence. The implementation properly handles the case where no commits exist by catching CalledProcessError and returning False. Using subprocess.DEVNULL for stdout/stderr suppression is appropriate for this check.


59-60: Good early return pattern implementation.

The modification to get_commit_info() correctly implements an early return when no commits exist, preventing the execution of git log commands that would fail. The message "Repo has no commits yet." is clear and informative.

.pre-commit-config.yaml (1)

44-45: Comment updates look good.

The hook ID naming updates in the comments improve consistency with the actual hook naming conventions. These changes don't affect functionality since they're commented out.

tests/util_test.py (5)

4-4: LGTM! Proper import addition.

The import for has_commits is correctly added to support the new test cases.


50-86: Excellent test coverage for the new function.

The TestHasCommits class provides comprehensive test coverage:

  • Tests the success case when git rev-parse --verify HEAD succeeds
  • Tests the failure case when it raises CalledProcessError
  • Properly mocks subprocess.run with correct arguments
  • Verifies the exact command and parameters used
  • Asserts the correct return values

The test implementation is thorough and correctly validates the behavior of the has_commits() function.


119-135: Good integration of mocking for updated function.

The existing test for get_commit_info() is correctly updated to mock has_commits() returning True, ensuring the test still validates the original functionality while accounting for the new dependency.


136-151: Excellent test for the new no-commits scenario.

This test correctly validates the new behavior when no commits exist:

  • Mocks has_commits() to return False
  • Verifies that cmd_output is not called (avoiding unnecessary git commands)
  • Asserts the correct return message "Repo has no commits yet."

This test ensures the early return logic works correctly.


152-176: Properly updated exception handling test.

The exception handling test is correctly updated to mock has_commits() returning True, ensuring it still tests the exception path in get_commit_info() while accounting for the new dependency.

✨ 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 added the bug Something isn't working label Jul 6, 2025
Copy link

codecov bot commented Jul 6, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

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

Additional details and impacted files
@@            Coverage Diff            @@
##              main      #244   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            7         7           
  Lines          290       298    +8     
=========================================
+ Hits           290       298    +8     

☔ 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

sonarqubecloud bot commented Jul 6, 2025

@shenxianpeng shenxianpeng marked this pull request as ready for review July 6, 2025 22:02
@shenxianpeng shenxianpeng requested a review from a team as a code owner July 6, 2025 22:02
@shenxianpeng shenxianpeng changed the title fix: check if there are commit in the current branch fix: display a clear message when there are no commits Jul 6, 2025
@shenxianpeng shenxianpeng merged commit dac2281 into main Jul 6, 2025
27 checks passed
@shenxianpeng shenxianpeng deleted the patch-1 branch July 6, 2025 22:05
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] First commit
1 participant