-
-
Notifications
You must be signed in to change notification settings - Fork 8
fix: use git branch --show-current
to get branch name
#252
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
Conversation
WalkthroughThe Changes
Sequence Diagram(s)sequenceDiagram
participant User
participant get_branch_name()
participant Git
User->>get_branch_name(): Call function
get_branch_name()->>Git: Run 'git branch --show-current'
Git-->>get_branch_name(): Return branch name or error
get_branch_name()-->>User: Return branch name or None
Assessment against linked issues
Assessment against linked issues: Out-of-scope changesNo out-of-scope changes found. Poem
📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
⏰ 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)
✨ Finishing Touches
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. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed 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)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
commit_check/util.py
(1 hunks)tests/util_test.py
(2 hunks)
⏰ 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: Run benchmarks
🔇 Additional comments (3)
commit_check/util.py (1)
16-30
: Good documentation and error handling maintained.The docstring appropriately documents the Git version requirement and provides a link for users to report issues. The error handling logic remains robust with proper exception handling for
CalledProcessError
.tests/util_test.py (2)
27-29
: Test properly updated for new Git command.The test assertion correctly expects the new
git branch --show-current
command. The test structure and expected behavior remain consistent.
47-49
: Exception handling test properly updated.The test for exception handling correctly expects the new Git command while maintaining the same error handling logic and assertions.
# Git 2.22 and above supports `git branch --show-current` | ||
commands = ['git', 'branch', '--show-current'] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💡 Verification agent
🧩 Analysis chain
Verify compatibility and behavioral differences with the new Git command.
The change from git rev-parse --abbrev-ref HEAD
to git branch --show-current
introduces a Git version dependency (2.22+) and potential behavioral differences:
- Git version compatibility: Environments with Git < 2.22 will experience failures
- Detached HEAD behavior:
git rev-parse --abbrev-ref HEAD
returns "HEAD" in detached HEAD state, whilegit branch --show-current
returns an empty string
Please verify the behavioral compatibility:
🏁 Script executed:
#!/bin/bash
# Check Git version compatibility and behavioral differences
echo "=== Git Version Check ==="
git --version
echo "=== Testing normal branch behavior ==="
# Create a test repository and branch
mkdir -p /tmp/git-test && cd /tmp/git-test
git init
git config user.email "test@example.com"
git config user.name "Test User"
echo "test" > test.txt
git add test.txt
git commit -m "Initial commit"
git checkout -b feature-branch
echo "Current branch with old command:"
git rev-parse --abbrev-ref HEAD
echo "Current branch with new command:"
git branch --show-current
echo "=== Testing detached HEAD behavior ==="
# Test detached HEAD state
git checkout HEAD~0 2>/dev/null || echo "No previous commits to detach to"
echo "Detached HEAD with old command:"
git rev-parse --abbrev-ref HEAD
echo "Detached HEAD with new command:"
git branch --show-current
# Clean up
cd / && rm -rf /tmp/git-test
Length of output: 1756
Fix Git command fallback for detached HEAD and enforce Git ≥ 2.22
The new git branch --show-current
works as expected on a normal branch but:
- In a detached HEAD state it returns an empty string (whereas
git rev-parse --abbrev-ref HEAD
returns"HEAD"
). - It requires Git 2.22 or newer, so older environments will fail outright.
Please update commit_check/util.py
to:
- Check
git --version
(or otherwise ensure Git ≥ 2.22) before using--show-current
, or - Fall back to the old command when:
- The version is older than 2.22, or
git branch --show-current
yields an empty string
For example:
--- a/commit_check/util.py
+++ b/commit_check/util.py
@@ -23,7 +23,15 @@ def get_branch_name():
"""
- # Git 2.22 and above supports `git branch --show-current`
- commands = ['git', 'branch', '--show-current']
+ # Prefer `--show-current` on Git ≥2.22, but fallback for older versions or detached HEAD
+ def git_version_tuple():
+ v = run_cmd(['git', '--version']).strip().split()[-1]
+ return tuple(int(x) for x in v.split('.')[:2])
+
+ version = git_version_tuple()
+ cmd = ['git', 'branch', '--show-current'] if version >= (2, 22) else ['git', 'rev-parse', '--abbrev-ref', 'HEAD']
+ branch = run_cmd(cmd).strip()
+ if not branch:
+ # detached HEAD or empty result → fall back
+ branch = run_cmd(['git', 'rev-parse', '--abbrev-ref', 'HEAD']).strip()
if not branch:
raise RuntimeError("Unable to determine Git branch name")
- Verify parsing of
git --version
handles major.minor correctly. - Ensure existing error handling remains in place.
- Add tests for detached HEAD to confirm desired behavior.
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
# Git 2.22 and above supports `git branch --show-current` | |
commands = ['git', 'branch', '--show-current'] | |
""" | |
# Prefer `--show-current` on Git ≥2.22, but fallback for older versions or detached HEAD | |
def git_version_tuple(): | |
v = run_cmd(['git', '--version']).strip().split()[-1] | |
return tuple(int(x) for x in v.split('.')[:2]) | |
version = git_version_tuple() | |
cmd = ['git', 'branch', '--show-current'] if version >= (2, 22) else ['git', 'rev-parse', '--abbrev-ref', 'HEAD'] | |
branch = run_cmd(cmd).strip() | |
if not branch: | |
# detached HEAD or empty result → fall back | |
branch = run_cmd(['git', 'rev-parse', '--abbrev-ref', 'HEAD']).strip() | |
if not branch: | |
raise RuntimeError("Unable to determine Git branch name") |
🤖 Prompt for AI Agents
In commit_check/util.py around lines 25-26, the current use of 'git branch
--show-current' does not handle detached HEAD states and requires Git version
2.22 or newer. Modify the code to first check the installed Git version by
running 'git --version' and parsing the major and minor version numbers. Use
'git branch --show-current' only if the version is 2.22 or higher; otherwise,
fall back to 'git rev-parse --abbrev-ref HEAD'. Additionally, if 'git branch
--show-current' returns an empty string (indicating detached HEAD), also fall
back to the older command. Maintain existing error handling and add tests to
verify behavior in detached HEAD states.
CodSpeed Performance ReportMerging #252 will not alter performanceComparing Summary
|
|
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #252 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 7 7
Lines 298 298
=========================================
Hits 298 298 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
closes #249
Summary by CodeRabbit